Ejemplo n.º 1
0
def main():
    env = ArmEnvToggleTopOnly(size_x=5,
                              size_y=5,
                              cubes_cnt=4,
                              episode_max_length=600,
                              finish_reward=100,
                              action_minus_reward=-0.001,
                              tower_target_size=4)
    vertexes = sorted([
        Stop(),
        Start(),
        Action(env.ACTIONS.LEFT),
        Action(env.ACTIONS.RIGHT),
        Action(env.ACTIONS.UP),
        Action(env.ACTIONS.DOWN),
        # Action(env.ACTIONS.TOGGLE),
        Choice(),
        # Action(env.ACTIONS.LEFT),
        # Action(env.ACTIONS.RIGHT),
        # Action(env.ACTIONS.UP),
        # Action(env.ACTIONS.DOWN),
        # Action(env.ACTIONS.TOGGLE),
        # Choice(),
    ])

    # clearing directory
    pathlib.Path('pics/').mkdir(parents=True, exist_ok=True)
    shutil.rmtree('pics/')
    pathlib.Path('pics/').mkdir(parents=True, exist_ok=True)
    # brute force
    for max_vertex_count in range(7):
        vc = vertex_combination(vertex_types=vertexes,
                                max_vertex_count=max_vertex_count)
        for index, vertex_types in enumerate(vc):
            for graph_id in sorted(
                    get_graph_id_fast(
                        MachineStored(vertex_types=vertex_types,
                                      binary_matrix_representation=412,
                                      env=env))):
                ms = MachineStored(vertex_types=vertex_types,
                                   binary_matrix_representation=graph_id,
                                   env=env)
                if is_ham_ok(ms.get_machine_without_on_model()):
                    if check_for_one_component_graph(
                            ms.get_machine_without_on_model()):
                        if is_it_machine_runnable(
                                ms.get_machine_without_on_model()):
                            ms.draw("pics/" + str(max_vertex_count) + ":" +
                                    str(index) + ":" + str(graph_id))
                            print("added")
Ejemplo n.º 2
0
def go(transitions, brute_force, index_):
    machine = AbstractMachine(MachineGraph(transitions=transitions))
    am = RootMachine(LoopInvokerMachine(machine))

    # if randrange(1000) == 0:
    #     draw_graph("{brute_force}".format(**locals()), am.get_graph_to_draw(action_to_name_mapping=env.get_actions_as_dict()))
    #     exit(0)

    if is_it_machine_runnable(machine):
        sum_rew = 0
        try:
            params = HAMParamsCommon(environments[0])
            ham_runner(ham=am,
                       num_episodes=2,
                       env=environments[0],
                       params=params)
            sum_rew = sum(params.logs["ep_rewards"])

        except ChildProcessError:
            # print(brute_force)
            pass
            # if randrange(1500) == 0:
            #     draw_graph("bf{brute_force}".format(**locals()), am.get_graph_to_draw())

        if sum_rew > 0:
            # TODO
            # with open("out.txt", "a") as f:
            #     f.write(str(brute_force) + "\n")
            # return

            # print("\n\n EPISODE REWARD: ", sum_rew)
            # draw_graph("{sum_rew}__{brute_force}".format(**locals()), am.get_graph_to_draw(action_to_name_mapping=env.get_actions_as_dict()))
            rew = None
            print("\n\n\n")
            for e in environments:
                params = HAMParamsCommon(e)
                ham_runner(ham=am, num_episodes=600, env=e, params=params)
                if rew is None:
                    rew = 0
                rew += sum(params.logs["ep_rewards"])
                print("to_add:", sum(params.logs["ep_rewards"]))
                # except ChildProcessError:
                #     draw_graph("{rew}__{brute_force}".format(**locals()), am.get_graph_to_draw(action_to_name_mapping=env.get_actions_as_dict()))
                #     exit(0)
                # pass
            if rew is not None:
                draw_graph(
                    "{rew}__{brute_force}_{index_}".format(**locals()),
                    am.get_graph_to_draw(
                        action_to_name_mapping=env.get_actions_as_dict()))
Ejemplo n.º 3
0
def generate_machines_by_ids(env, vertexes, ids):
    machines = []
    for max_vertex_count in range(7):
        vc = vertex_combination(vertex_types=vertexes,
                                max_vertex_count=max_vertex_count)
        for index, vertex_types in enumerate(vc):
            for graph_id in ids:
                ms = MachineStored(vertex_types=vertex_types,
                                   binary_matrix_representation=graph_id,
                                   env=env)
                if is_ham_ok(ms.get_machine_without_on_model()):
                    if check_for_one_component_graph(
                            ms.get_machine_without_on_model()):
                        if is_it_machine_runnable(
                                ms.get_machine_without_on_model()):
                            machines.append(ms)
    return machines
    def _step(self, action):
        self.state = self.state + tuple([action])

        self.ham = RootMachine(
            LoopInvokerMachine(
                machine_to_invoke=super_runner(self.machine, self.env)))
        reward = None

        if action is None:
            raise KeyError
        elif action == self.ACTIONS.ACTION_01:
            self.add(Action(action=action))
        elif action == self.ACTIONS.ACTION_02:
            self.add(Action(action=action))
        elif action == self.ACTIONS.ACTION_03:
            self.add(Action(action=action))
        elif action == self.ACTIONS.ACTION_04:
            self.add(Action(action=action))
        elif action == self.ACTIONS.ACTION_05:
            self.add(Action(action=action))
        elif action == self.ACTIONS.ACTION_06:
            self.add(Action(action=action))

        if is_it_machine_runnable(self.machine):
            if self.state in self.dp:
                reward = self.dp[self.state]
            else:
                params = HAMParamsCommon(self.env)
                ham_runner(ham=self.ham,
                           num_episodes=self.num_of_episodes,
                           env=self.env,
                           params=params,
                           no_output=True)
                reward = sum(params.logs["ep_rewards"])
                self.dp[self.state] = reward
            draw_graph(
                "pics/" + str(reward).rjust(10, "0") + str(self.state) + " ",
                self.machine.get_graph_to_draw(
                    action_to_name_mapping=self.env.get_actions_as_dict()))

        observation = self.state
        if len(self.state) >= self.max_size:
            self._done = True

        return observation, reward, self._done, None
Ejemplo n.º 5
0
def generate_good_graphs(env, vertexes, vertex_count):
    good_graphs = []
    vertex_count += 1
    for max_vertex_count in range(vertex_count):
        vc = vertex_combination(vertex_types=vertexes,
                                max_vertex_count=max_vertex_count)
        for index, vertex_types in enumerate(vc):
            for graph_id in sorted(
                    get_graph_id_fast(
                        MachineStored(vertex_types=vertex_types,
                                      binary_matrix_representation=412,
                                      env=env))):
                ms = MachineStored(vertex_types=vertex_types,
                                   binary_matrix_representation=graph_id,
                                   env=env)
                if is_ham_ok(ms.get_machine_without_on_model()):
                    if check_for_one_component_graph(
                            ms.get_machine_without_on_model()):
                        if is_it_machine_runnable(
                                ms.get_machine_without_on_model()):
                            good_graphs.append(ms)
    return good_graphs