Page 1 of 1

### Macros containing equations

Posted: Sun Nov 04, 2018 2:03 pm
I have a problem where the distance from upper and lower values of a set of parameters is to be maximized. Let's say the number of parameters is 4, and we know its lower and upper bounds. Then a set of instances is formed where each instance contains a single parameter (to be optimized, i.e. a single decision variable) and all the others are fixed at lower or upper bound. That is, 2*4 instances are formed.

One of the constraints is reused for each of the instances formed wherein each case a single decision variable p is active and all the other parameters are fixed. Does anybody have suggestions about how to write this model in GAMS using a single equation?

I made primary tests using macros of equations, but without much success. The main difficulty is that one decision variable is only a decision variable in an equation and a parameter in all the others for the remaining instances.

Any tips are welcome.

BEL

### Re: Macros containing equations

Posted: Mon Nov 05, 2018 7:07 am
Why don't you send what you have and we can see if we can help/improve on this.

-Michael

### Re: Macros containing equations

Posted: Tue Nov 06, 2018 9:47 am
Hi Michael,

Thank you. I spent some time making the test code readable. Here, I upload it, and it is running for the simplest case where all the equations are discriminated. My goal is to automate the equations eq_low... and eq_up... in 2 different macros where the variables entering the equations are modified from one call to another.

To be fair, I am probably missing the proper syntax to have equations with two or three indices within a macro.

Any tips are welcome. I have been following the information in https://www.gams.com/latest/testlib_ml/ ... cro01.html

### Re: Macros containing equations

Posted: Tue Nov 06, 2018 10:35 am
I still do not understand what you want to do in the end. I speculate that you want to solve for each j a model that has some of the x variables fixed and others free, e.g. fix the x(j) to 0 for the loop index j and leave the others to be determined by the model. I fear that the \$macro approach is not going to help you with something like this. The GAMS "way" is to work with dynamic sets. So you rewrite your equation algebra using a subset jj of j (here only for the first equation):

Code: Select all

``````* Equations listing:
Set jj(j) 'subset of j';
eq_low_p1_low(jj)     ..  pE_b('1')*exp(pE_b('2')*x(jj))-pLow('1')*exp(pLOW_b('2')*x(jj)) =g= eLow;
``````
Noiw when it comes to solving you need to iterate over j and adjust the subset j and what you want to do with your variables x. Here is the loop for the example I explained above. We save the results of the model we are interested in a parameter rep:

Code: Select all

``````parameter rep(j,i,*);
jj(j) = yes;
loop(j,
x.up(j) = 0;
solve orthot using nlp maximizing ort;
x.up(j) = 1; # restore the original bound
jj(j) = yes; # restore the original set
rep(j,i,'pLow') = pLow.l(i);
rep(j,i,'pUp') = pUp.l(i);
)
display rep;
``````
I have attached a running version of your model.

-Michael

### Re: Macros containing equations

Posted: Wed Nov 07, 2018 10:04 am
Thank you again. This problem is the lower-level program of a bilevel one where at the upper level we optimize the x variables. First I am trying to automate the lower level one, and here I have at the beginning np parameters which are expanded to a set of 2*np combinations of parameters. That is, with this approximation we will produce a set of 2*np possible points in the domain of the parameters and 2*np^2 variables as each point is formed by estimates for parameters and estimates for their lower and upper bounds. Then, the same function of the parameters for each combination is limited from below and above and we end with the 2*np constraints of <= type and 2*np constraints of >= type. The functions in each constraint are equal, only the variables (combinations of possible parameters) in each of the 2*np points change.

So the idea is reusing the same equation wroten as a macro, and invoking it 2*np times, one for each set of variables. Then, the problem is to be solved at once include the complete set of 4*np constraints. For np=2 one can write the equations for each of the combinations one by one but in case np is 8 we will end up with 32 constraints with the same function. Anyway, in case you see another way (that does not include macros let me know). I can try.

BEL

### Re: Macros containing equations

Posted: Mon Nov 12, 2018 10:52 am
I still have no clue of what you try to do. Perhaps someone can chime in. Or just make a simple example or something that I can understand the underlying problem. Moreover, have you looked at the EMP framework to formulate bilevel optimization problem (see https://www.gams.com/latest/docs/UG_EMP_Bilevel.html)?

-Michael

### Re: Macros containing equations

Posted: Tue Dec 18, 2018 10:38 am
I have been trying to use \$macro commend instead of repeating the lines (attached file/snipping shot) 23 times. But I cannot make it work.

What I need to do is the following:
a. first I recalculate a sector using some variables,
b. than I save recalculated values in a variable,
c. I clean the variables, but keep only the variable with recalculated values;
d. I repeat the procedure again for a second sector (with different assumptions),

The only step that is repeating is c.