Introduction Batch 2 to Constraint

advertisement
Introduction
to
Constraint
Programming
Batch 2
The aim of the second batch of exercises is to model various problems using logic
programming in combination with some of the constraint programming concepts
introduced in the first part of the course. The exercises should be solved individually.
All solutions must be returned to Ulf Nilsson before May 3 and should contain commented code, plus running examples demonstrating that the program works as
intended.
1
Optimization problem
John has strange eating habits; he only eats items containing chocolate, sugar and
fat. Assume that we have a small database of facts of the form:
contains(item, calories, chocolate, sugar, fat).
price(item, cost).
Each day John must ingest at least 500 calories, 6 units of chocolate, 10 units of
sugar and 8 units of fat. Write a general CLP(Q) program in SICStus Prolog which
given a database (such as the one given below) composes Johns intake of “food”
while satisfying the constraints and minimizing the cost.You may use the following
sample database but the rest of the program should be general:
contains(brownie, 400, 3, 2, 2).
contains(ice_cream, 200, 2, 2, 4).
contains(coke, 150, 0, 4, 1).
contains(cheesecake, 500, 0, 4, 5).
cost(brownie, 50).
cost(ice_cream, 20).
cost(coke, 30).
cost(cheesecake, 80).
Hint 1: It is perfectly possible to manipulate constraints as terms before posting
them to the store. For instance, it is possible to write a rule setup(X) :- { X }. We can
then send a constraint to the store by saying ?- setup(X > 3).
Hint 2: In order to solve the problem you probably need to use the built in predicate findall/3 (or possibly setof/3 or bagof/3).
2
Water containers
The following graph models the flow of water per time unit between three containers of water:
7%
5%
A
5%
B
C
3%
That is, in each unit of time 7% of the water in container C flows to container A
and 5% of the water in container A flows to container B etc. Write a CLP(R) or
CLP(Q) program that describes the relationship between
•
the initial amount of water in each container, and
•
the amount of water in each container after N time units.
Answer the question “how much water is there in each container after 10 units of
time when the containers initially contain the same amount of water?”.
3
Verification in CLP(B)
Combinatorial circuits are hardware devices that implement Boolean functions.
Here we consider circuits built by means of inverters, and-gates and or-gates. They
are often depicted as follows:
≥1
&
The primitives can be used to build complex combinatorial circuits. The following
is an example of a so-called 2-1-mux
A
&
≥1
B
D
&
C
A 2-1-mux has three inputs and one output. The input C is a switch; if C = 1 then
D = A; if C = 0 then D = B.
We now want to verify properties of combinatorial circuits by means of CLP(B).
The structure of combinatorial circuits can be described by means of terms. For
instance, the circuit above can be described by a term or(and(in(A), in(C)),
and(in(B),not(in(C)))) and the relationship between a circuit and its output can be
described by a binary relation circuit/2 which, given a term representing the structure
of the circuit and the output of the circuit, sets up the appropriate constraints between
inputs and output. Define such a relation! Then use the relation to define a new relation mux(A, B, C, D) and verify that the following property is satisfied by a mux: if D
≠ A then C = 0 and B = D.
We can use the 2-1-mux to build a 4-1-mux in the following way:
mux
A1
mux
A2
C
mux
A3
A4
B1
B2
Use the previous definition of a 2-1-mux and define a new relation mux(A1, A2,
A3, A4, B1, B2, C) and verify that if B1 = 1 then C = A1 or C = A3.
Hint: Verifying whether a combinatorial circuit C(Inputs, Output) satisfies a property P(Inputs, Output) amounts to checking if the property is entailed by the circuit.
In CLP(B) we can do this (schematically) by proving
?- sat(C(Inputs, Output)), taut(P(Inputs, Output), 1).
4
N-queens in CLP(B)
The N-queens problem (of positioning N queens on an N*N chess board so that
no queen attacks any other queen) can be encoded using Boolean variables. For
instance, when N = 4 we can encode the board as a 4-element list where each element
is in turn a 4-element list of Boolean variables:
[
[
[
[
[
X11,
X21,
X31,
X41,
X12,
X22,
X32,
X42,
X13,
X23,
X33,
X43,
X14
X24
X34
X44
],
],
],
]
]
A partial solution such as
can, for instance, be encoded as the constraint store
X21* X42* ~X11* ~X31* ~X41* ~X12* ~X22* ~X32* ~X23* ~X33* ~X43* ~X24* ~X44
With such an encoding it is allowed to position a queen on a square Xij iff neither
Xij nor ~Xij are entailed by the current constraint store.
Write a CLP(B) program which takes as input the integer N and solves the Nqueens problem using the Boolean encoding described above.
Hint 1: The names of the variables cannot be controlled in a logic program since
variables are automatically renamed during evaluation. The names are, on the other
hand, not important; it is only important that the variables are distinct.
Hint 2: It is important that you never add both X and ~X to the store! If you do,
the store becomes unsatisfiable.
Hint 3: There are several ways of solving the the problem. You may for instance
consider using the cardinality constraint available in SICStus Prolog to constrain the
problem.
Hint 4: There are in general several ways of positioning the queens. Depending on
how you solve the problem you may have to use the built-in labeling/1 to enumerate
all solutions.
Download