Assume the following constraint:
#constraint(model, [i=1:5,j=1:5], a[i] <= b[j])
How do I exclude constraint where i == j from the above? This is simple to do in AMPL but I can't find any similar syntax in JuMP.
#constraint(model, [i=1:5, j=1:5; i != j], a[i] <= b[j])
Related
I'm trying to write a constraint in the from of the following (also attached):
sum([s,i], x[i,j,s,p] ) = sum([s,k], x[j,k,s,p] ) for all j in N\{0,n}, p in P
I already have all possible combinations of (i,j,s,p) stored in a set Xs and X0nswhich a vector of such 4-tuples.
Thus I tried to write it down as
#constraint( model, [p in P, jj in X0ns],
sum(x[(i, j, s, p)] for (i, j, s, p) in Xs if j == jj)
== sum(x[(j, k, s, p)] for (i, j, s, p) in Xs if j == jj)
This gives me an Error. On top of that I think this is not correct way of writing. Because I did not include a summation on S anywhere. Is it needed?
How can I build this kind of constraint?
Here is what I think that you mean (this is the exact implementation of your LaTeX equation).
Normally you should not have used tuples for array indexing.
m = Model();
S=1:3
N=1:4
K=1:4
P=1:5
#variable(m, x[N,N,S,P] >= 0);
#constraint(m, [j in N[2:end-1], p in P], sum(x[i,j,s,p] for i in N, s in S)
== sum(x[j,k,s,p] for k in K, s in S))
I am struggling with some constraints that have ranged variables and several decision variables.
The constraints are
`if a[i] <= t <= a[i]+b[i], then M[t][i] == 1,
else if t < a[i] or t > a[i]+b[i], then M[t][i] == 0.
decision variables (int a[i], b[i])
(boolean M[t][i])
Examples of ranges of the variables
0 <= a[i] <= 50
0 <= b[i] <= 10
0 <= t <= 100`
I'd like to linearize these formulations, like as Big-M method or etc...
Plz, let me know the way to overcome it.
Thank you.
Linearization by using the Big-M method or etc...
such as...
M*t + (M-1)*t <= M*(a[i]) + (M-1)*a[i]
It is not possible for Julia to solve it when I use a[i, j] = 1. how can i get julia to solve this problem?
using JuMP
using GLPK
u = [1 2 3 ; 1 2 3 ; 1 2 3]
m = Model(GLPK.Optimizer)
#variable(m, a[1:3,1:3], Bin)
#objective(m, Max, sum(u[i,j]*a[i,j] for i=1:3, j=1:3))
#constraint(m, [a[i,j]=1], sum(a[i:j][i:j]) == 1)
solution = optimize!(m)
opt_value = value.(a)
in the line
#constraint(m, [a[i,j]=1], sum(a[i:j][i:j]) == 1)
1) You're trying to set a variable, not to test equality, instead use
a[i,j] == 1
2) i and j are undefined. Without a minimal example to run, I would say according to the previous line of your code, I would say something like
sum(<what-to-sum-here> for i=1:3, j=1:3)
Or loop on the list of index you want to use if not the proper one.
I am trying to define a #NLexpression in JuMP that has different specifications at different indices. In the below example, I want the expression y[j,k] to be defined as 1/(x[j] - x[k]) when j != k and take some other value when j == k. I can simulate this behavior by defining an auxiliary variable z and adding constraints conditional on index values. Is there a similar way to define expression conditional on index values?
using JuMP, Ipopt
model = JuMP.Model(with_optimizer(Ipopt.Optimizer))
#variable(model, 0 <= x[1:2])
#NLexpression(model, y[j=1:2,k=1:2], 1/(x[j] - x[k])) # <- problematic line
#variable(model, z[1:2,1:2])
for j=1:2, k=1:2
if j == k
#constraint(model, z[j,k] == 1)
else
#NLconstraint(model, z[j,k] == 1/(p[j] - p[k]))
end
end
display(model)
You're not obligated to use JuMP's macros to create containers for expressions. You should be able to conditionally create the expressions as follows:
model = JuMP.Model()
#variable(model, 0 <= x[1:2])
y = Dict() # Or Array, if you prefer.
for j=1:2, k=1:2
if j == k
y[j,k] = 1
else
y[j,k] = #NLexpression(model, 1/(p[j] - p[k]))
end
end
I am working in an optimization problem (A*v = b) where I would like to rank a set of alternatives X = {x1,x2,x3,x4}. However, I have the following normalization constraint: |v[i] - v[j]| <= 1, which can be in the form -1 <= v[i] - v[j] <= 1.
My code is as follows:
import cvxpy as cp
n = len(X) #set of alternatives
v = cp.Variable(n)
objective = cp.Minimize(cp.sum_squares(A*v - b))
constraints = [0 <= v]
#Normalization condition -1 <= v[i] - v[j] <= 1
for i in range(n):
for j in range(n):
constraints = [-1 <= v[i]-v[j], 1 >= v[i]-v[j]]
prob = cp.Problem(objective, constraints)
# The optimal objective value is returned by `prob.solve()`.
result = prob.solve()
# The optimal value for v is stored in `v.value`.
va2 = v.value
Which outputs:
[-0.15 0.45 -0.35 0.05]
Result, which is not close to what should be and even have negative values. I think, my code for the normalization contraint most probably is wrong.
You are not appending your constraints, instead you are overwriting them each time. Instead of this line
constraints = [-1 <= v[i]-v[j], 1 >= v[i]-v[j]]
You should have
constraints += [-1 <= v[i]-v[j], 1 >= v[i]-v[j]]
For cleanliness you may want to change this
for i in range(n):
for j in range(n):
To only consider each pair once:
for i in range(n):
for j in range(i+1, n):