def test_remote_goal(self): method_prefix = self.__message_prefix + "TestRemoteGoal" planner_prefix = method_prefix + "Manager" m = Manager(activationThreshold=7, prefix=planner_prefix) topic_name = method_prefix + '/Topic' sensor = SimpleTopicSensor(topic=topic_name, message_type=Bool, initial_value=False) condition = Condition(sensor, BooleanActivator()) pddl_function_name = condition.getFunctionNames()[0] SetTrueBehavior(effect_name=pddl_function_name, topic_name=topic_name, name=method_prefix + "SetTrue", plannerPrefix=planner_prefix) goal = GoalBase(method_prefix + 'CentralGoal', plannerPrefix=planner_prefix) goal.addCondition(condition) for x in range(0, 3, 1): m.step() rospy.sleep(0.1) goal_proxy = m.goals[0] goal_proxy.fetchStatus(3) self.assertTrue(goal_proxy.satisfied, 'Goal is not satisfied')
def test_rl_basic(self): method_prefix = self.__message_prefix + "test_rl_basic" planner_prefix = method_prefix + "Manager" register_in_factory(ActivationAlgorithmFactory) m = Manager(activationThreshold=7, prefix=planner_prefix, activation_algorithm="reinforcement") behaviour_a = BehaviourA(name=method_prefix + "A", planner_prefix=planner_prefix) behaviour_b = BehaviourB(name=method_prefix + "B", planner_prefix=planner_prefix) sensor = Sensor(name="bool_sensor", initial_value=False) # same effect so planner cannot distinguish behaviour_a.add_effect(Effect(sensor_name=sensor.name, indicator=1.0)) behaviour_b.add_effect(Effect(sensor_name=sensor.name, indicator=1.0)) goal = GoalBase(method_prefix + '_goal', planner_prefix=planner_prefix) goal.add_condition(Condition(sensor, BooleanActivator())) for x in range(0, 10, 1): m.step() rospy.sleep(0.1)
def test_independent_behaviour(self): """ Test behaviour property independentFromPlanner """ method_prefix = self.__message_prefix + "test_independent_behaviour" planner_prefix = method_prefix + "Manager" m = Manager(activationThreshold=7, prefix=planner_prefix) topic_name_1 = method_prefix + '/sensor_1' sensor = TopicSensor(topic=topic_name_1, message_type=Bool, initial_value=False) condition = Condition(sensor, BooleanActivator()) #independentFromPlanner and effects independent_behaviour = SetTrueBehavior(effect_name=sensor.name, topic_name=topic_name_1, name=method_prefix + "SetTrue", planner_prefix=planner_prefix, independentFromPlanner=True) #independentFromPlanner and no effects independent_behaviour2 = SetTrueBehavior(effect_name=None, topic_name=topic_name_1, name=method_prefix + "SetTrue2", planner_prefix=planner_prefix, independentFromPlanner=True) # not independentFromPlanner and no effects independent_behaviour3 = SetTrueBehavior(effect_name=None, topic_name=topic_name_1, name=method_prefix + "SetTrue3", planner_prefix=planner_prefix, independentFromPlanner=False) goal = GoalBase(method_prefix + 'CentralGoal', planner_prefix=planner_prefix) goal.add_condition(condition) for x in range(0, 3, 1): m.step() rospy.sleep(0.1) goal_proxy = m.goals[0] goal_proxy.fetchStatus(3) self.assertTrue(goal_proxy.satisfied, 'Goal is not satisfied') self.assertTrue(independent_behaviour._isExecuting, "independent_behaviour is not executed") self.assertFalse(independent_behaviour2.was_executed, "independent_behaviour2 was executed") self.assertFalse(independent_behaviour3.was_executed, "independent_behaviour2 was executed")
def test_guarantee_decision(self): """ Testing guarantee_decision parameter of manager.step() """ method_prefix = self.__message_prefix + "test_guarantee_decision" planner_prefix = method_prefix + "Manager" manager = Manager(activationThreshold=7.0, prefix=planner_prefix) sensor_3 = Sensor(name="Sensor3", initial_value=False) behaviour_1 = BehaviourBase(name="Behaviour1", planner_prefix=planner_prefix) goal1 = GoalBase(name="Test_Goal1", conditions=[Condition(sensor_3, BooleanActivator())], planner_prefix=planner_prefix) manager.step() self.assertFalse( behaviour_1._isExecuting, "Behaviour 1 is executed even though it should not have enough activation" ) manager.step(guarantee_decision=True) self.assertTrue( behaviour_1._isExecuting, "Behaviour 1 is not executed even though a decision was forced")
def _initialize_behaviour_model(self): """ This function initialises the RHBP behaviour/goal model. """ # Random Move/Exploration random_move = RandomMove(name="random_move", agent_name=self._agent_name) self.behaviours.append(random_move) random_move.add_effect( Effect(self.perception_provider.dispenser_visible_sensor.name, indicator=True)) # Moving to a dispenser if in vision range move_to_dispenser = MoveToDispenser( name="move_to_dispense", perception_provider=self.perception_provider, agent_name=self._agent_name) self.behaviours.append(move_to_dispenser) move_to_dispenser.add_effect( Effect(self.perception_provider.closest_dispenser_distance_sensor. name, indicator=-1, sensor_type=float)) move_to_dispenser.add_precondition( Condition(self.perception_provider.dispenser_visible_sensor, BooleanActivator(desiredValue=True))) move_to_dispenser.add_precondition( Condition( self.perception_provider.closest_dispenser_distance_sensor, ThresholdActivator(isMinimum=True, thresholdValue=2))) # Dispense a block if close enough dispense = Dispense(name="dispense", perception_provider=self.perception_provider, agent_name=self._agent_name) self.behaviours.append(dispense) dispense.add_effect( Effect(self.perception_provider.number_of_blocks_sensor.name, indicator=+1, sensor_type=float)) dispense.add_precondition( Condition( self.perception_provider.closest_dispenser_distance_sensor, ThresholdActivator(isMinimum=False, thresholdValue=1))) # Our simple goal is to create more and more blocks dispense_goal = GoalBase( "dispensing", permanent=True, conditions=[ Condition(self.perception_provider.number_of_blocks_sensor, GreedyActivator()) ], planner_prefix=self._agent_name) self.goals.append(dispense_goal)
def test_interruptable_behaviour(self): """ Test behaviour interruptable property """ method_prefix = self.__message_prefix + "test_interruptable_behaviour" planner_prefix = method_prefix + "Manager" m = Manager(activationThreshold=7, prefix=planner_prefix) topic_name_1 = method_prefix + '/sensor_1' non_interruptable_sensor = SimpleTopicSensor(topic=topic_name_1, message_type=Int32, initial_value=False) non_interruptable_condition = Condition(non_interruptable_sensor, GreedyActivator()) condition_function_name = non_interruptable_condition.getFunctionNames()[0] non_interruptable_behaviour = IncreaserBehavior(effect_name=condition_function_name, topic_name=topic_name_1, name=method_prefix + "TopicIncreaser", plannerPrefix=planner_prefix, interruptable=False) topic_name_2 = method_prefix + '/sensor_2' interruptable_sensor = SimpleTopicSensor(topic=topic_name_2, message_type=Int32, initial_value=False) interruptable_condition = Condition(interruptable_sensor, GreedyActivator()) condition_function_name2 = interruptable_condition.getFunctionNames()[0] interruptable_behaviour = IncreaserBehavior(effect_name=condition_function_name2, topic_name=topic_name_2, name=method_prefix + "TopicIncreaser2", plannerPrefix=planner_prefix, interruptable=True) enable_sensor = Sensor(name='enable_sensor', initial_value=True) enable_cond = Condition(enable_sensor, BooleanActivator()) non_interruptable_behaviour.addPrecondition(enable_cond) goal = GoalBase(method_prefix + 'CentralGoal', plannerPrefix=planner_prefix, permanent=True) goal.addCondition(non_interruptable_condition) goal.addCondition(interruptable_condition) # first normal operation: every behaviour runs as expected for x in range(0, 4, 1): m.step() rospy.sleep(0.1) self.assertTrue(non_interruptable_behaviour._isExecuting, "Non-Interruptable Behaviour is not executed") self.assertTrue(interruptable_behaviour._isExecuting, "Interruptable Behaviour is not executed") #disable non_interruptable_behaviour precondition and check if it is affected enable_sensor.update(False) for x in range(0, 1, 1): m.step() rospy.sleep(0.1) self.assertTrue(non_interruptable_behaviour._isExecuting, "Non-Interruptable Behaviour is not executed") self.assertTrue(interruptable_behaviour._isExecuting, "Interruptable Behaviour is not executed") #disable precondition of interruptable behaviour and check if it is disabled as well interruptable_behaviour.addPrecondition(enable_cond) for x in range(0, 1, 1): m.step() rospy.sleep(0.1) self.assertTrue(non_interruptable_behaviour._isExecuting, "Non-Interruptable Behaviour is not executed") self.assertFalse(interruptable_behaviour._isExecuting, "Interruptable Behaviour is executed")
def test_plan_monitoring_float_sensors(self): """ Testing plan monitoring and replanning management """ method_prefix = self.__message_prefix + "test_plan_monitoring_float_sensors" planner_prefix = method_prefix + "Manager" manager = Manager(activationThreshold=7.0, prefix=planner_prefix, max_parallel_behaviours=1) sensor_1 = Sensor(name="Sensor1", initial_value=0) behaviour_1 = BehaviourBase(name="Behaviour1", planner_prefix=planner_prefix) behaviour_1.add_effect(Effect(sensor_1.name, 1, sensor_type=float)) sensor_2 = Sensor(name="Sensor2", initial_value=0) behaviour_2 = BehaviourBase(name="Behaviour2", planner_prefix=planner_prefix) behaviour_2.add_effect(Effect(sensor_2.name, 1, sensor_type=float)) behaviour_3 = BehaviourBase(name="Behaviour3", planner_prefix=planner_prefix) # adding precondition to get a reference to the sensor in the manager behaviour_3.add_precondition(Condition(sensor_2, BooleanActivator())) behaviour_3.add_effect(Effect(sensor_2.name, 1, sensor_type=float)) goal1 = GoalBase(name="Test_Goal1", conditions=[ Conjunction( Condition(sensor_1, ThresholdActivator(1)), Condition( sensor_2, ThresholdActivator(0), )) ], planner_prefix=planner_prefix, permanent=True) manager.step(guarantee_decision=True) self.assertTrue( behaviour_1._isExecuting, "Behaviour 1 is not executed even though a decision was forced and it would fulfill the plan" ) sensor_1.update( 0.5 ) # faking partial effect of behaviour1, all changes are induced by behaviour, no replanning manager.step(guarantee_decision=True) self.assertTrue( behaviour_1._isExecuting, "Behaviour 1 is not executed even though a decision was forced and it would fulfill the plan" ) self.assertTrue( manager._are_all_sensor_changes_from_executed_behaviours()) self.assertFalse(manager._are_effects_of_planned_behaviour_realised() ) # we did not yet reach the full effect self.assertTrue(manager._executed_behaviours_influenced_as_expected()) sensor_1.update( 1.0 ) # faking partial effect of behaviour1, all changes are induced by behaviour, no replanning manager.step(guarantee_decision=True) self.assertTrue( behaviour_1._isExecuting, "Behaviour 1 is not executed even though a decision was forced and it would fulfill the plan" ) self.assertTrue( manager._are_all_sensor_changes_from_executed_behaviours()) self.assertTrue(manager._executed_behaviours_influenced_as_expected()) manager._planExecutionIndex -= 1 # we have to manipulate here because it was incremented self.assertTrue(manager._are_effects_of_planned_behaviour_realised()) manager._planExecutionIndex += 1
def test_plan_with_registered_goals(self): method_prefix = self.__message_prefix + "test_handle_interfering_correlations" planner_prefix = method_prefix + "Manager" manager = Manager(activationThreshold=7.0, prefix=planner_prefix) sensor_1 = Sensor(name="Sensor1", initial_value=False) sensor_2 = Sensor(name="Sensor2", initial_value=False) sensor_3 = Sensor(name="Sensor3", initial_value=False) behaviour_1 = BehaviourBase(name="Behaviour1", planner_prefix=planner_prefix) behaviour_1.add_effect(Effect(sensor_name=sensor_1.name, indicator=1)) behaviour_2 = BehaviourBase(name="Behaviour2", planner_prefix=planner_prefix) behaviour_2.add_effect(Effect(sensor_name=sensor_2.name, indicator=1)) behaviour_2.add_precondition(Condition(sensor_1, BooleanActivator())) behaviour_3 = BehaviourBase(name="Behaviour3", planner_prefix=planner_prefix) behaviour_3.add_effect(Effect(sensor_name=sensor_3.name, indicator=1)) behaviour_3.add_precondition(Condition(sensor_2, BooleanActivator())) goal1 = GoalBase(name="Test_Goal1", conditions=[Condition(sensor_3, BooleanActivator())], planner_prefix=planner_prefix) goal2 = GoalBase(name="Test_Goal2", conditions=[Condition(sensor_2, BooleanActivator())], planner_prefix=planner_prefix) # test plannning without prior decision step plan_seq = manager.plan_with_registered_goals([manager.goals[0]]) expected_plan_seq = ["Behaviour1", "Behaviour2", "Behaviour3"] self.assertEquals(len(plan_seq), len(expected_plan_seq)) self.assertListEqual(plan_seq, expected_plan_seq) # plan again using a service service_name = planner_prefix + '/' + 'PlanWithGoal' # do not wait forever here, manager might be already closed rospy.wait_for_service(service_name, timeout=1) plan_service = rospy.ServiceProxy(service_name, PlanWithGoal) # use all registered goals res = plan_service() self.assertEquals(len(res.plan_sequence), len(expected_plan_seq)) self.assertListEqual(res.plan_sequence, expected_plan_seq) # use other named goal expected_plan_seq = ["Behaviour1", "Behaviour2"] res = plan_service(goal_names=[goal2.name]) self.assertEquals(len(res.plan_sequence), len(expected_plan_seq)) self.assertListEqual(res.plan_sequence, expected_plan_seq) # test infeasible plan sensor_4 = Sensor(name="Sensor4", initial_value=False) goal3 = GoalBase(name="Test_Goal3", conditions=[Condition(sensor_4, BooleanActivator())], planner_prefix=planner_prefix) expected_plan_seq = [] # force an update to make the new goal available res = plan_service(goal_names=[goal3.name], force_state_update=True) self.assertEquals(len(res.plan_sequence), len(expected_plan_seq)) self.assertListEqual(res.plan_sequence, expected_plan_seq)
def test_handle_interfering_correlations(self): """ Test manager interference resolving capabilities """ method_prefix = self.__message_prefix + "test_handle_interfering_correlations" planner_prefix = method_prefix + "Manager" m = Manager(activationThreshold=7.0, prefix=planner_prefix) topic_name_1 = method_prefix + '/sensor_1' sensor1 = TopicSensor(topic=topic_name_1, message_type=Int32, initial_value=False) condition1 = Condition(sensor1, GreedyActivator()) behaviour1 = IncreaserBehavior(effect_name=sensor1.name, topic_name=topic_name_1, name=method_prefix + "TopicIncreaser", planner_prefix=planner_prefix, interruptable=True) topic_name_2 = method_prefix + '/sensor_2' sensor2 = TopicSensor(topic=topic_name_2, message_type=Int32, initial_value=False) condition2 = Condition(sensor2, GreedyActivator()) behaviour2 = IncreaserBehavior(effect_name=sensor2.name, topic_name=topic_name_2, name=method_prefix + "TopicIncreaser2", planner_prefix=planner_prefix, interruptable=True) # add a conflict here "-1" behaviour1.add_effect( Effect(sensor_name=sensor2.name, indicator=-1, sensor_type=int)) goal = GoalBase(method_prefix + 'CentralGoal', planner_prefix=planner_prefix, permanent=True) goal.add_condition(condition1) goal.add_condition(condition2) # first one of the behaviours can not be executed due to a conflict for x in range(0, 4, 1): m.step() rospy.sleep(0.1) # behaviour 2 should be executed because it does not conflict self.assertFalse(behaviour1._isExecuting, "Behaviour 1 is executed in spite of a conflict") self.assertTrue(behaviour2._isExecuting, "Behaviour 2 is not executed") behaviour1.priority = 1 # increase priority for x in range(0, 1, 1): m.step() rospy.sleep(0.1) # now behaviour 1 should be executed self.assertTrue( behaviour1._isExecuting, "Behaviour 1 is not executed in spite of higher priority") self.assertFalse(behaviour2._isExecuting, "Behaviour 2 is executed in spite of lower priority") behaviour1.priority = 0 # reset priority # Manipulate activation of behaviour 2 with an extra goal, wish is positively influencing goal2 = GoalBase(method_prefix + 'ExtraGoal', planner_prefix=planner_prefix, permanent=True) goal2.add_condition( condition2) # this condition is targeted by behaviour 2 rospy.sleep(0.1) for x in range( 0, 5, 1 ): # it takes some time with configured decay factor to integrate the changed activation m.step() rospy.sleep(0.1) self.assertFalse(behaviour1._isExecuting, "Behaviour 1 is executed") self.assertTrue(behaviour2._isExecuting, "Behaviour 2 is not executed")
def _initialize_behaviour_model(self): """ This function initialises the RHBP behaviour/goal model. """ ### Exploration ## exploration_move = ExplorationBehaviour(name="exploration_move", agent_name=self._agent_name, rhbp_agent=self) self.behaviours.append(exploration_move) # assigned to a task exploration_move.add_precondition(Condition(sensor=self.sensor_manager.assigned_task_list_empty, activator=BooleanActivator(desiredValue=True))) exploration_move.add_effect(Effect(self.perception_provider.dispenser_visible_sensor.name, indicator=True)) exploration_move.add_effect(Effect(self.sensor_manager.assigned_task_list_empty.name, indicator=True)) ### Move to Dispenser ### move_to_dispenser = MoveToDispenserBehaviour(name="move_to_dispenser", agent_name=self._agent_name, rhbp_agent=self) self.behaviours.append(move_to_dispenser) # assigned to a task precondition move_to_dispenser.add_precondition( Condition(self.sensor_manager.assigned_task_list_empty, BooleanActivator(desiredValue=False)) ) # block not attached precondition move_to_dispenser.add_precondition( Condition(self.sensor_manager.attached_to_block, BooleanActivator(desiredValue=False)) ) # not at the dispenser precondition move_to_dispenser.add_precondition( Condition(self.sensor_manager.at_the_dispenser, BooleanActivator(desiredValue=False)) ) move_to_dispenser.add_effect(Effect(self.sensor_manager.at_the_dispenser.name, indicator=True)) # # Our simple goal is to create more and more blocks # dispense_goal = GoalBase("dispensing", permanent=True, # conditions=[ # Condition(self.sensor_manager.at_the_dispenser, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(dispense_goal) ### Requeste block - Dispense ### dispense = DispenseBehaviour(name="dispense", agent_name=self._agent_name, rhbp_agent=self) self.behaviours.append(dispense) # assigned to a task precondition dispense.add_precondition( Condition(self.sensor_manager.assigned_task_list_empty, BooleanActivator(desiredValue=False)) ) # block not attached precondition dispense.add_precondition( Condition(self.sensor_manager.attached_to_block, BooleanActivator(desiredValue=False)) ) # not at the dispenser precondition dispense.add_precondition( Condition(self.sensor_manager.at_the_dispenser, BooleanActivator(desiredValue=True)) ) # not next to block dispense.add_precondition(Condition(self.sensor_manager.next_to_block, BooleanActivator(desiredValue=False))) # effect of dispense is that agent is next to block dispense.add_effect(Effect(self.sensor_manager.next_to_block.name, indicator=True)) # Our simple goal is to create more and more blocks # dispense_goal = GoalBase("dispensing", permanent=True, # conditions=[ # Condition(self.sensor_manager.attached_to_block, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(dispense_goal) #### Attach to Block ### attach = AttachBehaviour(name="attach", agent_name=self._agent_name, rhbp_agent=self) self.behaviours.append(attach) # Preconditions # assigned to a task attach.add_precondition(Condition(sensor=self.sensor_manager.assigned_task_list_empty, activator=BooleanActivator(desiredValue=False))) # is not yet attached to a block of type of the current task attach.add_precondition(Condition(sensor=self.sensor_manager.attached_to_block, activator=BooleanActivator(desiredValue=False))) # has already dispensed the block, temporary solution for lack of communication attach.add_precondition( Condition(sensor=self.sensor_manager.is_dispensed, activator=BooleanActivator(desiredValue=True))) # is next to a block attach.add_precondition( Condition(sensor=self.sensor_manager.next_to_block, activator=BooleanActivator(desiredValue=True))) # has free capacity to attach attach.add_precondition(Condition(sensor=self.sensor_manager.fully_attached, activator=BooleanActivator(desiredValue=False))) # effect of attach is that agent is attached to a block attach.add_effect(Effect(self.sensor_manager.attached_to_block.name, indicator=True)) # attach_goal = GoalBase("attaching", permanent=True, # conditions=[ # Condition(self.sensor_manager.attached_to_block, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(attach_goal) #### Reach meeting point ### reach_meeting_point = ReachMeetingPointBehaviour(name="reach_meeting_point", agent_name=self._agent_name, rhbp_agent=self) self.behaviours.append(reach_meeting_point) # assigned to a task reach_meeting_point.add_precondition(Condition(sensor=self.sensor_manager.assigned_task_list_empty, activator=BooleanActivator(desiredValue=False))) # have the block attached reach_meeting_point.add_precondition(Condition(sensor=self.sensor_manager.attached_to_block, activator=BooleanActivator(desiredValue=True))) # the shape is not complete reach_meeting_point.add_precondition(Condition(sensor=self.sensor_manager.shape_complete, activator=BooleanActivator(desiredValue=False))) # has not reached the meeting point already reach_meeting_point.add_precondition(Condition(sensor=self.sensor_manager.at_meeting_point, activator=BooleanActivator(desiredValue=False))) # effect is moving till the agent reaches the meeting point reach_meeting_point.add_effect(Effect(self.sensor_manager.at_meeting_point.name, indicator=True)) # at_meeting_point_goal = GoalBase("reach_meeting_point_goal", permanent=True, # conditions=[ # Condition(self.sensor_manager.at_meeting_point, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(at_meeting_point_goal) #### Connect ### connect = ConnectBehaviour(name="connect", agent_name=self._agent_name, rhbp_agent=self) self.behaviours.append(connect) # assigned to a task connect.add_precondition(Condition(sensor=self.sensor_manager.assigned_task_list_empty, activator=BooleanActivator(desiredValue=False))) # have the block attached connect.add_precondition(Condition(sensor=self.sensor_manager.attached_to_block, activator=BooleanActivator(desiredValue=True))) # connection not completed connect.add_precondition(Condition(sensor=self.sensor_manager.connect_successful, activator=BooleanActivator(desiredValue=False))) # has reached the meeting point connect.add_precondition(Condition(sensor=self.sensor_manager.at_meeting_point, activator=BooleanActivator(desiredValue=True))) # effect is creating the shape connect.add_effect(Effect(self.sensor_manager.connect_successful.name, indicator=True)) connect.add_effect(Effect(self.sensor_manager.shape_complete.name, indicator=True)) # connect_successful_goal = GoalBase("connect_successful_goal", permanent=True, # conditions=[ # Condition(self.sensor_manager.connect_successful, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(connect_successful_goal) #### Detach to Block ### detach = DetachBehaviour(name="detach", agent_name=self._agent_name, rhbp_agent=self) self.behaviours.append(detach) # Preconditions # assigned to a task detach.add_precondition(Condition(sensor=self.sensor_manager.assigned_task_list_empty, activator=BooleanActivator(desiredValue=False))) # connect action was succesful detach.add_precondition( Condition(sensor=self.sensor_manager.connect_successful, activator=BooleanActivator(desiredValue=True))) # is NOT the agent assigned to submit detach.add_precondition( Condition(sensor=self.sensor_manager.can_submit, activator=BooleanActivator(desiredValue=False))) detach.add_effect(Effect(self.sensor_manager.points.name, indicator=True)) #### Go to goal area ### go_to_goal_area = ReachGoalAreaBehaviour(name="go_to_goal_area", agent_name=self._agent_name, rhbp_agent=self) self.behaviours.append(go_to_goal_area) # Preconditions # assigned to a task go_to_goal_area.add_precondition(Condition(sensor=self.sensor_manager.assigned_task_list_empty, activator=BooleanActivator(desiredValue=False))) # connect action was succesful go_to_goal_area.add_precondition( Condition(sensor=self.sensor_manager.shape_complete, activator=BooleanActivator(desiredValue=True))) # is the agent assigned to submit go_to_goal_area.add_precondition( Condition(sensor=self.sensor_manager.can_submit, activator=BooleanActivator(desiredValue=True))) # is the agent assigned to submit go_to_goal_area.add_precondition( Condition(sensor=self.sensor_manager.at_goal_area, activator=BooleanActivator(desiredValue=False))) # effect is that agent is at goal area go_to_goal_area.add_effect(Effect(self.sensor_manager.at_goal_area.name, indicator=True)) #### Submit ### submit = SubmitBehaviour(name="submit", agent_name=self._agent_name, rhbp_agent=self) self.behaviours.append(submit) # Preconditions # assigned to a task submit.add_precondition(Condition(sensor=self.sensor_manager.assigned_task_list_empty, activator=BooleanActivator(desiredValue=False))) # have the block attached connect.add_precondition(Condition(sensor=self.sensor_manager.attached_to_block, activator=BooleanActivator(desiredValue=True))) # connect action was succesful submit.add_precondition( Condition(sensor=self.sensor_manager.shape_complete, activator=BooleanActivator(desiredValue=True))) # is the agent assigned to submit submit.add_precondition( Condition(sensor=self.sensor_manager.can_submit, activator=BooleanActivator(desiredValue=True))) # is the agent at the goal area submit.add_precondition( Condition(sensor=self.sensor_manager.at_goal_area, activator=BooleanActivator(desiredValue=True))) # effect of is that points are earned submit.add_effect(Effect(self.sensor_manager.points.name, indicator=True)) # our unique goal is to make points make_points_goal = GoalBase("make_points_goal", permanent=True, conditions=[ Condition(self.sensor_manager.points, GreedyActivator())], planner_prefix=self._agent_name) self.goals.append(make_points_goal)
def _initialize_behaviour_model(self): """ This function initialises the RHBP behaviour/goal model. """ if self.manual_player: control_player() control = AgentControl(name="manual_control", perception_provider=self.perception_provider, agent_name=self._agent_name) self.behaviours.append(control) else: # Exploration targeted at locating goal explorer = Explore_astar(name="explore", perception_provider=self.perception_provider, agent_name=self._agent_name,priority=1) self.behaviours.append(explorer) explorer.add_effect( Effect(self.perception_provider.count_goal_cells.name, indicator=+1, sensor_type=float)) #Reach dispenser once origin has been obtained reach_dispenser = MoveToDispenser(name="reach_dispenser", perception_provider=self.perception_provider, agent_name=self._agent_name,priority=2) self.behaviours.append(reach_dispenser) reach_dispenser.add_effect( Effect(self.perception_provider.closest_dispenser_distance_sensor.name, indicator=-1, sensor_type=float)) pre_cond2 = Condition(self.perception_provider.target_dispenser_selected_sensor, BooleanActivator(desiredValue=True)) reach_dispenser.add_precondition(pre_cond2) # #Dispense from dispenser if near dispense = Dispense(name="dispense", perception_provider=self.perception_provider, agent_name=self._agent_name,priority=3) self.behaviours.append(dispense) dispense.add_effect( Effect(self.perception_provider.sensor_dispensed_blocks.name, indicator=+1, sensor_type=float)) pre_cond3 = Condition(self.perception_provider.closest_dispenser_distance_sensor, ThresholdActivator(isMinimum=False, thresholdValue=1)) dispense.add_precondition(Conjunction(pre_cond2,pre_cond3)) # # Attach a block if close enough attach = Attach(name="attach", perception_provider=self.perception_provider, agent_name=self._agent_name,priority=4) self.behaviours.append(attach) pre_cond4 = Condition(self.perception_provider.sensor_dispensed_blocks, ThresholdActivator(isMinimum=True,thresholdValue=1)) pre_cond5 = Condition(self.perception_provider.closest_block_distance_sensor, ThresholdActivator(isMinimum=False, thresholdValue=1)) attach.add_effect( Effect(self.perception_provider.sensor_attached_blocks.name, indicator=+1, sensor_type=float)) attach.add_precondition(Conjunction(pre_cond4,pre_cond5)) #Move to goal once attached reach_goal = MoveToGoal(name="reach_goal", perception_provider=self.perception_provider, agent_name=self._agent_name,priority=10) self.behaviours.append(reach_goal) reach_goal.add_effect( Effect(self.perception_provider.submit_count.name, indicator=+1, sensor_type=float)) pre_cond6 = Condition(self.perception_provider.sensor_attached_blocks, ThresholdActivator(isMinimum=True,thresholdValue=1)) reach_goal.add_precondition(pre_cond6) #Submit Task if agent has reached location submit_task = Submit(name="submit_task",perception_provider=self.perception_provider, agent_name=self._agent_name,priority=20) self.behaviours.append(submit_task) submit_task.add_effect(Effect(self.perception_provider.score_sensor.name,indicator=+1, sensor_type=float)) pre_cond7 = Condition(self.perception_provider.submit_sensor, ThresholdActivator(isMinimum=True,thresholdValue=1)) submit_task.add_precondition(pre_cond7) #Keep dispensing if possible dispense_goal = GoalBase("dispense_goal",permanent=True, conditions=[Condition(self.perception_provider.sensor_dispensed_blocks, GreedyActivator())], planner_prefix=self._agent_name, priority=1) self.goals.append(dispense_goal) #Attach to dispensed blocks attach_goal = GoalBase("attach_goal",permanent=True, conditions=[Condition(self.perception_provider.sensor_attached_blocks, GreedyActivator())], planner_prefix=self._agent_name, priority=2) self.goals.append(attach_goal) #Primary goal to get more points submit_goal = GoalBase("submit_goal",permanent=True, conditions=[Condition(self.perception_provider.score_sensor, GreedyActivator())], planner_prefix=self._agent_name, priority=4)
def test_register_unregister(self): method_prefix = self.__message_prefix + "TestRegisterUnregisterGoal" planner_prefix = method_prefix + "Manager" m = Manager(activationThreshold=7, prefix=planner_prefix) topic_name = method_prefix + '/Topic' sensor = TopicSensor(topic=topic_name, message_type=Bool, initial_value=False) condition = Condition(sensor, BooleanActivator()) goal = GoalBase(method_prefix + 'Goal', planner_prefix=planner_prefix) goal.add_condition(condition) rospy.sleep(0.1) m.step() self.assertEquals(len(m.goals), 1, 'goal not registered properly') goal.unregister() rospy.sleep(0.1) m.step() self.assertEquals(len(m.goals), 0, 'goal not unregistered properly') # try to register again goal.register() rospy.sleep(0.1) m.step() self.assertEquals(len(m.goals), 1, 'goal not registered properly') goal.unregister(terminate_services=False) rospy.sleep(0.1) m.step() self.assertEquals(len(m.goals), 0, 'goal not unregistered properly') # try to register again goal.register() rospy.sleep(0.1) m.step() self.assertEquals(len(m.goals), 1, 'goal not registered properly')
def _initialize_behaviour_model(self): """ This function initialises the RHBP behaviour/goal model. """ # RANDOM MOVE random_move = RandomMove(name="random_move", agent_name=self._agent_name) self.behaviours.append(random_move) random_move.add_effect( Effect(self.sensor_manager.dispenser_distance_sensor.name, indicator=True)) random_move.add_precondition( Condition(self.sensor_manager.dispenser_in_map_sensor, BooleanActivator(desiredValue=False))) # Exploration # explore_move = ExploreMove(name="explore_move", agent_name=self._agent_name, agent_map=self.map) # self.behaviours.append(explore_move) # explore_move.add_effect(Effect(self.perception_provider.dispenser_visible_sensor.name, indicator=True)) # MOVE TO DISPENSER move_to_dispenser = MoveToDispenser( name="move_to_dispenser", perception_provider=self.perception_provider, agent_name=self._agent_name, agent_map=self.map) self.behaviours.append(move_to_dispenser) move_to_dispenser.add_effect( Effect(self.sensor_manager.next_to_dispenser_sensor.name, indicator=True)) # move_to_dispenser.add_effect( # Effect(self.sensor_manager.pseudo_sensor.name, indicator=1, sensor_type=float)) move_to_dispenser.add_precondition( Condition(self.sensor_manager.dispenser_in_map_sensor, BooleanActivator(desiredValue=True))) move_to_dispenser.add_precondition( Condition(self.sensor_manager.next_to_dispenser_sensor, BooleanActivator(desiredValue=False))) # # Our simple goal is to move to the dispenser # move_to_dispenser_goal = GoalBase("move_to_dispenser_goal", permanent=True, # conditions=[Condition(self.sensor_manager.pseudo_sensor, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(move_to_dispenser_goal) # Request a block if close enough request = Request(name="request", perception_provider=self.perception_provider, agent_name=self._agent_name, agent_map=self.map) self.behaviours.append(request) # request.add_effect( # Effect(self.sensor_manager.pseudo_sensor.name, indicator=+1, sensor_type=float)) request.add_effect( Effect(self.sensor_manager.next_to_block_sensor.name, indicator=True)) request.add_precondition( Condition(self.sensor_manager.next_to_dispenser_sensor, BooleanActivator(desiredValue=True))) # # Our simple goal is to request a block # request_goal = GoalBase("request_goal", permanent=True, # conditions=[Condition(self.sensor_manager.pseudo_sensor, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(request_goal) # # Move to nearby block # move_to_block = MoveToBlock(name="move_to_block", perception_provider=self.perception_provider, # agent_name=self._agent_name, agent_map=self.map) # self.behaviours.append(move_to_block) # move_to_block.add_effect( # Effect(self.sensor_manager.block_distance_sensor.name, indicator=-1, sensor_type=float)) # # move_to_block.add_effect( # # Effect(self.sensor_manager.pseudo_sensor.name, indicator=1, sensor_type=float)) # move_to_block.add_precondition( # Condition(self.sensor_manager.block_in_map_sensor, BooleanActivator(desiredValue=True))) # move_to_block.add_precondition(Condition(self.sensor_manager.block_distance_sensor, # ThresholdActivator(isMinimum=True, thresholdValue=2))) # # Our simple goal is to move to a block # move_to_block_goal = GoalBase("move_to_block_goal", permanent=True, # conditions=[Condition(self.sensor_manager.pseudo_sensor, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(move_to_block_goal) # ATTACH attach = Attach(name="attach", perception_provider=self.perception_provider, agent_name=self._agent_name, agent_map=self.map) self.behaviours.append(attach) # attach.add_effect(Effect(self.sensor_manager.pseudo_sensor.name, indicator=+1, sensor_type=float)) attach.add_effect( Effect(self.sensor_manager.block_attached_sensor.name, indicator=True)) attach.add_precondition( Condition(self.sensor_manager.next_to_block_sensor, BooleanActivator(desiredValue=True))) # # Our simple goal is to attach to a block # attach_goal = GoalBase("attach_goal", permanent=True, # conditions=[Condition(self.sensor_manager.pseudo_sensor, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(attach_goal) # MOVE TO GOAL move_to_goal = MoveToGoal(name="move_to_goal", perception_provider=self.perception_provider, agent_name=self._agent_name, agent_map=self.map) self.behaviours.append(move_to_goal) # move_to_goal.add_effect(Effect(self.sensor_manager.pseudo_sensor.name, indicator=+1, sensor_type=float)) move_to_goal.add_effect( Effect(self.sensor_manager.on_goal_sensor.name, indicator=True)) move_to_goal.add_precondition( Condition(self.sensor_manager.block_attached_sensor, BooleanActivator(desiredValue=True))) # # Our simple goal is to attach to a block # move_to_goal_goal = GoalBase("move_to_goal_goal", permanent=True, # conditions=[Condition(self.sensor_manager.pseudo_sensor, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(move_to_goal_goal) # PREPARE SUBMISSION prepare_submission = PrepareSubmission( name="prepare_submission", perception_provider=self.perception_provider, agent_name=self._agent_name) self.behaviours.append(prepare_submission) prepare_submission.add_effect( Effect(self.sensor_manager.correct_pattern_position_sensor.name, indicator=True)) # prepare_submission.add_effect(Effect(self.sensor_manager.pseudo_sensor.name, indicator=+1, sensor_type=float)) prepare_submission.add_precondition( Condition(self.sensor_manager.on_goal_sensor, BooleanActivator(desiredValue=True))) prepare_submission.add_precondition( Condition(self.sensor_manager.block_attached_sensor, BooleanActivator(desiredValue=True))) # # Our simple goal is to rotate the pattern into the correct position # prepare_submission_goal = GoalBase("prepare_submission_goal", permanent=True, # conditions=[Condition(self.sensor_manager.pseudo_sensor, GreedyActivator())], # planner_prefix=self._agent_name) # self.goals.append(prepare_submission_goal) # SUBMIT submit = Submit(name="submit", perception_provider=self.perception_provider, agent_name=self._agent_name, agent=self) self.behaviours.append(submit) submit.add_effect( Effect(self.sensor_manager.pseudo_sensor.name, indicator=+1, sensor_type=float)) submit.add_precondition( Condition(self.sensor_manager.on_goal_sensor, BooleanActivator(desiredValue=True))) submit.add_precondition( Condition(self.sensor_manager.correct_pattern_position_sensor, BooleanActivator(desiredValue=True))) # Our simple goal is to submit a task submit_goal = GoalBase("submit_goal", permanent=True, conditions=[ Condition(self.sensor_manager.pseudo_sensor, GreedyActivator()) ], planner_prefix=self._agent_name) self.goals.append(submit_goal)