# Analytical problem
Defining a problem with an explicit mathematical representation is straightforwars.

As an example, consider the following multiobjective optimization problem

\begin{equation}
\begin{aligned}
& \underset{\mathbf x}{\text{min}}
& & x_1^2 - x_2; x_2^2 - 3x_1 \\
& \text{s.t.} & &  x_1 + x_2 \leq 10 \\
& & &  \mathbf{x} \; \in S, \\
\end{aligned}
\end{equation}

where the feasible region is

\begin{equation}
x_i \in \left[-5, 5\right] \; \forall i \;\in \left[1,2\right].
\end{equation}

Begin by importing the necessary classes:


```python
from desdeov2.problem.Problem import ScalarMOProblem
from desdeov2.problem.Objective import ScalarObjective
from desdeov2.problem.Variable import Variable
from desdeov2.problem.Constraint import ScalarConstraint
```

Define the variables:


```python
# Args: name, starting value, lower bound, upper bound
x1 = Variable("x_1", 0, -0.5, 0.5)
x2 = Variable("x_2", 0, -0.5, 0.5)
```

Define the objectives, notice the argument of the callable objective function, it is assumed to be array-like.


```python
# Args: name, callable
obj1 = ScalarObjective("f_1", lambda x: x[0]**2 - x[1])
obj2 = ScalarObjective("f_2", lambda x: x[1]**2 - 3*x[0])
```

Define the constraints. Constraint may depend on objective function as well (second argument to the lambda, notice the underscore). In that case, the objectives should not be defined inline, like above, but as their own function definitions. The constraint should be defined so, that when evaluated, it should return a positive value, if the constraint is adhered to, and a negative, if the constraint is breached.


```python
# Args: name, n of variables, n of objectives, callable
cons1 = ScalarConstraint("c_1", 2, 2, lambda x, _: 10 - (x[0] + x[1]))
```

Finally, put it all together and create the problem.


```python
# Args: list of objevtives, variables and constraints
problem = ScalarMOProblem([obj1, obj2]
                         ,[x1, x2]
                         ,[cons1])
```

Now, the problem is fully specified and can be evaluated and played around with.


```python
import numpy as np

print("N of objectives:", problem.n_of_objectives)
print("N of variables:", problem.n_of_variables)
print("N of constraints:", problem.n_of_constraints)

res1, eval_cons1 = problem.evaluate(np.array([2, 4]))
res2, eval_cons2 = problem.evaluate(np.array([6, 6]))
res3, eval_cons3 = problem.evaluate(np.array([[6, 3], [4,3], [7,4]]))

print("Single feasible decision variables:", res1, "with constraint values", eval_cons1)
print("Single non-feasible decision variables:", res2, "with constraint values", eval_cons2)
print("Multiple decision variables:", res3, "with constraint values", eval_cons3)
```

62f4b411b18df3ff4d60773c795725dc6ec5e25e
ipynb
Jupyter Notebook
notebooks/analytical_problem.ipynb
gialmisi/DESDEOv2
0eeb4687d2e539845ab86a5018ff99b92e4ca5cf
MIT
2019-08-08T05:11:21.000Z
2019-08-08T05:11:21.000Z
notebooks/analytical_problem.ipynb
gialmisi/DESDEOv2
0eeb4687d2e539845ab86a5018ff99b92e4ca5cf
MIT
2019-08-25T08:49:33.000Z
2019-09-06T08:06:46.000Z
notebooks/analytical_problem.ipynb
gialmisi/DESDEOv2
0eeb4687d2e539845ab86a5018ff99b92e4ca5cf
MIT
2019-11-07T14:42:29.000Z
2019-11-07T14:42:29.000Z
Qwen/Qwen-72B
1. YES
2. YES