Calculate after getting the optimal solution

Problems with modeling
Post Reply
sandy12310627
User
User
Posts: 23
Joined: 4 years ago

Calculate after getting the optimal solution

Post by sandy12310627 »

Hi,
I wonder if there is any way to do the easy calculation after I got the optimal solution.
For example,
I would like to use the optimal solution values (e.g., X(1), X(2), .... X(1000)) to calculate some other quantity (e.g., variance of X(1), X(2), .. X(1000)).
This is just a calculation that uses the optimal solution, and its calculation does not have to be included in the objective function or constraint.
I can do it by adding the formula to the formulation as a constraint, but this will make the problem non-linear and the constraints more complicated.
I am wondering if there is any way to solve it in a simple way.

I am appreciated for your help!

Sincerely,
Sandy
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: Calculate after getting the optimal solution

Post by dirkse »

Sandy,

What you ask about is very easy to do.

Outside of a solve statement, a variable is similar to a parameter. You can assign to it, display it and use it in calculations. For example:

* setting a level value is good practice for nonlinear models
x.L(i) = X0(i);
* now solve a model - this changes the value of x
solve m using nlp min z;

* now do some computation
scalar mu 'mean';
mu = sum{i, x.L(i)} / card(i);

The GAMS User's Guide is full of info and is a big read if you go cover to cover, but you should at least digest the Rosenthal tutorial fully. That is not long at all and gives a big-picture idea of how it all works, so that you know about levels and such and that you can do computations with GAMS symbols outside of a model.

https://www.gams.com/latest/docs/UG_Tutorial.html

-Steve
Post Reply