Is there a way to make a specific variable from a set an integer in Ampl? - 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.

Related

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...

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;

AMPL Variables in subscripts are not yet allowed

I really hope someone can help with this...
This is what i have in the .mod file
set I := 1..10;
set J := 1..10;
set K := 1..2;
set W := 1..20;
param v{K, W};
param d{I, J};
var x1, integer;
var y1, integer;
var x2, integer;
var y2, integer;
var assist{W}, binary;
and this is the code generating error:
minimize nome: sum{w in W} (if (assist[w] == 0) then
(if (x1 >= v[1,w]) then
(if (y1 >= v[2,w]) then
(d[x1 - v[1,w],y1 - v[2,w]])....
where the error regards the last line and says:
Variables in subscripts are not yet allowed.
context: (d[x1 - v[1,w],y1 - >>> v[2,w]] <<< )
this is one of constraints (others are just the same):
subject to rangex1:
x1 > 0 && x1 <= 10;
As the error message says, you can't use decision variables within a subscript in AMPL. In this case x1 and y1 are decision variables, so d[x1 - v[1,w],y1 - v[2,w]] is not allowed. You'll need to reformulate the problem in a way that avoids this issue.

declare default value for variables in ampl

Now I have variable set x in my ampl model, and I want to define a default value for each x.
set N := 1..10;
var x {i in N} default 0;
If I build the model like this, the initial value of all x would be set as 0. How can I set different value for each x, like [0,0,0,0,0,1,1,1,1,1]?
If you just want to change some parts of x to a non-default value, this is easy to do. For example:
var x{i in N} default 0;
let{i in 6..10} x[i] := 1;
I'm not aware of any way to have more than one default value for different elements of a var.

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.