convert_ref_data.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Take ref data in the format of 
00004 # some-dirs: domain-name: 01/ 02/ 03/...
00005 # where each numbered dir will contain a file plan.soln.1
00006 # copy the file 04/plan.soln.1 to plan.p04.best 
00007 # and create a times.p04.pddl with the makespan of plan.soln.1 and invalid timestamp
00008 
00009 from __future__ import with_statement
00010 
00011 from optparse import OptionParser
00012 import os
00013 import subprocess
00014 import shutil
00015 import collections
00016 import datetime
00017 import re
00018 
00019 from plan_file_parser import parsePlan
00020 from plan_file_parser import makespanFromPlan
00021 
00022 def convertDomainDir(dir, probdirs):
00023     """ Each probdir should contain:
00024         domain.pddl, problem.pddl, plan.soln.1 """
00025     print "Converting ", dir
00026     
00027     # first figure out, if we can just take the same domain
00028     allDomainsEqual = True
00029     for p in probdirs:
00030         domainx = os.path.join(dir, probdirs[0], "domain.pddl")
00031         domainy = os.path.join(dir, p, "domain.pddl")
00032         if not os.path.exists(domainx):
00033             continue
00034         if not os.path.exists(domainy):
00035             continue
00036         retcode = subprocess.call(["diff", "-q", domainx, domainy], stdout=subprocess.PIPE)
00037         if retcode != 0:
00038             allDomainsEqual = False
00039             break
00040     print "All domains equal:", allDomainsEqual
00041     if allDomainsEqual:
00042         # convert domain
00043         domain = os.path.join(dir, probdirs[0], "domain.pddl")
00044         newDomain = os.path.join(dir, "domain.pddl")
00045         shutil.copyfile(domain, newDomain)
00046 
00047     for d in probdirs:
00048         #print "Problem:", d
00049 
00050         # Convert domain/problem
00051         if not allDomainsEqual:
00052             # convert domain
00053             domain = os.path.join(dir, d, "domain.pddl")
00054             newDomain = os.path.join(dir, "d" + d + ".pddl")
00055             shutil.copyfile(domain, newDomain)
00056         # convert problem
00057         problem = os.path.join(dir, d, "problem.pddl")
00058         newProblem = os.path.join(dir, "p" + d + ".pddl")
00059         shutil.copyfile(problem, newProblem)
00060         
00061         # Convert plan
00062         plan = os.path.join(dir, d, "plan.soln.1")
00063         if not os.path.exists(plan):
00064             print "WARNING: No plan file at: " + plan
00065             continue
00066         newPlan = os.path.join(dir, "plan.p" + d + ".pddl.best")
00067         shutil.copyfile(plan, newPlan)
00068         # read makespan and create times file
00069         plan = parsePlan(newPlan)
00070         makespan = makespanFromPlan(plan)
00071         timesfile = os.path.join(dir, "times.p" + d + ".pddl")
00072         with open(timesfile, "w") as f:
00073             print >> f, "# makespan search_time(s)"
00074             print >> f, makespan, "-1"
00075  
00076 
00077 def convertRefData(ref_dir):
00078     """ Parse dir and all subdirectories to find domain dirs. 
00079         That is a directory that contains directories that only contain numbers,
00080         e.g. 01/ 02/ etc.
00081         Convert that dir to the eval format. """
00082     # List directories, a problem/domain directory contains one or more files with "pddl"
00083 
00084     allResults = {}
00085     for root, dirs, files in os.walk(ref_dir):
00086         #print "Root: ", root
00087         #print "Dirs: ", dirs
00088         #print "Files: ", files
00089         #print ""
00090         # If dirs contains a dir that is named "01" this is a domain dir
00091         reg = "^[0-9]+$"
00092         probs = [f for f in dirs if re.match(reg, f)]
00093         probs.sort()
00094         if probs:
00095             convertDomainDir(root, probs)
00096 
00097 def main():
00098     parser = OptionParser("usage: %prog PROBLEMS")
00099     parser.add_option("-r", "--ref-dir", dest="ref_dir", type="string", action="store")
00100     opts, args = parser.parse_args()
00101     print "Ref-data dir: %s" % opts.ref_dir
00102 
00103     convertRefData(opts.ref_dir)
00104 
00105 if __name__ == "__main__":
00106     main()
00107 


planner_benchmarks
Author(s): Multiple
autogenerated on Mon Oct 6 2014 07:51:52