Algorithm for solving optimisation problems¶
In this example we will consider a standard algorithm for solving optimisation problems using the library JuMP
.
JuMP.jl is an open source library. This library implements an algebraic modelling language that allows users to formulate and solve a wide range of optimization problems using various solvers.
Library installation¶
If your environment does not have the latest version of the JuMP
package installed , uncomment and run the box below:
Pkg.add(["JuMP", "HiGHS"])
#Pkg.add("JuMP");
To launch a new version of the library after the installation is complete, click on the "My Account" button:
Then click on the "Stop" button:
Restart the session by pressing the "Start Engee" button:
Algorithm for solving the problem¶
The algorithm for solving an optimisation problem using JuMP
involves several systematic steps, from formulating the problem to obtaining and interpreting the results. Here is the standard algorithm for solving the problem:
- connecting the library
JuMP
, - selecting and connecting the solver library,
- creating an optimisation problem,
- declarations of variables,
- formulation of the target function,
- formulating the condition,
- solving the problem,
- deriving the results,
- analysing the results.
Task description¶
In this example we will find the values of the variables x and y that will lead to the smallest possible value of the linear target function: $$F = 13x + 22y \rightarrow min$$ given the following conditions: $$3x + 8y >= 20 Условие 1$$ $$6x + 10y <= 100 Условие 2$$ We will also denote the following bounds on the values of the variables x and y: $$x >= 1$$ $$0 <= y <= 20$$
Problem solving¶
Connect the library JuMP
:
using JuMP;
In this example, we use the HiGHS.jl solver library. Connect the library HiGHS
:
using HiGHS;
Create an optimisation problem using the function Model()
and specify the name of the solver in brackets:
optimization_problem = Model(HiGHS.Optimizer)
Use the macro @variable
to define the solver variables. Specify the limits of the values of the variables x and y:
@variable(optimization_problem, x >= 1);
@variable(optimization_problem, 0 <= y <= 20);
Define the target function to be optimised using the macro @objective
:
@objective(optimization_problem, Min, 13x + 22y);
Define the first condition for solving the optimisation problem using the macro @constraint
:
@constraint(optimization_problem, c1, 3x + 8y >= 20)
Set the second condition for solving the optimisation problem:
@constraint(optimization_problem, c2, 6x + 10y <= 100)
Solve the optimisation problem:
optimize!(optimization_problem)
Output the found optimal values of x and y:
println("Оптимальное значение x: ", value(x))
println("Оптимальное значение y: ", value(y))
Output the status of the solver result:
status = termination_status(optimization_problem)
println("Статус: ", status)
The OPTIMAL status means that the solver has found a globally optimal solution to the problem.
Conclusion¶
In this example we have described the algorithm for solving a basic optimisation problem using the libraries JuMP
and HiGHS
. The JuMP
library allows you to formulate an optimisation problem, set the conditions and solve it using a user-defined solver. You can read about practical applications of the JuMP
library in our course Fundamentals of Optimisation.