Exemple #1
0
 def next_results(self, verbose=False):
     start_time = time.time()
     assert not self.enumerated
     new_values = self._next_outputs()
     if verbose:
         print('{}:{}->[{}]'.format(
             self.external.name, str_from_tuple(self.get_input_values()),
             ', '.join(map(str_from_tuple, new_values))))
     self._check_output_values(new_values)
     results = []
     for output_values in new_values:
         output_objects = tuple(map(Object.from_value, output_values))
         results.append(self.external._Result(self, output_objects))
     self.update_statistics(start_time, results)
     return results
Exemple #2
0
def visualize_constraints(constraints,
                          filename='constraint_network.pdf',
                          use_functions=True):
    from pygraphviz import AGraph

    graph = AGraph(strict=True, directed=False)
    graph.node_attr['style'] = 'filled'
    graph.node_attr['shape'] = 'circle'
    graph.node_attr['fontcolor'] = 'black'
    graph.node_attr['colorscheme'] = 'SVG'
    graph.edge_attr['colorscheme'] = 'SVG'

    functions = set()
    heads = set()
    for fact in constraints:
        if get_prefix(fact) == EQ:
            functions.add(fact[1])
        else:
            heads.add(fact)
    heads.update(functions)

    objects = {a for head in heads for a in get_args(head)}
    optimistic_objects = filter(lambda o: isinstance(o, OptimisticObject),
                                objects)
    for opt_obj in optimistic_objects:
        graph.add_node(str(opt_obj), shape='circle', color=PARAMETER_COLOR)

    for head in heads:
        if not use_functions and (head in functions):
            continue
        # TODO: prune values w/o free parameters?
        name = str_from_tuple(head)
        color = COST_COLOR if head in functions else CONSTRAINT_COLOR
        graph.add_node(name, shape='box', color=color)
        for arg in get_args(head):
            if arg in optimistic_objects:
                graph.add_edge(name, str(arg))
    graph.draw(filename, prog='dot')
    return graph
Exemple #3
0
 def next_results(self, accelerate=1, verbose=False):
     all_new_values = []
     all_results = []
     start_calls = self.num_calls
     for attempt in range(accelerate):
         if all_results or self.enumerated:
             break
         start_time = time.time()
         new_values = self._next_outputs()
         self._check_output_values(new_values)
         results = [
             self.external._Result(self, tuple(map(Object.from_value, ov)))
             for ov in new_values
         ]
         all_new_values.extend(new_values)
         all_results.extend(results)
         self.update_statistics(start_time, results)
     if verbose and all_new_values:
         print('{}-{}) {}:{}->[{}]'.format(
             start_calls, self.num_calls, self.external.name,
             str_from_tuple(self.get_input_values()),
             ', '.join(map(str_from_tuple, all_new_values))))
     return all_results
Exemple #4
0
 def next_results(self, accelerate=1, verbose=False):
     start_time = time.time()
     assert not self.enumerated
     self.enumerated = True
     input_values = self.get_input_values()
     try:
         value = self.external.fn(*input_values)
     except TypeError:
         raise TypeError('Function [{}] expects {} inputs'.format(
             self.external.name, len(input_values)))
     self.value = self.external._codomain(value)
     # TODO: cast the inputs and test whether still equal?
     #if not (type(self.value) is self.external._codomain):
     #if not isinstance(self.value, self.external._codomain):
     if self.value != value:
         raise ValueError(
             'Function [{}] produced a nonintegral value [{}]. '
             'FastDownward only supports integral costs. '
             'To "use" real costs, scale each cost by a large factor, '
             'capturing the most significant bits.'.format(
                 self.external.name, self.value))
     if self.value < 0:
         raise ValueError(
             'Function [{}] produced a negative value [{}]'.format(
                 self.external.name, self.value))
     if verbose:
         print('{}{}={}'.format(get_prefix(self.external.head),
                                str_from_tuple(self.get_input_values()),
                                self.value))
     results = [self.external._Result(self, self.value)]
     if isinstance(self, PredicateInstance) and (
             self.value != self.external.opt_fn(*input_values)):
         self.update_statistics(start_time,
                                [])  # TODO: do this more automatically
     else:
         self.update_statistics(start_time, results)
     return results
Exemple #5
0
def str_from_head(head):
    return '{}{}'.format(get_prefix(head), str_from_tuple(get_args(head)))
Exemple #6
0
 def add_fact(fact):
     head, color = (fact[1], COST_COLOR) if get_prefix(fact) == EQ else (fact, CONSTRAINT_COLOR)
     s_fact = str_from_tuple(head)
     graph.add_node(s_fact, shape='box', color=color)
     return s_fact
Exemple #7
0
 def __repr__(self):
     return '{}:{}->{}'.format(self.instance.external.name,
                               str_from_tuple(self.instance.input_objects),
                               str_from_tuple(self.output_objects))