def test_linear_sarsa(iterations=1000, mlambda=None, n0=100, avg_it=100): print "\n-------------------" print "TD control Sarsa, with Linear function approximation" print "run for n. iterations: "+str(iterations) print "plot graph mse vs episodes for lambda equal 0 and lambda equal 1" print "list (std output) win percentage for values of lambda 0, 0.1, 0.2, ..., 0.9, 1" monte_carlo_Q = pickle.load(open("Data/Qval_func_1000000_MC_control.pkl", "rb")) n_elements = monte_carlo_Q.shape[0]*monte_carlo_Q.shape[1]*2 mse = [] if not isinstance(mlambda,list): # if no value is passed for lambda, default 0.5 l = 0.5 if mlambda==None else mlambda # learn game = Environment() agent = Agent(game, n0) agent.TD_control_linear(iterations,l,avg_it) agent.show_statevalue_function() else: # test each value of lambda for l in mlambda: game = Environment() agent = Agent(game, n0) l_mse = agent.TD_control_linear(iterations,l,avg_it) mse.append(l_mse) plt.plot(mlambda,mse) plt.ylabel('mse') plt.show()
def test_monte_carlo(iterations=1000000, n0=100): print "\n-------------------" print "Monte Carlo control" print "run for n. iterations: "+str(iterations) print "win percentage: " # learn game = Environment() agent = Agent(game, n0) agent.MC_control(iterations) # plot and store agent.show_statevalue_function() agent.store_Qvalue_function()
def test_linear_sarsa(iterations=1000, mlambda=None, n0=100, avg_it=100): print "\n-------------------" print "TD control Sarsa, with Linear function approximation" print "run for n. iterations: " + str(iterations) print "plot graph mse vs episodes for lambda equal 0 and lambda equal 1" print "list (std output) win percentage for values of lambda 0, 0.1, 0.2, ..., 0.9, 1" monte_carlo_Q = pickle.load( open("Data/Qval_func_1000000_MC_control.pkl", "rb")) n_elements = monte_carlo_Q.shape[0] * monte_carlo_Q.shape[1] * 2 mse = [] if not isinstance(mlambda, list): # if no value is passed for lambda, default 0.5 l = 0.5 if mlambda == None else mlambda # learn game = Environment() agent = Agent(game, n0) agent.TD_control_linear(iterations, l, avg_it) agent.show_statevalue_function() else: # test each value of lambda for l in mlambda: game = Environment() agent = Agent(game, n0) l_mse = agent.TD_control_linear(iterations, l, avg_it) mse.append(l_mse) plt.plot(mlambda, mse) plt.ylabel('mse') plt.show()
def reset(self, training=True): # Generate new obstacles self.generate_obstacles(self.n_rows, self.n_cols) # Generate agents positions team_agents, team_opponents = self.generate_random_agents( n_agents=self.n_agents, n_opponents=self.n_opponents, rows=self.n_rows, cols=self.n_cols, cluster=True) # Instantiate agents self.agents = [Agent(pos, i == 0) for i, pos in enumerate(team_agents)] self.opponents = [DummyAgent(position=pos) for pos in team_opponents] # Assign training and target nets to agents for a in self.agents: if a.training: a.brain = self.training_net else: a.brain = self.target_net # Generate all allowed moves for each position of the board self.allowed_moves_per_position = self.create_allowed_moves() self.training_net.training = training
def test_presence_request_server_response(): sensor1 = Sensor('A222', 21.02, 'second', 'accelerometr') sensor2 = Sensor('B333', 56.0003, 'meter', 'mleko') agent = Agent(1, 'xxx', 'rrr', 'yyy', [sensor1, sensor2]) response = log.presence_request(agent, 'mleko', None, None, 'SL', None, None) assert response.status != 404 and response.status != 500
def test__presence_request_json(): agent = Agent(1, 'Adam1', 'Testowy agent', 'agent_mobilny', []) result = log._presence_request_json(agent, res('logger\\precence_type\\start'), protocol=None, request_type=None, language='SL', action_receiver_id=3, action_sender_id=None) json_data = json.loads(result) assert json_data['content'] == 'presenceStatus:start_presence,agent:Adam1'
def test_monte_carlo(iterations=1000000, n0=100): print "\n-------------------" print "Monte Carlo control" print "run for n. iterations: " + str(iterations) print "win percentage: " # learn game = Environment() agent = Agent(game, n0) agent.MC_control(iterations) # plot and store agent.show_statevalue_function() agent.store_Qvalue_function()
def test__agent_registration_json_agent_without_sensors(): agent = Agent(1, 'Adam1', 'Testowy agent', 'agent_mobilny', []) result = log._agent_registration_json(agent, protocol=None, request_type=None, language='SL', action_receiver_id=3, action_sender_id=None) json_data = json.loads(result) assert json_data[ 'content'] == 'agentName:Adam1,agentDescription:Testowy agent,agentType:agent_mobilny,sensors:' assert json_data['action_protocol'] is None assert json_data['action_requesttype'] is None assert json_data['language'] == 'SL' assert json_data['action_receiver']['id'] == 3 assert json_data['action_sender'] is None
def __init__(self, n_rows, n_cols, n_agents, n_opponents, gcr_weight=0.5, lcr_weight=0.5, rr_weight=0.5): self.n_rows = n_rows self.n_cols = n_cols self.n_agents = n_agents self.n_opponents = n_opponents self.obstacles = None self.allowed_moves_per_position = None self.generate_obstacles(n_rows, n_cols) team_agents, team_opponents = self.generate_random_agents( n_agents=n_agents, n_opponents=n_opponents, rows=n_rows, cols=n_cols, cluster=True) self.agents = [Agent(pos, i == 0) for i, pos in enumerate(team_agents)] self.training_net = Brain(True) self.target_net = Brain(False) for a in self.agents: if a.training: a.brain = self.training_net else: a.brain = self.target_net self.opponents = [DummyAgent(position=pos) for pos in team_opponents] self.global_capture_weight = gcr_weight self.local_capture_weight = gcr_weight self.reachability_weight = rr_weight self.training_ate_weight = lcr_weight
def test__agent_registration_json_agent_with_sensors(): agent = Agent(1, 'Adam1', 'Testowy agent', 'agent_mobilny', []) sensor1 = Sensor('A222', 21.02, 'second', 'accelerometr') sensor2 = Sensor('B333', 56.0003, 'meter', 'mleko') agent.sensor_list.append(sensor1) agent.sensor_list.append(sensor2) result = log._agent_registration_json(agent, protocol=None, request_type=None, language='SL', action_receiver_id=None, action_sender_id=1) json_data = json.loads(result) assert json_data['content'] == 'agentName:Adam1,agentDescription:Testowy agent,agentType:agent_mobilny,' \ + 'sensors:sensorName-A222 sensorAccuracy-21.02 ' \ + 'unitType-second sensorType-accelerometr;' \ + 'sensorName-B333 sensorAccuracy-56.0003 unitType-meter sensorType-mleko' assert json_data['action_protocol'] is None assert json_data['action_requesttype'] is None assert json_data['language'] == 'SL' assert json_data['action_receiver'] is None assert json_data['action_sender']['id'] == 1
def test_agent_registration_server_response_empty_strings(): agent = Agent(1, '', '', '', []) response = log.agent_registration(agent, None, None, 'SL', None, None) assert response.status != 404 and response.status != 500
def test_presence_request_server_response_empty_strings(): agent = Agent(1, '', '', '', []) response = log.presence_request(agent, 'mleko', None, None, 'SL', None, None) assert response.status != 404 and response.status != 500
def test_td_control(iter=50000): lambdas = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] for l in lambdas: agent = Agent() agent.TD_control(iter, l) agent.plot_state()
def test_mc_control(iter=50000, n0=100): agent = Agent() agent.MC_control(iter, n0) agent.plot_state()
def test_td_control_linear_app(iter=50000): lambdas = [0.0, 1.0] for l in lambdas: agent = Agent() agent.TD_control_linear_app(iter, 0.9) agent.plot_state()
def get_agents(): return agent def perform_agent(name, method: str): getattr(agent, method)() def perform_recorder(name, method: str): recorder = [recorder for recorder in recorders if recorder.name == name][0] getattr(recorder, method)() recorders = create_recorders() agent = Agent("Main Agent", recorders) # agent.start() # # agent.stop() remote_api_cfg = cfg.get_configuration_of('remote_api_config') api_server = RemoteAPIServer(remote_api_cfg.get('ip'), remote_api_cfg.get('port')) logging.debug("Create RemoteAPIServer") api_server.register_function(get_agents) api_server.register_function(perform_agent) api_server.register_function(perform_recorder) api_server.start_listen()