Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ex 4 constraint satisfaction #917

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Let's define each problem as a Constraint Satisfaction Problem (CSP). A CSP consists of:

1. **Variables**: The entities we need to assign values to.

2. **Domains**: The possible values that each variable can take.

3. **Constraints**: The restrictions on the values that the variables can take simultaneously.


### 1\. Rectilinear Floor-Planning

**Variables**:

* RiR\_iRi​ for each smaller rectangle iii, representing the coordinates of the bottom-left corner of the rectangle.


**Domains**:

* Possible coordinates (xi,yi)(x\_i, y\_i)(xi​,yi​) for the bottom-left corner of each rectangle within the larger rectangle's dimensions.


**Constraints**:

* **Non-overlapping constraint**: For any two rectangles RiR\_iRi​ and RjR\_jRj​, the rectangles must not overlap:

* xi+wi≤xjx\_i + w\_i \\leq x\_jxi​+wi​≤xj​ or xj+wj≤xix\_j + w\_j \\leq x\_ixj​+wj​≤xi​ or yi+hi≤yjy\_i + h\_i \\leq y\_jyi​+hi​≤yj​ or yj+hj≤yiy\_j + h\_j \\leq y\_iyj​+hj​≤yi​, where (xi,yi)(x\_i, y\_i)(xi​,yi​) are the coordinates of the bottom-left corner of RiR\_iRi​, and wiw\_iwi​ and hih\_ihi​ are the width and height of RiR\_iRi​.

* **Boundary constraint**: Each rectangle must be completely inside the larger rectangle:

* xi≥0x\_i \\geq 0xi​≥0, yi≥0y\_i \\geq 0yi​≥0, xi+wi≤Wx\_i + w\_i \\leq Wxi​+wi​≤W, yi+hi≤Hy\_i + h\_i \\leq Hyi​+hi​≤H, where WWW and HHH are the width and height of the larger rectangle.


### 2\. Class Scheduling

**Variables**:

* CiC\_iCi​ for each class iii, representing the time slot and classroom assigned to the class.


**Domains**:

* Possible pairs (t,r)(t, r)(t,r) where ttt is a time slot and rrr is a classroom.


**Constraints**:

* **Classroom availability constraint**: No two classes can be in the same classroom at the same time:

* If Ci=(t,r)C\_i = (t, r)Ci​=(t,r) and Cj=(t,r)C\_j = (t, r)Cj​=(t,r), then i=ji = ji=j.

* **Professor availability constraint**: Each professor can teach only one class at a time and must be available to teach the assigned class:

* If Ci=(t,\_)C\_i = (t, \\\_)Ci​=(t,\_) and Cj=(t,\_)C\_j = (t, \\\_)Cj​=(t,\_) and professor PPP is assigned to both classes, then i=ji = ji=j.

* **Professor qualification constraint**: A professor can only teach classes they are qualified to teach:

* If PPP is assigned to class iii, then PPP must be in the set of qualified professors for class iii.


### 3\. Hamiltonian Tour

**Variables**:

* CiC\_iCi​ for each city iii, representing the position of the city in the tour.


**Domains**:

* Possible positions in the tour, ranging from 1 to NNN (where NNN is the number of cities).


**Constraints**:

* **Unique position constraint**: Each city must occupy a unique position in the tour:

* For all i≠ji \\neq ji=j, Ci≠CjC\_i \\neq C\_jCi​=Cj​.

* **Connectivity constraint**: The cities must be connected in the given network:

* For all consecutive positions kkk and k+1k+1k+1, there must be a road between the cities assigned to these positions:

* If Ci=kC\_i = kCi​=k and Cj=k+1C\_j = k+1Cj​=k+1, then there must be a road between city iii and city jjj.

* **Closed tour constraint**: The tour must return to the starting city:

* There must be a road between the cities assigned to positions NNN and 1:

* If Ci=NC\_i = NCi​=N and Cj=1C\_j = 1Cj​=1, then there must be a road between city iii and city jjj.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
To show how a single ternary constraint such as A+B=CA + B = CA+B=C can be turned into three binary constraints using an auxiliary variable, and how constraints with more than three variables can be treated similarly, and finally how unary constraints can be eliminated by altering the domains of variables, follow these steps:

### Transforming a Ternary Constraint into Binary Constraints

Given the ternary constraint:A+B=CA + B = CA+B=C

1. Define a new auxiliary variable DDD which takes on values that are pairs (a,b)(a, b)(a,b) where aaa is a value from the domain of AAA and bbb is a value from the domain of BBB. Let DDD be such that:D=(A,B)D = (A, B)D=(A,B)

2. Here, first(D)\\text{first}(D)first(D) and second(D)\\text{second}(D)second(D) represent functions that return the first and second elements of the pair DDD respectively, and sum(D)\\text{sum}(D)sum(D) represents a function that returns the sum of the elements of the pair DDD.

* **Constraint 1:** The auxiliary variable DDD must represent the pair of values from AAA and BBB:A=first(D)A = \\text{first}(D)A=first(D)

* **Constraint 2:** Similarly, BBB must match the second element of the pair represented by DDD:B=second(D)B = \\text{second}(D)B=second(D)

* **Constraint 3:** The sum of AAA and BBB must equal CCC:C=sum(D)C = \\text{sum}(D)C=sum(D)


### Example:

Let the domains be:

* Domain of AAA: {1, 2, 3}

* Domain of BBB: {4, 5, 6}

* Domain of CCC: {5, 6, 7, 8, 9}

* Domain of DDD: {(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)}


Then, the binary constraints are:

* A=first(D)A = \\text{first}(D)A=first(D)

* B=second(D)B = \\text{second}(D)B=second(D)

* C=sum(D)C = \\text{sum}(D)C=sum(D)


### Extending to Constraints with More Than Three Variables

For a constraint involving more than three variables, such as:A+B+C=DA + B + C = DA+B+C=D

1. Define a new auxiliary variable EEE to represent a pair of values (A,B)(A, B)(A,B), and another auxiliary variable FFF to represent the result of the sum of AAA and BBB:E=(A,B)E = (A, B)E=(A,B)F=A+BF = A + BF=A+B

2. **Define Binary Constraints:**

* A=first(E)A = \\text{first}(E)A=first(E)

* B=second(E)B = \\text{second}(E)B=second(E)

* F=sum(E)F = \\text{sum}(E)F=sum(E)

* F+C=DF + C = DF+C=D


Thus, the original constraint A+B+C=DA + B + C = DA+B+C=D is decomposed into a set of binary constraints.

### Eliminating Unary Constraints

Unary constraints can be eliminated by altering the domains of variables.

For example, if we have a unary constraint:A∈{1,2}A \\in \\{1, 2\\}A∈{1,2}

Instead of having this unary constraint, we can directly restrict the domain of AAA to the set {1, 2}.

This demonstrates that any CSP can be transformed into a CSP with only binary constraints by using auxiliary variables and modifying domains.

### Conclusion

By introducing auxiliary variables and altering domains, any Constraint Satisfaction Problem (CSP) can be transformed into a form with only binary constraints. This method systematically decomposes higher-order constraints into simpler binary constraints, making the problem easier to handle using binary CSP solvers.