import numpy as np import random from resourse import ICA, Country, Empire, Colony, decode,evaluate import matplotlib.pylab as plt import time start = time.time() ica = ICA() best1 = [10, 16, 17, 1, 4, 7, 13, 11, 12, 5, 8, 2, 14, 9, 15, 3, 6, 3, 4, 3, 1, 2, 2, 3, 5, 4, 1, 1, 2, 4, 4, 4, 2, 3] best2 = [1, 5, 12, 11, 14, 2, 10, 4, 6, 7, 17, 16, 3, 8, 13, 9, 15, 5, 4, 1, 4, 1, 1, 1, 3, 3, 5, 2, 4, 3, 5, 2, 4, 3] best3 = [7, 10, 17, 12, 4, 14, 1, 2, 11, 15, 13, 9, 16, 6, 5, 8, 3, 4, 4, 5, 3, 1, 3, 2, 3, 1, 3, 4, 4, 1, 4, 2, 2, 5] bestlst = [best1,best2, best3] country = ica.createCountries() # for num, blst in enumerate(bestlst): # ica.countries[num].ind = blst # ica.countries[num].cost= evaluate(blst) ica.createEmpires() empire = ica.empires # """ index = 1 plt.ion() while len(empire) != 1: empire = ica.empires ica.moveAction() ica.compare() ica.compete() ica.weedOut() ica.revolution() empiresCost = [e.getempirecost() for e in empire] print("第{}伦 -- {}".format(index, empiresCost)) print("帝国数: {} , 殖民地数: {}".format(len(ica.empires), len(ica.colonies))) minEmpiresCost = np.argmin(empiresCost) marsEmpire = empire[minEmpiresCost] # print("第{}伦 -- 战神 :{}".format(index, marsEmpire.ind)) index += 1 print() # plt.pause(0.05) # plt.show() print("cost: {} ,最终坐标: {} ".format(empire[0].cost, empire[0].getPosit())) print(decode(empire[0].getPosit())) # plt.show() # plt.ioff() # """ end = time.time() print("程序process_1的运行时间为:{}s".format(end - start))
时间: 2023-06-01 19:07:49 浏览: 205
import numpy as np import matplotlib.pyplot as plt import math
这段代码用于实现一种基于ICA算法的国家糖果分配问题的仿真。首先通过import导入需要使用的相关库(numpy、random、matplotlib等),然后从resourse中导入用于ICA算法的类和相关函数。接下来定义了三个糖果分配方案(best1、best2、best3)和一个包含这三个方案的列表(bestlst)。最后定义了一个ica对象,并使用该对象的createCountries方法创建了多个代表国家的对象。
阅读全文