run_eval.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from optparse import OptionParser
00004 import os
00005 import subprocess
00006 import shutil
00007 
00008 # Perform an evaluation run
00009 
00010 class Configuration(object):
00011     def __init__(self, configfile):
00012         assert(len(configfile) > 0)
00013         self.config = configfile
00014         # for file /bla/myconf.dat the name is myconf
00015         root,ext = os.path.splitext(configfile)
00016         assert(len(root) > 0)
00017         self.name = os.path.basename(root)
00018         assert(len(self.name) > 0)
00019     def dump(self):
00020         print "Config \"%s\" at %s" % (self.name, self.config)
00021     def set(self):
00022         """ Set config in ROS for planner call """
00023         cmd = "rosparam load %s /tfd_modules" % self.config
00024         code = os.system(cmd)
00025         assert(code == 0)
00026 
00027 class Problem(object):
00028     def __init__(self, path, domain, problem):
00029         self.domain = domain
00030         self.problem = problem
00031         self.path = path
00032     def dump(self):
00033         print "Base path: %s Domain: %s, Problem: %s" % (self.path, self.domain, self.problem)
00034     def run(self, outdir):
00035         # create dir for results as outdir + relative path
00036         probDir = os.path.join(outdir, self.path)
00037         if not os.path.exists(probDir):
00038             os.makedirs(probDir)
00039         # create cmd
00040         # get problem base path from rospack find planner_benchmarks
00041         pipe = subprocess.Popen(["rospack", "find", "planner_benchmarks"], stdout=subprocess.PIPE)
00042         base_path = pipe.communicate()[0].strip()
00043 
00044         domain_path = os.path.join(base_path, self.path, self.domain)
00045         problem_path = os.path.join(base_path, self.path, self.problem)
00046 
00047         # set the tfd_modules/plan param for output
00048         cmd = "rosparam set /tfd_modules/plan_name %s" % (os.path.join(outdir, self.path, "plan." + self.problem))
00049         code = os.system(cmd)
00050         assert(code == 0)
00051         cmd = "rosparam set /tfd_modules/time_debug_file %s" % (os.path.join(outdir, self.path, "times." + self.problem))
00052         code = os.system(cmd)
00053         assert(code == 0)
00054 
00055         print "Exec run for %s, %s" % (domain_path, problem_path)
00056         #cmd = "rosrun tfd_modules tfd_plan_eval %s %s plan.%s 3600 %s" % \
00057          #   (domain_path, problem_path, self.problem, probDir)
00058         cmd = "rosrun tfd_modules tfd_plan %s %s" % \
00059             (domain_path, problem_path)
00060 
00061         # run it
00062         code = os.system(cmd)
00063         print "%s run with %d" % (self.problem, code)
00064     def parse(line):
00065         dp = line.split()
00066         assert(len(dp) == 3)
00067         return Problem(dp[0], dp[1], dp[2])
00068     parse = staticmethod(parse)
00069 
00070 
00071 def main():
00072     parser = OptionParser("usage: %prog PROBLEMS")
00073     parser.add_option("-p", "--problems", dest="problems", type="string", action="store")
00074     # format: path domain problem [per line]
00075     parser.add_option("-r", "--results-dir", dest="results_dir", type="string", action="store")
00076     parser.add_option("-c", "--config", dest="config", type="string", action="store")
00077     opts, args = parser.parse_args()
00078     print "Problems file: %s" % opts.problems
00079     print "Results dir: %s" % opts.results_dir
00080 
00081     config = Configuration(opts.config)
00082     config.dump()
00083     config.set()
00084 
00085     problems = []
00086     with open(opts.problems) as f:
00087         for line in f:
00088             prob = Problem.parse(line)
00089             problems.append(prob)
00090     for i in problems:
00091         i.dump()
00092 
00093     if not os.path.exists(opts.results_dir):
00094         os.mkdir(opts.results_dir)
00095     outdir = os.path.join(opts.results_dir, config.name)
00096     if os.path.exists(outdir):
00097         print "WARNING outdir %s already exists!" % outdir
00098     else:
00099         os.mkdir(outdir)
00100 
00101     # save the config
00102     shutil.copy(config.config, outdir)
00103     # save the run script
00104     pipe = subprocess.Popen(["rospack", "find", "tfd_modules"], stdout=subprocess.PIPE)
00105     tfd_path = pipe.communicate()[0].strip()
00106     shutil.copy(os.path.join(tfd_path, "scripts/tfd_plan"), outdir)
00107 
00108     # run all problems
00109     for num, i in enumerate(problems):
00110         print "Running: %s: %s - %s (%.1f %%)" % (i.path, i.domain, i.problem, 100.0*num/len(problems))
00111         i.run(outdir)
00112 
00113 if __name__ == "__main__":
00114     main()
00115 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


planner_benchmarks
Author(s): Multiple
autogenerated on Tue Jan 22 2013 12:24:38