Files
SkillCompiler/data/skills-bench/tasks/pddl-airport-planning/environment/solve.py
T
2026-09-04 14:58:42 +08:00

61 lines
1.9 KiBLFS
Python

import pickle
# from unified_planning.engines import SequentialPlanValidator
from unified_planning.io import PDDLReader, PDDLWriter
from unified_planning.shortcuts import OneshotPlanner, PlanValidator
def solve_and_validate(domain_file, problem_file, plan_out="plan.txt"):
reader = PDDLReader()
problem = reader.parse_problem(domain_file, problem_file)
writer = PDDLWriter(problem)
# print('start of 1 problem')
# print("1 problem: ", problem)
# print('end of 1 problem')
# --- Solve ---
with OneshotPlanner(problem_kind=problem.kind) as planner:
result = planner.solve(problem)
print("planner.name: %s" % (planner.name))
if result.plan is None:
print("No plan found")
return False
# print('start of 2 problem')
# print("2 problem: ", problem)
# print('end of 2 problem')
plan = result.plan
print("plan.actions: ", plan.actions)
# --- Save plan ---
# with open(plan_out, "w") as f:
# for a in plan.actions:
# f.write(str(a) + "\n")
# name = a.action.name
# params = [p.name for p in a.actual_parameters]
# f.write("(" + name + " " + " ".join(params) + ")\n")
writer.write_plan(plan, filename=plan_out)
# with open(plan_out.replace(".txt", ".pkl"), "wb") as f:
# pickle.dump(plan, f)
# --- Validate ---
# validator = SequentialPlanValidator()
# val = validator.validate(problem, plan)
with PlanValidator(problem_kind=problem.kind, plan_kind=plan.kind) as validator:
val = validator.validate(problem, plan)
print('val: ', val)
if val.status.name == "VALID":
print("PLAN VALID")
return True
else:
print("PLAN INVALID:", val)
return False
if __name__ == "__main__":
ok = solve_and_validate("airport/domain02.pddl", "airport/task02.pddl")
exit(0 if ok else 1)