# Biased Initialization¶

One way of customizing an algorithm is a biased initial population. This can be very helpful if expert knowledge already exists and known solutions should be improved. In the following two different ways of initialization are provided: a) just providing the design space of the variables and b) a Population object where the objectives and constraints are provided and are not needed to be calculated again.

NOTE: This works with all population-based algorithms in pymoo. Technically speaking all algorithms which inherit from GeneticAlgorithm. For local-search based algorithm the initial solution can be provided by setting x0 instead of sampling.

## By Array¶

[1]:

import numpy as np

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_problem
from pymoo.optimize import minimize

problem = get_problem("zdt2")

X = np.random.random((300, problem.n_var))

algorithm = NSGA2(pop_size=100, sampling=X)

minimize(problem,
algorithm,
('n_gen', 10),
seed=1,
verbose=True)

============================================================
n_gen |  n_eval |     igd      |      gd      |      hv
============================================================
1 |     300 |  3.481862151 |  3.891229516 |  0.00000E+00
2 |     400 |  3.480264729 |  3.657111762 |  0.00000E+00
3 |     500 |  3.108853324 |  3.668094382 |  0.00000E+00
4 |     600 |  3.108853324 |  3.137531238 |  0.00000E+00
5 |     700 |  2.943036924 |  2.936487925 |  0.00000E+00
6 |     800 |  2.768143770 |  2.607942204 |  0.00000E+00
7 |     900 |  2.768143770 |  2.565896955 |  0.00000E+00
8 |    1000 |  2.654621380 |  2.408145325 |  0.00000E+00
9 |    1100 |  2.326534287 |  2.256206502 |  0.00000E+00
10 |    1200 |  2.081444601 |  2.208504133 |  0.00000E+00

[1]:

<pymoo.model.result.Result at 0x1188f1a50>


## By Population (pre-evaluated)¶

[2]:

import numpy as np

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_problem
from pymoo.model.evaluator import Evaluator
from pymoo.model.population import Population
from pymoo.optimize import minimize

problem = get_problem("zdt2")

# create initial data and set to the population object
X = np.random.random((300, problem.n_var))
pop = Population().new("X", X)
Evaluator().eval(problem, pop)

algorithm = NSGA2(pop_size=100, sampling=pop)

minimize(problem,
algorithm,
('n_gen', 10),
seed=1,
verbose=True)

============================================================
n_gen |  n_eval |     igd      |      gd      |      hv
============================================================
1 |       0 |  3.545092213 |  3.827601216 |  0.00000E+00
2 |     100 |  3.545092213 |  3.825368934 |  0.00000E+00
3 |     200 |  3.366254282 |  3.522554883 |  0.00000E+00
4 |     300 |  2.966148158 |  3.111186346 |  0.00000E+00
5 |     400 |  2.663611694 |  3.096243735 |  0.00000E+00
6 |     500 |  2.663611694 |  2.924202265 |  0.00000E+00
7 |     600 |  2.372672075 |  2.692961803 |  0.00000E+00
8 |     700 |  2.372672075 |  2.395526868 |  0.00000E+00
9 |     800 |  2.146421941 |  2.232161323 |  0.00000E+00
10 |     900 |  2.016162053 |  2.191762693 |  0.00000E+00

[2]:

<pymoo.model.result.Result at 0x1184f19d0>