def test_invade_3(self): env = Environment("map_init.txt", "population_init.txt") try: env.invade(GamePlayId.P2, 1, 4, 1) except Exception as error: self.assertEqual( str(error), "Can't invade no route from ( " + str(1) + " ) to ( " + str(4) + " )")
def test_march_5(self): env = Environment("map_init.txt", "population_init.txt") try: env.march_troops(GamePlayId.P1, 1, 5, 1) self.fail() except Exception as error: self.assertEqual( str(error), "Can't march no route from ( " + str(1) + " ) to ( " + str(5) + " )")
def test_march_4(self): env = Environment("map_init.txt", "population_init.txt") try: env.march_troops(GamePlayId.P1, 1, 3, 13) self.fail() except Exception as error: self.assertEqual( str(error), "Not enough troops to march from ( " + str(1) + " ) to ( " + str(3) + " ) troops are in country are " + str(7))
def test_march_3(self): env = Environment("map_init.txt", "population_init.txt") try: env.march_troops(GamePlayId.P1, 1, 4, 1) self.fail() except Exception as error: self.assertEqual( str(error), "Can't march troops to unowned country : country owner (" + str(GamePlayId.P2) + ") ," + "player (" + str(GamePlayId.P1) + ")")
def test2_greedy(self): env = Environment("map_init.txt", "population_init.txt") state = EnvState(env, None, None, GamePlayId.P1) player1 = Greedy(GamePlayId.P1) player2 = PacifistAgent(GamePlayId.P2) player1.search(state, player2) print(player1.writeout_path())
def test3_Astar(self): env = Environment("map_init.txt", "population_init.txt") state = EnvState(env, None, None, GamePlayId.P1) player1 = AStar(GamePlayId.P2) player2 = PassiveAgent(GamePlayId.P1) res = player1.search(state, player2) print(player1.writeout_path())
def test_invade_1(self): env = Environment("map_init.txt", "population_init.txt") p2 = Human(GamePlayId.P2) p2.invade(env, 2, 3, 6) self.assertEqual(env.country_list[1].troops_count, 1) self.assertEqual(env.country_list[2].troops_count, 1) self.assertEqual(env.country_list[1].owner_id , GamePlayId.P2)
def test_invade_2(self): env = Environment("map_init.txt", "population_init.txt") p2 = Human(GamePlayId.P2) try: p2.invade(env, 2, 4, 1) except Exception as error: self.assertEqual(str(error), "Can't invade your own country")
def __init__(self, input_stream: TextIOBase, output_stream: TextIOBase, debug=False): self.env = Environment() extend_environment(self.env) self.handlers_net = HandlersNet(self.env, input_stream, output_stream) if debug: enable_debug_logging()
def test_copy(self): env = Environment("map_init.txt", "population_init.txt") env_c = copy.deepcopy(env) env_c.country_list[0].owner_id = None env_c.border_list[0].country1 = None env_c.continent_list[0].owner_id = None self.assertNotEqual(env.country_list[0].owner_id, None) self.assertNotEqual(env.border_list[0].country1, None) self.assertNotEqual(env.continent_list[0].owner_id, None)
def test1_RTAstar(self): env = Environment("map_init.txt", "population_init.txt") state = EnvState(env, None, None, GamePlayId.P1) player1 = RTAStar(GamePlayId.P1) state = player1.search(state, MoveType.DEPLOY) print(state.env.change, state) state = player1.search(state, MoveType.MARCH) print(state.env.change, state) state = player1.search(state, MoveType.INVADE) print(state.env.change, state)
def test_march_2(self): env = Environment("map_init.txt", "population_init.txt") p1 = Human(GamePlayId.P1) try: p1.march_troops(env, 2, 3, 1) self.fail() except Exception as error: self.assertEqual(str(error), "Can't march troops from unowned country : country owner (" + str(GamePlayId.P2) + ") ," + "player (" + str(GamePlayId.P1) + ")")
def test_deploy_1(self): env = Environment("map_init.txt", "population_init.txt") p1 = Human(GamePlayId.P1) p2 = Human(GamePlayId.P2) p1.deploy_reserve_troops(env, 1, 1) try: p2.deploy_reserve_troops(env, 1, 1) self.fail() except Exception as error: self.assertEqual(str(error), "Can't deploy troops to unowned country : country owner (" + str(GamePlayId.P1) + ") ," + "player " + str(GamePlayId.P2) + ")")
def test_deploy_1(self): env = Environment("map_init.txt", "population_init.txt") env.reserve_1 = 1 env.deploy_reserve_troops(GamePlayId.P1, 1) try: env.deploy_reserve_troops(GamePlayId.NONE, 1) self.fail() except Exception as error: self.assertEqual( str(error), "Can't deploy troops to unowned country : country owner (" + str(GamePlayId.P1) + ") ," + "player " + str(GamePlayId.NONE) + ")")
def __init__(self, player1_type, player2_type): self.player1 = self.get_player(player1_type, GamePlayId.P1) self.player2 = self.get_player(player2_type, GamePlayId.P2) self.env = Environment("/server_files/map_init.txt", "/server_files/population_init.txt") self.env.reserve_1 = 2 self.env.reserve_2 = 2 self.turn = True self.state = EnvState(self.env, None, None, GamePlayId.P1) self.history = [] self.history_counter = 0 if player1_type == "astar": self.history = self.player1.search(self.state, self.player2) elif player1_type == "greedy": self.history = self.player1.search(self.state, self.player2) elif player2_type == "astar": self.history = self.player2.search(self.state, self.player1) elif player2_type == "greedy": self.history = self.player2.search(self.state, self.player1)
class ShellTransformerTest(unittest.TestCase): command_factory = CommandFactory(Environment()) def setUp(self) -> None: self.parser = ShellParser(ShellTransformerTest.command_factory) def parse(self, string): return self.parser.parse(string) def testEmpty(self): result = self.parse('') self.assertEqual(0, len(result)) def testEcho(self): result = self.parse('echo 123 | echo "hey" | echo 7') self.assertEqual(3, len(result)) args = ['123', 'hey', '7'] for command, arg in zip(result, args): self.assertTrue(isinstance(command, EchoCommand)) self.assertEqual([arg], command.args) def testExit(self): result = self.parse('exit') self.assertEqual(1, len(result)) command = result[0] self.assertTrue(isinstance(command, ExitCommand)) @parameterized.expand([ ('echo', 'echo 123', EchoCommand, ['123']), ('exit', 'exit', ExitCommand, []), ('cat', 'cat main.py', CatCommand, ['main.py']), ('custom command', '/bin/sh main.sh', CustomCommand, ['/bin/sh', 'main.sh']), ('equality', 'x = 3', AssignmentCommand, ['x', '3']), ('pwd', 'pwd', PwdCommand, []), ('wc', 'wc main.py', WcCommand, ['main.py']), ]) def test(self, _, string, command_type, args): result = self.parse(string) self.assertEqual(1, len(result)) command = result[0] self.assertTrue(isinstance(command, command_type)) self.assertEqual(args, command.args)
class EnvironmentTest(unittest.TestCase): def setUp(self) -> None: self.env = Environment() def testEmpty(self): self.assertRaises(ShellException, lambda: self.env.get('x')) def testExisting(self): self.env.set('x', 'y') self.assertEqual('y', self.env.get('x')) def testNonExisting(self): self.env.set('x', 'y') self.assertRaises(ShellException, lambda: self.env.get('y')) def testOsEnvironment(self): extend_environment(self.env) self.assertTrue('PATH' in self.env.data)
def setup(self): try: self.environment = Environment.setup(self.arguments) self.mission = Mission.create(self.environment, self.arguments) self.monitor = Monitor(self.mission, self.environment) except Exception: self.arguments.error(traceback.format_exc()) self.arguments.check_help() infrared_sensor = self.environment.get_infrared_sensor() if infrared_sensor is not None: infrared_sensor.register("start", self.enable) infrared_sensor.register("pause", self.monitor.pause) infrared_sensor.register("stop", self._infrared_disable) infrared_sensor.register("poweroff", self._infrared_poweroff) infrared_sensor.activate() else: self.activated = True print("Setting up mission") self.mission.setup() self.mission.display() self.monitor.setup() # Arm the vehicle and take off to the specified altitude if the vehicle # can fly. self.mission.arm_and_takeoff() self.mission.display() while not self.activated: self.monitor.sleep() self.start()
def setup(self): try: self.environment = Environment.setup(self.arguments) self.mission = Mission.create(self.environment, self.arguments) self.monitor = Monitor(self.mission, self.environment) except Exception: self.arguments.error(traceback.format_exc()) self.arguments.check_help() infrared_sensor = self.environment.get_infrared_sensor() if infrared_sensor is not None: infrared_sensor.register("start", self.enable) infrared_sensor.register("pause", self.monitor.pause) infrared_sensor.register("stop", self._infrared_disable) infrared_sensor.register("poweroff", self._infrared_poweroff) infrared_sensor.activate() else: self.activated = True print("Setting up mission") self.mission.setup() self.mission.display() self.monitor.setup() # Arm the vehicle and take off to the specified altitude if the vehicle # can fly. self.mission.arm_and_takeoff() self.mission.display() while not self.activated: self.monitor.sleep() self.start()
def test_march_1(self): env = Environment("map_init.txt", "population_init.txt") p1 = Human(GamePlayId.P1) p1.march_troops(env, 1, 3, 1) self.assertEqual(env.country_list[0].troops_count, 2) self.assertEqual(env.country_list[2].troops_count, 6)
def test_deploy_2(self): env = Environment("map_init.txt", "population_init.txt") p1 = Human(GamePlayId.P1) p1.deploy_reserve_troops(env, 1, 1) self.assertEqual(env.country_list[0].troops_count, 4)
def optimize_joint(system_nn, pol_nn, log_writer, **kwargs): # unpack kwargs horizon = kwargs.get("horizon") nb_iterations = kwargs.get("nb_iterations") batch_size = kwargs.get("batch_size") policy_fit = kwargs.get("policy", False) system_fit = kwargs.get("system", False) mc_samples = kwargs.get("mc_samples", 128) env = Environment(system_nn) agent = Agent(pol_nn, env, horizon) # Optimizers parameters_list = [] if policy_fit: parameters_list = parameters_list + list(pol_nn.parameters()) if system_fit: parameters_list = parameters_list + list(system_nn.parameters()) if parameters_list: lr = kwargs.get("learning_rate", .001) optimizer = Adam(parameters_list, lr=lr) for it in range(nb_iterations): loss = {} params = {} # set gradient to zero optimizer.zero_grad() # generate the batch _, states_batch, dist_batch, _, oha_batch, rew_batch = agent.sample_trajectory( batch_size) # Loss # system_loss = system_error(system_nn, pol_nn, states_batch, dist_batch, oha_batch, rew_batch) system_loss.backward(retain_graph=policy_fit) optimizer.step() system_nn.project_parameters() pol_nn.project_parameters() if system_fit and log_writer is not None: params['system'] = system_nn.unwrapped.named_parameters() log_writer.add_system_parameters(system_nn.parameters_dict(), step=it) if policy_fit and log_writer is not None: params['policy'] = pol_nn.named_parameters() actions = pol_nn( states_batch) # (B, H, A), need to stack along the B dim log_writer.add_policy_histograms(actions.view( -1, actions.shape[2]), step=it) if log_writer is not None: loss['loss'] = system_loss.item() log_writer.add_grad_histograms(params, step=it) log_writer.add_loss(loss, step=it) # performance of the agent on the epoch ep_perf, return_estimate = agent.avg_performance(mc_samples) log_writer.add_expected_return(ep_perf, step=it) log_writer.add_return(return_estimate, step=it) return env, agent
def setUp(self) -> None: self.parser = SubstitutionParser() self.env = Environment()
from tqdm import trange from agents.agents import * from environment.Environment import Environment if __name__ == '__main__': fst_agent = GreedyVFuncAgent(1, 0.7, 0.1) snd_agent = BasicVFuncAgent(2, 0.7) for _ in trange(1000): env = Environment() turn = 0 while not env.is_finished(env.field): # First agents turn current_state = env.get_state_description(env.field) possible_actions = env.get_possible_actions(fst_agent.player_num) chosen_action = fst_agent.chose_action(current_state, possible_actions) prev_field, action, new_field, is_done, rewards = env.step( chosen_action, fst_agent.player_num) fst_reward, snd_reward = rewards if is_done: fst_agent.learn(fst_reward) snd_agent.learn(snd_reward) break # Second agents turn current_state = env.get_state_description(env.field)
import numpy as np import torch import torch.optim as optim from agent.Agent import Agent from environment.Environment import Environment from policy.Policy import Policy from utils.utils import plot_training_evolution learning_rate = 0.01 discount_factor = 0.99 episodes = 5000 env = Environment('LunarLander-v2') policy: Policy = Policy(env.observation_space(), env.action_space()) optimizer = optim.Adam(policy.parameters(), lr=learning_rate) agent = Agent(env, policy, optimizer) for episode in range(episodes): agent.run_episode() agent.update_policy(discount_factor=discount_factor) if episode % 50 == 0: print('Episode {}\tAverage reward: {}'.format( episode, np.array(policy.reward_history[-50:]).mean())) if env.is_solved(np.array(policy.reward_history[-50:]).mean()): break
def test_reference(self): env = Environment("map_init.txt", "population_init.txt") p1 = Player(GamePlayId.P1) self.assertEqual(1, 1)
def extend_environment(environment: Environment) -> None: """Load variable from real environment.""" os_vars = os.environ for key, value in os_vars.items(): environment.set(key, value)
import torch from environment.Environment import Environment from policy.Policy import Policy env = Environment('LunarLander-v2') policy: Policy = Policy(env.observation_space(), env.action_space()) policy.load_state_dict(torch.load('saved_policy/policy.pt')) policy.eval() for episode in range(500): state = env.reset() done = False for time in range(1000): action = policy.select_action(state) state, reward, done, _ = env.step(action) env.render() if done: break env.close()
def setUp(self) -> None: self.environment = Environment() self.input_stream = io.StringIO() self.output_stream = io.StringIO()
class SubstitutionTest(unittest.TestCase): def setUp(self) -> None: self.parser = SubstitutionParser() self.env = Environment() def testSubstitution(self): self.env.set('x', 'x_value') res = self.parser.parse(self.env, '$x') self.assertEqual('x_value', res) def testSubstitutionInSingleQuotes(self): self.env.set('x', 'x_value') res = self.parser.parse(self.env, "'$x'") self.assertEqual("'$x'", res) def testSubstitutionInDoubleQuotes(self): self.env.set('x', 'x_value') res = self.parser.parse(self.env, '"hello $x"') self.assertEqual('"hello x_value"', res) def testSubstitutionInDoubleAndSingleQuotes(self): self.env.set('x', 'x_value') res = self.parser.parse(self.env, '"\'hello $x\'"') self.assertEqual('"\'hello x_value\'"', res) def testExit(self): self.env.set('x', 'ex') self.env.set('y', 'it') res = self.parser.parse(self.env, '$x$y') self.assertEqual('exit', res) @parameterized.expand([ 'echo hello | wc', 'echo "hello"', "echo 'hello'", 'x=ghj', 'cat file.txt', "cat '$x'", "cat '\"$x\"'", 'echo hello', 'echo \t hello \t', ' ', '', ]) def testWithoutSubstitution(self, string): res = self.parser.parse(self.env, string) self.assertEqual(string, res) @parameterized.expand([ ('illegal var name', '$67', 1), ('wrong quotes', 'echo \'hello"', 5), ('wrong quotes', "echo 'hello", 5), ]) def testUnexpectedCharacter(self, _, input_string, position): try: self.parser.parse(self.env, input_string) except ShellException as e: self.assertEqual( '[Substitution]Unexpected characters at position %d' % position, e.error) return assert False @parameterized.expand([ ('wrong quotes', 'echo "hello'), ('wrong quotes', 'echo "hello\''), ]) def testParseError(self, _, input_string): try: self.parser.parse(self.env, input_string) except ShellException as e: self.assertEqual('[Substitution]Parse error', e.error) return assert False
def setUp(self) -> None: self.env = Environment()