C. Varela1 Logic Programming (CTM 12.3-12.5) Constraint Programming: Constraints and Computation Spaces Carlos Varela Rennselaer Polytechnic Institute.

Slides:



Advertisements
Similar presentations
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Advertisements

C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model From kernel to practical language (CTM 2.6) Exceptions (CTM.
Best-First Search: Agendas
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management, Exceptions, From kernel to practical language.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Data Abstraction and State Abstract Data Types (3.7) State/Data Abstraction(6.1, 6.3, 6.4.1,
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Introduction to Programming Concepts Carlos Varela RPI Adapted with permission from: Seif.
Constraint Satisfaction Problems
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
C. Varela; Adapted with permission from S. Haridi and P. Van Roy1 Declarative Concurrency (VRH4) Carlos Varela RPI Adapted with permission from: Seif Haridi.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management, Exceptions, From kernel to practical language.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Non-declarative needs (VRH 3.8) Program design in the.
Branch and Bound Algorithm for Solving Integer Linear Programming
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Declarativeness, iterative computation (VRH 3.1-2) Higher-order.
Backtracking.
Decision Procedures An Algorithmic Point of View
1. Copyright © 2006 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Polynomials CHAPTER 5.1Exponents and Scientific Notation 5.2Introduction.
S. Haridi and P. Van Roy1 Declarative Concurrency Seif Haridi KTH Peter Van Roy UCL.
9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC Lecture 8 (Multiple) Threads Semantics.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
WAES 3308 Numerical Methods for AI
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 2 Graphs and Functions Copyright © 2013, 2009, 2005 Pearson Education, Inc.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
S. Haridi and P. Van Roy1 Lazy Execution Seif Haridi KTH Peter Van Roy UCL.
1 Exception Handling, Modules, Names Per Brand. 2 Exception Handling An exception is any expression E. To raise the exception, one executes the following.
1 Causal-Consistent Reversible Debugging Ivan Lanese Focus research group Computer Science and Engineering Department University of Bologna/INRIA Bologna,
10/24/2015COSC , Lecture 101 Programming Language Concepts, COSC Lecture 10 Relational Programming Constraints programming.
1 Lecture 2: Basic Control and Procedures Per Brand.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC Lecture 4 Procedures, last call optimization.
Agenda Basic Logic Purpose if statement if / else statement
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
On the Relation between SAT and BDDs for Equivalence Checking Sherief Reda Rolf Drechsler Alex Orailoglu Computer Science & Engineering Dept. University.
S. Haridi and P. Van Roy1 Introduction to Programming Seif Haridi KTH Peter Van Roy UCL.
Goal-based Problem Solving Goal formation Based upon the current situation and performance measures. Result is moving into a desirable state (goal state).
C. Varela1 Logic Programming (PLP 11, CTM 9.2, 9.4, ) Constraint Satisfaction Problems, Natural Language Parsing (Definite Clause Grammars) Carlos.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Problem Reduction So far we have considered search strategies for OR graph. In OR graph, several arcs indicate a variety of ways in which the original.
Branch and Bound Algorithms Present by Tina Yang Qianmei Feng.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
Analysis & Design of Algorithms (CSCE 321)
1 Lecture 4: Concurrency Per Brand. 2 Threads and the store Store f Thread 1Thread 2Thread 3 Thread 4 ‘|’ Two runnable and two suspended threads Execution.
Adversarial Search 2 (Game Playing)
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Higher-Order Programming: Iterative computation (CTM Section 3
Carlos Varela Rennselaer Polytechnic Institute November 18, 2016
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
Higher-Order Programming: Iterative computation (CTM Section 3
Carlos Varela RPI September 22, 2017 Adapted with permission from:
Declarative Programming Techniques Accumulators (CTM 3. 4
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute November 21, 2017
Carlos Varela Rensselaer Polytechnic Institute December 5, 2017
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
Algorithm Discovery and Design
Introduction to Programming Concepts (VRH )
Declarative Computation Model Single assignment store (VRH 2
Declarative Programming Techniques Accumulators (CTM 3. 4
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Presentation transcript:

C. Varela1 Logic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces Carlos Varela Rennselaer Polytechnic Institute December 4, 2015

Generate and Test Example We can use the relational computation model to generate all digits: fun {Digit} choice 0 [] 1 [] 2 [] 3 [] 4 [] 5 [] 6 [] 7 [] 8 [] 9 end end {Browse {Search.base.all Digit}} % displays [ ] C. Varela2

Finding palindromes Find all four-digit palindromes that are products of two- digit numbers: fun {Palindrome} X in X = (10*{Digit}+{Digit})*(10*{Digit}+{Digit}) % generate (X>=1000) = true % test (X div 1000) mod 10 = (X div 1) mod 10 % test (X div 100) mod 10 = (X div 10) mod 10 % test X end {Browse {Search.base.all Palindrome}} % 118 solutions C. Varela3

Propagate and Search The generate and test programming pattern can be very inefficient (e.g., Palindrome program explores possibilities). An alternative is to use a propagate and search technique. Propagate and search filters possibilities during the generation process, to prevent combinatorial explosion when possible. C. Varela4

Propagate and Search Propagate and search approach is based on three key ideas: Keep partial information, e.g., “in any solution, X is greater than 100”. Use local deduction, e.g., combining “X is less than Y” and “X is greater than 100”, we can deduce “Y is greater than 101” (assuming Y is an integer.) Do controlled search. When no more deductions can be done, then search. Divide original CSP problem P into two new problems: (P ^ C) and (P ^  C) and where C is a new constraint. The solution to P is the union of the two new sub- problems. Choice of C can significantly affect search space. C. Varela5

Propagate and Search Example Find two digits that add to 10, multiply to more than 24: D1::0#9D2::0#9% initial constraints {Browse D1}{Browse D2}% partial results D1+D2 =: 10 % reduces search space from 100 to 81 possibilities % D1 and D2 cannot be 0. D1*D2 >=: 24 % reduces search space to 9 possibilities % D1 and D2 must be between 4 and 6. D1 <: D2 % reduces search space to 4 possibilities % D1 must be 4 or 5 and D2 must be 5 or 6. % It does not find unique solution D1=4 and D2=6 C. Varela6

Propagate and Search Example(2) Find a rectangle whose perimeter is 20, whose area is greater than or equal to 24, and width less than height: fun {Rectangle} W H in W::0#9 H::0#9 W+H =: 10 W*H >=: 24 W <: H {FD.distribute naive rect(W H)} rect(W H) end {Browse {Search.base.all Rectangle}} % displays [rect(4 6)] C. Varela7

8 Constraint-based Computation Model Constraints are of two kinds: –Basic constraints: represented directly in the single-assignment store. For example, X in {0..9}. –Propagators: constraints represented as threads that use local deduction to propagate information across partial values by adding new basic constraints. For example, X+Y =: 10. A computation space encapsulates basic constraints and propagators. Spaces can be nested, to support distribution and search strategies. –Distribution strategies determine how to create new computation spaces, e.g., a subspace assuming X = 4 and another with X \= 4. –Search strategies determine in which order to consider subspaces, e.g., depth-first search or breadth-first search.

C. Varela; Adapted with permission from S. Haridi and P. Van Roy9 Partial Values (Review) Is a data structure that may contain unbound variables The store contains the partial value: person(name: “ George ” age: x 2 ) declare Y X X = person(name: “ George ” age: Y) The identifier ’Y’ refers to x 2 person “ George ” Unbound nameage The Store “X”“X” “Y”“Y” x1x1 x2x2

C. Varela; Adapted with permission from S. Haridi and P. Van Roy10 Partial Values (2) Partial Values may be complete declare Y X X = person(name: “ George ” age: Y) Y = 25 person “ George ” 25 nameage The Store “X”“X” “Y”“Y” x1x1 x2x2

C. Varela; Adapted with permission from S. Haridi and P. Van Roy11 Variables and partial values Declarative variable: –is an entity that resides in a single-assignment store, that is initially unbound, and can be bound to exactly one (partial) value –it can be bound to several (partial) values as long as they are compatible with each other Partial value: –is a data-structure that may contain unbound variables –When one of the variables is bound, it is replaced by the (partial) value it is bound to –A complete value, or value for short is a data-structure that does not contain any unbound variables

C. Varela 12 Constraint-based computation model Constraint 1 u+v =: 10 Constraint N u <: v Propagators (threads) u = {0#9} v = {0#9} x z = person(a:y) y =  1 s = ChildSpace …  1: x … Constraint store Mutable store Computation Space Basic constraints

C. Varela 13 Constraint propagation: Rectangle example Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {0#9} h = {0#9} Sol = rect(w h) Constraint storeMutable store Top-level Computation Space Basic constraints Constraint 2 w*h >=: 24 Let us consider propagator 1: w+h =: 10  w cannot be 0; h cannot be 0.

C. Varela 14 Constraint propagation: Rectangle example Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {1#9} h = {1#9} Sol = rect(w h) Constraint storeMutable store Top-level Computation Space Basic constraints Constraint 2 w*h >=: 24 Let us consider propagator 2: w*h >=: 24  w or h cannot be 1 or 2.

C. Varela 15 Constraint propagation: Rectangle example Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {3#9} h = {3#9} Sol = rect(w h) Constraint storeMutable store Top-level Computation Space Basic constraints Constraint 2 w*h >=: 24 Let us consider propagator 3: w <: h  w cannot be 9, h cannot be 3.

C. Varela 16 Constraint propagation: Rectangle example Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {3#8} h = {4#9} Sol = rect(w h) Constraint storeMutable store Top-level Computation Space Basic constraints Constraint 2 w*h >=: 24 Let us consider propagator 1 again: w + h =: 10  w >= 3 implies h cannot be 8 or 9, h >= 4 implies w cannot be 7 or 8.

C. Varela 17 Constraint propagation: Rectangle example Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {3#6} h = {4#7} Sol = rect(w h) Constraint storeMutable store Top-level Computation Space Basic constraints Constraint 2 w*h >=: 24 Let us consider propagator 2 again: w * h >=: 24  h <= 7 implies w cannot be 3.

C. Varela 18 Constraint propagation: Rectangle example Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {4#6} h = {4#7} Sol = rect(w h) Constraint storeMutable store Top-level Computation Space Basic constraints Constraint 2 w*h >=: 24 Let us consider propagator 3 again: w <: h  h cannot be 4.

C. Varela 19 Constraint propagation: Rectangle example Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {4#6} h = {5#7} Sol = rect(w h) Constraint storeMutable store Top-level Computation Space Basic constraints Constraint 2 w*h >=: 24 Let us consider propagator 1 once more: w + h =: 10  h cannot be 7.

C. Varela 20 Constraint propagation: Rectangle example Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {4#6} h = {5#6} Sol = rect(w h) Constraint storeMutable store Top-level Computation Space Basic constraints Constraint 2 w*h >=: 24 Let us consider propagator 3 once more: w <: h  w cannot be 6.

C. Varela 21 Constraint propagation: Rectangle example Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {4#5} h = {5#6} Sol = rect(w h) Constraint storeMutable store Top-level Computation Space Basic constraints Constraint 2 w*h >=: 24 We have reached a stable computation space state: no single propagator can add more information to the constraint store.

Search Once we reach a stable computation space (no local deductions can be made by individual propagators), we need to do search to make progress. Divide original problem P into two new problems: (P ^ C) and (P ^  C) and where C is a new constraint. The solution to P is the union of the solutions to the two new sub-problems. In our Rectangle example, we divide the computation space S into two new sub-spaces S1 and S2 with new (respective) constraints: w =: 4 w \=: 4 C. Varela22

C. Varela 23 Computation Space Search: Rectangle example with w=4 Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {4#5} h = {5#6} Sol = rect(w h) Constraint storeMutable store S1 Computation Sub-Space Basic constraints Constraint 2 w*h >=: 24 Constraint 4 implies that w = 4. Constraint 4 w =: 4

C. Varela 24 Computation Space Search: Rectangle example with w=4 Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = 4 h = {5#6} Sol = rect(w h) Constraint storeMutable store S1 Computation Sub-Space Basic constraints Constraint 2 w*h >=: 24 Constraint 1 or 2 implies that h = 6. Constraint 4 w =: 4

C. Varela 25 Computation Space Search: Rectangle example with w=4 Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = 4 h = 6 Sol = rect(w h) Constraint storeMutable store S1 Computation Sub-Space Basic constraints Constraint 2 w*h >=: 24 Since all the propagators are entailed by the store, their threads can terminate. Constraint 4 w =: 4

C. Varela 26 Computation Space Search: Rectangle example with w=4 w = 4 h = 6 Sol = rect(w h) Constraint storeMutable store S1 Computation Sub-Space Basic constraints This is the final value store. A solution has been found. The sub-space can now be merged with its parent computation space.

C. Varela 27 Computation Space Search: Rectangle example with w\=4 Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = {4#5} h = {5#6} Sol = rect(w h) Constraint storeMutable store S2 Computation Sub-Space Basic constraints Constraint 2 w*h >=: 24 Constraint 4 implies that w = 5. Constraint 4 w \=: 4

C. Varela 28 Computation Space Search: Rectangle example with w\=4 Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = 5 h = {5#6} Sol = rect(w h) Constraint storeMutable store S2 Computation Sub-Space Basic constraints Constraint 2 w*h >=: 24 Constraint 1, w+h =: 10  h = 5. Constraint 4 w \=: 4

C. Varela 29 Computation Space Search: Rectangle example with w\=4 Constraint 1 w+h =: 10 Constraint 3 w <: h Propagators (threads) w = 5 h = 5 Sol = rect(w h) Constraint storeMutable store S2 Computation Sub-Space Basic constraints Constraint 2 w*h >=: 24 Constraint 3, w <: h, cannot be satisfied: computation sub-space S2 fails. Constraint 4 w \=: 4

Finding palindromes (revisited) Find all four-digit palindromes that are products of two- digit numbers: fun {Palindrome} A B C X Y in A::1000#9999 B::0#99 C::0#99 A =: B*C X::1#9 Y::0#9 A =: X*1000+Y*100+Y*10+X {FD.distribute ff [X Y]} A end {Browse {Search.base.all Palindrome}} % 36 solutions C. Varela30

Computation spaces for Palindrome with Explorer C. Varela31 At top-level, we have X=1, X\=1. Green diamonds correspond to successful sub-spaces. Red squares correspond to failed sub-spaces.

Programming Search with Computation Spaces The search strategy specifies the order to consider nodes in the search tree, e.g., depth-first search. The distribution strategy specifies the shape and content of the tree, i.e., how many alternatives exist at a node and what constraints are added for each alternative. They can be independent of each other. Distribution strategy is decided within the computation space. Search strategy is decided outside the computation space. C. Varela32

Programming Search with Computation Spaces Create the space with program (variables and constraints). Program runs in space: variables and propagators are created. Space executes until it reaches stability. Computation can create a choice point. Distribution strategy decides what constraint to add for each alternative. Computation inside space is suspended. Outside the space, if no choice point has been created, execution stops and returns a solution. Otherwise, search strategy decides what alternative to consider next and commits to that. C. Varela33

Primitive Operations for Computation Spaces C. Varela34  statement  ::= {NewSpace  x   y  } | {WaitStable} | {Choose  x   y  } | {Ask  x   y  } | {Commit  x   y  } | {Clone  x   y  } | {Inject  x   y  } | {Merge  x   y  }

Depth-first single-solution search fun {DFE S} case {Ask S} of failed then nil [] succeeded then [S] [] alternatives(2) then C={Clone S} in {Commit S 1} case {DFE S} of nil then {Commit C 2} {DFE C} [] [T] then [T] end C. Varela35 % Given {Script Sol}, returns solution [Sol] or nil: fun {DFS Script} case {DFE {NewSpace Script}} of nil then nil [] [S] then [{Merge S}] end

C. Varela36 Relational computation model (Oz)  s  ::= skip empty statement |  x  =  y  variable-variable binding |  x  =  v  variable-value binding |  s 1   s 2  sequential composition |local  x  in  s 1  end declaration | proc {  x   y 1  …  y n  }  s 1  end procedure introduction |if  x  then  s 1  else  s 2  end conditional |{  x   y 1  …  y n  } procedure application |case  x  of  pattern  then  s 1  else  s 2  end pattern matching | choice  s 1  [] … []  s n  end choice | fail failure The following defines the syntax of a statement,  s  denotes a statement

C. Varela37 Relational Computation Model Declarative model (purely functional) is extended with relations. The choice statement groups a set of alternatives. –Execution of choice statement chooses one alternative. –Semantics is to rollback and try other alternatives if a failure is subsequently encountered. The fail statement indicates that the current alternative is wrong. –A fail is implicit upon trying to bind incompatible values, e.g., 3=4. This is in contrast to raising an exception (as in the declarative model).

C. Varela38 Search tree and procedure The search tree is produced by creating a new branch at each choice point. When fail is executed, execution « backs up » or backtracks to the most recent choice statement, which picks the next alternative (left to right). Each path in the tree can correspond to no solution (« fail »), or to a solution (« succeed »). A search procedure returns a lazy list of all solutions, ordered according to a depth-first search strategy.

C. Varela; Adapted with permission from S. Haridi and P. Van Roy39 Rainy/Snowy Example fun {Rainy} choice seattle [] rochester end fun {Cold} rochester end proc {Snowy X} {Rainy X} {Cold X} end % display all {Browse {Search.base.all proc {$ C} {Rainy C} end}} {Browse {Search.base.all Snowy}} % new search engine E = {New Search.object script(Rainy)} % calculate and display one at a time {Browse {E next($)}}

Implementing the Relational Computation Model choice  s 1  [] … []  s n  end is a linguistic abstraction translated to: case {Choose N} of 1 then  s 1  [] 2 then  s 2  … [] N then  s n  end Solve (Figure 12.6) C. Varela40

Implementing the Relational Computation Model C. Varela41 % Returns the list of solutions of Script given by a lazy depth-first exploration fun {Solve Script} {SolveStep {Space.new Script} nil} end % Returns the list of solutions of S appended with SolTail fun {SolveStep S SolTail} case {Space.ask S} of failed then SolTail [] succeeded then {Space.merge S}|SolTail [] alternatives(N) then {SolveLoop S 1 N SolTail} end % Lazily explores the alternatives I through N of space S, and returns the list of solutions found, appended with SolTail fun lazy {SolveLoop S I N SolTail} if I>N then SolTail elseif I==N then {Space.commit S I} {SolveStep S SolTail} else C={Space.clone S} NewTail={SolveLoop S I+1 N SolTail} in {Space.commit C I} {SolveStep C NewTail} end

C. Varela42 Exercises 97.Try different orders of execution for propagator threads. Do they always end up in the same constraint store? Why or why not? 98.CTM Exercise (pg 774). 99.CTM Exercise (pg 775).