python进化算法工具箱_Python遗传算法框架DEAP-Operators and Algorithms

Before starting with complex algorithms, we will see some basics of DEAP. First, we will start by creating simple individuals (as seen in the Creating Types tutorial) and make them interact with each other using different operators. Afterwards, we will learn how to use the algorithms and other tools.

在开始复杂的算法之前,我们将会看到一些基础的东西。首先,我们开始创建简单的个体(之前的教程中介绍了)和让它们利用操作算子互相作用。然后,我们将学习怎么使用算法和其他工具。

A First Individual(第一个个体)

First import the required modules and register the different functions required to create individuals that are lists of floats with a minimizing two objectives fitness.

一开始导入需要模块和注册不同的函数去创建个体(浮点数列表),求解的是一个最小化二目标适应度问题。

import random

from deap import base

from deap import creator

from deap import tools

IND_SIZE = 5

creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0))

creator.create("Individual", list, fitness=creator.FitnessMin)

toolbox = base.Toolbox()

toolbox.register("attr_float", random.random)

toolbox.register("individual", tools.initRepeat, creator.Individual,

toolbox.attr_float, n=IND_SIZE)

The first individual can now be built by adding the appropriate line to the script.

第一个个体现在能用合适一行代码去创建添加了。

ind1 = toolbox.individual()

Printing the individual ind1 and checking if its fitness is valid will give something like this

打印出个体ind1,检查它的适应度是否有效:

print ind1 # [0.86..., 0.27..., 0.70..., 0.03..., 0.87...]

print ind1.fitness.valid # False

The individual is printed as its base class representation (here a list) and the fitness is invalid because it contains no values.

这个个体打印出来了。这个适应度是无效的,因为没有值。

Evaluation(重头戏来了,评价函数/适应度函数)

The evaluation is the most personal part of an evolutionary algorithm, it is the only part of the library that you must write yourself. A typical evaluation function takes one individual as argument and returns its fitness as a tuple. As shown in the in the core section, a fitness is a list of floating point values and has a property valid to know if this individual shall be re-evaluated. The fitness is set by setting the values to the associated tuple. For example, the following evaluates the previously created individual ind1 and assigns its fitness to the corresponding values.

评价函数部分是评价算法中最“个人”的部分,它是库中你必须自己写的一部分。一个典型的评价函数需要一个个体作为参数,然后返回这个个体的fitne

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值