Exemplo n.º 1
0
def extend_mutexes(mutexes, task, atoms, actions):
    atoms = common.filter_atoms(atoms)
    pairs = gen_all_pairs(mutexes)
    if len(pairs) == 0:
        return set(), set()

    actions = [ExtendAction(x, atoms) for x in actions]
    spurious = SpuriousDetector(actions)
    spurious.set_spurious(pairs)
    facts = { x : ExtendFact(x, pairs, actions) for x in atoms }

    candidates = gen_all_pairs([atoms]) - pairs
    init = set(task.init) & atoms
    candidates -= gen_all_pairs([init])

    candidates = set([frozenset([facts[x] for x in y]) for y in candidates])

    candidates_size = 0
    while candidates_size != len(candidates):
        candidates_size = len(candidates)
        for cand in list(candidates):
            pair = frozenset([x.fact for x in cand])
            if spurious.exist(pair):
                continue

            c1, c2 = cand
            if c1.check(c2) and c2.check(c1):
                spurious.set_spurious([pair])
                pairs.add(pair)
                candidates.remove(cand)
                c1.add_mutex(c2)
                c2.add_mutex(c1)
    return pairs, set()
Exemplo n.º 2
0
def full(task, atoms, actions, verbose = False, max_states = -1):
    atoms = common.filter_atoms(atoms)
    init = set(task.init) & atoms
    actions = [FullAction(a, atoms) for a in actions]

    unreached_facts = set(atoms)
    unreached_pairs = common.gen_all_pairs([atoms])
    reached_states = set()

    states = set([frozenset(init)])
    while len(states) > 0:
        #print(reached_states)
        next_states = set()
        for s in states:
            for a in actions:
                action_apply(a, s, next_states)
            unreached_facts -= s
            unreached_pairs -= common.gen_all_pairs([s])
            reached_states.add(s)
        states = next_states - reached_states

        if verbose:
            print('mutex.full():: Reached states:', len(reached_states))

        if max_states >= 0 and len(reached_states) > max_states:
            print('mutex.full():: Exceeded max-states ({0}/{1}).  Aborted.'
                    .format(len(reached_states), max_states))
            return None, None

    mutexes = unreached_pairs
    return mutexes, unreached_facts
Exemplo n.º 3
0
def rfa_conflict_bind(task, atoms, actions):
    atoms = common.filter_atoms(atoms)
    atom_to_fact = { a : RFAFact(a, atoms) for a in atoms }
    facts = atom_to_fact.values()
    actions = [RFAAction(a, atoms, atom_to_fact) for a in actions]
    [(f.set_all_facts(facts), f.set_actions(actions)) for f in facts]

    rfa_init(atom_to_fact, task, atoms, actions)

    change = True
    while change:
        change = False
        for f in facts:
            change |= f.apply_transitivity()
            change |= f.check_useless()
            change |= f.check_actions()
    return facts
Exemplo n.º 4
0
def rfa_conflict_bind(task, atoms, actions):
    atoms = common.filter_atoms(atoms)
    atom_to_fact = {a: RFAFact(a, atoms) for a in atoms}
    facts = atom_to_fact.values()
    actions = [RFAAction(a, atoms, atom_to_fact) for a in actions]
    [(f.set_all_facts(facts), f.set_actions(actions)) for f in facts]

    rfa_init(atom_to_fact, task, atoms, actions)

    change = True
    while change:
        change = False
        for f in facts:
            change |= f.apply_transitivity()
            change |= f.check_useless()
            change |= f.check_actions()
    return facts
Exemplo n.º 5
0
def h2(task, atoms, actions):
    atoms = common.filter_atoms(atoms)
    actions = [H2Action(a, atoms) for a in actions]

    init = set(task.init) & atoms

    reached1 = init
    reached2 = common.gen_all_pairs([init])
    lastsize = 0
    while len(reached1) + len(reached2) != lastsize:
        lastsize = len(reached1) + len(reached2)

        for a in actions:
            if a.pre1.issubset(reached1) and a.pre2.issubset(reached2):
                r1, r2 = a.eff(reached1, reached2)
                reached1 |= r1
                reached2 |= r2

    m2 = common.gen_all_pairs([atoms]) - reached2
    return m2, atoms - reached1
Exemplo n.º 6
0
def h2(task, atoms, actions):
    atoms = common.filter_atoms(atoms)
    actions = [H2Action(a, atoms) for a in actions]

    init = set(task.init) & atoms

    reached1 = init
    reached2 = common.gen_all_pairs([init])
    lastsize = 0
    while len(reached1) + len(reached2) != lastsize:
        lastsize = len(reached1) + len(reached2)

        for a in actions:
            if a.pre1.issubset(reached1) and a.pre2.issubset(reached2):
                r1, r2 = a.eff(reached1, reached2)
                reached1 |= r1
                reached2 |= r2

    m2 = common.gen_all_pairs([atoms]) - reached2
    return m2, atoms - reached1
Exemplo n.º 7
0
def fa(task, atoms, actions, rfa=False, rfa_bind_conflict=None):
    atoms = common.filter_atoms(atoms)
    atoms_dict, atoms_list = common.create_atoms_dict(atoms)

    ilp = cplex.Cplex()
    ilp.objective.set_sense(ilp.objective.sense.maximize)
    ilp.set_log_stream(None)
    ilp.set_error_stream(None)
    ilp.set_results_stream(None)
    ilp.set_warning_stream(None)
    ilp.parameters.tune_problem([(ilp.parameters.threads, 1)])
    ilp.variables.add(obj=[1. for x in atoms_list],
                      names=[str(x) for x in atoms_list],
                      types=['B' for x in atoms_list])

    # Initial state constraint
    s_init = list(set(task.init) & atoms)
    constr = cplex.SparsePair(ind=[atoms_dict[x] for x in s_init],
                              val=[1. for _ in s_init])
    ilp.linear_constraints.add(lin_expr=[constr], senses=['L'], rhs=[1.])

    # Action constraints
    for action in actions:
        pre = set(action.precondition) & atoms
        add_eff = set([x[1] for x in action.add_effects]) & atoms
        del_eff = set([x[1] for x in action.del_effects]) & atoms
        pre = [atoms_dict[x] for x in pre if not x.negated]
        add_eff = [atoms_dict[x] for x in add_eff]
        del_eff = [atoms_dict[x] for x in del_eff]
        if len(add_eff) == 0:
            continue

        pre_del = list(set(pre) & set(del_eff))
        constr = cplex.SparsePair(ind=add_eff + pre_del,
                                  val=[1. for _ in add_eff] +
                                  [-1. for _ in pre_del])
        ilp.linear_constraints.add(lin_expr=[constr], senses=['L'], rhs=[0.])
        if rfa:
            constr2 = cplex.SparsePair(ind=pre_del, val=[1. for _ in pre_del])
            ilp.linear_constraints.add(lin_expr=[constr2],
                                       senses=['L'],
                                       rhs=[1.])

    # Bind and conflict set constraints
    if rfa_bind_conflict is not None:
        for f in rfa_bind_conflict:
            if f in f.conflict:
                constr = cplex.SparsePair(ind=[atoms_dict[f.fact]], val=[1.])
                ilp.linear_constraints.add(lin_expr=[constr],
                                           senses=['E'],
                                           rhs=[0.])
                continue

            if len(f.bind) > 1:
                bind = [atoms_dict[x.fact] for x in f.bind - set([f])]
                cbind = cplex.SparsePair(ind=bind + [atoms_dict[f.fact]],
                                         val=[-1. for _ in bind] + [len(bind)])
                ilp.linear_constraints.add(lin_expr=[cbind],
                                           senses=['L'],
                                           rhs=[0.])

            confl = [atoms_dict[x.fact] for x in f.conflict]
            cconfl = cplex.SparsePair(ind=confl + [atoms_dict[f.fact]],
                                      val=[1. for _ in confl] + [len(confl)])
            ilp.linear_constraints.add(lin_expr=[cconfl],
                                       senses=['L'],
                                       rhs=[len(confl)])

    mutexes = set()
    ilp.solve()
    #print(c.solution.get_status())
    while ilp.solution.get_status() == 101:
        values = ilp.solution.get_values()
        if sum(values) <= 1.5:
            break
        sol = [atoms_list[x] for x in range(len(values)) if values[x] > 0.5]
        mutexes.add(frozenset(sol))

        # Add constraint removing all subsets of the current solution
        constr_idx = [x for x in range(len(values)) if values[x] < 0.5]
        constr = cplex.SparsePair(ind=constr_idx, val=[1. for _ in constr_idx])
        ilp.linear_constraints.add(lin_expr=[constr], senses=['G'], rhs=[1.])
        ilp.solve()

    return mutexes, set()
Exemplo n.º 8
0
def fa(task, atoms, actions, rfa = False, rfa_bind_conflict = None):
    atoms = common.filter_atoms(atoms)
    atoms_dict, atoms_list = common.create_atoms_dict(atoms)

    ilp = cplex.Cplex()
    ilp.objective.set_sense(ilp.objective.sense.maximize)
    ilp.set_log_stream(None)
    ilp.set_error_stream(None)
    ilp.set_results_stream(None)
    ilp.set_warning_stream(None)
    ilp.parameters.tune_problem([(ilp.parameters.threads, 1)])
    ilp.variables.add(obj = [1. for x in atoms_list],
                      names = [str(x) for x in atoms_list],
                      types = ['B' for x in atoms_list])

    # Initial state constraint
    s_init = list(set(task.init) & atoms)
    constr = cplex.SparsePair(ind = [atoms_dict[x] for x in s_init],
                              val = [1. for _ in s_init])
    ilp.linear_constraints.add(lin_expr = [constr], senses = ['L'], rhs = [1.])

    # Action constraints
    for action in actions:
        pre = set(action.precondition) & atoms
        add_eff = set([x[1] for x in action.add_effects]) & atoms
        del_eff = set([x[1] for x in action.del_effects]) & atoms
        pre = [atoms_dict[x] for x in pre if not x.negated]
        add_eff = [atoms_dict[x] for x in add_eff]
        del_eff = [atoms_dict[x] for x in del_eff]
        if len(add_eff) == 0:
            continue

        pre_del = list(set(pre) & set(del_eff))
        constr = cplex.SparsePair(ind = add_eff + pre_del,
                                  val = [1. for _ in add_eff]
                                        + [-1. for _ in pre_del])
        ilp.linear_constraints.add(lin_expr = [constr],
                                   senses = ['L'], rhs = [0.])
        if rfa:
            constr2 = cplex.SparsePair(ind = pre_del, val = [1. for _ in pre_del])
            ilp.linear_constraints.add(lin_expr = [constr2],
                                       senses = ['L'], rhs = [1.])

    # Bind and conflict set constraints
    if rfa_bind_conflict is not None:
        for f in rfa_bind_conflict:
            if f in f.conflict:
                constr = cplex.SparsePair(ind = [atoms_dict[f.fact]], val = [1.])
                ilp.linear_constraints.add(lin_expr = [constr],
                                           senses = ['E'], rhs = [0.])
                continue

            if len(f.bind) > 1:
                bind = [atoms_dict[x.fact] for x in f.bind - set([f])]
                cbind = cplex.SparsePair(ind = bind + [atoms_dict[f.fact]],
                                         val = [-1. for _ in bind] + [len(bind)])
                ilp.linear_constraints.add(lin_expr = [cbind],
                                           senses = ['L'], rhs = [0.])

            confl  = [atoms_dict[x.fact] for x in f.conflict]
            cconfl = cplex.SparsePair(ind = confl + [atoms_dict[f.fact]],
                                      val = [1. for _ in confl] + [len(confl)])
            ilp.linear_constraints.add(lin_expr = [cconfl],
                                       senses = ['L'], rhs = [len(confl)])

    mutexes = set()
    ilp.solve()
    #print(c.solution.get_status())
    while ilp.solution.get_status() == 101:
        values = ilp.solution.get_values()
        if sum(values) <= 1.5:
            break
        sol = [atoms_list[x] for x in range(len(values)) if values[x] > 0.5]
        mutexes.add(frozenset(sol))

        # Add constraint removing all subsets of the current solution
        constr_idx = [x for x in range(len(values)) if values[x] < 0.5]
        constr = cplex.SparsePair(ind = constr_idx,
                                  val = [1. for _ in constr_idx])
        ilp.linear_constraints.add(lin_expr = [constr],
                                   senses = ['G'], rhs = [1.])
        ilp.solve()

    return mutexes, set()