essential thinking. introduction to constraint programming example

25
Essential Thinking. Introduction to Constraint Programming Example Problems IV Antoni Lig ˛ eza Faculty of EEACSE Department of Automatics AGH’2011 Kraków, Poland A Lig ˛ eza (AGH-UST) Essential Thinking 2011 1 / 25

Upload: truongcong

Post on 11-Jan-2017

244 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Essential Thinking. Introduction to Constraint Programming Example

Essential Thinking.Introduction to Constraint ProgrammingExample Problems IV

Antoni Ligeza

Faculty of EEACSEDepartment of Automatics

AGH’2011Kraków, Poland

A Ligeza (AGH-UST) Essential Thinking 2011 1 / 25

Page 2: Essential Thinking. Introduction to Constraint Programming Example

Outline

1 References

2 Introduction

3 What is CSP?

4 Backtracking Search and Heuristics

5 Exploring Problem Structure

6 Tools: Prolog clp(fd) Library

7 Further Example Problems

A Ligeza (AGH-UST) Essential Thinking 2011 2 / 25

Page 3: Essential Thinking. Introduction to Constraint Programming Example

References

1 Stuart J. Russel, Peter Norvig: Artificial Intelligence. A Modern Approach.Third Edition. Pearson, Prentice Hall, Boston, 2010.http://aima.cs.berkeley.edu/.

2 Ivan Bratko: Prolog Programming for Artificial Intelligence. Fourth Edition,2011. Pearson, Addison Wesley, 2012. http://www.pearsoned.co.uk/HigherEducation/Booksby/Bratko/

3 Frank van Harmelen, Vladimir Lifschitz, Bruce Porter (Eds.): Handbook ofKnowledge Representation. Elsevier B.V., Amsterdam, 2008.

4 Michael Negnevitsky: Artificial Intelligence. A Guide to IntelligentSystems. Addison-Wesley, Pearson Education Limited, Harlow, England,2002.

5 Adrian A. Hopgood: Intelligent Systems for Engineers and Scientists.CRC Press, Boca Raton, 2001.

6 Joseph C. Giarratano, Gary D. Riley: Expert Systems. Principles andProgramming. Fourth Edition, Thomson Course Technology, 2005.

A Ligeza (AGH-UST) Essential Thinking 2011 3 / 25

Page 4: Essential Thinking. Introduction to Constraint Programming Example

References

1 George Polya: How to Solve it?. Princeton University Press, 1945; PWN1993. http://en.wikipedia.org/wiki/How_to_Solve_It.

2 John Mason, Leone Burton, Kaye Stacey: Thinking Mathematically.Addison-Wesley, 1985; WSiP, 2005.

3 Mordechai Ben-Ari: Mathematical Logic for Computer Science.Springer-Verlag, London, 2001.

4 Michael R. Genesereth, Nils J. Nilsson: Logical Foundations of ArtificialIntelligence. Morgan Kaufmann Publishers, Inc., Los Altos, California,1987.

5 Zbigniew Huzar: Elementy logiki dla informatyków. Oficyna WyawniczaPolitechniki Wrocławskiej, Wrocław, 2007.

6 Peter Jackson: Introduction to Expert Systems. Addison-Wesley, Harlow,England, 1999.

7 Antoni Ligeza: Logical Foundations for Rule-Based Systems.Springer-Verlag, berlin, 2006.

A Ligeza (AGH-UST) Essential Thinking 2011 4 / 25

Page 5: Essential Thinking. Introduction to Constraint Programming Example

References

1 Krzysztof R. Apt: Principles of Constraint Programming. CambridgeUniversity Press, Cambridge, UK, 2006.

2 Krzysztof R. Apt and Mark Wallace: Constraint Logic Programming UsingECLiPSe. Cambridge University Press, Cambridge, UK, 2006.

3 Rina Dechter: Constraint Processing. Morgan Kaufmann Publishers, SanFrancisco, CA, 2003.

4 Antoni Niederlinski: Programowanie w logice z ograniczeniami. Łagodnewprowadzenie do platformy ECLiPSe. PKJS, Gliwice, 2010(http://www.pwlzo.pl/).

5 Roman Bartak: On-line Guide to Constraint Programming.http://kti.mff.cuni.cz/ bartak/constraints/index.html.

6 http://en.wikibooks.org/wiki/Prolog/Constraint_Logic_Programming.

7 http://eclipseclp.org/

A Ligeza (AGH-UST) Essential Thinking 2011 5 / 25

Page 6: Essential Thinking. Introduction to Constraint Programming Example

What is worth learning?

A bit provocative position statementLanguages — enable communication and knowledge representation;Wieviel Sprachen du sprichst, sooftmal bist du Mensch; GoetheProblem Solving — analytical thinking; cross-curricular competencies,

Learning — persistent learning, quick learning, focused learning, learning

on-demand, ...

Tools — finding and using proper tools for specificproblems.

I conceptual tools,I software tools.

A Ligeza (AGH-UST) Essential Thinking 2011 6 / 25

Page 7: Essential Thinking. Introduction to Constraint Programming Example

Thinking — What is the Essence of it?

A Ligeza (AGH-UST) Essential Thinking 2011 7 / 25

Page 8: Essential Thinking. Introduction to Constraint Programming Example

An Inspiration Example

A cryptoarithemtic problem

SEND+ MORE------MONEY

A Ligeza (AGH-UST) Essential Thinking 2011 8 / 25

Page 9: Essential Thinking. Introduction to Constraint Programming Example

An Intuitive Introduction

1 ?- [library(clpfd)].2

3 ?- X #> 3.4 X in 4..sup.5

6 ?- X #\= 20.7 X in inf..19\/21..sup.8

9 ?- 2*X #= 10.10 X = 5.11

12 ?- X*X #= 144.13 X in -12\/12.14

15 ?- 4*X + 2*Y #= 24, X + Y #= 9, [X,Y] ins 0..sup.16 X = 3, Y = 6.17

18 ?- Vs = [X,Y,Z], Vs ins 1..3, all_different(Vs), X = 1, Y #\= 2.19 Vs = [1, 3, 2], X = 1, Y = 3, Z = 2.

A Ligeza (AGH-UST) Essential Thinking 2011 9 / 25

Page 10: Essential Thinking. Introduction to Constraint Programming Example

Basic Constraints

clp(fd) — intro

1 ?- [library(clpfd)].2

3 Expr1 #>= Expr2 % Expr1 is larger than or equal to Expr24

5 Expr1 #=< Expr2 % Expr1 is smaller than or equal to Expr26

7 Expr1 #= Expr2 % Expr1 equals Expr28

9 Expr1 #\= Expr2 % Expr1 is not equal to Expr210

11 Expr1 #> Expr2 % Expr1 is strictly larger than Expr212

13 Expr1 #< Expr2 % Expr1 is strictly smaller than Expr2

A Ligeza (AGH-UST) Essential Thinking 2011 10 / 25

Page 11: Essential Thinking. Introduction to Constraint Programming Example

Basic Constraints

clp(fd) — intro

1 ?- [library(clpfd)].2

3 #\ Q % True iff Q is false4

5 P #\/ Q % True iff either P or Q6

7 P #/\ Q % True iff both P and Q8

9 P #<==> Q % True iff P and Q are equivalent10

11 P #==> Q % True iff P implies Q12

13 P #<== Q % True iff Q implies P

A Ligeza (AGH-UST) Essential Thinking 2011 11 / 25

Page 12: Essential Thinking. Introduction to Constraint Programming Example

Basic Constraints

clp(fd) — intro

1 ?Var in Domain2

3 ?Vars ins Domain

Var is an element of Domain. Domain is one of:

IntegerSingleton set consisting only of Integer.

Lower .. UpperAll integers I such that Lower =< I =< Upper.Lower must be an integer or the atom inf, which denotes negative infinity.Upper must be an integer or the atom sup, which denotes positive infinity.

Domain1 \/ Domain2The union of Domain1 and Domain2.

A Ligeza (AGH-UST) Essential Thinking 2011 12 / 25

Page 13: Essential Thinking. Introduction to Constraint Programming Example

Basic Constraints

clp(fd) — intro

1 label(+Vars)2

3 labeling(+Options, +Vars)

Labeling means systematically trying out values forthe finite domain variables Vars until all of them are ground.The domain of each variable in Vars must be finite.Options is a list of options that let you exhibitsome control over the search process.

clp(fd) — intro

1 indomain(+Var)

Bind Var to all feasible values of its domain on backtracking.The domain of Var must be finite.

A Ligeza (AGH-UST) Essential Thinking 2011 13 / 25

Page 14: Essential Thinking. Introduction to Constraint Programming Example

Basic Constraints

clp(fd) — intro

1 all_different(+Vars)2

3 all_distinc(+Vars)

Vars are pairwise distinct.

The second command has stronger propagation(can detect inconsistency).

A Ligeza (AGH-UST) Essential Thinking 2011 14 / 25

Page 15: Essential Thinking. Introduction to Constraint Programming Example

Basic Constraints

clp(fd) — intro

1 sum(+Vars, +Rel, +Expr)

The sum of elements of the list Vars is in relation Relto Expr, where Rel is #=, #\=, #<, #>, #=< or #>=.For example:

1 ?- [A,B,C] ins 0..sup, sum([A,B,C], #=, 100).2 A in 0..100,3 A+B+C#=100,4 B in 0..100,5 C in 0..100.

A Ligeza (AGH-UST) Essential Thinking 2011 15 / 25

Page 16: Essential Thinking. Introduction to Constraint Programming Example

The SEND+MORE=MONEY Example

Code

1 :- use_module(library(clpfd)).2

3 puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]) :-4 Vars = [S,E,N,D,M,O,R,Y],5 Vars ins 0..9,6 all_different(Vars),7 S*1000 + E*100 + N*10 + D +8 M*1000 + O*100 + R*10 + E #=9 M*10000 + O*1000 + N*100 + E*10 + Y,10 M #\= 0, S #\= 0.11

12 ?- puzzle(As+Bs=Cs), label(As).13 As = [9, 5, 6, 7],14 Bs = [1, 0, 8, 5],15 Cs = [1, 0, 6, 5, 2] ;16 false.

A Ligeza (AGH-UST) Essential Thinking 2011 16 / 25

Page 17: Essential Thinking. Introduction to Constraint Programming Example

Bidirectional Factorial

Code

1 :- use_module(library(clpfd)).2

3 n_factorial(0, 1).4 n_factorial(N, F) :- N #> 0, N1 #= N - 1, F #= N * F1, n_factorial(N1, F1).5

6 ?- n_factorial(47, F).7 F = 258623241511168180642964355153611979969197632389120000000000 ;8 false.9

10 ?- n_factorial(N, 1).11 N = 0 ;12 N = 1 ;13 false.14

15 ?- n_factorial(N, 3).16 false.

A Ligeza (AGH-UST) Essential Thinking 2011 17 / 25

Page 18: Essential Thinking. Introduction to Constraint Programming Example

N-Queens

2

2

1

2

3

1

2

3

3

2

3

2

3

0

A Ligeza (AGH-UST) Essential Thinking 2011 18 / 25

Page 19: Essential Thinking. Introduction to Constraint Programming Example

Examples

A cryptoarithemtic problem

SEND+ MORE------MONEY

A Ligeza (AGH-UST) Essential Thinking 2011 19 / 25

Page 20: Essential Thinking. Introduction to Constraint Programming Example

Examples

A cryptoarithemtic problem

DONALD+ GERALD------ROBERT

A Ligeza (AGH-UST) Essential Thinking 2011 20 / 25

Page 21: Essential Thinking. Introduction to Constraint Programming Example

Examples

A cryptoarithemtic problem

CROSS+ ROADS------DANGER

A Ligeza (AGH-UST) Essential Thinking 2011 21 / 25

Page 22: Essential Thinking. Introduction to Constraint Programming Example

Enoxamples

A cryptoarithemtic problem

TEN+ TENFORTY------SIXTY

A Ligeza (AGH-UST) Essential Thinking 2011 22 / 25

Page 23: Essential Thinking. Introduction to Constraint Programming Example

A Generic Problem Example

A Ligeza (AGH-UST) Essential Thinking 2011 23 / 25

Page 24: Essential Thinking. Introduction to Constraint Programming Example

A Generic Problem Example

A Ligeza (AGH-UST) Essential Thinking 2011 24 / 25

Page 25: Essential Thinking. Introduction to Constraint Programming Example

Another Example: The Zebra Puzzle

a) Norweg zamieszkuje pierwszy dom;b) Anglik mieszka w czerwonym domu;c) Zielony dom znajduje sie po lewej stronie domu białego;d) Dunczyk pija herbatke;e) Palacz Rothmansów mieszka obok hodowcy kotów;f) Mieszkaniec zółtego domu pali Dunhille;

g) Niemiec pali Marlboro;h) Mieszkaniec srodkowego domu pija mleko;i) Palacz Rothmansów ma sasiada, który pija wode;j) Palacz Pall Malli hoduje ptaki;

k) Szwed hoduje psy;l) Norweg mieszka obok niebieskiego domu;

m) Hodowca koni mieszka obok zółtego domu;n) Palacz Philip Morris pija piwo;o) W zielonym domu pija sie kawe.

A Ligeza (AGH-UST) Essential Thinking 2011 25 / 25