进化算法-DEAP

官方文档: https://deap.readthedocs.io/en/master/index.html

DEAP常用的方法、类和模块

1. Creator

为进化算法创建所需的类,如创建适度值类和Individual类
deap.creator.create(name, base[, attribute[, ...]])

Parameters:
name – 创建的类名.
base – 所要继承的原类.
attribute – 需要加入这个类的属性, 可选. 如果是类,则为实例属性,否则为类属性

注:在将遗传算法包装为自定义类时,由于creator这个类是共享的,可将其设置为类属性。

下面两段代码等价:

# 例1
Create("Foo", list, bar=dict, spam=1)
# 例2
class Foo(list):
    spam = 1

    def __init__(self):
        self.bar = dict()
# 实际使用例子,base.Fitness的weights参数:负数表示最小化,正数表示最大化
creator.create('Fitness', base.Fitness, weights=(-1.0, -1.0))
creator.create('Individual', set, fitness=creator.Fitness)

由上例可知,base.Fitness为实例属性,这样每个种群个体的适度值可以不同。

2. Base

为算法提供基础的结构,包含ToolboxFitness

Toolbox

储存算法运算符(包括个体和种群的创建)
注:这里的个体Individual为种群个体,如染色体。
class deap.base.Toolbox
可以使用register增加运算方法
register(alias, method[, argument[, ...]])

Parameters:
alias – 增加的运算符的别名,如果已存在则覆盖.
function – 要增加的函数.
argument – 传递给函数的参数, 可选。

>>> def func(a, b, c=3):
...     print a, b, c
...
>>> tools = Toolbox()
>>> tools.register("myFunc", func, 2, c=4)
>>> tools.myFunc(3)
2 3 4
# 实际使用例子,生产
toolbox = base.Toolbox()
# 随机生成变量,由有效变量池中的数据生成
toolbox.register('attr_item', random.choice, batches)
# 生成个体
toolbox.register('individual', tools.initRepeat, creator.Individual,
                toolbox.attr_item, IND_INIT_SIZE)
# 生成种群
toolbox.register('population', tools.initRepeat, list, toolbox.individual)

# 注册适度值函数,交叉,编译,选择函数
toolbox.register('evaluate', evalWIPloding)
toolbox.register('mate', cxSet)
toolbox.register('mutate', mutSet)
toolbox.register('select', tools.selNSGA2)

unregister(alias)
删除别名
decorate(alias, decorator[, decorator[, ...]])
给注册的函数增加装饰器

Fitness

评价算法结果优劣的测量值,有两个参数weight(必须)和values(可选),通过对这两个参数的乘法来计算最大值于最小值。这个两个参数必须为tuple类型。
dominates(other, obj=slice(None, None, None))
如果两个结果的差异很大就为True,否则为False

Algorithms

代码执行的具体算法
所有的算法都要求有如下的方法
toolbox.mate(), toolbox.mutate(), toolbox.select() and toolbox.evaluate()
deap.algorithms.eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen[, stats, halloffame, verbose])

| Parameters: |

  • population – A list of individuals.
  • toolbox – A Toolbox that contains the evolution operators.
  • mu – The number of individuals to select for the next generation.
  • lambda_ – The number of children to produce at each generation.
  • cxpb – The probability that an offspring is produced by crossover.
  • mutpb – The probability that an offspring is produced by mutation.
  • ngen – The number of generation.
  • stats – A Statistics object that is updated inplace, optional.
  • halloffame – A HallOfFame object that will contain the best individuals, optional.
  • verbose – Whether or not to log the statistics.
    | Returns: |
    The final population
    A class:deap.tools.Logbook with the statistics of the evolution.

Evolutionary Tools

deap.tools.initRepeat(container, func, n)
重复调用func n次的结果到container中(如list),用于初始化Individual

toolbox = base.Toolbox()
toolbox.register('attr_item', random.choice, batches)
toolbox.register('individual', tools.initRepeat, creator.Individual,
                toolbox.attr_item, IND_INIT_SIZE)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)

deap.tools.selNSGA2(individuals, k, nd='standard')
挑选Individual用于下一代
indivuduals的大小要大于k
为多个目标值(即Fitness的目标值),且FITNESS变化差异不大的快速选择法

Parameters:
individuals – A list of individuals to select from.
k – The number of individuals to select.
nd – Specify the non-dominated algorithm to use: ‘standard’ or ‘log’.
Returns:
A list of selected individuals.

你可能感兴趣的:(进化算法-DEAP)