Presentation is loading. Please wait.

Presentation is loading. Please wait.

Klassische Themen der Computerwissenschaft -Constraint Satisfaction-

Similar presentations


Presentation on theme: "Klassische Themen der Computerwissenschaft -Constraint Satisfaction-"— Presentation transcript:

1 Klassische Themen der Computerwissenschaft -Constraint Satisfaction-
Alexander Felfernig Institut für Softwaretechnologie Inffeldgasse 16b/2 A-8010 Graz Austria

2 Organization Persons: Alexander Felfernig and Müslüm Atas VO: h VO: h UE: h UE: h “Abgabegespräche” (selective – announcement before Feb. 1st, 2018): “Nach Vereinbarung” Individual presentations of exercises (random selection process)! Exercises in groups: (groups as defined at the beginning of the course), mail names/matr.nr. including the solution as integrated .pdf and the CHOCO-Code as a .java-File to: Subject of must be “[KTdCW Submission Part-Nr]” Exercises Part-1 (10 points): (mail before 23:59h) Exercises Part-2 (10 points): (mail before 23:59h)

3 References A. Felfernig, L. Hotz, C. Bagley, and J. Tiihonen. Knowledge-based Configuration – From Research to Business Cases, Elsevier/Morgan Kaufmann, pp. 1—357, 2014.

4 Goals Part 1 Introduction Applications CSP Solving Part 2
Conflict Detection & Diagnosis Debugging Constraint Sets

5 Introduction

6 Constraint Technologies
Constraint technologies are one of the closest approaches computer science has yet made to the Holy Grail of programming: a user states the problem, the computer solves it [Constraints1997]. Example application areas: Configuration (e.g., cars, telecommunication switches) Scheduling (e.g., job shop scheduling, course scheduling) Recommendation (e.g., computers, financial services) Test case generation (e.g., in model-based testing)

7 Overall Goals Modeling Constraint Satisfaction Problems (CSPs)
Applications of constraint technologies Solving CSPs Testing & Debugging CSPs

8 Constraint Satisfaction Problem (CSP)
Specific type of decision problem where different alternatives constrain each other Alternatives are typically represented in the form of finite domain variables Alternatives should be selected in a way that none of the constraints is violated Users could have preferences on the inclusion of certain alternatives, e.g., prefer(Rome, Paris) Optimization requirements could exist, e.g., minimize costs in a building design, maximize number of used slots in a module

9 Constraint Satisfaction Problem (CSP)
A Constraint Satisfaction Problem (or CSP) is defined by a set of variables V = {v1,v2, …, vm}, and a set of constraints C = {c1, c2, …, cn}. Each variable vi has a nonempty domain dvi of possible values. Each constraint cj involves some subset of the variables and specifies the allowable combinations of values for that subset. A state of the problem is defined by an assignment of values to some or all of the variables. An assignment that does not violate any constraints is called a consistent or legal assignment. A complete assignment is one in which every variable is mentioned, and a solution to a CSP is a complete assignment that satisfies all the constraints. Let d be the domain size → O(d m) (m … #variables in CSP)

10 CSPs: A Simple Example V={v1, v2, v3} dv1=[1,2] dv2=[1,2,3] dv3=[1,2]
C={c1,c2} c1:v1>v2 c2:v2=v3 Solutions …

11 Types of Constraints Unary Constraints Binary Constraints
restrict the values of a single variable e.g., v1 <> 5 Binary Constraints binary constraints relate two variables e.g., v1 > v2 Higher order constraints involve three or more variables e.g., (v1 = 5 or v2 = 3) and v4 > v5 Extensional vs. intensional dom(vi)={1,2} intensional: v1 = v2 extensional: {(1,1), (2,2)}

12 Boolean CSPs Variable domains with exactly two values
Any CSP is convertible into a Boolean CSP SAT encoding a boolean variable indicates whether a certain value of a variable is set Conventional representation V={va, vb}; dva={1,2}; dvb={1,2} C={c1: va>vb} Corresponding SAT encoding V={va1, va2, vb1, vb2}; dom(vai)=dom(vbi)={true, false} C={c1: va2=true  vb1=true  va1=false  vb2=false}

13 Conjunctive Queries variables and domains constraints solution 1 2 1 2
Table v1 (a) 1 2 Table v2 (a) 1 2 3 Table v3 (a) 1 2 variables and domains SELECT v1.a, v2.a, v3.a FROM v1,v2,v3 WHERE v1.a > v2.a and v2.a = v3.a; constraints solution

14 all constraints must be satisfied…
Prolog v1(1). v1(2). v2(1). v2(2). v2(3). v3(1). v3(2). c1(V1,V2):-V1>V2. c2(V2,V3):-V2=V3. q(V1,V2,V3):-v1(V1),v2(V2),v3(V3),c1(V1,V2),c2(V2,V3). solutions … ?- q(X,Y,Z). X = 2 Y = 1 Z = 1 ; variables and domains constraints all constraints must be satisfied…

15 Applications

16 Map Coloring [ArtInt2002] Map Coloring Problem: assign colors to each region s.t. no neighboring regions have the same color. Representation as CSP: V = {WA, NT, SA, Q, NSW, V, T}; dvi = {red, green, blue}; C = {WA≠NT, WA≠SA, NT≠SA, NT≠Q, SA≠Q, SA≠NSW, SA≠V, Q≠NSW, NSW≠V}

17 Map Coloring Example solution:
WA = red, NT = green, SA = blue, Q = red, NSW = green, V = red, T = red ; [ArtInt2002]

18 CHOCO CHOCO: JAVA library for CSP solving.
The central element of a CHOCO program is the Model object: Model model = new Model("my problem"); Definition of variables: IntVar WA = model.intVar("WA", 0, 2) Definition of constraints, e.g.: m.notAllEqual(Map[0], Map[1]).post(); Operators on Variables, e.g. for IntVar v1 and v2: v1.neq(v2) : v1 != v2. v1.eq(v2)  : v1 = v2. v1.leq(v2) : v1 <= v2. v1.lt(v2) : v1 < v2. v1.sub(v2) : v1 – v2. v1.add(v2) : v1 + v2.

19 CHOCO constraints model object output results variables array of
import org.chocosolver.solver.Model; import org.chocosolver.solver.Solver; import org.chocosolver.solver.variables.IntVar; public class Map { public static void main(String[] args) { Model m = new Model("MyProblem"); IntVar WA = m.intVar("WA", 0, 2); IntVar NT = m.intVar("NT", 0, 2); IntVar SA = m.intVar("SA", 0, 2); IntVar Q = m.intVar("Q", 0, 2); IntVar NSW = m.intVar("NSW", 0, 2); IntVar V = m.intVar("V", 0, 2); IntVar T = m.intVar("T", 0, 2); IntVar[] Map = {WA,NT,SA,Q,NSW,V,T}; // ... // ... m.notAllEqual(Map[0],Map[1]).post(); m.notAllEqual(Map[0], Map[2]).post(); m.notAllEqual(Map[1], Map[2]).post(); m.notAllEqual(Map[1], Map[3]).post(); m.notAllEqual(Map[2], Map[3]).post(); m.notAllEqual(Map[2], Map[4]).post(); m.notAllEqual(Map[2], Map[5]).post(); m.notAllEqual(Map[3], Map[4]).post(); m.notAllEqual(Map[4], Map[5]).post(); Solver solver = m.getSolver(); while (solver.solve()) { System.out.println("WA:" + WA.getValue()); System.out.println("NT:" + NT.getValue()); System.out.println("SA:" + SA.getValue()); System.out.println("Q:" + Q.getValue()); System.out.println("NSW:" + NSW.getValue()); System.out.println("V:" + V.getValue()); System.out.println("T:" + T.getValue() + "\n"); } model object variables output results array of variables

20 CHOCO Version: Use for the exercises General Information, JavaDoc, Tutorials and more: How to use locally (Maven):

21 Constraints (example):
8 Queens a b c d e f g h 8 7 6 5 4 3 2 1 8 queens: positioning 8 queens on a 8x8 board: none of the queens endangers any of the other queens. Constraints (example): Line: board11 = true  board1j = false (j=2..8) Column: board11 = true  boardk1 = false (k=2..8) Diagonal: board11 = true  boardlm = false (8 ≥ l,m ≥ 2  l=m)

22 Magic Square Magic Square: n2 numbers are arranged in a way s.t. the values in rows, columns, and diagonals result in the same sum. Constraints (example): alldifferent(v11, v12, v13, v21, v22, v23, v31, v32, v33) sum(v11..v13) = … sum(v31..v33) = … sum(v11..v31) = … 2 7 6 15 9 5 1 4 3 8

23 Resource Allocation Task:
The following items should be distributed between three different containers: 1 barrel of fuel 1 paper roll 1 box of fireworks 1 palette with 6 PCs 1 palette with computer games 1 barell of old oil 1 palette of roof tiles 1 palette of aluminium rain pipes  Constraints: The following constraints have to be taken into account: c1: fuel and oil must not be combined with fireworks in one container c2: personal computers and computer games must be stored in one container c3: aluminium rain pipes must be stored in container #3 c4: each container is allowed to have at maximum 3 items c5: container #1 is not allowed to carry fuel c6: if fuel is in container #3 then container #2 must not store roof tiles

24 Resource Allocation C={c1, c2, c3, c4, c5, c6}
V={fuel, paper, fireworks, computer, games, oil, rooftiles, rainpipes, c1fuel, c2fuel, c3fuel, c1paper, …, c3rainpipes} dfuel={1..3} dpaper={1..3} dfireworks={1..3} dcomputer={1..3} dgames={1..3} doil={1..3} drooftiles={1..3} drainpipes={1..3} dc1fuel={0,1}, …, dc3rainpipes={0,1} C={c1, c2, c3, c4, c5, c6} c1: fuel <> fireworks and oil <> fireworks c2: computer = games c3: rainpipes = 3 c4a: c1fuel=1  fuel=1 and c2fuel=1  fuel=2 and c3fuel=1  fuel=3 … ? c4b: c1fuel + c1paper + c1fireworks + c1computer + c1games + c1oil + c1rooftiles + c1rainpipes <=3 … c5: fuel <> 1 c6: not(fuel = 3 ) or not(rooftiles=2)

25 Configuration

26 Re-Scheduling oimj: order oi is processed on machine mj dom(oimj) in [1,2,3] – time slot for processing oi on mj oimj < oimk: oi has to be processed on mj before being processed on mk oimj ≠ okmj: oi and ok must not be processed on mj at the same time

27 Feature Models [Benavides et al. 2010]

28 Feature Models: Semantics

29 Example: Feature Set Configuration
[Benavides et al. 2010] c2 c4 c5 c3 c7 c9 c11 c12 c8 c6 c10 V = {Phone, Calls, GPS, Screen, Media, Basic, Colour, HighRes, Camera, MP3} D = {dom(Phone)=dom(Calls)= … = dom(MP3)={yes,no}} CKB = {c1: Phone = yes, c2: Phone = yes  Calls = yes, c3: GPS = yes  Phone = yes, c4: Phone = yes  Screen = yes, c5: Media = yes  Phone = yes, c6: ¬(GPS=yes  Basic = yes), c7: Basic=yes  Colour = no  HighRes=no  Screen = yes c8: Colour=yes  Basic = no  HighRes=no  Screen = yes c9: HighRes=yes  Colour = no  Basic=no  Screen = yes c10: Camera = yes  HighRes = yes, c11: Camera = yes  Media = yes, c12: MP3 = yes  Media = yes, …} CR = {c13: GPS=yes, c14: Camera = yes} S = {Phone=yes, Calls=yes, GPS=yes, Screen=yes, Media=yes, Basic=no, Colour=no, HighRes=yes, Camera=yes, MP3=no}

30 CSP Solving

31 CSP Solving Goal: find an assignment for the variables which is consistent and complete Basic approach: Select variable and assign a corresponding value Test consistency between instantiated variables and constraints Backtrack in the case of an inconsistency Backtracking is a complete search algorithm every existing solution is found [ArtInt2002]

32 Notation used in Examples
v1=2 v2=4 v3=1 v4=3 v1=2

33 Backtracking „Uninformed“ algorithm which is very inefficient for large problems Improvements: variable & value orderings, forward checking, local consistency [OR1999]

34 Variable and value orderings
Backtracking Variable and value orderings [ArtInt2002]

35 Variable & Value Orderings
Static (predefined) Dynamic (at runtime) „First Fail“ principle try to instantiate variable with the minimum remaining values (MRV) More than 1 alternative with MRV: use degree heuristic, i.e., variable with the highest number of connected constraints  the sooner infeasiblity is discovered, the better (x1[1..10], x2[1], x3[1], x2x3) “Succeed First” principle try to select a value that maximizes the # of remaining options (least constraining value – LCV  forward checking)  a solution should be identified as fast as possible [ArtInt2002]

36 Forward Checking (with MRV)
6 1 2 least constraining value – LCV Whenever a variable vi is assigned, forward checking looks at each unassigned variable vj connected to vi via a constraint and deletes from vjs domain any value that is inconsistent with the value chosen from vi.

37 Forward Checking: Another Example
V={x,y,z} dom(x)={1,2,3}; dom(y)={3,4}; dom(z)={3,4,5} c1:x=y; c2:y=z dom(x)={1,2,3} dom(y)={3,4} dom(z)={3,4,5} x=1 x=2 x=3 dom(x)={1} dom(y)={} dom(z)={3,4,5} dom(x)={2} dom(y)={} dom(z)={3,4,5} dom(x)={3} dom(y)={3} dom(z)={3,4,5} y=3 dom(x)={3} dom(y)={3} dom(z)={3}

38 Forward Checking: Another Example
V={x,y,z} dom(x)={1,2,3}; dom(y)={1,2,3}; dom(z)={1,2,3} c1:x<y; c2:y<z; c3:z<x dom(x)={1,2,3} dom(y)={1,2,3} dom(z)={1,2,3} x=1 x=2 x=3 dom(x)={1} dom(y)={2,3} dom(z)={} dom(x)={2} dom(y)={3} dom(z)={1} dom(x)={3} dom(y)={} dom(z)={1,2} … no solution! y=3 dom(x)={2} dom(y)={3} dom(z)={}

39 Local Consistency Node consistency: Node consistency requires that every unary constraint on a variable is satisfied by all values in the domain of the variable. Arc consistency: A variable of a constraint satisfaction problem is arc-consistent with another one if each of its admissible values is consistent with some admissible value of the second variable. A CSP is arc consistent if every variable is arc consistent with any other one. [OR1999]

40 Local Consistency [OR1999]

41 Local Consistency [ArtInt2002]

42 Local Consistency x, y: 1..5 x < y – 2 queue: [x:y, y:x]
remove-first -> x:y remove-inconsistent-values(x:y) -> ({3,4,5} deleted from the domain of x), no further arcs are added to queue! remove-first -> y:x remove-inconsistent-values(y:x) -> ({1,2,3} deleted from the domain of y) add x:y to queue remove-inconsistent-values(x:y)  false x arc consistent with y and vice-versa!

43 Thank You!


Download ppt "Klassische Themen der Computerwissenschaft -Constraint Satisfaction-"

Similar presentations


Ads by Google