Variable in subscript Error

52 views
Skip to first unread message

ramz

unread,
Sep 18, 2016, 10:15:49 PM9/18/16
to AMPL Modeling Language
Hi 

I am trying to write an AMPL code to minimize the number of boxes to be filled with balls of different colors (boxes are of 3 types Type 1, 2 & 3). Each type of box has a different mixture/proportion of ball colors (as given in Box.dat) . Each box can have maximum weight of 12000. Each color ball ( different objects in each color) has a weight and quantity associated. Total weight of all the balls is 308000. Please find the Box.mod and Box.dat attached..

There are three sets as below:

set box_type ordered;
set color;
set obj;

param weight{c in color, o in obj};
param Qty{c in color, o in obj};
param prop{b in box_type, c in color};
param num{b in box_type};

var v{b in box_type, i in 1..num[b], c in color, o in obj} >= 0;

minimize OF: sum{b in box_type} num[b];

The above code works however, only if I specify 'num' as a parameter and not a variable. I want to specify num{b in box_type} as a variable. If I specify 'num' as a variable:

var num{b in box_type}  >= 0. 

I get an error 'variable in subscript'. Both the variables are integer variables but not binary. The question was to minimize the total number of boxes, which I assumed as a parameter itself as I could not specify it as a variable here. How can I reformulate my model?

Thank you.
Box.dat
Box.mod

Robert Fourer

unread,
Sep 19, 2016, 1:03:06 PM9/19/16
to am...@googlegroups.com
If you define

var v{b in box_type, i in 1..num[b], c in color, o in obj} >= 0;

then the values num[b] cannot be variables; this is because, if num[b] is a variable, then the number of variables v[b,i,c,o] is unknown and AMPL cannot determine the problem to be sent to the solver. Instead this kind of problem is often formulated using variables like

var Use {b in box_type, i in 1..num[b]} binary;

where num[b] is the largest number of boxes of type b that could possibly be used, and where Use[b,i] = 1 if the ith box of type b is used, and = 0 otherwise. The sum of all the Use[b,i] variables gives the number of boxes used, and some constraints have to be added to force the appropriate v-variables to be zero when Use[b,i] is zero. Also to speed up the solution process it can help to include constraints Use[b,i+1] <= Use[b,i] which rule out a lot of redundant solutions.

Bob Fourer
am...@googlegroups.com

=======

Ramz

unread,
Sep 22, 2016, 11:04:55 PM9/22/16
to AMPL Modeling Language, 4...@ampl.com
Thank you Bob. Your suggestions have really helped me solve the problem! 

I have introduced a new binary variable 
var use{b in box_type, i in 1..num[b]} binary;

and added the below constraints:
subject to const05{b in box_type, i in 1..(num[b]-1)}: use[b,i+1] <= use[b,i];

subject to const06{b in box_type, i in 1..num[b]}: use[b,i] = 0 ==> sum{c in color, o in obj} v[b,i,c,o] = 0;

Also changed the objective to 
minimize OF: sum{b in box_type,i in 1..num[b]} use[b,i];

This perfectly works!! Gives the minimum number of boxes of each type used as well as allocation of balls in the boxes. Variable 'v' takes fractional values.

However, if I specify variable 'v' as an integer variable, I get the below 'ran out of memory' error after running for about 20 min

Presolve eliminates 516 constraints and 7000 variables.
Adjusted problem:
4100 variables, all nonlinear
1018 algebraic constraints, all linear; 17194 nonzeros
300 logical constraints
1 linear objective; 13 nonzeros.

CPLEX 12.2.0.0: There may be further error information in the clone logs.ran out of memory.
0 MIP simplex iterations
0 branch-and-bound nodes
No basis.
1 cover cut
4 flow-cover cuts
17 Gomory cuts
269 implied-bound cuts
2 zero-half cuts
84 mixed-integer rounding cuts

I then specified the cplex options with timelimit tolerance / treememlim tolerance 'option cplex_options 'nodefile=3 mipdisplay 2 mipinterval 1000 timelimit 100'  but still end up getting a message 'time limit with no integer solution'. Please see the updated .mod, .dat & log files attached. Is integer solution feasible at all? Are there any other ways I can apply tolerances to get a feasible integer solution for the variable 'v'. Or how can I resolve this. Please give me your inputs.

Thank you.
Box_Updated.mod
Box_Updated.dat
log.txt

Robert Fourer

unread,
Sep 26, 2016, 9:32:02 AM9/26/16
to am...@googlegroups.com
When a MIP solver runs for a long time without finding any integer-feasible solution, the first possibility to consider is that no feasible solution exists. In that case, if you run the solver long enough with enough memory and disk space, it will confirm that there is no feasible solution; but proofs of infeasibility can be very difficult and may require far more time and memory than you can provide. Instead you can experiment with relaxing or dropping some constraints. For example your problem is integer-feasible when the const03 constraints are dropped, so it might be worth looking at those more carefully; using "expand" I see a lot of constraints like

subject to const03[2,57,'G']:
-50*v[2,57,'R',1] - 47.5*v[2,57,'R',2] - 45*v[2,57,'Y',1] -
42.5*v[2,57,'Y',2] - 40*v[2,57,'Y',3] - 37.5*v[2,57,'Y',4] -
35*v[2,57,'Y',5] - 32.5*v[2,57,'Y',6] + 30*v[2,57,'G',1] +
27.5*v[2,57,'G',2] + 25*v[2,57,'G',3] + 22.5*v[2,57,'G',4] +
20*v[2,57,'G',5] + 17.5*v[2,57,'G',6] + 15*v[2,57,'G',7] +
12.5*v[2,57,'G',8] + 10*v[2,57,'G',9] + 7.5*v[2,57,'G',10] +
5*v[2,57,'G',11] + 2.5*v[2,57,'G',12] = 0;

which might be hard to satisfy exactly with integer values of the variables.

If there really is an integer-feasible solution, then that is a different situation. You can help the solver to find a solution by looking for constraints that can be relaxed because they are tighter than necessary. For example there might be an = constraint that can be changed to <= or >= without making a difference to the optimal solution.

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages