- Notifications
You must be signed in to change notification settings - Fork732
Evolutionary algorithm toolbox and framework with high performance for Python
License
geatpy-dev/geatpy
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The Genetic and Evolutionary Algorithm Toolbox for Python with high performance.
- Website (including documentation):https://geatpy.github.io/
- Quickstart:https://github.com/geatpy-dev/geatpy/blob/master/docs/quickstart.html
- Demo :https://github.com/geatpy-dev/geatpy/tree/master/demo
- Pypi page :https://pypi.org/project/geatpy/
- Bug reports:https://github.com/geatpy-dev/geatpy/issues
Jazzbin, J. "Geatpy: the genetic and evolutionary algorithm toolbox with high performance in python."https://github.com/geatpy-dev/geatpy/ (accessed 2020) (2020).
The features of Geatpy:
Capability of solving single-objective, multi-objectives, many-objectives and combinatorial optimization problems fast.
A huge number of operators with high performance of evolutionary algorithms (selection, recombination, mutation, migration...).
Support numerous encodings for the chromosome of the population.
Many evolutionary algorithm templates, including GA, DE, ES for single/multi-objective(s) evolution.
Multiple population evolution.
Support polysomy evolution.
Parallelization and distribution of evaluations.
Testbeds containing most common benchmarks functions.
Support tracking analysis of the evolution iteration.
Many evaluation metrics of algorithms.
Add a new way to define the aim function of the problem.
Support calculating objectives and constraints for the variables of only one individual.
Add a optimize function to do the optimization more convenient.
Add new open-source plotting functions.
Remove the dependency on scipy.
A new and faster core.
1.Installing online:
pip install geatpy2.From source:
python setup.py installor
pip install <filename>.whlAttention: Geatpy requires numpy>=1.17.0 and matplotlib>=3.0.0, the installation program won't help you install them so that you have to install both of them by yourselves.
Geatpy must run underPython3.5, 3.6, 3.7, 3.8, 3.9, or 3.10 in Windows x32/x64, Linux x64 or MacOS x64.
There are different versions forWindows,Linux andMac, you can download them fromhttp://geatpy.com/
The version ofGeatpy on github is the latest version suitable forPython >= 3.5
You can alsoupdate Geatpy by executing the command:
pip install --upgrade geatpyIf something wrong happened, such as decoding error about 'utf8' of pip, run this command instead or execute it as an administrator:
pip install --upgrade --user geatpyHere is the UML figure of Geatpy2.
For solving a multi-objective optimization problem, you can useGeatpy mainly in two steps:
1.Write down the aim function and some relevant settings in a derivative class namedMyProblem, which is inherited fromProblem class:
"""MyProblem.py"""importnumpyasnpimportgeatpyaseaclassMyProblem(ea.Problem):# Inherited from Problem class.def__init__(self,M):# M is the number of objects.name='DTLZ1'# Problem's name.maxormins= [1]*M# All objects are need to be minimized.Dim=M+4# Set the dimension of decision variables.varTypes= [0]*Dim# Set the types of decision variables. 0 means continuous while 1 means discrete.lb= [0]*Dim# The lower bound of each decision variable.ub= [1]*Dim# The upper bound of each decision variable.lbin= [1]*Dim# Whether the lower boundary is included.ubin= [1]*Dim# Whether the upper boundary is included.# Call the superclass's constructor to complete the instantiationea.Problem.__init__(self,name,M,maxormins,Dim,varTypes,lb,ub,lbin,ubin)defaimFunc(self,pop):# Write the aim function here, pop is an object of Population class.Vars=pop.Phen# Get the decision variablesXM=Vars[:,(self.M-1):]g=np.array([100* (self.Dim-self.M+1+np.sum(((XM-0.5)**2-np.cos(20*np.pi* (XM-0.5))),1))]).Tones_metrix=np.ones((Vars.shape[0],1))pop.ObjV=0.5*np.fliplr(np.cumprod(np.hstack([ones_metrix,Vars[:,:self.M-1]]),1))*np.hstack([ones_metrix,1-Vars[:,range(self.M-2,-1,-1)]])*np.tile(1+g, (1,self.M))defcalReferObjV(self):# Calculate the theoretic global optimal solution here.uniformPoint,ans=ea.crtup(self.M,10000)# create 10000 uniform points.realBestObjV=uniformPoint/2returnrealBestObjV
2.InstantiateMyProblem class and a derivative class inherited fromAlgorithm class in a Python script file "main.py" then execute it.For example, trying to find the pareto front ofDTLZ1, do as the following:
"""main.py"""importgeatpyasea# Import geatpyfromMyProblemimportMyProblem# Import MyProblem classif__name__=='__main__':M=3# Set the number of objects.problem=MyProblem(M)# Instantiate MyProblem class# Instantiate a algorithm class.algorithm=ea.moea_NSGA3_templet(problem,ea.Population(Encoding='RI',NIND=100),# Set 100 individuals.MAXGEN=500,# Set the max iteration number.logTras=1)# Set the frequency of logging. If it is zero, it would not log.# Do the optimizationres=ea.optimize(algorithm,verbose=False,drawing=1,outputMsg=True,drawLog=True,saveFlag=True)
Run the "main.py" and the part of the result is:
Execution time: 0.3650233745574951 s
Evaluation number: 45500
The number of non-dominated solutions is: 91
gd: 0.00033
igd: 0.02084
hv: 0.84061
spacing: 0.00158
For solving another problem:Ackley-30D, which has only one object and 30 decision variables, what you need to do is almost the same as above.
1.Write the aim function in "MyProblem.py".
importnumpyasnpimportgeatpyaseaclassAckley(ea.Problem):# Inherited from Problem class.def__init__(self,D=30):name='Ackley'# Problem's name.M=1# Set the number of objects.maxormins= [1]*M# All objects are need to be minimized.Dim=D# Set the dimension of decision variables.varTypes= [0]*Dim# Set the types of decision variables. 0 means continuous while 1 means discrete.lb= [-32.768]*Dim# The lower bound of each decision variable.ub= [32.768]*Dim# The upper bound of each decision variable.lbin= [1]*Dim# Whether the lower boundary is included.ubin= [1]*Dim# Whether the upper boundary is included.# Call the superclass's constructor to complete the instantiationea.Problem.__init__(self,name,M,maxormins,Dim,varTypes,lb,ub,lbin,ubin)defaimFunc(self,pop):# Write the aim function here, pop is an object of Population class.x=pop.Phen# Get the decision variablesn=self.Dimf=np.array([-20*np.exp(-0.2*np.sqrt(1/n*np.sum(x**2,1)))-np.exp(1/n*np.sum(np.cos(2*np.pi*x),1))+np.e+20]).Treturnf,CVdefcalReferObjV(self):# Calculate the global optimal solution here.realBestObjV=np.array([[0]])returnrealBestObjV
2.Write "main.py" to execute the algorithm templet to solve the problem.
importgeatpyasea# import geatpyimportnumpyasnpfromMyProblemimportAckleyif__name__=='__main__':# Instantiate MyProblem class.problem=Ackley(30)# Instantiate a algorithm class.algorithm=ea.soea_DE_rand_1_bin_templet(problem,ea.Population(Encoding='RI',NIND=20),MAXGEN=1000,# Set the max times of iteration.logTras=1)# Set the frequency of logging. If it is zero, it would not log.algorithm.mutOper.F=0.5# Set the F of DEalgorithm.recOper.XOVR=0.2# Set the Cr of DE (Here it is marked as XOVR)# Do the optimizationres=ea.optimize(algorithm,verbose=False,drawing=1,outputMsg=True,drawLog=True,saveFlag=True,dirName='result')
Part of the result is:
Execution time: 0.256328821182251 s
Evaluation number: 20000
The best objective value is: 3.209895993450118e-08
To get more tutorials, please link tohttp://www.geatpy.com.
About
Evolutionary algorithm toolbox and framework with high performance for Python
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors8
Uh oh!
There was an error while loading.Please reload this page.
