Fixed binary variables

Problems with modeling
Post Reply
martinagherardi
User
User
Posts: 12
Joined: 2 months ago

Fixed binary variables

Post by martinagherardi » 1 week ago

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

User avatar
bussieck
Moderator
Moderator
Posts: 749
Joined: 5 years ago

Re: Fixed binary variables

Post by bussieck » 6 days ago

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

martinagherardi
User
User
Posts: 12
Joined: 2 months ago

Re: Fixed binary variables

Post by martinagherardi » 6 days ago

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

User avatar
bussieck
Moderator
Moderator
Posts: 749
Joined: 5 years ago

Re: Fixed binary variables

Post by bussieck » 5 days ago

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

martinagherardi
User
User
Posts: 12
Joined: 2 months ago

Re: Fixed binary variables

Post by martinagherardi » 5 days ago

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?

Manassaldi
User
User
Posts: 102
Joined: 5 years ago
Location: Rosario - Argentina

Re: Fixed binary variables

Post by Manassaldi » 5 days ago

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

User avatar
bussieck
Moderator
Moderator
Posts: 749
Joined: 5 years ago

Re: Fixed binary variables

Post by bussieck » 4 days ago

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

martinagherardi
User
User
Posts: 12
Joined: 2 months ago

Re: Fixed binary variables

Post by martinagherardi » 4 days ago

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

Post Reply