Exemplo n.º 1
0
    def __init_encoding(self):
        def get_null_pred(variables):
            nulls = []
            for var in variables:
                pred = var.arguments[0].arguments[0]
                if (pred.type == clingo.SymbolType.String):
                    nulls.append(pred.string)
            return nulls

        with create_solver() as ctl:
            self._init_solver(ctl)
            ctl.ground([(Logic.base)])
            symbols = ctl.solve(solvekwargs=dict(yield_=True),
                                symbolkwargs=dict(atoms=True))
            assert (len(symbols) == 1)
            variables = SymbolSet(symbols[0]).get_atoms('variable', 1)
            #Add predicates of arity 0
            ctl.addSymbols([
                clingo.Function('arity', [n, 0])
                for n in get_null_pred(variables)
            ])

            ctl.ground([('predicates', []), ('const', [])])
            symbols = ctl.solve(solvekwargs=dict(yield_=True),
                                symbolkwargs=dict(shown=True))
            assert (len(symbols) == 1)
            symset = SymbolSet(symbols[0])
            self._pred = symset.get_atoms('pred', 1)
            self._arity = symset.get_atoms('arity', 2)
            self._const = symset.get_atoms('const', 1)
Exemplo n.º 2
0
 def load(symbols, id, children, depth, parent, encoding):
     node = Node(SymbolSet.from_pickle(symbols),
                 depth,
                 parent,
                 id=id,
                 encoding=SymbolSet.from_pickle(encoding))
     node._children = children
     for child in node.children:
         node._transitions.append(
             clingo.Function('transition', [node.id, child]))
     return node
Exemplo n.º 3
0
 def encode(self, prob):
     assert (self._encoding == None)
     with create_solver() as ctl:
         ctl.load(Logic.problem)
         ctl.load(Logic.sample_encoding)
         ctl.addSymbols(self.symbols.get_all_atoms() + prob.predicates +
                        prob.const)
         ctl.ground([Logic.base, hold(), encode()])
         models = ctl.solve(solvekwargs=dict(yield_=True),
                            symbolkwargs=dict(shown=True))
         #print(ctl.getAtoms('hold', 3), len(models))
         assert (len(models) == 1)
         self._encoding = SymbolSet(models[0])
Exemplo n.º 4
0
 def get_successors(self, node: Node) -> List[Node]:
     #print('Generating successors of', node.id)
     write_symbols(
         node.symbols.get_all_atoms(),
         '/home/ivan/Documents/ai/features/res/samples/clear/hard_complete/node.lp'
     )
     with create_solver(args=dict(arguments=['-n 0'])) as ctl:
         self._init_solver(ctl)
         ctl.addSymbols(node.symbols.get_all_atoms())
         ctl.ground([Logic.base, successor()])
         ctl.solve()
         ctl.cleanup()
         #print(ctl.getAtoms('validAction', 3))
         ctl.ground([evaluate()])
         ctl.solve()
         ctl.cleanup()
         ctl.ground([show_expanded()])
         models = ctl.solve(solvekwargs=dict(yield_=True),
                            symbolkwargs=dict(shown=True))
         ctl.cleanup()
     new_nodes = []
     for m in models:
         new_nodes.append(Node(SymbolSet(m), node.depth + 1, node))
     #print('Child nodes', [node.symbols.get_atoms('expanded', 2) for node in new_nodes])
     return new_nodes
Exemplo n.º 5
0
 def to_feature(self, id):
     with create_solver() as ctl:
         ctl.load(Logic.featureFile)
         ctl.addSymbols(self._symbols.get_all_atoms())
         ctl.ground([Logic.base, ('to_feature', [id])])
         sym = ctl.solve(solvekwargs=dict(yield_=True),
                         symbolkwargs=dict(shown=True))[0]
         symbols = SymbolSet(sym)
     return FeatureObj(id, symbols)
Exemplo n.º 6
0
 def as_role(self, id):
     with create_solver() as ctl:
         ctl.load(Logic.grammarFile)
         ctl.addSymbols(self.symbols.get_all_atoms())
         ctl.ground([Logic.base, ('to_role', [id])])
         sym = ctl.solve(solvekwargs=dict(yield_=True),
                         symbolkwargs=dict(shown=True))
         symbols = SymbolSet(sym[0])
     return Role(id, self.cost, symbols)
Exemplo n.º 7
0
def roles(sample):
    with create_solver(args=dict(arguments=['-n 0'])) as ctl:
        ctl.load([Logic.grammarFile])
        ctl.addSymbols(sample.get_sample())
        ctl.ground([Logic.base, Logic.roles])
        ctl.solve()
        ctl.ground([('show_role', [])])
        models = ctl.solve(solvekwargs=dict(yield_=True),
                           symbolkwargs=dict(shown=True))
    return [PreRole(SymbolSet(model), 1) for model in models]
Exemplo n.º 8
0
 def __call__(self) -> List[clingo.Symbol]:
     logging.debug('Calling Nullary')
     with create_solver(args=dict(arguments=['-n 0'])) as ctl:
         ctl.load([Logic.featureFile])
         ctl.addSymbols(self.sample.get_sample())
         ctl.ground(
             [Logic.base, Feature.primitiveFeature, Feature.processFeat])
         models = ctl.solve(solvekwargs=dict(yield_=True),
                            symbolkwargs=dict(shown=True))
     return [PreFeature(SymbolSet(model)) for model in models]
Exemplo n.º 9
0
 def __call__(self) -> List[Expression]:
     logging.debug('Calling Primitive')
     with create_solver(args=dict(arguments=['-n 0'])) as ctl:
         ctl.load([Logic.grammarFile])
         ctl.addSymbols(self.sample.get_sample())
         ctl.ground([
             Logic.base,
             Concept.primitive(1), ('cardinality', []), ('show_exp', [])
         ])
         models = ctl.solve(solvekwargs=dict(yield_=True),
                            symbolkwargs=dict(shown=True))
     return [Expression(SymbolSet(model), 1) for model in models]
Exemplo n.º 10
0
 def initial_state(self) -> Node:
     with create_solver() as ctl:
         self._init_solver(ctl)
         ctl.ground([Logic.base, initial_state(), evaluate()])
         ctl.solve()
         ctl.ground([show_expanded()])
         models = ctl.solve(solvekwargs=dict(yield_=True),
                            symbolkwargs=dict(shown=True))
         assert (len(models) == 1)
         #print('Models', models)
         symbols = SymbolSet(models[0])
     return Node(symbols, 0, None)
Exemplo n.º 11
0
 def make_state(self, id):
     assert (self.id == None)
     self._id = id
     with create_solver() as ctl:
         ctl.load(Logic.problem)
         ctl.load(Logic.sample_encoding)
         ctl.addSymbols(self.symbols.get_all_atoms())
         ctl.ground([Logic.base, to_state(id)])
         #print(ctl.getAtoms('expanded', 2))
         models = ctl.solve(solvekwargs=dict(yield_=True),
                            symbolkwargs=dict(shown=True))
         assert (len(models) == 1)
         self._symbols = SymbolSet(models[0])
Exemplo n.º 12
0
 def __call__(self) -> List[Expression]:
     logging.debug('Calling EqualRole')
     sym = []
     for r in self.roles:
         sym += r.symbols.get_all_atoms()
     with create_solver(args=dict(arguments=['-n 0'])) as ctl:
         ctl.load([Logic.grammarFile])
         ctl.addSymbols(self.sample.get_const() + self.sample.get_states())
         ctl.addSymbols(sym)
         ctl.ground([
             Logic.base,
             Concept.equalRole(3), ('cardinality', []), ('show_exp', [])
         ])
         models = ctl.solve(solvekwargs=dict(yield_=True),
                            symbolkwargs=dict(shown=True))
     return [Expression(SymbolSet(model), 3) for model in models]
Exemplo n.º 13
0
 def __call__(self) -> List[FeatureObj]:
     logging.debug('Calling Distance({})'.format(
         tuple(([c.id for c in co]
                for co in (self.conc1, self.conc, self.conc2)))))
     with create_solver(args=dict(arguments=['-n 0'])) as ctl:
         possym = []
         symbols = []
         for i, cs in enumerate((self.conc1, self.conc, self.conc2)):
             s = []
             for c in cs:
                 s += c.concept
                 symbols += c.symbols.get_all_atoms()
             ctl.load([Logic.featureFile])
             ctl.addSymbols(s)
             ctl.ground([Logic.base, ('dist_pos', [i])])
             models = ctl.solve(solvekwargs=dict(yield_=True),
                                symbolkwargs=dict(shown=True))
             assert (len(models) == 1)
             possym += models[0]
             ctl.reset(arguments=['-n 0'])
         for r in self.roles:
             symbols += r.symbols.get_all_atoms()
         #print(possym)
         ctl.load([Logic.featureFile])
         ctl.addSymbols(self.sample.get_states())
         ctl.addSymbols(self.sample.get_transitions())
         ctl.addSymbols(self.sample.get_const())
         ctl.addSymbols(symbols + possym)
         ctl.ground([Logic.base, Feature.distFeature(self.max_cost)])
         count = 1
         while True:
             ctl.ground([('dist_const', [count - 1])])
             models = ctl.solve(solvekwargs=dict(yield_=True),
                                symbolkwargs=dict(shown=True))
             assert (len(models) == 1)
             model = models[0]
             #print(model)
             if len(model) != count:
                 break
             count += 1
         ctl.ground([('value_dist', []), Feature.processFeat])
         models = ctl.solve(solvekwargs=dict(yield_=True),
                            symbolkwargs=dict(shown=True))
         logging.debug('Found {} features'.format(len(models)))
     return [PreFeature(SymbolSet(model)) for model in models]
Exemplo n.º 14
0
    def __call__(self) -> List[Expression]:
        logging.debug('Calling Negation({})'.format(
            [c.id for c in self.primitive]))
        sym = []
        for c in self.primitive:
            sym += c.symbols.get_all_atoms()
        with create_solver(args=dict(arguments=['-n 0'])) as ctl:
            ctl.load([Logic.grammarFile])
            ctl.addSymbols(sym)
            ctl.addSymbols(self.sample.get_const() + self.sample.get_states())
            ctl.ground([
                Logic.base,
                Concept.negation(2), ('cardinality', []), ('show_exp', [])
            ])
            models = ctl.solve(solvekwargs=dict(yield_=True),
                               symbolkwargs=dict(shown=True))

        return [Expression(SymbolSet(model), 2) for model in models]
Exemplo n.º 15
0
 def __call__(self) -> List[clingo.Symbol]:
     logging.debug('Calling ConceptFeat({})'.format(
         [c.id for c in self.concepts]))
     sym = []
     for c in self.concepts:
         sym += c.symbols.get_all_atoms()
     with create_solver(args=dict(arguments=['-n 0'])) as ctl:
         ctl.load([Logic.featureFile])
         ctl.addSymbols(sym)
         ctl.addSymbols(self.sample.get_states())
         ctl.addSymbols(self.sample.get_transitions())
         ctl.addSymbols(self.sample.get_const())
         #ctl.addSymbols(self.sample.get_sample())
         ctl.ground(
             [Logic.base, Feature.conceptFeature, Feature.processFeat])
         models = ctl.solve(solvekwargs=dict(yield_=True),
                            symbolkwargs=dict(shown=True))
     return [PreFeature(SymbolSet(model)) for model in models]
Exemplo n.º 16
0
 def __init__(self,
              symbols: SymbolSet,
              depth: int,
              parent,
              id=None,
              encoding=None):
     self._symbols = symbols
     self._id = id
     self._children = []
     self._transitions = []
     self._depth = depth
     if parent != None:
         if isinstance(parent, Node):
             self._parent = parent.id
         else:
             self._parent = parent
     else:
         self._parent = None
     self._goal = bool(symbols.count_atoms('goal', 1))
     self._hash = None
     self._sym_hash = {}
     self._calc_hash()
     self._encoding = encoding
Exemplo n.º 17
0
 def load(info):
     info['symbols'] = SymbolSet.from_pickle(info['symbols'])
     return Role(**info)
Exemplo n.º 18
0
 def load(info):
     info['symbols'] = SymbolSet.from_pickle(info['symbols'])
     return ConceptObj(**info)
Exemplo n.º 19
0
class Node:
    def __init__(self,
                 symbols: SymbolSet,
                 depth: int,
                 parent,
                 id=None,
                 encoding=None):
        self._symbols = symbols
        self._id = id
        self._children = []
        self._transitions = []
        self._depth = depth
        if parent != None:
            if isinstance(parent, Node):
                self._parent = parent.id
            else:
                self._parent = parent
        else:
            self._parent = None
        self._goal = bool(symbols.count_atoms('goal', 1))
        self._hash = None
        self._sym_hash = {}
        self._calc_hash()
        self._encoding = encoding

    def store(self):
        info = {
            'symbols': self._symbols.to_pickle(),
            'id': self.id,
            'children': self.children,
            'depth': self.depth,
            'parent': self.parent,
            'encoding': self._encoding.to_pickle()
        }
        return info

    @staticmethod
    def load_state(info):
        def load(symbols, id, children, depth, parent, encoding):
            node = Node(SymbolSet.from_pickle(symbols),
                        depth,
                        parent,
                        id=id,
                        encoding=SymbolSet.from_pickle(encoding))
            node._children = children
            for child in node.children:
                node._transitions.append(
                    clingo.Function('transition', [node.id, child]))
            return node

        return load(**info)

    @property
    def id(self) -> Optional[int]:
        return self._id

    @property
    def name(self) -> clingo.Symbol:
        return clingo.Number(self.id)

    @property
    def symbols(self) -> SymbolSet:
        return self._symbols

    @property
    def transitions(self) -> List[clingo.Symbol]:
        return self._transitions

    @property
    def depth(self) -> int:
        return self._depth

    @property
    def parent(self) -> Optional[int]:
        return self._parent

    @property
    def is_goal(self) -> bool:
        return self._goal

    @property
    def state(self):
        return self._symbols.get_atoms('state', 2) + self._symbols.get_atoms('stateId', 2) \
            + self._symbols.get_atoms('state', 1)

    @property
    def goal(self):
        return self._symbols.get_atoms('goal', 1)

    def add_child(self, child):
        self._children.append(child.id)
        self._transitions.append(
            clingo.Function('transition', [self.id, child.id]))

    @property
    def children(self):
        return self._children

    def _calc_hash(self):
        if self._hash != None: return
        self._hash = 0
        for var, val in self.description:
            self._hash += hash(str(var) + str(val))
            self._sym_hash[(SymbolHash(var), SymbolHash(val))] = True

    @property
    def description(self):
        return [s.arguments[0:2] for s in self._symbols.get_atoms('holds', 3)]

    def holds(self, variable: clingo.Symbol, value: clingo.Symbol):
        return self._sym_hash.get((SymbolHash(variable), SymbolHash(value)),
                                  False)

    def __hash__(self):
        return self._hash

    def __eq__(self, other):
        global time_eq
        start = time.time()
        if not isinstance(other, Node):
            NotImplemented
        if hash(self) != hash(other):
            return False
        holds = self.description
        if len(holds) != len(other.description): return False
        for var, val in holds:
            if not other.holds(var, val):
                time_eq += time.time() - start
                return False
        time_eq += time.time() - start
        return True

    def make_state(self, id):
        assert (self.id == None)
        self._id = id
        with create_solver() as ctl:
            ctl.load(Logic.problem)
            ctl.load(Logic.sample_encoding)
            ctl.addSymbols(self.symbols.get_all_atoms())
            ctl.ground([Logic.base, to_state(id)])
            #print(ctl.getAtoms('expanded', 2))
            models = ctl.solve(solvekwargs=dict(yield_=True),
                               symbolkwargs=dict(shown=True))
            assert (len(models) == 1)
            self._symbols = SymbolSet(models[0])
        #print(self.state)

    def encode(self, prob):
        assert (self._encoding == None)
        with create_solver() as ctl:
            ctl.load(Logic.problem)
            ctl.load(Logic.sample_encoding)
            ctl.addSymbols(self.symbols.get_all_atoms() + prob.predicates +
                           prob.const)
            ctl.ground([Logic.base, hold(), encode()])
            models = ctl.solve(solvekwargs=dict(yield_=True),
                               symbolkwargs=dict(shown=True))
            #print(ctl.getAtoms('hold', 3), len(models))
            assert (len(models) == 1)
            self._encoding = SymbolSet(models[0])

    @property
    def encoding(self) -> Optional[List[clingo.Symbol]]:
        return self._encoding.get_all_atoms()
Exemplo n.º 20
0
 def load(id, symbols):
     return FeatureObj(id, SymbolSet.from_pickle(symbols))