Exemple #1
0
    def test_step_find_max_one_packedagent(self):
        '''
        Test where QCoordinator has 1 sub-agent that uses a packed space,
        which prefers the action ab=24. 
        '''
        # we don't want to test spaces but we need to get DecoratedSpace
        # so it seems easier to make a real space anyway.
        space = spaces.Dict({'a': spaces.Discrete(3), 'b': spaces.Discrete(7)})
        # env = Mock(spec=Env)
        # env.action_space = space

        # we also grab this moment to demo packedspace again.
        # for proper junit test this should be mocked.
        packedspace = PackedSpace(space, {'ab': ['a', 'b']})
        packedenv = Mock(spec=Env)
        packedenv.action_space = packedspace

        component1 = Mock(spec=QAgent)
        component1.agentId = 'a'  # the controlled entity
        component1.getQ = Mock(side_effect=testQCoordinator.maxAtAB16)
        component1.getActionSpace = Mock(return_value=packedspace)

        coordinator = QCoordinator([component1], space, None)
        bestAction = coordinator.step()
        # 16  = 3*5+1
        self.assertEqual(1, bestAction['a'])
        self.assertEqual(5, bestAction['b'])
Exemple #2
0
def main():
    """
    Demo how to run an agent
    """
    dirname = os.path.dirname(__file__)

    if (len(sys.argv) > 1):
        env_configName = str(sys.argv[1])
        agent_configName = str(sys.argv[2])

    else:
        print("Default config ")
        env_configName = "./configs/factory_floor_experiment.yaml"
        env_filename = os.path.join(dirname, env_configName)
        agent_configName = "./configs/agent_combined_config.yaml"
        agent_filename = os.path.join(dirname, agent_configName)

    env_parameters = getParameters(env_filename)
    agent_parameters = getParameters(agent_filename)

    # whao, you need to know exact contents of all files here..
    recursive_update(
        agent_parameters['subAgentList'][0]['parameters']['simulator'],
        env_parameters['environment'])

    print(env_parameters)
    print(agent_parameters)

    random.seed(env_parameters['seed'])
    maxSteps = env_parameters['max_steps']
    baseEnv = FactoryFloor(env_parameters['environment'])
    packedActionSpace = PackedSpace(baseEnv.action_space,
                                    {"robots": ["robot1", "robot2", "robot3"]})
    env = ModifiedGymEnv(baseEnv, packedActionSpace)

    logging.info("Starting example MCTS agent")
    logoutput = io.StringIO("episode output log")

    try:
        logoutputpickle = open('./' + os.environ["SLURM_JOB_ID"] + '.pickle',
                               'wb')
    except KeyError:
        print("No SLURM_JOB_ID found")
        logoutputpickle = io.BytesIO()

    obs = env.reset()
    complexAgent = createAgent(env.action_space, env.observation_space,
                               agent_parameters)

    experiment = Experiment(complexAgent, env, maxSteps, render=True)
    experiment.addListener(JsonLogger(logoutput))
    experiment.addListener(PickleLogger(logoutputpickle))
    stats, confidence_ints = experiment.run()
    logoutputpickle.close()

    print("json output:", logoutput.getvalue())

    print("\n\nREWARD STATS: " + str(stats) + " \nCONFIDENCE INTERVALS " +
          str(confidence_ints))
    def test_Run(self):

        params = {
            'steps':
            10,
            'robots': [{
                'id': ENTITY1,
                'pos': 'random'
            }, {
                'id': ENTITY2,
                'pos': 'random'
            }, {
                'id': ENTITY3,
                'pos': 'random'
            }],  # initial robot positions
            'seed':
            None,
            'map': ['....', '....', '....', '....']
        }

        basicEnv = GroupingRobots(params)
        logoutput = io.StringIO("episode output log")
        packedActionSpace = PackedSpace(basicEnv.action_space, {
            "e1": [ENTITY1],
            "e23": [ENTITY2, ENTITY3]
        })
        env = ModifiedGymEnv(basicEnv, packedActionSpace)

        agent1 = RandomAgent("e1", env.action_space, env.observation_space)
        agent2 = QAgent("e23", env.action_space, env.observation_space, {
            'alpha': 0.4,
            'gamma': 1,
            'epsilon': 0.1
        })
        complexAgent = BasicComplexAgent([agent1, agent2],
                                         basicEnv.action_space,
                                         basicEnv.observation_space)

        episode = Episode(complexAgent, env, None, True, 0)
        episode.addListener(JsonLogger(logoutput))
        episode.run()

        print("json output:", logoutput.getvalue()
              )  # logs from all episodes within the experiment
Exemple #4
0
def learnEpisode(randomentity: str) -> QAgent:
    '''
    @param randomentity the entity controlled by the randomagent.
    Should be one of {ENTITY1,ENTITY2, ENTITY3}.
    The remaining entities will be controlled by the QAgent.
    @return the trained QAgent.
    '''
    qagententities = [ENTITY1, ENTITY2, ENTITY3]
    qagententities.remove(randomentity)

    basicEnv = GroupingRobots(params)
    packedActionSpace = PackedSpace(basicEnv.action_space, \
                {"random":[randomentity], "qlearn": qagententities})
    env = ModifiedGymEnv(basicEnv, packedActionSpace)
    agent1 = RandomAgent("random", env.action_space, env.observation_space)
    agent2 = QAgent("qlearn", env.action_space, env.observation_space,
                    LEARN_PARAMS)
    complexAgent = BasicComplexAgent([agent1, agent2], env.action_space,
                                     env.observation_space)
    episode = Episode(complexAgent, env, None, False, 0)
    episode.run()

    print("learned episode")
    return agent2
Exemple #5
0
 def test_smoke(self):
     space = Dict({'a': Discrete(4), 'b': Discrete(3), 'c': Discrete(5)})
     PackedSpace(space, {'a_b': ['a', 'b']})
Exemple #6
0
 def test_smoke_bad_packingkeys(self):
     space = Dict({'a': Discrete(4), 'b': Discrete(3), 'c': Discrete(5)})
     with self.assertRaises(Exception) as context:
         PackedSpace(space, {'x': ['unknownkey']})
     self.assertContains('refers unknown key', context.exception)
Exemple #7
0
 def test_smoke_bad_actionspace(self):
     with self.assertRaises(Exception) as context:
         PackedSpace({'a': 1, 'b': 2, 'c': 3}, {'a_b': ['a', 'b']})
     self.assertContains('Unsupported space type', context.exception)