reduce searching space in GAMS - gams-math

Now,I have the GAMS code like the sample below. The statement binary variables x(i,j) indicates that the GAMS will creat one hundred variables x with index(i,j). How Can I do to let GAMS creats variables x(i,j) only when r(i,j) exists to reduce the number of variable x?
set i nodes /1*10/;
alias (i, j);
parameter r(i,j) factor /1.2 1 ...... 7.8 1/;
binary variables x(i,j);

You need to use $-condition when you use the variables. The declaration
binary variables x(i,j);
itself does not generate any variables. They are created when they are used, e.g., in an equation. Look for example at this dummy equation:
equation dummy(i);
dummy(i).. sum(j, x(i,j)) =l= 5;
This would generate 100 x variables as you said, but now lets modify this using a $-condition:
dummy(i).. sum(j$r(i,j), x(i,j)) =l= 5;
This would create only those x variables where r is not 0. You would have to use the same $-condition wherever x is used.
Hope that helps!
Lutz

Related

In AMPL, how to refer to part of the result, and use them in multiple places

I'm learning AMPL to speed up a model currently in excel spreadsheet with excel solver. It basically based on the matrix multiplication result of a 1 x m variables and an m x n parameters. And it would find the variables to maximize the minimum of certain values in the result while keeping some other values in the same result satisfying a few constraints. How to do so in AMPL?
Given: P= m x n parameters
Variable: X= 1 x m variable we tried to solve
Calculate: R= X x P , result of matrix multiplication of X and P
Maximize: min(R[1..3]), the minimum value of the first 3 values in the result
Subject to: R[2]<R[4]
min(R[6..8])>20
R[5]-20>R[7]
X are all integers
I read several tutorials and look up the manual but can't find the solution to this seemingly straightforward problem. All I found is maximize a single value, which is the calculation result. And it was used only once and does not appear again in the constraint.
The usual approach for "maximize the minimum" problems in products like AMPL is to define an auxiliary variable and set linear constraints that effectively define it as the minimum, converting a nonlinear function (min) into linear rules.
For instance, suppose I have a bunch of decision variables x[i] with i ranging over an index set S, and I want to maximize the minimum over x[i]. AMPL syntax for that would be:
var x_min;
s.t. DefineMinimum{i in S}: x_min <= x[i];
maximize ObjectiveFunction: x_min;
The constraint only requires that x_min be less than or equal to the minimum of x[i]. However, since you're trying to maximize x_min and there are no other constraints on it, it should always end up exactly equal to that minimum (give or take machine-arithmetic epsilon considerations).
If you have parameters (i.e. values are known before you run the optimisation) and want to refer to their minimum, AMPL lets you do that more directly:
param p_min := min{j in IndexSet_P} p[j];
While AMPL also supports this syntax for variables, not all of the solvers used with AMPL are capable of accepting this type of constraint. For instance:
reset;
option solver gecode;
set S := {1,2,3};
var x{S} integer;
var x_min = min{s in S} x[s];
minimize OF: sum{s in S} x[s];
s.t. c1: x_min >= 5;
solve;
This will run and do what you'd expect it to do, because Gecode is programmed to recognise and deal with min-type constraints. However, if you switch the solver option to gurobi or cplex it will fail, since these only accept linear or quadratic constraints. To apply a minimum constraint with those solvers, you need to use something like the linearization trick I discussed above.

GAMS - Unit step function

I need to use the step function in order to count the number of non-zero elements in a parameter. The step function that I am considering is the following:
After searching on the internet for solution, I realized we can create stepwise functions in GAMS, but I need a continuous function for x > 1.
I tried the following code to reproduce a step-like function:
round(1 / (1 + exp(-x)) - 0.01)
which is:
Unfortunately, this formula does not work with GAMS. When I try to run the code, I got this error:
Endogenous function argument(s) not allowed in linear models
I am working with a MIP (Mixed Integer Linear Program) model. Is there a way to use a step function in GAMS?
I assume, that x is a variable in your code? Then you can try something like this (if x would be a parameter, it would be easier):
Equation a, b;
Variable x;
Binary Variable y;
Scalar BigM / 1e3/
SmallM /1e-3/;
a.. y*BigM =g= x;
b.. y*SmallM =l= x;
So, if x=0, y will be 0 as well because of equation b. And if x>0, y will become 1 because of equation a. The BigM you should choose as small as possible and as big as necessary (so it should be the maximum value x can take) and SmallM the other way around. This assumes of course, that there is something like a lower and upper bound for x, if it is not 0...
Hope that helps!
Lutz

GAMS Error: Endogenous function arguments not allowed in linear models

I am trying to solve in GAMS for the binary variables using MIP but am constantly getting an error. I am unable to understand the reason. Anyone got a solution?
Set i cities /1*7/;
Binary variables z1,z2,z3,z4,z5,z6,z7 1 if selected and 0 otherwise ;
variable y ;
Equations con1,con2,con3,con4,con5,con6,con7,obj ;
obj.. y =E= z1*10+z2*6+z3*7+z4*8+z5*13+z6*9+z7*8;
con1.. min(z2*6.1,z3*15.2,z4*17,z5*16.8,z6*8.4,z7*16.6) =L= 10 ;
con2.. min(z1*6.1,z3*7.6,z4*16.0,z5*11.3,z6*2.2,z7*11.9) =L= 10 ;
con3.. min(z1*15.2,z2*7.6,z4*22.0,z5*17.3,z6*10.2,z7*16.7) =L= 10 ;
con4.. min(z1*17.0,z2*16.0,z3*22.0,z5*12.1,z6*14.8,z7*7.2) =L= 10 ;
con5.. min(z1*16.8,z2*11.3,z3*17.3,z4*12.1,z6*9.4,z7*2.9) =L= 10 ;
con6.. min(z1*8.4,z2*2.2,z3*10.2,z4*14.8,z5*9.4,z7*9.2) =L= 10 ;
con7.. min(z1*16.6,z2*11.9,z3*16.7,z4*7.2,z5*2.9,z6*9.2) =L= 10 ;
Model planmall /all/ ;
Solve planmall using MIP minimizing y;
display z1.L,z2.L,z3.L,z4.L,z5.L,z6.L,z7.L,y.L;
If I run your model, I see this in the lst file:
**** 51 Endogenous function argument(s) not allowed in linear models
**** 256 Error(s) in analyzing solve statement. More detail appears
**** Below the solve statement above
**** The following MIP errors were detected in model planmall:
**** 51 equation con1.. the function MIN is called with non-constant arguments
...
So the problem is, that you try to solve a (linear) MIP, but you the function MIN with variables, which is not allowed for this model type. So, one way to resolve this is to change your model type to MINLP like this:
Solve planmall using MINLP minimizing y;
Or, you reformulate your model, so that you do not use the MIN function anymore.

Is the equation below means in a MIP model

As the title mentioned
sum((r,l), Mer.l(e,r) * Mel(e,l)) =e= 0;
I use a GAMS mip model to solve a problem, and Mer(e,r) and Mel(e,l) are both binary variables, and if I did not write the .l suffix, the compile will give Endogenous relational operations require model type "dnlp"" error message, but I am not sure the equation above staying the original meaning that is the Mer(e,r) still a variable? and is it still changeable with the mip solving process?
If you use Mer.l, the model will not use Mer as a variable that gets optimized anymore, but will use the (initial) levels of the variables Mer as constant numbers. But you could reformualte your equation, so that it stays linear. As I understand it, you want to make sure that for each e and each combination of r and l you will never get Mer=1 and Mel=1 (one could be 1 or both should be 0). So you could formulate is as e.g.:
equation e(e,r,l);
e(e,r,l).. Mer(e,r) + Mel(e,l) =l= 1;
I hope that helps,
Lutz

Defining Set Packing using MathProg

For someone with zero experience, this can be very confusing.
How do I define the Set Packing problem as seen in the wikipedia article as a MathProg program, to be later ran in the GLPK tool?
Intution alone would lead me into something like this:
var x
maximize SetPacking :
sum {s in Subsets} x
s.t. ?? //x is an integer 0 or 1
s.t. ?? //amount of x <=1
end;
But its logic is obviously wrong and I can't even finish it.
You can formulate the set packing problem in AMPL (and MathProg which is based on a subset of AMPL) as follows:
set U; # universe
set S; # subsets
set Members{S}; # members of subsets
# every set is either in the set packing or not
var x{S} binary;
# maximize the total number of subsets
maximize NumSets: sum{s in S} x[s];
# selected sets have to be pairwise disjoint
s.t. Disjoint{e in U}: sum{s in S: e in Members[s]} x[s] <= 1;
Note that you have to introduce indexed set Members with each element Members[s]representing members of subset s. This is the only difference from the algebraic formulation.