Coding In AMPL Software - ampl

How can I start with a specific city under a set of cities, rather than using x[i,j]
I want to write such constraint x[1,2] + x[2,5] = 1
Moreover, how can I start from i=2, j=2 rather than start with i,j =1
Thanks,
Haidar

I want to write such constraint x[1,2] + x[2,5] = 1
s.t. c1: x[1,2]+x[2,5]=1;
how can I start from i=2, j=2 rather than start with i,j =1
Define index sets that don't have 1 in them. For instance, if you have
set S := 1..s;
then you could either do:
set S_2 := 2..s;
or:
set S_2 := S diff {2};
and then create a constraint indexed on S_2.

Related

How should you calculate partial derivative ∂f/∂x using first the chain rule & directly in MAPLE?

If f(u,v) = vsinu+v^2, u(x,y)=tan^−1􏰀(y􏰁/x), v=sqrt(􏰂x^2 + y^2)
Calculate using chain rule and directly substituting for u(x,y), v(x,y).
Your syntax is not altogether clear to me, but I am guessing you are trying to achieve something like this:
uveqs := [u(x,y)=arctan(y/x), v(x,y)=sqrt(x^2+y^2)]:
f := v(x,y)*sin(u(x,y))+v(x,y)^2:
Differentiate f with respect to x (noticing the effect of the chain-rule),
K := diff(f,x):
diff(v(x,y),x) * sin(u(x,y))
+ v(x,y) * diff(u(x,y),x) * cos(u(x,y))
+ 2*v(x,y) * diff(v(x,y),x)
Now substitute for u(x,y) and v(x,u), and simplify,
S1 := eval(K,uveqs):
simplify(S1);
2 x
Alternatively, first substitute for u(x,y) and v(x,y) into f itself,
fxy := eval(f,uveqs):
(x^2+y^2)^(1/2)*y/x/(1+y^2/x^2)^(1/2)+x^2+y^2
And then differentiate with respect to x, and simplify,
S2 := diff(fxy,x):
simplify(S2);
2 x
If you have trouble understanding the mechanisms, you might also compare these two pairs of operations,
diff( v(x,y), x );
eval( %, uveqs );
and,
eval( v(x,y), uveqs );
diff( %, x );

Minizinc "var set of int: x" instead of "set of int: x"

I have an array of set in the Golfers problem (in each week there should be formed groups, such that no two players play together more than once, and everybody plays exactly one time each week):
int: gr; %number of groups
set of int: G=1..gr;
int: sz; %size of groups
set of int: S=1..sz;
int: n=gr*sz; %number of players
set of int: P=1..n;
int: we; % number of weeks
set of int: W=1..we;
include "globals.mzn";
array[G,W] of var set of P: X; %X[g,w] is the set of people that form group g in week w
My constraints are as follow (I'm not sure if everything works correctly yet):
constraint forall (g in G, w in W) (card (X[g,w]) = sz); %Each group should have size sz
constraint forall (w in W, g,h in G where g > h) (disjoint(X[g,w], X[h,w])); % Nobody plays twice in one week
constraint forall (w,u in W where w > u) (forall (g,h in G) (card(X[g,w] intersect X[h,u]) <= 1 )); % Two players never meet more than once
constraint forall (w in 2..we) (w+sz-1 in X[1,w] /\ 1 in X[1,w]); %Symmetries breaking: week permutations
constraint forall (w in W, g in 1..gr-1) ( min(X[g,w]) < min(X[g+1,w]) ); %Symmetries breaking: group permutations
constraint forall (g in G, s in S) ( s+sz*(g-1) in X[g,1]);
solve satisfy;
output [ show(X[i,j]) ++ if j == we then "\n" else " " endif | i in 1..gr, j in 1..we ];
My problem lies in constraint number 5. I cannot use min on "var set of int: x", I should use it on "set of int: x". Unfortunately, I do not understand the difference between those two (from what I've read this may be connected to defining the size of each set, but I'm not sure).
Could someone explain the problem to me and propose a solution? I would be very very grateful. Thanks!
First of all: A var is a decision variable. The goal of all Minizinc programs are to decide the the value of all decision variables. You don't know what the values are and you are trying to find the values. Anything that is not a var is simply a known number. (disregarding the use of sets)
Doing min(X[g,w]) of a decision variable (var) is simply not implemented in Minizinc. The reason would be that using X[g,w] < X[g+1,w] without the min makes more sense. Why only constrain the lowest number in both sets insted of all numbers. I.e {1,3,5} < {1,4} insted of 1 < 1
(I hope MiniZinc has < on sets so I don't lie, I am not sure)
I have found out the solution - we should make an array of elements of the set to make the max function possible in this case.
constraint forall (w in 2..we) ( max([i | i in X[1,w-1]]) < max([i | i in X[1,w]])); %Symmetries breaking: week permutations
constraint forall (w in W, g in 1..gr-1) ( min([i | i in X[g,w]]) < min([i | i in X[g+1,w]]));% Symmetries breaking: group permutations (I have been trying to speed up the constraint above, but it does not work with var set of int..)

Sum the binary variables in GLPK

I am new in GLPK. This is some of my code:
set I := setof{(i,r,p,d) in T} i;
var Y{I,I}, binary;
s.t. c1{i in I, j in I}: sum{Y[i,j]} = 6;
I want to have only six values in Y that are 1. Can anyone tell me how to do it in proper way? Because s.t. c1{i in I, j in I}: sum{Y[i,j]} = 6;always produces an error.
Thank you.
This is just a syntax problem. The constraint should look like the following:
s.t. c1: sum{i in I, j in I}(Y[i,j]) = 6;
The first brackets after the name of your constraints imply that the constraint is applied to every single [I, I]. What you want is to fix the sum of all Y in your problem, so you need the constraint to only apply once to your problem (so delete these brackets).
In the sum-syntax don't put the variable you want to sum in the brackets, they belong after them. Inside the brackets you can define the range of the sum.

AMPL double conditions in constraints

I'm working on an optimization project and I'm using AMPL with CPLEX for this.
My problem is somehow simple but I couldn't do it without using some additional "useless" variables.
So assume I have the following code:
set A:= a b c;
set B:= 1 2 3;
var x{A,B} binary;
now I want a constraint to be processed under 2 conditions, for example:
if x[a,1] = 1 and x[a,2] = 1 then (some constraint).
Unfortunately, CPLEX won't let me use the syntax:
s.t const: x[a,1] = 1 and x[a,2] = 1 ==> (some constraint)
it says "logical constraint _slogcon[1] is not an indicator constraint.
Now the way I did it was by introducing a new variable.
var dummyVar{A,A,B,B} binary;
this variable is equal to 1 if both x[a,1] = 1 and x[a,2] = 1.
subject to condition: 2*dummyVar[a,a,1,2] <= x[a,1] + x[a,2]
My problem is with the large model I'm working with. In my case, this dummyVar is not simply a small set, it contains a set of sets. When AMPL is processing the code (translating it to be read by CPLEX), it crashes due to lack of memory.
Is there any simple way to write something like
s.t const: x[a,1] = 1 and x[a,2] = 1 ==> (some constraint)
without introducing any additional variables? Thanks in advance.
To model this effectively, you should consider two things
'some-constraint' is linear or quadratic.
if either x[a,1] or x[a,2] are 1, then the constraint can be violated.
On point 1, your some-constraint is of the form
l <= f(x) <= u
where f(x) is a quadratic of linear function and l and u are constraints. If you let f_max and f_min be the upper and lower bounds of the function f(x) over all feasible values of x then you can write your conditional constraint as
l - (l - f_min) (x[a,1] + x[a,2]) <= f(x) <= u + (f_max - u)(x[a,1] + x[a,2])
If either x[a,1] or x[a,2] are 1 then the constraint becomes
f_min <= f(x) <= f_max
or looser if both x[a,1] and x[a,2] are both 1. If both are 0, then the original constraint is enforced.

AMPL: Subscript out of bound

Hello fellow optimizers!
I'm having some issues with the following constraint:
#The supply at node i equals what was present at the last time period + any new supply and subtracted by what has been extracted from the node.
subject to Constraint1 {i in I, t in T, c in C}:
l[i,t-1,c] + splus[i,t] - sum{j in J, v in V, a in A} x[i,j,v,t,c,a]= l[i,t,c];
which naturally causes this constraint to provide errors the first time it loops, as the t-1 is not defined (for me, l[i,0,c] is not defined. Where
var l{I,T,C} >= 0; # Supply at supply node I in time period T for company C.
param splus{I,T}; # Additional supply at i.
var x{N,N,V,T,C,A} integer >= 0; #Flow from some origin within N to a destination within N using vehicle V, in time T, for company C and product A.
and set T; (in the .mod) is a set defined as:
set T := 1 2 3 4 5 6 7; in the .dat file
I've tried to do:
subject to Constraint1 {i in I, t in T: t >= 2, c in C}:
all else same
which got me a syntax error. I've also tried to include "let l[1,0,1] := 0" for all possible combinations, which got me the error
error processing var l[...]:
no data for set I
I've also tried
subject to Constraint1 {i in I, t in T, p in TT: p>t, c in C}:
l[i,t,c] + splus[i,p] - sum{j in J, v in V, a in A} x[i,j,v,p,c,a]= l[i,p,c];
where
set TT := 2 3 4 5 6;
in the .dat file (and merely set TT; in the .mod) which also gave errors. Does someone have any idea of how to do this?
One way to fix this is to specify the condition t >= 2 at the end of the indexing expression:
subject to Constraint1 {i in I, t in T, c in C: t >= 2}:
...
See also Section A.3 Indexing expressions and subscripts for more details on syntax of indexing expressions.