我正在尝试求解6个离散值的最佳组合,这些离散值取2到16之间的任何数字,这将使我返回该函数的最小函数值= 1 / x1 + 1 / x2 + 1 / x3 .. 。1 / xn
约束是函数值必须小于0.3
我已经关注了一个在线教程,该教程描述了如何针对此类问题实施Google Analytics(分析),但我得到了错误的结果。在没有约束的情况下,最佳值应该是该问题中的最大值(16),但我没有得到
import random
from operator import add
def individual(length, min, max):
'Create a member of the population.'
return [ random.randint(min,max) for x in xrange(length) ]
def population(count, length, min, max):
"""
Create a number of individuals (i.e. a population).
count: the number of individuals in the population
length: the number of values per individual
min: the minimum possible value in an individual's list of values
max: the maximum possible value in an individual's list of values
"""
##print 'population',[ individual(length, min, max) for x in xrange(count) ]
return [ individual(length, min, max) for x in xrange(count) ]
def fitness(individual, target):
"""
Determine the fitness of an individual. Higher is better.
individual: the individual to evaluate
target: the target number individuals are aiming for
"""
pressure = 1/sum(individual)
print individual
return abs(target-pressure)
def grade(pop, target):
'Find average fitness for a population.'
summed = reduce(add, (fitness(x, target) for x in pop))
'Average Fitness', summed / (len(pop) * 1.0)
return summed / (len(pop) * 1.0)
def evolve(pop, target, retain=0.4, random_select=0.05, mutate=0.01):
graded = [ (fitness(x, target), x) for x in pop]
print 'graded',graded
graded = [ x[1] for x in sorted(graded)]
print 'graded',graded
retain_length = int(len(graded)*retain)
print 'retain_length', retain_length
parents = graded[:retain_length]
print 'parents', parents
# randomly add other individuals to
# promote genetic diversity
for individual in graded[retain_length:]:
if random_select > random.random():
parents.append(individual)
# mutate some individuals
for individual in parents:
if mutate > random.random():
pos_to_mutate = random.randint(0, len(individual)-1)
# this mutation is not ideal, because it
# restricts the range of possible values,
# but the function is unaware of the min/max
# values used to create the individuals,
individual[pos_to_mutate] = random.randint(
min(individual), max(individual))
# crossover parents to create children
parents_length = len(parents)
desired_length = len(pop) - parents_length
children = []
while len(children) < desired_length:
male = random.randint(0, parents_length-1)
female = random.randint(0, parents_length-1)
if male != female:
male = parents[male]
female = parents[female]
half = len(male) / 2
child = male[:half] + female[half:]
children.append(child)
parents.extend(children)
return parents
target = 0.3
p_count = 6
i_length = 6
i_min = 2
i_max = 16
p = population(p_count, i_length, i_min, i_max)
fitness_history = [grade(p, target),]
for i in xrange(100):
p = evolve(p, target)
print p
fitness_history.append(grade(p, target))
for datum in fitness_history:
print datum
预期结果是2到16之间的值的组合,该函数返回函数的最小值,同时遵守该函数不能大于0.3的约束。
答案 0 :(得分:0)
对于遗传算法,执行试探法的顺序非常不寻常。通常,genetic algorithm
遵循以下步骤:
另一种稍微不同的方法称为evolution strategy
(ES),但它也执行不同的方法。我所知道的进化算法都没有最后使用交叉。在ES中,交叉用于计算总体的质心个体,并将其用作变异的基础。然后,质心的所有突变体形成下一代。在ES中,也仅使用新一代(逗号选择-要求您对当前父代进行过采样)或使用旧的和新一代(加选择)来形成下一代。 ES执行
在实现的算法(都不是)中,您似乎没有施加足够的选择压力来推动搜索向更好的区域发展。仅对种群进行排序并获取精英子集并不一定是遗传算法的想法。您必须从整个人口中选择父母,但会给更好的人带来一些偏见。通常,这是使用健身比例或锦标赛选择来完成的。
将随机的个人引入搜索也是不标准的。您确定需要为问题保留多样性吗?它会提供比没有结果更好的结果,或者可能给您带来更差的结果吗?一种简单的替代方法是检测收敛并重新启动整个算法,直到达到停止标准(超时,生成的个人数量等)为止。
交叉和突变都可以。但是,在单点交叉中,通常选择随机的交叉点。
另一观察结果:说明中的适应度函数与代码中实现的适应度函数不匹配。
1/(x1 + x2 + ... + xn)
不相等
1/x1 + 1/x2 + ... + 1/xn