Page 1 of 1

Fixed binary variables

Posted: Thu May 12, 2022 1:12 pm
by martinagherardi
Hi everyone,

How can I arrange the GAMS code in order to do the following step?

1. Solve MIP problem
2. Fix binaries to the values obtained in 1.
3. Solve LP problem

With the binary variable alfa, I tried this

solve Model using MIP minimizing z;

loop ((k,time),
alfa.fx(k,time)=alfa.l(k,time) ;
);

solve Model using LP minimizing z;


but it doesn't work and I obtained the following error:
equation potenza.. discrete variable "alfa" not allowed

How can I fix the binary variable to the values obtained and use it as a parameter? (changing the name in all equations would take considerable time)

Thank you in advance

Re: Fixed binary variables

Posted: Fri May 13, 2022 9:17 am
by bussieck
First of all, what you try to do on the GAMS level, most GAMS MIP solvers do this automatically (in order to get duals from the LP with fixed discrete variables). So there should be no need to do that on the GAMS level. If there is more to this and you still need to do it, you need to solve the model not as an LP but an RMIP (relaxed MIP).

-Michael

Re: Fixed binary variables

Posted: Fri May 13, 2022 11:29 am
by martinagherardi
Thank you Michael.

Yes, there is more:
I have a 2 stages model.
After step 1 (first resolution of the MIP), I want to fix the values of some continuous variables (the variables of the first stage) and the values of all the binaries to the optimal values obtained in Step 1 in order to ask for the extension .m (dual variable of one constraint considering only the second stage).

Given your suggestion,
does the code do exactly what I want if I
1. Solve MIP problem
2. Fix continuous variables of my interest and binaries to the values obtained in 1. with the extensions .fx = .l
3. Solve RMIP problem
?

I obtained a strange error using both MIP and RMIP in Step 3 (In Step 1 there are no errors):
**** Matrix error - bounds on discrete variables have to be integer
alfa(GT_ZN_1,18) (.LO, .L, .UP = 1.13654149850481E-8, 1.13654149850481E-8, 1.13654149850481E-8)
**** Matrix error - bounds on discrete variables have to be integer
gamma(GT_ZN_1,18) (.LO, .L, .UP = 1.38632231992955E-8, 1.38632231992955E-8, 1.38632231992955E-8)


Do you know why in Step 1 I obtained those small values instead of 0? (alfa and gamma are defined as binaries).

Many thanks

Re: Fixed binary variables

Posted: Sat May 14, 2022 8:11 am
by bussieck
All solvers work with tolerances. There is a tolerance when a variable value is considered integer (see e.g. https://www.gams.com/latest/docs/S_CPLE ... CPLEXepint). You can play around setting this to 0 (for some solvers), but you can also just round (and hope for the best): x.fx(i,j) = round(x.l(i,j));

-Michael

Re: Fixed binary variables

Posted: Sat May 14, 2022 8:36 am
by martinagherardi
I’m using an if statement that sets all those binaries to 0 if are <0.1 but I can try with your method ☺️


May I ask a closely related question?

Are there instructions for change the declaration of a variable into a parameter?
I want to solve a problem itaratively, where I have the bilinear term a*b:
1. Fix a to a feasible value
2. Solve the model: find b
3. Solve the model with b fixed: find a
4. Iteration until convergence.

If I use .fix=.l, the problem is still bilinear and not MIP because GAMS sees the fixed variable as a variable instead of as a parameter.
If I fix the variables with different parameters (with different names), I have to declare all model equations twice and change all the names.

Do you know a more efficient method?

Re: Fixed binary variables

Posted: Sat May 14, 2022 2:13 pm
by Manassaldi
Hi, you can try this simple implementation:

alfa.fx(k,time)$(alfa.l(k,time) ge 0.99)=1 ;
alfa.fx(k,time)$(alfa.l(k,time) le 0.01)=0 ;

best

Re: Fixed binary variables

Posted: Sun May 15, 2022 8:36 am
by bussieck
I’m using an if statement that sets all those binaries to 0 if are <0.1 but I can try with your method ☺️
That seems not to work since your variable alfa(GT_ZN_1,18) has been fixed to 1.386...e-8...

You cannot change the type of a symbol. But here is a trick that you can do: Use holdFixed (https://www.gams.com/latest/docs/UG_Gam ... Oholdfixed) and tryLinear (https://www.gams.com/latest/docs/UG_Gam ... Otrylinear). So you have bilinear term x*y and you need to solve your model as an MIQCP, but if you do:

Code: Select all

mymodel.holdFixed = 1;
mymodel.tryLinear = 1;
x.l=123;
while(someCondition,
  ...
  x.fx = x.l; // or round(x.l)
  y.lo = 0; y.up = 1000;
  solve mymodel using miqcp min obj
  x.lo = 0; x.up = 1000;
  y.fx = y.l; // or round(x.l)
  solve mymodel using miqcp min obj
  ...
)

GAMS recognizes that each model instance solved by the "solve" statement is indeed is linear and gives it to the selected MIP (not MIQCP) solver.

-Michael

Re: Fixed binary variables

Posted: Sun May 15, 2022 12:30 pm
by martinagherardi
The method (if<0.1, set binary to 0) works because 1.386...e-8 is equal to 0.00000001 and hence <0.1
but round is still correct because the nearest integer value is 0.

Your suggestion for the iterative model works perfectly!!!
Thanks again, I couldn't have pulled this off without you.

Martina