# Synthetic Objective Functions and ZDT2

Contents

## Preamble¶

In :
# used to create block diagrams
%xdiag_output_format svg

import numpy as np                   # for multi-dimensional containers
import pandas as pd                  # for DataFrames
import plotly.graph_objects as go    # for data visualisation
import plotly.io as pio              # to set shahin plot layout

pio.templates['shahin'] = pio.to_templated(go.Figure().update_layout(legend=dict(orientation="h",y=1.1, x=.5, xanchor='center'),margin=dict(t=0,r=0,b=40,l=40))).layout.template
pio.templates.default = 'shahin'


## Introduction¶

In mathematics, optimisation is concerned with the selection of optimal solutions to objective functions. An objective function consists of input arguments referred to as problem variables (or genotype) which are computed by one or many mathematical functions to determine the objective value (or phenotype).

Real-world optimisation problems are divided into one (in the case of single-objective optimisation) or many (in the case of multi-objective optimisation) objective functions in order to be optimised by an optimisation algorithm. The difficulty of convergence can be reduced by the bounding of problem variables as this reduces the size of the search domain.

In order to determine an Evolutionary Algorithm's robustness when solving problems consisting of multiple objectives, its performance must be assessed on the optimisation of synthetic test functions which are created for the purpose of testing. These problems may also be used to systematically compare two or more Evolutionary Algorithms.

In :
%%blockdiag
{
orientation = portrait
"Problem Variables" -> "Test Function" -> "Objective Values"
"Test Function" [color = '#ffffcc']
}


Synthetic test functions are typically:

• Intentionally difficult, meaning they are designed to include optimisation difficulties which are present in real-world problems.
• Scalable, meaning they can be configured with a different number of problem variables and objectives.
• Computationally efficient, meaning they are faster to execute than a real-world problem. This is desirable when benchmarking an Evolutionary Algorithm.

In contrast, real-world problems which have been encapsulated within an objective function in order to be used by an optimiser are often computationally expensive and have long execution times. This is because synthetic test functions are often mathematical equations which aim to cause difficulty for an optimiser when searching for problem variables that produce optimal objective values, whereas real-world problems often involve computationally expensive simulations in order to arrive at the objective values.

Put simply, using a real-world problem to evaluate the performance of a newly proposed Evolutionary Algorithm only allows us to determine if an algorithm is good at solving that single problem. What we're interested in is analysing how Evolutionary Algorithms perform when encountering various difficulties that appear in multi-objective problems, and how they compare to each other.

## The ZDT2 test function¶

We will be using a synthetic test problem throughout this notebook called ZDT2. It is part of the ZDT test suite, consisting of six different two-objective synthetic test problems. This is quite an old test suite, easy to solve, and very easy to visualise.

Mathematically, the ZDT21 two-objective 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}{(D-1)}\\ h(f_1,g) &= 1 - (f1/g)^2 \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 bi-objective 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$.

In :
D = 30
x = np.random.rand(D)
print(x)

[0.69736055 0.3487122  0.62056133 0.55759986 0.3044511  0.97060512
0.23282167 0.0660443  0.65614064 0.24385309 0.5734633  0.69154841
0.06398355 0.22288744 0.80012176 0.58775877 0.78395163 0.65259331
0.54355702 0.04722441 0.11961518 0.16837589 0.2855977  0.61455196
0.03237611 0.00248936 0.23129054 0.62993026 0.78930318 0.30583076]


Now that we have a solution to evaluate, let's implement the ZDT2 synthetic test function using Equation 1.

In :
def ZDT2(x):
f1 = x  # objective 1
g = 1 + 9 * np.sum(x[1:D] / (D-1))
h = 1 - (f1 / g) ** 2
f2 = g * h  # objective 2

return [f1, f2]


Finally, let's invoke our implemented test function using our solution $x$ from earlier.

In :
objective_values = ZDT2(x)
print(objective_values)

[0.6973605494375432, 4.66787733965319]


Now we can see the two objective values that measure our solution $x$ according to the ZDT2 synthetic test function, which is a minimisation problem.

### Performance in Objective Space¶

We will be discussing desirable characteristics in multi-objective solutions, but for now, let's plot some randomly initialised solutions against an optimal set of solutions for ZDT2. This is a synthetic test function, and as such the authors have provided us with a way to calculate the optimal set.

$$f_2 = 1 - \sqrt{f_1} \tag{4}$$

Let's use this to generate 20 ideal sets of objective values.

In :
true_front = np.empty((0, 2))

for f1 in np.linspace(0, 1, num=20):
f2 = 1 - (f1 ** 2)
true_front = np.vstack([true_front, [f1, f2]])

true_front = pd.DataFrame(true_front, columns=['f1', 'f2'])  # convert to DataFrame
true_front

Out:
f1 f2
0 0.000000 1.000000
1 0.052632 0.997230
2 0.105263 0.988920
3 0.157895 0.975069
4 0.210526 0.955679
5 0.263158 0.930748
6 0.315789 0.900277
7 0.368421 0.864266
8 0.421053 0.822715
9 0.473684 0.775623
10 0.526316 0.722992
11 0.578947 0.664820
12 0.631579 0.601108
13 0.684211 0.531856
14 0.736842 0.457064
15 0.789474 0.376731
16 0.842105 0.290859
17 0.894737 0.199446
18 0.947368 0.102493
19 1.000000 0.000000

Now we can plot them to have an idea of the shape of the true front for ZDT2 in objective space.

In :
fig = go.Figure(layout=dict(xaxis=dict(title='f1'),yaxis=dict(title='f2')))

fig.show()


To wrap things up, let's generate 50 objective values using the ZDT2 objective function created above. We achieve this by passing in 50 randomly initialised sets of problem variables.

In :
objective_values = np.empty((0, 2))

for i in range(50):
x = np.random.rand(D)
y = ZDT2(x)
objective_values = np.vstack([objective_values, y])

# convert to DataFrame
objective_values = pd.DataFrame(objective_values, columns=['f1', 'f2'])

Out:
f1 f2
0 0.338768 5.452485
1 0.017055 5.267072
2 0.433374 5.615262
3 0.276780 5.917905
4 0.629197 5.168079

Now we will plot the objective values of our randomly initialised solutions on top of a plot of the true front, this will give us some idea of the difference in performance between the two sets.

In :
fig = go.Figure(layout=dict(xaxis=dict(title='f1'), yaxis=dict(title='f2')))

fig.show()


## Conclusion¶

In this section, we introduced the concept of synthetic test functions along with ZDT2, a popular and relatively easy example. We expressed the concept mathematically and then made a direct implementation using Python. We then generated a set of 50 solutions, calculated the objective values for each one, and plotted the objective space using a scatter plot.

There are many suites of synthetic test functions in the literature, some to read about are ZDT, DTLZ, CEC09, and WFG Toolkit.

1. E. Zitzler, K. Deb, and L. Thiele. Comparison of Multiobjective Evolutionary Algorithms: Empirical Results. Evolutionary Computation, 8(2):173-195, 2000

Support this work

You can access this notebook and more by getting the e-book on Practical Evolutionary Algorithms.