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.
In [1]:
!pip3 install --user -q cvxpy
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
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)
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)
In [12]:
compute_error(x_ast,x)
Out[12]:
Error relativo muy alto
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
)
In [15]:
compute_error(x_ast,x)
Out[15]:
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
)
In [17]:
compute_error(x_ast,x)
Out[17]:
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)
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
)
In [24]:
compute_error(x_ast,x)
Out[24]:
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
)
In [26]:
compute_error(x_ast,x)
Out[26]:
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
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
)
In [33]:
compute_error(x_ast,x)
Out[33]:
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
)
In [35]:
compute_error(x_ast,x)
Out[35]:
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
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
)
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
)
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
)
In [45]:
compute_error(x_ast,x)
Out[45]:
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
)
In [47]:
compute_error(x_ast,x)
Out[47]:
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
)
In [50]:
compute_error(x_ast,x)
Out[50]:
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
)
In [52]:
compute_error(x_ast,x)
Out[52]:
Resolver: $$\displaystyle \min_{x \in \mathbb{R}^3} \quad ||x||_2^2$$
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])
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
)
In [59]:
compute_error(x_ast,x)
Out[59]:
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
)
In [61]:
compute_error(x_ast,x)
Out[61]:
Hay que aumentar el número de iteraciones...
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)
In [64]:
compute_error(x_ast,x)
Out[64]:
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)
In [66]:
compute_error(x_ast,x)
Out[66]:
Hay que aumentar el número de iteraciones...
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]:
In [72]:
print("optimal value", prob.value)
In [73]:
print("optimal var", x.value)
In [74]:
A@x.value
Out[74]:
In [75]:
np.linalg.norm(x.value)
Out[75]:
In [76]:
vec = [1,-2,2]
A@vec
Out[76]:
In [77]:
np.linalg.norm(vec)
Out[77]:
In [78]:
vec = [2,-3,2]
A@vec
Out[78]:
In [79]:
np.linalg.norm(vec)
Out[79]:
In [80]:
vec = [3,-4,2]
A@vec
Out[80]:
In [81]:
np.linalg.norm(vec)
Out[81]:
Referencias: