def make_pr_domain_file(self): hyp_problem = 'hyp_%d_problem.pddl' % 9999 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() path = os.getcwd() + "/prob-PR/O/pr-domain.pddl " + os.getcwd() os.system("mv " + path) path = os.getcwd() + "/prob-PR/O/pr-problem.pddl " + os.getcwd() os.system("mv " + path) # erase folder after done unpacking os.system("rm " + "prob-PR")
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 __init__(self, domain_name, hyps=[]): self.domain_name = domain_name self.has_duplicates = False self.actions = [] self.predicates = [] self.actions_dict = {} self.predicates_dict = {} # actions done in hyp path self.fulfilled_actions = [] self.fulfilled_predicates = [] self.seen_in_path_actions = [] self.actions_in_cost = [] self.cost = 0 # actions done in obs path self.obs_fulfilled_actions = [] self.obs_fulfilled_predicates = [] self.obs_seen_actions = [] self.obs_in_cost = [] self.obs_in_process_actions = [] self.obs_in_process_preconditions = [] self.obs_cost = 0 self.data_saved = {} self.temp_fullfilled = [] self.in_process_actions = [] self.in_process_preconditions = [] # creating full pr-domain file trans_cmd = translation.Probabilistic_PR('domain.pddl', 'pr-domain.pddl', 'obs.dat') trans_cmd.execute() # parse the domain to our format if "intrusion" in domain_name or "block" in domain_name: self.intrusion() elif "easy" in domain_name or "bui" in domain_name or "logistics" in domain_name: self.easy()
def test_for_sim(self, index, options): import math, csv #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.convert_to_integers = True trans_cmd.factor = 1000.0 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 # EXPLAIN: creates appropriate planner according to the flags in option.py file for id, domain, instance in self.walk('prob-%s-PR' % index): if options.optimal: plan_for_G_Obs_cmd = planners.H2(domain, instance, index, options.max_time, options.max_memory) else: if options.use_hspr: plan_for_G_Obs_cmd = planners.HSPr(domain, instance, index, options.max_time, options.max_memory) elif options.use_FF: plan_for_G_Obs_cmd = planners.Metric_FF( domain, instance, index, options.max_time, options.max_memory) else: plan_for_G_Obs_cmd = planners.LAMA(domain, instance, index, options.max_time, options.max_memory) self.greedy = True # EXPLAIN: run the planner plan_for_G_Obs_cmd.execute() # EXPLAIN: return if fails if plan_for_G_Obs_cmd.signal != 0 and plan_for_G_Obs_cmd.signal != 256: self.test_failed = True return # 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 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) # EXPLAIN: calculate complimentory plans (without obs) if id == 'neg-O': self.Plan_Time_Not_O = plan_for_G_Obs_cmd.time # EXPLAIN: update minimal cost self.costs[id] = plan_for_G_Obs_cmd.cost / trans_cmd.factor if self.costs[id] < min_cost: min_cost = self.costs[id] print >> sys.stdout, "Min Cost:", min_cost print >> sys.stdout, "Costs:", self.costs 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( -options.beta * (self.costs['O'] - self.costs['neg-O'])) except Exception: likelihood_ratio = 0 # likelihood_ratio = math.exp( -options.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']
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']
def test_for_sim(self, index, options): 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.convert_to_integers = True trans_cmd.factor = 1000.0 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 for id, domain, instance in self.walk('prob-%s-PR' % index): if options.optimal: plan_for_G_Obs_cmd = planners.H2(domain, instance, index, options.max_time, options.max_memory) else: if options.use_hspr: plan_for_G_Obs_cmd = planners.HSPr(domain, instance, index, options.max_time, options.max_memory) elif options.use_FF: plan_for_G_Obs_cmd = planners.Metric_FF( domain, instance, index, options.max_time, options.max_memory) else: plan_for_G_Obs_cmd = planners.LAMA( domain, instance, index, options.max_time, options.max_memory ) #Michael changed standard Lama to Fastdownward Lama plan_for_G_Obs_cmd.execute() if plan_for_G_Obs_cmd.signal != 0 and plan_for_G_Obs_cmd.signal != 256: self.test_failed = True return 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 self.costs[id] = plan_for_G_Obs_cmd.cost / trans_cmd.factor if self.costs[id] < min_cost: min_cost = self.costs[id] print >> sys.stdout, "Min Cost:", min_cost print >> sys.stdout, "Costs:", self.costs 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(-options.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']