AMPL: Invalid subscripts - ampl

I'm intending to conduct an optimization but I get an error which I cannot find the cause to. The compiler complains about servicetime, startlimit and endlimit not being invalid. In particular the compiler complains in the constraint TimeConstraint that there is no value for servicetime['ORIGINS'].
The constraint in question is:
subject to TimeConstraint {k in H}: sum{i in UNI, j in UNI} servicetime[i]*x[i,j,k] <= 1440;
where:
set ORIGINS;
set DESTINATIONS;
set UNI;
param servicetime{UNI} integer > 0;
var x{UNI,UNI, H} binary;
param startlimit{UNI};
param endlimit{UNI};
the .dat file is:
set ORIGINS := 1 2 3 4 5 6 7;
set DESTINATIONS := 8 9 10 11 12 13 14;
set UNI = ORIGINS union DESTINATIONS;
param: startlimit endlimit servicetime:=
1 0 1440 360
2 0 1440 360
3 0 1440 360
4 0 1440 360
5 0 1440 360
6 0 1440 120
7 0 1440 120
8 0 1440 360
9 0 1440 360
10 0 1440 360
11 0 1440 360
12 0 1440 360
13 0 1440 120
14 0 1440 120;
I cannot see how this does not work. To me this looks accurate. Hope someone can shed some light into this!
Regards,

AMPL data format doesn't permit expressions, so ORIGINS, union and DESTINATIONS are interpreted literally as strings rather than a set expression ORIGINS union DESTINATIONS in the data statement
set UNI = ORIGINS union DESTINATIONS;
You can fix this by initializing the set in the declaration in the model file:
set ORIGINS;
set DESTINATIONS;
set UNI = ORIGINS union DESTINATIONS;
param servicetime{UNI} integer > 0;
...

Related

Writing piecewise constraints in GAMS

I'm trying to solve the network problem below in GAMS Cplex. I have a piecewise constraint that depend on the node situations (whether a node is an origin (o) node, in between node, and destination (d) node).
How do I write these piecewise constraints? Or is there any way to write this 'manually' for the equation 1 to 5?
In the program below, I've written:
eq1 represents node 1 as an origin node,
eq2 eq3 eq4 represents node 2,3, and 4 as in between nodes,
eq5 represents node 5 as a destination node.
Set
i nodes /1,2,3,4,5/;
Alias(i,j);
Set
arc(i,j) arcs from node i to j
/1 .2
2 .1
1 .3
3 .1
1 .4
4 .1
2 .3
3 .2
2 .5
5 .2
3 .5
5 .3
4 .5
5 .4/;
Table c(i,j) population exposed from node i to node j
1 2 3 4 5
1 0 105000 90000 65000 0
2 105000 0 100000 0 85000
3 90000 100000 0 0 80000
4 65000 0 0 0 55000
5 0 85000 80000 55000 0
;
Table l(i,j) distance from node i to node j
1 2 3 4 5
1 0 5 8 10 0
2 5 0 2 0 7
3 8 2 0 0 11
4 10 0 0 0 8
5 0 7 11 8 0
Binary Variables
x(i,j)
y(i,j);
Positive Variables
v(i,j)
lambda(i,j);
Free Variables
w(i) node i
w(j) node j
z optimization solution;
Scalar
R very large number;
R = 10000000000000000;
Equations
sol optimization solution
eq1(i,j) constraint 1
eq2(i,j) constraint 2
eq3(i,j) constraint 3
eq4(i,j) constraint 4
eq5(i,j) constraint 5
eq6(i,j) constraint 6
eq7(i,j) constraint 7
eq8(i,j) constraint 8
eq9(i,j) constraint 9;
sol.. z =e= sum(arc(i,j),c(arc)*x(arc));
eq1(i,j).. x(1,2) - x(2,1) + x(1,3) - x(3,1) + x(1,4) - x(4,1) =e= 1;
eq2(i,j).. - x(1,2) + x(2,1) + x(2,3) - x(3,2) + x(2,5) - x(5,2) =e= 0;
eq3(i,j).. - x(1,3) + x(3,1) - x(2,3) + x(3,2) + x(3,5) - x(5,3) =e= 0;
eq4(i,j).. - x(1,4) + x(4,1) + x(4,5) - x(5,4) =e= 0;
eq5(i,j).. - x(2,5) + x(5,2) - x(3,5) + x(5,3) - x(4,5) + x(5,4) =e= -1;
eq6(i,j).. - y(i,j) + x(i,j) =l= 0;
eq7(i,j).. l(i,j) - w(i) + w(j) - v(i,j) + lambda(i,j) =e= 0;
eq8(i,j).. v(i,j) - R * (1 - x(i,j)) =l= 0;
eq9(i,j).. lambda(i,j) - R * (1 - (y(i,j) - x(i,j))) =l= 0;
Model contohTMB /all/;
Solve contohTMB using MIP Minimizing z;
Display "Solution values:"
Display
x.l, z.l;

How do I do it to just get a integer solution in AMPL?

We make phones. We have selling price, production cost, profit.
The goal is to maximize profits.
The following components are required to assemble each phone.
Maximum quantity of components .
Orders (so many phones were ordered from us, we sold them) :
Here is my mod file:
set PHONE;
set COMPONENTS;
param price {PHONE} >= 0;
param cost {PHONE} >= 0;
param maxComponents {COMPONENTS} >= 0;
param ordered {PHONE} >= 0;
param matrix {COMPONENTS, PHONE}; #The amount of components needed to make a particular phone.
var x {PHONE} >= 0; # Number of manufactured telephones.
maximize profit: sum {i in PHONE} ( ordered[i] * price[i] - x[i] * cost[i] );
subject to min_manufacture {i in PHONE}:
x[i] >= ordered[i]; # We must produce a minimum of what is ordered
subject to component {i in COMPONENTS}:
sum {j in PHONE} matrix[i,j] * x[j] <= maxComponents[i]; # The number of components used must not exceed the maximum.
subject to min_quantity {i in COMPONENTS, l in PHONE}:
sum {j in PHONE} matrix[i,j] * x[j] >= matrix[i,l]; # Minimum quantity used per component if we manufacture at least one telephone. For example, a triple phone requires at least 2 of the five components.
and dat file:
set PHONE := 1 2 3 4 5;
set COMPONENTS:= 1 2 3 4 5 6 7;
param price :=
1 450
2 120
3 500
4 390
5 100;
param cost :=
1 370
2 90
3 400
4 320
5 70;
param maxComponents :=
1 28
2 20
3 8
4 30
5 47
6 27
7 15;
param ordered :=
1 3
2 5
3 5
4 0
5 10;
param matrix: 1 2 3 4 5 :=
1 1 1 0 0 0
2 1 1 0 0 0
3 1 0 0 0 0
4 1 0 1 1 0
5 0 0 2 1 1
6 0 0 2 1 0
7 0 0 1 1 0;
The problem is that if, for example, the maximum amount of sixth components is three, the maximum amount of seventh components is two , then 1.5 is produced from the triple phone which cannot be . And quantity used of the fourth, fifth, sixth, seventh components for the triple phone 1,5 3 3 1,5 which also cannot be.
How do I do it to just get a integer solution?
Because if I write to the variable x that it's an integer, I get zero for everything.
My run file:
model phone.mod;
data phone.dat;
option presolve 0;
option solver cplex;
solve;
display profit, x;
display {i in COMPONENTS, j in PHONE} matrix[i,j] * x[j];
You need to declare the relevant variables as integer, like so:
var x {PHONE} >= 0 integer;
Some solvers are not able to deal with integer constraints and may ignore that constraint (with a warning message) but CPLEX should be fine.

In GAMS; Endogenous relational operations require model type "dnlp" Error

Parameter st(t) starting time of curfew on airport t
/11 540
22 540
33 540
44 540
55 540/;
Table arr(i,t) arrival
11 22 33 44 55
101 0 1 0 0 0
102 0 0 1 0 0
103 1 0 0 0 0
104 0 0 0 1 0
105 1 0 0 0 0
106 0 0 0 0 1
107 1 0 0 0 0;
Table dep(i,t) departure
11 22 33 44 55
101 1 0 0 0 0
102 0 1 0 0 0
103 0 0 1 0 0
104 1 0 0 0 0
105 0 0 0 1 0
106 1 0 0 0 0
107 0 0 0 0 1;
Variables
db(i) new estimated time of departure of flight i
r(i) new estimated time of arrival of flight ??
v(i) equal to 1 if flight i violates the curfew requirement
z total cost;
Binary Variables v(i);
Positive Variables db(i),r(i);
Equations
costt objective
c1(i) constraint
c2(i) constraint
c3(i,k) constraint
c4(i,i2,k) constaint
c5(i,t,tt) constraint
c6(i,t,tt) constraint
c7(i) constraint;
'''c5(i,t,tt)$((dep(i,t)=1 and arr(i,tt)=1))..v(i) =g= ifthen((db(i)>=(st(t)) or (r(i))>=(st(tt))),1,1-M);
c6(i,t,tt)$((dep(i,t)=1 and arr(i,tt)=1))..v(i) =l= ifthen((db(i)<(st(t)) or (r(i))<(st(tt))),1,1-M);'''
I don't understand why I got this error. But I know that these constraints are the cause of the error. I can not think of any other way of expressing this constraint in GAMS. Can you help me?
Constraint in the photo: https://imgur.com/DSrPxPE
You use the $( )-Condition on the equation domain of c5 to reduce the number of single equations.
When GAMS prepares the model for the solver it evaluates the $( )-condition and eliminates all (i,t,tt) combinations which fail the $( ) - condition. The problem is: your $( )-condition includes variables, whose values (in GAMS: 'levels') are determined during the optimization run of the model by a solver.
This means: GAMS cannot use the variables when it prepares the model for the solver, because their values are determined during the model run. That are the "endogenous relational operations".

Code if then statement by only using $ utility

How can I code this 'if' conditions in GAMS?
Set j/1*10/
S/1*6/;
Parameter
b(s,j) export this from excel
U(s,j) export from excel
M(s)/1 100,2 250,3 140,4 120,5 132/ export from excel
;
table b(s,j)
1 2 3 4 5 6 7 8 9 10
1 3 40 23 12 9 52 9 14 89 33
2 0 0 42 0 11 32 11 15 3 7
3 10 20 12 9 5 30 14 5 14 5
4 0 0 0 9 0 3 8 0 13 5
5 0 10 11 32 11 0 3 1 12 1
6 12 20 2 9 15 3 14 5 14 5
;
u(s,j)=0;
u(s,j)$(b(s,j))=1;
Variable delta(j); "binary"
After solving a model I got the value of delta ( suppose delta(1)=1, delta(5)=1). Then Set A is
A(j)$(delta.l(j)=1)=Yes; (A={1,5})
I want to calculate parameter R(s) according to the following :
If there is no j in A(j) s.t. j in u(s,j) then R(s)=M(s)
Else if there is a j in A(j) s.t. j in u(s,j) then R(s)=min{b(s,j): j in A(j) , j in u(s,j) }
Then R(1)=3, R(2)=11,R(3)=5, R(4)=120, R(5)=11,R(6)=12.
Is it possible to code this ' if then ' statement only by $ utility?
Thanks
Following on from the comments, I think this should work for you.
(Create a parameter that mimics your variable delta just for demonstration:)
parameter delta(j);
delta('1') = 1;
delta('5') = 1;
With loop and if/else:
Create parameter R(s). Then, looping over s , pick the minimum of b(s,A) across set A where b(s,A) is defined if the sum of b(s,A) is not zero (i.e. if one of the set is non-zero. Else, set R(s) equal to M(s).
Note, the loop is one solution to the issue you were having with mixed dimensions. And the $(b(s,A)) needs to be on the first argument of smin(.), not on the second argument.
parameter R(s);
loop(s,
if (sum(A, b(s,A)) ne 0,
R(s) = smin(A$b(s,A), b(s,A));
else
R(s) = M(s);
);
);
With $ command only (#Lutz in comments):
R(s)$(sum(A, b(s,A)) <> 0) = smin(A$b(s,A), b(s,A));
R(s)$(sum(A, b(s,A)) = 0) = M(s);
Gives:
---- 56 PARAMETER R
1 3.000, 2 11.000, 3 5.000, 4 120.000, 5 11.000, 6 12.000

Minimum Network Flow in GAMS

I am trying to solve below network problem in GAMS Cplex. I am unable to get the desired output as the GAMS is giving the arcs which doesnt exist as output. Could you please help me in correcting this.
Program:
Set
i supply nodes /1,2,3,4/;
Alias(i,j);
Set
arc(i,j) arcs from node i to j
/1 .2
2 .3
3 .4
1 .4
2 .4/;
Parameter b(i) number of units available or required at node i
/ 1 5
2 2
3 -4
4 -3/ ;
Table c(i,j) cost of shipping from node i to node j
1 2 3 4
1 0 3 0 1
2 0 0 6 5
3 0 0 0 0
4 0 0 2 0 ;
Positive variables
x(i,j) number of units shipped along arc from i to j;
Variable z;
Equations obj, cons(i);
obj.. z =E= sum(arc(i,j),c(arc)*x(arc));
cons(i).. sum(j,x(i,j)) - sum(j,x(j,i)) =E= b(i);
I think you need to use the set "arc" in your constraints "cons" like this:
cons(i).. sum(arc(i,j),x(i,j)) - sum(arc(j,i),x(j,i)) =E= b(i);
Hope that helps,
Lutz