def update_fields(self): # Init of the field for key in self.fields.keys(): self.fields[key] = array([[0 for i in range(self.nb_cols)] for j in range(self.nb_lines)]) for agent in self.agents: for key in agent.fields.keys(): for (i, j) in agent.field_range(key, *self.size): self.fields[key][i, j] += agent.emmit( key, Agent.dist(agent.pos, (i, j)))
def add_customer(agent_id, time, belly_mean): start_pos = random.choice(range(5)) belly_size = max([np.random.normal(loc=belly_mean, scale=2) / 2, 0.001]) entrance_occupied, _ = check_if_pos_empty(belly_size, parkEntrances[start_pos], np.nan) if entrance_occupied: return agent_id first_target = random.choice(attractions) customers[agent_id] = Agent(index=agent_id, entrance=parkEntrances[start_pos], entrancesStr=parkEntrancesStr[start_pos], entranceTime=time, firstTarget=first_target, mapSize=mapSize, belly=belly_size) path = ParkMap.get_path_to_next_pos(customers[agent_id]) customers[agent_id].path = path customersInPark.append(agent_id) ParkMap.agentsLocation[agent_id] = customers[agent_id].pos return agent_id + 1
init_holding = 1 initialcash = 20000 theta = 75 gene_length = 64 risk_coef = 0.5 num_strategies = 80 max_stockprice = 200 min_stockprice = 0.01 min_excess = 0.005 eta = 0.005 specialist_iterations = 10 the_market = Stock(num_shares, init_price, dividend_startvalue, rho, noise_sd, d_bar, interest_rate) agents = [ Agent(init_holding, initialcash, num_strategies, theta, gene_length, risk_coef, num_shares, init_price, dividend_startvalue, rho, noise_sd, d_bar, interest_rate) for _ in range(100) ] the_specialist = Specialist(max_stockprice, min_stockprice, min_excess, eta, specialist_iterations, num_shares, init_price, dividend_startvalue, rho, noise_sd, d_bar, interest_rate) for t in range(1000): the_market.advance_arprocess() the_specialist.clear_market(agents) ma = the_market.calculate_ma(query='dividend', period=50)
def __init__(self, data_handler, load_key=None, sender=True, receiver=True, image_captioner=True, image_selector=False, track_results=True): self.sess = Agent.sess self.N = 1 # number of steps taken for each task - should be > 1 self.S = SenderAgent() self.R = ReceiverAgent(*self.S.get_output()) self.IC = ImageCaptioner() # self.IS = ImageSelector() self.S.all_agents_initialized(load_key) self.R.all_agents_initialized(load_key) self.train_metrics = {} self.val_metrics = {} self.experiment = Experiment(api_key='1jl4lQOnJsVdZR6oekS6WO5FI', project_name='Reptile', auto_param_logging=False, auto_metric_logging=False, disabled=(not track_results)) self.params = {} self.params.update(Agent.get_params()) self.params.update(data_handler.get_params()) self.experiment.log_parameters(self.params) self.T = {} if image_captioner: self.ic = ImageCaptioning(self.IC, experiment=self.experiment, track_results=False) self.T["Image Captioner"] = lambda img, capts: self.ic.train_batch( (img, capts), mode="train") if image_selector: self.is_ = ImageSelection(self.IS, experiment=self.experiment, track_results=False) self.T["Image Selector"] = lambda img, capts: self.is_.train_batch( (img, capts), mode="train") if sender or receiver: self.rg = ReferentialGame(self.S, self.R, experiment=self.experiment, track_results=False) if receiver: self.T["Receiver"] = lambda img, capts: self.rg.train_batch( img, mode="receiver_train") if sender: self.T["Sender"] = lambda img, capts: self.rg.train_batch( img, mode="sender_train") # Initialize TF variables_to_initialize = tf.global_variables() if load_key is not None: dont_initialize = [] if SenderAgent.loaded: dont_initialize += SenderAgent.get_all_weights() if ReceiverAgent.loaded: dont_initialize += ReceiverAgent.get_all_weights() if ImageCaptioner.loaded: dont_initialize += ImageCaptioner.get_all_weights() variables_to_initialize = [ v for v in tf.global_variables() if v not in dont_initialize ] # REMOVE LATER #variables_to_initialize += ImageCaptioner.optimizer.variables() Agent.sess.run(tf.variables_initializer(variables_to_initialize)) self.sender_shared_state = VariableState( self.sess, SenderAgent.get_shared_weights()) self.receiver_shared_state = VariableState( self.sess, ReceiverAgent.get_shared_weights()) self.sender_own_state = VariableState(self.sess, SenderAgent.get_weights()) self.receiver_own_state = VariableState(self.sess, ReceiverAgent.get_weights()) # print(SenderAgent.get_shared_weights()) # print(ReceiverAgent.get_shared_weights()) # print(SenderAgent.get_weights()) # print(ReceiverAgent.get_weights()) # print(tf.trainable_variables()) self.shared_states = { "shared_sender": self.sender_shared_state, "shared_receiver": self.receiver_shared_state } self.own_states = { "own_sender": self.sender_own_state, "own_receiver": self.receiver_own_state } shared_average = [] for k, v in self.shared_states.items(): shared_average.append(v.export_variables()) shared_average = np.mean(shared_average, axis=0) self.set_weights(new_shared_weights=shared_average) self.dh = data_handler with open( "{}/data/csv_loss_{}.csv".format(project_path, self.experiment.get_key()), 'w+') as csv_loss_file: csv_loss_file.write( "Image Captioner Loss,Image Selector Loss,Sender Loss,Receiver Loss\n" ) with open( "{}/data/csv_accuracy_{}.csv".format( project_path, self.experiment.get_key()), 'w+') as csv_acc_file: csv_acc_file.write( "Image Captioner Loss,Image Selector Loss,Sender Loss,Receiver Loss\n" ) self.step = 0
def main(): f = open("parameters2.txt", "r") flines = list(map(lambda x: x.replace('\n', '').split(), f.readlines())) n = int(flines[0][1]) deadLine = float(flines[1][1]) cDeadLine = deadLine graph = {} j = 2 totalNumOfPpl = 0 for i in range(2, 2 + n): ppl = int(flines[i][1][1:]) if len(flines[i]) > 1 else 0 graph[flines[i][0][1:]] = {"p": ppl, "e": []} j = j + 1 totalNumOfPpl += ppl for i in range(j + 1, len(flines)): v1 = "V" + flines[i][1] v2 = "V" + flines[i][2] w = int(flines[i][3][1:]) graph[v1]["e"].append({"v": v2, "w": w, "blocked": False}) graph[v2]["e"].append({"v": v1, "w": w, "blocked": False}) print("please enter number of agents:") num_of_agents = int(input()) print("please enter agents details: ") agents_details = input().split(',') agentDetails = list(map(lambda x: x.split(' '), agents_details)) agentsList = list() for i in range(0, num_of_agents): agentsList.append( Agent(agentDetails[i][0], agentDetails[i][1], len(graph), L)) # main loop print("Agents: \n", agentsList) print("Graph: \n", graph) while deadLine > 0 and totalNumOfPpl > 0 and not allTerminated(agentsList): for i in agentsList: if i.terminated: continue elif i.stepsLeft > 0: i.stepsLeft -= 1 elif i.calcTime > 0: i.calcTime -= 1 elif i.type == 's': saboAct(i, graph) else: # we reached vertice, evacuating ppl verPpl = graph[i.currentPosition]["p"] totalNumOfPpl -= verPpl i.peopleEvacuated += verPpl graph[i.currentPosition]["p"] = 0 if totalNumOfPpl == 0: break #finding next vertice to travel prevVer = i.currentPosition i.currentPosition = getNextStep(i, graph) print("Next step: ", i.currentPosition) if not i.currentPosition: i.terminated = True else: i.stepsLeft = getEdgeWeight(graph, prevVer, i.currentPosition) i.numOfActions += 1 deadLine -= 1 feedback = "Well Done!!! Agent: " if agentsList[0].peopleEvacuated == 0: feedback = "Too Bad.. You could've done better, " print(feedback, agentsList[0].type, "\nevacuated ", agentsList[0].peopleEvacuated, " people! And it took ", cDeadLine - deadLine, " rounds.")
if __name__ == '__main__': environment_name = 'CarRacing-v0' env = environment(environment_name, img_dim, num_stack, num_actions, render, lr) num_states = img_dim print(env.env.action_space.shape) action_dim = env.env.action_space.shape[0] assert action_list.shape[ 1] == action_dim, "length of Env action space does not match action buffer" num_actions = action_list.shape[0] # Setting random seeds with respect to python inbuilt random and numpy random random.seed(901) np.random.seed(1) agent = Agent(num_states, num_actions, img_dim, model_path) randomAgent = RandomAgent(num_actions) print(test_mode, train_mode) try: #Train agent if test_mode: if train_mode: print("Initialization with random agent. Fill memory") while randomAgent.exp < memory_size: env.run(randomAgent) print(randomAgent.exp, "/", memory_size) agent.memory = randomAgent.memory randomAgent = None
# the action space action_size = brain.vector_action_space_size print('Number of actions:', action_size) # the state space state = env_info.vector_observations[0] state_size = len(state) print('States have length:', state_size) ####################################### ########### Agent Setting ########### agent = Agent(state_size, action_size, seed=0, hidden_layers=hidden_layers, drop_p=drop_p, method=method, Dueling=Dueling) print('-------- Model structure --------') print('method :', method) print(agent.qnetwork_local) print('---------------------------------') ####################################### scores = [] # list containing scores from each episode scores_window = deque(maxlen=100) # last 100 scores eps = eps_start # initialize epsilon isFirst = True
def wallet_to_agents(self, wallets): return [Agent(wallet) for wallet in wallets]
def main(epochs=100, task="reptile", D=127, K=10000, L=15, loss_type='pairwise'): """ Run epochs of games :return: """ load_key = "8cf8c8b35484461fafb92252af469313" track_results = True Agent.set_params(K=K, D=D, L=L, loss_type=loss_type, train=True) dh = Data_Handler(batch_size=Agent.batch_size, same_category=False) with tf.variable_scope("all", reuse=tf.AUTO_REUSE): # Set up Agents and Tasks if task.lower() in [ "rg", "referential game", "referential_game", "referentialgame" ]: s = SenderAgent() r = ReceiverAgent(*s.get_output()) s.all_agents_initialized(load_key) r.all_agents_initialized(load_key) t = ReferentialGame(s, r, data_handler=dh, track_results=track_results) dh.set_params(distractors=D) elif task.lower() in [ "ic", "image captioning", "image_captioning", "imagecaptioning" ]: ic = ImageCaptioner() ic.all_agents_initialized(load_key) t = ImageCaptioning(ic, data_handler=dh, track_results=track_results) elif task.lower() in [ "is", "image selection", "image_selection", "imageselection" ]: is_ = ImageSelector() is_.all_agents_initialized(load_key) t = ImageSelection(is_, data_handler=dh, track_results=track_results) dh.set_params(distractors=D) elif task.lower() in ["r", "reptile"]: t = Reptile(data_handler=dh, track_results=track_results, load_key=load_key) else: raise ValueError( "Unknown task {}, select from ['referential_game', 'image_captioning']" .format(task)) # Initialize TF variables_to_initialize = tf.global_variables() if load_key is not None: dont_initialize = [] if SenderAgent.loaded: dont_initialize += SenderAgent.get_all_weights() if ReceiverAgent.loaded: dont_initialize += ReceiverAgent.get_all_weights() variables_to_initialize = [ v for v in tf.global_variables() if v not in dont_initialize ] Agent.sess.run(tf.variables_initializer(variables_to_initialize)) exp_key = t.get_experiment_key() losses = [] best_accuracy = 0.0 # Starting point if not isinstance(t, Reptile): print("Validating epoch 0:") accuracy, loss = t.train_epoch(0, mode="val") print("\rloss: {0:1.4f}, accuracy: {1:5.2f}%".format( loss, accuracy * 100), end="\n") # Start training for e in range(1, epochs + 1): print("Training epoch {0}".format(e)) train_accuracy, train_loss = t.train_epoch(e, mode="train") print("\rloss: {0:1.4f}, accuracy: {1:5.2f}%".format( train_loss, train_accuracy * 100), end="\n") if not isinstance(t, Reptile): print("Validating epoch {0}".format(e)) val_accuracy, val_loss = t.train_epoch(e, mode="val") print("\rloss: {0:1.4f}, accuracy: {1:5.2f}%".format( val_loss, val_accuracy * 100), end="\n") losses.append(val_loss) # End training if 100% communication rate or convergence reached on loss if val_accuracy == 1.0 or converged(losses): break if val_accuracy > best_accuracy: print("Saving Model") best_accuracy = val_accuracy save_models(exp_key) else: print("Saving Model") save_models(exp_key) Agent.sess.close()