def run_planner_on_obs(self, obs_index): # os.chdir(ROOT_DIR) obs_plans = [] obs_actions = [] for i in range(obs_index): hyp_problem = 'obs_%d_problem.pddl' % i self.modify_obs_file(i) # creating the derived problem with G_Obs trans_cmd = translation.Probabilistic_PR('domain.pddl', hyp_problem, 'obs.dat') trans_cmd.execute() os.system('mv prob-PR obs_prob-%s-PR' % i) for id, domain, instance in self.walk('obs_prob-%s-PR' % i): if id == "O": plan_for_G_Obs_cmd = planners.HSP(domain, instance, i) plan_for_G_Obs_cmd.execute() obs_actions = plan_for_G_Obs_cmd.get_plan() obs_plans.append(obs_actions) return obs_plans
def test_offline(self, index, max_time, max_mem, optimal=False, beta=1.0): # EXPLAIN: definition 1, (Ramirez & Geffner 2010) # generate the problem with G=H hyp_problem = 'hyp_%d_problem.pddl' % index self.generate_pddl_for_hyp_plan(hyp_problem) # EXPLAIN: definition 2, (Ramirez & Geffner 2010) # derive problem with G_Obs trans_cmd = translation.Probabilistic_PR('domain.pddl', hyp_problem, 'obs.dat') trans_cmd.execute() self.trans_time = trans_cmd.time os.system('mv prob-PR prob-%s-PR' % index) self.costs = dict() G_Obs_time = 0.0 min_cost = 1e7 time_bound = max_time # EXPLAIN: if optimal flag in options.py was risen if optimal: time_bound = max_time / 2 for id, domain, instance in self.walk('prob-%s-PR' % index): # EXPLAIN: creates an HSP planner plan_for_G_Obs_cmd = planners.HSP(domain, instance, index, time_bound, max_mem) # EXPLAIN: run the planner plan_for_G_Obs_cmd.execute() # EXPLAIN: calculate plans with obs if id == 'O': self.Plan_Time_O = plan_for_G_Obs_cmd.time # EXPLAIN: calculate complimentory plans (without obs) if id == 'neg-O': self.Plan_Time_Not_O = plan_for_G_Obs_cmd.time # EXPLAIN: update time G_Obs_time += plan_for_G_Obs_cmd.time self.costs[id] = plan_for_G_Obs_cmd.cost # EXPLAIN: update minimal cost if plan_for_G_Obs_cmd.cost < min_cost: min_cost = plan_for_G_Obs_cmd.cost # Save solution files if id == 'O' or id == 'neg-O': save_sol_file(id, index, "offline") # EXPLAIN: if optimal flag in options.py was not risen if not optimal: # time_bound = max_time / 3 # plan_for_G_cmd = planners.LAMA( 'domain.pddl', hyp_problem, index, time_bound, max_mem ) # plan_for_G_cmd.execute() # if plan_for_G_cmd.cost < min_cost : # min_cost = plan_for_G_cmd.cost # remainder = time_bound - plan_for_G_cmd.time # print >> sys.stdout, "Time remaining:", time_bound # if remainder > 0 : # time_bound = (max_time / 3 ) + (remainder / 2 ) time_bound = max_time / 2 # EXPLAIN: creates an LAMA planner for id, domain, instance in self.walk('prob-%s-PR' % index): plan_for_G_Obs_cmd = planners.LAMA(domain, instance, index, time_bound, max_mem) # EXPLAIN: run the planner plan_for_G_Obs_cmd.execute() # EXPLAIN: not failed, update time G_Obs_time += plan_for_G_Obs_cmd.time # EXPLAIN: calculate plans with obs if id == 'O': self.Plan_Time_O = plan_for_G_Obs_cmd.time # EXPLAIN: calculate complimentory plans (without obs) if id == 'neg-O': self.Plan_Time_Not_O = plan_for_G_Obs_cmd.time remainder = time_bound - plan_for_G_Obs_cmd.time if remainder > 0: time_bound = time_bound + remainder # EXPLAIN: update minimal cost self.costs[id] = plan_for_G_Obs_cmd.cost if plan_for_G_Obs_cmd.cost < min_cost: min_cost = plan_for_G_Obs_cmd.cost # Save solution files if id == 'O' or id == 'neg-O': save_sol_file(id, index, "offline") self.plan_time = G_Obs_time self.total_time = trans_cmd.time + self.plan_time # EXPLAIN: definition 4 and 5, (Ramirez & Geffner 2010) # P(O|G) / P( \neg O | G) = exp { -beta Delta(G,O) } # Delta(G,O) = cost(G,O) - cost(G,\neg O) # EXPLAIN: delta(G|O) try: likelihood_ratio = math.exp( -beta * (self.costs['O'] - self.costs['neg-O'])) except Exception: likelihood_ratio = 0 # likelihood_ratio = math.exp( -beta*(self.costs['O']-self.costs['neg-O']) ) # P(O|G) = exp { -beta Delta(G,O) } / 1 + exp { -beta Delta(G,O) } # EXPLAIN: (O|G) self.Probability_O = likelihood_ratio / (1.0 + likelihood_ratio) # EXPLAIN: (NEG-O|G) self.Probability_Not_O = 1.0 - self.Probability_O self.cost_O = self.costs['O'] self.cost_Not_O = self.costs['neg-O'] if "H" in self.name: path = os.getcwd() + "/" + 'pr-problem.soln' self.offline_ideal_cost = self.get_greedy_cost(path, self.name)
def test_online(self, index, obs_index, max_time, max_mem, optimal=False, beta=1.0): # generate the problem with G=H hyp_problem = 'hyp_%d_problem.pddl' % index self.generate_pddl_for_hyp_plan(hyp_problem) global count if obs_index > count: count += 1 self.curr_obs_num = count + 1 modify_obs_file(count) # creating the derived problem with G_Obs trans_cmd = translation.Probabilistic_PR('domain.pddl', hyp_problem, 'obs.dat') trans_cmd.execute() self.trans_time = trans_cmd.time os.system('mv prob-PR prob-%s-PR' % index) self.costs = dict() G_Obs_time = 0.0 min_cost = 1e7 time_bound = max_time # modifying init in pr_domain for id, domain, instance in self.walk('prob-%s-PR' % index): if id == 'O': self.modify_init_pr_problem_file(self.domain, instance) if optimal: time_bound = max_time / 2 for id, domain, instance in self.walk('prob-%s-PR' % index): plan_for_G_Obs_cmd = planners.HSP(domain, instance, index, time_bound, max_mem) plan_for_G_Obs_cmd.execute() if id == 'O': self.Plan_Time_O = plan_for_G_Obs_cmd.time if self.greedy: if not os.path.exists( "temp_results/ideal_temp_files/soln_files"): os.makedirs( "temp_results/ideal_temp_files/soln_files", mode=0777) command = "cp pr-problem.soln temp_results/ideal_temp_files/soln_files/pr-problem_" + str( index) + ".soln" os.system(command) if id == 'neg-O': self.Plan_Time_Not_O = plan_for_G_Obs_cmd.time G_Obs_time += plan_for_G_Obs_cmd.time self.costs[id] = plan_for_G_Obs_cmd.cost if plan_for_G_Obs_cmd.cost < min_cost: min_cost = plan_for_G_Obs_cmd.cost # Save solution files if id == 'O' or id == 'neg-O': save_sol_file(id, index, "online", obs_index) self.path = plan_for_G_Obs_cmd.get_plan() if not optimal: #time_bound = max_time / 3 #plan_for_G_cmd = planners.LAMA( 'domain.pddl', hyp_problem, index, time_bound, max_mem ) #plan_for_G_cmd.execute() #if plan_for_G_cmd.cost < min_cost : # min_cost = plan_for_G_cmd.cost #remainder = time_bound - plan_for_G_cmd.time #print >> sys.stdout, "Time remaining:", time_bound #if remainder > 0 : # time_bound = (max_time / 3 ) + (remainder / 2 ) time_bound = max_time / 2 for id, domain, instance in self.walk('prob-%s-PR' % index): plan_for_G_Obs_cmd = planners.LAMA(domain, instance, index, time_bound, max_mem) plan_for_G_Obs_cmd.execute() G_Obs_time += plan_for_G_Obs_cmd.time if id == 'O': self.Plan_Time_O = plan_for_G_Obs_cmd.time if self.greedy: if not os.path.exists( "temp_results/ideal_temp_files/soln_files"): os.makedirs( "temp_results/ideal_temp_files/soln_files", mode=0777) command = "cp pr-problem.soln temp_results/ideal_temp_files/soln_files/pr-problem_" + str( index) + ".soln" os.system(command) if id == 'neg-O': self.Plan_Time_Not_O = plan_for_G_Obs_cmd.time remainder = time_bound - plan_for_G_Obs_cmd.time if remainder > 0: time_bound = time_bound + remainder self.costs[id] = plan_for_G_Obs_cmd.cost if plan_for_G_Obs_cmd.cost < min_cost: min_cost = plan_for_G_Obs_cmd.cost # Save solution files if id == 'O' or id == 'neg-O': save_sol_file(id, index, "online", obs_index) self.plan_time = G_Obs_time self.total_time = trans_cmd.time + self.plan_time # P(O|G) / P( \neg O | G) = exp { -beta Delta(G,O) } # Delta(G,O) = cost(G,O) - cost(G,\neg O) try: likelihood_ratio = math.exp( -beta * (self.costs['O'] - self.costs['neg-O'])) except Exception: likelihood_ratio = 0 # likelihood_ratio = math.exp( -beta*(self.costs['O']-self.costs['neg-O']) ) # P(O|G) = exp { -beta Delta(G,O) } / 1 + exp { -beta Delta(G,O) } self.Probability_O = likelihood_ratio / (1.0 + likelihood_ratio) self.Probability_Not_O = 1.0 - self.Probability_O self.cost_O = self.costs['O'] self.cost_Not_O = self.costs['neg-O'] if "H" in self.name: path = os.getcwd() + "/" + 'pr-problem.soln' self.offline_ideal_cost = self.get_greedy_cost(path, self.name)
def test(self, index, max_time, max_mem, optimal=False, beta=1.0): import math, csv # generate the problem with G=H hyp_problem = 'hyp_%d_problem.pddl' % index self.generate_pddl_for_hyp_plan(hyp_problem) # derive problem with G_Obs trans_cmd = translation.Probabilistic_PR('domain.pddl', hyp_problem, 'obs.dat') trans_cmd.execute() self.trans_time = trans_cmd.time os.system('mv prob-PR prob-%s-PR' % index) self.costs = dict() G_Obs_time = 0.0 min_cost = 1e7 time_bound = max_time if optimal: time_bound = max_time / 2 for id, domain, instance in self.walk('prob-%s-PR' % index): plan_for_G_Obs_cmd = planners.HSP(domain, instance, index, time_bound, max_mem) plan_for_G_Obs_cmd.execute() if id == 'O': self.Plan_Time_O = plan_for_G_Obs_cmd.time if id == 'neg-O': self.Plan_Time_Not_O = plan_for_G_Obs_cmd.time G_Obs_time += plan_for_G_Obs_cmd.time self.costs[id] = plan_for_G_Obs_cmd.cost if plan_for_G_Obs_cmd.cost < min_cost: min_cost = plan_for_G_Obs_cmd.cost if not optimal: #time_bound = max_time / 3 #plan_for_G_cmd = planners.LAMA( 'domain.pddl', hyp_problem, index, time_bound, max_mem ) #plan_for_G_cmd.execute() #if plan_for_G_cmd.cost < min_cost : # min_cost = plan_for_G_cmd.cost #remainder = time_bound - plan_for_G_cmd.time #print >> sys.stdout, "Time remaining:", time_bound #if remainder > 0 : # time_bound = (max_time / 3 ) + (remainder / 2 ) time_bound = max_time / 2 for id, domain, instance in self.walk('prob-%s-PR' % index): plan_for_G_Obs_cmd = planners.FastDownward_LAMA( domain, instance, index, time_bound, max_mem ) #Michael changed standard Lama to Fastdownward Lama plan_for_G_Obs_cmd.execute() G_Obs_time += plan_for_G_Obs_cmd.time if id == 'O': self.Plan_Time_O = plan_for_G_Obs_cmd.time if id == 'neg-O': self.Plan_Time_Not_O = plan_for_G_Obs_cmd.time remainder = time_bound - plan_for_G_Obs_cmd.time if remainder > 0: time_bound = time_bound + remainder self.costs[id] = plan_for_G_Obs_cmd.cost if plan_for_G_Obs_cmd.cost < min_cost: min_cost = plan_for_G_Obs_cmd.cost self.plan_time = G_Obs_time self.total_time = trans_cmd.time + self.plan_time # P(O|G) / P( \neg O | G) = exp { -beta Delta(G,O) } # Delta(G,O) = cost(G,O) - cost(G,\neg O) likelihood_ratio = math.exp(-beta * (self.costs['O'] - self.costs['neg-O'])) # P(O|G) = exp { -beta Delta(G,O) } / 1 + exp { -beta Delta(G,O) } self.Probability_O = likelihood_ratio / (1.0 + likelihood_ratio) self.Probability_Not_O = 1.0 - self.Probability_O self.cost_O = self.costs['O'] self.cost_Not_O = self.costs['neg-O']