1

Please consider the following image:

enter image description here

I know when I use a:b in @constraint it means an array from a to b. I need to code the arrays like {a_j,b_j} in @constraint of the mentioned code. Can you please help me to code that?

2

2 Answers 2

1

I understand that you are asking about a custom indice iteration over a single constraint. This can be done in JuMP as:

using JuMP, Cbc
m = Model(Cbc.Optimizer)
@variable(m,x[1:3,1:3]>=0)
@constraint(m, con[ (i,j) in [(1,2),(2,3),(3,3)] ], x[i,j] >= 5)

Let us have a look what we got:

julia> println(m)
Feasibility
Subject to
 con[(1, 2)] : x[1,2] >= 5.0
 con[(2, 3)] : x[2,3] >= 5.0
 con[(3, 3)] : x[3,3] >= 5.0
 x[1,1] >= 0.0
 x[2,1] >= 0.0
 x[3,1] >= 0.0
 x[1,2] >= 0.0
 x[2,2] >= 0.0
 x[3,2] >= 0.0
 x[1,3] >= 0.0
 x[2,3] >= 0.0
 x[3,3] >= 0.0
Sign up to request clarification or add additional context in comments.

Comments

0

A key point to understand is that--unlike AMPL and GAMS--there is no specialized syntax for constructing and managing sets in JuMP. Instead, you can use any available Julia syntax and datastructures.

Examples:

using JuMP
N = 10
model = Model();
@variable(model, x[1:N]);
@constraint(model, [s=1:3], sum(x[j] for j in 1:N if mod(j, s) == 0) == 1)
using JuMP
N = 10
model = Model();
@variable(model, x[1:N]);
d = [Set([1, 2, 3]), Set([2, 4, 6])]
a = [Set([3, 4]), Set([5])]
J(s) = [j for j in 1:2 if s in union(d[j], a[j])]
@constraint(model, [s=1:2], sum(x[j] for j in J(s)) == 1)

1 Comment

I tried both codes. But they do not work in my case. d and a are both subsets of set S in the size of N. For example: (N=5, T=3, S=6), j=1:N, t=1:T, s=1:S. d=[1,2,2,3,1] (the elements of d are the first 3 digits of S and the size of d is N) and a=[6,4,5,6,4] (the elements of a are the 3 last digits of set S and the size of a is N). Now I want to code that the index s is starting from d and ending with a. It is like this s[j=1]={1,6}, s[j=2]={2,4}, s[j=3]={2,5}, s[j=4]={3,6}, s[j=5}={1,4}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.