Engee documentation
Notebook

Shortest distance from a point to a plane

This example demonstrates how to formulate and solve a least squares method (LSM) optimisation problem using a problem-based approach.

We will use the functions of the library JuMP.jl to formulate the optimisation problem and the nonlinear optimisation library Ipopt.jl.

Installing the libraries

If your environment does not have the latest version of the JuMP package installed , uncomment and run the box 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 "My Account" button:

screenshot_20240710_134852.png Then click on the "Stop" button:

screenshot_20240710_2.png

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

screenshot_20240710_135431.png

Task Description

The aim of the problem is to find the shortest distance from the origin (point $(0,0,0)$) to the plane $x_1+2x_2+4x_3=7$.

Thus, the problem is to minimise the function: $$f(x)=x_1^2+x_2^2+x_3^2$$

Given the constraint: $$x_1+2x_2+4x_3=7$$

The function $f(x)$ is the target function and $x_1+2x_2+4x_3=7$ is the equality constraint.

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

Connecting libraries

Connect the library JuMP:

In [ ]:
using JuMP;

Connect the nonlinear solver library Ipopt:

In [ ]:
using Ipopt;

Creating an optimisation problem

Create an optimisation problem using the function Model() and specify the name of the solver in brackets:

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, - $x_1$, $x_2$ and $x_3$:

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

Create a target function to solve the minimisation 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 optimisation problem $x_1+2x_2+4x_3=7$. 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 formulation of the problem is complete. You can review and check the problem:

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

Problem solution

Solve the optimisation 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.

Store the values of x in a variable:

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

Output the results of the optimisation. You can use the function round, to round off excessively accurate values:

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

Thus, the shortest distance from the point $(0,0,0)$ to the plane $x_1+2x_2+4x_3=7$ is at $(0.3333, 0.6667, 1.3333)$. Now we can calculate the distance between the 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 given plane.

Conclusion

In this example, we solved the problem of finding the shortest distance from the origin of coordinates (point $(0,0,0)$) to the plane $x_1+2x_2+4x_3=7$ using the least squares method (LSM) using a problem-based approach.