def __init__(self, env: MazeWorldEpisodeLength): left4 = Action(action=env.ACTIONS.LEFT) left5 = Action(action=env.ACTIONS.LEFT) up4 = Action(action=env.ACTIONS.UP) up5 = Action(action=env.ACTIONS.UP) choice1 = Choice() choice2 = Choice() left = Action(action=env.ACTIONS.LEFT) right = Action(action=env.ACTIONS.RIGHT) up = Action(action=env.ACTIONS.UP) down = Action(action=env.ACTIONS.DOWN) stop = Stop() transitions = ( MachineRelation(left=Start(), right=choice1), MachineRelation(left=choice1, right=left4), MachineRelation(left=left4, right=left5, label=0), MachineRelation(left=left5, right=choice2, label=0), MachineRelation(left=left4, right=stop, label=1), MachineRelation(left=left5, right=stop, label=1), MachineRelation(left=choice1, right=up4), MachineRelation(left=up4, right=up5, label=0), MachineRelation(left=up5, right=choice2, label=0), MachineRelation(left=up4, right=stop, label=1), MachineRelation(left=up5, right=stop, label=1), MachineRelation(left=choice2, right=left), MachineRelation(left=choice2, right=right), MachineRelation(left=choice2, right=up), MachineRelation(left=choice2, right=down), MachineRelation( left=left, right=stop, label=1, ), MachineRelation(left=right, right=stop, label=1), MachineRelation(left=up, right=stop, label=1), MachineRelation(left=down, right=stop, label=1), MachineRelation( left=left, right=stop, label=0, ), MachineRelation(left=right, right=stop, label=0), MachineRelation(left=up, right=stop, label=0), MachineRelation(left=down, right=stop, label=0), ) super().__init__(graph=MachineGraph(transitions=transitions))
def __init__(self, env: MazeWorldEpisodeLength): stop = Stop() up1 = Action(env.ACTIONS.UP) choice = Choice() transitions = [ MachineRelation(left=Start(), right=up1), MachineRelation(left=up1, right=choice, label=0), MachineRelation(left=choice, right=stop), MachineRelation(left=choice, right=up1), ] transitions = add_action_transitions(transitions) super().__init__(graph=MachineGraph(transitions=transitions))
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")
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) ms = MachineStored(vertex_types=sorted([ Start(), Stop(), Action(env.ACTIONS.LEFT), Action(env.ACTIONS.LEFT), Choice(), Action(env.ACTIONS.RIGHT), Action(env.ACTIONS.TOGGLE), Action(env.ACTIONS.TOGGLE), ]), binary_matrix_representation=42, env=env) ms.draw("a") d = ms.to_dict() ms = MachineStored.from_dict(d, env=env) ms.draw("b")
def super_runner(call_me_maybe, env): start = Start() choice_one = Choice() actions = [Action(action=_) for _ in env.get_actions_as_dict().values()] stop = Stop() call = Call(call_me_maybe) transitions = [ MachineRelation(left=start, right=choice_one), ] for action in actions: transitions.append(MachineRelation(left=choice_one, right=action)) transitions.append(MachineRelation(left=action, right=stop, label=0)) transitions.append(MachineRelation(left=action, right=stop, label=1)) transitions.append(MachineRelation(left=choice_one, right=call)) transitions.append(MachineRelation(left=call, right=stop)) return AbstractMachine(graph=MachineGraph(transitions=transitions))
def from_dict(graph_dict, env): vertex_types = [] for v in graph_dict["vertices"]: if isinstance(v, (list, tuple)): _, action_id = v vertex_types.append(Action(action=action_id)) elif isinstance(v, str): if v == "Choice": vertex_types.append(Choice()) elif v == "Start": vertex_types.append(Start()) elif v == "Stop": vertex_types.append(Stop()) else: raise TypeError else: raise TypeError return MachineStored(vertex_types=vertex_types, binary_matrix_representation=graph_dict["binary_matrix_representation"], env=env)
def __init__(self, env): start = Start() choice_one = Choice() actions = [ Action(action=_) for _ in env.get_actions_as_dict().values() ] stop = Stop() transitions = [ MachineRelation(left=start, right=choice_one), ] for action in actions: transitions.append(MachineRelation(left=choice_one, right=action)) transitions.append( MachineRelation(left=action, right=stop, label=0)) transitions.append( MachineRelation(left=action, right=stop, label=1)) super().__init__(graph=MachineGraph(transitions=transitions))
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) vertex_types = sorted([ Stop(), Start(), Action(env.ACTIONS.LEFT), Action(env.ACTIONS.RIGHT), Action(env.ACTIONS.UP), Action(env.ACTIONS.DOWN), Call(None), Choice(), ]) res = generate_list_of_vertexes(vertex_types=vertex_types, vertex_of_each_type_max_count=3, max_vertex_count=5) for i in res: print(vertex_list_to_str(i))
def main(): def get_on_model(self): return self.get_arm_x(), self.is_cube_graped() def get_all_on_model(self): res = [] for height in range(0, self._size_x - 1): for graped in [True, False]: if height == self._size_x - 1 and graped: continue res.append((height, graped)) return res def get_arm_x(self): return self._arm_x return self._size_x - self._arm_x def is_cube_graped(self): cube_dx, cube_dy = self.MOVE_ACTIONS[self.ACTIONS.DOWN] cube_x, cube_y = self._arm_x + cube_dx, self._arm_y + cube_dy return self._magnet_toggle and self.ok( cube_x, cube_y) and self._grid[cube_x][cube_y] == 1 ArmEnvToggleTopOnly.get_arm_x = get_arm_x ArmEnvToggleTopOnly.get_all_on_model = get_all_on_model ArmEnvToggleTopOnly.is_cube_graped = is_cube_graped ArmEnvToggleTopOnly.get_on_model = get_on_model # env = ArmEnvToggleTopOnly(size_x=5, size_y=4, cubes_cnt=4, episode_max_length=500, finish_reward=100, action_minus_reward=-0.001, tower_target_size=4) env = ArmEnvToggleTopOnly(size_x=4, size_y=3, cubes_cnt=3, episode_max_length=200, finish_reward=100, action_minus_reward=-0.00001, tower_target_size=3) 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(), ]) # part_one(env, vertexes) # part_two(env) # part_three(env) # part_four(env) env = ArmEnvToggleTopOnly(size_x=6, size_y=4, cubes_cnt=5, episode_max_length=800, finish_reward=100, action_minus_reward=-0.00001, tower_target_size=5) # part_six(env) # env = ArmEnvToggleTopOnly(size_x=5, size_y=4, cubes_cnt=4, episode_max_length=500, finish_reward=100, action_minus_reward=-0.001, tower_target_size=4) part_seven(env)
def main(): class UpMachine4(AbstractMachine): def __init__(self, env: ArmEnvToggleTopOnly): d1 = Action(action=env.ACTIONS.UP) d2 = Action(action=env.ACTIONS.UP) d3 = Action(action=env.ACTIONS.UP) d4 = Action(action=env.ACTIONS.UP) stop = Stop() transitions = ( MachineRelation(left=Start(), right=d1), MachineRelation(left=d1, right=d2, label=0), MachineRelation(left=d2, right=d3, label=0), MachineRelation(left=d3, right=d4, label=0), MachineRelation(left=d4, right=stop, label=0), MachineRelation(left=d1, right=stop, label=1), MachineRelation(left=d2, right=stop, label=1), MachineRelation(left=d3, right=stop, label=1), MachineRelation(left=d4, right=stop, label=1), ) super().__init__(graph=MachineGraph(transitions=transitions)) class UpMachine3(AbstractMachine): def __init__(self, env: ArmEnvToggleTopOnly): d1 = Action(action=env.ACTIONS.UP) d2 = Action(action=env.ACTIONS.UP) d3 = Action(action=env.ACTIONS.UP) # d4 = Action(action=env.ACTIONS.UP) stop = Stop() transitions = ( MachineRelation(left=Start(), right=d1), MachineRelation(left=d1, right=d2, label=0), MachineRelation(left=d2, right=d3, label=0), MachineRelation(left=d3, right=stop, label=0), # MachineRelation(left=d4, right=stop, label=0), MachineRelation(left=d1, right=stop, label=1), MachineRelation(left=d2, right=stop, label=1), MachineRelation(left=d3, right=stop, label=1), # MachineRelation(left=d4, right=stop, label=1), ) super().__init__(graph=MachineGraph(transitions=transitions)) a = [ Choice(), Action(ArmEnvToggleTopOnly.ACTIONS.RIGHT), Action(ArmEnvToggleTopOnly.ACTIONS.LEFT), Action(ArmEnvToggleTopOnly.ACTIONS.DOWN), # Action(ArmEnvToggleTopOnly.ACTIONS.UP), Call(machine_to_call=UpMachine4(environments[1])), ] transitions = [] for i in a: for j in a: if randrange(2): if isinstance(i, Action): transitions.append( MachineRelation(left=i, right=j, label=0)) else: transitions.append(MachineRelation(left=i, right=j)) # len_ = len(goodhams) # print(len_) # len_4 = len_ // 4 + 1 # l1, r1 = 0, len_4 # l2, r2 = len_4, 2 * len_4 # l3, r3 = 2 * len_4, 3 * len_4 # l4, r4 = 3 * len_4, 4 * len_4 # print(l1, r1 ) # print(l2, r2 ) # print(l3, r3 ) # print(l4, r4 ) # exit(0) # for brute_force in goodhams: # for index, brute_force in enumerate(goodhams[l1: r1]): # for index, brute_force in enumerate(goodhams[l2: r2]): # for index, brute_force in enumerate(goodhams[l3: r3]): brute_force = 1180698 # if bin(brute_force).count("1") > 12 or bin(brute_force).count("1") < 4: # continue # continue go_continue = False transitions = [] ss = set() for ii in range(len(a)): for jj in range(len(a)): i = a[ii] j = a[jj] if (2**(ii * len(a) + jj)) & brute_force: if isinstance(i, Action): transitions.append( MachineRelation(left=i, right=j, label=0)) else: transitions.append(MachineRelation(left=i, right=j)) if ii in ss and isinstance(a[ii], (Action, Call)): go_continue = True break ss.add(ii) stop = Stop() for ii in range(len(a)): if ii not in ss: i = a[ii] if isinstance(i, Action): transitions.append(MachineRelation(left=i, right=stop, label=0)) else: transitions.append(MachineRelation(left=i, right=stop)) for i in a: if isinstance(i, Action): transitions.append(MachineRelation(left=i, right=stop, label=1)) transitions.append(MachineRelation(left=Start(), right=a[0])) machine = AbstractMachine(MachineGraph(transitions=transitions)) am = RootMachine(LoopInvokerMachine(machine)) env = environments[0] draw_graph( "{brute_force}".format(**locals()), am.get_graph_to_draw(action_to_name_mapping=env.get_actions_as_dict())) name = "02_auto" def run(global_env): full_name = name params = HAMParamsCommon(environments[0]) ham_runner(ham=am, num_episodes=global_episodes, env=env, params=params) rewards = params.logs["ep_rewards"] # with open(full_name + " cumulative_reward.txt", "w") as w: # for out in get_cumulative_rewards(rewards=rewards): # w.write(str(out) + '\n', ) with open(full_name + " reward.txt", "w") as w: for out in rewards: w.write(str(out) + '\n', ) def main(): # for global_env in EnvironmentsArticle().environments: run(EnvironmentsArticle().environments[0]) if __name__ == '__main__': main()
def __init__(self, env): pull_up_start = Start() pull_up_on = Action(action=env.get_actions_as_dict()["ON"]) pull_up_down_01 = Action(action=env.get_actions_as_dict()["DOWN"]) pull_up_down_02 = Action(action=env.get_actions_as_dict()["DOWN"]) pull_up_down_03 = Action(action=env.get_actions_as_dict()["DOWN"]) pull_up_down_04 = Action(action=env.get_actions_as_dict()["DOWN"]) pull_up_up_01 = Action(action=env.get_actions_as_dict()["UP"]) pull_up_up_02 = Action(action=env.get_actions_as_dict()["UP"]) pull_up_up_03 = Action(action=env.get_actions_as_dict()["UP"]) pull_up_up_04 = Action(action=env.get_actions_as_dict()["UP"]) pull_up_stop = Stop() pull_up_transitions = ( MachineRelation(left=pull_up_start, right=pull_up_on), MachineRelation(left=pull_up_on, right=pull_up_down_01, label=0), MachineRelation(left=pull_up_down_01, right=pull_up_down_02, label=0), MachineRelation(left=pull_up_down_02, right=pull_up_down_03, label=0), MachineRelation(left=pull_up_down_03, right=pull_up_down_04, label=0), MachineRelation(left=pull_up_down_04, right=pull_up_up_01, label=0), MachineRelation(left=pull_up_up_01, right=pull_up_up_02, label=0), MachineRelation(left=pull_up_up_02, right=pull_up_up_03, label=0), MachineRelation(left=pull_up_up_03, right=pull_up_up_04, label=0), MachineRelation(left=pull_up_up_04, right=pull_up_stop, label=0), MachineRelation(left=pull_up_on, right=pull_up_stop, label=1), MachineRelation(left=pull_up_down_01, right=pull_up_stop, label=1), MachineRelation(left=pull_up_down_02, right=pull_up_stop, label=1), MachineRelation(left=pull_up_down_03, right=pull_up_stop, label=1), MachineRelation(left=pull_up_down_04, right=pull_up_stop, label=1), MachineRelation(left=pull_up_up_01, right=pull_up_stop, label=1), MachineRelation(left=pull_up_up_02, right=pull_up_stop, label=1), MachineRelation(left=pull_up_up_03, right=pull_up_stop, label=1), MachineRelation(left=pull_up_up_04, right=pull_up_stop, label=1), ) pull_up = AbstractMachine( MachineGraph(transitions=pull_up_transitions)) start = Start() choice_one = Choice() left = Action(action=env.get_actions_as_dict()["LEFT"]) right = Action(action=env.get_actions_as_dict()["RIGHT"]) off = Action(action=env.get_actions_as_dict()["OFF"]) call = Call(machine_to_call=pull_up) stop = Stop() transitions = ( MachineRelation(left=start, right=choice_one), MachineRelation(left=choice_one, right=left), MachineRelation(left=choice_one, right=right), MachineRelation(left=choice_one, right=off), MachineRelation(left=choice_one, right=call), MachineRelation(left=call, right=stop), MachineRelation(left=left, right=stop, label=0), MachineRelation(left=right, right=stop, label=0), MachineRelation(left=off, right=stop, label=0), MachineRelation(left=left, right=stop, label=1), MachineRelation(left=right, right=stop, label=1), MachineRelation(left=off, right=stop, label=1), ) super().__init__(graph=MachineGraph(transitions=transitions))
MachineRelation(left=pull_up_up_03, right=pull_up_up_04, label=0), MachineRelation(left=pull_up_up_04, right=pull_up_stop, label=0), MachineRelation(left=pull_up_on, right=pull_up_stop, label=1), MachineRelation(left=pull_up_down_01, right=pull_up_stop, label=1), MachineRelation(left=pull_up_down_02, right=pull_up_stop, label=1), MachineRelation(left=pull_up_down_03, right=pull_up_stop, label=1), MachineRelation(left=pull_up_down_04, right=pull_up_stop, label=1), MachineRelation(left=pull_up_up_01, right=pull_up_stop, label=1), MachineRelation(left=pull_up_up_02, right=pull_up_stop, label=1), MachineRelation(left=pull_up_up_03, right=pull_up_stop, label=1), MachineRelation(left=pull_up_up_04, right=pull_up_stop, label=1), ) pull_up = AbstractMachine(MachineGraph(transitions=pull_up_transitions)) start = Start() choice_one = Choice() left = Action(action=env.get_actions_as_dict()["LEFT"]) right = Action(action=env.get_actions_as_dict()["RIGHT"]) off = Action(action=env.get_actions_as_dict()["OFF"]) call = Call(machine_to_call=pull_up) stop = Stop() transitions = ( MachineRelation(left=start, right=choice_one), MachineRelation(left=choice_one, right=left), MachineRelation(left=choice_one, right=right), MachineRelation(left=choice_one, right=off), MachineRelation(left=choice_one, right=call), MachineRelation(left=call, right=stop),
def main(): class UpMachine4(AbstractMachine): def __init__(self, env: ArmEnvToggleTopOnly): d1 = Action(action=env.ACTIONS.UP) d2 = Action(action=env.ACTIONS.UP) d3 = Action(action=env.ACTIONS.UP) d4 = Action(action=env.ACTIONS.UP) stop = Stop() transitions = ( MachineRelation(left=Start(), right=d1), MachineRelation(left=d1, right=d2, label=0), MachineRelation(left=d2, right=d3, label=0), MachineRelation(left=d3, right=d4, label=0), MachineRelation(left=d4, right=stop, label=0), MachineRelation(left=d1, right=stop, label=1), MachineRelation(left=d2, right=stop, label=1), MachineRelation(left=d3, right=stop, label=1), MachineRelation(left=d4, right=stop, label=1), ) super().__init__(graph=MachineGraph(transitions=transitions)) class UpMachine3(AbstractMachine): def __init__(self, env: ArmEnvToggleTopOnly): d1 = Action(action=env.ACTIONS.UP) d2 = Action(action=env.ACTIONS.UP) d3 = Action(action=env.ACTIONS.UP) # d4 = Action(action=env.ACTIONS.UP) stop = Stop() transitions = ( MachineRelation(left=Start(), right=d1), MachineRelation(left=d1, right=d2, label=0), MachineRelation(left=d2, right=d3, label=0), MachineRelation(left=d3, right=stop, label=0), # MachineRelation(left=d4, right=stop, label=0), MachineRelation(left=d1, right=stop, label=1), MachineRelation(left=d2, right=stop, label=1), MachineRelation(left=d3, right=stop, label=1), # MachineRelation(left=d4, right=stop, label=1), ) super().__init__(graph=MachineGraph(transitions=transitions)) a = [ Choice(), Action(ArmEnvToggleTopOnly.ACTIONS.RIGHT), Action(ArmEnvToggleTopOnly.ACTIONS.LEFT), Action(ArmEnvToggleTopOnly.ACTIONS.DOWN), # Action(ArmEnvToggleTopOnly.ACTIONS.UP), Call(machine_to_call=UpMachine4(environments[1])), ] transitions = [] for i in a: for j in a: if randrange(2): if isinstance(i, Action): transitions.append( MachineRelation(left=i, right=j, label=0)) else: transitions.append(MachineRelation(left=i, right=j)) len_ = len(goodhams) print(len_) len_4 = len_ // 4 + 1 l1, r1 = 0, len_4 l2, r2 = len_4, 2 * len_4 l3, r3 = 2 * len_4, 3 * len_4 l4, r4 = 3 * len_4, 4 * len_4 # print(l1, r1 ) # print(l2, r2 ) # print(l3, r3 ) # print(l4, r4 ) # exit(0) # for brute_force in goodhams: # for index, brute_force in enumerate(goodhams[l1: r1]): # for index, brute_force in enumerate(goodhams[l2: r2]): # for index, brute_force in enumerate(goodhams[l3: r3]): for index, brute_force in enumerate(goodhams[l4:r4]): if index >= len_: break if index % (len_ // 100) == 0: print(index // (len_ // 100), "%") if bin(brute_force).count("1") > 10: continue if bin(brute_force).count("1") < 4: continue # if bin(brute_force).count("1") > 12 or bin(brute_force).count("1") < 4: # continue # continue go_continue = False transitions = [] ss = set() for ii in range(len(a)): for jj in range(len(a)): i = a[ii] j = a[jj] if (2**(ii * len(a) + jj)) & brute_force: if isinstance(i, Action): transitions.append( MachineRelation(left=i, right=j, label=0)) else: transitions.append(MachineRelation(left=i, right=j)) if ii in ss and isinstance(a[ii], (Action, Call)): go_continue = True break ss.add(ii) if go_continue: # print('continue') continue stop = Stop() for ii in range(len(a)): if ii not in ss: i = a[ii] if isinstance(i, Action): transitions.append( MachineRelation(left=i, right=stop, label=0)) else: transitions.append(MachineRelation(left=i, right=stop)) for i in a: if isinstance(i, Action): transitions.append(MachineRelation(left=i, right=stop, label=1)) for index_, II in enumerate(a): transitions.append(MachineRelation(left=Start(), right=II)) go(transitions=transitions, brute_force=brute_force, index_=index_) transitions.pop()