Example of nonlinear optimization with Ipopt

hello

can you provide me with an example or link to an example on NL optimization with Ipopt

Thank you

From my book Julia Quick Syntax Reference (Apress)

Choosing between pizzas and sandwiches, a non-linear problem

The only differences of non-linear problems are that: (1) we have to provide good starting points for the variables (try running the following example with 0 as starting point !); (2) we define the (non-linear) constraints with the specific @NLconstraints macro and (3) the (non-linear) objective with @NLobjective macro.
Note how we don’t need to provide any other further information (in particular no derivatives are needed).

The problem

A student has to choose how to allocate its weekly budget of 80 dollars. She can choose only between pizzas (p) or sandwiches (s). Pizzas cost 10$, while sandwiches are cheap and cost only 4$.

Her “utility function” (how much she is happy with any given combination of pizzas and sandwiches) is 100p - 2p^2 + 70s - 2 s^2 -3ps . She likes both, with preferences for pizza, but without excesses!

We use a simple problem of a maximisation of a non-linear function of 2 variables subject to a single constraints. To make the example simple we don’t use sets here (i.e. both variables and constraints are just scalar).

\max\limits_{p,s} {100p - 2p^2 + 70s - 2 s^2 -3ps }

subject to:

10p+4s \leq 80

Where the first equation is the utility function and the second one is what the economists call the “budget constraint”, i.e. we can’t spend more money that what we have :slight_smile: .

Importing the libraries and declaring the model

As we deal with a non-linear model we need to select a solver engine capable to handle such class of problem. IPOPT does the job:

using JuMP, Ipopt
m = Model(with_optimizer(Ipopt.Optimizer, print_level=0))

Declaration of model variables, constraints and objective

Being physical quantities, on top of the explicit constraint, we need to add a lower bound of 0 to our variables (lower and upper bounds can be specified in one expression as lB <= var <= uB).

@variable(m, 0 <= p, start=1, base_name="Quantities of pizzas")
@variable(m, 0 <= s, start=1, base_name="Quantities of sandwiches")

@constraint(m, budget,     10p + 4s <=  80 )

@NLobjective(m, Max, 100*p - 2*p^2 + 70*s - 2*s^2 - 3*p*s)

We interpret the problem as the optimal “weekly average”, so it is ok if the optimal result is half pizza. Otherwise we should limit the variables to be integer adding the option integer=true on their declaration, but then the class of the problem would change to Mixed Integer NonLinear Programming (MINLP) and we would have to change also the solver engine (bonmin is a good solver for MINLP problems).

Resolution of the model and visualisation of the results

Given that the problem is non-linear, the solution is reported as a “local optima”. It is up to us then decide if (like in this case) the nature of the problem guarantees that the local optima is also a global one.

optimize!(m)

status = termination_status(m)

if (status == MOI.OPTIMAL || status == MOI.LOCALLY_SOLVED || status == MOI.TIME_LIMIT) && has_values(m)
    if (status == MOI.OPTIMAL)
        println("** Problem solved correctly **")
    else
        println("** Problem returned a (possibly suboptimal) solution **")
    end
    println("- Objective value : ", objective_value(m))
    println("- Optimal solutions:")
    println("pizzas: $(value.(p))")
    println("sandwitches: $(value.(s))")
    println("- Dual (budget): $(dual.(budget))")
else
    println("The model was not solved correctly.")
    println(status)
end

# Expected output:
# ** Problem returned a (possibly suboptimal) solution **
# - Objective value : 750.8928616403513
# - Optimal solutions:
# pizzas: 4.642857242997538
# sandwitches: 8.39285709239478
# - Dual (budget): -5.624999975248088
3 Likes

Thanks so so much ! :smiley:

I have a question to you pls
If I am dealing with a big system where for each item there are four variables and there are 5 items for example…cant I divide my work by making functions to check constraints and call the these functions one by one in my file where I declared the model…I have faaced problems when I tried to make so