Engee documentation
Notebook

Shortest distance from a point to a plane

This example demonstrates how to formulate and solve a least squares optimization problem using a problem-oriented approach.

We will use the functions of the [JuMP.jl] library(https://github.com/jump-dev/JuMP .jl) for the formulation of the optimization problem and the library of nonlinear optimization Ipopt.jl.

Installing Libraries

If the latest version of the package is not installed in your environment JuMP, uncomment and run the cell below:

In [ ]:
Pkg.add(["Ipopt", "JuMP"])
In [ ]:
#Pkg.add("JuMP");

To launch a new version of the library after the installation is complete, click on the "Personal Account" button:

Screenshot_20240710_134852.png Then click on the "Stop" button: Screenshot_20240710_2.png

Restart the session by clicking the "Start Engee" button:

Screenshot_20240710_135431.png

Task description

The goal of the task is to find the shortest distance from the origin (point ) to the plane .

Thus, the task is to minimize the function:

Subject to restrictions:

Function is an objective function, and it is an equality constraint.

More complex tasks may contain other equality constraints, inequality constraints, and upper or lower bound constraints.

Connecting libraries

Connect the library JuMP:

In [ ]:
using JuMP;

Connect the library of the nonlinear solver Ipopt:

In [ ]:
using Ipopt;

Creating an optimization task

Create an optimization task using the function Model() and specify the name of the solver in parentheses.:

In [ ]:
plane_prob = Model(Ipopt.Optimizer)
Out[0]:
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: EMPTY_OPTIMIZER
Solver name: Ipopt

Create a variable x containing three values, – , and :

In [ ]:
@variable(plane_prob, x[1:3]);

Create an objective function to solve the minimization problem:

In [ ]:
@objective(plane_prob, Min, sum(x[i]^2 for i in 1:3))
Out[0]:
$$ x_{1}^2 + x_{2}^2 + x_{3}^2 $$

Set the condition for solving the optimization problem . You can use the function dot() for the scalar product of two vectors, or write the equation in the traditional way:

In [ ]:
v = [1, 2, 4]
@constraint(plane_prob, dot(x, v) == 7)
Out[0]:
$$ x_{1} + 2 x_{2} + 4 x_{3} = 7.0 $$

The task statement is completed. You can view and check the issue.:

In [ ]:
println(plane_prob)
Min x[1]² + x[2]² + x[3]²
Subject to
 x[1] + 2 x[2] + 4 x[3] = 7.0

Problem solving

Solve the optimization problem:

In [ ]:
optimize!(plane_prob)
This is Ipopt version 3.14.13, running with linear solver MUMPS 5.6.1.

Number of nonzeros in equality constraint Jacobian...:        3
Number of nonzeros in inequality constraint Jacobian.:        0
Number of nonzeros in Lagrangian Hessian.............:        3

Total number of variables............................:        3
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:        1
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  0.0000000e+00 7.00e+00 0.00e+00  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  2.3333333e+00 0.00e+00 0.00e+00  -1.0 1.33e+00    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 1

                                   (scaled)                 (unscaled)
Objective...............:   2.3333333333333330e+00    2.3333333333333330e+00
Dual infeasibility......:   0.0000000000000000e+00    0.0000000000000000e+00
Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   0.0000000000000000e+00    0.0000000000000000e+00


Number of objective function evaluations             = 2
Number of objective gradient evaluations             = 2
Number of equality constraint evaluations            = 2
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 1
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 1
Total seconds in IPOPT                               = 0.001

EXIT: Optimal Solution Found.

Save the values x in the variable:

In [ ]:
sol = value.(x);

Print the optimization results. You can use the function round to round up overly precise values:

In [ ]:
println("Оптимизированные значения x = ", round.(sol, digits=4))
Оптимизированные значения x = [0.3333, 0.6667, 1.3333]

Thus, the shortest distance from the point is up to the plane located at the point . Now we can calculate the distance between two points.:

In [ ]:
origin = [0, 0, 0]
distance = sqrt(sum((sol .- origin).^2))
println("Расстояние ", round(distance, digits=4))
Расстояние 1.5275

You have found the shortest distance between the origin and the specified plane.

Conclusion

In this example, we solved the problem of finding the shortest distance from the origin (point ) to the plane using the least squares method (OLS), using a problem-oriented approach.