00001 
00002 
00003 
00004 
00005 
00006 
00007 
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     
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         
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         
00049 
00050         
00051         if not allDomainsEqual:
00052             
00053             domain = os.path.join(dir, d, "domain.pddl")
00054             newDomain = os.path.join(dir, "d" + d + ".pddl")
00055             shutil.copyfile(domain, newDomain)
00056         
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         
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         
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     
00083 
00084     allResults = {}
00085     for root, dirs, files in os.walk(ref_dir):
00086         
00087         
00088         
00089         
00090         
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