def queue_init(self): import strips2 import towers3 as towers import subprocess import strips2_show # solve for show (user can click through) subproc = subprocess.Popen(['python', 'BlocksTower/strips2.py'], stdout=subprocess.PIPE) plan = '' while True: try: out = subproc.stdout.read(1) except: break if out == '': break else: plan += out # actually solve to get the plan of actions plan = strips2.solve(towers.INIT, towers.GOAL, towers.ACTIONS) action_queue = [5] state = copy(towers.INIT) at = towers.Pole1 for (move, what, frm, to) in plan: frm_pole = strips2_show.get_pole(state, frm) to_pole = strips2_show.get_pole(state, to) print what, frm, to, at, frm_pole, to_pole if at != frm_pole: action_queue += MOVES[(at, frm_pole)] action_queue += CARRY_MOVES[(frm_pole, to_pole)] move(state, what, frm, to) at = to_pole return action_queue
def queue_init(self): import subprocess # solve for show (user can click through) subproc = subprocess.Popen([ 'python', 'BlocksTower/strips.py', 'BlocksTower/towers3_strips.txt' ], stdout=subprocess.PIPE) plan = '' while True: try: out = subproc.stdout.read(1) except: break if out == '': break else: plan += out print plan hl_actions = [] # high level action for line in plan.split('\n'): words = line.strip().split() if len(words) == 3: (what, frm, to) = words hl_actions.append((what, frm, to)) # use strips2 stuff for translating the output into low level actions import strips2 import towers3 as towers import strips2_show action_queue = [5] state = copy(towers.INIT) at = towers.Pole1 for (what, frm, to) in hl_actions: frm_pole = strips2_show.get_pole(state, frm) to_pole = strips2_show.get_pole(state, to) print what, frm, to, at, frm_pole, to_pole if at != frm_pole: action_queue += MOVES[(at, frm_pole)] action_queue += CARRY_MOVES[(frm_pole, to_pole)] towers.Move(state, what, frm, to) at = to_pole return action_queue
def queue_init(self): import subprocess # solve for show (user can click through) subproc = subprocess.Popen(['python', 'BlocksTower/strips.py', 'BlocksTower/towers3_strips.txt'], stdout=subprocess.PIPE) plan = '' while True: try: out = subproc.stdout.read(1) except: break if out == '': break else: plan += out print plan hl_actions = [] # high level action for line in plan.split('\n'): words = line.strip().split() if len(words) == 3: (what, frm, to) = words hl_actions.append((what, frm, to)) # use strips2 stuff for translating the output into low level actions import strips2 import towers3 as towers import strips2_show action_queue = [5] state = copy(towers.INIT) at = towers.Pole1 for (what, frm, to) in hl_actions: frm_pole = strips2_show.get_pole(state, frm) to_pole = strips2_show.get_pole(state, to) print what, frm, to, at, frm_pole, to_pole if at != frm_pole: action_queue += MOVES[(at, frm_pole)] action_queue += CARRY_MOVES[(frm_pole, to_pole)] towers.Move(state, what, frm, to) at = to_pole return action_queue
def queue_init(self): import subprocess # solve for show (user can click through) subproc = subprocess.Popen(['python', 'BlocksTower/text_interface.py'], stdout=subprocess.PIPE) plan = '' while True: try: out = subproc.stdout.read(1) except: break if out == '': break else: plan += out print plan parsed_plan = self.semantic_parser(plan) if (self.action_queue == [10]): action_queue = [5] else: action_queue = [] state = self.global_state at = self.global_at # use strips2 stuff for translating the output into low level actions import strips2 import towers2 as towers import strips2_show words = parsed_plan.strip().split() (command) = words[0] if command == 'Mov': (what, frm, to) = words[1:] frm_pole = strips2_show.get_pole(state, frm) to_pole = strips2_show.get_pole(state, to) print what, frm, to, at, frm_pole, to_pole if at != frm_pole: action_queue += MOVES[(at, frm_pole)] action_queue += CARRY_MOVES[(frm_pole, to_pole)] towers.Move(state, what, frm, to) at = to_pole elif command == 'Pick': (what, frm) = words[1:] frm_pole = strips2_show.get_pole(state, frm) if at != frm_pole: action_queue += MOVES[(at, frm_pole)] action_queue += [3] #Pick up towers.Move(state, what, frm, frm) at = frm_pole elif command == 'Put': (what, to) = words[1:] to_pole = strips2_show.get_pole(state, to) if at != to_pole: action_queue += MOVES[(at, to_pole)] action_queue += [2] #Put Down towers.Move(state, what, to, to) at = to_pole self.global_state = state self.global_at = at return action_queue
def queue_init(self): import subprocess # solve for show (user can click through) subproc = subprocess.Popen(['python', 'BlocksTower/text_interface.py'], stdout=subprocess.PIPE) parsed_plan = '' while True: try: out = subproc.stdout.read(1) except: break if out == '': break else: parsed_plan += out parsed_plan = parsed_plan.splitlines() print parsed_plan if (self.action_queue == [10]): action_queue = [5] else: action_queue = [] state = self.global_state at = self.global_at # use strips2 stuff for translating the output into low level actions import strips2 import towers3 as towers import strips2_show #Get a count of how many commands to parse, of the parser didn't return anything then make sure we don't try to process and commands if (len(parsed_plan) == 0): return [] #the plan could be a list of strings or a string, if it's a string then put it in a single element list if (type(parsed_plan) == str): parsed_plan = [parsed_plan] number_of_commands = len(parsed_plan) log_info( parsed_plan, "Plan appears to contain {0} commands.".format(number_of_commands)) for command_number in range(number_of_commands): words = parsed_plan[command_number].strip().split() (command) = words[0] #check and see if this command string is value i.e. 'Mov disk pole pole', 'Pick, disk pole', etc if command == 'Mov': if len(words) != 4: log_error( parsed_plan[command_number], "Invalid command string, the \'Mov\' command expects the format Mov disk from_pole to_pole" ) continue #check the args elif not (words[1][0:4] == 'Disk' and (words[1][4] == '1' or words[1][4] == '2' or words[1][4] == '3')): log_error( parsed_plan[command_number], "Invalid command string, the \'Mov\' command expects the format Mov disk from_pole to_pole" ) continue elif not (words[2][0:4] == 'Pole' and (words[2][4] == '1' or words[2][4] == '2' or words[2][4] == '3')): log_error( parsed_plan[command_number], "Invalid command string, the \'Mov\' command expects the format Mov disk from_pole to_pole" ) continue elif not (words[3][0:4] == 'Pole' and (words[3][4] == '1' or words[3][4] == '2' or words[3][4] == '3')): log_error( parsed_plan[command_number], "Invalid command string, the \'Mov\' command expects the format Mov disk from_pole to_pole" ) continue elif (command == 'Pick') or (command == 'Put'): if len(words) != 3: log_error( parsed_plan[command_number], "Invalid command string, the \'Pick\' or \'Put\' command expects the format Mov disk from/to_pole" ) continue #check the args elif not (words[1][0:4] == 'Disk' and (words[1][4] == '1' or words[1][4] == '2' or words[1][4] == '3')): log_error( parsed_plan[command_number], "Invalid command string, the \'Pick\' or \'Put\' command expects the format Mov disk from/to_pole" ) continue elif not (words[2][0:4] == 'Pole' and (words[2][4] == '1' or words[2][4] == '2' or words[2][4] == '3')): log_error( parsed_plan[command_number], "Invalid command string, the \'Pick\' or \'Put\' command expects the format Mov disk from/to_pole" ) continue #if we got this far the action must be good, but make sure it is legal in the current state before trying to do it if command == 'Mov': #check if we are holding something if (self.source_of_disk_in_hand != None): log_error( "Can't pick up another disk, because we are alreadying holding one." ) break (what, frm, to) = words[1:] frm_pole = strips2_show.get_pole(state, frm) to_pole = strips2_show.get_pole(state, to) print what, frm, to, at, frm_pole, to_pole #do a sanity check before doing anything #! make sure that what we are picking up is where it is supposed to be and that it is on top if not (towers.Clear(what) in state): log_error( words, "Can't pick up {0}, because there is something on it". format(what)) break elif not (self.check_on_pole(what, frm_pole, state)): log_error( words, "Can't pick up {0} because it's not at the specified location {1}" .format(what, frm_pole)) break #now check if the destination pole is clear, or if it is not, whether the disk(s) on it are largers elif not (self.check_can_put(what, to_pole, state)): log_error( words, "Can't put {0} on {1}, because a smaller disk is already there." .format(what, to_pole)) break #now that everything looks okay, perform the nessecary actions if at != frm_pole: action_queue += MOVES[(at, frm_pole)] action_queue += CARRY_MOVES[(frm_pole, to_pole)] if towers.Move(state, what, self.get_object_below(what, state), self.get_top_object(to_pole, state)): print "move successful" at = to_pole elif command == 'Pick': #check if we are holding something if (self.source_of_disk_in_hand != None): log_error( "Can't pick up another disk, because we are alreadying holding one." ) break (what, frm) = words[1:] frm_pole = strips2_show.get_pole(state, frm) #do a sanity check before doing anything #! make sure that what we are picking up is where it is supposed to be and that it is on top if not (towers.Clear(what) in state): log_error( words, "Can't pick up {0}, because there is something on it". format(what)) break if not (self.check_on_pole(what, frm_pole, state)): log_error( words, "Can't pick up {0} because it's not at the specified location {1}" .format(what, frm_pole)) break if at != frm_pole: action_queue += MOVES[(at, frm_pole)] action_queue += [3] #Pick up self.source_of_disk_in_hand = self.get_object_below( what, state ) #store where the disk we are picking up came from so we can clean up the state when we put it down towers.Move(state, what, self.get_object_below(what, state), self.source_of_disk_in_hand) at = frm_pole elif command == 'Put': (what, to) = words[1:] to_pole = strips2_show.get_pole(state, to) #now check if the destination pole is clear, or if it is not, whether the disk(s) on it are largers if not (self.check_can_put(what, to_pole, state)): log_error( words, "Can't put {0} on {1}, because a smaller disk is already there." .format(what, to_pole)) break #everything looks okay, take the action if at != to_pole: action_queue += MOVES[(at, to_pole)] action_queue += [2] #Put Down towers.Move(state, what, self.source_of_disk_in_hand, self.get_top_object(to_pole, state)) self.source_of_disk_in_hand = None #clear the source of the disk we are holding, since we put it down at = to_pole self.global_state = state self.global_at = at return action_queue
def queue_init(self): import subprocess # solve for show (user can click through) subproc = subprocess.Popen(['python', 'BlocksTower/text_interface.py'], stdout=subprocess.PIPE) parsed_plan = '' while True: try: out = subproc.stdout.read(1) except: break if out == '': break else: parsed_plan += out parsed_plan = parsed_plan.splitlines() print parsed_plan if (self.action_queue == [10]): action_queue = [5] else: action_queue = [] state = self.global_state at = self.global_at # use strips2 stuff for translating the output into low level actions import strips2 import towers3 as towers import strips2_show #Get a count of how many commands to parse, of the parser didn't return anything then make sure we don't try to process and commands if (len(parsed_plan) == 0): return [] #the plan could be a list of strings or a string, if it's a string then put it in a single element list if (type(parsed_plan) == str): parsed_plan = [parsed_plan] number_of_commands = len(parsed_plan) log_info(parsed_plan, "Plan appears to contain {0} commands.".format(number_of_commands)) for command_number in range(number_of_commands): words = parsed_plan[command_number].strip().split() (command) = words[0] #check and see if this command string is value i.e. 'Mov disk pole pole', 'Pick, disk pole', etc if command == 'Mov': if len(words) != 4: log_error(parsed_plan[command_number], "Invalid command string, the \'Mov\' command expects the format Mov disk from_pole to_pole") continue #check the args elif not(words[1][0:4] == 'Disk' and (words[1][4] == '1' or words[1][4] == '2' or words[1][4] == '3')): log_error(parsed_plan[command_number], "Invalid command string, the \'Mov\' command expects the format Mov disk from_pole to_pole") continue elif not(words[2][0:4] == 'Pole' and (words[2][4] == '1' or words[2][4] == '2' or words[2][4] == '3')): log_error(parsed_plan[command_number], "Invalid command string, the \'Mov\' command expects the format Mov disk from_pole to_pole") continue elif not(words[3][0:4] == 'Pole' and (words[3][4] == '1' or words[3][4] == '2' or words[3][4] == '3')): log_error(parsed_plan[command_number], "Invalid command string, the \'Mov\' command expects the format Mov disk from_pole to_pole") continue elif (command == 'Pick') or (command == 'Put'): if len(words) != 3: log_error(parsed_plan[command_number], "Invalid command string, the \'Pick\' or \'Put\' command expects the format Mov disk from/to_pole") continue #check the args elif not(words[1][0:4] == 'Disk' and (words[1][4] == '1' or words[1][4] == '2' or words[1][4] == '3')): log_error(parsed_plan[command_number], "Invalid command string, the \'Pick\' or \'Put\' command expects the format Mov disk from/to_pole") continue elif not(words[2][0:4] == 'Pole' and (words[2][4] == '1' or words[2][4] == '2' or words[2][4] == '3')): log_error(parsed_plan[command_number], "Invalid command string, the \'Pick\' or \'Put\' command expects the format Mov disk from/to_pole") continue #if we got this far the action must be good, but make sure it is legal in the current state before trying to do it if command == 'Mov': #check if we are holding something if (self.source_of_disk_in_hand != None): log_error("Can't pick up another disk, because we are alreadying holding one.") break (what, frm, to) = words[1:] frm_pole = strips2_show.get_pole(state, frm) to_pole = strips2_show.get_pole(state, to) print what, frm, to, at, frm_pole, to_pole #do a sanity check before doing anything #! make sure that what we are picking up is where it is supposed to be and that it is on top if not(towers.Clear(what) in state): log_error(words, "Can't pick up {0}, because there is something on it".format(what)) break elif not(self.check_on_pole(what, frm_pole, state)): log_error(words, "Can't pick up {0} because it's not at the specified location {1}".format(what, frm_pole)) break #now check if the destination pole is clear, or if it is not, whether the disk(s) on it are largers elif not(self.check_can_put(what, to_pole, state)): log_error(words, "Can't put {0} on {1}, because a smaller disk is already there.".format(what, to_pole)) break #now that everything looks okay, perform the nessecary actions if at != frm_pole: action_queue += MOVES[(at, frm_pole)] action_queue += CARRY_MOVES[(frm_pole, to_pole)] if towers.Move(state, what, self.get_object_below(what, state), self.get_top_object(to_pole, state)): print "move successful" at = to_pole elif command == 'Pick': #check if we are holding something if (self.source_of_disk_in_hand != None): log_error("Can't pick up another disk, because we are alreadying holding one.") break (what, frm) = words[1:] frm_pole = strips2_show.get_pole(state, frm) #do a sanity check before doing anything #! make sure that what we are picking up is where it is supposed to be and that it is on top if not(towers.Clear(what) in state): log_error(words, "Can't pick up {0}, because there is something on it".format(what)) break if not(self.check_on_pole(what, frm_pole, state)): log_error(words, "Can't pick up {0} because it's not at the specified location {1}".format(what, frm_pole)) break if at != frm_pole: action_queue += MOVES[(at, frm_pole)] action_queue += [3] #Pick up self.source_of_disk_in_hand = self.get_object_below(what, state)#store where the disk we are picking up came from so we can clean up the state when we put it down towers.Move(state, what, self.get_object_below(what, state), self.source_of_disk_in_hand) at = frm_pole elif command == 'Put': (what, to) = words[1:] to_pole = strips2_show.get_pole(state, to) #now check if the destination pole is clear, or if it is not, whether the disk(s) on it are largers if not(self.check_can_put(what, to_pole, state)): log_error(words, "Can't put {0} on {1}, because a smaller disk is already there.".format(what, to_pole)) break #everything looks okay, take the action if at != to_pole: action_queue += MOVES[(at, to_pole)] action_queue += [2] #Put Down towers.Move(state, what, self.source_of_disk_in_hand, self.get_top_object(to_pole, state)) self.source_of_disk_in_hand = None#clear the source of the disk we are holding, since we put it down at = to_pole self.global_state = state self.global_at = at return action_queue