def deviation_from_centroid(agent,coord,agents): # doesn't count the agent themselves in the centroid agents_in_tile=agents_in_cell(coord,agents) if len (agents_in_tile)==1:return 0 list_of_phenotypes=[other_agent.phenotype for other_agent in agents_in_tile if other_agent!=agent] centroid=centroidnp(list_of_phenotypes) return euclidean_distance(centroid,agent.phenotype)
def deviation_from_centroid(agent, coord, agents): # doesn't count the agent themselves in the centroid agents_in_tile = agents_in_cell(coord, agents) if len(agents_in_tile) == 1: return 0 list_of_phenotypes = [ other_agent.phenotype for other_agent in agents_in_tile if other_agent != agent ] centroid = centroidnp(list_of_phenotypes) return euclidean_distance(centroid, agent.phenotype)
def run_simulation(starting_pop=25): if GRAPHICS_ON: initialize_screen() initial_strategy=np.zeros((7,13)) initial_strategy[3][5]=100 initial_strategy[4][6]=70 initial_strategy[6][0]=20 initial_phenotype=(WMAX/4*3,WMAX/4*3,WMAX/4*3,WMAX/4*3,WMAX/4*3,WMAX/4*3,WMAX/4*3,WMAX/4*3,WMAX/4*3) print initial_strategy #generate agents for i in range(starting_pop): spawn_location=r.choice(COORDINATES) initial_direction=r.choice(DIRECTIONS) AGENTS.append(Agent(parent_genes=initial_strategy, initial_location=spawn_location, initial_energy=INITIAL_ENERGY, #p0007 phenotype=initial_phenotype, parent_direction=initial_direction)) #spawn initial food supply for coord in COORDINATES: #TODO: determine how to distribute the initial supply RESOURCES[coord]=FOOD_SIZE tick=1 print "total energy: ", tally_energy(AGENTS,RESOURCES,COORDINATES) print "total agents: ", len(AGENTS) while tick : #graphics if GRAPHICS_ON: pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit(); tick+=1 if tick%FOOD_FREQUENCY==0: #p0004 for i in range(FOOD_COUNT): #p0005 coord=r.choice(COORDINATES) RESOURCES[coord]+=FOOD_SIZE #p0006 if GRAPHICS_ON: draw_food(coord) for agent in AGENTS: if count_agents(agent.location,AGENTS)>2: candidates=agents_in_cell(agent.location,AGENTS) targets= [target for target in candidates if target != agent] partner=r.choice(targets) kinship_to_partner=determine_kinship_to_partner(agent,partner,WMAX,K) elif count_agents(agent.location,AGENTS)>1: candidates=agents_in_cell(agent.location,AGENTS) targets= [target for target in candidates if target != agent] partner=targets[0] else: partner=agent kinship_to_partner=0 #0 is high directional_resources= [bool(RESOURCES[agent.location])]+[ bool(RESOURCES[agent.forwards]),bool(RESOURCES[agent.right]),bool(RESOURCES[agent.left])] directional_agents= [count_agents(agent.location,AGENTS)]+[ count_agents(agent.forwards,AGENTS),count_agents(agent.right,AGENTS),count_agents(agent.left,AGENTS) ] kinship_with_locals=deviation_from_centroid(agent,agent.location,AGENTS) # p0001 , p0003 input=np.transpose(np.matrix([K, float(agent.energy)/ENERGY_MAX,float(ENERGY_MAX-agent.energy)/ENERGY_MAX , kinship_with_locals ,kinship_to_partner] +directional_resources+directional_agents)) #print input output=agent.genes*input #print output top_choice=max(output) indices = [i for i, x in enumerate(output) if x==top_choice] action=r.choice(indices) #print indices,action if action ==0: agent.rest() elif action ==1: agent.turn(right=True) elif action ==2: agent.turn(right=False) elif action==3: agent.feed() elif action==4: agent.move_forward() elif action==5: agent.attack(partner) elif action==6: agent.divide() else:print "error" if tick%50==0: if GRAPHICS_ON or LOG_TO_CONSOLE>1: agent_energy, available_energy=tally_energy(AGENTS,RESOURCES,COORDINATES) if GRAPHICS_ON: draw_world_stats((len(AGENTS),round(agent_energy,2),round(available_energy,2),tick/100.0)) #drawing print "total agent energy: ", agent_energy print "total agents: ", len(AGENTS) print "available energy: ", available_energy if GRAPHICS_ON or LOG_TO_CONSOLE>0: print tick if len(AGENTS)==0:tick=False