Exemplo n.º 1
0
def augment_evaluations(evaluations, future_map):
    for evaluation in list(filter(is_atom, evaluations)):
        name = evaluation.head.function
        if name in future_map:
            new_head = Head(future_map[name], evaluation.head.args)
            new_evaluation = Evaluation(new_head, evaluation.value)
            add_fact(evaluations, fact_from_evaluation(new_evaluation),
                     result=INTERNAL_EVALUATION, complexity=0)
Exemplo n.º 2
0
def get_achieving_streams(evaluations,
                          stream_results,
                          max_effort=INF,
                          **effort_args):
    unprocessed_from_atom = defaultdict(list)
    node_from_atom = {NULL_COND: Node(0, None)}
    conditions_from_stream = {}
    remaining_from_stream = {}
    for result in stream_results:
        conditions_from_stream[result] = result.instance.get_domain() + (
            NULL_COND, )
        remaining_from_stream[result] = len(conditions_from_stream[result])
        for atom in conditions_from_stream[result]:
            unprocessed_from_atom[atom].append(result)
    for atom in evaluations:
        if not is_negated_atom(atom):
            node_from_atom[fact_from_evaluation(atom)] = Node(0, None)

    queue = [
        HeapElement(node.effort, atom)
        for atom, node in node_from_atom.items()
    ]
    while queue:
        atom = heappop(queue).value
        if atom not in unprocessed_from_atom:
            continue
        for result in unprocessed_from_atom[atom]:
            remaining_from_stream[result] -= 1
            if remaining_from_stream[result]:
                continue
            effort = result.get_effort(**effort_args)
            total_effort = effort + EFFORT_OP(
                node_from_atom[cond].effort
                for cond in conditions_from_stream[result])
            if max_effort <= total_effort:
                continue
            for new_atom in result.get_certified():
                if (new_atom not in node_from_atom) or (
                        total_effort < node_from_atom[new_atom].effort):
                    node_from_atom[new_atom] = Node(total_effort, result)
                    heappush(queue, HeapElement(total_effort, new_atom))
        del unprocessed_from_atom[atom]
    del node_from_atom[NULL_COND]
    return node_from_atom
Exemplo n.º 3
0
def get_achieving_streams(evaluations,
                          stream_results,
                          unit_efforts=False):  #, max_effort=INF):
    # TODO: could do this with bound_stream_instances instead
    unprocessed_from_atom = defaultdict(list)
    node_from_atom = {NULL_COND: Node(0, None)}
    conditions_from_stream = {}
    remaining_from_stream = {}
    for result in stream_results:
        conditions_from_stream[result] = result.instance.get_domain() + (
            NULL_COND, )
        remaining_from_stream[result] = len(conditions_from_stream[result])
        for atom in conditions_from_stream[result]:
            unprocessed_from_atom[atom].append(result)
    for atom in evaluations:
        if is_atom(atom):
            node_from_atom[fact_from_evaluation(atom)] = Node(0, None)

    queue = [
        HeapElement(node.effort, atom)
        for atom, node in node_from_atom.items()
    ]
    while queue:
        atom = heappop(queue).value
        if atom not in unprocessed_from_atom:
            continue
        for result in unprocessed_from_atom[atom]:
            remaining_from_stream[result] -= 1
            if remaining_from_stream[result]:
                continue
            effort = get_instance_effort(result.instance, unit_efforts)
            total_effort = effort + COMBINE_OP(
                node_from_atom[cond].effort
                for cond in conditions_from_stream[result])
            #if max_effort <= total_effort:
            #    continue
            for new_atom in result.get_certified():
                if (new_atom not in node_from_atom) or (
                        total_effort < node_from_atom[new_atom].effort):
                    node_from_atom[new_atom] = Node(total_effort, result)
                    heappush(queue, HeapElement(total_effort, new_atom))
        del unprocessed_from_atom[atom]
    del node_from_atom[NULL_COND]
    return node_from_atom
Exemplo n.º 4
0
def get_achieving_streams(evaluations, stream_results, op=sum):
    # TODO: could do this with bound_stream_instances instead
    unprocessed_from_atom = defaultdict(list)
    none = (None, )  # None
    node_from_atom = {none: Node(0, None)}
    conditions_from_stream = {}
    remaining_from_stream = {}
    for stream_result in stream_results:
        conditions_from_stream[
            stream_result] = stream_result.instance.get_domain() + (none, )
        remaining_from_stream[stream_result] = len(
            conditions_from_stream[stream_result])
        for atom in conditions_from_stream[stream_result]:
            unprocessed_from_atom[atom].append(stream_result)
    for atom in evaluations:
        if is_atom(atom):
            node_from_atom[fact_from_evaluation(atom)] = Node(0, None)

    queue = [
        HeapElement(node.effort, atom)
        for atom, node in node_from_atom.items()
    ]
    while queue:
        atom = heappop(queue).value
        if atom not in unprocessed_from_atom:
            continue
        for stream_result in unprocessed_from_atom[atom]:
            remaining_from_stream[stream_result] -= 1
            if remaining_from_stream[stream_result]:
                continue
            effort = 1
            total_effort = op(
                node_from_atom[cond].effort
                for cond in conditions_from_stream[stream_result]) + effort
            for new_atom in stream_result.get_certified():
                if (new_atom not in node_from_atom) or (
                        total_effort < node_from_atom[new_atom].effort):
                    node_from_atom[new_atom] = Node(total_effort,
                                                    stream_result)
                    heappush(queue, HeapElement(total_effort, new_atom))
        del unprocessed_from_atom[atom]
    del node_from_atom[none]
    return node_from_atom