Indexed variable in AMPL - 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.

Related

Is there a way to make a specific variable from a set an integer in Ampl?

set P;
set K;
set I:= {i in K};
set J:={j in P};
param C {P} >=0;
param A {K,P} >=0;
param B {K} >=0;
var X{j in P} >=0;
P consists of 4 set values, namely sweatshirt-f, sweatshirtB/F, tshirtf and tshirtBF, but I would like sweatshirt-f only to return an integer:
maximize f: sum{j in P} C[j]*X[j];
s.t. Constraint {i in K}:
sum{j in P} A[i,j]*X[j]<=B[i];
Option 1: declare all of them as integer, then relax integer constraints for the others:
var X{j in P} >=0 integer;
let {i in P: i <> "sweatshirt-f"} X[i].relax := 1;
Option 2: declare a dummy variable as integer, then constrain relevant value to match it:
var X{j in P} >=0;
var int_dummy integer;
s.t. force_integer: X["sweatshirt-f"] = int_dummy;
I'm not able to test those right now, so you may have to do a little debugging, but that should get you in the neighbourhood of a solution.

Debugging specialize and/or apply errors in Coq

I'm trying to figure out the source of the following error with the apply (list2map_not_in_default [(k, v)] i) in H2. command.
Here is the list2map_not_in_default type:
list2map_not_in_default
: forall (al : list (key * V)) (i : key),
~ (exists v : V, In (i, v) al) -> list2map al i = default
And the error:
Error:
Unable to apply lemma of type
"~ (exists v0 : V, In (i, v0) [(k, v)]) -> list2map [(k, v)] i = default"
on hypothesis of type "~ (exists v : V, In (i, v) [(k, v)])".
Alternatively apply (list2map_not_in_default [(k, v)] i) in H2. , can be seen as the following error, whereby you can see my context fully.
Error:
In environment
V : Type
default : V
lo : key
l : tree
k : key
v : V
r : tree
hi : key
H0_ : SearchTree' lo l k
H0_0 : SearchTree' (S k) r hi
i : nat
Hleft : ~ (exists v : V, In (i, v) (slow_elements l))
H : i < k
H0 : k <> i
H1 : list2map (slow_elements l) i = default
H2 : ~ (exists v : V, In (i, v) [(k, v)])
The term "H2" has type "~ (exists v : V, In (i, v) [(k, v)])"
while it is expected to have type "~ (exists v0 : V, In (?i, v0) ?al)".
Earlier in the proof I use the exact same lemma specialize (list2map_not_in_default _ _ Hleft). with no problems.
This applied in the following context
V : Type
default : V
lo : key
l : tree
k : key
v : V
r : tree
hi : key
H0_ : SearchTree' lo l k
H0_0 : SearchTree' (S k) r hi
i : nat
Hleft : ~ (exists v : V, In (i, v) (slow_elements l))
H : i < k
H0 : k <> i
============================
list2map (slow_elements l ++ [(k, v)] ++ slow_elements r) i =
list2map (slow_elements l) i
yields a goal with the desired application.
list2map (slow_elements l) i = default ->
list2map (slow_elements l ++ [(k, v)] ++ slow_elements r) i =
list2map (slow_elements l) i
What is happening here? Any suggestions on how I can fix it?
The unification algorithm used by apply in is exceedingly weak. There is nothing you can do about it (except opening bug reports and hoping someone fixes it). Just use some other tactic. As you already noticed, specialize works better, since you are explicitly writing a lambda-term. Another similar tactic is assert (H3 := list2map_not_in_default _ _ Hleft). Or you can go to the most expressive tactic of all: (simple) refine (let H3 := list2map_not_in_default _ _ Hleft in _).

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

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;

Conditional summation in CPLEX using OPL

I'm trying write in OPL this sum:
I did this, but it is not exactly what I need.
forall (n in cont, t in tempo, o in portos)
sum(i in colunap, j in linhap)b[i][j][n][t] + v[n][t] == 1;
I should be something like, but opl does not accept it:
forall (n in cont[o], t in tempo[o], o in portos)
sum(i in colunap[o], j in linhap[o])b[i][j][n][t] + v[n][t] == 1;
This should work:
int P=3;
int H[1..P-1] = [1 , 2];
range linhap=1..max(o in 1..P-1) H[o];

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