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?

Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
Mahsa
  • 69
  • 1
  • You need to provide more information: https://stackoverflow.com/help/minimal-reproducible-example. Here is the JuMP documentation: https://jump.dev/JuMP.jl/stable/manual/constraints – Oscar Dowson Oct 04 '21 at 00:32
  • Does this answer your question? [Dependent Arrays in Constraints JuMP](https://stackoverflow.com/questions/69489076/dependent-arrays-in-constraints-jump) – Oscar Dowson Oct 08 '21 at 01:44

2 Answers2

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
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
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)
Oscar Dowson
  • 2,395
  • 1
  • 5
  • 13
  • 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} – Mahsa Oct 07 '21 at 23:00