def single_experiment(x, y, mode): N = 1 inputs = [] start_time = time.time() model = Model(N, 7, 7, x, y, mode) i = 0 while True: print("step : " + str(i)) show_grid(model, 2) model.step() running_time = time.time() - start_time i += 1 agent_perc = model.datacollector.get_agent_vars_dataframe() if (running_time >= 20 or agent_perc['Position'][-1] == (3, 6)): break running_time = time.time() - start_time print("runningtime : " + str(running_time)) #inputs.append(IF.Inputs_Function(agent_perc)) #print(agent_perc[['View','Position', 'Behaviour', 'Light Perception','Sound Perception']][0:100]) figure = plt.figure(3) _ = plt.title('Sound Perception during steps') steps = agent_perc['Iterations'].tolist() _ = plt.axis([steps[0], steps[-1], 0.4, 1.]) _ = plt.plot(steps, agent_perc['Sound Perception'].tolist()) figure = plt.figure(4) _ = plt.title('Light Perception during steps') _ = plt.axis([steps[0], steps[-1], 0., 60.]) _ = plt.plot(steps, agent_perc['Light Perception'].tolist()) agent_perc.head() plt.show()
def __init__(self, model, pos, direction, speed=1, active=True, trace=None): """ Initializes an Agent :param model: The model that the agent lives in. :param pos: The initial position in (x, y) :param direction: The initial agent direction as a Direction-object :param speed: The initial speed. :param active: Whether or not the agent is not eliminated. :param trace: A list of positions that the agent occupied during the last step (just for pre-initialization) """ if model is None: # use an empty model if an agent is used to play against an online game model = Model() super().__init__(None, model) else: super().__init__(model.next_id(), model) self.pos = pos self.direction = direction self.speed = speed self.active = active self.deadline = None # Holds all cells that were visited in the last step if trace is None: self.trace = [] else: self.trace = trace self.action = None self.elimination_step = -1 # Saves the step that the agent was eliminated in (-1 if still active)
def __init__(self): Model.__init__(self) self.discussion_post = 0 self.discussion_social_post = 0 self.total_chat = 0 self.total_social_chat = 0 # counters for current period self.chat_count = 0 self.chat_social_count = 0 self.step_count = 0
for i, x in enumerate(espacio.grid): for j, y in enumerate(x): n_individuos_en_celda = len(y) cuenta[i][j] = n_individuos_en_celda total += n_individuos_en_celda saturacion = total / espacio.height*espacio.width ##Luego se muestra una representación plt.figure(figsize = figsize) plt.imshow(cuenta, vmin = 0, vmax = saturacion) plt.colorbar() plt.show() if __name__=='__main__': import networkx as nx modelo = Model() modelo.num_ind = 10 class Individuo(Agent): def __init__(self, unique_id, model, edad, sexo): super().__init__(unique_id, model) self.model = model self.edad = edad self.sexo = sexo ciudad = Ciudad(modelo, Individuo) ciudad.generarindividuos() #Se crean las casas distribuyendo los individuos ciudad.crear_hogares() #Se agrega una tienda a la ciudad y se conecta con todas las casas ciudad.crear_nodo('aurrera', tipo='tienda', tamano=10)
def __init__(self, x, y, agent_type): self.x = x self.y = y self.type = agent_type def step(self, model): neighbors = model.grid.get_neighbors(self.x, self.y, moore=True) similar = 0 for neighbor in neighbors: if neighbor.type == self.type: similar += 1 # If unhappy, move: if similar < model.homophily: new_x, new_y = random.choice(mode.get_empty()) grid[self.y][self.x] = None grid[new_y][new_x] = self else: model.happy += 1 if __name__ == "__main__": model = Model(10, 10, 0.8, 0.2, 3) while model.happy < model.model.schedule.get_agent_count(): model.go()
PMSM = random.uniform(.02,.05) MSMandHIV = 0.001917 NotMSMandHIV = 0.001509 MSMacquire = 26200/(321039839*PMSM*(1-MSMandHIV/PMSM)) NotMSMacquire = 8800/(321039839*(1-PMSM)) PHIV=[MSMandHIV/PMSM,NotMSMandHIV/(1-PMSM)] AcHIV=[MSMacquire/365,NotMSMacquire/365] if ExcludeMSM: Eligibility= .38/(1-PMSM) else: Eligibility= .38 + PMSM*(1-PHIV[0]) a=time.time() Model = MoneyModel(agentnum) print('Running Model') for i in tqdm(range(365)): Model.step() data = Model.datacollector.get_model_vars_dataframe() b=time.time() print(str((b-a)/60)+' Minutes Elapsed') Date= str(random.randint(1,9999999)) file_Name = Date+'HIVDetected'+'.pickle' fileObject = open(file_Name,'wb') pickle.dump([a.HIVDetected for a in Model.schedule.agents],fileObject, protocol=2) fileObject.close() file_Name = Date+'HIVDonations'+'.pickle' fileObject = open(file_Name,'wb') pickle.dump([a.HIVDonation for a in Model.schedule.agents],fileObject, protocol=2) fileObject.close() file_Name = Date+'HIV'+'.pickle'
def setUp(self): self.original_games_path = os.path.abspath( "../..") + "/res/originalGames/" self.test_games = os.listdir(self.original_games_path) self.model = Model() self.maxDiff = None