<jupyter_start><jupyter_text>Example 3=========The next example demonstrates the multi-parameter capabilities of ``cgn``. We are going to solve a nonlinear least-squares problem that depends on two parameters $x$ and $y$, where $x$ has to satisfy a nonlinear equality constraint, while $y$ is only nonnegativity constrained.$\begin{align}\min_{x \in \mathbb{R}^2, y \in \mathbb{R}} \quad & ||F(x, y)||_2^2 + \beta ||R(x - m)||_2^2\\\text{s. t.} \quad  & x_1 + x_1^3 + x_2 + x_2^2 = 0, \quad y > 0, \\\text{where } \quad & F(x) = \left(\begin{matrix}x_1 + e^{-x_2} + \sqrt{y} \\x_1^2 + 2 x_2 + 1 - \sqrt{y}\end{matrix} \right), \\& R = \left( \begin{matrix}1 & 2 \\3 & 4\end{matrix} \right), \quad m = \left(\begin{matrix}1 \\ 1 \\\end{matrix} \right), \quad \beta = 0.1.\end{align}$Let us start by implementing the required functions and their derivatives:<jupyter_code>from math import exp, sqrt
import numpy as np
import cgn

def F(x, y):
    out = np.array([x[0] + exp(-x[1] + sqrt(y[0])),
                    x[0] ** 2 + 2 * x[1] + 1 - sqrt(y[0])])
    return out

def DF(x, y):
    jac = np.array([[1., -exp(-x[1]), 0.5 / sqrt(y[0])],
                    [2 * x[0], 2., - 0.5 / sqrt(y[0])]])
    return jac<jupyter_output><empty_output><jupyter_text>Next, we set up the inequality constraint, which only depends on $x$:<jupyter_code>def g(x):
    out = x[0] + x[0] ** 3 + x[1] + x[1] ** 2
    return np.array([out])

def Dg(x):
    jac = np.array([1 + 3 * x[0] ** 2, 1. + 2 * x[1]]).reshape((1, 2))
    return jac<jupyter_output><empty_output><jupyter_text>Next, we set up our ``cgn.Parameter`` objects. For the initial guess, let's just try $x = [0, 0]$ and $y = 1$:<jupyter_code>x = cgn.Parameter(name="x", start=np.zeros(2))
y = cgn.Parameter(name="y", start=np.ones(1))<jupyter_output><empty_output><jupyter_text>For ``x``, we have to specify the regularization term $\beta ||R(x-m)||_2^2$:<jupyter_code>x.regop = np.array([[1., 2.], [3., 4.]])
x.mean = np.array([1., 1.])
x.beta = 0.1<jupyter_output><empty_output><jupyter_text>The second parameter ``y`` is not regularized, but it has to satisfy the lower-bound constraint $y > 0$. We implement this strict inequality constraint as an equality constraint$y \geq \epsilon$ for a small, positive $\epsilon > 0$.<jupyter_code>eps = 1e-10
y.lb = np.array([eps])<jupyter_output><empty_output><jupyter_text>With this setup, we can finally create our optimization problem and solve it with ``cgn``.<jupyter_code>inequality_constraint = cgn.NonlinearConstraint(parameters=[x], fun=g, jac=Dg, ctype="ineq")

problem = cgn.Problem(parameters=[x, y], fun=F, jac=DF, constraints=[inequality_constraint])
solver = cgn.CGN()

solution = solver.solve(problem=problem)<jupyter_output><empty_output><jupyter_text>Seems to work without problems. Let us look at the minimizers:<jupyter_code>x_min = solution.minimizer("x")
y_min = solution.minimizer("y")
print(f"x_min = {x_min}")
print(f"y_min = {y_min}")<jupyter_output>x_min = [-0.00522749  0.83669584]
y_min = [1.02461374]
{'fraction_non_alphanumeric': 0.18754141815772035, 'fraction_numerical': 0.036779324055666, 'mean_word_length': 4.324514991181658, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '512872', 'license': 'MIT', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1161, 'n_words': 304, 'path': 'examples/example3.ipynb', 'repo_name': 'FabianKP/cgn', 'size': '3018'}