Ejemplo n.º 1
0
 def run(self, cycle, verbose = 2):
     '''
     nbeacons logic for obstacles:
     1. QUICKSAND: 
        i. If the agent executes a move command that results in the agent's new location
        being that same as a location of quicksand, then the simulator inserts 'stuck'
        and removes 'free' atoms from the state.
        ii. If the agent executes a move command and it is located in quicksand, if there is a
        'free' atom in the state, then the agent will successfully move out of the quicksand. 
        Otherwise it will remain in the same location and no change to the world state will occur.
        
     NOTE: current implementation only supports executing one action of a plan at a time, if
     multiple actions are selected by the agent and given to the simulator (by being stored in
     self.mem.ACTIONS) this will break
     '''
     self.mem.set(self.mem.MIDCA_CYCLES, 1+self.mem.get(self.mem.MIDCA_CYCLES))
     self.verbose = verbose
     if self.filehandle: self.filehandle.write("Cycle # "+str(cycle)+"\n")
     # update wind if on schedule
     for wind_sched_item in self.wind_schedule:
         if wind_sched_item[0] == cycle:
             self.wind_strength = wind_sched_item[1]
             print "Just changed wind strength to "+str(self.wind_strength)
             if self.filehandle: self.filehandle.write("Just changed wind strength to "+str(self.wind_strength)+"\n")
     
     first_action = None
     try:
         #get selected actions for this cycle. This is set in the act phase.
         actions = self.mem.get(self.mem.ACTIONS)[-1]
         first_action = actions[0]
     except TypeError, IndexError:
         if verbose >= 1:
             print "Simulator: no actions selected yet by MIDCA."
         if self.filehandle: self.filehandle.write("Simulator: no actions selected yet by MIDCA.\n")
         if self.filehandle: self.filehandle.write(nbeacons_util.drawNBeaconsScene(self.world,rtn_str=True)+"\n")
         if self.filehandle: self.filehandle.write("----------------------------------------------\n")
         if self.filehandle: self.filehandle.flush()
         return
Ejemplo n.º 2
0
 def run(self, cycle, verbose = 2):
     '''
     nbeacons logic for obstacles:
     1. QUICKSAND: 
        i. If the agent executes a move command that results in the agent's new location
        being that same as a location of quicksand, then the simulator inserts 'stuck'
        and removes 'free' atoms from the state.
        ii. If the agent executes a move command and it is located in quicksand, if there is a
        'free' atom in the state, then the agent will successfully move out of the quicksand. 
        Otherwise it will remain in the same location and no change to the world state will occur.
        
     NOTE: current implementation only supports executing one action of a plan at a time, if
     multiple actions are selected by the agent and given to the simulator (by being stored in
     self.mem.ACTIONS) this will break
     '''
     self.mem.set(self.mem.MIDCA_CYCLES, 1+self.mem.get(self.mem.MIDCA_CYCLES))
     self.verbose = verbose
     if self.filehandle: self.filehandle.write("Cycle # "+str(cycle)+"\n")
     # update wind if on schedule
     for wind_sched_item in self.wind_schedule:
         if wind_sched_item[0] == cycle:
             self.wind_strength = wind_sched_item[1]
             print "Just changed wind strength to "+str(self.wind_strength)
             if self.filehandle: self.filehandle.write("Just changed wind strength to "+str(self.wind_strength)+"\n")
     
     first_action = None
     try:
         #get selected actions for this cycle. This is set in the act phase.
         actions = self.mem.get(self.mem.ACTIONS)[-1]
         first_action = actions[0]
     except TypeError, IndexError:
         if verbose >= 1:
             print "Simulator: no actions selected yet by MIDCA."
         if self.filehandle: self.filehandle.write("Simulator: no actions selected yet by MIDCA.\n")
         if self.filehandle: self.filehandle.write(nbeacons_util.drawNBeaconsScene(self.world,rtn_str=True)+"\n")
         if self.filehandle: self.filehandle.write("----------------------------------------------\n")
         if self.filehandle: self.filehandle.flush()
         return
Ejemplo n.º 3
0
        
        # add subsequent actions depending on wind strength
        if self.wind and self.wind_dir in str(first_action):
            # check to see how far this move is
            start_x, start_y = map(int,str(first_action.args[1])[2:].split('y')) #hacky - relies on tiles represented as 'Tx2y3'
            end_x, end_y = map(int,str(first_action.args[-1])[2:].split('y')) # same kind of hackyness
            move_dist = (abs(end_x - start_x)+abs(end_y-start_y))
            if self.verbose >= 1: print "move_dist is "+str(move_dist)
            
            pushes_needed = self.wind_strength+1 - move_dist
            if self.verbose >= 1: print "  -->> pushes needed is "+str(pushes_needed)
            if self.filehandle: self.filehandle.write("  -->> pushes needed is "+str(pushes_needed)+"\n")

        # now start execution by executing the first action
        if not self.execute_action(first_action):
            if self.filehandle: self.filehandle.write(nbeacons_util.drawNBeaconsScene(self.world,rtn_str=True)+"\n")
            if self.filehandle: self.filehandle.write("----------------------------------------------\n")
            if self.filehandle: self.filehandle.flush()
            return

        if 'push' not in str(first_action):
            agent_stuck_in_mud = self.check_agent_in_mud()
        else:
            # we need to have a special case where, when the agent executes a push to become free
            # the agent doesn't immediately become stuck in mud again
            # so only when an action is not a 'push' will we check for, and insert, stuck atoms
            pass

        # now loop through the rest of the wind actions, unless the agent gets stuck in mud
        while pushes_needed > 0 and not agent_stuck_in_mud:
            # get next action
Ejemplo n.º 4
0
        
        # add subsequent actions depending on wind strength
        if self.wind and self.wind_dir in str(first_action):
            # check to see how far this move is
            start_x, start_y = map(int,str(first_action.args[1])[2:].split('y')) #hacky - relies on tiles represented as 'Tx2y3'
            end_x, end_y = map(int,str(first_action.args[-1])[2:].split('y')) # same kind of hackyness
            move_dist = (abs(end_x - start_x)+abs(end_y-start_y))
            if self.verbose >= 1: print "move_dist is "+str(move_dist)
            
            pushes_needed = self.wind_strength+1 - move_dist
            if self.verbose >= 1: print "  -->> pushes needed is "+str(pushes_needed)
            if self.filehandle: self.filehandle.write("  -->> pushes needed is "+str(pushes_needed)+"\n")

        # now start execution by executing the first action
        if not self.execute_action(first_action):
            if self.filehandle: self.filehandle.write(nbeacons_util.drawNBeaconsScene(self.world,rtn_str=True)+"\n")
            if self.filehandle: self.filehandle.write("----------------------------------------------\n")
            if self.filehandle: self.filehandle.flush()
            return

        if 'push' not in str(first_action):
            agent_stuck_in_mud = self.check_agent_in_mud()
        else:
            # we need to have a special case where, when the agent executes a push to become free
            # the agent doesn't immediately become stuck in mud again
            # so only when an action is not a 'push' will we check for, and insert, stuck atoms
            pass

        # now loop through the rest of the wind actions, unless the agent gets stuck in mud
        while pushes_needed > 0 and not agent_stuck_in_mud:
            # get next action
Ejemplo n.º 5
0
    def create_vanilla_MIDCA(self):

        #STATE_FILE = DOMAIN_ROOT + "states/.sim" # state file is generated dynamically
        DISPLAY_FUNC = nbeacons_util.drawNBeaconsScene
        DECLARE_METHODS_FUNC = methods_nbeacons.declare_methods
        DECLARE_OPERATORS_FUNC = operators_nbeacons.declare_operators
        GOAL_GRAPH_CMP_FUNC = nbeacons_util.preferFree

        WIND_ENABLED = True

        # Load domain
        world = domainread.load_domain(DOMAIN_FILE)

        # Load state
        stateread.apply_state_str(world, self.start_state)
        scenario_str = nbeacons_util.drawNBeaconsScene(world, rtn_str=True)

        # the first time, save to file
        if not os.path.isfile(SCENARIO_FILENAME):
            fs = open(SCENARIO_FILENAME, 'w')
            fs.write(str(scenario_str))
            fs.flush()
            fs.close()
        else:
            nbeacons_util.drawNBeaconsScene(world)

        #time.sleep(10)
        # Creates a PhaseManager object, which wraps a MIDCA object
        myMidca = base.PhaseManager(world, display=DISPLAY_FUNC, verbose=2)

        # Add phases by name
        for phase in [
                "Simulate", "Perceive", "Interpret", "Eval", "Cleanup",
                "Intend", "Plan", "Act"
        ]:
            myMidca.append_phase(phase)

        # Add the modules which instantiate basic operation
        #myMidca.append_module("Simulate", simulator.MidcaActionSimulator())
        myMidca.append_module(
            "Simulate",
            simulator.NBeaconsActionSimulator(wind=WIND_ENABLED,
                                              wind_dir=self.wind_dir,
                                              wind_strength=self.wind_strength,
                                              dim=DIMENSION,
                                              wind_schedule=WIND_SCHEDULE_1,
                                              logfilenm=DATADIR + "vanilla" +
                                              NOW_STR + ".log"))
        myMidca.append_module("Simulate",
                              simulator.ASCIIWorldViewer(DISPLAY_FUNC))
        myMidca.append_module("Perceive", perceive.PerfectObserver())

        #myMidca.append_module("Interpret", note.StateDiscrepancyDetector())
        #myMidca.append_module("Interpret", assess.SimpleNBeaconsExplain())
        #myMidca.append_module("Interpret", guide.UserGoalInput())
        myMidca.append_module(
            "Interpret",
            guide.NBeaconsGoalGenerator(numbeacons=2, goalList=self.goal_list))
        myMidca.append_module("Eval", evaluate.NBeaconsDataRecorder())
        myMidca.append_module(
            "Cleanup",
            simulator.NBeaconsSimulator(beacon_fail_rate=BEACON_FAIL_RATE))
        myMidca.append_module("Intend", intend.SimpleIntend())
        myMidca.append_module("Plan", planning.HeuristicSearchPlanner())
        #myMidca.append_module("Plan", planning.PyHopPlanner(nbeacons_util.pyhop_state_from_world,
        #                                                    nbeacons_util.pyhop_tasks_from_goals,
        #                                                    DECLARE_METHODS_FUNC,
        #                                                    DECLARE_OPERATORS_FUNC)) # set up planner for sample domain
        myMidca.append_module("Act", act.NBeaconsSimpleAct())

        # Set world viewer to output text
        myMidca.set_display_function(nbeacons_util.drawNBeaconsScene)

        # Tells the PhaseManager to copy and store MIDCA states so they can be accessed later.
        # Note: Turning this on drastically increases MIDCA's running time.
        myMidca.storeHistory = False
        myMidca.mem.logEachAccess = False

        # Initialize and start running!
        myMidca.initGoalGraph(cmpFunc=GOAL_GRAPH_CMP_FUNC)
        return myMidca
Ejemplo n.º 6
0
    def run(self, cycle, verbose=2):
        '''
        nbeacons logic for obstacles:
        1. QUICKSAND: 
           i. If the agent executes a move command that results in the agent's new location
           being that same as a location of quicksand, then the simulator inserts 'stuck'
           and removes 'free' atoms from the state.
           ii. If the agent executes a move command and it is located in quicksand, if there is a
           'free' atom in the state, then the agent will successfully move out of the quicksand. 
           Otherwise it will remain in the same location and no change to the world state will occur.
           
        NOTE: current implementation only supports executing one action of a plan at a time, if
        multiple actions are selected by the agent and given to the simulator (by being stored in
        self.mem.ACTIONS) this will break
        '''
        self.mem.set(self.mem.MIDCA_CYCLES,
                     1 + self.mem.get(self.mem.MIDCA_CYCLES))
        self.verbose = verbose
        if self.filehandle:
            self.filehandle.write("Cycle # " + str(cycle) + "\n")
        # update wind if on schedule
        for wind_sched_item in self.wind_schedule:
            if wind_sched_item[0] == cycle:
                self.wind_strength = wind_sched_item[1]
                print("Just changed wind strength to " +
                      str(self.wind_strength))
                if self.filehandle:
                    self.filehandle.write("Just changed wind strength to " +
                                          str(self.wind_strength) + "\n")

        first_action = None
        try:
            #get selected actions for this cycle. This is set in the act phase.
            actions = self.mem.get(self.mem.ACTIONS)[-1]
            first_action = actions[0]
        except TypeError as IndexError:
            if verbose >= 1:
                print("Simulator: no actions selected yet by MIDCA.")
            if self.filehandle:
                self.filehandle.write(
                    "Simulator: no actions selected yet by MIDCA.\n")
            if self.filehandle:
                self.filehandle.write(
                    nbeacons_util.drawNBeaconsScene(self.world, rtn_str=True) +
                    "\n")
            if self.filehandle:
                self.filehandle.write(
                    "----------------------------------------------\n")
            if self.filehandle: self.filehandle.flush()
            return
        except:
            return

        # generate wind actions if applicable, and going in the same direction
        remaining_actions = []  # these will be wind pushes
        pushes_needed = 0

        # add subsequent actions depending on wind strength
        if self.wind and self.wind_dir in str(first_action):
            # check to see how far this move is
            start_x, start_y = list(
                map(int,
                    str(first_action.args[1])[2:].split(
                        'y')))  #hacky - relies on tiles represented as 'Tx2y3'
            end_x, end_y = list(
                map(int,
                    str(first_action.args[-1])[2:].split(
                        'y')))  # same kind of hackyness
            move_dist = (abs(end_x - start_x) + abs(end_y - start_y))
            if self.verbose >= 1: print("move_dist is " + str(move_dist))

            pushes_needed = self.wind_strength + 1 - move_dist
            if self.verbose >= 1:
                print("  -->> pushes needed is " + str(pushes_needed))
            if self.filehandle:
                self.filehandle.write("  -->> pushes needed is " +
                                      str(pushes_needed) + "\n")

        # now start execution by executing the first action
        if not self.execute_action(first_action):
            if self.filehandle:
                self.filehandle.write(
                    nbeacons_util.drawNBeaconsScene(self.world, rtn_str=True) +
                    "\n")
            if self.filehandle:
                self.filehandle.write(
                    "----------------------------------------------\n")
            if self.filehandle: self.filehandle.flush()
            return

        if 'push' not in str(first_action):
            agent_stuck_in_mud = self.check_agent_in_mud()
        else:
            # we need to have a special case where, when the agent executes a push to become free
            # the agent doesn't immediately become stuck in mud again
            # so only when an action is not a 'push' will we check for, and insert, stuck atoms
            pass

        # now loop through the rest of the wind actions, unless the agent gets stuck in mud
        while pushes_needed > 0 and not agent_stuck_in_mud:
            # get next action
            #next_wind_action = remaining_actions[0]
            #remaining_actions = remaining_actions[1:]
            # execute action
            try:
                #print "About to apply named action "+str(next_wind_action)
                #self.world.apply_named_action(next_wind_action[0],next_wind_action[1:])
                self.sim_action_push()
                pushes_needed -= 1
                if self.verbose >= 1:
                    print("Wind has blown the agent in the " +
                          str(self.wind_dir) + " direction")
                if self.filehandle:
                    self.filehandle.write("Wind has blown the agent in the " +
                                          str(self.wind_dir) + " direction\n")
            except:
                if self.verbose >= 1:
                    print("Error executing sim push action : " +
                          str(sys.exc_info()[0]))
                if self.filehandle:
                    self.filehandle.write(
                        "Error executing sim push action : " +
                        str(sys.exc_info()[0]) + "\n")
                break
            # check to see if agent in mud
            agent_stuck_in_mud = self.check_agent_in_mud()
        if self.filehandle:
            self.filehandle.write(
                nbeacons_util.drawNBeaconsScene(self.world, rtn_str=True) +
                "\n")
        if self.filehandle:
            self.filehandle.write(
                "----------------------------------------------\n")
        if self.filehandle: self.filehandle.flush()