@variable(model,Y[1:n], lower_bound=0)

L = Int64.(model.L)

D = Float64.(model.TD)

T = Float64.(model.TP)

n = 2
m = 3

@objective(model,Max,sum(Y[i]*L[i] for i in 1:2))

Yy = transpose(Y)

global a = Int64

global b = Int64

a = 1

b = n

for j in 1:m

``````@constraint(model,Yy[1:n].*T[a:b] .<= D[j])

global a = a+n

global b = b+n
``````

end

1. format you code with backticks,
2. make an MWE,
1 Like
``````for j in 1:m
@constraint(model,Yy[1:n].*T[a:b] .<= D[j])
global a = a+n
global b = b+n
end

#model is bound to a 2x2 dataframe

print(model)

output
Subject to
Y[1] >= 0.0
Y[2] >= 0.0
0.1 Y[1] <= 15.0
0.4 Y[2] <= 15.0
0.1 Y[1] <= 22.0
0.75 Y[2] <= 22.0
0.25 Y[1] <= 36.0
0.5 Y[2] <= 36.0

#The program returns me is output, but I need an output in the form of matrix 3x2
``````

The question still isnāt clear to me, and this is not an MWE. Have you read through the link posted above? Youāve asked a few similar questions and youād make your own life and that of people on here trying to help you a lot easier if you followed the advice above.

From what you have posted now it seems to me like you are printing a Jump model and somehow want the output (the constraints?) in a matrix. Could you show what output youāre looking for?

1 Like

Yes, Iām printing a JuMP template, and I need the array constraints

``````#I am using these packages
using JuMP
using Cbc
using DataFrames
using CSV

# When the code is compiled, I get this message

Max 12 Y [1] + 60 Y [2]
Subject to
Y [1]> = 0.0
Y [2]> = 0.0
0.1 Y [1] <= 15.0
0.4 Y [2] <= 15.0
0.1 Y [1] <= 22.0
0.75 and [2] <= 22.0
0.25 Y [1] <= 36.0
0.5 Y [2] <= 36.0

#my goal is to receive this message

Max 12 Y [1] + 60 Y [2]
Subject to
Y [1]> = 0.0
Y [2]> = 0.0
0.1 Y [1] + 0.4 Y [2] <= 15.0
0.1 Y [1] + 0.75 Y [2] <= 22.0
0.25 Y [1] + 0.5 Y [2] <= 36.0
``````

Okay I think Iām getting there, but this still isnāt an MWE which means itās still unnecessarily hard to answer this question, and involves some guesswork on my part. It originally seemed like youāre unhappy with the format in which JuMP prints its models, but now it looks rather like you have an issue in how youāre constructing your constraints.

Consider the following (note that this is an MWE, so you can try it out yourself by copy-pasting my code below!):

``````using JuMP, GLPK

model = Model(with_optimizer(GLPK.Optimizer))
@variable(model, Y[1:2] >= 0)
@objective(model, Max, 12*Y[1] + 60*Y[2])

@constraint(model, 0.1*Y[1] <= 15.0)
@constraint(model, 0.4*Y[2] <= 15.0)
``````

Which yields:

``````julia> print(model)
Max 12 Y[1] + 60 Y[2]
Subject to
Y[1] >= 0.0
Y[2] >= 0.0
0.1 Y[1] <= 15.0
0.4 Y[2] <= 15.0
``````

However, if we do:

``````model2 = Model(with_optimizer(GLPK.Optimizer))
@variable(model2, Y[1:2] >= 0)
@objective(model2, Max, 12*Y[1] + 60*Y[2])

@constraint(model2, 0.1*Y[1] + 0.4*Y[2] <= 15.0)
``````

we get:

``````julia> print(model2)
Max 12 Y[1] + 60 Y[2]
Subject to
Y[1] >= 0.0
Y[2] >= 0.0
0.1 Y[1] + 0.4 Y[2] <= 15.0
``````

The output looks like youāre desired output (Iāve reduced the number of constraints for brevity), however note that the difference in outputs is because of different constraints - your original model restricts `0.1*Y[1]` and `0.4*Y[2]` to each be smaller or equal to 15, while your desired output only restricts the sum of both to be below 15. You might want to go through the JuMP documentation in detail to ensure that you understand how constraints are specified.

Now the guesswork part: you asked some questions previously about errors you got when trying to define constraints in a loop, which is probably how you ended up with a bunch of separate univariate constraints that you didnāt actually want. This is the whole point of the MWE: if you had provided a simple code snippet saying āhereās the loop I run to define constraints, hereās the resulting model and hereās what I would have wanted my model to look likeā, you probably would have had answers to this as well as your last two (1, 2) questions within 5 minutes.

I will put here exactly what I need so that at last there are no more attempts to guess.
I am using this packages, I am developing a mathematical model and for this I needed to create dataframes and a database, which is the csv.

``````using JuMP
using GLPK
using DataFrames
using CSV

model = Model ()
``````

In the tables are the information that will be used to construct the constraints. This information is the code output response values

``````Toy = CSV.read #dataframe 2x2, column "TypeToy" type String and column "LC" Int64
Process = CSV.read #dataframe 3x2, column "TypeProcess" type String and column "TD" Float64
Toy_Process = CSV.read #dataframe 6x3, column "TypeToy" and "TypeProcess" are String and column "TP" Float64
``````

At this stage I need the program to recognize the size of the table by pulling this information from the database

``````n = size (Toy, 1)
m = size (Process, 1)

@variable (model, Y [1: n], lower_bound = 0)
``````

Here I take only the values āāthat will be used in the constraints

``````L = Int64 (Toy.LC)
D = Float64. (Process.TD)
T = Float64. (Toy_Process.TP)

@objective (model, Max, sum (Y [i] * L [i] for i in 1: 2))
``````

In order to calculate the matrix, I used the ātransposeā

``````Yy = transpose (Y)
``````

Finally, the loop will cycle through the tables in the database and print the response in matrix form

``````global a = Int64
global b = Int64
a = 1
b = n

for j in 1: m
@constraint (model, Yy [1: n]. * T [a: b]. <= D [j])
global a = a + n
global b = b + n
end

print (model)
``````

Taking the information from the CSV file, which works like the database, the code should give me this answer:

``````Max 12 Y [1] + 60 Y [2]
Subject to
Y [1]> = 0.0
Y [2]> = 0.0
0.1 Y [1] + 0.4 Y [2] <= 15.0
0.1 Y [1] + 0.75 Y [2] <= 22.0
0.25 Y [1] + 0.5 Y [2] <= 36.0
``````

But unfortunately I can not get the answer in matrix form

Okay again this isnāt really an MWE, for two reasons:

1. The code relies on reading csv files which are only available on your computer; and
2. There are errors in there which prevent the code from running.

That notwithstanding hereās my best guess for a solution:

``````using JuMP, GLPK, DataFrames

# Create some dummy data - this is reverse engineered from your desired solution
# If you post questions here that rely on data you have on your own machine make
# sure you include something similar
toy = DataFrame(LC = [12, 60])
process = DataFrame(TD = [15.0, 22.0, 36.0])
toy_process = DataFrame(TP = [0.1, 0.4, 0.1, 0.75, 0.25, 0.5])

model = Model() # your code has spaces before almost all parentheses which cause errors

n = size(toy, 1)
m = size(process, 1)

@variable(model, y[1:n], lower_bound = 0)

# Note that your columns are typed already so no need to convert types
l = toy.LC
# also note variables are usually lowercase in Julia with uppercase reserved for Types
d = process.TD
t = toy_process.TP

@objective(model, Max, sum(y[i]*l[i] for i ā 1: 2))

yā² = transpose(y)

for i ā 1:m
@constraint(model, yā² * t[2*i-1:2*i] <= d[i])
end

print(model)
``````

Should give:

``````Max 12 y[1] + 60 y[2]
Subject to
y[1] >= 0.0
y[2] >= 0.0
0.1 y[1] + 0.4 y[2] <= 15.0
0.1 y[1] + 0.75 y[2] <= 22.0
0.25 y[1] + 0.5 y[2] <= 36.0
``````
2 Likes

It worked perfectly, thank you!