Practical Evolutionary Algorithms
A practical book on Evolutionary Algorithms that teaches you the concepts and how they’re implemented in practice.
Get the bookUsing a Framework and the ZDT Test Suite
Preamble¶
# used to create block diagrams
%reload_ext xdiag_magic
%xdiag_output_format svg
import numpy as np # for multidimensional containers
import pandas as pd # for DataFrames
import platypus as plat # multiobjective optimisation framework
Introduction¶
When preparing to implement multiobjective optimisation experiments, it's often more convenient to use a readymade framework/library instead of programming everything from scratch. There are many libraries and frameworks that have been implemented in many different programming languages, but as we're using Python we will be selecting from frameworks such as DEAP, PyGMO, and Platypus.
With our focus on multiobjective optimisation, our choice is an easy one. We will choose Platypus which has a focus on multiobjective problems and optimisation.
Platypus is a framework for evolutionary computing in Python with a focus on multiobjective evolutionary algorithms (MOEAs). It differs from existing optimization libraries, including PyGMO, Inspyred, DEAP, and Scipy, by providing optimization algorithms and analysis tools for multiobjective optimization.
As a first look into Platypus, let's repeat the process covered in the earlier section on "Synthetic Objective Functions and ZDT1", where we randomly initialise a solution and then evaluate it using ZDT1
.
%%blockdiag
{
orientation = portrait
"Problem Variables" > "Test Function" > "Objective Values"
"Test Function" [color = '#ffffcc']
}
The ZDT test function¶
Similar to the last time, we will be using a synthetic test problem throughout this notebook called ZDT1. It is part of the ZDT test suite, consisting of six different twoobjective synthetic test problems. This is quite an old test suite, easy to solve, and very easy to visualise.
Mathematically, the ZDT1^{1} twoobjective test function can be expressed as:
$$ \begin{aligned} f_1(x_1) &= x_1 \tag{1} \\ f_2(x) &= g \cdot h \\ g(x_2,\ldots,x_{\mathrm{D}}) &= 1 + 9 \cdot \sum_{d=2}^{\mathrm{D}} \frac{x_d}{(D1)}\\ h(f_1,g) &= 1  \sqrt{f1/g} \end{aligned} $$where $x$ is a solution to the problem, defined as a vector of $D$ decision variables.
$$ x= \langle x_{1},x_{2},\ldots,x_{\mathrm{D}} \rangle \tag{2} $$and all decision variables fall between $0$ and $1$.
$$ 0 \le x_d \le 1, d=1,\ldots,\mathrm{D} \tag{3} $$For this biobjective test function, $f_1$ is the first objective, and $f_2$ is the second objective. This particular objective function is, by design, scalable up to any number of problem variables but is restricted to two problem objectives.
Let's start implementing this in Python, beginning with the initialisation of a solution according to Equations 2 and 3. In this case, we will have 30 problem variables $\mathrm{D}=30$.
D = 30
x = np.random.rand(D)
print(x)
Now that we have a solution to evaluate, let's implement the ZDT1 synthetic test function using Equation 1.
def ZDT1(x):
f1 = x[0] # objective 1
g = 1 + 9 * np.sum(x[1:D] / (D1))
h = 1  np.sqrt(f1 / g)
f2 = g * h # objective 2
return [f1, f2]
Finally, let's invoke our implemented test function using our solution $x$ from earlier.
objective_values = ZDT1(x)
print(objective_values)
Now we can see the two objective values that measure our solution $x$ according to the ZDT1 synthetic test function, which is a minimisation problem.
Using a Framework¶
We've quickly repeated our earlier exercise, where we move from our mathematical description of ZDT1 to an implementation in Python. Now, let's use the Platypus' implementation of ZDT1, which would have saves us from having to implement it in Python ourselves.
We have already imported Platypus as plat
above, so to get an instance of ZDT1 all we need to do is use the object constructor.
problem = plat.ZDT1()
Just like that, our variable problem
references an instance of the ZDT1 test problem.
Now we need to create a solution in a structure that is defined by Platypus. This solution object is what Platypus expects when performing all of the operations that it provides.
solution = plat.Solution(problem)
By using the Solution()
constructor and passing in our earlier instantiated problem, the solution is initialised with the correct number of variables and objectives. We can check this ourselves.
print(f"This solution's variables are set to:\n{solution.variables}")
print(f"This solution has {len(solution.variables)} variables")
print(f"This solution's objectives are set to:\n{solution.objectives}")
print(f"This solution has {len(solution.objectives)} objectives")
Earlier in this section we randomly generated 30 problem variables and stored them in the variable x
. Let's assign this to our variables and check that it works.
solution.variables = x
print(f"This solution's variables are set to:\n{solution.variables}")
Now we can invoke the evaluate()
method which will use the assigned problem to evaluate the problem variables and calculate the objective values. We can print these out afterwards to see the results.
solution.evaluate()
print(solution.objectives)
These objectives values should be the same as the ones that were calculated by our own implementation of ZDT1, within some margin of error.
Conclusion¶
In this section, we introduced a framework for multiobjective optimisation and analysis. We used it to create an instance of the ZDT1 test problem, which we then used to initialise an empty solution. We then assigned randomly generated problem variables to this solution and evaluated it with the ZDT test function to determine the objective values.
Exercise
Using the framework introduced in this section, evaluate a number of randomly generated solutions for ZDT2, ZDT3, ZDT4, and ZDT6.

E. Zitzler, K. Deb, and L. Thiele. Comparison of Multiobjective Evolutionary Algorithms: Empirical Results. Evolutionary Computation, 8(2):173195, 2000 ↩
Support this work
You can access this notebook and more by getting the ebook on Practical Evolutionary Algorithms.