def run_simulation(sim_vals) -> float: """ Run caterpillar with a policy given in argument. This function is for multiprocessing. sim_vals: ( steps: int, actor_module_name: str, actor_params: [np.array(params_0), np.array(params_1), ...], disable_list: list ) """ steps, actor_module_name, actor_params, disable_list = sim_vals assert isinstance(steps, int) assert isinstance(actor_params, Iterable) assert isinstance(disable_list, Iterable) # Init actor actor_module = import_module(actor_module_name) actor = getattr(actor_module, config.COMMON_ACTOR_NAME)() actor.set_params(actor_params) # Init caterpillar caterpillar = Caterpillar(config.somites, mode="default") # Run steps (accumulated_tension,) = exec_steps(steps, actor, caterpillar, disable_list=disable_list) reward = caterpillar.moved_distance() - accumulated_tension / config.params["tension_divisor"] return reward
def exec_steps(steps: int, actor: base_actor.BaseActor, caterpillar: Caterpillar, disable_list: list, episode=None) -> float: """ Run caterpillar for designated steps. return run information which is (accumulated tensions,) """ accumulated_tension = 0 for step in range(steps): (_, _, tensions), _ = caterpillar_runner.observe_and_act(actor, caterpillar, disable_list, episode=episode) accumulated_tension += np.sum(np.power(tensions, 2)) caterpillar.step(step, int(1 / config.params["time_dalte"] / 10)) return (accumulated_tension,)
def run_caterpillar(actor, save_dir: str, steps: int, disable_list=None, broken_value=0): if disable_list is None: disable_list = [] caterpillar = Caterpillar(config.somites, mode="default") # Record during sampling sim_distance_file = DataCSVSaver(os.path.join(save_dir, "distance.txt"), ("step", "distance")) sim_phase_diffs_file = DataCSVSaver( os.path.join(save_dir, "phase_diffs.txt"), ["step"] + ["phi_{}".format(i) for i in range(config.oscillators)]) sim_phases_file = DataCSVSaver( os.path.join(save_dir, "phases.txt"), ["step"] + ["phi_{}".format(i) for i in range(config.oscillators)]) sim_actions_file = DataCSVSaver( os.path.join(save_dir, "actions.txt"), ["step"] + ["action_{}".format(i) for i in range(config.oscillators)]) sim_frictions_file = DataCSVSaver( os.path.join(save_dir, "frictions.txt"), ["step"] + ["friction_{}".format(i) for i in range(config.somites)]) sim_tension_file = DataCSVSaver( os.path.join(save_dir, "tensions.txt"), ["step"] + ["tension_{}".format(i) for i in range(config.oscillators)]) for step in range(steps): obv, action = observe_and_act(actor, caterpillar, disable_list=disable_list, broken_value=broken_value) _, frictions, tensions = obv caterpillar.step(step, int(1 / config.params["time_dalte"] / 10)) # Save data sim_distance_file.append_data(step, caterpillar.moved_distance()) sim_phase_diffs_file.append_data(step, *caterpillar.phases_from_base()) sim_phases_file.append_data(step, *caterpillar.phis) sim_actions_file.append_data(step, *action) sim_frictions_file.append_data(step, *frictions) sim_tension_file.append_data(step, *tensions) caterpillar.save_simulation("{}/render.sim".format(save_dir)) return caterpillar.moved_distance()
def train_caterpillar(save_dir: utils.SaveDir, actor_module_name: str): # Dump train parameters config.print_config() actor_module = import_module(actor_module_name) actor_class = getattr(actor_module, config.COMMON_ACTOR_NAME) rlm = PEPGActorManager(actor_class) caterpillar = Caterpillar(config.somites, mode="default") config.dump_config(save_dir.log_dir()) distance_log = DataCSVSaver(os.path.join(save_dir.log_dir(), "distance.txt"), ("episode", "distance")) reward_log = DataCSVSaver(os.path.join(save_dir.log_dir(), "reward.txt"), ("episode", "reward")) sigma_log = DataCSVSaver(os.path.join(save_dir.log_dir(), "sigma.txt"), ("episode", "average sigma")) episode = 0 while episode < config.params["episodes"]: params_sets = rlm.sample_params() print("\nEpisode: {}".format(episode)) print("---------------------------------------------------------------------") rewards = [] try: with Pool(processes=config.exec_params["worker_processes"], initializer=mute) as pool: rewards = pool.map(run_simulation, [(config.params["steps"], actor_module_name, p_set, []) for p_set in params_sets]) rlm.update_params(np.array(rewards)) episode += 1 # Try parameters after this episode -------------------------------------- rlm.set_params(rlm.parameters) caterpillar.reset() (accumulated_tension,) = exec_steps(config.params["steps"], rlm.get_actor(), caterpillar, [], episode=episode - 1) reward = caterpillar.moved_distance() - accumulated_tension / config.params["tension_divisor"] # Save parameter performance distance_log.append_data(episode, caterpillar.moved_distance()) sigma_log.append_data(episode, np.mean(rlm.sigmas)) reward_log.append_data(episode, reward) announce = " --- Distance: {} Reward: {}".format(caterpillar.moved_distance(), reward) print(announce) except KeyboardInterrupt: command = input("\nSample? Finish? : ") if command in ["sample", "Sample"]: rlm.set_params(rlm.parameters) test_current_params(rlm.get_actor(), save_dir.log_dir(), episode) continue if command in ["finish", "Finish"]: print("Ending training ...") break rlm.set_params(rlm.parameters) rlm.save(save_file_path=os.path.join(save_dir.model_dir(), 'actor_model.pickle')) return rlm.get_actor()
class InputCaterpillar(BoxLayout): def __init__(self,**kwargs): self.spacing=5 self.register_event_type('on_input') super(InputCaterpillar, self).__init__(**kwargs) self.orientation="vertical" self.chain_widget=Caterpillar() self.func=None buttons=BoxLayout(orientation="horizontal") for (k,v) in sorted(Caterpillar._colors.items()): b=Button() b.num=k b.background_color=[c*3 for c in list(v)]+[1] b.bind(on_press=self.color_press) buttons.add_widget(b) b=Button(text="<=") b.bind(on_press=self.backspace) buttons.add_widget(b) b=Button(text=u"OK") b.bind(on_press=self.enter) buttons.add_widget(b) self.add_widget(self.chain_widget) self.add_widget(buttons) def on_input(self,chain): pass def set_chain(self,chain): # if caterpillar is valid it looks to the left, otherwise to the right eyes='right' eyes='left' if chain and self.func and self.func(chain) else 'right' self.chain_widget.set_chain(chain,eye_direction=eyes) def backspace(self,instance): chain=self.chain_widget.get_chain()[:-1] self.set_chain(chain) def color_press(self,instance): chain=self.chain_widget.get_chain() if len(chain)==7: return chain=chain+[instance.num] self.set_chain(chain) def enter(self,instance): if self.chain_widget.get_chain()==[]:return self.dispatch('on_input',tuple(self.chain_widget.get_chain())) self.chain_widget.set_chain([])
def test_current_params(actor: base_actor, log_dir: str, episode: int): steps = input("How many steps for this sample?: ") if steps == "": utils.notice("default steps {}".format(config.params["default_sample_steps"])) steps = config.params["default_sample_steps"] # Record during sampling sim_distance_file = DataCSVSaver( "{}/train_result_ep{}_distance.txt".format(log_dir, episode), ("step", "distance") ) sim_phase_diffs_file = DataCSVSaver( "{}/train_result_ep{}_phase_diffs.txt".format(log_dir, episode), ["step"] + ["phi_{}".format(i) for i in range(config.oscillators)] ) sim_actions_file = DataCSVSaver( "{}/train_result_ep{}_actions.txt".format(log_dir, episode), ["step"] + ["action_{}".format(i) for i in range(config.oscillators)] ) sim_frictions_file = DataCSVSaver( "{}/train_result_ep{}_frictions.txt".format(log_dir, episode), ["step"] + ["friction_{}".format(i) for i in range(config.somites)] ) caterpillar = Caterpillar(config.somites, mode="default") for step in range(int(steps)): try: _, action = caterpillar_runner.observe_and_act(actor, caterpillar, []) caterpillar.step(step, int(1 / config.params["time_dalte"] / 10)) except Exception: continue else: # Save data sim_distance_file.append_data(step, caterpillar.moved_distance()) sim_phase_diffs_file.append_data(step, *caterpillar.phases_from_base()) sim_actions_file.append_data(step, *action) frictions = caterpillar.frictions() sim_frictions_file.append_data(step, *frictions) print("Moved distance:", caterpillar.moved_distance()) caterpillar.save_simulation("{}/train_result_ep{}.sim".format(log_dir, episode))
def draw(self, *args): self.box.clear_widgets() x, y = self.size if x > y: cols = 2 else: cols = 1 self.box.cols = cols for guess in self.caterpillars: self.box.add_widget( Caterpillar(chain=guess, size_hint_y=None, size=(x, x / 7 / cols))) self.scroll_y = 1
def __init__(self, **kwargs): self.register_event_type('on_input') super(InputCaterpillar, self).__init__(**kwargs) self.orientation = "vertical" self.chain_widget = Caterpillar() self.func = None buttons = BoxLayout(orientation="horizontal") for (k, v) in sorted(Caterpillar._colors.items()): b = Button() b.num = k b.background_color = list(v) + [1] b.bind(on_press=self.color_press) buttons.add_widget(b) b = Button(text="<=") b.bind(on_press=self.backspace) buttons.add_widget(b) b = Button(text=u"OK") b.bind(on_press=self.enter) buttons.add_widget(b) self.add_widget(self.chain_widget) self.add_widget(buttons)
def observe_and_act(actor: base_actor.BaseActor, caterpillar: Caterpillar, disable_list: list, broken_value=0, episode=None): assert np.all(np.array(disable_list) < config.somites) if disable_list is None: disable_list = [] mask = np.ones(config.somites) mask[disable_list] = 0 bias = np.zeros(config.somites) bias[disable_list] = broken_value frictions = caterpillar.frictions() phis = caterpillar.phis tensions = caterpillar.tensions() state = np.concatenate([frictions * mask + bias, phis, tensions]) action = actor.get_action(state) caterpillar.feedback_phis(action) observation = (phis, frictions, tensions) return observation, action
class InputCaterpillar(BoxLayout): def __init__(self, **kwargs): self.register_event_type('on_input') super(InputCaterpillar, self).__init__(**kwargs) self.orientation = "vertical" self.chain_widget = Caterpillar() self.func = None buttons = BoxLayout(orientation="horizontal") for (k, v) in sorted(Caterpillar._colors.items()): b = Button() b.num = k b.background_color = list(v) + [1] b.bind(on_press=self.color_press) buttons.add_widget(b) b = Button(text="<=") b.bind(on_press=self.backspace) buttons.add_widget(b) b = Button(text=u"OK") b.bind(on_press=self.enter) buttons.add_widget(b) self.add_widget(self.chain_widget) self.add_widget(buttons) def on_input(self, chain): pass def set_chain(self, chain): # if caterpillar is valid it looks to the left, otherwise to the right eyes = 'right' eyes = 'left' if chain and self.func and self.func(chain) else 'right' self.chain_widget.set_chain(chain, eye_direction=eyes) def backspace(self, instance): chain = self.chain_widget.get_chain()[:-1] self.set_chain(chain) def color_press(self, instance): chain = self.chain_widget.get_chain() if len(chain) == 7: return chain = chain + [instance.num] self.set_chain(chain) def enter(self, instance): if self.chain_widget.get_chain() == []: return self.dispatch('on_input', tuple(self.chain_widget.get_chain())) self.chain_widget.set_chain([])
def __init__(self,**kwargs): self.register_event_type('on_finish') self.register_event_type('on_answer') super(Exam, self).__init__(**kwargs) self.orientation="vertical" self.question_label=Label() self.chain_widget=Caterpillar() buttons=BoxLayout(orientation="horizontal") b=Button(text="Valid") b.background_color=(0,1,0,1) b.val=True b.bind(on_press=self.answer) buttons.add_widget(b) b=Button(text="Invalid") b.background_color=(1,0,0,1) b.val=False b.bind(on_press=self.answer) buttons.add_widget(b) self.add_widget(self.question_label) self.add_widget(self.chain_widget) self.add_widget(buttons)
def __init__(self,**kwargs): self.register_event_type('on_input') super(InputCaterpillar, self).__init__(**kwargs) self.orientation="vertical" self.chain_widget=Caterpillar() self.func=None buttons=BoxLayout(orientation="horizontal") for (k,v) in sorted(Caterpillar._colors.items()): b=Button() b.num=k b.background_color=list(v)+[1] b.bind(on_press=self.color_press) buttons.add_widget(b) b=Button(text="<=") b.bind(on_press=self.backspace) buttons.add_widget(b) b=Button(text=u"OK") b.bind(on_press=self.enter) buttons.add_widget(b) self.add_widget(self.chain_widget) self.add_widget(buttons)
class Exam(BoxLayout): def __init__(self,**kwargs): self.register_event_type('on_finish') self.register_event_type('on_answer') super(Exam, self).__init__(**kwargs) self.orientation="vertical" self.question_label=Label() self.chain_widget=Caterpillar() buttons=BoxLayout(orientation="horizontal") b=Button(text="Valid") b.background_color=(0,1,0,1) b.val=True b.bind(on_press=self.answer) buttons.add_widget(b) b=Button(text="Invalid") b.background_color=(1,0,0,1) b.val=False b.bind(on_press=self.answer) buttons.add_widget(b) self.add_widget(self.question_label) self.add_widget(self.chain_widget) self.add_widget(buttons) def start_exam(self,valid,invalid,valid_history,invalid_history): self.questions=self.make_questions(valid,invalid,valid_history,invalid_history) self.question_num=0 self.ask_question() def make_questions(self,valid,invalid,valid_history,invalid_history): valid_num=random.randint(5,10) invalid_num=15-valid_num valids=[(x,True) for x in get_n(valid_num,valid,valid_history)] invalids=[(x,False) for x in get_n(invalid_num,invalid,invalid_history)] l=valids+invalids random.shuffle(l) return l def ask_question(self): if self.question_num==len(self.questions): self.dispatch("on_finish",True) else: self.question_label.text="Question %s/%s"%(self.question_num+1,len(self.questions)) self.chain_widget.set_chain(self.questions[self.question_num][0]) def answer(self,instance): self.dispatch("on_answer",self.questions[self.question_num][0]) if self.questions[self.question_num][1]==instance.val: self.question_num+=1 self.ask_question() else: self.dispatch("on_finish",False) def on_finish(self,*args): pass def on_answer(self,*args): pass
def fight(): fight = True #PROBABLY CHANGE opponent = PaintbrushTown.challenger() if opponent == 'Rocky': enemy = 'bat' elif opponent == 'Slander': enemy = 'rat' elif opponent == 'Kyle': enemy = 'caterpillar' print(opponent,'chooses', enemy,'\b!') if(enemy == 'bat'): combatant = Bat() Bat.work() elif(enemy == 'caterpillar'): combatant = Caterpillar() Caterpillar.work() elif(enemy == 'rat'): combatant = Rat() Rat.work() time.sleep(1) while(True): start,attack, = '0','0' while start != 'k' and start != 'b' and start != 'm': start = input('Which bokémon would you like to start with?\n[k = Kennen, b = Belldolpine, m = Meister Beast]\n').lower() while(fight == True): #PLAYER TURN if start == 'k': print('You have chosen Kennen!') you = 'Kennen' pc = Kennen() elif start == 'b': print('You have chosen Belldolpine!') you = 'Belldolpine' pc = Belldolpine() elif start == 'm': print('You have chosen Meister Beast!') you = 'MeisterBeast' pc = MeisterBeast() while attack != 'b' and attack != 's': attack = input('Would you like to use your basic attack [b] or your special attack [s]\n') if attack == 'b': print(you,'does', pc.basicDamage, 'damage to the', enemy) combatant.takeDamage(pc.basicDamage) attack = '0' break if attack == 's': print(you, 'does', pc.specialDamage, 'damage to the', enemy) combatant.takeDamage(pc.specialDamage) attack = '0' break #ENEMY TURN if(combatant.hp > 0): time.sleep(1) print('The', enemy, 'attacks!') combatant.basicAttack() print('The', enemy, 'does', combatant.basicAttack(), 'damage to you!') pc.takeDamage(combatant.damage) else: time.sleep(1) print('The', enemy, 'is knocked unconscious!\nYou have won!\n',opponent,'congradulates you and hands you a badge.') time.sleep(1) print('You should take the badge back to Professor Cottonwood so you can go find your parents.') time.sleep(5) pc.heal() fight = False break