예제 #1
0
    def __init__(self,
                 grid_name='DiscountGrid',
                 discount=0.9,
                 learning_rate=0.5,
                 living_reward=0.0,
                 noise=0,
                 epsilon=0.3,
                 display_speed=0.5,
                 grid_size=150,
                 text_only=False,
                 n_episodes=100,
                 agent_window_size=1,
                 agent_max_n_experiences=1000,
                 check_value_converge=False,
                 check_policy_converge=False,
                 optimal_policy=None,
                 expr_log_dir=None,
                 delta=0.02,
                 is_use_q_agent=False,
                 init_temp=1024.0,
                 temp_decrease_rate=2.0,
                 is_asyn_input=True):

        ###########################
        # GENERAL CONTROL
        ###########################

        self.text_only = text_only
        self.display_speed = display_speed
        self.n_episodes = n_episodes
        self.discount = discount
        self.check_value_converge = check_value_converge
        self.check_policy_converge = check_policy_converge
        self.optimal_policy = optimal_policy
        self.expr_log_dir = expr_log_dir
        self.delta = delta

        ###########################
        # GET THE INPUT MODULE
        ###########################

        if is_use_q_agent:
            self.user_input_module = None
        else:
            self.user_input_module = user_input.UserInputModule(
                is_asyn=is_asyn_input)

        ###########################
        # GET THE GRIDWORLD
        ###########################

        # noinspection PyUnresolvedReferences
        import gridworld
        mdp_function = getattr(gridworld, "get" + grid_name)
        self.mdp = mdp_function()
        self.mdp.setLivingReward(living_reward)
        self.mdp.setNoise(noise)
        self.env = gridworld.GridworldEnvironment(self.mdp)

        ###########################
        # GET THE DISPLAY ADAPTER
        ###########################

        import textGridworldDisplay
        self.display = textGridworldDisplay.TextGridworldDisplay(self.mdp)
        if not text_only:
            import graphicsGridworldDisplay
            self.display = graphicsGridworldDisplay.GraphicsGridworldDisplay(
                self.mdp, grid_size, display_speed)
        try:
            self.display.start()
        except KeyboardInterrupt:
            sys.exit(0)

        ###########################
        # GET THE TAMER AGENT
        ###########################

        import qlearningAgents
        # env.getPossibleActions, opts.discount, opts.learningRate, opts.epsilon
        # simulationFn = lambda agent, state: simulation.GridworldSimulation(agent,state,mdp)
        self.gridWorldEnv = GridworldEnvironment(self.mdp)
        action_function = lambda state: self.mdp.getPossibleActions(state)
        q_learn_opts = {
            'gamma': discount,
            'alpha': learning_rate,
            'epsilon': epsilon,
            'actionFn': action_function,
            'init_temp': init_temp,
            'temp_decrease_rate': temp_decrease_rate
        }

        if is_use_q_agent:
            self.agent = qlearningAgents.QLearningAgent(**q_learn_opts)
        else:
            self.agent = qlearningAgents.TamerQAgent(
                max_n_experiences=agent_max_n_experiences,
                window_size=agent_window_size,
                is_asyn_input=is_asyn_input,
                **q_learn_opts)
예제 #2
0
    def __init__(self, grid_name='DiscountGrid', discount=0.9, learning_rate=0.5, living_reward=0.0
                 , noise=0, epsilon=0.3, display_speed=0.5
                 , grid_size=150, text_only=False, n_episodes=100
                 , agent_window_size=1
                 , agent_max_n_experiences=1000
                 , check_policy_converge=False
                 , optimal_policy=None
                 , expr_log_dir=None
                 , agent_type="qLearningAgent"
                 , init_temp=1024.0
                 , temp_decrease_rate=2.0
                 , is_asyn_input=True):
        """
        :param agent_type: "qLearningAgent" or "TamerAgent" or "preferenceTAMERAgent"
        """
        ###########################
        # GENERAL CONTROL
        ###########################

        self.text_only = text_only
        self.display_speed = display_speed
        self.n_episodes = n_episodes
        self.discount = discount
        self.check_policy_converge = check_policy_converge
        self.optimal_policy = optimal_policy
        self.expr_log_dir = expr_log_dir
        self.save_VDBE = False

        ###########################
        # GET THE INPUT MODULE
        ###########################
        if agent_type == qlearningAgents.QLearningAgent.getAgentType():
            self.user_input_module = None
        else:
            self.user_input_module = user_input.UserInputModule(is_asyn=is_asyn_input)

        ###########################
        # GET THE GRIDWORLD
        ###########################

        # noinspection PyUnresolvedReferences
        import gridworld
        mdp_function = getattr(gridworld, "get" + grid_name)
        self.mdp = mdp_function()
        self.mdp.setLivingReward(living_reward)
        self.mdp.setNoise(noise)
        self.env = gridworld.GridworldEnvironment(self.mdp)

        ###########################
        # Variables used to store parameters values
        ###########################

        # init VDBE values records
        global VDBE_RECORDS
        VDBE_RECORDS = dict()
        for state in self.env.getGridWorld().getNonTerminalStates():
            VDBE_RECORDS[state] = list()

        ###########################
        # GET THE DISPLAY ADAPTER
        ###########################

        import textGridworldDisplay
        self.display = textGridworldDisplay.TextGridworldDisplay(self.mdp)
        if not text_only:
            import graphicsGridworldDisplay
            self.display = graphicsGridworldDisplay.GraphicsGridworldDisplay(self.mdp, grid_size, display_speed)
        try:
            self.display.start()
        except KeyboardInterrupt:
            sys.exit(0)

        ###########################
        # GET THE TAMER AGENT
        ###########################

        # env.getPossibleActions, opts.discount, opts.learningRate, opts.epsilon
        # simulationFn = lambda agent, state: simulation.GridworldSimulation(agent,state,mdp)
        self.gridWorldEnv = GridworldEnvironment(self.mdp)
        action_function = lambda m_state: self.mdp.getPossibleActions(m_state)
        q_learn_opts = {
            'gamma': discount,
            'alpha': learning_rate,
            'epsilon': epsilon,
            'actionFn': action_function,
            'init_temp': init_temp,
            'temp_decrease_rate': temp_decrease_rate
        }

        if agent_type == qlearningAgents.QLearningAgent.getAgentType():
            self.agent = qlearningAgents.QLearningAgent(**q_learn_opts)
        elif agent_type == qlearningAgents.TamerQAgent.getAgentType():
            self.agent = qlearningAgents.TamerQAgent(max_n_experiences=agent_max_n_experiences
                                                     , window_size=agent_window_size
                                                     , is_asyn_input=is_asyn_input
                                                     , **q_learn_opts)
        elif agent_type == preferenceTamerAgent.PreferenceTAMERAgent.getAgentType():
            self.agent = preferenceTamerAgent.PreferenceTAMERAgent(max_n_experiences=agent_max_n_experiences
                                                     , window_size=agent_window_size
                                                     , is_asyn_input=is_asyn_input
                                                     , **q_learn_opts)
예제 #3
0
    def __init__(self,
                 grid_name='DiscountGrid',
                 discount=0.9,
                 learning_rate=0.5,
                 living_reward=0.0,
                 noise=0.2,
                 epsilon=0.3,
                 display_speed=0.5,
                 grid_size=150,
                 text_only=False,
                 n_episodes=100,
                 agent_window_size=1,
                 agent_max_n_experiences=1000,
                 is_use_q_agent=False):
        self.text_only = text_only
        self.display_speed = display_speed
        self.n_episodes = n_episodes
        self.discount = discount

        ###########################
        # GET THE INPUT MODULE
        ###########################

        if is_use_q_agent:
            self.user_input_module = None
        else:
            self.user_input_module = user_input.UserInputModule()

        ###########################
        # GET THE GRIDWORLD
        ###########################

        # noinspection PyUnresolvedReferences
        import gridworld
        mdp_function = getattr(gridworld, "get" + grid_name)
        self.mdp = mdp_function()
        self.mdp.setLivingReward(living_reward)
        self.mdp.setNoise(noise)
        self.env = gridworld.GridworldEnvironment(self.mdp)

        ###########################
        # GET THE DISPLAY ADAPTER
        ###########################

        import textGridworldDisplay
        self.display = textGridworldDisplay.TextGridworldDisplay(self.mdp)
        if not text_only:
            import graphicsGridworldDisplay
            self.display = graphicsGridworldDisplay.GraphicsGridworldDisplay(
                self.mdp, grid_size, display_speed)
        try:
            self.display.start()
        except KeyboardInterrupt:
            sys.exit(0)

        ###########################
        # GET THE TAMER AGENT
        ###########################

        import qlearningAgents
        # env.getPossibleActions, opts.discount, opts.learningRate, opts.epsilon
        # simulationFn = lambda agent, state: simulation.GridworldSimulation(agent,state,mdp)
        self.gridWorldEnv = GridworldEnvironment(self.mdp)
        action_function = lambda state: self.mdp.getPossibleActions(state)
        q_learn_opts = {
            'gamma': discount,
            'alpha': learning_rate,
            'epsilon': epsilon,
            'actionFn': action_function
        }

        if is_use_q_agent:
            self.agent = qlearningAgents.QLearningAgent(**q_learn_opts)
        else:
            self.agent = qlearningAgents.TamerQAgent(
                max_n_experiences=agent_max_n_experiences,
                window_size=agent_window_size,
                **q_learn_opts)