18
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

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

Embed Size (px)

Citation preview

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

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

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

Finding subsets

• In PrologsubSet([],_).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?

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

Generation of Stable Models

• A pair of rulesa :- not bb :- 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 2N stable models

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

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.

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

Restricting Stable Models

• A rulea :- 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

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

N-Queens

• Place N queens in a NxN chess board so that none attacks no other.% Generating modelshasQueen(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)

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

The facts (in smodels)

• Define the domain of predicates and the specific program• Possible to write in abbreviated form, and by resorting to

constantsconst size=8.column(1..size).row(1..size).hide.show hasQueen(X,Y).

• Solutions by:> lparse –c size=4 | smodels 0

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

N-Queens version 2

• Generate less, such that no two queens appear in the same row or column.% Generating modelshasQueen(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)).

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

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 2hasTwo :- 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.

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

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 columns1 {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)).

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

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

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

Graph colouringarc(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

wisill

iow ind

mic

ohio

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

Graph colouring

%generatecol(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).

%auxiliarycon(X,Y) :- arc(X,Y).con(X,Y) :- arc(Y,X).

node(N) :- con(N,C).

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

min

wis

ill

iow ind

mic

ohio

One colouring solution

min

wis

ill

iow ind

mic

ohio

Answer: 1Stable Model: col(minnesota,blue) col(wisconsin,green) col(michigan,red) col(indiana,green) col(illinois,blue) col(iowa,red) col(ohio,blue)

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

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).

a b

d c

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

Hamiltonian paths% Subsets of arcsin_arc(X,Y) :- arc(X,Y), not out_arc(X,Y).out_arc(X,Y) :- arc(X,Y), not in_arc(X,Y).

% Nodesnode(N) :- arc(N,_).node(N) :- arc(_,N).

% Notion of reachablereachable(X) :- initial(X).reachable(X) :- in_arc(Y,X), reachable(Y).

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

Hamiltonian paths

% initial is one (and only one) of the nodesinitial(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).

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

Hamiltonian paths (solutions)

a b

d c

{in_arc(a,d), in_arc(b,c), in_arc(d,b)}{in_arc(a,d), in_arc(b,a), in_arc(d,c)}