File size: 9,862 Bytes
483f96c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | import pdb
import ast
import multiprocessing
import re
import shlex
import subprocess
import time
import constants
from utils import game_util
from utils import py_util
DEBUG = False
CAPS_ACTION_TO_PLAN_ACTION = {
'GOTOLOCATION': 'GotoLocation',
'SCAN': 'Scan',
'OPENOBJECT': 'OpenObject',
'CLOSEOBJECT': 'CloseObject',
'PICKUPOBJECT': 'PickupObject',
'PICKUPOBJECTINRECEPTACLE1': 'PickupObjectInReceptacle',
'PICKUPOBJECTINRECEPTACLE2': 'PickupObjectInReceptacle',
'PICKUPRECEPTACLEOBJECTINRECEPTACLE1': 'PickupObjectInReceptacle',
'PICKUPRECEPTACLEOBJECTINRECEPTACLE2': 'PickupObjectInReceptacle',
'PICKUPOBJECTINOBJECT1': 'PickupObjectInObject',
'PICKUPOBJECTINOBJECT2': 'PickupObjectInObject',
'PUTOBJECTINRECEPTACLE1': 'PutObjectInReceptacle',
'PUTOBJECTINRECEPTACLE2': 'PutObjectInReceptacle',
'PUTOBJECTINRECEPTACLEOBJECT1': 'PutObjectInReceptacleObject',
'PUTOBJECTINRECEPTACLEOBJECT2': 'PutObjectInReceptacleObject',
'PUTRECEPTACLEOBJECTINRECEPTACLE1': 'PutReceptacleObjectInReceptacle',
'PUTRECEPTACLEOBJECTINRECEPTACLE2': 'PutReceptacleObjectInReceptacle',
'PICKUPOBJECTNORECEPTACLE': 'PickupObjectNoReceptacle',
'PUTOBJECT': 'PutObject',
'CLEANOBJECT': 'CleanObject',
'HEATOBJECT': 'HeatObject',
'TOGGLEOBJECT': 'ToggleObject',
'COOLOBJECT': 'CoolObject',
'SLICEOBJECT': 'SliceObject',
'REACH-GOAL': 'End'
}
LOWER_TO_FULL = {name.lower(): name for name in constants.OBJECTS}
def lower_to_full(input_str):
arr = input_str.split('|')
new_arr = []
for item in arr:
if item in LOWER_TO_FULL:
new_arr.append(LOWER_TO_FULL[item])
else:
new_arr.append(item)
return '|'.join(new_arr)
def parse_action_arg(action_arg):
action_arg = action_arg.lower()
action_arg = py_util.multireplace(action_arg,
{'_minus_': '-',
'-': '#',
'_bar_': '|',
'_plus_': '+',
'_dot_': '.',
'_comma_': ','})
action_arg = lower_to_full(action_arg)
return action_arg
def parse_line(line):
line = re.sub(r'^\s*step|\d+:\s*', '', line)
line = line.strip()
line_args = line.split(' ')
if line_args[0] not in CAPS_ACTION_TO_PLAN_ACTION:
return None
action = CAPS_ACTION_TO_PLAN_ACTION[line_args[0]]
if action == 'End':
return {'action': 'End', 'value': 1}
action_dict = {'action': action}
line_args = line_args[1:] # Remove action name from line_args
if action in {'GotoLocation', 'Scan'}:
action_arg = line_args[2].lower()
action_arg = py_util.multireplace(action_arg,
{'_minus_': '-',
'-': '#',
'_bar_': '|',
'_plus_': '+',
'_dot_': '.',
'_comma_': ','})
action_dict['location'] = action_arg
elif action in {'OpenObject', 'CloseObject', 'ToggleObject'}:
action_dict['objectId'] = parse_action_arg(line_args[2])
action_dict['receptacleObjectId'] = parse_action_arg(line_args[2])
elif action in {'HeatObject', 'CoolObject'}:
action_dict['receptacleObjectId'] = parse_action_arg(line_args[2])
elif action in {'PickupObjectInReceptacle', 'PickupObjectNoReceptacle'}:
action_dict['action'] = 'PickupObject'
action_dict['objectId'] = parse_action_arg(line_args[2])
if action == 'PickupObjectInReceptacle':
action_dict['receptacleObjectId'] = parse_action_arg(line_args[3])
elif action in {'SliceObject'}:
action_dict['objectId'] = parse_action_arg(line_args[2])
elif action in {'CleanObject'}:
action_dict['objectId'] = parse_action_arg(line_args[3])
action_dict['receptacleObjectId'] = parse_action_arg(line_args[2])
elif action in {'PutObjectInReceptacle',
'PutObjectInReceptacleObject',
'PutReceptacleObjectInReceptacle'}:
action_dict['action'] = 'PutObject'
action_dict['objectId'] = parse_action_arg(line_args[3])
action_dict['receptacleObjectId'] = parse_action_arg(line_args[4])
elif action in {'PickupObjectInObject'}:
action_dict['action'] = 'PickupObject'
return action_dict
def parse_plan(lines):
plan = []
for line in lines:
action_dict = parse_line(line)
if action_dict is not None:
plan.append(action_dict)
return plan
def parse_plan_from_file(self, path):
lines = [line for line in open(path)]
return self.parse_plan(lines)
def get_plan_from_file(args):
domain, filepath, solver_type = args
start_t = time.time()
try:
command = ('ff_planner/ff '
'-o %s '
'-s %d '
'-f %s ' % (domain, solver_type, filepath))
if DEBUG:
print(command)
planner_output = subprocess.check_output(shlex.split(command), timeout=300)
except subprocess.CalledProcessError as error:
# Plan is done
output_str = error.output.decode('utf-8')
if DEBUG:
print('output', output_str)
if ('goal can be simplified to FALSE' in output_str or
"won't get here: simplify, non logical" in output_str):
return [{'action': 'End', 'value': 0}]
elif 'goal can be simplified to TRUE' in output_str:
return [{'action': 'End', 'value': 1}]
elif len(output_str) == 0:
# Usually indicates segfault with ffplanner
# This happens when the goal needs an object that hasn't been seen yet like
# Q: "is there an egg in the garbage can," but no garbage can has been seen.
print('Empty plan')
print('Seg Fault')
return [{'action': 'End', 'value': 0}]
else:
print('problem', filepath)
print(output_str)
print('Empty plan')
return [{'action': 'End', 'value': 0}]
# except subprocess.TimeoutExpired:
# print('timeout solver', solver_type, 'problem', filepath)
# print('Empty plan')
# return ['timeout', {'action': 'End', 'value': 0}]
unparsed_plan = planner_output.decode('utf-8').split('\n')
if DEBUG:
print('unparsed', '\n'.join(unparsed_plan))
parsed_plan = parse_plan(unparsed_plan)
if constants.DEBUG:
print('planned %s in %.5f, plan length %d solver type %d' % (
filepath, time.time() - start_t, len(parsed_plan), solver_type))
if len(parsed_plan) == 0:
parsed_plan = [{'action': 'End', 'value': 1}]
return parsed_plan
# Example of how to call ff
# /path/to/Metric-FF-v2.1/ff -o planner/domains/Question_domain.pddl -f planner/exists_problem.pddl
def get_plan_async(args):
domain, problem_id, solver_type = args
filepath = '%s/planner/generated_problems/problem_%s.pddl' % (constants.LOG_FILE, problem_id)
return get_plan_from_file((domain, filepath, solver_type))
class PlanParser(object):
def __init__(self, domain_file_path):
self.domain = domain_file_path
self.problem_id = -1
self.process_pool = multiprocessing.Pool(3)
#from multiprocessing.pool import ThreadPool
#self.process_pool = ThreadPool(3)
def get_plan(self):
parsed_plans = self.process_pool.map(get_plan_async, zip([self.domain] * 3, [self.problem_id] * 3, range(3, 6)))
return self.find_best_plan(parsed_plans)
def get_plan_from_file(self, domain_path, filepath):
parsed_plans = self.process_pool.map(get_plan_from_file, zip([domain_path] * 3, [filepath] * 3, range(3, 6)))
return self.find_best_plan(parsed_plans)
# Unncessary, planner should be optimal. But the planner produces some weird actions
def clean_plan(self, plan):
cleaned_plan = list()
for i in range(len(plan)-1):
if not (plan[i]['action'] == 'GotoLocation' and plan[i+1]['action'] == 'GotoLocation'):
cleaned_plan.append(plan[i])
cleaned_plan.append(plan[len(plan)-1])
return cleaned_plan
def find_best_plan(self, parsed_plans):
if all([parsed_plan[0] == 'timeout' for parsed_plan in parsed_plans]):
parsed_plan = parsed_plans[0][1:]
else:
parsed_plans = [self.clean_plan(parsed_plan) for parsed_plan in parsed_plans if parsed_plan[0] != 'timeout']
parsed_plan = min(parsed_plans, key=len)
if constants.DEBUG:
print('plan\n' + '\n'.join(['%03d: %s' % (pp, game_util.get_action_str(pl))
for pp, pl in enumerate(parsed_plan)]))
else:
print('plan\n' + '\n'.join(['%03d: %s' % (pp, game_util.get_action_str(pl))
for pp, pl in enumerate(parsed_plan)]))
return parsed_plan
class SinglePlanParser(PlanParser):
def get_plan(self):
parsed_plan = get_plan_async([self.domain, self.problem_id, 3])
return parsed_plan
def get_plan_from_file(self, domain_path, filepath):
parsed_plan = get_plan_from_file([domain_path, filepath, 3])
return parsed_plan
if __name__ == '__main__':
import sys
DEBUG = constants.DEBUG
parser = PlanParser('planner/domains/PutTaskExtended_domain.pddl')
parser.problem_id = sys.argv[1]
result_plan = parser.get_plan()
print('plan\n' + '\n'.join(['%03d: %s' % (pp, game_util.get_action_str(pl)) for pp, pl in enumerate(result_plan)]))
|