Search code examples
stringsetconstraintsjuliajulia-jump

How to use sets that contain strings instead of integers in Julia/JuMP?


I am working on a linear optimization problem. I am looping through sets to add constraints and define my varialbe. Here is the working code I have so far:

using JuMP
m = Model()
si=[12 23 1 3309 5]
sj=[1,2,3]
c= [3 5 2;
    4 3 5;
    4 5 3;
    5 4 3;
    3 5 4]
b= [80;
    75;
    80;
    120;
    60]
# x_ij >= 0  ∀ i = 1,...,5, j = 1,...,3
n = length(si)
p = length(sj)

@defVar(m, x[i=1:n,j=1:p] >= 0)
@setObjective(m,Min,sum{c[i,j]*x[i,j],i=1:n,j=1:p})
for j=1:p
    @addConstraint(m, sum{x[i,j],i=1:n} <= 480)
end
for i=1:n
    @addConstraint(m, sum{x[i,j],j=1:p} >= b[i])
    end

endstatus=solve(m)

I need to add another set to replace set sj however this set contains strings not integer values.

set_P = [ IMA_1ABC IMA_23 IMA_4AB  ]
P = length(set_P)

Everytime I try and run the code with this new set, it does not work because it doesn't recognize letters. Does anyone know how to use a set of strings in place of a set of integers?


Solution

  • Strings have to be written "string", not string. If you leave out the quotation marks, Julia will look for a variable named string

    For reference, compare typeof("string") to typeof(string).