这是一个简单的进化算法。循环的每次迭代都随机地排列函数的初始条件,并用迄今为止找到的最佳解更新下一次迭代的初始条件。你知道吗
为了避免陷入局部最优循环,我希望算法拒绝等于任何3个先前(或n个先前)解的解。你知道吗
如何创建这样的列表?你知道吗
for j in range(0, its+1):
# Seed initial conditions with best condition thus far.
k2, candidate1, candidate2 = k1, best_cond1, best_cond2
# Choose random nodes to swap from current conditions.
rand_node1, rand_node2 = choice(best_cond1), choice(best_cond2)
# Swap the nodes to create new candidate lists.
candidate_list1.append(rand_node2).remove(rand_node1)
candidate_list2.append(rand_node1).remove(rand_node2)
# Calculates a solution given the new conditions.
k2 = cost(candidate_list1, candidate_list2)
if k2 < k1:
k1, best1, best2 = k2, candidate1, candidate2
你可以这样做:
为了做
j%3
的事情,我不得不将循环改为从1
开始。你知道吗相关问题 更多 >
编程相关推荐