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.moo.nsga2 import NSGA2
from pymoo.problems 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 | n_nds | igd | gd | hv
==========================================================================
1 | 300 | 3 | 3.3964081538 | 3.6259891178 | 0.000000E+00
2 | 400 | 6 | 3.1097757797 | 3.6194649000 | 0.000000E+00
3 | 500 | 7 | 2.5086266661 | 3.0300779266 | 0.000000E+00
4 | 600 | 8 | 2.4924464817 | 2.8454565056 | 0.000000E+00
5 | 700 | 7 | 2.1828638281 | 2.6355705446 | 0.000000E+00
6 | 800 | 10 | 1.9559331968 | 2.8513196787 | 0.000000E+00
7 | 900 | 5 | 1.6050907669 | 2.1003702004 | 0.000000E+00
8 | 1000 | 8 | 1.5079232696 | 2.1068711721 | 0.000000E+00
9 | 1100 | 5 | 1.3825471777 | 1.8258211682 | 0.000000E+00
10 | 1200 | 9 | 0.9505397695 | 1.3279334785 | 0.000000E+00
[1]:
<pymoo.core.result.Result at 0x7fb8cac8afd0>
By Population (pre-evaluated)¶
[2]:
import numpy as np
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.problems import get_problem
from pymoo.core.evaluator import Evaluator
from pymoo.core.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 | n_nds | igd | gd | hv
==========================================================================
1 | 0 | 9 | 3.3815805339 | 3.8812224224 | 0.000000E+00
2 | 100 | 5 | 3.3815805339 | 3.2191629500 | 0.000000E+00
3 | 200 | 4 | 2.1685470597 | 2.6570874278 | 0.000000E+00
4 | 300 | 5 | 1.9735924562 | 2.1336859663 | 0.000000E+00
5 | 400 | 6 | 1.5130978941 | 1.8369989439 | 0.000000E+00
6 | 500 | 3 | 1.5130978941 | 1.6175725573 | 0.000000E+00
7 | 600 | 7 | 1.4299626763 | 1.4248231870 | 0.000000E+00
8 | 700 | 6 | 1.1522367478 | 1.2460282461 | 0.000000E+00
9 | 800 | 8 | 1.1522367478 | 1.3090562172 | 0.000000E+00
10 | 900 | 6 | 0.9918438754 | 0.8925696618 | 0.000000E+00
[2]:
<pymoo.core.result.Result at 0x7fb8cadf6eb0>