Logic Programming Lecture 9: Constraint logic programming.

Slides:



Advertisements
Similar presentations
Programming Language Concepts
Advertisements

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,
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Logic Programming Lecture 9: Constraint logic programming.
Introduction. IC-Parc2 ECLiPSe Components Constraint Logic Programming system, consisting of  A runtime core Data-driven computation, backtracking, garbage.
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 Chapter 8: Modelling with Finite Domain Constraints Where we examine how modelling and controlling search interact with finite domain constraints.
1 Pierangelo Dell’Acqua Dept. of Science and Technology Linköping University Constraint programming 2001 November 13th 2001
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
/425 Declarative Methods - J. Eisner1 Constraint Programming.
Meta-interpreters Interpreted languages like Prolog can easily treat program code as data Interpreted languages like Prolog can easily treat program code.
© 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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Computational Methods for Management and Economics Carla Gomes
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Approximation Algorithms
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Penn ESE535 Spring DeHon 1 ESE535: Electronic Design Automation Day 5: February 2, 2009 Architecture Synthesis (Provisioning, Allocation)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Theory and Applications
Lecture outline Support vector machines. Support Vector Machines Find a linear hyperplane (decision boundary) that will separate the data.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.
Penn ESE535 Spring DeHon 1 ESE535: Electronic Design Automation Day 5: February 2, 2009 Architecture Synthesis (Provisioning, Allocation)
MIT and James Orlin © Chapter 3. The simplex algorithm Putting Linear Programs into standard form Introduction to Simplex Algorithm.
Constraint Logic Programming (CLP) Luis Tari March 10, 2005.
LP formulation of Economic Dispatch
1 Lecture 4 Maximal Flow Problems Set Covering Problems.
7.1 Graphing Linear Systems
Chapter 0 – Objectives Solve for a variable in one-variable equations ex: Solve -3(x+5) = 10 Solve for a variable in two-variable equations ex: Solve.
0.1 Functions and Their Graphs. Real Numbers A set is a collection of objects. The real numbers represent the set of numbers that can be represented as.
Module B: Linear Programming
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
INTRODUCTION TO ARTIFICIAL INTELLIGENCE COS302 MICHAEL L. LITTMAN FALL 2001 Satisfiability.
Lecture 9: Constraint logic programming
Constraint Satisfaction CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
Declarative Programming Arithmetic in PROLOG Autumn 2014.
Advanced Operations Research Models Instructor: Dr. A. Seifi Teaching Assistant: Golbarg Kazemi 1.
Functions Section 1.4. Relation The value of one variable is related to the value of a second variable A correspondence between two sets If x and y are.
15.053Tuesday, April 9 Branch and Bound Handouts: Lecture Notes.
Chapter 5 Constraint Satisfaction Problems
1 System Planning 2013 Lecture 7: Optimization Appendix A Contents: –General about optimization –Formulating optimization problems –Linear Programming.
Constraints and Search Toby Walsh Cork Constraint Computation Centre (4C) Logic & AR Summer School, 2002.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
CONSTRAINT LOGIC PROGRAMMING Ivan Bratko Faculty of Computer and Information Sc. University of Ljubljana Slovenia.
First Order Logic Lecture 3: Sep 13 (chapter 2 of the book)
Unit 1: Functions Lesson 4: Domain and Range Learning Goals: I can determine domains and ranges for tables, graphs, equations, and real world situations.
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.
A relation is a correspondence between two sets. If x and y are two elements in these sets and if a relation exists between x and y, then x corresponds.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Searching a Linear Subspace Lecture VI. Deriving Subspaces There are several ways to derive the nullspace matrix (or kernel matrix). ◦ The methodology.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 2: Unification and proof search.
CSC Modeling with FD Constraints
Expressions and Assignment
Excel Solver.
Closures and Streams cs784(Prasad) L11Clos
Artificial Intelligence CS370D
Prolog syntax + Unification
Graphing systems of linear equations and inequalities
Chapter 3 Operators and Expressions
Constraint satisfaction problems
Graphical solution A Graphical Solution Procedure (LPs with 2 decision variables can be solved/viewed this way.) 1. Plot each constraint as an equation.
Constraint satisfaction problems
Presentation transcript:

Logic Programming Lecture 9: Constraint logic programming

Logic Programming Outline for today Infix operators/declarations Logic programming with constraints Finite domain constraints Real/rational constraints Course review outline

Logic Programming 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

Logic Programming 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,y indicate associativity (x needs explicit parentheses) Op can be an atom or list of atoms

Logic Programming 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, [ +, - ])....

Logic Programming 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...

Logic Programming 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 programs that use arithmetic making programs less readable or reusable

Logic Programming Example /* generate numbers between Low and High, give result in N */ 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).

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

Logic Programming 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? That's Constraint Logic Programming

Logic Programming Basic idea Expand the program "state" to include special predicates called constraints Program can generate constraints at any time Note: Equations t = u are a form of constraint. Reduce new constraint goals to normal form e.g. unification for = Backtrack if collection of all constraints becomes inconsistent Enumerate solutions on request

Logic Programming 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 generate or require values

Logic Programming between revisited ?- N in , N #> 99. N in ?- N in , N #> 99, indomain(N). N = 100.

Logic Programming 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 ? ;

Logic Programming 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]).

Logic Programming 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

Logic Programming 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

Logic Programming A cryptarithmetic 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

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

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

Logic Programming 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]).

Logic Programming 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.

Logic Programming 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)

Logic Programming Using CLP Provided as SICSTUS libraries [library(clpfd)]. [library(clpr)]. [library(clpq)].

Logic Programming Learning more There is a lot more to logic programming Books: "The Art of Prolog", Sterling & Shapiro, MIT Press 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. Principles and Practice of Declarative Programming (PPDP) - covers LP and other "declarative" paradigms