PyASP basics

In the following I try to explain the basics on how to use the PyASP library. I assume that pyasp is already installed on your system. This tutorial has been tested with Python 3 and pyasp 1.4.4. You can download the logic programs that are used in this example here -> queens.lp and facts.lp.

First you need to download the logic programs for our running example.


In [1]:
from urllib.request import urlretrieve
urlretrieve('http://sthiele.github.io/data/queens.lp','queens.lp')
urlretrieve('http://sthiele.github.io/data/facts.lp','facts.lp')


Out[1]:
('facts.lp', <http.client.HTTPMessage at 0x7f213c1ade10>)

Import the pyasp library.


In [2]:
from pyasp.asp import *

Create a solver object. Before creating a solver object you need to declare what options your solver should run with. There are two kind of options the options for the grounder (see gringo documentation) and the options for the solver (see clasp documentation). In this example our only solver option is '2' which means we are looking for atmost 2 solutions.


In [3]:
goptions = ''
soptions = ' 2'
solver   = Gringo4Clasp(gringo_options=goptions, clasp_options=soptions)

Start the solver with some input. Just like gringo the run method of the solver object accepts a list of input files. Here we use the encoding of the queens problems and solve it for a size of 10 as declared in facts.lp.


In [4]:
result   = solver.run(['queens.lp', 'facts.lp'], collapseTerms=True, collapseAtoms=False)

The result is a list of the solutions as TermSets.


In [5]:
print(result)


[TermSet({Term('q',['3','4']), Term('q',['2','10']), Term('q',['8','3']), Term('q',['10','9']), Term('q',['9','6']), Term('q',['6','2']), Term('q',['7','5']), Term('q',['4','8']), Term('q',['1','1']), Term('q',['5','7'])}), TermSet({Term('q',['3','4']), Term('q',['2','10']), Term('q',['6','3']), Term('q',['7','2']), Term('q',['10','9']), Term('q',['4','8']), Term('q',['9','5']), Term('q',['8','6']), Term('q',['1','1']), Term('q',['5','7'])})]

Create your own set of facts. You can also create your own facts from within python and feed it as input to the solver. So far we have solved the 10-queens problem. Now we create a new set of facts including 'd(11)' and 'd(12)' and solve the 12-queens problem.


In [6]:
newfacts = TermSet()
newterm1 = Term('d', ["11"])
newfacts.add(newterm1)
newterm2 = Term('d', ["12"])
newfacts.add(newterm2)
result   = solver.run(['queens.lp', 'facts.lp', newfacts.to_file()], collapseTerms=True, collapseAtoms=False)

Now the result contains 2 solutions to the 12-queens problem.


In [7]:
print(result)


[TermSet({Term('q',['10','7']), Term('q',['4','12']), Term('q',['3','8']), Term('q',['12','1']), Term('q',['9','3']), Term('q',['11','11']), Term('q',['7','9']), Term('q',['2','5']), Term('q',['8','6']), Term('q',['1','2']), Term('q',['5','4']), Term('q',['6','10'])}), TermSet({Term('q',['10','7']), Term('q',['4','12']), Term('q',['3','8']), Term('q',['9','1']), Term('q',['11','11']), Term('q',['7','9']), Term('q',['2','5']), Term('q',['8','6']), Term('q',['1','2']), Term('q',['5','4']), Term('q',['6','10']), Term('q',['12','3'])})]

Parse and pretty print your solutions.


In [8]:
count=1
for s in result :
  print('Solution '+str(count)+':')
  print('   ', end=' ')
  for a in s :
    args= ",".join(a.args())
    print(a.pred(),'(',args,')',sep='',end=' ')
  print()
  count+=1


Solution 1:
    q(10,7) q(4,12) q(3,8) q(12,1) q(9,3) q(11,11) q(7,9) q(2,5) q(8,6) q(1,2) q(5,4) q(6,10) 
Solution 2:
    q(10,7) q(4,12) q(3,8) q(9,1) q(11,11) q(7,9) q(2,5) q(8,6) q(1,2) q(5,4) q(6,10) q(12,3) 

In [ ]: