0

I am using Julia 1.0 with the JuMP package to solve optimization problems. These are my first days using the language. Hence, I am not really comfortable with the syntax.

Following the Quick Start Guide, I was able to solve the problem bellow:

enter image description here

I used this code, which worked perfectly:

using JuMP

using GLPK

model = Model(with_optimizer(GLPK.Optimizer))

@variable(model, 0 <= x <= 6000)

@variable(model, 0 <= y <= 4000)

@objective(model, Max, (25*x) + (30*y))

@constraint(model, con, ((1/200)*x) + ((1/140)*y) <= 40)

optimize!(model)

termination_status(model)

primal_status(model)

dual_status(model)

println(objective_value(model))

println(value(x))

println(value(y))

As a consequence of the success from the implementation above, I tried to adapt the code to a new problem:

enter image description here

I know the differences between a classic Linear Program problem and one that explicitly defines only integer values.

In order to make it simple I treated the problem as a float one, considering x1 to be greater than 0 and smaller then 6.

I decided to let the integer aspect of the problem as a future step.

This is my code:

using JuMP

using GLPK

model = Model(with_optimizer(GLPK.Optimizer))

@variable(model, 0 <= x <= 6)

@variable(model,  y>=0 )

@objective(model, Max, (x) + (2*y))

@constraint(model, con, x + y <= 8)
@constraint(model, con, -x + y <= 2)
@constraint(model, con, x - y <= 4)


optimize!(model)

termination_status(model)

primal_status(model)

dual_status(model)

println(objective_value(model))

println(value(x))

println(value(y))

For some reason, I receive the following error messaging:

ERROR: LoadError: An object of name con is already attached to this model. If this is intended, consider using the anonymous construction syntax, e.g., x = @variable(model, [1:N], ...) where the name of the object does not appear inside the macro.

I tried to change some things and to read the documentation. Nonetheless, macros seem as a strange concept for me.

After some tries, I decided to ask for help.

Thanks in advance.

Pedro Delfino
  • 2,421
  • 1
  • 15
  • 30

1 Answers1

1

I think the message is pretty clear. So try something like:

@constraint(model, con1, x + y <= 8)
@constraint(model, con2, -x + y <= 2)
@constraint(model, con3, x - y <= 4)

Of course in real models you should use meaningful names.

Anonymous means without names. E.g.:

@constraint(model, x + y <= 8)
@constraint(model, -x + y <= 2)
@constraint(model, x - y <= 4)
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39