Mahsa
Mahsa

Reputation: 79

Coding arrays in constraint JuMP

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?

Upvotes: 1

Views: 137

Answers (2)

Oscar Dowson
Oscar Dowson

Reputation: 2574

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)

Upvotes: 0

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

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

Upvotes: 1

Related Questions