Ejemplo n.º 1
0
def create_static_stream(stream, evaluations, fluent_predicates, get_future):
    def static_fn(*input_values):
        instance = stream.get_instance(tuple(map(Object.from_value, input_values)))
        if all(evaluation_from_fact(f) in evaluations for f in instance.get_domain()):
            return None
        return tuple(FutureValue(stream.name, input_values, o) for o in stream.outputs)

    #opt_evaluations = None
    def static_opt_gen_fn(*input_values):
        instance = stream.get_instance(tuple(map(Object.from_value, input_values)))
        if all(evaluation_from_fact(f) in evaluations for f in instance.get_domain()):
            return
        for output_values in stream.opt_gen_fn(*input_values):
            yield output_values
        # TODO: need to replace regular opt_gen_fn to update opt_evaluations
        # if I want to prevent switch from normal to static in opt

    # Focused algorithm naturally biases against using future because of axiom layers
    stream_name = 'future-{}'.format(stream.name)
    gen_fn = from_fn(static_fn)
    static_domain = list(filter(lambda a: get_prefix(a) not in fluent_predicates, stream.domain))
    new_domain = list(map(get_future, static_domain))
    stream_atom = ('{}-result'.format(stream.name),) + tuple(stream.inputs + stream.outputs)
    new_certified = [stream_atom] + list(map(get_future, stream.certified))
    static_stream = Stream(stream_name, gen_fn, stream.inputs, new_domain,
                          stream.outputs, new_certified, stream.info)
    static_stream.opt_gen_fn = static_opt_gen_fn
    return static_stream
Ejemplo n.º 2
0
def opt_from_graph(names, orders, infos={}):
    param_from_order = {order: PARAM_TEMPLATE.format(*order) for order in orders}
    fact_from_order = {order: (PREDICATE, param_from_order[order]) for order in orders}
    object_from_param = {param: parse_value(param) for param in param_from_order.values()}

    incoming_from_edges, outgoing_from_edges = neighbors_from_orders(orders)
    stream_plan = []
    for i, n in enumerate(names):
        #info = infos.get(n, StreamInfo(p_success=1, overhead=0, verbose=True))
        inputs = [param_from_order[n2, n] for n2 in incoming_from_edges[n]]
        outputs = [param_from_order[n, n2] for n2 in outgoing_from_edges[n]]
        #gen = get_gen(outputs=outputs, p_success=info.p_success)
        #gen = get_gen(infos[i], outputs=outputs)
        stream = Stream(
            name=n,
            #gen_fn=DEBUG,
            #gen_fn=from_gen(gen),
            gen_fn=from_gen_fn(get_gen_fn(outputs=outputs, **infos[i])),
            inputs=inputs,
            domain=[fact_from_order[n2, n] for n2 in incoming_from_edges[n]],
            fluents=[],
            outputs=outputs,
            certified=[fact_from_order[n, n2] for n2 in outgoing_from_edges[n]],
            #info=info,
            info=StreamInfo(),
        )
        # TODO: dump names

        print()
        print(stream)
        input_objects = safe_apply_mapping(stream.inputs, object_from_param)
        instance = stream.get_instance(input_objects)
        print(instance)
        output_objects = safe_apply_mapping(stream.outputs, object_from_param)
        result = instance.get_result(output_objects)
        print(result)
        stream_plan.append(result)

    opt_plan = OptPlan(action_plan=[], preimage_facts=[])
    opt_solution = OptSolution(stream_plan, opt_plan, cost=1)
    return opt_solution
Ejemplo n.º 3
0
def parse_rule(lisp_list, stream_map, stream_info):
    value_from_attribute = parse_lisp_list(lisp_list[1:])
    assert set(value_from_attribute) <= {':inputs', ':domain', ':certified'}
    # TODO: if len(certified) == 1, augment existing streams
    RULES.append(Stream(name='rule{}'.format(len(RULES)),
                        gen_fn=from_test(lambda *args: True),
                        inputs=value_from_attribute.get(':inputs', []),
                        domain=list_from_conjunction(value_from_attribute.get(':domain', [])),
                        fluents=[],
                        outputs=[],
                        certified=list_from_conjunction(value_from_attribute.get(':certified', [])),
                        info=StreamInfo(eager=True, p_success=1, overhead=0)))
    return RULES[-1]