Notas para contenedor de docker:

Comando de docker para ejecución de la nota de forma local:

nota: cambiar <ruta a mi directorio> por la ruta de directorio que se desea mapear a /datos dentro del contenedor de docker.

docker run --rm -v <ruta a mi directorio>:/datos --name jupyterlab_numerical -p 8888:8888 -d palmoreck/jupyterlab_numerical:1.1.0

password para jupyterlab: qwerty

Detener el contenedor de docker:

docker stop jupyterlab_numerical

Documentación de la imagen de docker palmoreck/jupyterlab_numerical:1.1.0 en liga.


Nota generada a partir de liga1, liga2.

Dando click en: se ejecuta la nota de forma interactiva.


In [1]:
!pip3 install --user -q cvxpy


  WARNING: The scripts futurize and pasteurize are installed in '/home/miuser/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: You are using pip version 19.3.1; however, version 20.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

In [1]:
import os

In [2]:
cur_directory = os.getcwd()

In [3]:
dir_alg_python = '/algoritmos/Python'

In [4]:
os.chdir(cur_directory + dir_alg_python)

In [5]:
import math

import numpy as np

from algorithms_for_ceco import Newtons_method_feasible_init_point, \
                                Newtons_method_infeasible_init_point, \
                                Newtons_method_infeasible_init_point_2nd_version
from line_search import line_search_for_residual_by_backtracking
from utils import compute_error

Primer ejemplo

$$\displaystyle \min_{x \in \mathbb{R}^2} \quad x_1^2 + 2x_1x_2 + x_2^2-2x_2$$
$$\text{sujeto a: } x_1 = 0$$

In [6]:
fo = lambda x: x[0]**2 + 2*x[0]*x[1]+x[1]**2-2*x[1]

In [7]:
A = np.array([1,0],dtype=float)

In [8]:
b = np.array([0])

In [9]:
x_ast=np.array([0,1], dtype=float)

En el siguiente ejemplo si usamos el algoritmo anterior para punto inicial no factible ($Ax^{(0)} \neq b$) obsérvese que no funciona


In [10]:
x_0 = np.array([1,-2],dtype=float)

In [11]:
tol=1e-8
tol_backtracking=1e-14
maxiter=50
p_ast=fo(x_ast)
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_feasible_init_point(fo,A, x_0,tol, 
                                                       tol_backtracking, x_ast, p_ast, maxiter)


I	Normgf 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	4.47e+00	8.01e+00	3.16e+00	6.00e+00	---		2.03e+07
1	4.47e+00	5.12e-06	1.41e+00	2.00e+00	1.00e+00	2.03e+07
2	4.47e+00	9.86e-16	1.41e+00	2.00e+00	1.00e+00	2.03e+07
Error of x with respect to x_ast: 1.41e+00
Approximate solution: [ 1.00000000e+00 -3.34455919e-08]

In [12]:
compute_error(x_ast,x)


Out[12]:
1.4142135860226999

Error relativo muy alto

Método de Newton para puntos iniciales no factibles

Punto inicial no factible ($Ax^{(0)} \neq b$)


In [13]:
x_0 = np.array([1,-2], dtype=float)
nu_0 = 5

Versión del método de Newton para puntos iniciales no factibles que se apoya del método de Newton para puntos iniciales factibles


In [14]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point(fo, A, b,x_0, nu_0, tol, 
                                                         tol_backtracking, x_ast, p_ast, maxiter
                                                         )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	1.00e+00	5.00e+00	1.00e+01	3.16e+00	6.00e+00	---		2.03e+07
1	5.00e-01	2.50e+00	3.00e+00	1.58e+00	2.00e+00	5.00e-01	2.03e+07
2	2.50e-01	1.25e+00	9.99e-01	7.90e-01	7.49e-01	5.00e-01	2.03e+07
3	1.25e-01	6.25e-01	3.75e-01	3.95e-01	3.12e-01	5.00e-01	2.03e+07
4	6.25e-02	3.12e-01	1.56e-01	1.98e-01	1.41e-01	5.00e-01	2.03e+07
5	3.13e-02	1.56e-01	7.03e-02	9.88e-02	6.64e-02	5.00e-01	2.03e+07
6	1.56e-02	7.81e-02	3.32e-02	4.94e-02	3.22e-02	5.00e-01	2.03e+07
7	7.81e-03	3.90e-02	1.61e-02	2.47e-02	1.59e-02	5.00e-01	2.03e+07
8	3.91e-03	1.95e-02	7.93e-03	1.23e-02	7.87e-03	5.00e-01	2.03e+07
9	1.95e-03	9.76e-03	3.94e-03	6.17e-03	3.92e-03	5.00e-01	2.03e+07
10	9.77e-04	4.88e-03	1.96e-03	3.09e-03	1.96e-03	5.00e-01	2.03e+07
11	4.88e-04	2.44e-03	9.78e-04	1.54e-03	9.78e-04	5.00e-01	2.03e+07
12	2.44e-04	1.22e-03	4.89e-04	7.72e-04	4.89e-04	5.00e-01	2.03e+07
13	1.22e-04	6.10e-04	2.44e-04	3.86e-04	2.44e-04	5.00e-01	2.03e+07
14	6.10e-05	3.05e-04	1.22e-04	1.93e-04	1.22e-04	5.00e-01	2.03e+07
15	3.05e-05	1.53e-04	6.10e-05	9.65e-05	6.10e-05	5.00e-01	2.03e+07
16	1.53e-05	7.63e-05	3.05e-05	4.82e-05	3.05e-05	5.00e-01	2.03e+07
17	7.63e-06	3.81e-05	1.53e-05	2.41e-05	1.53e-05	5.00e-01	2.03e+07
18	3.81e-06	1.91e-05	7.63e-06	1.21e-05	7.63e-06	5.00e-01	2.03e+07
19	1.91e-06	9.53e-06	3.81e-06	6.03e-06	3.81e-06	5.00e-01	2.03e+07
20	9.54e-07	4.76e-06	1.91e-06	3.02e-06	1.91e-06	5.00e-01	2.03e+07
21	4.77e-07	2.38e-06	9.54e-07	1.51e-06	9.54e-07	5.00e-01	2.03e+07
22	2.38e-07	1.20e-06	4.77e-07	7.57e-07	4.77e-07	5.00e-01	2.03e+07
23	1.19e-07	5.93e-07	2.38e-07	3.80e-07	2.38e-07	5.00e-01	2.03e+07
24	5.96e-08	2.98e-07	1.19e-07	1.92e-07	1.19e-07	5.00e-01	2.03e+07
25	2.98e-08	1.55e-07	5.96e-08	9.59e-08	5.96e-08	5.00e-01	2.03e+07
26	1.49e-08	6.99e-08	2.98e-08	4.81e-08	2.98e-08	5.00e-01	2.03e+07
27	7.45e-09	2.90e-08	1.49e-08	2.55e-08	1.49e-08	5.00e-01	2.03e+07
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
Beginning Newtons method for feasible initial point
I	Normgf 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	2.00e+00	-1.57e-17	2.55e-08	1.49e-08	---		3.60e+04
Error of x with respect to x_ast: 2.55e-08
Approximate solution: [7.45058060e-09 9.99999976e-01]

In [15]:
compute_error(x_ast,x)


Out[15]:
2.554210325052966e-08

Versión del método de Newton para puntos iniciales no factibles (2o versión)


In [16]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point_2nd_version(fo, A, b,x_0, nu_0, tol, 
                                                                     tol_backtracking, x_ast, p_ast, maxiter
                                                                     )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	1.00e+00	5.00e+00	1.00e+01	3.16e+00	6.00e+00	---		2.03e+07
1	5.00e-01	2.50e+00	3.00e+00	1.58e+00	2.00e+00	5.00e-01	2.03e+07
2	2.50e-01	1.25e+00	9.99e-01	7.90e-01	7.49e-01	5.00e-01	2.03e+07
3	1.25e-01	6.25e-01	3.75e-01	3.95e-01	3.12e-01	5.00e-01	2.03e+07
4	6.25e-02	3.12e-01	1.56e-01	1.98e-01	1.41e-01	5.00e-01	2.03e+07
5	3.13e-02	1.56e-01	7.03e-02	9.88e-02	6.64e-02	5.00e-01	2.03e+07
6	1.56e-02	7.81e-02	3.32e-02	4.94e-02	3.22e-02	5.00e-01	2.03e+07
7	7.81e-03	3.90e-02	1.61e-02	2.47e-02	1.59e-02	5.00e-01	2.03e+07
8	3.91e-03	1.95e-02	7.93e-03	1.23e-02	7.87e-03	5.00e-01	2.03e+07
9	1.95e-03	9.76e-03	3.94e-03	6.17e-03	3.92e-03	5.00e-01	2.03e+07
10	9.77e-04	4.88e-03	1.96e-03	3.09e-03	1.96e-03	5.00e-01	2.03e+07
11	4.88e-04	2.44e-03	9.78e-04	1.54e-03	9.78e-04	5.00e-01	2.03e+07
12	2.44e-04	1.22e-03	4.89e-04	7.72e-04	4.89e-04	5.00e-01	2.03e+07
13	1.22e-04	6.10e-04	2.44e-04	3.86e-04	2.44e-04	5.00e-01	2.03e+07
14	6.10e-05	3.05e-04	1.22e-04	1.93e-04	1.22e-04	5.00e-01	2.03e+07
15	3.05e-05	1.53e-04	6.10e-05	9.65e-05	6.10e-05	5.00e-01	2.03e+07
16	1.53e-05	7.63e-05	3.05e-05	4.82e-05	3.05e-05	5.00e-01	2.03e+07
17	7.63e-06	3.81e-05	1.53e-05	2.41e-05	1.53e-05	5.00e-01	2.03e+07
18	3.81e-06	1.91e-05	7.63e-06	1.21e-05	7.63e-06	5.00e-01	2.03e+07
19	1.91e-06	9.53e-06	3.81e-06	6.03e-06	3.81e-06	5.00e-01	2.03e+07
20	9.54e-07	4.76e-06	1.91e-06	3.02e-06	1.91e-06	5.00e-01	2.03e+07
21	4.77e-07	2.38e-06	9.54e-07	1.51e-06	9.54e-07	5.00e-01	2.03e+07
22	2.38e-07	1.20e-06	4.77e-07	7.57e-07	4.77e-07	5.00e-01	2.03e+07
23	1.19e-07	5.93e-07	2.38e-07	3.80e-07	2.38e-07	5.00e-01	2.03e+07
24	5.96e-08	2.98e-07	1.19e-07	1.92e-07	1.19e-07	5.00e-01	2.03e+07
25	2.98e-08	1.55e-07	5.96e-08	9.59e-08	5.96e-08	5.00e-01	2.03e+07
26	1.49e-08	6.99e-08	2.98e-08	4.81e-08	2.98e-08	5.00e-01	2.03e+07
27	7.45e-09	2.90e-08	1.49e-08	2.55e-08	1.49e-08	5.00e-01	2.03e+07
28	0.00e+00	1.11e-08	6.16e-17	1.14e-08	1.11e-16	1.00e+00	2.03e+07
29	0.00e+00	1.11e-08	6.16e-17	1.14e-08	1.11e-16	2.22e-16	2.03e+07
Error of x with respect to x_ast: 1.14e-08
Approximate solution: [0.         0.99999999]
Backtracking value less than tol_backtracking, check approximation

In [17]:
compute_error(x_ast,x)


Out[17]:
1.1430297930381528e-08

Segundo ejemplo

$$\displaystyle \min_{x \in \mathbb{R}^2} \quad \frac{1}{2}(x_1^2 + x_2^2)$$
$$\text{sujeto a: } 2x_1 -x_2 = 5$$

In [18]:
fo = lambda x: 1/2*(x[0]**2  + x[1]**2)

In [19]:
A = np.array([2,-1],dtype=float)

In [20]:
b=5

In [21]:
x_ast = np.array([2,-1],dtype=float)

Método de Newton para puntos iniciales no factibles

Punto inicial no factible ($Ax^{(0)} \neq b$)


In [22]:
x_0 = np.array([1,1],dtype=float)
nu_0 = 4

Versión del método de Newton para puntos iniciales no factibles que se apoya del método de Newton para puntos iniciales factibles


In [23]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point(fo, A, b,x_0, nu_0, tol, 
                                                         tol_backtracking, x_ast, p_ast, maxiter
                                                         )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	4.00e+00	9.49e+00	1.00e+00	1.00e+00	2.00e+00	---		1.00e+00
1	0.00e+00	7.56e-04	3.19e-08	7.99e-05	3.50e+00	1.00e+00	1.00e+00
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
Beginning Newtons method for feasible initial point
I	Normgf 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	2.24e+00	3.19e-08	7.99e-05	3.50e+00	---		1.00e+00
1	2.24e+00	9.87e-15	4.08e-08	3.50e+00	1.00e+00	1.00e+00
Error of x with respect to x_ast: 4.08e-08
Approximate solution: [ 1.99999996 -1.00000008]

In [24]:
compute_error(x_ast,x)


Out[24]:
4.078981343802468e-08

Versión del método de Newton para puntos iniciales no factibles (2o versión)


In [25]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point_2nd_version(fo, A, b,x_0, nu_0, tol, 
                                                                     tol_backtracking, x_ast, p_ast, maxiter
                                                                     )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	4.00e+00	9.49e+00	1.00e+00	1.00e+00	2.00e+00	---		1.00e+00
1	0.00e+00	7.56e-04	3.19e-08	7.99e-05	3.50e+00	1.00e+00	1.00e+00
2	0.00e+00	6.58e-08	3.55e-15	6.20e-09	3.50e+00	1.00e+00	1.00e+00
3	8.88e-16	1.03e-09	8.88e-16	1.29e-08	3.50e+00	2.50e-01	1.00e+00
Error of x with respect to x_ast: 1.29e-08
Approximate solution: [ 1.99999999 -1.00000003]

In [26]:
compute_error(x_ast,x)


Out[26]:
1.2867310816844223e-08

Tercer ejemplo

$$\displaystyle \min_{x \in \mathbb{R}^2} \quad x_1^2 + x_2^2$$
$$\text{sujeto a :} x_1+x_2 = 1$$

In [27]:
fo = lambda x: x[0]**2+x[1]**2

In [28]:
x_ast = np.array([.5,.5],dtype=float)

In [29]:
A = np.array([1,1],dtype=float)

In [30]:
b=1

Método de Newton para puntos iniciales no factibles

Punto inicial no factible ($Ax^{(0)} \neq b$)


In [31]:
x_0 = np.array([1,1],dtype=float)
nu_0 = 4

Versión del método de Newton para puntos iniciales no factibles que se apoya del método de Newton para puntos iniciales factibles


In [32]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point(fo, A, b,x_0, nu_0, tol, 
                                                         tol_backtracking, x_ast, p_ast, maxiter
                                                         )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	1.00e+00	8.49e+00	2.00e+00	1.00e+00	3.00e+00	---		1.00e+00
1	1.11e-15	1.26e-04	-1.08e-15	1.12e-15	1.50e+00	1.00e+00	1.00e+00
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
Beginning Newtons method for feasible initial point
I	Normgf 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	1.41e+00	-0.00e+00	1.12e-15	1.50e+00	---		1.00e+00
Error of x with respect to x_ast: 1.12e-15
Approximate solution: [0.5 0.5]

In [33]:
compute_error(x_ast,x)


Out[33]:
1.1157603309187458e-15

Versión del método de Newton para puntos iniciales no factibles (2o versión)


In [34]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point_2nd_version(fo, A, b,x_0, nu_0, tol, 
                                                                     tol_backtracking, x_ast, p_ast, maxiter
                                                                     )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	1.00e+00	8.49e+00	2.00e+00	1.00e+00	3.00e+00	---		1.00e+00
1	2.22e-16	1.26e-04	-1.91e-16	2.48e-16	1.50e+00	1.00e+00	1.00e+00
2	0.00e+00	7.85e-09	8.27e-25	5.55e-09	1.50e+00	1.00e+00	1.00e+00
Error of x with respect to x_ast: 5.55e-09
Approximate solution: [0.5 0.5]

In [35]:
compute_error(x_ast,x)


Out[35]:
5.550929882414124e-09

Cuarto ejemplo

$$\displaystyle \min_{x \in \mathbb{R}^2} \quad e^{x_1+3x_2-0.1} + e^{x_1 -3x_2-0.1} + e^{-x_1-0.1}$$
$$\text{sujeto a:} x_1 + 3x_2 = 0$$

In [36]:
fo = lambda x: math.exp(x[0] + 3*x[1]-0.1) + math.exp(x[0]  -3*x[1]-0.1) + math.exp(-x[0]-0.1)

In [37]:
x_ast = np.array([-0.23104907880100917,0.0770163596518852],dtype=float)

In [38]:
A = np.array([1,3],dtype=float)

In [39]:
b=0

Método de Newton para puntos iniciales no factibles

Punto inicial no factible ($Ax^{(0)} \neq b$)


In [40]:
x_0 = np.array([1,1],dtype=float) #with 1,1 doesn't work
nu_0 = 1

Versión del método de Newton para puntos iniciales no factibles que se apoya del método de Newton para puntos iniciales factibles


In [41]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point(fo, A, b,x_0, nu_0, tol, 
                                                         tol_backtracking, x_ast, p_ast,maxiter
                                                        )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	4.00e+00	1.59e+02	1.97e+02	6.32e+00	5.09e+01	---		6.62e+02
1	4.00e+00	1.59e+02	1.97e+02	6.32e+00	5.09e+01	2.78e-17	6.62e+02
Backtracking value less than tol_backtracking, try other initial point

Versión del método de Newton para puntos iniciales no factibles (2o versión)


In [42]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point_2nd_version(fo, A, b,x_0, nu_0, tol, 
                                                                     tol_backtracking, x_ast, p_ast,maxiter
                                                                     )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	4.00e+00	1.59e+02	1.97e+02	6.32e+00	5.09e+01	---		6.62e+02
1	4.00e+00	1.59e+02	1.97e+02	6.32e+00	5.09e+01	2.78e-17	6.62e+02
Error of x with respect to x_ast: 6.32e+00
Approximate solution: [1. 1.]
Backtracking value less than tol_backtracking, check approximation

(Probando el comportamiento con punto inicial factible ($Ax^{(0)} = b$))


In [43]:
x_0 = np.array([0,0],dtype=float) 
nu_0 = 1

Versión del método de Newton para puntos iniciales no factibles que se apoya del método de Newton para puntos iniciales factibles


In [44]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point(fo, A, b,x_0, nu_0, tol, 
                                                         tol_backtracking, x_ast, p_ast,maxiter
                                                         )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	0.00e+00	3.55e+00	1.81e-01	1.00e+00	3.71e+00	---		6.00e+00
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
Beginning Newtons method for feasible initial point
I	Normgf 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	9.05e-01	1.81e-01	1.00e+00	3.71e+00	---		6.00e+00
1	9.05e-01	3.30e-03	1.34e-01	3.62e+00	1.00e+00	6.00e+00
2	9.05e-01	8.46e-07	2.15e-03	3.61e+00	1.00e+00	6.00e+00
3	9.05e-01	7.41e-14	6.52e-07	3.61e+00	1.00e+00	6.00e+00
Error of x with respect to x_ast: 6.52e-07
Approximate solution: [-0.23104893  0.07701631]

In [45]:
compute_error(x_ast,x)


Out[45]:
6.524588707146708e-07

Versión del método de Newton para puntos iniciales no factibles (2o versión)


In [46]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point_2nd_version(fo, A, b,x_0, nu_0, tol, 
                                                                     tol_backtracking, x_ast, p_ast,maxiter
                                                                     )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	0.00e+00	3.55e+00	1.81e-01	1.00e+00	3.71e+00	---		6.00e+00
1	1.11e-16	1.96e-01	3.30e-03	1.34e-01	3.62e+00	1.00e+00	6.00e+00
2	0.00e+00	3.38e-03	8.33e-07	2.14e-03	3.61e+00	1.00e+00	6.00e+00
3	0.00e+00	1.06e-06	6.98e-14	7.34e-07	3.61e+00	1.00e+00	6.00e+00
4	0.00e+00	5.92e-08	1.03e-15	1.16e-07	3.61e+00	1.00e+00	6.00e+00
5	2.78e-17	1.78e-08	5.48e-17	9.73e-08	3.61e+00	2.50e-01	6.00e+00
6	2.78e-17	1.78e-08	5.48e-17	9.73e-08	3.61e+00	2.22e-16	6.00e+00
Error of x with respect to x_ast: 9.73e-08
Approximate solution: [-0.23104906  0.07701635]
Backtracking value less than tol_backtracking, check approximation

In [47]:
compute_error(x_ast,x)


Out[47]:
9.732233305891635e-08

Punto inicial no factible ($Ax^{(0)} \neq b$)


In [48]:
x_0 = np.array([-.1,.1],dtype=float) 
nu_0 = 1

Versión del método de Newton para puntos iniciales no factibles que se apoya del método de Newton para puntos iniciales factibles


In [49]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point(fo, A, b,x_0, nu_0, tol, 
                                                         tol_backtracking, x_ast, p_ast,maxiter
                                                         )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	2.00e-01	4.81e+00	1.28e-01	5.46e-01	3.71e+00	---		6.14e+00
1	2.22e-16	5.96e-02	1.28e-05	8.39e-03	3.61e+00	1.00e+00	6.14e+00
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
Beginning Newtons method for feasible initial point
I	Normgf 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	1.06e+00	1.28e-05	8.39e-03	3.61e+00	---		5.30e+00
1	1.06e+00	1.04e-11	7.62e-06	3.61e+00	1.00e+00	5.30e+00
Error of x with respect to x_ast: 7.62e-06
Approximate solution: [-0.23104732  0.07701577]

In [50]:
compute_error(x_ast,x)


Out[50]:
7.6180302179383765e-06

Versión del método de Newton para puntos iniciales no factibles (2o versión)


In [51]:
[x,total_of_iterations,
 Err_plot,x_plot]= Newtons_method_infeasible_init_point_2nd_version(fo, A, b,x_0, nu_0, tol, 
                                                                    tol_backtracking, x_ast, p_ast,maxiter
                                                                    )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	2.00e-01	4.81e+00	1.28e-01	5.46e-01	3.71e+00	---		6.14e+00
1	2.22e-16	5.96e-02	1.28e-05	8.39e-03	3.61e+00	1.00e+00	6.14e+00
2	0.00e+00	1.34e-05	1.01e-11	7.62e-06	3.61e+00	1.00e+00	6.14e+00
3	2.78e-17	6.56e-08	1.02e-15	1.62e-07	3.61e+00	1.00e+00	6.14e+00
4	0.00e+00	6.73e-08	2.56e-16	1.25e-07	3.61e+00	5.00e-01	6.14e+00
5	2.78e-17	2.88e-08	2.66e-16	8.73e-08	3.61e+00	1.00e+00	6.14e+00
6	2.78e-17	2.88e-08	2.66e-16	8.73e-08	3.61e+00	2.22e-16	6.14e+00
Error of x with respect to x_ast: 8.73e-08
Approximate solution: [-0.23104906  0.07701635]
Backtracking value less than tol_backtracking, check approximation

In [52]:
compute_error(x_ast,x)


Out[52]:
8.734385833741386e-08

Quinto ejemplo: con más restricciones de igualdad

Resolver: $$\displaystyle \min_{x \in \mathbb{R}^3} \quad ||x||_2^2$$

$$\text{sujeto a: }\begin{array}{l} \begin{array}{c} x_1 + x_2 + x_3 = 1 \\ x_1 + x_2 + 2x_3 = 3 \end{array} \end{array} $$

In [53]:
fo = lambda x: x.dot(x)

In [54]:
x_ast = np.array([-0.5,-0.5,2. ], dtype=float)

In [55]:
A = np.array([[1, 1, 1],
              [1, 1, 2]],dtype=float)

In [56]:
b = np.array([1,3])

Método de Newton para puntos iniciales no factibles

(Probando el comportamiento con un punto inicial factible ($Ax^{(0)} = b$))


In [57]:
x_0 = np.array([3,-4,2], dtype=float) #Ax_0 = b
#x_0 = np.array([2,-3,2], dtype=float)
nu_0 = np.array([0,0],dtype=float)

Versión del método de Newton para puntos iniciales no factibles que se apoya del método de Newton para puntos iniciales factibles


In [58]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point(fo, A, b,x_0, nu_0, tol, 
                                                         tol_backtracking, x_ast, p_ast
                                                         )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	0.00e+00	1.08e+01	4.90e+01	2.33e+00	3.00e+01	---		1.00e+00
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
Beginning Newtons method for feasible initial point
I	Normgf 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	1.08e+01	4.90e+01	2.33e+00	3.00e+01	---		1.00e+00
1	1.08e+01	3.14e-05	1.87e-03	5.50e+00	1.00e+00	1.00e+00
2	1.08e+01	5.83e-13	2.52e-07	5.50e+00	1.00e+00	1.00e+00
Error of x with respect to x_ast: 2.52e-07
Approximate solution: [-0.49999962 -0.50000038  2.        ]

In [59]:
compute_error(x_ast,x)


Out[59]:
2.518419818612709e-07

Versión del método de Newton para puntos iniciales no factibles (2o versión)


In [60]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point_2nd_version(fo, A, b,x_0, nu_0, tol, 
                                                                     tol_backtracking, x_ast, p_ast
                                                                     )


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	0.00e+00	1.08e+01	4.90e+01	2.33e+00	3.00e+01	---		1.00e+00
1	6.28e-16	1.01e+01	4.30e+01	2.19e+00	2.70e+01	6.25e-02	1.00e+00
2	1.26e-15	9.47e+00	3.79e+01	2.05e+00	2.44e+01	6.25e-02	1.00e+00
3	0.00e+00	8.87e+00	3.33e+01	1.92e+00	2.21e+01	6.25e-02	1.00e+00
4	6.28e-16	8.32e+00	2.92e+01	1.80e+00	2.01e+01	6.25e-02	1.00e+00
5	1.26e-15	7.80e+00	2.57e+01	1.69e+00	1.83e+01	6.25e-02	1.00e+00
6	6.28e-16	7.31e+00	2.26e+01	1.58e+00	1.68e+01	6.25e-02	1.00e+00
7	6.28e-16	6.85e+00	1.99e+01	1.49e+00	1.54e+01	6.25e-02	1.00e+00
8	6.28e-16	6.43e+00	1.74e+01	1.39e+00	1.42e+01	6.25e-02	1.00e+00
9	6.28e-16	6.02e+00	1.53e+01	1.31e+00	1.32e+01	6.25e-02	1.00e+00
10	1.26e-15	5.65e+00	1.35e+01	1.22e+00	1.22e+01	6.25e-02	1.00e+00
11	1.26e-15	5.30e+00	1.18e+01	1.15e+00	1.14e+01	6.25e-02	1.00e+00
12	6.28e-16	4.96e+00	1.04e+01	1.08e+00	1.07e+01	6.25e-02	1.00e+00
13	6.28e-16	4.65e+00	9.16e+00	1.01e+00	1.01e+01	6.25e-02	1.00e+00
14	9.16e-16	4.36e+00	8.05e+00	9.45e-01	9.52e+00	6.25e-02	1.00e+00
15	4.44e-16	4.09e+00	7.07e+00	8.86e-01	9.03e+00	6.25e-02	1.00e+00
16	2.22e-16	3.83e+00	6.21e+00	8.31e-01	8.61e+00	6.25e-02	1.00e+00
17	1.11e-15	3.59e+00	5.46e+00	7.79e-01	8.23e+00	6.25e-02	1.00e+00
18	9.16e-16	3.37e+00	4.80e+00	7.30e-01	7.90e+00	6.25e-02	1.00e+00
19	2.22e-16	3.16e+00	4.22e+00	6.85e-01	7.61e+00	6.25e-02	1.00e+00
20	1.11e-15	2.96e+00	3.71e+00	6.42e-01	7.35e+00	6.25e-02	1.00e+00
21	2.22e-16	2.78e+00	3.26e+00	6.02e-01	7.13e+00	6.25e-02	1.00e+00
22	4.44e-16	2.60e+00	2.86e+00	5.64e-01	6.93e+00	6.25e-02	1.00e+00
23	6.28e-16	2.44e+00	2.52e+00	5.29e-01	6.76e+00	6.25e-02	1.00e+00
24	9.16e-16	2.29e+00	2.21e+00	4.96e-01	6.61e+00	6.25e-02	1.00e+00
25	4.44e-16	2.15e+00	1.94e+00	4.65e-01	6.47e+00	6.25e-02	1.00e+00
26	2.22e-16	2.01e+00	1.71e+00	4.36e-01	6.35e+00	6.25e-02	1.00e+00
27	2.22e-16	1.89e+00	1.50e+00	4.08e-01	6.25e+00	6.25e-02	1.00e+00
28	2.22e-16	1.77e+00	1.32e+00	3.83e-01	6.16e+00	6.25e-02	1.00e+00
29	0.00e+00	1.66e+00	1.16e+00	3.59e-01	6.08e+00	6.25e-02	1.00e+00
Error of x with respect to x_ast: 3.59e-01
Approximate solution: [ 0.03854189 -1.03854189  2.        ]
Reached maximum of iterations, check approximation

In [61]:
compute_error(x_ast,x)


Out[61]:
0.35902792568287245

Hay que aumentar el número de iteraciones...

Punto inicial no factible ($Ax^{(0)} \neq b$)


In [62]:
x_0 = np.array([3,-4,0], dtype=float) #Ax_0 different to b
#x_0 = np.array([2,-3,1], dtype=float) #Ax_0 different to b
                                       #Compare different versions of
                                       #Newtons method infeasible initial points
nu_0 = np.array([0,0],dtype=float)

Versión del método de Newton para puntos iniciales no factibles que se apoya del método de Newton para puntos iniciales factibles


In [63]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point(fo, A, b,x_0, nu_0, tol, 
                                                         tol_backtracking, x_ast, p_ast)


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	4.47e+00	1.00e+01	4.90e+01	2.52e+00	2.60e+01	---		1.00e+00
1	3.91e+00	8.75e+00	3.66e+01	2.20e+00	2.03e+01	1.25e-01	1.00e+00
2	3.42e+00	7.66e+00	2.73e+01	1.93e+00	1.61e+01	1.25e-01	1.00e+00
3	3.00e+00	6.70e+00	2.02e+01	1.69e+00	1.29e+01	1.25e-01	1.00e+00
4	2.62e+00	5.86e+00	1.49e+01	1.48e+00	1.06e+01	1.25e-01	1.00e+00
5	2.29e+00	5.13e+00	1.09e+01	1.29e+00	8.90e+00	1.25e-01	1.00e+00
6	2.01e+00	4.49e+00	7.89e+00	1.13e+00	7.65e+00	1.25e-01	1.00e+00
7	1.76e+00	3.93e+00	5.65e+00	9.88e-01	6.75e+00	1.25e-01	1.00e+00
8	1.54e+00	3.44e+00	3.98e+00	8.65e-01	6.12e+00	1.25e-01	1.00e+00
9	1.34e+00	3.01e+00	2.75e+00	7.57e-01	5.67e+00	1.25e-01	1.00e+00
10	1.18e+00	2.63e+00	1.84e+00	6.62e-01	5.37e+00	1.25e-01	1.00e+00
11	1.03e+00	2.30e+00	1.18e+00	5.79e-01	5.17e+00	1.25e-01	1.00e+00
12	9.01e-01	2.01e+00	7.02e-01	5.07e-01	5.05e+00	1.25e-01	1.00e+00
13	7.88e-01	1.76e+00	3.61e-01	4.44e-01	4.98e+00	1.25e-01	1.00e+00
14	6.90e-01	1.54e+00	1.22e-01	3.88e-01	4.94e+00	1.25e-01	1.00e+00
15	6.03e-01	1.35e+00	-4.14e-02	3.40e-01	4.94e+00	1.25e-01	1.00e+00
16	5.28e-01	1.18e+00	-1.50e-01	2.97e-01	4.95e+00	1.25e-01	1.00e+00
17	4.62e-01	1.03e+00	-2.18e-01	2.60e-01	4.98e+00	1.25e-01	1.00e+00
18	4.04e-01	9.04e-01	-2.57e-01	2.28e-01	5.01e+00	1.25e-01	1.00e+00
19	3.54e-01	7.91e-01	-2.76e-01	1.99e-01	5.05e+00	1.25e-01	1.00e+00
20	3.10e-01	6.92e-01	-2.81e-01	1.74e-01	5.08e+00	1.25e-01	1.00e+00
21	2.71e-01	6.06e-01	-2.75e-01	1.52e-01	5.12e+00	1.25e-01	1.00e+00
22	2.37e-01	5.30e-01	-2.64e-01	1.33e-01	5.16e+00	1.25e-01	1.00e+00
23	2.07e-01	4.64e-01	-2.48e-01	1.17e-01	5.19e+00	1.25e-01	1.00e+00
24	1.81e-01	4.06e-01	-2.31e-01	1.02e-01	5.22e+00	1.25e-01	1.00e+00
25	1.59e-01	3.55e-01	-2.12e-01	8.93e-02	5.25e+00	1.25e-01	1.00e+00
26	1.39e-01	3.11e-01	-1.93e-01	7.82e-02	5.28e+00	1.25e-01	1.00e+00
27	1.22e-01	2.72e-01	-1.75e-01	6.84e-02	5.30e+00	1.25e-01	1.00e+00
28	1.06e-01	2.38e-01	-1.58e-01	5.99e-02	5.33e+00	1.25e-01	1.00e+00
29	9.31e-02	2.08e-01	-1.42e-01	5.24e-02	5.35e+00	1.25e-01	1.00e+00
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
Reached maximum of iterations, check primal feasibility
Continuing with Newtons method for feasible initial point
I	Normgf 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	4.17e+00	2.12e-02	5.24e-02	5.35e+00	---		1.00e+00
1	4.17e+00	1.67e-10	1.96e-02	5.34e+00	1.00e+00	1.00e+00
Error of x with respect to x_ast: 1.96e-02
Approximate solution: [-0.49999354 -0.50000646  1.95838369]

In [64]:
compute_error(x_ast,x)


Out[64]:
0.019618115491299666

Hay que aumentar el número de iteraciones...

Versión del método de Newton para puntos iniciales no factibles (2o versión)


In [65]:
[x,total_of_iterations,
 Err_plot,x_plot] = Newtons_method_infeasible_init_point_2nd_version(fo, A, b,x_0, nu_0, tol, 
                                                                     tol_backtracking, x_ast, p_ast)


I	||res_primal||	||res_dual|| 	Newton Decrement	Error x_ast	Error p_ast	line search	CondHf
0	4.47e+00	1.00e+01	4.90e+01	2.52e+00	2.60e+01	---		1.00e+00
1	3.91e+00	8.75e+00	3.66e+01	2.20e+00	2.03e+01	1.25e-01	1.00e+00
2	3.42e+00	7.66e+00	2.73e+01	1.93e+00	1.61e+01	1.25e-01	1.00e+00
3	3.00e+00	6.70e+00	2.02e+01	1.69e+00	1.29e+01	1.25e-01	1.00e+00
4	2.62e+00	5.86e+00	1.49e+01	1.48e+00	1.06e+01	1.25e-01	1.00e+00
5	2.29e+00	5.13e+00	1.09e+01	1.29e+00	8.90e+00	1.25e-01	1.00e+00
6	2.01e+00	4.49e+00	7.89e+00	1.13e+00	7.65e+00	1.25e-01	1.00e+00
7	1.76e+00	3.93e+00	5.65e+00	9.88e-01	6.75e+00	1.25e-01	1.00e+00
8	1.54e+00	3.44e+00	3.98e+00	8.65e-01	6.12e+00	1.25e-01	1.00e+00
9	1.34e+00	3.01e+00	2.75e+00	7.57e-01	5.67e+00	1.25e-01	1.00e+00
10	1.18e+00	2.63e+00	1.84e+00	6.62e-01	5.37e+00	1.25e-01	1.00e+00
11	1.03e+00	2.30e+00	1.18e+00	5.79e-01	5.17e+00	1.25e-01	1.00e+00
12	9.01e-01	2.01e+00	7.02e-01	5.07e-01	5.05e+00	1.25e-01	1.00e+00
13	7.88e-01	1.76e+00	3.61e-01	4.44e-01	4.98e+00	1.25e-01	1.00e+00
14	6.90e-01	1.54e+00	1.22e-01	3.88e-01	4.94e+00	1.25e-01	1.00e+00
15	6.03e-01	1.35e+00	-4.14e-02	3.40e-01	4.94e+00	1.25e-01	1.00e+00
16	5.28e-01	1.18e+00	-1.50e-01	2.97e-01	4.95e+00	1.25e-01	1.00e+00
17	4.62e-01	1.03e+00	-2.18e-01	2.60e-01	4.98e+00	1.25e-01	1.00e+00
18	4.04e-01	9.04e-01	-2.57e-01	2.28e-01	5.01e+00	1.25e-01	1.00e+00
19	3.54e-01	7.91e-01	-2.76e-01	1.99e-01	5.05e+00	1.25e-01	1.00e+00
20	3.10e-01	6.92e-01	-2.81e-01	1.74e-01	5.08e+00	1.25e-01	1.00e+00
21	2.71e-01	6.06e-01	-2.75e-01	1.52e-01	5.12e+00	1.25e-01	1.00e+00
22	2.37e-01	5.30e-01	-2.64e-01	1.33e-01	5.16e+00	1.25e-01	1.00e+00
23	2.07e-01	4.64e-01	-2.48e-01	1.17e-01	5.19e+00	1.25e-01	1.00e+00
24	1.81e-01	4.06e-01	-2.31e-01	1.02e-01	5.22e+00	1.25e-01	1.00e+00
25	1.59e-01	3.55e-01	-2.12e-01	8.93e-02	5.25e+00	1.25e-01	1.00e+00
26	1.39e-01	3.11e-01	-1.93e-01	7.82e-02	5.28e+00	1.25e-01	1.00e+00
27	1.22e-01	2.72e-01	-1.75e-01	6.84e-02	5.30e+00	1.25e-01	1.00e+00
28	1.06e-01	2.38e-01	-1.58e-01	5.99e-02	5.33e+00	1.25e-01	1.00e+00
29	9.31e-02	2.08e-01	-1.42e-01	5.24e-02	5.35e+00	1.25e-01	1.00e+00
Error of x with respect to x_ast: 5.24e-02
Approximate solution: [-0.42715589 -0.57284411  1.95838369]
Reached maximum of iterations, check approximation

In [66]:
compute_error(x_ast,x)


Out[66]:
0.0523756665364094

Hay que aumentar el número de iteraciones...

Comparación del quinto ejemplo con cvxpy


In [67]:
import cvxpy as cp

In [68]:
x = cp.Variable(3)
A = np.array([[1, 1, 1],
              [1, 1, 2]])
b = np.array([1,3])

In [69]:
obj = cp.Minimize(cp.norm(x,2))

constraints = [A@x == b]

In [70]:
prob = cp.Problem(obj, constraints)

In [71]:
prob.solve()


Out[71]:
2.1213203277662585

In [72]:
print("optimal value", prob.value)


optimal value 2.1213203277662585

In [73]:
print("optimal var", x.value)


optimal var [-0.5 -0.5  2. ]

In [74]:
A@x.value


Out[74]:
array([1., 3.])

In [75]:
np.linalg.norm(x.value)


Out[75]:
2.121320343558957

Otros puntos iniciales


In [76]:
vec = [1,-2,2]
A@vec


Out[76]:
array([1, 3])

In [77]:
np.linalg.norm(vec)


Out[77]:
3.0

In [78]:
vec = [2,-3,2]
A@vec


Out[78]:
array([1, 3])

In [79]:
np.linalg.norm(vec)


Out[79]:
4.123105625617661

In [80]:
vec = [3,-4,2]
A@vec


Out[80]:
array([1, 3])

In [81]:
np.linalg.norm(vec)


Out[81]:
5.385164807134504

Referencias:

  • S. P. Boyd, L. Vandenberghe, Convex Optimization, Cambridge University Press, 2009.