Пример #1
0
def agent():
    env = RemoteEnv(IN_PORT=8080, OUT_PORT=8081, host="127.0.0.1")
    env.open(0)
    actions = {'w': 0, 's': 3, 'a': 4, 'd': 1}
    for i in range(10000000):
        #state = env.step("restart")
        state = env.step("restart")
        prev_energy = state['energy']
        done = state['done']
        print("OK")
        while not done:
            print('Action: ', end='')
            action = getch.getche()
            #action = np.random.choice([0, 1])
            reward_sum = 0
            state = env.step("act", actions[action])
            reward_sum += state['reward']
            for i in range(1):
                done = state['done']
                if (not done):
                    state = env.step('act', -1)
                    reward_sum += state['reward']
                else:
                    break

            energy = state['energy']
            prev_energy = energy
            frame = state['frame']
            print(transform(frame))
            print('Reward: ', reward_sum)
            print('Done: ', state['done'])
            prev_energy = energy
        if i >= 2:
            break
    env.close()
def agent():
    env = RemoteEnv(IN_PORT=8080, OUT_PORT=7070, host="127.0.0.1")
    env.open(0)
    for i in range(10000000):
        #state = env.step("restart")
        state = env.step("restart")
        prev_energy = state['energy']
        done = state['done']
        print("OK")
        while not done:
            action = int(input('action'))
            #action = np.random.choice([0, 1])
            reward_sum = 0
            touched = np.zeros(8)
            for i in range(8):
                state = env.step("act", action)
                energy = state['energy']
                touched[i] = state['touched']
                reward_sum += state['reward']
                if state['done']:
                    break
            done = state['done']
            prev_energy = energy
            frame = state['frame']
            frame = image_decode(state['frame'], 20, 20)
            print(frame)
            print('Reward: ', reward_sum)
            print('Touched: ', touched)
            print('Signal: ', state['signal'])
            print('Done: ', state['done'])
            prev_energy = energy
        if i >= 2:
            break
    env.close()
def agent():
    env = RemoteEnv(IN_PORT=8081, OUT_PORT=7071)
    env.open(0)
    for i in range(10000000):
        state = env.step("restart")
        done = state['done']
        print('new episode')
        while not done:
            frame = image_from_str(state['state'], 10, 10)
            print(frame)
            action = int(input('action'))
            state = env.step("move", action)
            done = state['done']
            print(state)
    env.close()
Пример #4
0
def agent():
    env = RemoteEnv(IN_PORT=8080, OUT_PORT=7070, host="127.0.0.1")
    env.open(0)
    actions = {
        'w': ('walk', 10),
        's': ('walk', -10),
        'd': ('walk_in_circle', 10),
        'a': ('walk_in_circle', -10),
        'r': ('run', 20)
    }
    for i in range(10000000):
        #state = env.step("restart")
        state = env.step("restart")
        prev_energy = state['energy']
        done = state['done']
        print("OK")
        while not done:
            print('Action: ', end='')
            action = getch.getche()
            #action = np.random.choice([0, 1])
            reward_sum = 0
            for i in range(7):
                state = env.step(actions[action][0], actions[action][1])
                reward_sum += state['reward']
                if (done):
                    break
            if not done:
                state = env.step("get_result")
                reward_sum += state['reward']
                done = state['done']

            energy = state['energy']
            prev_energy = energy
            frame = state['frame']
            print(transform(frame))
            print('Reward: ', reward_sum)
            print('Done: ', state['done'])
            print('Delta energy: ', prev_energy - energy)
            prev_energy = energy
        if i >= 2:
            break
    env.close()
Пример #5
0
from ai4u.core import RemoteEnv

env = RemoteEnv()
env.open()

env.step('tx', 5)

env.close()
from ai4u.core import RemoteEnv
import numpy as np

env = RemoteEnv()
env.open(0)

if __name__ == "__main__":
    for i in range(10):
        sum_reward = 0
        state = env.step("restart")
        print(state)
        done = state['done']
        while not done:
            frame = None
            action = int(
                input("action-----------------------------------------"))
            if action == 0:
                state = env.step("fx", 0.1)
            elif action == 1:
                state = env.step("fz", 0.1)
            else:
                state = env.step("noop", 0.0)

            done = state["done"]
            reward = state['reward']
            sum_reward += reward
        print(sum_reward)
env.close()
Пример #7
0
    def configure(self, environment_definitions, port_inc=0):
        if 'action_space' in environment_definitions:
            self.action_space = environment_definitions['action_space']
        elif 'action_shape' in environment_definitions:
            self.action_space = spaces.Discrete(
                environment_definitions['action_shape'][0])
        else:
            raise Exception(
                'environment_definitions do not contains action_space or action_shape'
            )

        assert 'actions' in environment_definitions, 'environment_definitions do not contain actions (a list of actions descriptors)'
        self.actions = environment_definitions['actions']

        if 'observation_space' in environment_definitions:
            self.observation_space = environment_definitions[
                'observation_space']
        elif 'state_shape' in environment_definitions:
            state_shape = environment_definitions['state_shape']
            state_type = environment_definitions['state_type']
            min_value = environment_definitions['min_value']
            max_value = environment_definitions['max_value']
            self.observation_space = spaces.Box(low=min_value,
                                                high=max_value,
                                                shape=state_shape,
                                                dtype=state_type)
        else:
            raise Exception(
                'environment_definitions do not contain observation_space or state_shape.'
            )

        if 'n_envs' in environment_definitions:
            self.n_envs = environment_definitions['n_envs']
        else:
            self.n_envs = 1

        self.verbose = False

        if 'verbose' in environment_definitions:
            self.verbose = environment_definitions['verbose']

        if 'action_meaning' in environment_definitions:
            self.action_meaning = environment_definitions['action_meaning']
        else:
            self.action_meaning = ['action'] * len(self.actions)

        if 'seed' in environment_definitions:
            self._seed = environment_definitions['seed']
        else:
            self._seed = 0

        if 'agent' in environment_definitions:
            if inspect.isclass(environment_definitions['agent']):
                self.agent = environment_definitions['agent']()
            else:
                raise Exception("Agent object is not a class!!!!")
        else:
            self.agent = BasicAgent()

        host = '127.0.0.1'
        if host in environment_definitions:
            host = environment_definitions['host']

        input_port = 8080
        if 'input_port' in environment_definitions:
            input_port = environment_definitions['input_port']

        output_port = 8081
        if 'output_port' in environment_definitions:
            output_port = environment_definitions['output_port']

        self.remoteenv = RemoteEnv(host, output_port + port_inc,
                                   input_port + port_inc)
        self.id = port_inc
        self.remoteenv.verbose = self.verbose
        self.remoteenv.open(0)
        self.nlives = 1
        self.state = None
        self.configureFlag = True
Пример #8
0
class Environment(gym.Env):
    metadata = {'render.modes': ['human']}

    def __init__(self):
        super(Environment, self).__init__()
        self.ale = AleWrapper(self)
        self.configureFlag = False
        self.id = 0
        if not (BasicAgent.environment_definitions is None):
            self.configure(BasicAgent.environment_definitions,
                           BasicAgent.environment_port_id)
            BasicAgent.environment_port_id += 1

    def configure(self, environment_definitions, port_inc=0):
        if 'action_space' in environment_definitions:
            self.action_space = environment_definitions['action_space']
        elif 'action_shape' in environment_definitions:
            self.action_space = spaces.Discrete(
                environment_definitions['action_shape'][0])
        else:
            raise Exception(
                'environment_definitions do not contains action_space or action_shape'
            )

        assert 'actions' in environment_definitions, 'environment_definitions do not contain actions (a list of actions descriptors)'
        self.actions = environment_definitions['actions']

        if 'observation_space' in environment_definitions:
            self.observation_space = environment_definitions[
                'observation_space']
        elif 'state_shape' in environment_definitions:
            state_shape = environment_definitions['state_shape']
            state_type = environment_definitions['state_type']
            min_value = environment_definitions['min_value']
            max_value = environment_definitions['max_value']
            self.observation_space = spaces.Box(low=min_value,
                                                high=max_value,
                                                shape=state_shape,
                                                dtype=state_type)
        else:
            raise Exception(
                'environment_definitions do not contain observation_space or state_shape.'
            )

        if 'n_envs' in environment_definitions:
            self.n_envs = environment_definitions['n_envs']
        else:
            self.n_envs = 1

        self.verbose = False

        if 'verbose' in environment_definitions:
            self.verbose = environment_definitions['verbose']

        if 'action_meaning' in environment_definitions:
            self.action_meaning = environment_definitions['action_meaning']
        else:
            self.action_meaning = ['action'] * len(self.actions)

        if 'seed' in environment_definitions:
            self._seed = environment_definitions['seed']
        else:
            self._seed = 0

        if 'agent' in environment_definitions:
            if inspect.isclass(environment_definitions['agent']):
                self.agent = environment_definitions['agent']()
            else:
                raise Exception("Agent object is not a class!!!!")
        else:
            self.agent = BasicAgent()

        host = '127.0.0.1'
        if host in environment_definitions:
            host = environment_definitions['host']

        input_port = 8080
        if 'input_port' in environment_definitions:
            input_port = environment_definitions['input_port']

        output_port = 8081
        if 'output_port' in environment_definitions:
            output_port = environment_definitions['output_port']

        self.remoteenv = RemoteEnv(host, output_port + port_inc,
                                   input_port + port_inc)
        self.id = port_inc
        self.remoteenv.verbose = self.verbose
        self.remoteenv.open(0)
        self.nlives = 1
        self.state = None
        self.configureFlag = True

    def seed(self, seed=None):
        self.agent.seed(seed, self)

    def get_action_meanings(self):
        self.__check_configuration_()
        return [self.action_meaning[i] for i in range(len(self.actions))]

    def reset(self):
        self.__check_configuration_()
        return self.agent.reset(self)

    def render(self, mode='human', close=False):
        self.__check_configuration_()
        self.agent.render()

    def close(self):
        self.__check_configuration_()
        self.remoteenv.close()

    def one_step(self, action):
        return self.remoteenv.step(self.actions[action][0],
                                   self.actions[action][1])

    def one_stepfv(self, action):
        return self.remoteenv.stepfv(self.actions[action][0],
                                     self.actions[action][1])

    def step(self, action, info=None):
        self.__check_configuration_()
        return self.agent.act(self, action, info)

    def __del__(self):
        self.__check_configuration_()
        self.remoteenv.close()

    def __check_configuration_(self):
        if not self.configureFlag:
            raise Exception(
                "The environment is not configured. Try to set up the environment before trying again!!!"
            )