def imitate(domain, demo, state0, verbose=0): # Infer tasks with copct status, tlcovs, g = co.explain(domain.causes, demo, domain.M_causes, verbose=verbose > 0) tlcovs_ml, ml = co.minCardinalityTLCovers(tlcovs) _, ops, args = zip(*tlcovs_ml[0][0]) # arbitrarily use first ML-cover # Plan tasks with pyhop tasks = [(o, ) + a for (o, a) in zip(ops, args)] actions = pl.plan(domain, state0, tasks, verbose=verbose) # Execute plan success, states = pl.execute(state0, domain.htn_operators(), actions) return success, states, actions
def htn_methods(): # Stub, without this only copct part of ceril will work return dict(locals()) def htn_operators(): # Stub, without this only copct part of ceril will work return dict(locals()) if __name__ == "__main__": import copct as co import parse_demo as pd # load demo from baxter_corpus.demo_replace_red_with_spare_1 import demo as w # compute explanations status, tlcovs, g = co.explain(causes, w, M=M_causes, verbose=True) # Prune by minimum cardinality tlcovs_ml, ml = co.minCardinalityTLCovers(tlcovs) # Print results print("Observed") tasks = list(zip(*w))[1] print("len %d: %s" % (len(tasks), tasks)) print("Top-level covers:") for (u,_,_,_,_) in tlcovs_ml: states, tasks, args = zip(*u) print("len %d: %s" % (len(tasks), tasks)) print("Ratio: %d / %d" % (ml,len(w)))
def run_experiments(check_irr=True): results = {} # Dock maintenance demos demos = [ "demo_%s_%d" % (skill, di) for di in [1, 2] for skill in [ "remove_red_drive", "replace_red_with_green", "replace_red_with_spare", "swap_red_with_green" ] ] # Block stacking demos demos += ["demo_il", "demo_ai", "demo_um"] # Cover demos print("Covering demos...") for demo_name in demos: print(demo_name) # import demo and ground truth exec_str = "from baxter_corpus.%s import demo" % demo_name exec(exec_str, globals()) exec_str = "from baxter_corpus.%s_ground_truth import ground_truth" % demo_name exec(exec_str, globals()) # Cover and prune by each parsimony criterion results[demo_name] = {} start_time = time.clock() status, tlcovs, g = copct.explain(causes, demo, M=M) results[demo_name]["run_time"] = time.clock() - start_time results[demo_name]["tlcovs"], results[demo_name]["g"] = tlcovs, g results[demo_name]["tlcovs_mc"] = [ u for (u, _, _, _, _) in copct.minCardinalityTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_md"] = [ u for (u, _, _, _, _) in copct.maxDepthTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_xd"] = [ u for (u, _, _, _, _) in copct.minimaxDepthTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_mp"] = [ u for (u, _, _, _, _) in copct.minParametersTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_fsn"] = [ u for (u, _, _, _, _) in copct.minForestSizeTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_fsx"] = [ u for (u, _, _, _, _) in copct.maxForestSizeTLCovers(tlcovs)[0] ] start_time = time.clock() if check_irr: status, tlcovs_irr = copct.irredundantTLCovers(tlcovs, timeout=1000) if status == False: print("IRR timeout") else: tlcovs_irr = tlcovs results[demo_name]["run_time_irr"] = time.clock() - start_time results[demo_name]["tlcovs_irr"] = [ u for (u, _, _, _, _) in tlcovs_irr ] results[demo_name]["u in tlcovs"] = ground_truth in [ u for (u, _, _, _, _) in tlcovs ] results[demo_name]["u in tlcovs_mc"] = ground_truth in results[ demo_name]["tlcovs_mc"] results[demo_name]["u in tlcovs_md"] = ground_truth in results[ demo_name]["tlcovs_md"] results[demo_name]["u in tlcovs_xd"] = ground_truth in results[ demo_name]["tlcovs_xd"] results[demo_name]["u in tlcovs_mp"] = ground_truth in results[ demo_name]["tlcovs_mp"] results[demo_name]["u in tlcovs_fsn"] = ground_truth in results[ demo_name]["tlcovs_fsn"] results[demo_name]["u in tlcovs_fsx"] = ground_truth in results[ demo_name]["tlcovs_fsx"] results[demo_name]["u in tlcovs_irr"] = ground_truth in results[ demo_name]["tlcovs_irr"] # display results criteria = ["_mc", "_irr", "_md", "_xd", "_mp", "_fsn", "_fsx"] print("Accuracy:") for crit in criteria: correct_demos = [ d for d in results if results[d]["u in tlcovs%s" % crit] ] print('%s: %f%%' % (crit, 1.0 * len(correct_demos) / len(demos))) print("# of covers found:") print(["Demo", "Runtime (explain)", "Runtime (irr)"] + criteria) for demo_name in demos: num_tlcovs = [ len(results[demo_name]["tlcovs%s" % crit]) for crit in criteria ] print([ demo_name, results[demo_name]["run_time"], results[demo_name] ["run_time_irr"] ] + num_tlcovs) return results
def run_experiments(check_irr=True): results = {} # Dock maintenance demos demos = [ "demo_%s_%d" % (skill, di) for di in [1, 2] for skill in [ "remove_red_drive", "replace_red_with_green", "replace_red_with_spare", "swap_red_with_green" ] ] # demos = ["demo_%s_%d"%(skill, di) for di in [1,2] for skill in ["remove_red_drive"]] # Block stacking demos demos += ["demo_il", "demo_ai", "demo_um"] # Cover demos print("Covering demos...") for demo_name in demos: results[demo_name] = {} results[demo_name]["run_time"] = 0.0 results[demo_name]["run_times"] = [] for i in range(0, 5): print(demo_name) # import demo and ground truth exec_str = "from baxter_corpus.%s import demo" % demo_name exec(exec_str, globals()) exec_str = "from baxter_corpus.%s_ground_truth import ground_truth" % demo_name exec(exec_str, globals()) # Cover and prune by each parsimony criterion start_time = time.clock() status, tlcovs, g = copct.explain(causes, demo, M=M) # I ADDED THIS!! print('copct status: %s' % status) print('%d covers' % len(tlcovs)) # get most parsimonious covers pcovs, length = copct.minCardinalityTLCovers(tlcovs) print('%d covers of length %d' % (len(pcovs), length)) # Show the first one print('First cover of length %d:' % length) for intention in pcovs[0][0]: print(intention[1:]) results[demo_name]["run_times"].append(time.clock() - start_time) results[demo_name]["run_time"] += time.clock() - start_time results[demo_name]["tlcovs"], results[demo_name]["g"] = tlcovs, g results[demo_name]["tlcovs_mc"] = [ u for (u, _, _, _, _) in copct.minCardinalityTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_md"] = [ u for (u, _, _, _, _) in copct.maxDepthTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_xd"] = [ u for (u, _, _, _, _) in copct.minimaxDepthTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_mp"] = [ u for (u, _, _, _, _) in copct.minParametersTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_fsn"] = [ u for (u, _, _, _, _) in copct.minForestSizeTLCovers(tlcovs)[0] ] results[demo_name]["tlcovs_fsx"] = [ u for (u, _, _, _, _) in copct.maxForestSizeTLCovers(tlcovs)[0] ] start_time = time.clock() if check_irr: status, tlcovs_irr = copct.irredundantTLCovers(tlcovs, timeout=1000) if status == False: print("IRR timeout") else: tlcovs_irr = tlcovs results[demo_name]["run_time_irr"] = time.clock() - start_time results[demo_name]["tlcovs_irr"] = [ u for (u, _, _, _, _) in tlcovs_irr ] results[demo_name]["u in tlcovs"] = ground_truth in [ u for (u, _, _, _, _) in tlcovs ] results[demo_name]["u in tlcovs_mc"] = ground_truth in results[ demo_name]["tlcovs_mc"] results[demo_name]["u in tlcovs_md"] = ground_truth in results[ demo_name]["tlcovs_md"] results[demo_name]["u in tlcovs_xd"] = ground_truth in results[ demo_name]["tlcovs_xd"] results[demo_name]["u in tlcovs_mp"] = ground_truth in results[ demo_name]["tlcovs_mp"] results[demo_name]["u in tlcovs_fsn"] = ground_truth in results[ demo_name]["tlcovs_fsn"] results[demo_name]["u in tlcovs_fsx"] = ground_truth in results[ demo_name]["tlcovs_fsx"] results[demo_name]["u in tlcovs_irr"] = ground_truth in results[ demo_name]["tlcovs_irr"] # display results criteria = ["_mc", "_irr", "_md", "_xd", "_mp", "_fsn", "_fsx"] # print("Accuracy:") # for crit in criteria: # correct_demos = [d for d in results if results[d]["u in tlcovs%s"%crit]] # print('%s: %f%%'%(crit, 1.0*len(correct_demos)/len(demos))) # print("# of covers found:") # print(["Demo","Runtime (explain)", "Runtime (irr)"]+criteria) print("BAXTER Demo: Average Runtime, Standard Deviation") for demo_name in demos: num_tlcovs = [ len(results[demo_name]["tlcovs%s" % crit]) for crit in criteria ] # print([demo_name, results[demo_name]["run_time"], results[demo_name]["run_time_irr"]]+num_tlcovs) avg = str(results[demo_name]["run_time"] / 5.0) std_dev = str(calc_std_dev(results[demo_name]["run_times"], float(avg))) print(str(demo_name) + ': ' + avg + ', ' + std_dev) return results
def causes(v): return self.causes(v) | operational_causes(v) return causes if __name__ == "__main__": from baxter_experiments import M, causes # from baxter_corpus.demo_replace_red_with_spare_2 import demo # from baxter_corpus.demo_remove_red_drive_2 import demo # demo = demo[3:7] # exclude open/close from baxter_corpus.demo_remove_bad_drive import demo status, tlcovs, g = copct.explain(causes, demo, M=M) pcovs, _ = copct.minCardinalityTLCovers(tlcovs) copct.logCovers(pcovs, sys.stdout) kb = DescriptiveKnowledgeBase() name = "remove bad drive" kb.grow(name, pcovs) print(kb.causal_relation) from baxter_corpus.demo_remove_two_bad_drives import demo het_causes = kb.make_heterogenous_causes(causes) status, tlcovs, g = copct.explain(het_causes, demo, M=M) pcovs, _ = copct.minParametersTLCovers(tlcovs) copct.logCovers(pcovs, sys.stdout)
object_id = arguments[1][0] object_type = lookup_type(object_id, states[0]) if object_type == 'block' and argument[0][0] == argument[1][0]: g.add((states[0], 'stack', arguments[0][1] + arguments[0][2] + arguments[0][3] + arguments[0][4] + arguments[0][5] + arguments[1][0] + arguments[1][5] + arguments[1][6] + arguments[1][7:])) return g M = 2 if __name__ == '__main__': # Load demo demo = load_demo(demo_directory='./SMILE-1.1.0/room_demo/') # interpret demo (fixed-parameter-tractable when M is small and explicitly provided) status, tlcovs, g = copct.explain(causes, demo, M=M) print('copct status: %s' % status) print('%d covers' % len(tlcovs)) # get most parsimonious covers pcovs, length = copct.minCardinalityTLCovers(tlcovs) print('%d covers of length %d' % (len(pcovs), length)) # Show the first one print('First cover of length %d:' % length) for intention in pcovs[0][0]: print(intention[1:])