Esempio n. 1
0
 def __init__(self, optimizer, constraint, domain, fluents):
     self.optimizer = optimizer
     self.constraint = constraint
     inputs = get_args(constraint)
     outputs = []
     certified = [constraint]
     name = '{}-{}'.format(optimizer.name, get_prefix(constraint))
     gen_fn = get_gen_fn(optimizer.procedure, inputs, outputs, certified)
     #gen_fn = empty_gen()
     info = StreamInfo(effort_fn=get_effort_fn(optimizer.name), simultaneous=DEFAULT_SIMULTANEOUS)
     self.stream_fact = Fact('_{}'.format(name), concatenate(inputs, outputs))
     super(ConstraintStream, self).__init__(name, gen_fn, inputs, domain,
                                            outputs, certified, info, fluents=fluents)
Esempio n. 2
0
 def __init__(self, optimizer, variable, inputs, domain, certified, info):
     self.optimizer = optimizer
     self.variable = variable
     outputs = [variable]
     name = '{}-{}'.format(optimizer.name, get_parameter_name(variable))
     gen_fn = get_gen_fn(optimizer.procedure, inputs, outputs, certified)
     #gen_fn = empty_gen()
     #info = StreamInfo(effort_fn=get_effort_fn(optimizer_name, inputs, outputs))
     #info = StreamInfo(opt_gen_fn=PartialInputs(unique=DEFAULT_UNIQUE, num=DEFAULT_NUM))
     # Each stream could certify a stream-specific fact as well
     # TODO: will I need to adjust simultaneous here as well?
     info = StreamInfo(opt_gen_fn=PartialInputs(unique=DEFAULT_UNIQUE), simultaneous=DEFAULT_SIMULTANEOUS)
     self.stream_fact = Fact('_{}'.format(name), concatenate(inputs, outputs)) # TODO: just add to certified?
     super(VariableStream, self).__init__(name, gen_fn, inputs, domain,
                                          outputs, certified, info)
Esempio n. 3
0
    def __init__(self, name, gen_fn, inputs, domain, outputs, certified, info, fluents=[]):
        super(Stream, self).__init__(name, info, inputs, domain)
        self.outputs = tuple(outputs)
        self.certified = tuple(map(convert_constants, certified))
        self.constants.update(a for i in certified for a in get_args(i) if not is_parameter(a))

        for p, c in Counter(self.outputs).items():
            if not is_parameter(p):
                raise ValueError('Output [{}] for stream [{}] is not a parameter'.format(p, name))
            if c != 1:
                raise ValueError('Output [{}] for stream [{}] is not unique'.format(p, name))
        for p in set(self.inputs) & set(self.outputs):
            raise ValueError('Parameter [{}] for stream [{}] is both an input and output'.format(p, name))
        certified_parameters = {a for i in certified for a in get_args(i) if is_parameter(a)}
        for p in (certified_parameters - set(self.inputs + self.outputs)):
            raise ValueError('Parameter [{}] for stream [{}] is not included within outputs'.format(p, name))
        for p in (set(self.outputs) - certified_parameters):
            print('Warning! Output [{}] for stream [{}] is not covered by a certified condition'.format(p, name))

        # TODO: automatically switch to unique if only used once
        self.gen_fn = get_debug_gen_fn(self, shared=True) if gen_fn == DEBUG else gen_fn
        assert callable(self.gen_fn)
        self.num_opt_fns = 0 if self.is_test() else 1 # Always unique if no outputs
        if isinstance(self.info.opt_gen_fn, PartialInputs):
            #self.info.opt_gen_fn.register(self)
            if self.info.opt_gen_fn.unique:
                self.num_opt_fns = 0
        #self.bound_list_fn = None # TODO: generalize to a hierarchical sequence
        #self.opt_fns = [get_unique_fn(self), get_shared_fn(self)] # get_unique_fn | get_shared_fn

        self.fluents = fluents
        #self.fluents = [] if gen_fn == DEBUG else fluents
        if NEGATIVE_BLOCKED:
            self.blocked_predicate = '~{}{}'.format(self.name, NEGATIVE_SUFFIX) # Args are self.inputs
        else:
            self.blocked_predicate = '~{}'.format(self.name)
        self.disabled_instances = [] # For tracking disabled axioms
        self.stream_fact = Fact('_{}'.format(name), concatenate(inputs, outputs)) # TODO: just add to certified?

        if self.is_negated():
            if self.outputs:
                raise ValueError('Negated streams cannot have outputs: {}'.format(self.outputs))
            #assert len(self.certified) == 1 # TODO: is it okay to have more than one fact?
            for certified in self.certified:
                if not (set(self.inputs) <= set(get_args(certified))):
                    raise ValueError('Negated streams must have certified facts including all input parameters')