00001 import pddl
00002
00003 class SASTask:
00004 def __init__(self, variables, init, goal, operators,
00005 temp_operators,axioms, num_axioms, comp_axioms, oplinit, objects, condition_modules, effect_modules, cost_modules,
00006 translation, module_inits, subplan_generators, init_constant_predicates, init_constant_numerics):
00007 self.variables = variables
00008 self.init = init
00009 self.goal = goal
00010 self.operators = operators
00011 self.temp_operators = temp_operators
00012 self.axioms = axioms
00013 self.num_axioms = num_axioms
00014 self.comp_axioms = comp_axioms
00015 self.oplinit = oplinit
00016 self.objects = objects
00017 self.condition_modules = condition_modules
00018 self.effect_modules = effect_modules
00019 self.cost_modules = cost_modules
00020 self.translation = translation
00021 self.module_inits = module_inits
00022 self.subplan_generators = subplan_generators
00023 self.init_constant_predicates = init_constant_predicates
00024 self.init_constant_numerics = init_constant_numerics
00025 def output(self, stream):
00026 self.variables.output(stream)
00027 print >> stream, "begin_oplinits"
00028 print >> stream, len(self.oplinit)
00029 for init in self.oplinit:
00030 print >> stream, init.init_function
00031 print >> stream, "end_oplinits"
00032 print >> stream, "begin_objects"
00033 print >> stream, len(self.objects)
00034 for object in self.objects:
00035 print >> stream, object.opl_print()
00036 print >> stream, "end_objects"
00037 self.translation.output(stream)
00038 print >> stream, "begin_constant_facts"
00039 print >> stream, "%d" % len(self.init_constant_predicates)
00040 for i in self.init_constant_predicates:
00041 print >> stream, "%s %d %s" % (i.predicate, len(i.args),
00042 " ".join(map(lambda o : o.name, i.args)))
00043 print >> stream, "%d" % len(self.init_constant_numerics)
00044 for i in self.init_constant_numerics:
00045 print >> stream, "%s %d %s %f" % (i.fluent.symbol, len(i.fluent.args),
00046 " ".join(map(lambda o : o.name, i.fluent.args)), i.expression.value)
00047 print >> stream, "end_constant_facts"
00048 print >> stream, "begin_modules"
00049 print >> stream, "%d" % len(self.module_inits)
00050 for mod_init in self.module_inits:
00051 print >> stream, "%s %d %s" % (mod_init.init_function, len(mod_init.parameters), " ".join(mod_init.parameters))
00052 print >> stream, "%d" % len(self.subplan_generators)
00053 for spg in self.subplan_generators:
00054 print >> stream, "%s %s %s" % (spg.genFn, spg.outputFn, spg.execFn)
00055 print >> stream, "%d" % len(self.condition_modules)
00056 for module in self.condition_modules:
00057 module.output(stream)
00058 print >> stream, "%d" % len(self.effect_modules)
00059 for module in self.effect_modules:
00060 module.output(stream)
00061 print >> stream, "%d" % len(self.cost_modules)
00062 for module in self.cost_modules:
00063 module.output(stream)
00064 print >> stream, "end_modules"
00065 self.init.output(stream)
00066 self.goal.output(stream)
00067 if len(self.operators) > 0:
00068 assert len(self.temp_operators) == 0
00069 print >> stream, len(self.operators)
00070 for op in self.operators:
00071 op.output(stream)
00072 else:
00073 print >> stream, len(self.temp_operators)
00074 for op in self.temp_operators:
00075 op.output(stream)
00076 print >> stream, len(self.axioms)
00077 for axiom in self.axioms:
00078 axiom.output(stream)
00079 print >> stream, len(self.comp_axioms)
00080 for axiom in self.comp_axioms:
00081 axiom.output(stream)
00082 print >> stream, len(self.num_axioms)
00083 for axiom in self.num_axioms:
00084 axiom.output(stream)
00085
00086 class SASVariables:
00087 def __init__(self, ranges, axiom_layers):
00088 self.ranges = ranges
00089 self.axiom_layers = axiom_layers
00090 def dump(self):
00091 for var, (rang, axiom_layer) in enumerate(zip(self.ranges, self.axiom_layers)):
00092 if axiom_layer != -1:
00093 axiom_str = " [axiom layer %d]" % axiom_layer
00094 else:
00095 axiom_str = ""
00096 print "v%d in {%s}%s" % (var, range(rang), axiom_str)
00097 def output(self, stream):
00098 print >> stream, "begin_variables"
00099 print >> stream, len(self.ranges)
00100 for var, (rang, axiom_layer) in enumerate(zip(self.ranges, self.axiom_layers)):
00101 print >> stream, "var%d %d %d" % (var, rang, axiom_layer)
00102 print >> stream, "end_variables"
00103
00104 class SASInit:
00105 def __init__(self, values):
00106 self.values = values
00107 def dump(self):
00108 for var, val in enumerate(self.values):
00109 if val != -1:
00110 print "v%d: %d" % (var, val)
00111 def output(self, stream):
00112 print >> stream, "begin_state"
00113 for val in self.values:
00114 print >> stream, val
00115 print >> stream, "end_state"
00116
00117 class SASGoal:
00118 def __init__(self, pairs):
00119 self.pairs = sorted(pairs)
00120 def dump(self):
00121 for var, val in self.pairs:
00122 print "v%d: %d" % (var, val)
00123 def output(self, stream):
00124 print >> stream, "begin_goal"
00125 print >> stream, len(self.pairs)
00126 for var, val in self.pairs:
00127 print >> stream, var, val
00128 print >> stream, "end_goal"
00129
00130 class SASOperator:
00131 def __init__(self, name, prevail, pre_post, assign_effects):
00132 self.name = name
00133 self.prevail = sorted(prevail)
00134 self.pre_post = sorted(pre_post)
00135 self.assign_effects = assign_effects
00136 def dump(self):
00137 print self.name
00138 print "Prevail:"
00139 for var, val in self.prevail:
00140 print " v%d: %d" % (var, val)
00141 print "Pre/Post:"
00142 for var, pre, post, cond in self.pre_post:
00143 if cond:
00144 cond_str = " [%s]" % ", ".join(["%d: %d" % tuple(c) for c in cond])
00145 else:
00146 cond_str = ""
00147 print " v%d: %d -> %d%s" % (var, pre, post, cond_str)
00148 def output(self, stream):
00149 print >> stream, "begin_operator"
00150 print >> stream, self.name[1:-1]
00151 print >> stream, len(self.prevail)
00152 for var, val in self.prevail:
00153 print >> stream, var, val
00154 num = len(self.pre_post) + len(self.assign_effects)
00155 print >> stream, num
00156 for var, pre, post, cond in self.pre_post:
00157 print >> stream, len(cond),
00158 for cvar, cval in cond:
00159 print >> stream, cvar, cval,
00160 print >> stream, var, pre, post
00161 for assignment in self.assign_effects:
00162 assignment.output(stream)
00163 print >> stream, "end_operator"
00164
00165 class SASTemporalOperator:
00166 def __init__(self, name, duration, prevail, pre_post, assign_effects, mod_effects):
00167 self.name = name
00168
00169
00170
00171
00172
00173
00174 assert (len(duration[1]) == 0 and len(duration[0]) == 1
00175 and duration[0][0].op == "="), \
00176 "unsupported duration constraint"
00177 self.duration = duration
00178 self.prevail = prevail
00179 self.pre_post = pre_post
00180 self.assign_effects = assign_effects
00181 self.module_effects = mod_effects
00182 def output(self, stream):
00183 print >> stream, "begin_operator"
00184 print >> stream, self.name[1:-1]
00185 self.duration[0][0].output(stream)
00186 for time in range(3):
00187 print >> stream, len(self.prevail[time])
00188 for var, val in self.prevail[time]:
00189 print >> stream, var, val
00190 for time in range(2):
00191 num = len(self.pre_post[time]) + len(self.assign_effects[time]) + len(self.module_effects[time])
00192 print >> stream, num
00193 for var, pre, post, cond in self.pre_post[time]:
00194 for cond_time in range(3):
00195 print >> stream, len(cond[cond_time]),
00196 for cvar, cval in cond[cond_time]:
00197 print >> stream, cvar, cval,
00198 print >> stream, var, pre, post
00199 for assignment in self.assign_effects[time]:
00200 assignment.output(stream)
00201 mod_eff = self.module_effects[time]
00202 for me, cond in mod_eff.iteritems():
00203 for c in cond:
00204
00205 for cond_time in range(3):
00206 print >> stream, len(c[cond_time]),
00207 for cvar, cval in c[cond_time]:
00208 print >> stream, cvar, cval,
00209 print >> stream, "me-%d" % me
00210 print >> stream, "end_operator"
00211
00212 class SASDuration:
00213 def __init__(self, op, var):
00214 self.op = op
00215 self.var = var
00216 def output(self, stream):
00217 print >> stream, self.op, self.var
00218
00219 class SASAssignmentEffect:
00220 def __init__(self, var, op, valvar, prevail, temporal=False):
00221 self.var = var
00222 self.op = op
00223 self.valvar = valvar
00224 self.prevail = prevail
00225 self.temporal = temporal
00226 def output(self, stream):
00227 if self.temporal:
00228 for time in range(3):
00229 print >> stream, len(self.prevail[time]),
00230 for var, val in self.prevail[time]:
00231 print >> stream, var, val,
00232 else:
00233 print >> stream, len(self.prevail),
00234 for var, val in self.prevail:
00235 print >> stream, var, val,
00236 print >> stream, self.var, self.op, self.valvar
00237
00238 class SASAxiom:
00239 def __init__(self, condition, effect):
00240 self.condition = condition
00241 self.effect = effect
00242 assert self.effect[1] in (0, 1)
00243
00244 for _, val in condition:
00245 assert val >= 0, condition
00246 def dump(self):
00247 print "Condition:"
00248 for var, val in self.condition:
00249 print " v%d: %d" % (var, val)
00250 print "Effect:"
00251 var, val = self.effect
00252 print " v%d: %d" % (var, val)
00253 def output(self, stream):
00254 print >> stream, "begin_rule"
00255 print >> stream, len(self.condition)
00256 for var, val in self.condition:
00257 print >> stream, var, val
00258 var, val = self.effect
00259 print >> stream, var, 1 - val, val
00260 print >> stream, "end_rule"
00261
00262 class SASCompareAxiom:
00263 def __init__(self, comp, parts, effect):
00264 self.comp = comp
00265 self.parts = parts
00266 self.effect = effect
00267 def dump(self):
00268 values = (self.effect, self.comp,
00269 " ".join([str(var) for var in self.parts]))
00270 print "v%d: %s %s" % values
00271 def output(self, stream):
00272 values = (self.effect, self.comp,
00273 " ".join([str(var) for var in self.parts]))
00274 print >> stream, "%d %s %s" % values
00275
00276 class SASNumericAxiom:
00277 def __init__(self, op, parts, effect):
00278 self.op = op
00279 self.parts = parts
00280 self.effect = effect
00281
00282 def dump(self):
00283 values = (self.effect, self.op,
00284 " ".join([str(var) for var in self.parts]))
00285 print "v%d: %s %s" % values
00286 def output(self, stream):
00287 values = (self.effect, self.op,
00288 " ".join([str(var) for var in self.parts]))
00289 print >> stream, "%d %s %s" % values
00290
00291 class SASConditionModule:
00292 def __init__(self, modulecall, parameters, var):
00293 self.modulecall = modulecall
00294 self.parameters = parameters
00295 self.var = var
00296 def output(self, stream):
00297 print >> stream, "%s %d" % (self.modulecall, len(self.parameters)),
00298 for param in self.parameters:
00299 print >> stream, "%s %s %s" % (param[0], param[1], param[2]),
00300 print >> stream, "%d" % self.var
00301
00302 class SASEffectModule:
00303 def __init__(self, modulecall, parameters, effect_num, effect_vars):
00304 self.modulecall = modulecall
00305 self.parameters = parameters
00306 self.effect_num = effect_num
00307 self.effect_vars = effect_vars
00308 def output(self, stream):
00309 print >> stream, "%s %d" % (self.modulecall, len(self.parameters)),
00310 for param in self.parameters:
00311 print >> stream, "%s %s %s" % (param[0], param[1], param[2]),
00312 print >> stream, "me-%d" % self.effect_num,
00313 print >> stream, "%d" % len(self.effect_vars),
00314 for eff_var in self.effect_vars:
00315 print >> stream, "%d" % eff_var,
00316 print >> stream, ""
00317
00318 class SASTranslation:
00319 def __init__(self, dict):
00320 self.dict = dict
00321 def output(self, stream):
00322 print >> stream, "begin_pddl_translation"
00323 keys = self.dict.keys()
00324 keys.sort(lambda x,y:cmp(str(x), str(y)))
00325 predKeys = [key for key in keys if isinstance(key, pddl.Atom)
00326 and not key.predicate.startswith("defined!") and not key.predicate.startswith("new-axiom@")]
00327 pneKeys = [key for key in keys
00328 if isinstance(key, pddl.PrimitiveNumericExpression)
00329 and not key.symbol.startswith("derived!")]
00330 constPneKeys = [key for key in pddl.Task.CONSTANT_MAPPING.keys()]
00331 constPneKeys.sort(lambda x,y:cmp(str(x), str(y)))
00332 print >> stream, "%d" % len(predKeys)
00333 for key in predKeys:
00334 print >> stream, "%s %d" % (key.predicate, len(key.args)),
00335 for arg in key.args:
00336 print >> stream, "%s" % arg.name,
00337 vals = self.dict[key]
00338 assert len(vals) == 1
00339 for val in vals:
00340 print >> stream, val[0], val[1]
00341 print >> stream, "%d" % (len(pneKeys) + len(constPneKeys))
00342 for key in pneKeys:
00343 print >> stream, "%s %d" % (key.symbol, len(key.args)),
00344 for arg in key.args:
00345 print >> stream, "%s" % arg.name,
00346 vals = self.dict[key]
00347 assert len(vals) == 1
00348 for val in vals:
00349 print >> stream, val[0]
00350 for key in constPneKeys:
00351 vals = self.dict[pddl.Task.CONSTANT_MAPPING[key]]
00352 print >> stream, "%s" % key,
00353 assert len(vals) == 1
00354 for val in vals:
00355 print >> stream, val[0]
00356 print >> stream, "end_pddl_translation"