Friday 10 February 2012

Lparse 1.0 User's Manual - Smodels simple example

Today I ran my first ASP program and find the solution using the smodels solver. I code the Node Coloring problem. In this problem we are given a number of nodes and a set of edges that connect the nodes. The problem is to use some fixed number of colors to color each node so that two adjacent nodes do not have the same color.

color1.lp:
color(red). color(blue). color(yellow).
col(X,red)    :- node(X), not col(X,blue), not col(X,yellow).
col(X,blue)   :- node(X), not col(X,red), not col(X,yellow).
col(X,yellow) :- node(X), not col(X,blue), not col(X,red).
fail          :- edge(X,Y), color(AB), col(X,AB), col(Y,AB).
compute 1 { not fail }.
graph1:
node(a). node(b).
node(c). node(d).
edge(a,b). edge(b,c).
edge(c,d). edge(d,a).
Representing a graph with four nodes and four edges.
And to run it:
$ lparse color1.lp graph1 | smodels
After that it will show the stable model for this program:
Answer: 1
Stable Model: edge(d,a) edge(c,d) edge(b,c) edge(a,b) 
node(d) node(c) node(b) node(a) 
col(a,yellow) col(c,blue) col(d,red) col(b,red) 
color(yellow) color(blue) color(red) 
True
Duration: 0.000
Number of choice points: 3
Number of wrong choices: 0
Number of atoms: 25
Number of rules: 35
Number of picked atoms: 41
Number of forced atoms: 0
Number of truth assignments: 139
Size of searchspace (removed): 12 (0)

No comments:

Post a Comment