750 likes | 1.17k Views
Constraint Satisfaction Problems & Constraint Programming Representing Knowledge with Constraints . x 1. x 2. x 6. x 3. x 4. x 5. KR&R for Combinatorial Problems. Many, many practical applications
E N D
Constraint Satisfaction Problems & Constraint Programming Representing Knowledge with Constraints x1 x2 x6 x3 x4 x5 KNOWLEDGE REPRESENTATION & REASONING
KR&R for Combinatorial Problems • Many, many practical applications • Resource allocation, scheduling, routing, frequency assignment, timetabling, vehicle routing, etc. • Sports scheduling example • In sports league scheduling we try to build the schedule of matches between teams (e.g. football teams). There are various constraints: • Each team must play each other exactly twice (once home and once away) • No team can play more than two consecutive home or away matches • The number of times that a team plays two consecutive home or away matches must be minimum • Teams that use the same stadium cannot play home games at the same date • Games between top teams must occur at certain dates (due to TV coverage) • Etc. • Properties • Computationally difficult • Technical and modeling expertise needed • Experimental in nature • Important ($$$) in practice • Many solution techniques • (Mixed) integer programming • Specialized methods • Local search/metaheuristics • Constraint programming KNOWLEDGE REPRESENTATION & REASONING
Quotations • “Constraint programming represents one of the closest approaches computer science has yet made to the Holy Grail of programming: the user states the problem, the computer solves it.” Eugene C. Freuder, Constraints, April 1997 • “Were you to ask me which programming paradigm is likely to gain most in commercial significance over the next 5 years I’d have to pick Constraint Programming, even though it’s perhaps currently one of the least known and understood.” Dick Pountain, BYTE, February 1995 KNOWLEDGE REPRESENTATION & REASONING
x1 x2 x6 x3 x4 x5 Constraint Programming • Constraint programming is the continuous dream of programming • State the constraints • The solver will find a solution • Knowledge representation with constraints is the preferred model in many domains • Scheduling • Vehicle routing • Resource allocation • Temporal Reasoning • … KNOWLEDGE REPRESENTATION & REASONING
What is Constraint Programming? • Broad Answer: • Programming where the use of constraints plays a central role • alternative to logic programming, functional programming, object-orientedprogramming • there are constraint programming languages that support this • What is a constraint? • Let X1,X2, . . . ,Xn be a finite sequence of variables, each associated witha domain, D1,D2, . . . ,Dn. • A constraint on X1,X2, . . . ,Xn is a relation D1 × D2 × · · · × Dn • can be definedexplicitly/extensionally or implicitly/intentionally KNOWLEDGE REPRESENTATION & REASONING
What is Constraint Programming? • Narrow Answer: • A more specific answer is obtained by programming with constraints in aparticular manner. • Constraint programming involves solving a problem by: • Modelling: Formulate the problem as a finite set of constraints (aConstraint Satisfaction Problem). • Solving: Solve the CSP, perhaps by using a constraint programminglanguage • Mapping: Map the solution to the CSP to a solution to the originalproblem KNOWLEDGE REPRESENTATION & REASONING
There are constraints everywhere! implicit explicit • Room Myrtois occupied from12:00 until 15:00 • Traffic in the web < 100 Gbytes/sec • Salary< 15k Euro • Train 1 must leave 20 minutes before train 2 arrives • Exams for 1st semester must be at least 2 days apart x+y>z (0,1,0) (1,0,0) (1,1,0) (1,1,1) What is a Constraint? • What is a constraint? • Let X1,X2, . . . ,Xn be a finite sequence of variables, each associated witha domain, D1,D2, . . . ,Dn. • A constraint on X1,X2, . . . ,Xn is a relation D1 × D2 × · · · × Dn • can be definedexplicitly/extensionally or implicitly/intentionally KNOWLEDGE REPRESENTATION & REASONING
Software Engineering AI CP Logic Programming OR Discrete Mathematics Let’s look at this Constraint Programming (CP) • Began in late 1970s – early 1980s from AI world • Prolog III (Marseilles, France) • CLP(R) • CHIP (ECRC, Germany) • Application areas • Scheduling, sequencing, resource and personnel allocation, etc. etc. • Active research area • Specialized conferences (CP, CP/AI-OR, …) • Journal (Constraints) • Companies (ILOG, COSYTEC,..) These days… KNOWLEDGE REPRESENTATION & REASONING
Constraint Programming • Two main contributions • A new solving approach to combinatorial problems • AI search methods and heuristics • Orthogonal and complementary to standard OR methods • A new language for representing combinatorial problems • Rich language for constraints • Much closer to the real problem than OR • Language for search procedures • Easily extensible KNOWLEDGE REPRESENTATION & REASONING
Let’s look at this The Origins • Artificial Intelligence • Scene Labelling (Waltz) • Interactive Graphics • Sketchpad (Sutherland) • ThingLab (Borning) • Operations Research • NP-hard combinatorial problems • Logic Programming • unification --> constraint solving KNOWLEDGE REPRESENTATION & REASONING
Integer Programming (IP) • Consider the manufacture of television sets. A linear programming model might give a production plan of 205.7 sets per week. • No trouble stating that production should be 205 sets per week (or even ``roughly 200 sets per week''). • Suppose we were buying warehouses to store finished goods. A model that suggests we buy 0.7 warehouse at some location and 0.6 somewhere else would be of little value. • Warehouses come in integer quantities, and we would like our model to reflect that fact. • This integrality restriction has far reaching effects. Modeling with integer variables has turned out to be useful far beyond restrictions to integral production quantities. With integer variables, one can model logical requirements, fixed costs, sequencing and scheduling requirements, and many other problem aspects. KNOWLEDGE REPRESENTATION & REASONING
Integer Programming (IP) • The trouble with all this modeling power, however, is that problems with as few as 40 variables can be beyond the abilities of even the most sophisticated computers. • Most real problems with more than 100 or so variables are not possible to solve unless they show specific exploitable structure. • Despite the possibility (or even likelihood) of enormous computing times, there are methods that can be applied to solving integer programs. • An IP problem in which all variables are required to be integer is called a pure integer programming problem. • If some variables are restricted to be integer and some are not then the problem is a mixed integer programming problem (MIP). • The case where the integer variables are restricted to be 0 or 1 is called pure (mixed) 0-1 programming problems or pure (mixed) binary integer programming problems. KNOWLEDGE REPRESENTATION & REASONING
Relationship to Linear Programming Given an integer program There is an associated linear program called the linear relaxation formed by dropping the integrality restrictions: Since (LR) is less constrained than (IP), the following are immediate: • If (IP) is a minimization, the optimal objective value for (LR) is less than or equal to the optimal objective for (IP). • If (IP) is a maximization, the optimal objective value for (LR) is greater than or equal to that of (IP). • If (LR) is infeasible, then so is (IP). Solving (LR) does give some information: it gives a bound on the optimal value, and, if we are lucky, may give the optimal solution to IP. But for some problems it is very difficult to even get a feasible solution! KNOWLEDGE REPRESENTATION & REASONING
Branch and Bound We will explain branch and bound by using this model: Maximize 8X1+ 11X2 + 6X3 + 4X4 Subject to 5X1+ 7X2 + 4X3 + 3X4 ≤ 14 Xj {0,1} j = 1,…4. The linear relaxation solution is X1=1, X2 = 1,X3 = 0.5, X4 =0 with a value of 22. We know that no integer solution will have value more than 22. Unfortunately, since X3 is not integer, we do not have an integer solution yet. We want to force X3 to be integer. To do so, we branch on X3, creating two new problems. In one, we will add the constraint X3=0. In the other, we add the constraint X3= 1. Note that any optimal solution to the overall problem must be feasible to one of the subproblems. If we solve the linear relaxations of the subproblems, we get the following solutions: X3 = 0: objective 21.65, X1 = 1, X2 = 1, X3 = 0 ,X4=0.677; X3 = 1: objective 21.85, X1 = 1, X2 = 0.714, X3 = 1, X4 = 0. At this point we know that the optimal integer solution is no more than 21.85, but we still do not have any feasible integer solution. So, we will take a subproblem and branch on one of its variables. In general, we will choose the subproblem as follows: We will choose an active subproblem, which so far only means one we have not chosen before, and we will choose the subproblem with the highest solution value (for maximization) (lowest for minimization). In this case, we will choose the subproblem with X3 = 1, and branch on X2. After solving the resulting subproblems, we have the branch and bound tree in Figure 2. Figure 1 Figure 2 KNOWLEDGE REPRESENTATION & REASONING
Branch and Bound The solutions are: X3 = 1,X2 = 0: objective 18, X1 = 1, X2=0, X3=1,X4=1; X3 = 1, X2=1: objective 21.8, X1 = 0.6, X2=1, X3=1, X4=0. We now have a feasible integer solution with value 18. Furthermore, since the X3=1, X2=0 problem gave an integer solution, no further branching on that problem is necessary. It is not active due to integrality of solution. There are still active subproblems that might give values more than 18. Using our rules, we will branch on problem X3=1, X2=1 by branching on X1to get Figure 3. The solutions are: X3=1, X2=1, X1=0: objective 21, X1=0, X2=1, X3= 1, X4=1 X3 =1 , X2=1, X1=1: infeasible. Our best integer solution now has value 21. The subproblem that generates that is not active due to integrality of solution. The other subproblem generated is not active due to infeasibility. There is still a subproblem that is active. It is the subproblem with solution value 21.65. There is no better integer solution for this subproblem than 21. But we already have a solution with value 21. It is not useful to search for another such solution. Therefore, we can mark this subproblem it not active. There are no longer any active subproblems, so the optimal solution value is 21. Figure 3 KNOWLEDGE REPRESENTATION & REASONING
Constraint Satisfaction Problems At the core of Constraint Programming KNOWLEDGE REPRESENTATION & REASONING
What is a Constraint Satisfaction Problem? • Aconstraint satisfaction problem (CSP) is defined by: • A set ofvariablesX1,…,Xn • Each variableΧi has a domainDi with its possible values • A set of constraintsC1,…,Cm • Each constraint involves a subset of the variablesit specifies the allowed combinations of values for this subset • Ak-ary constraintC on a set of variables X1,…,Xkis a subset of the Cartesian productD1 x…x Dk • The set of variables in a constraint is called the constraint scope • Binary andnon-binary(or n-ary)constraint satisfaction problems x1 x2 {0,…,5} {0,…,3} KNOWLEDGE REPRESENTATION & REASONING
Constraint Satisfaction Problems • Solution of a CSP • Assignment of a value to each variable so that all constraints are satisfied • Goals: • Find one solution (feasibility problem) • Find all solutions • Find a solution that maximizes (or minimizes) some quantity • constraint optimization problem • Find an approximate “solution” All these tasks are NP-hard! (except perhaps one of them) KNOWLEDGE REPRESENTATION & REASONING
x1 x2 x1 x2 x6 x3 x6 x3 x4 x5 x4 x5 Constraint Graphs & Hypergraphs • variables – nodes • binary constraints – edges • the label of an edge specifies the constraint variables – nodes n-ary constraints – hyperedges KNOWLEDGE REPRESENTATION & REASONING
Example – Map Coloring We want to color each area in the map with a different color We have three colors red, green, blue KNOWLEDGE REPRESENTATION & REASONING
Example – Map Coloring • Formal Definition: • Variables • WA, NT, SA, Q, NSW, V, T • Domains (the same for all variables) • {red, green, blue} • Constraints • C(WA,NT) = {(red, green), (red, blue), (green, red), (green, blue), (blue,red), (blue, green)} • C(WA,SA) = … KNOWLEDGE REPRESENTATION & REASONING
Constraint Graph NT Q WA Two unconnected components NSW SA All constraints are binary V T KNOWLEDGE REPRESENTATION & REASONING
Example – 8 Queens problem We want to place 8 queens on the chessboard so they can’t attack each other KNOWLEDGE REPRESENTATION & REASONING
Example– 8 Queens problem • Formal Definition: • Variables • Each variableXi (i=1,…,8) represents the column where there is thei-thqueen (i.e. the queen in thei-throw) • Domains • If the columns are represented by numbers from 1 to 8 then the domain of each variableXiis Di = {1,2,…,8} KNOWLEDGE REPRESENTATION & REASONING
Παράδειγμα – 8 Queens problem • Constraints • There is a binary constraintC(Xi, Xj) for each pair of variables. These constraints can be defined as follows: • For all variablesXiandXj,Xi Xj • For all variablesXiandXj,ifXi = a andXj = b then i–ja – b andi–jb – a KNOWLEDGE REPRESENTATION & REASONING
Example – Cryptoarithmetics T W O + T W O F O U R F T U R O W X3 X2 X1 KNOWLEDGE REPRESENTATION & REASONING
T W O + T W O F O U R Example – Cryptoarithmetics • Formal Definition: • Variables and Domains • F, T, U, W, R, O {0,1,2,3,4,5,6,7,8,9} • X1, X2, X3 {0,1} • Constraints • alldifferent(F, T, U, W, R, O) • O + O = R + 10 X1 • X1 + W + W = U + 10 X2 • X2 + T + T = O + 10 X3 • X3 = F KNOWLEDGE REPRESENTATION & REASONING
1 2 3 4 5 Example: Crossword puzzle KNOWLEDGE REPRESENTATION & REASONING
Crossword puzzle as a CSP • Variables and their domains • X1 is 1 across D1 consists of all 5-letter words in the dictionary • X2 is 2 down D2 consists of all 4-letter words in the dictionary • X3 is 3 down D3 consists of all 3-letter words in the dictionary • X4 is 4 across D4 consists of all 4-letter words in the dictionary • X5 is 5 across D5 consists of all 2-letter words in the dictionary • Constraints (implicit/intensional) • C12 is “the 3rd letter of X1 must equal the 1st letter of X2” • C13 is “the 5th letter of X1 must equal the 1st letter of X3” • C24 is … • C25 is … • C34 is ... KNOWLEDGE REPRESENTATION & REASONING
1 2 3 4 5 Crossword puzzle as a CSP Variables: X1 X2 X3 X4 X5 X1 X2 X3 X4 Constraints (explicit/extensional): C12 = {(astar, talk), (happy, peal), (happy, peel), (hello, live) …} C13 = ... Domains: D1 = {astar, happy, hello, hoses} D2 = {live, load, peal, peel, save, talk} D3 = {ant, oak, old} D4 = {live, load, peal, peel, save, talk} KNOWLEDGE REPRESENTATION & REASONING
Real Constraint Satisfaction Problems • puzzles (not really practical applications, but they are fun) • N-queens, Zebra (five house puzzle), crossword puzzle, cryptoarithmetics (SEND+MORE=MONEY), mastermind • graph coloring • analysis and synthesis of analog circuits • option trading analysis • cutting stock • DNA sequencing • crew scheduling • chemical hypothetical reasoning • warehouse location • patient treatment scheduling • airport counter allocation (Cathay Pacific Airways Ltd) • crew rostering problem (Italian Railway Company) • well activity scheduling (Saga Petroleum a.s.) KNOWLEDGE REPRESENTATION & REASONING
Early Commercial Applications (90s) • Lufthansa: Short-term staff planning. • Hongkong Container Harbor: Resource planning. • Renault: Short-term production planning. • Nokia: Software configuration for mobile phones. • Airbus: Cabin layout. • Siemens: Circuit verification. • Caisse d’epargne: Portfolio management. KNOWLEDGE REPRESENTATION & REASONING
Applications in Research • Artificial Intelligence • Machine Vision • Natural Language Understanding • Temporal and Spatial Reasoning • Theorem Proving • Qualitative Reasoning • Robotics • Agents • Planning • Timetabling • Scheduling • Vehicle Routing • Resource allocation • Frequency Assignment KNOWLEDGE REPRESENTATION & REASONING
Applications in Research • Computer Science: • Program Analysis, Robotics, Agents • Molecular Biology, Biochemestry, Bioinformatics: • Protein Folding, Genomic Sequencing • Economics: • Scheduling, Stock Investment Planning • Linguistics: • Parsing • Medicine: • Decision Support • Physics: System Modeling KNOWLEDGE REPRESENTATION & REASONING
CSP Technology: Practical & Successful • Constraint satisfaction technology is one of the most successful examples of practical AI • There are many successful companies which build and trade CSP technology • ILOG • Cosytec • Parc Technologies • i2 Technologies • IQ Software • … KNOWLEDGE REPRESENTATION & REASONING
CSP Technology: Practical & Successful! FSQP/CFSQP Goedel GNU-Prolog ICE InC++ library IF/Prolog ILOG Numerica, ILOG Schedule, ILOG Solver Interval Solver for Microsoft Excel JSolver LIFE MAC Newton Nicolog Omega Oz ProFIT Prolog III, Prolog IV Pulsar QUAD-CLP(R) Quantum Leap • AKL • ALE • Amulet and Garnet • B-Prolog • Bertrand • Brandeis Interval Arithmetic Constraint Solver • CHIP • CIAL • CLAIRE • CLP • CONFLEX' • CPLEX • Cassowary • Contax • Cooldraw, Deltablue, Skyblue, ThinglabII • ECLiPSe RISC-CLP(Real) SEL ICStus Screamer StarFLIP++ Steeles constraint system' TOY Toupie Trilogy Unicalc cu-Prolog opbdp KNOWLEDGE REPRESENTATION & REASONING
A real CSP – Job-shop scheduling Examples of job shop scheduling problems include • factory scheduling problems, in which some operations have to be performed within one or several shifts • spacecraft mission scheduling problems, in which time windows are determined by astronomical events over which we have no control • patient treatment scheduling problems, in which a number of patients need to receive treatment that requires certain equipment within certain time windows, etc. • When solving a job shop CSP, the objective is to find as quickly as possible a feasible schedule, namely a schedule where each operation is performed within one of its legal time windows and no resource is oversubscribed. KNOWLEDGE REPRESENTATION & REASONING
O1 O6 O4 O1 O3 O2 O2 O7 O5 Job 2 O3 O8 Job 1 Job-shop scheduling problem (JSSP) • A JSSP requires scheduling a set of jobs J={ j1, ... , jn} on a set of physical resources RES={R1, ... ,Rm} • Each job jconsists of a set of operations O ={O1, ... ,On} to bescheduled according to a process routing that specifies a partial ordering among these operations(e.g. OiBEFORE Oj). KNOWLEDGE REPRESENTATION & REASONING
Job-shop scheduling problem (JSSP) • Each job j has a release date rdjand a due date (or deadline) ddjbetween which all its operations have to be performed. • Each operation Oihas a fixed duration duiand a start time stiwhose value has to be selected. • The domain of possible start times of each operation is initially constrained by the release and due dates of the job to whichthe operation belongs. • there can be additional unary constraints that furtherrestrict the set of admissible start times of each operation, thereby defining one or several timewindows within which an operation has to be carried out • e.g. a specific shift in factoryscheduling • In order to be successfully executed, each operation Oirequires pidifferent resources (e.g. a machine) Rij(1 £ j £ pi ) KNOWLEDGE REPRESENTATION & REASONING
The JSSP as a CSP • Variables • A set of variables is associated with each operation, Oi, which consists of • the operation start time, sti • its resource requirements, Rij • Constraints • Precedence constraints defined by the process routings translate into linearinequalities of the type: sti+dui£ stj(i.e. Oi BEFORE Oj ) • Capacity constraints that restrict the use of each resource to only one operation ata time translate into disjunctive constraints of the form:("p where Oi,Ojrequire Rp) sti+dui£ stjÚ stj+duj£ sti. These constraints simply expressthat, unless they use different resources, two operations Oiand Ojcannot overlap. KNOWLEDGE REPRESENTATION & REASONING
The JSSP as a CSP A job shop problem with 4 jobs Each nodeis labeled by the operation that it represents and the resource required by this operation. Each operation has a single resource requirement with a single possible value. Operation start times are the only variables. KNOWLEDGE REPRESENTATION & REASONING
A real CSP – The car sequencing problem • In a car production scenario, cars are placed on conveyor belts which move through different work areas. • A production line is normally required to produce cars of different models. The number of cars required for each model is called the production requirement. • Each work area is constrained by its resource constraint or Capacity constraint. • variable – one for every position in the conveyor belt (i.e. if there are n cars to be scheduled, the problem consists of n variables). • domain - the set of car models, for example from model A to D. • The task - to assign a value (a car model) to each variable (a position in the conveyor belt), satisfying both the production requirements and capacity constraints. KNOWLEDGE REPRESENTATION & REASONING
The car sequencing problem KNOWLEDGE REPRESENTATION & REASONING
Constraints and Databases • There are close links between CSPs and relational database theory Constraint terminology CSP Variable Domain Constraint Constraint scope Constraint tuples Set of solutions Database terminology Database Attribute Attribute domain Table Table schema Table instance Join of all tables KNOWLEDGE REPRESENTATION & REASONING
Constraints and Databases – Example • Consider the following CSP • A set of variablesX = {x0,…,x9} • All variables have the domainD = {0,1,2} • There are constraints with the following scopes and allowed tuples: • c1 = {x0,x1,x3} – {(0,0,0), (0,1,0), (1,0,1), (1,1,1), (0,1,2)} • c2 = {x1,x2,x3} – {(0,0,0), (0,0,1), (1,1,0), (1,0,1), (0,1,2)} • c3 = {x1,x4} – {(0,0), (1,1)} • c4 = {x3,x6} – {(0,0), (1,1), (1,0), (2,0)} • c5 = {x4,x5,x6} – {(0,0,0), (0,0,1), (1,1,1), (1,0,2)} • c6 = {x4,x7} – {(0,1), (1,0)} • c7 = {x5,x8} – {(0,1), (1,0), (1,1)} • c8 = {x6,x9} – {(0,0), (1,1)} KNOWLEDGE REPRESENTATION & REASONING
Constraints and Databases – Example The constraints as a relational database KNOWLEDGE REPRESENTATION & REASONING
Solving CSPs • Assuming we have expressed knowledge about a problem as a CSP • how can we reason with it? • how can we find a solution (if one exists)? • how can we find all solutions? • how can we infer new knowledge? • Generate and test • Backtracking search algorithms • Approximation algorithms • Constraint propagation algorithms KNOWLEDGE REPRESENTATION & REASONING
SolvingCSPs • There are two general approaches to solving CSPs that are used in practice • Systematic Search • Explore systematically the space of all assignments • systematic = every valuation will be explored sometime • extends partial assignments • Local Search • explore the search space by small steps • start with an initial complete assignment • repairs complete assignments KNOWLEDGE REPRESENTATION & REASONING
First of all: Generate & Test • Probably the most general problem solving method • Algorithm: generate labelling test satisfaction Drawbacks:Improvements: blind generator smart generator --> local search late discovery of testing within generator inconsistencies --> backtracking KNOWLEDGE REPRESENTATION & REASONING
1 2 3 4 5 Generate and test: Crossword Puzzle • Try each possible combination until you find one that works: • astar – live – ant – live • astar – live – ant – load • astar – live – ant – peal • … • Doesn’t check constraints until all variables have been instantiated • Very inefficient way to explore the space of possibilities (4*6*3*6 = 432 for this trivial problem, most inconsistent) KNOWLEDGE REPRESENTATION & REASONING