Version: 0.5.0

# ResultΒΆ

After an algorithm has been executed a result object is returned. In the following, single- and multi-objective runs with and without constraints are shown and the corresponding Result object is explained:

[1]:

from pymoo.algorithms.soo.nonconvex.ga import GA
from pymoo.factory import get_problem
from pymoo.optimize import minimize

problem = get_problem("sphere")
algorithm = GA(pop_size=5)
res = minimize(problem,
algorithm,
('n_gen', 30),
seed=1)



After an algorithm has been executed, a result object is returned. In the following, single- and multi-objective runs with and without constraints are shown, and the corresponding Result object is explained:

In this single-objective optimization problem, there exists a single best solution that was found. The result directly contains the best-found values in the corresponding spaces.

• res.X: Design space values are

• res.F: Objective spaces values

• res.G: Constraint values

• res.CV: Aggregated constraint violation

• res.algorithm: Algorithm object which has been iterated over

• res.opt: The solutions as a Population object.

• res.pop: The final Population

• res.history: The history of the algorithm. (only if save_history has been enabled during the algorithm initialization)

• res.time: The time required to run the algorithm

[2]:

res.X

[2]:

array([0.43553009, 0.64623533, 0.41297004, 0.63310497, 0.5128972 ,
0.50359626, 0.48004933, 0.4811089 , 0.61176118, 0.59986192])

[3]:

res.F

[3]:

array([0.07422943])

[4]:

res.G

[5]:

res.CV

[5]:

array([0.])

[6]:

res.algorithm

[6]:

<pymoo.algorithms.soo.nonconvex.ga.GA at 0x7f6914692130>

[7]:

pop = res.pop


The values from the final population can be extracted by using the get method. The population object is used internally and store information for each individual. The get method allows returning vectors or matrices based on the provided properties.

[8]:

pop.get("X")

[8]:

array([[0.43553009, 0.64623533, 0.41297004, 0.63310497, 0.5128972 ,
0.50359626, 0.48004933, 0.4811089 , 0.61176118, 0.59986192],
[0.50920138, 0.64623533, 0.48592119, 0.6294815 , 0.51325573,
0.50359626, 0.48004933, 0.42286843, 0.59554591, 0.34019917],
[0.43553009, 0.64623533, 0.41297004, 0.63310497, 0.5128972 ,
0.50359626, 0.48004933, 0.4811089 , 0.61176118, 0.36503906],
[0.44074274, 0.64623533, 0.4241345 , 0.63050284, 0.51325573,
0.50359626, 0.48004933, 0.44778674, 0.59554591, 0.34109635],
[0.43553009, 0.64623533, 0.41297004, 0.63310497, 0.65786479,
0.50359626, 0.48004933, 0.4811089 , 0.61176118, 0.36503906]])

[9]:

pop.get("F")

[9]:

array([[0.07422943],
[0.07963439],
[0.08247148],
[0.08537505],
[0.10722643]])


In this run, the problem did not have any constraints, and res.G evaluated to None. Also, note that res.CV will always be set to 0, no matter if the problem has constraints or not.

Let us consider a problem that has, in fact, constraints:

[10]:

problem = get_problem("g01")
algorithm = GA(pop_size=5)
res = minimize(problem,
algorithm,
('n_gen', 5),
verbose=True,
seed=1)

==========================================================================================
n_gen |  n_eval |   cv (min)   |   cv (avg)   |     fopt     |   fopt_gap   |     favg
==========================================================================================
1 |       5 |  1.35027E+02 |  5.47570E+02 |            - |            - |            -
2 |      10 |  1.35027E+02 |  3.32603E+02 |            - |            - |            -
3 |      15 |  1.35023E+02 |  1.45457E+02 |            - |            - |            -
4 |      20 |  1.34783E+02 |  1.35253E+02 |            - |            - |            -
5 |      25 |  1.33212E+02 |  1.34262E+02 |            - |            - |            -

[11]:

res.X, res.F, res.G, res.CV

[11]:

(None, None, None, None)


Here, the algorithm was not able to find any feasible solution in 5 generations. Therefore, all values contained in the results are equals to None. If the least feasible solution should be returned when no feasible solution was found, the flag return_least_infeasible can be enabled:

[12]:

problem = get_problem("g01")
algorithm = GA(pop_size=5)
res = minimize(problem,
algorithm,
('n_gen', 5),
verbose=True,
return_least_infeasible=True,
seed=1)

==========================================================================================
n_gen |  n_eval |   cv (min)   |   cv (avg)   |     fopt     |   fopt_gap   |     favg
==========================================================================================
1 |       5 |  1.35027E+02 |  5.47570E+02 |            - |            - |            -
2 |      10 |  1.35027E+02 |  3.32603E+02 |            - |            - |            -
3 |      15 |  1.35023E+02 |  1.45457E+02 |            - |            - |            -
4 |      20 |  1.34783E+02 |  1.35253E+02 |            - |            - |            -
5 |      25 |  1.33212E+02 |  1.34262E+02 |            - |            - |            -

[13]:

res.X, res.F, res.G, res.CV

[13]:

(array([ 0.75106286,  0.98886109,  0.45981887,  0.45896477,  0.78927933,
0.10322601,  0.74930314,  0.9085955 ,  0.28661703, 28.77753386,
13.00285721,  1.93669579,  0.31406932]),
array([-43.39475788]),
array([35.26023898, 23.1359931 ,  7.83691291, 22.76903095,  5.0919685 ,
-1.74185514, 27.070325  , 12.04710206, -0.16711225]),
array([133.21157149]))


We have made this design decision, because an infeasible solution can often not be considered as a solution of the optimization problem. Therefore, having a solution equals to None indicates the fact no feasible solution has been found.

If the problem has multiple objectives, the result object has the same structure but res.X, res.F, res .G, res.CV is a set of non-dominated solutions instead of a single one.

[14]:

from pymoo.algorithms.moo.nsga2 import NSGA2

problem = get_problem("zdt2")
algorithm = NSGA2()
res = minimize(problem,
algorithm,
('n_gen', 10),
seed=1)

[15]:

res.F

[15]:

array([[4.02723032e-01, 2.92285190e+00],
[2.42936792e-03, 3.09049957e+00],
[6.42640810e-05, 3.80452248e+00],
[9.03843390e-02, 2.97779402e+00],
[7.51601609e-04, 3.12489259e+00],
[2.66638116e-05, 4.15122904e+00],
[5.32367109e-01, 2.91333135e+00],
[7.51074119e-05, 3.17766175e+00],
[9.96726161e-01, 2.73633323e+00],
[9.37668137e-01, 2.76323527e+00],
[2.78148785e-05, 3.90537318e+00]])