Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

CICLOPS 2001 Finite Domain Constraints in SICStus Prolog Mats Carlsson Swedish Institute of Computer Science
What Are They? What Are They ?
Course Summary What have we learned and what are we expected to know?
Logic Programming Lecture 9: Constraint logic programming.
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Introduction. IC-Parc2 ECLiPSe Components Constraint Logic Programming system, consisting of  A runtime core Data-driven computation, backtracking, garbage.
1 Chapter 8: Modelling with Finite Domain Constraints Where we examine how modelling and controlling search interact with finite domain constraints.
G53CLP Constraint Logic Programming Modeling CSPs – Case Study I Dr Rong Qu.
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
/425 Declarative Methods - J. Eisner1 Constraint Programming.
Best-First Search: Agendas
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Problem.
Optimization Linear Programming and Simplex Method
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Introduction to a Programming Environment
Lesson 6. Refinement of the Operator Model This page describes formally how we refine Figure 2.5 into a more detailed model so that we can connect it.
Constraint Logic Programming (CLP) Luis Tari March 10, 2005.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CP Summer School Modelling for Constraint Programming Barbara Smith 1.Definitions, Viewpoints, Constraints 2.Implied Constraints, Optimization,
 Let A and B be any sets A binary relation R from A to B is a subset of AxB Given an ordered pair (x, y), x is related to y by R iff (x, y) is in R. This.
CS 321 Programming Languages and Compilers Prolog part 2.
What is a Function? by Judy Ahrens ~ 2005, 2006 Pellissippi State Technical Community College.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Michaelmas Term 2004 Discrete Mathematics CSC 141 Discrete Mathematics Dr. Corina Sas and Ms. Nelly Bencomo
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
Lecture 9: Constraint logic programming
1 Constraints for Multimedia Presentation Generation Joost Geurts, Multimedia and Human-Computer Interaction CWI Amsterdam
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Week 10Complexity of Algorithms1 Hard Computational Problems Some computational problems are hard Despite a numerous attempts we do not know any efficient.
Solving Compound Inequalities. Domain: A-REI Reasoning with Equations & Inequalities Cluster: 1. Understand solving equations as a process of reasoning.
Constraint Satisfaction CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Data Structures and Algorithms Dr. Tehseen Zia Assistant Professor Dept. Computer Science and IT University of Sargodha Lecture 1.
Chapter 5 Constraint Satisfaction Problems
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.
1. Computing Systems Lecture 3 Binary Representation & Boolean Logic Binary and Logic 2.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
The Hashemite University Computer Engineering Department
1 ECE 517: Reinforcement Learning in Artificial Intelligence Lecture 21: Dynamic Multi-Criteria RL problems Dr. Itamar Arel College of Engineering Department.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
Controlling Program Flow with Decision Structures.
Daniel Kroening and Ofer Strichman 1 Decision Procedures An Algorithmic Point of View Basic Concepts and Background.
Logic Programming Lecture 9: Constraint logic programming.
Computing Systems Lecture 3 Binary Representation & Boolean Logic Binary and Logic 1.
Logic Programming Lecture 2: Unification and proof search.
Computer Representation of Information
Cosc 2150: Computer Organization
Automatic Test Generation
Lecture 7: Constrained Conditional Models
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Equations Quadratic in form factorable equations
CSC Modeling with FD Constraints
Electrical and Computer Engineering Department
Arrays .
Artificial Intelligence
Constraint satisfaction problems
Presentation transcript:

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC PROGRAMMING (WEEK 8) Eleni E. Mangina Department of Computer Science University College Dublin The idea behind constraint logic programming Using finite domain constraints

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The idea behind constraint logic programming In old-fashioned prolog programming, programs can spend an awful lot of time searching through impossible solutions, because a test is applied after an entire solution has been generated We can partly overcome this problem by using delayed calls to let predicates “lie in wait” for information and substantial improvements can be made – eg. n! To ~ n 2 But we can improve things still further, by reasoning with logical theory about our data SICStus prolog has built in theories for integer finite domains, booleans, real numbers and national numbers

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The idea behind constraint logic programming (2) When we do this, we can think of the constraints as an enhancement of the unification process (there are logical ways of explaining this) instead of as part of the execution process, like delay declarations So if we insist in advance, for example, that X is an integer between 1 and 3, then the attempt to unify X with 5 can be made to fail as soon as it is tried, instead of at some time later Using constraints can cut down the search space solutions by many, many orders of magnitude They can effectively change the algorithm of a program

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Finite domains in Prolog Internally, to the advanced programmer, the SICStus constaint system is very complex and powerful However, it can be used very easily to produce striking effects We associate a set of “allowed values” with each variable, can then any attempt to unify it with something “not allowed” will fail We allow the specification of fixed relationships between variables (eg. X < Y) We could do this with delay declarations alone… In CLP (FD) we add a theory of integers so we can solve (mostly linear) equations

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Finite domains in prolog (2) This allows prolog to go beyond ordinary unification and actually suggest values in some cases eg. ?- X in 1…10, X #> _Y + 8, _Y = 1 X = 10? yes Like delayed goals, constraints can sometimes leave a useful residue: ?- X in 1…10, X#> _Y + 8, _Y = -2 X in 7…10 yes ?- X in 1…10, X#\=5 X in (1…4)\/(6…10) yes

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Finite domains in prolog (3) We can reason about the domains themselves, too: ?- X in 1…10, Y in 8…20, X = Y X = Y X in 8…10 yes And we can enumerate the members of a domain, if we need to: ?- X in 1…3, indomain(X) X = 1? ; X = 2? ; X = 3? ; no

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD How finite domains work in prolog First, we define domain constraints A domain constraint is of the form X::I where X is a variable, and I is a finite set of integers A set S of domain constraints is called a store The domain D(X,S) of X in S is the intersection of all I such that X::I is in S S is consistent if D(X,S) is inhabited for all X Otherwise it is contradictory

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD How finite domains work in prolog (2) Now, whenever we unify a domain variable with anything we add any constraints produced to the store If the resulting store is consistent, unification succeeds If the resulting store is contradictory, unification fails and we backtrack There is a further system involved with constraint programming called indexicals but we will not cover that here

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Domain Specification Syntax Domains are specified using a special kind of integer term, which I will call an integer domain term An integer domain term is one of the following: –An integer (eg 1) –Sup- positive infinity (supremum) –Inf – negative infinity (infimum)

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Domain Specification Syntax (2) A range is one of the following: –An interval (eg T1 … T2) –A set of integers (eg {1,20}) –A union of ranges (eg T1…T2 \/{1.10}) –An intersection of ranges (eg T1…T2/\ {2,7,8})

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predefined constraints SICStus prolog’s finite domain package is in library(clpfd) and contains the following predicates (among others): For testing and setting constraints: in/2 associates a range with a variable and checks consistency ~OP/2 where OP is one of >, >=, =, = Y + 2 ~\=/2 makes two expressions arithmetically not equal

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Example: SEND MORE MONEY A well known puzzle: Find the values of the variables in sum SEND + MORE MONEY where all the variables have different values We can write a constraint logic program to do this easily

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD SEND MORE MONEY (2) First we write down a predicate which will add the numbers appropriately: % we need to allow a carrying slot add(A,B,C) :- add (A,B,C, 0). % add numbers represented as lists of digits add ([],[],[],0). add([A|As], [B|Bs], [C|Cs], Carry):- A in 0…9, B in 0…9, C #= (A + B + NextCarry) mod 10, Carry #= (A + B + nextCarry) /10, add(As,Bs,Cs,NextCarry).

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD SEND MORE MONEY (3) And we call ?- add ([0,S,E,N,D], [0,M,O,R,E], [M,O,N,E,Y]), all_different([S,E,N,D,M,OR,Y]), labelling([], [S,E,N,D,M,O,R,Y]). all_different/1 constrains the variables in its argument list to be different labelling/2 enumerates the values of the variables in its second argument, as constraint by the store This version is roughly time faster than the best generate and test version

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Check the photocopies for extra slides on constraint programming & DCG