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 do not need 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 | 12 | 3.5513115138 | 3.9760489486 | 0.000000E+00
2 | 400 | 11 | 3.5513115138 | 3.9273884740 | 0.000000E+00
3 | 500 | 11 | 2.8951504930 | 3.7656060256 | 0.000000E+00
4 | 600 | 6 | 2.8951504930 | 3.2826949452 | 0.000000E+00
5 | 700 | 6 | 2.8951504930 | 3.1708123316 | 0.000000E+00
6 | 800 | 5 | 2.8951504930 | 3.2548673229 | 0.000000E+00
7 | 900 | 9 | 2.7285748978 | 2.9356138941 | 0.000000E+00
8 | 1000 | 5 | 2.4583365933 | 2.7465700598 | 0.000000E+00
9 | 1100 | 9 | 2.4419556130 | 2.5012801731 | 0.000000E+00
10 | 1200 | 8 | 2.2447405753 | 2.4139170936 | 0.000000E+00
[1]:
<pymoo.core.result.Result at 0x76270f4bb390>
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 | 7 | 3.1485569015 | 3.6299859113 | 0.000000E+00
2 | 100 | 8 | 3.1485569015 | 3.5345318848 | 0.000000E+00
3 | 200 | 8 | 3.1297820336 | 3.2821583628 | 0.000000E+00
4 | 300 | 11 | 2.9391437524 | 3.1783149160 | 0.000000E+00
5 | 400 | 6 | 2.7344934650 | 3.0635716922 | 0.000000E+00
6 | 500 | 13 | 2.3791000489 | 2.6773229576 | 0.000000E+00
7 | 600 | 11 | 2.2942052178 | 2.6151395433 | 0.000000E+00
8 | 700 | 12 | 2.0591994531 | 2.5336571674 | 0.000000E+00
9 | 800 | 15 | 1.9055932098 | 2.4678777539 | 0.000000E+00
10 | 900 | 11 | 1.7780927497 | 1.9921858118 | 0.000000E+00
[2]:
<pymoo.core.result.Result at 0x762703157750>