def generate(histogram, ubound, lbound, size, u, lb, m): agent = creator.Agent( [random.uniform(ubound, lbound), random.uniform(u, lb)] for _ in range(size)) agent.best = agent agent.fitness = toolbox.evaluate(agent=agent, data=histogram, m=m) return agent
def main(): Swarm = toolbox.swarm(n = 5) #intializing the swarm with n agents. GEN = 2000 #the numer of max iterations. best = None #initializing the best as none. #print(Swarm) for agent in Swarm: #finding the best position for the initial swarm/population if not best or best.fitness > agent.fitness: best = creator.Agent(agent) best.fitness = agent.fitness #print(best) for g in range(GEN): #runing the GAO Gen times c = compute_c(g + 1, GEN) #computing c for agent in Swarm: #updating each agent using the best solution found toolbox.update(agent = agent, Population = Swarm, c = c, best = best ) for agent in Swarm: #updating the best solution after updating the whole swarm. if not best or best.fitness > agent.fitness: best = creator.Agent(agent) best.fitness = agent.fitness print(best,best.fitness)
def generate(): agent = creator.Agent([random.uniform(ubound, lbound), random.uniform(u, 0)] for _ in range(size)) agent.best = agent agent.fitness = toolbox.evaluate(agent = agent) return agent