how can I use If-then-else in AMPL to write this contraint - ampl

I try to put this constraint at AMPL using logical conditional. Anyone could say a way to write this at AMPL
Thanks

I proposed this way and works
subject to c21 {k in CONVOY}:
sum {(i,j) in ARCOS:j=destino[k]} X[k,i,j] - sum{(j,h) in ARCOS:j=destino[k]}X[k,j,h]=1;
subject to c22 {k in CONVOY}:
sum {(i,j) in ARCOS: j=origen[k]} X[k,i,j] - sum{(j,h) in ARCOS:j=origen[k]}X[k,j,h]=-1;
subject to c23 {k in CONVOY, j in DESTINOS:j!=destino[k]&&j!=origen[k]}:
sum {(i,h) in ARCOS:h=j}X[k,i,j] - sum{(i,h) in ARCOS:i=j}X[k,j,h]=0;

Related

how to write "then" as IP constraint in Julia

Hello fellows, i am learning Julia and integer programing but i am stuck at one point
How to model "then" in julia-jump for integer programing leanring.
Stuck here here
#Define the variables of the model
#variable(mo, x[1:N,1:S], Bin)
#variable(mo, a[1:S]>=0)
#Assignment constraint
#constraint(mo, [i=1:N], sum(x[i,j] for j=1:S) == 1)
##constraint (mo, PLEASE HELP )
In cases like this you usually need to use Big-M constraints
So this will be:
a_ij >= s_i^2 - M*(1-x_ij)
where M is a "big enough" number. This means that if x_ij == 0 the inequality will always be true (and hence kind of turned-off). On the other hand when x_ij == 1 the M-part will be zeroed and the equation will hold.
In JuMP terms the code will look like this:
const M = 10_000
#constraint(mo, [i=1:N, j=1:S], a[i, j] >= s[i]^2 - M*(1 - x[i, j]))
However, if s[i] is an external parameter rather than model variable you could simply use x[i,j] <= a[j]/s[i]^2 proposed by #DanGetz. However when s[i] is #variable you really want to avoid dividing or multiplying variables by each other. So this big M approach is more general across use cases.

How to solve nonlinear inequality constraint

I am new in optimisation problem. In my optimisation problem, I have one binary variable y and one integer variable n.
I have L: L= b*c*y
Where b, c are parameters.
and the constraint is: L/n<1 (which means L over n). I can also consider L<n. Another constraint in my problem is: I need that if L==0 then n=0 (n should be equal to 0, too). Moreover, L and N are always non-negative.
How can I linearise these constraints? Is it possible?

How do I convert if statement to linear equation?

I wanna convert this if statement to linear equation.
for i,j -> 1 to n
if D[i]>D[j] and f[i] > s[j] then w[i]+=c[j]
The line below is what has come to my mind so far, but I do not know how to write the rest. C(j) has to be multiplied by a phrase (that phrase is a code condition)
If the result of that phrase (in parentheses below) was 1, add C(j) to w(j) and if it was 0, do not add it to w(j).
Can you tell me how to write that condition in such a way that if the condition is true, it becomes 1, and if the condition is false, it become 0?
sum( j, c[j]*(?) )
What do you refer for "to be multiplied by a phrase"? Phrase is a parameter or a variable in the model? C(j) is data or is a variable? Depending on your answer, you should add binary variables to your model to register the result of that phrase and then you have to formulate the conditional sum of c(j) and w(j) depending on that binary variable.

How to formulate equations with three indices in GAMS?

Xijk = Number of units of product k purchased from vendor i for DC j
ObjCost.. Sum(i,Sum(k,j), xijk*Procurement-Cost);
Is the ObjCost equation formulation alright?
The Gams Compiler tells you if it is at least syntactically correct, but it doesn't look correct:
The definition is done like this:
equation_name..
lhs =E= rhs;
with =E= (equals) can easily be replaced by =G= (greater than or equal to), or =L= (less than or equal to).
So you might want something like this:
...
defObjCost..
OBJCOST =E= sum((i,k,j), X(i,j,k)*PROCUREMENT_COST(i, k);
...
model some_model /all/;
solve some_model using nlp minimizing OBJCOST;

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.