Of the previous article I tried running platypus which can solve a little optimization problem So, I tried to move the DTLZ2 problem problem in the document. But I'm not sure because this document isn't written in detail.
It's a simple problem, and I'll try to understand how it is calculated.
minimize (x,-x)\qquad for\quad x\in[-10,10]
I've changed the problem in the plotypus example a bit. The meaning of this problem is that when $ x $ takes $ -10 $ to $ 10 $, the two functions
y=f(x)\qquad y=-f(x)
What is the answer to minimize? Is the problem.
First is ʻimport. These three look good.
from platypus import NSGAII, Problem, Real`
Next, create a function that is the basis of the multipurpose solution.
def schaffer(x):return [x[0], (x[0]*(-1))]
Set the number of explanatory variables and the number of objective variables with problem = Problem (1, 2)
. In other words, I think that the explanatory variable $ x $ is one and the objective variable is two.
next
With problem.types [:] = Real (-10, 10)
, set the type and range of numbers given to x. I think Real
is a declaration that it is probably a real number.
Declare the function to calculate with problem.function = schaffer
. Declare to solve the schaffer
function as a problem.
With ʻalgorithm = NSGAII (problem), create an instance that solves the problem of the function declared in
problem` in NSGAII.
ʻAlgorithm.run (10000)` is set to calculate repeatedly 10000 times, and the calculation is performed.
This is the basic, and if you change the settings, it seems to work somehow.
from platypus import NSGAII, Problem, Real
def schaffer(x):
return [x[0], (x[0]*(-1))]
problem = Problem(1, 2)
problem.types[:] = Real(-10, 10)
problem.function = schaffer
algorithm = NSGAII(problem)
algorithm.run(10000)
The answer comes out and it seems that it is in result
without permission, so take it out and draw a graph.
import matplotlib.pyplot as plt
plt.scatter([s.objectives[0] for s in algorithm.result],
[s.objectives[1] for s in algorithm.result])
plt.xlim([-10, 10])
plt.ylim([-10, 10])
plt.xlabel("$f_1(x)$")
plt.ylabel("$f_2(x)$")
plt.show()
The result.
Well, the answer is as expected.
Recommended Posts