Is this an issue with pre-solve or how AMPL uses arrays? - optimization

I am modeling a production problem in AMPL with start and end inventories to be zero.
Simple 3 products being produced over 4 day span, on a single machine.
however my model keeps having problems with the bigM constraint and no inventory is allocated.Model is infeasible per AMPL presolve.
I would really appreciate if someone could point whats wrong or what i should try. Thanks in advance!
HERE IS THE CODE -
param n;
param m;
param M;
param T;
set I := {1..n};
set J := {1..m};
param r {I} integer; #productionrate
param stp {I} integer; #setuptime
param rev {I} integer; #sellingprice
param h {I} integer; #inventorycost
param d {I,J} integer; #demand
var t {I,J} integer >= 0, <=480 default 50; #production time allocated for product i on day j
var s {I,J} integer >= 0 default 50; #inventory of product i on day j
var z {I,J} binary default 1; #setup for product i done on day j or not
#sum of revenue - sum of inventory cost
maximize K: sum {i in I,j in J}(rev[i] * t[i,j] * r[i] * z[i,j]) - sum {i in I,j in J}(h[i] * s[i,j]);
#inventory equation
s.t. c2 {i in I,j in 2..4}: s[i,j] = s[i,j-1] + t[i,j]*r[i] - d[i,j];
s.t. c14 {i in I}: s[i,1] = t[i,1]*r[i] - d[i,1];
#initial and end inventories
s.t. c3 {i in I}: s[i,1] = 0;
s.t. c4 {i in I}: s[i,4] = 0;
#ensuring time allocation only when setup
s.t. c5 {i in I,j in J}: t[i,j] <= M*z[i,j];
#ensuring demand is satisfied
s.t. c6 {i in I,j in 2..4}: s[i,j-1] + t[i,j]*r[i] = d[i,j];
s.t. c11 {i in I}: t[i,1]*r[i] = d[i,1];
#production time constraint
s.t. c7 {j in J}: sum{i in I}(z[i,j]*stp[i]) + sum{i in I}(t[i,j]) <= T;
#other non-negativity constraints
s.t. c12 {i in I,j in J}: s[i,j] >= 0;
#s.t. c13 {i in I,j in J}: t[i,j] >= 0;
end;
HERE IS THE DATA -
param n :=3;
param m :=4;
param T :=480;
param M :=480;
#param o :=2;
param d: 1 2 3 4 :=
1 400 600 200 800
2 240 440 100 660
3 80 120 40 100;
param r:=
1 5
2 4
3 2;
param stp:=
1 45
2 60
3 100;
param rev:=
1 50
2 70
3 120;
param h:=
1 2
2 1
3 3;
end;
RUN FILE -
#RESET THE AMPL ENVIROMENT
reset;
#LOAD THE MODEL
model 'EDX 15.053 Production problem.mod';
#LOAD THE DATA
data 'EDX 15.053 Production problem.dat';
#DISPLAY THE PROBLEM FORMULATION
expand K;
expand c2;
#expand c3;
expand c4;
expand c5;
expand c6;
expand c7;
expand c11;
#let s[3,3] := 20;
#CHANGE THE SOLVER (optional)
#option solver CBC;
option solver cplex, cplex_options ’presolve 0’;
option show_stats 1;
option send_statuses 0;
#option presolve 0;
#SOLVE
solve;
print {j in 1.._nvars:_var[j].status = "pre"}: _varname[j];
print {i in 1.._ncons:_con[i].status = "pre"}: _conname[i];
display solve_message;
display solve_result_num, solve_result;
#SHOW RESULTS
display t,s,z,K;
end;

Related

Two Loop Network on Gams

Regarding the Two Loop Network formulation on GAMS, I'm struggling with one of the equations.
I can solve the problem without the energy conservation loop constraint but, once i add it, the problem becomes infeasible. Also, I'm not sure if the two loops are well defined
I would apprecite if someone spots my error.
Thank you.
Set
n 'nodes' / 1, 2, 3, 4, 5, 6, 7 /
a(n,n) 'arcs/pipes arbitrarly directed'
/1.2, 4.(2,5,6), 3.(2,5), 7.(5,6)/
rn(n) 'reservoir' / 1 /
dn(n) 'demand nodes' /2, 3, 4, 5, 6, 7/
m 'number of loops' /c1, c2/
c1(n,n) 'loop 1'
/2.(3,4), 5.(3,4)/
c2(n,n) 'loop 2'
/4.(5,6), 7.(5,6)/
k 'Options available for the diameters'
/ k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14 /;
dn(n) = yes;
dn(rn) = no;
display a;
display dn;
display rn;
display m;
display c1;
display c2;
Alias(n,np);
Table node(n,*) 'node data'
demand elevation minhead
* m^3/sec m m
1 210 30
2 0.0278 150 30
3 0.0278 160 30
4 0.0333 155 30
5 0.0750 150 30
6 0.0917 165 30
7 0.0444 200 30 ;
display node;
Table Diam(k,*) 'Diameter and cost information'
Diameter Cost
* m $/m
k1 0.0254 2
k2 0.0508 5
k3 0.0762 8
k4 0.1016 11
k5 0.1524 16
k6 0.2032 23
k7 0.2540 32
k8 0.3048 50
k9 0.3556 60
k10 0.4064 90
k11 0.4572 130
k12 0.5080 170
k13 0.5588 300
k14 0.6096 550;
Scalar
length 'pipes diameter' /1000/
roughcoef 'roughness coefficient for every pipe' /130/
Vmin 'Minimum velocity (m/s)' /0.3/
Vmax 'Maximum velocity (m/s)' /3.0/
dmin 'minimum diameter of pipe' /0.0254/
dmax 'maximum diameter of pipe' /0.6096/
davg 'Diamter Average for starting point';
davg = sqrt(dmin*dmax);
Variable
x(n,n) 'absolute flow through each arc'
y(n,n,k) 'takes value 1 when a pipe in arc(n,n) has diameter e(k) and 0 otherwise'
t(n,n) 'auxiliary variable for modeling the flow going in the forward direction'
r(n,n) 'auxiliary variable for modeling the flow going in the reverse direction'
u(n) 'variable representing the head of node n'
d(n,n) 'representing the diameter of pipe in link (n,n), takes the same value as some e(n)'
v(n,n) 'Water velocity'
q(n,n)'real variable representing the flow direction by being the flow sign, being 1 if flow goes forward or −1 if in reverse direction for a link (n,n)'
Custo 'total cost';
Binary Variable y, t, r;
NonNegative Variable x, d;
Equation
UniPipeDiam(n,n) 'Unique Pipe Diameter'
PipeDiam(n,n) 'Pipe Diameter Real Value'
FlowDirection(n,n) 'Flow Direction'
FlowSign(n,n) 'Flow Sign'
FlowConservation(n) 'Flow Conservation at Each Node'
HeadLoss(n,n) 'Head Loss'
EnerConserLoop1(n,np) 'Energy Conservation in Loop 1'
EnerConserLoop2(n,np) 'Energy Conservation in Loop 2'
Objective 'Objective Function: Capital Cost'
Velocity(n,n) 'Velocity calculation'
VelocUp(n,np) 'Upper bound velocity'
VelocDown(n,np) 'Lower bound velocity';
UniPipeDiam(a).. sum(k, y(a,k)) =e= 1;
PipeDiam(a(n,np)).. d(n,np) =e= sum(k, Diam(k,'Diameter')*y(n,np,k));
FlowDirection(a(n,np)).. t(a) + r(a) =e= 1;
FlowSign(a(n,np)).. q(a) =e= t(a) - r(a);
FlowConservation(dn(n)).. sum(a(np,n), x(a)*q(a)) - sum(a(n,np), x(a)*q(a)) =e= node(n,"demand");
HeadLoss(a(n,np)).. u(n) - u(np) =e= [10.667]*[roughcoef**(-1.852)]*length*[d(a)**(-4.8704)]*[x(a)**(2)]*q(a);
Velocity(a(n,np)).. v(a) =e= (4.0*x(a))/(Pi*d(a)**2.0);
VelocUp(a).. v(a) =l= Vmax;
VelocDown(a).. v(a) =g= Vmin;
EnerConserLoop1(n,np).. sum(a(n,np)$c1(n,np), q(a) * (u(n) - u(np))) =e= 0;
EnerConserLoop2(n,np).. sum(a(n,np)$c2(n,np),q(a) * (u(n) - u(np))) =e= 0;
Objective.. Custo =e= sum(a(n,np), sum(k, length*Diam(k,'Cost')*y(n,np,k)));
*bounds
d.lo(n,np)$a(n,np) = dmin;
d.up(n,np)$a(n,np) = dmax;
u.lo(rn) = node(rn,"elevation");
u.lo(dn) = node(dn,"elevation") + 5.0 + 5.0*node(dn,"demand");
u.up(dn) = 300.0;
* initial values
d.l(n,np)$a(n,np) = davg;
u.l(n) = u.lo(n) + 5.0;
Model network / all /;
network.domLim = 1000;
Option Iterlim = 50000;
option MINLP = baron;
solve network using minlp minimizing Custo;

AMPL error "is not a set" "is not a param (or var or constraint or objective)""No variables declared"

I'm super new to AMPL and now struggling to solve the error.
I created both dat.file and mod.file separately, and wrote codes as exactly as the same as my teacher mentioned but it keeps showing below errors. I asked the teacher but seems it's properly shown on his PC, but not on mine (I'm Mac user).
Dat file:
`set PRODUCTION := P1 P2;
set RESOURCES := C W; # C W
param max_res := C 3000 W 1440;
param profit := P1 3.5 P2 1.5;
param param_res : C W :=
P1 1.5 5
P2 2 4.5;`
Mod file:
`reset;
option solver cplex;
set PRODUCTION;
set RESOURCES;
param profit {PRODUCTION};
param max_res {RESOURCES};
param param_res {PRODUCTION, RESOURCES};
var x {PRODUCTION} >= 0;
maximize profit: sum {i in PRODUCTION} profit[i] * x[i];
s.t. c_res {j in RESOURCES}: sum {i in production} param_res[i,j]*x[i] <= max_res[j];
s.t. c_minp2 {i in PRODUCTION]: sum {i in production} P2[i] >=30;
data PRODUCTION_REV.dat;`
Error:
ampl: data '/Users/XXX/Desktop/PRODUCTION_REV.dat';
/Users/XXX/Desktop/PRODUCTION_REV.dat, line 4 (offset 37):
PRODUCTION is not a set
context: set >>> PRODUCTION <<< := P1 P2;
/Users/XXX/Desktop/PRODUCTION_REV.dat, line 5 (offset 62):
RESOURCES is not a set
context: set >>> RESOURCES <<< := C W; # C W
/Users/XXX/Desktop/PRODUCTION_REV.dat, line 8 (offset 120):
max_res is not a param (or var or constraint or objective)
context: param >>> max_res <<< := C 3000 W 1440;
/Users/XXX/Desktop/PRODUCTION_REV.dat, line 9 (offset 152):
profit is not a param (or var or constraint or objective)
context: param >>> profit <<< := P1 3.5 P2 1.5;
/Users/XXX/Desktop/PRODUCTION_REV.dat, line 12 (offset 209):
param_res is not a param (or var or constraint or objective)
context: param >>> param_res <<< : C W :=
ampl: solve;
No variables declared.
ampl:
Anyone has a clue for this? The teacher's reply is slow and my assignment due is tomorrow night, can't wait for another reply from him...

SAS solve optimization problem for each row of a dataset

I have a dataset like the following:
data have;
id=1; x1=10; x2=3; x3=5; y=220;
y1g=12; y2g=6; y3g=10;
output;
id=2; x1=7; x2=0; x3=3; y=100;
y1g=12; y2g=0; y3g=10;
output;
run;
for each record I want to had values for y1, y2, y3, such that
x1*y1+x2*y2+x3*y3=y
0=<y1<=12
0=<y2<=6
0=<y3<=10
I am not sure of which can be the objective function, but to fix the ideas
I might want that the solutions are as similar as possible to some initial guess, y1g ,y2g, y3g.
so for instance the objective might be:
min (y1-y1g)**2+(y2-y2g)**2+(y3-y3g)**2
I am using SAS9 M5 and have to apply the program to a dataset of about 100000 records.
can you suggest me with the proc optmodel code?
thank you very much in advance
Although your solution is infeasible with your constraints, this is what the optmodel code looks like:
proc optmodel;
set row;
num id;
num x1{row};
num x2{row};
num x3{row};
num y1g{row};
num y2g{row};
num y3g{row};
num y{row};
num y1sol{row};
num y2sol{row};
num y3sol{row};
var y1;
var y2;
var y3;
read data have into row=[id]
x1 x2 x3 y y1g y2g y3g
;
con con1: x1[id]*y1 + x2[id]*y2 + x3[id]*y3 = y[id];
con con2: 0 <= y1 <= 12;
con con3: 0 <= y2 <= 6;
con con4: 0 <= y3 <= 10;
min sum_squared_dif = ( (y1-y1g[id])**2+(y2-y2g[id])**2+(y3-y3g[id])**2 );
cofor {i in row} do;
id = i;
solve;
/* Save each individual solution */
y1sol[id] = y1.sol;
y2sol[id] = y2.sol;
y3sol[id] = y3.sol;
end;
print y1sol y1g y2sol y2g y3sol y3g;
quit;
Note that the first row has an infeasible solution.

Indexed variable in AMPL

I have the following model with a variable that is a value from a vector (index of p in objective function)
But AMPL displays an error: subscript variables are not yet allowed.
How can I do to implement this kind of addressing in objective function?
Thanks in advance and best regards.
Gabriel
param dimension;
set T:={1..dimension};
set O:={0};
set V:= O union T;
param c{i in V, j in V};
param p{i in V};
set ady{i in V} within V := {j in V : i<>j and c[i,j] <> -1} ;
# Variables
var x{i in V, j in V} binary;
var u{i in V} integer;
# Objective
minimize costo: sum{i in V, j in V} p[u[i]-1] * x[i,j] * c[i,j];
# Constraints
s.t. grado_a {j in V} : sum{i in ady[j] : j <> i} x[i,j] = 1;
s.t. grado_b {i in V} : sum{j in ady[i] : i <> j} x[i,j] = 1;
s.t. origen {i in O} : u[i] = 0;
s.t. sigo_1 {i in T} : u[i] >=1;
s.t. sigo_2 {i in T} : u[i] <= card(V) -1;
s.t. precedencia {i in T, j in T : i <> j} : u[i] - u[j] + 1 <= (card(V) - 1)*(1 - x[i,j]) ;
AMPL doesn't allow variables in subscripts yet. However, the ilogcp driver for AMPL supports the element constraint, for example:
include cp.ampl;
minimize costo:
sum{i in V, j in V} element({v in V} p[v], u[i] - 1) * x[i,j] * c[i,j];
where element({v in V} p[v], u[i] - 1) is equivalent to p[u[i] - 1] and is translated into an IloElement constraint.

Summation on 1 <= i < j < k <= n in GLPK

I have been trying to solve seriation problem by using GNU. But I couldn't write a summation like the following.
param n, integer, >= 3;
set O := 1..n;
param d{i in O,j in O};
var x{i in O,j in O}, binary, i < j;
var v{i in O,j in O,k in O}, binary, i < j < k;
maximize total: sum{i in O,j in O, i<j}(d[i,j] - d[j,i])* x[i,j] + sum{i in O,j in O, i<j}d[j,i];
s.t. tran{i in O,j in O,k in O, i<j<k}: x[i,j] + x[j,i] - x[i,k] + v[i,j,k] = 1;
Thanks
You should use : instead of , in the "such that" clause i < j:
sum{i in O,j in O: i < j} ...
# ^ note ':' here