# Practice JuMP problem - scheduling LP questions

**URL:** <https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390>\
**Category:** Optimization (Mathematical)\
**Created:** [April 22, 2019, 4:33am UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390 "2019-04-22T04:33:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crghilardi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crghilardi/32/3738_2.png) [@Crghilardi](https://discourse.julialang.org/u/Crghilardi)\
**Post date:** [April 22, 2019, 4:33am UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390/1 "2019-04-22T04:33:20Z")

</div>

I am practising by translating [a python LP scheduling example](https://github.com/benalexkeen/Introduction-to-linear-programming/blob/master/Introduction%20to%20Linear%20Programming%20with%20Python%20-%20Part%205.ipynb) using JuMP.

I have been using a combination of the [SteelT3 example](https://github.com/JuliaOpt/JuMP.jl/blob/release-0.19/examples/steelT3.jl) and [some older notebooks I found](http://www.juliaopt.org/notebooks/JuMP-NetRevMgmt.html) as guiding references, but have not been able to get everything working. I have also found [this discourse thread](https://discourse.julialang.org/t/writing-a-simple-production-scheduling-optimal-control-problem-in-jump/17748) but it didn’t help me.

I have been able to add variables and constraints to the model, but cannot figure out the objective function syntax. If my understanding is correct, it is good practice to use dictionaries to store everything but I am getting confused on how the dictionaries should be referenced throughout the problem.

I created [a gist](https://gist.github.com/Crghilardi/07c8be7ad4dc0caeb81b0cf378c46c1f) with my attempt so far, any help is appreciated.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 22, 2019, 1:29pm UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390/2 "2019-04-22T13:29:58Z")

</div>

What do you mean by “not working”?

There are two issues:

1. you have some brackets in the wrong place. You need something like:

```julia
@objective(model, Min,
   sum(status[m,f] * production[m,f] * fixed_cost[m,f] * var_cost[m,f] for m in months,f in facts)
)

```

1. `status[m, f] * production[m, f]` is `variable * variable`. Cbc can only solve mixed-integer linear problems.

---

<div class="post-metadata">

**Author:** ![Crghilardi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crghilardi/32/3738_2.png) [@Crghilardi](https://discourse.julialang.org/u/Crghilardi)\
**Post date:** [April 23, 2019, 12:47am UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390/3 "2019-04-23T00:47:13Z")

</div>

By not working, I meant I am able to successfully add variables and constraints to the model, but not an objective.

I went back and both my previous example as well as your code from point 1 result in the message:

`ERROR: The solver does not support an objective function of type MathOptInterface.ScalarQuadraticFunction{Float64}.`

I realize the problem is integer and have a `Bin` and `Int` flag in my variables, perhaps my title was misworded. I chose Cbc as I was following [a previous exercise in the repo](https://github.com/benalexkeen/Introduction-to-linear-programming/blob/master/Introduction%20to%20Linear%20Programming%20with%20Python%20-%20Part%202.ipynb) where they note the use of Cbc for the python examples.

Now that I am looking at it, something else must be going wrong, since it is showing a `Float64` error but none of the dictionaries are showing as any kind of `Float`?

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [April 23, 2019, 2:14am UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390/4 "2019-04-23T02:14:34Z")

</div>

This error has nothing to do with `Float`s.

As @odow said you are multiplying two variables hence you get a quadratic function of the variables. The solver you have chosen, Cbc, does not support quadratic functions (like `status[m, f] * production[m, f]`) in the objective. That’s why you got the error saying that you solver (Cbc) does not support `ScalarQuadraticFunctions`:

> [@Crghilardi](#):
>
> ERROR: The solver does not support an objective function of type MathOptInterface.ScalarQuadraticFunction{Float64}.

You can solve this by selecting another solver, for [Gurobi](https://github.com/JuliaOpt/Gurobi.jl), [CPLEX](https://github.com/JuliaOpt/CPLEX.jl), [Xpress](https://github.com/JuliaOpt/Xpress.jl) will support quadratic functions in the objective, and will try to solve the problem if the objective function is convex.  
If the objective function is not convex you can try [Knitro](https://github.com/JuliaOpt/knitro.jl) for instance.

---

<div class="post-metadata">

**Author:** ![Crghilardi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crghilardi/32/3738_2.png) [@Crghilardi](https://discourse.julialang.org/u/Crghilardi)\
**Post date:** [April 23, 2019, 5:31am UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390/5 "2019-04-23T05:31:02Z")

</div>

OK, I don’t have access to any of the commercial solvers, so I tried a new model using `Ipopt` since that has QP listed [in the docs.](http://www.juliaopt.org/JuMP.jl/v0.19.0/installation/)  
The objective function worked was added to the model.

`@objective(model2, Min, sum(status[m,f] * production[m,f] * fixed_cost[m,f] * var_cost[m,f] for m in months,f in facts))`

`5000 status[1,A]*production[1,A] + 3000 status[1,B]*production[1,B] + 5500 status[2,A]*production[2,A] + 2400 status[2,B]*production[2,B] + 6000 status[3,A]*production[3,A] + 1800 status[3,B]*production[3,B] + ...`

I then tried to run a preliminary `solve!()` before moving on and got an error flag on the Bin variable:

`ERROR: MathOptInterface.UnsupportedConstraint{MathOptInterface.SingleVariable,MathOptInterface.ZeroOne}: `MathOptInterface.SingleVariable-in-MathOptInterface.ZeroOne` constraints is not supported by the model`

Is there a way for me to re-write the expressions to avoid the quadratic expression and be able to use Cbc?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 23, 2019, 1:18pm UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390/6 "2019-04-23T13:18:18Z")

</div>

> [@odow](#):
>
> @objective(model, Min, sum(status[m,f] \* production[m,f] \* fixed\_cost[m,f] \* var\_cost[m,f] for m in months,f in facts) )

My guess is that since you talked about solving an LP, you actually want

```julia
@objective(model, Min, sum(
    fixed_cost[m,f] * status[m,f] + var_cost[m,f] * production[m,f] 
    for m in months, f in facts))

```

This model can be solved using Cbc.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 23, 2019, 2:25pm UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390/7 "2019-04-23T14:25:34Z")

</div>

Just so this is clear, the original Python example is a mixed-integer LP which you could solve using CBC, but in your JuMP translation you’re using a completely different objective function. If the goal is truly to translate the Python example, you should be fixing the objective function rather than changing solvers, as @odow mentioned.

---

<div class="post-metadata">

**Author:** ![Crghilardi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crghilardi/32/3738_2.png) [@Crghilardi](https://discourse.julialang.org/u/Crghilardi)\
**Post date:** [April 24, 2019, 12:46am UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390/8 "2019-04-24T00:46:41Z")

</div>

Ahh ok, now that I am looking at them side by side I clearly see where my mistake was. I used the modified objective function above and the Cbc solver model happily accepted that.

While I am thinking about it , is there somewhere I can contribute finished problems as examples?

I spent a lot of time clicking through [the JuliaOpt notebooks page](http://www.juliaopt.org/notebooks/) but it seems like they are written using pre 0.19 syntax.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 24, 2019, 1:53pm UTC](https://discourse.julialang.org/t/practice-jump-problem-scheduling-lp-questions/23390/9 "2019-04-24T13:53:23Z")

</div>

> is there somewhere I can contribute finished problems as examples?

Yes, please! [https://github.com/JuliaOpt/JuMP.jl/tree/master/examples](https://github.com/JuliaOpt/JuMP.jl/tree/master/examples). Please pay attention to the structure of the existing examples: they get run in JuMP’s automated tests. (You can run the tests using this command [https://github.com/JuliaOpt/JuMP.jl/blob/fd5ea00cb602145b677c136295b07cab6791f1b0/examples/run\_examples.jl#L12](https://github.com/JuliaOpt/JuMP.jl/blob/fd5ea00cb602145b677c136295b07cab6791f1b0/examples/run_examples.jl#L12))

> I spent a lot of time clicking through [the JuliaOpt notebooks page](http://www.juliaopt.org/notebooks/) but it seems like they are written using pre 0.19 syntax.

We will have some news to share on this front soon.
