For those of you who are not familiar with Project Platypus , its a repository that supports a collectionof python libraries for multi-objective optimization, decision making and data analysis. All the libraries arewritten in a very intuitive way and it is just so slick.
In this post I will focusexclusively on the platypus library which supports a variety of multi-objective evolutionary algorithms (MOEAs), such asNSGA-II, NSGA-III, MOEA/D, IBEA, EpsMOEA, SPEA2, GDE3, OMOPSO and SMPSO. Along with a number of analysis tools and performance metrics (which we will be discussing in subsequent posts).
First you can install the entire frameworkby typing the following commands on your terminal:
git clone https://github.com/Project-Platypus/Platypus.git cd Platypus python setup.py developIf you have trouble with this first step, please feel free to report it. The library is still under development and it might be infested with some minor bugs.
I wantedto start this post with a classical allocation problem to illustrate how you would implement your own function and optimize it using platypus. This example, by the way, was inspired on Professor Loucks’ public system’s modeling class.
We have the following problem:
We want to allocatecoconuts to three different markets. The goal is to find the allocations that maximizes the total benefits. Wehave only 6 coconut trucks to distribute to the three markets.So, lets find the best allocations using the NSGAII algorithm supported by platypus:
from platypus.algorithms import NSGAII from platypus.core import Problem from platypus.types import Real class allocation(Problem): def __init__(self): super(allocation, self).__init__(3, 3, 1) self.types[:] = [Real(-10, 10), Real(-10, 10), Real(-10,10)] self.constraints[:] = "<=0" def evaluate(self, solution): x = solution.variables[0] y = solution.variables[1] z = solution.variables[2] solution.objectives[:] = [6*x/(1+x), 7*y/(1+1.5*y), 8*z/(1+0.5*z)] solution.constraints[:] = [x+y+z-6] algorithm = NSGAII(allocation()) algorithm.run(10000) for solution in algorithm.result: print(solution.objectives)Assuming that you already have Platypus installed. You should be able to import the classes specified in lines 1-3 in the code above. The first line, as you can tell, is were you import the MOEA of your choosing,make sure its supported by platypus. In the second line, we importthe Problem class that allows you to define your own function. You also need to import the type class to specify your decision variables’ type. The library supports Real, Binary, Integer, etc. I define the allocation class in line 5. In line 8, I specify the number of objectives=3, number of decision variables=3 and number of constraints =1. The bounds of my decision variables are defined in line 9. The function in line 12 evaluates and stores the solutions.
Note that for the solution.objectives, each of the objective functions are specified in a vector and separated by a comma. You can set constraints in line 17. If you have more than one constraint it will be in the same vector with a comma separation. In line 19, the function is to be optimized with the NSGAII problem, in line 20 you set the number of iterations for the algorithm, I went up to 100000, it really depends on the difficulty of your problem and your choice of algorithm. Finally, I print the objective values in line 23, but you can have the decision variables as well if you wish. As you can see, the setup of your problem can be extremely easy. The names of the classes and functions in the library are very instinctive andyou can focus entirely on your problem formulation.