Beispiel #1
0
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
Beispiel #2
0
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)))

Beispiel #3
0
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
Beispiel #4
0
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
Beispiel #5
0
    def make_heterogenous_causes(self, operational_causes):
        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)
def run_sample(M,
               causes,
               u_correct,
               w,
               verbose=True,
               timeout=600,
               timeout_irr=300,
               max_tlcovs=13000000):
    """
    Run experimental evaluation on one sample plan.
    Inputs:
        M: domain constant (maximum length of any effect sequence in the causal relation)
        causes: handle to the causes function encoding the domain
        u_correct: the "correct" cover against which copct is tested
        w: sequence of observed actions (the plan) to be covered by copct
        verbose: if True, print copct verbose output
        timeout: timeout for explain
        timeout_irr: timout for irredundancy
        max_tlcovs: maximum top-level cover count for explain
    Outputs:
        result: dictionary with various key:value pairs summarizing the outcomes of the experiment.
    """

    # run copct
    start = time.clock()
    status, tlcovs, g = copct.explain(causes,
                                      w,
                                      M=M,
                                      verbose=verbose,
                                      timeout=timeout,
                                      max_tlcovs=max_tlcovs)
    runtime = time.clock() - start

    # record execution info
    result = {}
    result["runtime"] = runtime
    result["status"] = status
    if not status == "Success": return result

    # top-level results
    result["correct"] = u_correct in [u for (u, _, _, _, _) in tlcovs]
    result["|tlcovs|"] = len(tlcovs)
    print("correct=%s, |tlcovs|=%d" % (result["correct"], result["|tlcovs|"]))

    # compare parsimony criteria
    criteria = [("_mc", copct.minCardinalityTLCovers),
                ("_md", copct.maxDepthTLCovers),
                ("_xd", copct.minimaxDepthTLCovers),
                ("_fsn", copct.minForestSizeTLCovers),
                ("_fsx", copct.maxForestSizeTLCovers),
                ("_mp", copct.minParametersTLCovers)]
    for (label, fun) in criteria:
        pruned_tlcovs, extremum = fun(tlcovs)
        correct = u_correct in [u for (u, _, _, _, _) in pruned_tlcovs]
        count = len(pruned_tlcovs)
        result["correct%s" % label] = correct
        result["|tlcovs%s|" % label] = count
        result["extremum%s" % label] = extremum
        print("%s: correct=%s, count=%d, extremum=%d" %
              (label, correct, count, extremum))

    # special handling for irredundancy
    status, tlcovs_irr = copct.irredundantTLCovers(tlcovs, timeout=timeout_irr)
    result["irr_success"] = status
    if not status: return result
    result["correct_irr"] = u_correct in [u for (u, _, _, _, _) in tlcovs_irr]
    result["|tlcovs_irr|"] = len(tlcovs_irr)
    print("correct_irr=%s, count_irr=%d" %
          (result["correct_irr"], result["|tlcovs_irr|"]))

    return result
Beispiel #7
0
def causes(v):
    if v == ('g', 'm', 'r'): return set(['p'])
    if v == ('p', 'p'): return set(['t'])
    if v == ('p', 'g'): return set(['x'])
    if v == ('r', 'p'): return set(['z'])
    return set()


# maximum effect sequence length (gmr case in causes)
M = 3

if __name__ == "__main__":
    # toy observed sequence
    w = tuple('gmrgmr')
    # compute explanations
    status, tlcovs, g = copct.explain(causes, w, M=M, verbose=True)
    # Prune by minimum cardinality
    tlcovs_ml, ml = copct.minCardinalityTLCovers(tlcovs)

    # Display results
    print('Observed w:')
    print(w)
    print('Singleton sub-covers:')
    for jk in itr.combinations(range(7), 2):
        if len(g[jk]) > 0:
            print("sub-seq from %d to %d covered by: %s" %
                  (jk[0], jk[1], g[jk]))
    print('Top-level covers:')
    print([u for (u, _, _, _, _) in tlcovs])
    print('MC top-level covers:')
    print([u for (u, _, _, _, _) in tlcovs_ml])