Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with SMs A new paradigm of problem representation with Logic Programming (Answer-Set Programming – ASP) –A problem is represented as (part.

Similar presentations


Presentation on theme: "Programming with SMs A new paradigm of problem representation with Logic Programming (Answer-Set Programming – ASP) –A problem is represented as (part."— Presentation transcript:

1 Programming with SMs A new paradigm of problem representation with Logic Programming (Answer-Set Programming – ASP) –A problem is represented as (part of) a logic program (intentional database) –An instance of a problem is represented as a set of fact (extensional database) –Solution of the problems are the models of the complete program In Prolog –A problem is represented by a program –Instances are given as queries –Solutions are substitutions

2 Finding subsets In Prolog subSet([],_). subSet([E|Ss],[_|S]) :- subSet([E|Ss],S). subSet([E|Ss],[E|S]) :- subSet(Ss,S). ?- subset(X,[1,2,3]). In ASP: –Program: in_sub(X) :- element(X), not out_sub(X). out_sub(X) :- element(X), not in_sub(X). –Facts: element(1). element(2). element(3). –Each stable model represents one subset. Which one do you find more declarative?

3 Generation of Stable Models A pair of rules a :- not b b :- not a generates two stable models: one with a and another with b. Rules: a(X) :- elem(X), not b(X). b(X) :- elem(X), not a(X). with elem(X) having N solutions, generates 2 N stable models

4 Small subsets From the previous program, eliminate stable models with more than one member –I.e. eliminate all stable models where in_sub(X), in_sub(Y), X ≠ Y Just add rule: foo :- element(X), in_sub(X), in_sub(Y), not eq(X,Y), not foo. %eq(X,X). Since there is no notion of query, it is very important to guarantee that it is possible to ground programs. –All variables appearing in a rule must appear in a predicate that defines the domains, and make it possible to ground it (in the case, the element(X) predicates.

5 Restricting Stable Models A rule a :- cond, not a. eliminates all stable models where cond is true. In most ASP solvers, this is simply written as an integrity constraint :- cond. An ASP programs usually has: –A part defining the domain (and specific instance of the problem) –A part generating models –A part eliminating models

6 N-Queens Place N queens in a NxN chess board so that none attacks no other. %Generating models hasQueen(X,Y) :- row(X), column(Y), not noQueen(Q,X,Y). noQueen(X,Y) :- row(X), column(Y), not hasQueen(Q,X,Y). %Eliminating models %No 2 queens in the same line or column or diagnonal :- row(X), column(Y), row(XX), hasQueen(X,Y), hasQueen(XX,Y), not eq(X,XX). :- row(X), column(Y), column(YY), hasQueen(X,Y), hasQueen(X,YY), not eq(Y,YY). :- row(X), column(Y), row(XX), column(YY), hasQueen(X,Y), hasQueen(XX,YY), not eq(abs(X-XX), abs(Y-YY)). %All rows must have at least one queen :- row(X), not hasQueen(X). hasQueen(X) :- row(X), column(Y), hasQueen(X,Y)

7 The facts (in smodels) Define the domain of predicates and the specific program Possible to write in abbreviated form, and by resorting to constants const size=8. column(1..size). row(1..size). hide. show hasQueen(X,Y). Solutions by: > lparse –c size=4 | smodels 0

8 N-Queens version 2 Generate less, such that no two queens appear in the same row or column. %Generating models hasQueen(X,Y) :- row(X), column(Y), not noQueen(Q,X,Y). noQueen(X,Y) :- row(X), column(Y), column(YY), not eq(Y,YY), hasQueen(X,YY). noQueen(X,Y) :- row(X), column(Y), rwo(XX), not eq(X,XX), hasQueen(XX,Y). This already guarantees that all rows have a queen. Elimination of models is only needed for diagonals: %Eliminating models :- row(X), column(Y), row(XX), column(YY), hasQueen(X,Y), hasQueen(XX,YY), not eq(abs(X-XX), abs(Y-YY)).

9 Back to subsets in_sub(X) :- element(X), not out_sub(X). out_sub(X) :- element(X), not in_sub(X). Generate subsets with at most 2 :- element(X), element(Y), element(Z), not eq(X,Y), not eq(Y,Z), not eq(X,Z), in_sub(X), in_sub(Y), in_sub(Z). Generate subsets with at least 2 hasTwo :- element(X), element(Y), not eq(X,Y), in_sub(X), in_sub(Y). :- not hasTwo. It could be done for any maximum and minimum Smodels has simplified notation for that: 2 {in_sub(X): element(X) } 2.

10 Simplified notation in Smodels Generate models with between N and M elements of P(X) that satisfy Q(X), given R. N {P(X):Q(X)} M :- R Example: %Exactly one hasQueen(X,Y) per model for each row(X) given column(Y) 1 {hasQueen(X,Y):row(X)} 1 :- column(Y). %Same for columns 1 {hasQueen(X,Y):column(Y)} 1 :- row(X). %Elimination in diagonal :- row(X), column(Y), row(XX), column(YY), hasQueen(X,Y), hasQueen(XX,YY), not eq(abs(X-XX), abs(Y-YY)).

11 Graph colouring Problem: find all colourings of a map of countries using not more than 3 colours, such that neighbouring countries are not given the same colour. The predicate arc connects two countries. Use ASP rules to generate colourings, and integrity constraints to eliminate unwanted solutions

12 Graph colouring arc(minnesota, wisconsin).arc(illinois, iowa). arc(illinois, michigan).arc(illinois, wisconsin). arc(illinois, indiana).arc(indiana, ohio). arc(michigan, indiana).arc(michigan, ohio). arc(michigan, wisconsin).arc(minnesota, iowa). arc(wisconsin, iowa).arc(minnesota, michigan). col(Country,Colour) ?? min wis ill iowind mic ohio

13 Graph colouring %generate col(C,red) :- node(C), not col(C,blue), not col(C,green). col(C,blue) :- node(C), not col(C,red), not col(C,green). col(C,green) :- node(C), not col(C,blue), not col(C,red). %eliminate :- colour(C), con(C1,C2), col(C1,C), col(C2,C). %auxiliary con(X,Y) :- arc(X,Y). con(X,Y) :- arc(Y,X). node(N) :- con(N,C).

14 min wis ill iowind mic ohio One colouring solution min wis ill iowind mic ohio Answer: 1 Stable Model: col(minnesota,blue) col(wisconsin,green) col(michigan,red) col(indiana,green) col(illinois,blue) col(iowa,red) col(ohio,blue)

15 Hamiltonian paths Given a graph, find all Hamiltonian paths arc(a,b). arc(a,d). arc(b,a). arc(b,c). arc(d,b). arc(d,c). ab dc

16 Hamiltonian paths % Subsets of arcs in_arc(X,Y) :- arc(X,Y), not out_arc(X,Y). out_arc(X,Y) :- arc(X,Y), not in_arc(X,Y). % Nodes node(N) :- arc(N,_). node(N) :- arc(_,N). % Notion of reachable reachable(X) :- initial(X). reachable(X) :- in_arc(Y,X), reachable(Y).

17 Hamiltonian paths % initial is one (and only one) of the nodes initial(N) :- node(N), not non_initial(N). non_initial(N) :- node(N), not initial(N). :- initial(N1), initial(N2), not eq(N1,N2). % In Hamiltonian paths all nodes are reachable :- node(N), not reachable(N). % Paths must be connected subsets of arcs % I.e. an arc from X to Y can only belong to the path if X is reachable :- arc(X,Y), in_arc(X,Y), not reachable(X). % No node can be visited more than once :- node(X), node(Y), node(Z), in_arc(X,Y), in_arc(X,Z), not eq(Y,Z).

18 Hamiltonian paths (solutions) ab dc {in_arc(a,d), in_arc(b,c), in_arc(d,b)} {in_arc(a,d), in_arc(b,a), in_arc(d,c)}


Download ppt "Programming with SMs A new paradigm of problem representation with Logic Programming (Answer-Set Programming – ASP) –A problem is represented as (part."

Similar presentations


Ads by Google