def get_fitness(string): """ Run the simulation and return the fitness """ state_machine = sm.StateMachine() behavior_tree = PyTree(string[:], behaviors=behaviors, world_interface=state_machine) # run the Behavior Tree behavior_tree.run_bt() return fitness_function.compute_fitness(state_machine, behavior_tree)
def get_fitness(self, individual, save_video=False): """ Run the simulation and return the fitness In case of error, restarts world_interface and tries again. """ pytree = PyTree(individual[:], behaviors=behaviors, world_interface=self.world_interface, verbose=self.verbose) status_ok = True fitness = None while fitness is None: if self.counter >= self.maximum_runs or not status_ok: self.world_interface.restart() self.counter = 0 else: self.world_interface.reset() self.counter += 1 if save_video: self.world_interface.start_video() ticks, status_ok = pytree.run_bt(max_ticks=200) count = 0 while count < 50 and status_ok: #Wait up to five seconds for everything to come to a resting state old_sensor_data = self.world_interface.get_sensor_data() status_ok = self.world_interface.get_feedback() if status_ok: if self.world_interface.at_standstill(old_sensor_data): break self.world_interface.send_references() count += 1 if save_video: time.sleep(1) #Needed for ros synch self.world_interface.stop_video() if status_ok: fitness = fitness_function.compute_fitness(self.world_interface, pytree, ticks, \ self.targets, self.fitness_coeff, self.verbose) else: self.world_interface.restart() self.counter = 0 print("Failed:", individual) fitness = -9999999 return fitness
def plan(world_interface, behaviors, goals): """ Main planner function Generates a behaviors tree to solve task given a set of goals and behaviors with preconditions and postconditions. Since the conditions are not always static, it actually runs the tree while evaluating the conditions. """ tree = RSequence() for goal in goals: goal_condition, _ = behaviors.get_node_from_string( goal, world_interface, []) tree.add_child(goal_condition) print(pt.display.unicode_tree(root=tree, show_status=True)) for i in range(60): tree.tick_once() print("Tick: ", i) print(pt.display.unicode_tree(root=tree, show_status=True)) if tree.status is pt.common.Status.FAILURE: expand_tree(tree, behaviors, world_interface) print(pt.display.unicode_tree(root=tree, show_status=True)) elif tree.status is pt.common.Status.SUCCESS: break pt.display.render_dot_tree(tree, name='Planned bt', target_directory='') print(PyTree('', behaviors, world_interface, tree).get_bt_from_root())
def get_fitness(self, individual): """ Run the simulation and return the fitness """ state_machine = sm.StateMachine(self.start_positions, self.random_events, self.sm_pars, self.mode) pytree = PyTree(individual[:], behaviors=behaviors, world_interface=state_machine, verbose=self.verbose) # run the Behavior Tree ticks, _ = pytree.run_bt() return fitness_function.compute_fitness(state_machine, pytree, ticks, self.targets, self.fitness_coeff)
def plot_individual(self, path, plot_name, individual): """ Saves a graphical representation of the individual """ if self.static_tree is not None: pytree = PyTree(self.add_to_static_tree(individual), behaviors=behaviors) else: pytree = PyTree(individual[:], behaviors=behaviors) pytree.save_fig(path, name=plot_name)
def plot_individual(path, plot_name, individual): """ Saves a graphical representation of the individual """ pytree = PyTree(individual[:], behaviors=behaviors) pytree.save_fig(path, name=plot_name)
def paper_figures(): # pylint: disable=import-outside-toplevel """ Creates the bt figures used in the paper """ from behavior_tree_learning.py_trees_interface import PyTree import behavior_tree_learning.behaviors_figures as behaviors behavior_tree.load_settings_from_file('BT_SETTINGS_TOWER.yaml') # Tower planned tree = ['s(', 'f(', 'Green at A?', 's(', 'f(', 'picked Green?', 'pick Green!', ')', 'place at A!', ')', ')', \ 'f(', 'Blue at pos B?', 's(', 'f(', 'Blue on Green?', 's(', 'f(', 'picked Blue?', 'pick Blue!', ')', 'place on Green!', ')', ')', 'apply force Blue!', ')', ')', \ 'f(', 'Red at pos C?', 's(', 'f(', 'Red on Blue?', 's(', 'f(', 'picked Red?', 'pick Red!', ')', 'place on Blue!', ')', ')', 'apply force Red!', ')', ')', ')'] pytree = PyTree(tree[:], behaviors=behaviors) pytree.save_fig('logs/paperfigures/', name='tower_planned', svg=True) # Tower solved tree = ['s(', 'f(', 'Green at pos A?', 's(', 'pick Green!', 'place at A!', ')', ')', \ 'f(', 'Blue at pos B?', 's(', 'f(', 'Blue on Green?', 's(', 'pick Blue!', 'place on Green!', ')', ')', 'apply force Blue!', ')', ')', \ 'f(', 'Red at pos C?', 's(', 'f(', 'Red on Blue?', 's(', 'pick Red!', 'place on Blue!', ')', ')', 'apply force Red!', ')', ')', ')'] pytree = PyTree(tree[:], behaviors=behaviors) pytree.save_fig('logs/paperfigures/', name='tower_solved', svg=True) behavior_tree.load_settings_from_file('BT_SETTINGS_CROISSANT.yaml') # Croissant planned tree = ['s(', 'f(', 'Green at pos B?', 's(', 'f(', 'picked Green?', 'pick Green!', ')', 'place at B!', ')', ')', \ 'f(', 'Blue at pos D?', 's(', 'f(', 'Blue on Green?', 's(', 'f(', 'picked Blue?', 'pick Blue!', ')', 'place on Green!', ')', ')', 'apply force Blue!', ')', ')', \ 'f(', 'Red at pos A?', 's(', 'f(', 'picked Red?', 'pick Red!', ')', 'place at A!', ')', ')', \ 'f(', 'Yellow at pos C?', 's(', 'f(', 'picked Yellow?', 'pick Yellow!', ')', 'place at C!', ')', ')', ')'] pytree = PyTree(tree[:], behaviors=behaviors) pytree.save_fig('logs/paperfigures/', name='croissant_planned', svg=True) # Croissant solved tree = ['s(', 'f(', 'Green at pos B?', 's(', 'pick Green!', 'place at B!', ')', ')', \ 'f(', 'Red at pos A?', 's(', 'pick Red!', 'place at A!', ')', ')', \ 'f(', 'Yellow at pos C?', 's(', 'pick Yellow!', 'place at C!', ')', ')', \ 'f(', 'Blue at pos D?', 's(', 'f(', 'Blue on Green?', 's(', 'pick Blue!', 'place on Green!', ')', ')', 'apply force Blue!', ')', ')', ')'] pytree = PyTree(tree[:], behaviors=behaviors) pytree.save_fig('logs/paperfigures/', name='croissant_solved', svg=True) behavior_tree.load_settings_from_file('BT_SETTINGS_BALANCE.yaml') # Balance planned tree = [ 's(', 'f(', 'Green at pos B?', 's(', 'f(', 'Blue at pos A?', 'put Blue at A!', ')', 'f(', 'Green on Blue?', 'put Green on Blue!', ')', 'apply force Green!', ')', ')', ')' ] pytree = PyTree(tree[:], behaviors=behaviors) pytree.save_fig('logs/paperfigures/', name='balance_planned', svg=True) # Balance solved tree = [ 'f(', 'Green at pos B?', 's(', 'put Red at A!', 'put Green on Red!', 'apply force Green!', ')', ')' ] pytree = PyTree(tree[:], behaviors=behaviors) pytree.save_fig('logs/paperfigures/', name='balance_solved', svg=True) behavior_tree.load_settings_from_file('BT_SETTINGS_BLOCKING.yaml') # Blocking planned tree = ['s(', 'f(', 'Green at A?', 'put Green at A!', ')', \ 'f(', 'Blue at E?', 's(', 'f(', 'Blue on Green?', 'put Blue on Green!', ')', 'apply force Blue!', ')', ')', \ 'f(', 'Red at C?', 'put Red at C!', ')', ')'] pytree = PyTree(tree[:], behaviors=behaviors) pytree.save_fig('logs/paperfigures/', name='blocking_planned', svg=True) # Blocking solved tree = ['s(', 'put Blue on Green!', 'f(', 'Green at A?', 'put Red at D!', ')', \ 'put Green at A!', \ 'f(', 'Blue at E?', 'apply force Blue!', ')', \ 'put Red at C!', ')'] pytree = PyTree(tree[:], behaviors=behaviors) pytree.save_fig('logs/paperfigures/', name='blocking_solved', svg=True)