<jupyter_start><jupyter_text>**Notas para contenedor de docker:** Comando de docker para ejecución de la nota de forma local:nota: cambiar `` por la ruta de directorio que se desea mapear a `/datos` dentro del contenedor de docker.```docker run --rm -v :/datos --name jupyterlab_numerical -p 8888:8888 -d palmoreck/jupyterlab_numerical:1.1.0```password para jupyterlab: `<PASSWORD>`Detener el contenedor de docker:```docker stop jupyterlab_numerical``` Documentación de la imagen de docker `palmoreck/jupyterlab_numerical:1.1.0` en [liga](https://github.com/palmoreck/dockerfiles/tree/master/jupyterlab/numerical). --- Nota generada a partir de [liga1](https://drive.google.com/file/d/1zCIHNAxe5Shc36Qo0XjehHgwrafKSJ_t/view), [liga2](https://drive.google.com/file/d/1RMwUXEN_SOHKue-J9Cx3Ldvj9bejLjiM/view).<jupyter_code>!pip3 install --user -q cvxpy
import os
cur_directory = os.getcwd()
dir_alg_python = '/algoritmos/Python'
os.chdir(cur_directory + dir_alg_python)
import math

import numpy as np

from utils_2nd_version import compute_error

from algorithms_for_cieco_2nd_version import path_following_method_infeasible_init_point_2nd_version, \
                                             path_following_method_infeasible_init_point<jupyter_output><empty_output><jupyter_text>Está lista la implementación para puntos iniciales no factibles en $Ax=b$ pero no para la parte de las desigualdades $f_i(x) <0 \quad \forall i=1,\dots,m$  Primer ejemplo $$ \min \quad x_1^2 + x_2^2 + x_3^2 + x_4^2 -2x_1-3x_4$$ $$\text{sujeto a: } $$ $$\begin{array}{c}2x_1 + x_2 + x_3 + 4x_4 = 7 \\x_1 + x_2 + 2x_3 + x_4 = 6\end{array}$$ $$x_1, x_2, x_3, x_4 \geq 0$$  Infeasible for Ax=b<jupyter_code>from utils_2nd_version import constraint_inequalities_funcs_eval
fo = lambda x: x[0]**2 + x[1]**2 + x[2]**2 + x[3]**2-2*x[0]-3*x[3]
const = {0: lambda x: -x[0],
         1: lambda x: -x[1],
         2: lambda x: -x[2],
         3: lambda x: -x[3]
        }
A= np.array([[2,1,1,4],
             [1,1,2,1]])
b=np.array([7,6])
x_ast=np.array([1.1232876712328763,0.6506849315068493,
                1.8287671232876714,0.5684931506849317])
x_0 = np.array([-5,-5,-5,-5],dtype=float) #with -5,-5,-5,-5 return one entry of x negative
nu_0 = np.array([0,0], dtype=float)
p_ast=fo(x_ast)
p_ast
tol_outer_iter = 1e-6
tol=1e-8
tol_backtracking=1e-8
maxiter=30
mu=10
x = path_following_method_infeasible_init_point_2nd_version(fo, A, b,
                                                                             const,
                                                                             x_0, nu_0, tol,
                                                                             tol_backtracking = tol_backtracking, 
                                                                             x_ast = x_ast, 
                                                                             p_ast=p_ast, 
                                                                             maxiter=maxiter,
                                                                             mu=mu, tol_outer_iter = tol_outer_iter 
                                                                             )
x
A@x<jupyter_output><empty_output><jupyter_text>Comparación con [cvxpy](https://github.com/cvxgrp/cvxpy)<jupyter_code>import cvxpy as cp
x1 = cp.Variable()
x2 = cp.Variable()
x3 = cp.Variable()
x4 = cp.Variable()

# Create two constraints.
constraints = [2*x1+x2+x3+4*x4-7 == 0,x1+x2+2*x3+x4-6 == 0,x1>=0,x2>=0,x3>=0,x4>=0]

# Form objective.

obj = cp.Minimize(x1**2+x2**2+x3**2+x4**2-2*x1-3*x4)
# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve()  # Returns the optimal value.
print("status:", prob.status)
print("optimal value", prob.value)
print("optimal var", x1.value, x2.value, x3.value,x4.value)<jupyter_output>status: optimal
optimal value 1.4006849315068515
optimal var 1.1232876712328763 0.6506849315068494 1.8287671232876717 0.5684931506849316<jupyter_text>Segundo ejemplo $$\min 2x_1 + 5x_2$$ $$\text{sujeto a: }$$ $$\begin{array}{c}6-x_1-x_2 \leq 0 \\-18 + x_1 +2x_2 \leq 0\\x_1, x_2 \geq 0\end{array}$$<jupyter_code>fo = lambda x: 2*x[0] + 5*x[1]
const = {0: lambda x: 6-x[0]-x[1],
         1: lambda x: -18+x[0]+2*x[1],
         2: lambda x: -x[0],
         3: lambda x: -x[1]
        }
A=np.array([0,0],dtype=float)
b = 0
x_ast = np.array([6,0], dtype=float)
#x_0 = np.array([-2,-2], dtype=float)
x_0 = np.array([5,0], dtype=float)
#x_0 = np.array([10,-1], dtype=float)
#x_0 = np.array([6.5,0], dtype=float)
p_ast=fo(x_ast)
p_ast
nu_0 = np.array([0,0], dtype=float)
tol_outer_iter = 1e-3
tol=1e-8
tol_backtracking=1e-4
maxiter=30
mu=10
fo(x_0)
x = path_following_method_infeasible_init_point_2nd_version(fo, A, b,
                                                                 const,
                                                               x_0, nu_0, tol,
                                                               tol_backtracking, x_ast, p_ast, maxiter,
                                                               mu, tol_outer_iter = tol_outer_iter 
                                                               )
from utils import constraint_inequalities_funcs_eval
constraint_inequalities_funcs_eval(x, const)<jupyter_output><empty_output><jupyter_text>Comparación con [cvxpy](https://github.com/cvxgrp/cvxpy)<jupyter_code>x1 = cp.Variable()
x2 = cp.Variable()

# Create two constraints.
constraints = [6-x1-x2 <= 0,-18+x1+2*x2<=0,x1>=0,x2>=0]

# Form objective.

obj = cp.Minimize(2*x1+5*x2)


# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve()  # Returns the optimal value.

print("status:", prob.status)
print("optimal value", prob.value)
print("optimal var", x1.value, x2.value)<jupyter_output>status: optimal
optimal value 12.0000000016275
optimal var 6.000000000175689 2.552244387851183e-10
{'fraction_non_alphanumeric': 0.13232030049513402, 'fraction_numerical': 0.07768482158101417, 'mean_word_length': 2.3171007927519818, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 26, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '1956', 'license': 'Apache-2.0', 'n_tokens_mistral': 2337, 'n_tokens_neox': 2131, 'n_words': 410, 'path': 'temas/IV.optimizacion_convexa_y_machine_learning/4.6.Metodo_de_BL_para_puntos_iniciales_no_factibles_Python_2nd_version.ipynb', 'repo_name': 'Danahirmt/analisis-numerico-computo-cientifico', 'size': '5853'}