python进化算法工具箱_Geatpy2 - 适用于Python的高性能进化算法工具箱和框架

Geatpy2

The Genetic and Evolutionary Algorithm Toolbox for Python with high performance.

Introduction

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.

Improvement of Geatpy 2.5.1

Support setting random seed in Geatpy.

The kernel of Geatpy is more stable.

Support 'xovud' in 'xovpmx'.

A better EA framework with higher performance.

Add MOEA/D-DE algorithm template.

Installation

1.Installing online:

pip install geatpy

2.From source:

python setup.py install

or

pip install .whl

Attention: Geatpy requires numpy>=1.16.0, matplotlib>=3.0.0 and scipy>=1.0.0, the installation program won't help you install them so that you have to install both of them by yourselves.

Versions

Geatpy must run under Python3.5, 3.6, 3.7 or 3.8 in Windows x32/x64, Linux x64 or Mac OS x64.

There are different versions for Windows, Linux and Mac, you can download them from http://geatpy.com/

The version of Geatpy on github is the latest version suitable for Python >= 3.5

You can also update Geatpy by executing the command:

pip install --upgrade geatpy

If 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 geatpy

Quick start

Here is the UML figure of Geatpy2.

For solving a multi-objective optimization problem, you can use Geatpy mainly in two steps:

1.Write down the aim function and some relevant settings in a derivative class named MyProblem, which is inherited from Problem class:

"""MyProblem.py"""

import numpy as np

import geatpy as ea

class MyProblem(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 instantiation

ea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin)

def aimFunc(self, pop): # Write the aim function here, pop is an object of Population class.

Vars = pop.Phen # Get the decision variables

XM = 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))]).T

ones_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))

def calReferObjV(self): # Calculate the theoretic global optimal solution here.

uniformPoint, ans = ea.crtup(self.M, 10000) # create 10000 uniform points.

realBestObjV = uniformPoint / 2

return realBestObjV

2.Instantiate MyProblem class and a derivative class inherited from Algorithm class in a Python script file "main.py" then execute it. For example, trying to find the pareto front of DTLZ1, do as the following:

"""main.py"""

import geatpy as ea # Import geatpy

from MyProblem import MyProblem # Import MyProblem class

if __name__ == '__main__':

"""=========================Instantiate your problem=========================="""

M = 3 # Set the number of objects.

problem = MyProblem(M) # Instantiate MyProblem class

"""===============================Population set=============================="""

Encoding = 'RI' # Encoding type.

NIND = 100 # Set the number of individuals.

Field = ea.crtfld(Encoding, problem.varTypes, problem.ranges, problem.borders) # Create the field descriptor.

population = ea.Population(Encoding, Field, NIND) # Instantiate Population class(Just instantiate, not initialize the population yet.)

"""================================Algorithm set==============================="""

myAlgorithm = ea.moea_NSGA3_templet(problem, population) # Instantiate a algorithm class.

myAlgorithm.MAXGEN = 500 # Set the max times of iteration.

"""===============================Start evolution=============================="""

NDSet = myAlgorithm.run() # Run the algorithm templet.

"""=============================Analyze the result============================="""

PF = problem.getReferObjV() # Get the global pareto front.

GD = ea.indicator.GD(NDSet.ObjV, PF) # Calculate GD

IGD = ea.indicator.IGD(NDSet.ObjV, PF) # Calculate IGD

HV = ea.indicator.HV(NDSet.ObjV, PF) # Calculate HV

Space = ea.indicator.spacing(NDSet.ObjV) # Calculate Space

print('The number of non-dominated result: %s'%(NDSet.sizes))

print('GD: ',GD)

print('IGD: ',IGD)

print('HV: ', HV)

print('Space: ', Space)

Run the "main.py" and the result is:

The number of non-dominated result: 91

GD: 0.00019492736742063313

IGD: 0.02058320808720775

HV: 0.8413590788841248

Space: 0.00045742613969278813

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".

import numpy as np

import geatpy as ea

class Ackley(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 instantiation

ea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin)

def aimFunc(self, pop): # Write the aim function here, pop is an object of Population class.

x = pop.Phen # Get the decision variables

n = self.Dim

f = 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]).T

return f, CV

def calReferObjV(self): # Calculate the global optimal solution here.

realBestObjV = np.array([[0]])

return realBestObjV

2.Write "main.py" to execute the algorithm templet to solve the problem.

import geatpy as ea # import geatpy

import numpy as np

from MyProblem import Ackley

if __name__ == '__main__':

"""=========================Instantiate your problem=========================="""

problem = Ackley(30) # Instantiate MyProblem class.

"""===============================Population set=============================="""

Encoding = 'RI' # Encoding type.

NIND = 20 # Set the number of individuals.

Field = ea.crtfld(Encoding, problem.varTypes, problem.ranges, problem.borders) # Create the field descriptor.

population = ea.Population(Encoding, Field, NIND) # Instantiate Population class(Just instantiate, not initialize the population yet.)

"""================================Algorithm set==============================="""

myAlgorithm = ea.soea_DE_rand_1_bin_templet(problem, population) # Instantiate a algorithm class.

myAlgorithm.MAXGEN = 1000 # Set the max times of iteration.

myAlgorithm.mutOper.F = 0.5 # Set the F of DE

myAlgorithm.recOper.XOVR = 0.2 # Set the Cr of DE (Here it is marked as XOVR)

myAlgorithm.drawing = 1 # 1 means draw the figure of the result

"""===============================Start evolution=============================="""

[population, obj_trace, var_trace] = myAlgorithm.run() # Run the algorithm templet.

"""=============================Analyze the result============================="""

best_gen = np.argmin(obj_trace[:, 1]) # Get the best generation.

best_ObjV = np.min(obj_trace[:, 1])

print('The objective value of the best solution is: %s'%(best_ObjV))

print('Effective iteration times: %s'%(obj_trace.shape[0]))

print('The best generation is: %s'%(best_gen + 1))

print('The number of evolution is: %s'%(myAlgorithm.evalsNum))

The result is:

The objective value of the best solution is: 5.8686921988737595e-09

Effective iteration times: 1000

The best generation is: 1000

The number of evolution is: 20000

To get more tutorials, please link to http://www.geatpy.com.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值