Logic Programming Lecture 9: Constraint logic programming.

Slides:



Advertisements
Similar presentations
Chapter 0 Review of Algebra.
Advertisements

1 Chapter 14 Making the most of things: Linear Programming.
Artificial Intelligence: Natural Language and Prolog
Flexible access control policy specification with constraint logic programming Steve Barker, Peter J. Stuckey Presenter: Vijayant Dhankhar.
February 14, 2002 Putting Linear Programs into standard form
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Formal Models of Computation Part II The Logic Model
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Programming Language Concepts
Where Syntax Meets Semantics
1 Outline relationship among topics secrets LP with upper bounds by Simplex method basic feasible solution (BFS) by Simplex method for bounded variables.
Brought To You By- Tutorial Services-The Math Center
Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
Order of Operations Lesson
Learning Objectives for Section 7.2 Sets
Distributive Property
CICLOPS 2001 Finite Domain Constraints in SICStus Prolog Mats Carlsson Swedish Institute of Computer Science
More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
Part 1 The Prolog Language Chapter 3 Lists, Operators, Arithmetic
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Modern Programming Languages, 2nd ed.
Columbus State Community College
Copyright © 2012, 2008, 2004 Pearson Education, Inc. Mrs. Rivas International Studies Charter School. Bell Ringer.
Getting started with Prolog
Logic Programming Lecture 1: Course orientation; Getting started with Prolog.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Modelling with Finite Domains u Domains and Labelling u Complex Constraints u Labelling u Different Problem Modellings u Efficiency u Using SICStus Prolog.
1 Logic Programming School of Informatics, University of Edinburgh Logic Programming in 50 Minutes The purpose of this lecture is to explain why logic.
Copyright © Cengage Learning. All rights reserved.
Arithmetic of random variables: adding constants to random variables, multiplying random variables by constants, and adding two random variables together.
Local Search Jim Little UBC CS 322 – CSP October 3, 2014 Textbook §4.8
CPSC 322, Lecture 14Slide 1 Local Search Computer Science cpsc322, Lecture 14 (Textbook Chpt 4.8) Oct, 5, 2012.
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
1 Functions and Applications
Let’s Do Algebra Tiles Algebra Tiles Manipulatives used to enhance student understanding of subject traditionally taught at symbolic level. Provide access.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009.
Introduction. IC-Parc2 ECLiPSe Components Constraint Logic Programming system, consisting of  A runtime core Data-driven computation, backtracking, garbage.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Logic Programming Lecture 3: Recursion, lists, and data structures.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.
Constraint Logic Programming (CLP) Luis Tari March 10, 2005.
Logic Programming Programming Lecture 1: Getting started with Prolog.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
Michaelmas Term 2004 Discrete Mathematics CSC 141 Discrete Mathematics Dr. Corina Sas and Ms. Nelly Bencomo
Lecture 9: Constraint logic programming
Declarative Programming Arithmetic in PROLOG Autumn 2014.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, assert/retract, collecting solutions.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Artificial Intelligence
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Cs774 (Prasad)L4ListProcessing1 List processing in Prolog
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 7: Search Strategies:
Logic Programming Lecture 9: Constraint logic programming.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 2: Unification and proof search.
Stacks Chapter 4.
Prolog syntax + Unification
Presentation transcript:

Logic Programming Lecture 9: Constraint logic programming

James CheneyLogic ProgrammingNovember 26, 2009 Outline for today Infix operators/declarations Logic programming with constraints Finite domain constraints Real/rational constraints

James CheneyLogic ProgrammingNovember 26, 2009 Infix operators Syntax of Prolog has many built-in infix operators + - * / = is =.. You can also define your own prefix, infix, or postfix operators Syntax and meaning are defined independently

James CheneyLogic ProgrammingNovember 26, 2009 Defining your own operators :- op(Prec, Fixity, Op). Prec is precedence - higher is weaker binding Fixity is xfx, xfy, yfx - infix (non, right, left assoc) fx, fy - prefix xf, yf - postfix x argument needs parentheses, y does not Op can be an atom or list of atoms

James CheneyLogic ProgrammingNovember 26, 2009 Looking under the hood (bonnet?) Standard Prolog ops declared as: :- op(1200, xfx, [ :-, --> ]). :- op(1100, xfy, [ ; ]). :- op(1000, xfy, [ ',' ]). :- op( 700, xfx, [ =, is,...]). :- op( 500, yfx, [ +, - ]). :- op( 500, fx, [ +, - ])....

James CheneyLogic ProgrammingNovember 26, 2009 Remember Prolog supports arithmetic, but it's not very "logical" ?- 2+2 = 4. no ?- X is 1+2. X = 3 ?- 1+2 is X. Instantiation error...

James CheneyLogic ProgrammingNovember 26, 2009 More problems with this Using is/2 for arithmetic, sometimes we have to commit to ground values too early Leads to higher branching factor Also imposes order of evaluation on arithmetic programs making programs less readable or reusable

James CheneyLogic ProgrammingNovember 26, 2009 Example between(Low,_,Low). between(Low,High,N) :- Low < High, Next is Low + 1, between(Next, High, N). ?- between(1,1000,N), N > 999. ?- N > 999, between(1,1000,N).

James CheneyLogic ProgrammingNovember 26, 2009 Constraint Programming Why can't we just say things like ?- X + 1 = 5 * Y, Y = 23. and have the system "solve for X "? X = 4 Constraint Programming is a well-studied framework that lets us do this (Example: Linear Programming)

James CheneyLogic ProgrammingNovember 26, 2009 Constraint Logic Programming Constraint Programming is powerful and declarative But it can be a pain to use Have to put problem in a specific syntactic form Wouldn't it be nicer to specify constraint problems using Prolog? Constraint Logic Programming does this

James CheneyLogic ProgrammingNovember 26, 2009 Basic idea Expand the program "state" to include special predicates called constraints Program can generate constraints at any time Note: Equations t = u are themselves a form of constraint. Reduce new constraints to normal form Backtrack if collection of all constraints becomes inconsistent When needed, enumerate solutions

James CheneyLogic ProgrammingNovember 26, 2009 Between again ?- N in , N #> 99. N in ?- N in , N #> 99, indomain(N). N = 99 N = 100.

James CheneyLogic ProgrammingNovember 26, 2009 Finite domain constraints N in i..j - says that N has one of finitely many values i..j t #= u - equality constraint t # u, etc. - inequality constraint These predicates constrain but don't unify

James CheneyLogic ProgrammingNovember 26, 2009 Distinctness We also have inequality constraints: X #\= Y says X and Y have to be different (both may be nonground) and distinctness constraints: all_different([X 1,...,X n ]) forces all elements of list to be different

James CheneyLogic ProgrammingNovember 26, 2009 indomain/1 Generates solutions to constraints ?- X in 1..5, Y #= 2*X+1, indomain(Y). X = 1, Y = 3 ? ; X = 2, Y = 5 ? ; X = 3, Y = 7 ? ; X = 4, Y = 9 ? ; X = 5, Y = 11 ? ;

James CheneyLogic ProgrammingNovember 26, 2009 labeling/2 First argument a list of options ([] for now) Second argument a list of constrained variables Enumerates all solutions, using options to control search. ?- X in 0..3, Y in 0..3, X #< Y, labeling([],[X,Y]).

James CheneyLogic ProgrammingNovember 26, 2009 minimize/2, maximize/2 Given a goal G, find min or max value of constrained var Y after running G ?- X in , Y #= (X - 50)*X, minimize(indomain(Y), Y). X = 25, Y = -625

James CheneyLogic ProgrammingNovember 26, 2009 A puzzle SEND + MORE MONEY Goal: Find distinct numbers S,E,N,D,M,O,R,Y between 0 and 9 such that the numbers formed by SEND and MORE add up to MONEY

James CheneyLogic ProgrammingNovember 26, 2009 Traditional solution solve_money( [S,E,N,D], [M,O,R,E], [M,O,N,E,Y]) :- between(0,9,S),..., between(0,9,Y), distinct([S,E,N,D,M,O,R,Y]), add_carry([0,S,E,N,D], [0,M,O,R,E], [M,O,N,E,Y], 0).

James CheneyLogic ProgrammingNovember 26, 2009 Traditional solution add_carry([],[],[],0). add_carry([A|As],[B|Bs],[C|Cs],Carry) :- add_carry(As,Bs,Cs,NextCarry), C is (A + B + NextCarry) mod 10, Carry is (A + B + NextCarry) / 10. distinct([]). distinct([X|Xs]) :- \+(member(X,Xs)), distinct(Xs).

James CheneyLogic ProgrammingNovember 26, 2009 CLP(FD) solution solve_money2( [S,E,N,D], [M,O,R,E], [M,O,N,E,Y]) :- S in 0..9,..., Y in 0..9, all_different([S,E,N,D,M,O,R,Y]), add_carry2([0,S,E,N,D], [0,M,O,R,E], [M,O,N,E,Y], 0), labeling([],[S,E,N,D,M,O,R,Y]).

James CheneyLogic ProgrammingNovember 26, 2009 CLP(FD) solution add_carry2([],[],[],0). add_carry2([A|As],[B|Bs],[C|Cs],Carry) :- add_carry2(As,Bs,Cs,NextCarry), C #= (A + B + NextCarry) mod 10, Carry #= (A + B + NextCarry) / 10. Note: Almost the same except for use of constraints.

James CheneyLogic ProgrammingNovember 26, 2009 Other constraint domains Real numbers: CLP(R) ?- { 2*X+Y =< 16, X+2*Y =< 11, X+3*Y =< 15, Z = 30*X+50*Y }, maximize(Z). X = 7.0, Y = 2.0, Z = Rational numbers: CLP(Q)

James CheneyLogic ProgrammingNovember 26, 2009 Using CLP Provided as SICSTUS libraries [library(clpfd)]. [library(clpr)]. [library(clpq)].

James CheneyLogic ProgrammingNovember 26, 2009 Note: Weird SICSTUS- ism ?- X is 3/2. % exact division X = 1.5 ?- X is 3//2. % integer division X = 1 ?- X #= 3/2. % FD-constraint integer division X = 1 ?- X #= 3//2. % error! Domain error....

James CheneyLogic ProgrammingNovember 26, 2009 Review Material covered in LPN, ch. 1-6: Terms, variables, unification (+/- occurs check) Arithmetic expressions/evaluation Recursion, avoiding nontermination Programming with lists and terms Expect ability to solve problems similar to those in tutorial programming exercises (or textbook exercises)

James CheneyLogic ProgrammingNovember 26, 2009 Review Material covered in LPN, ch. 7-11: Definite clause grammars Difference lists Nonlogical features ("is", cut, negation, assert/retract) Collecting solutions (findall, bagof, setof) Term manipulation Expect ability to explain concepts & their use in Prolog programs

James CheneyLogic ProgrammingNovember 26, 2009 Review Advanced topics (Bratko ch , 14, 23) Search techniques (DFS, BFS) Symbolic programming & meta- programming Constraint logic programming Expect understanding of basic ideas, not ability to write large programs from scratch

James CheneyLogic ProgrammingNovember 26, 2009 Some exam info Programming exam: 2 hours DICE machine with SICSTUS Prolog available (Documentation may not be, but exam will not rely on having memorized it) Sample exam from last year should be on course web page "soon" Exams from >1 year ago are on ITO web page; questions similar but different format.

James CheneyLogic ProgrammingNovember 26, 2009 Learning more There is a lot more to logic programming Books: "The Art of Prolog", Sterling & Shapiro Online: comp.lang.prolog Association for Logic Programming Main journal: Theory and Practice of Logic Programming (CUP) - main journal before 2001 was Journal of Logic Programming Main conferences: International Conference on Logic Programming (ICLP) - main annual conference. (In Edinburgh, July 2010). Principles and Practice of Declarative Programming (PPDP) - covers LP and other "declarative" paradigms