Professional Documents
Culture Documents
1 Constraints: Is The Amount of Interest To Be Paid), Let B The Final
1 Constraints: Is The Amount of Interest To Be Paid), Let B The Final
Programming
is
a
programming
paradigm,
which
is
built
up
constraints
and
constraint
solving.
The
CP
investigates
three
fundamental
operations
involving
constraints.
-‐ The
most
important
operation
is
to
determine
if
a
constraint
is
satisfiable.
-‐ The
second
operation,
simplification,
rewrites
a
constraint
in
a
form
which
makes
its
information
more
appearant.
-‐ The
third
operation,
optimization,
finds
a
solution
which
is
“best’’
according
to
some
criterion.
1
CONSTRAINTS
We
can
ask
about
a
system
of
constraints-‐
is
it
satisfiable,
that
is
to
say,
does
the
system
have
a
solution?
Constraints
are
mathematical
formalization
of
relationships
which
can
hold
between
objects.
For
example
“next
to’’
or
“father
of’’
are
constraints
which
hold
between
objects
in
the
real
world.
We
will
be
concerned
with
more
idealized
constraints
between
mathematical
objects,
such
as
numbers.
1.1 Constraints
The
statement
X
=
Y
+
2
is
an
example
of
a
constraint.
X
and
Y
are
variables,
that
is
they
are
place
holders
for
values,
presumably
in
this
case
numbers.
The
statement
is
not
simply
true
or
false.
It
depends
on
the
values
we
substitute
for
X
and
Y.
For
example:
X,
Y,
CI,
T,
List,
_X,
_List
are
all
variables.
Constraints
occur
in
all
sorts
of
everyday
reasoning.
Consider
the
following
constraint
which
describes
a
simple
loan.
Let
P
the
principle
of
the
loan,
let
I
be
the
interest
of
the
loan
(so
is
the
amount
of
interest
to
be
paid),
let
B
the
final
balance
of
the
loan.
Then
the
constraint
describes
the
relationship
forced
to
hold
between
these
variables
in
a
simple
loan.
For
example,
if
the
principal
P
is
$1000
and
the
interest
rate
I
12%,
then
the
balance
must
be
$1120.
1.2 Constraint
Domains
The
legitimate
forms
of
constraints
and
their
meaning
is
specified
by
a
constraint
domain.
The
constraint
domain
specifies
the
“syntax’’
of
the
constraints.
It
details
the
allowed
constant,
functions
and
constraint
relations
as
well
as
how
many
arguments
each
function
and
constraint
relation
is
required
to
have
and
how
the
arguments
are
placed.
The
constraint
domains
determines
also
the
values
that
variables
can
take
and
determines
the
meaning
of
all
these
symbols.
Some
popular
domains
for
constraint
programming
are:
-‐boolean
domains,
where
only
true/false
constraints
apply
(SAT
problem)
-‐integer
domains,
rational
domains
-‐linear
domains,
where
only
linear
functions
are
described
and
analyzed
(although
approaches
to
non-‐linear
problems
do
exist)
-‐finite
domains,
where
constraints
are
defined
over
finite
sets
-‐mixed
domains,
involving
two
or
more
of
the
above
1.3 Constraints
and
Valutaions
Given
a
constraint
domain
D
the
simplest
form
of
constraint
we
can
define
is
a
primitive
constraint.
A
primitive
constraint
consists
of
a
constraint
relation
symbol
from
D
together
with
the
appropriate
number
of
arguments.
Most
constraint
relations
are
binary,
that
is
they
require
two
arguments.
More
complicated
constraints
can
be
built
from
primitive
constraints
by
using
conjunctive
connetive
∧ which
stands
for
“and’’.
Consider
a
second
year
of
the
loan.
The
balance
after
two
years,
B2,
is
related
to
the
balance
after
the
first
year,
B,
and
the
interest
rate
I,
as
follows:
B2
=
B
+
I
x B.
Hence
the
entire
two
years
loan
is
described
by:
We
can
use
the
constraint
to
calculate
values
of
interest.
For
example
the
inital
principal
is
$1000,
and
the
interest
rate
is
12%,
then
the
balance
after
two
years,
B2,
is
$1254.40.
In
general,
constraints
are
just
sequences
of
primitve
constraints,
joined
by
conjuction.
Definiton
1.1
A
consraint
is
of
the
form
c1∧
...
∧cn
where
n≥0
and
c1,
.
.
.
,
cn
are
primitive
constraints.
The
symbol
∧ denotes
and,
so
a
constraint
c1∧
.
.
.
∧cn
holds
whenever
all
of
the
primitive
constraints
c1,
.
.
.
,
cn
hold.
There
are
two
distinct
constraints
true
and
false.
The
constraint
true
always
holds,
while
false
never
holds.
The
empty
conjuntion
of
constraints
(when
n
=
0)
is
written
as
true.
The
conjuction
of
two
constraints
and
,
written
is
defined
to
be
where
is
and
is
Definiton
1.2
Two
constraints
and
are
equivalent,
written
↔ , if they have the same set
of solution.
Definiton
1.3
The
function
primitives
takes
a
constraint
and
returns
the
set
of
the
primitive
constraints
.
That
is
to
say,
=
.
Clearly,
if
=
then
C1 ↔ C2.
V1
=
I1
x
R1
Ohm’s
Law
for
resistor
1,
V2
=
I2
x
R2
Ohm’s
Law
for
resistor
2,
V
–
V1
=
0
KVL
around
the
loop
(V,R1),
V
–
V2
=
0
KVL
around
the
loop
(V,R2),
V1
–
V2
=
0
KVL
around
the
loop
(R1,R2),
I
–
I1-‐
I2
=
0
KCL
at
the
top
junction,
-‐I
+
I1
+
I2
=
0
KCL
at
the
bottom
juction.
Given
the
constraint
describing
the
circuit,
we
can
investigate
the
circuit’s
behaviour
under
various
conditions.
Suppose
the
battery
is
10V
and
the
resistors
R1
=
10Ω
and
R2
=
5Ω.
Then
the
constraint
V
=
10
∧ R1
=
10
∧ R2
=
5
∧
V1
=
I1
x
R1
∧ V2
=
I2
x
R2
∧
V
–
V1
=
0
∧ V
–
V2
=
0
∧ V1
–
V2
=
0
∧
I
–
I1
–
I2
=
0
Describes
the
circuit.
This
constraint
has
the
single
solution
{V
→ 10, R1 → 10, R2 → 5, V1 → 10, V2 → 10, I1 → 1, I2 → 2, I → 3}.
1.5 Constraint
Satisfaction
As
we
have
seen,
given
a
constraint,
the
natural
question
to
ask
is:
“What
are
the
solutions
of
the
constraint?’’
Since
there
may
be
many
solutions
to
a
constraint,
this
quesiton
is
usually
modified
to
the
satisfaction
problem
“Does
the
constraint
have
a
solution?’’
An
algorithm
for
determining
the
satisfaction
of
a
constraint
is
called
a
constraint
solver.
What
does
solve
mean?
-‐determine
whether
a
(feasible/optimal)
solution
exists
-‐find
(exhibit)
one
(feasible/optimal)
solution
-‐find
(list)
all
(feasible/optimal)
solutions
Example:
SEND
+
MORE
=
MONEY
•Find
distinct
digits
for
letters
S,
E,
N,
D,
M,
O,
R,
Y,
such
that
the
following
sum
holds:
–
variables:
{s,
e,
n,
d,
m,
o,
r,
y}
–
domains:
{0,…,9}
S
E
N
D
+
–
constraints:
variables
must
be
assigned
distinct
values,
i.e.,
s
≠
e
,
s
≠
n,
…,
r
≠
y
M
O
R
E
=
and
____________
and
s
≠
0,
m
≠
0
M
O
N
E
Y
A
Solution:
{S
:=
9,
E
:=
5,
N
:=
6,
D
:=
7,
M
=
1,
O
=
0,
R
=
8,
Y
=
2
}
Example:
Suppose
we
wish
to
colour
a
map
of
Australia.
It
is
made
up
of
seven
different
states
and
territories
each
of
which
may
be
coloured
red,
green
or
blue.
Our
colouring
map
should
also
satisfy
the
constraint
that
adjacent
regions
have
different
colours.
–domain:
{red,
green,
blue}
The
following
constraint
captures
that
adjacent
regions
may
not
be
filled
the
same
colour.
WA
≠
NT
∧
WA
≠
SA
∧
NT
≠
Q
∧
NT
≠
SA
∧
SA
≠
Q
∧
Q
≠
NSW
∧
SA
≠
NSW
∧
V
≠
NSW
∧
SA
≠
V
The
user
is
encouraged
to
verify
that
there
is
in
fact
a
solution.
Example:
The
N-queens
problem.
This
is
the
problem
of
placing
N
queens
on
a
chessboard
of
size
NxN
so
that
no
queen
can
capture
another
queen.
We
can
formalise
this
by
associating
with
the
i-th
queen
two
variables,
Ri
and
Ci,
which
are,
resprectively,
its
row
and
column
position.
The
domain
of
each
variable
is
{1,2,..,N}.
Consider
the
constraint
with
the
variables
{1,2,3,4},
R1
≠
R2
∧
R1
≠
R3
∧
R1
≠
R4
∧R2
≠
R3
∧
R2
≠
R4
∧
R3
≠
R4,
ensures
that
no
two
queens
in
the
same
row,
the
constraint
C1
≠
C2
∧
C1
≠
C3
∧
C1
≠
C4
∧
C2
≠
C3
∧
C2
≠
C4
∧
C3
≠
C4,
ensures
that
no
two
queens
can
be
in
the
same
column,
and
the
constraints
C1
–
R1
≠
C2
-‐
R2∧
C1
-‐
R1≠
C3
-
R3
∧
C1
–
R1
≠
C4
-
R4
∧
C2
–
R2
≠
C3
-
R3
∧
C2
-
R2
≠
C4
-
R4
∧
C3
-
R3
≠
C4
–
R4
and
C1
+
R1
≠
C2
+
R2
∧
C1
+
R1≠
C3
+
R3
∧
C1
+
R1
≠
C4
+
R4
∧
C2
+
R2
≠
C3
+
R3
∧
C2
+
R2
≠
C4
+
R4
∧
C3
+
R3
≠
C4
+
R4
enforce
that
no
two
queens
on
the
same
diagonal.
One
solution
to
the
4-‐queens
problem
is
shown
in
the
figure:
1.6 A
Simple
Backtracking
Solver
It
is
always
possible
to
determine
satisfiability
of
a
CSP
by
a
brute
force
search
through
all
combinations
of
different
values,
since
the
number
of
such
combinations
is
finite.
One
of
the
simplest
tecqniques
for
determining
satisfiabilitiy
of
an
arbitary
CSP
is
chronological
backtracking.
The
bactracking
algoritm
recursively
use
a
parametric
function
satisfiable(c)
which
takes
a
primitive
contraint
c
that
involves
no
variables
and
returns
true
or
false
indicating
c
is
satisfiable
or
not.
2 Optimization
Sometimes
we
are
not
only
interested
in
the
satisfiability
of
a
constraint
but
also
wish
to
find
a
solution
to
the
constraint.
In
this
case,
we
are
rarely
desire
any
arbitary
solution,
but
instead
want
a
solution
that
is
“best’’.
Finding
a
best
solution
to
a
constraint
is
called
an
optimization
problem.
This
requires
some
way
of
specifying
which
solutions
are
better
than
others.
The
usual
way
of
doing
this
is
by
giving
an
objective
function
that
maps
each
solution
to
a
real
world.
Definiton
2 .1
An
optimization
problem,
written
,
consists
of
a
constraint
and
an
objective
function
f
which
is
an
expression
over
the
variables
in
and
which
evaluates
to
be
real
number.
A
valuation,
,
is
preferred
to
valutaion,
,
if
the
value
of
the
objects
function
f
under
is
less
than
the
value
under
.
In
other
words,
<
An
optimal
solution,
,
of
is
a
solution
of
such
that
there
is
other
solution
of
which
is
preferred
to
.
The
optimization
problem
in
which
is
the
constraint
X
+
Y
≥
4
and
f
is
the
objective
funtion
is
illustrated.
The
shaded
polygon
in
the
figure
represents
the
set
of
the
solutions
to
the
constraint.
The
semi-‐circles
across
the
polygon
are
contours
for
the
objective
function
with
the
value
of
the
objective
function
written
next
to
the
countour
line.
For
instance
some
solutions
of
are
{X
→ 0, Y → 4},
{X
→ 3, Y → 3}
and
{X
→ 2, Y → 2}.
Applied
to
f
they
give
16,
18,
8,
respectively.
From
this
diagramm
it
is
easy
to
see
that
the
optimal
solution
is
{X
→ 2, Y → 2}.
Example:
SEND
+
MOST
=
MONEY
•Find
distinct
digits
for
letters
S,
E,
N,
D,
M,
O,
T,
Y
such
that
the
following
sum
holds,
and
MONEY
is
maximal:
–
variables:
{s,
e,
n,
d,
m,
o,
t,
y}
–
domains:
{0,…,9}
S
E
N
D
+
–
constraints:
variables
must
be
assigned
distinct
values,
i.e.,
s
≠
e
,
s
≠
n,
…,
r
≠
y
M
O
S
T
=
and
____________
and
s
≠
0,
m
≠
0
M
O
N
E
Y
and
alldifferent(s,
e,
n,
d,
m,
o,
t,
y)
A
Solution:
{S
:=
9,
E
:=
7,
N
:=
8,
D
:=
2,
M
=
1,
O
=
0,
T
=
4,
Y
=
6
}
MONEY
=
10876
3 CONSTRAINT
LOGIC
PROGRAMS
3.1
User-Defined
Constraints
Constraint
logic
programming
languages
are
parametric
in
the
choice
of
the
underlying
constraint
domain
and
the
solver
and
simplifier
for
that
domain.
In
essence,
constraint
programming
laguages
provide
only
one
programming
construct—
“rules’’.
Rules
allow
programmers
to
define
their
own
constraints
in
erms
of
the
underlying
constraint
domain
of
the
CLP
language.
Somewhat
surprisingly,
this
simple
extension
leads
to
a
fully-‐fledged
programming
language
based
on
constraints
in
which
a
program
is
simple
a
collection
of
rules.
Rules
are
used
to
evalutae
“goals’’
given
by
the
user.
This
is
done
by
repeatedly
using
the
rules
to
replace
the
definition
of
user-‐defined
constraints
in
the
goal
until
only
primitive
constraints
are
left.
The
resulting
constraint
is
the
“answer’’
to
the
goal.
This
process
of
rewriting
is
called
a
“derivation’’.
Example
3.1
Consider
the
circuit
described
in
Figure
1.1.
If
we
are
only
interested
in
its
behaviour
with
respect
to
the
overall
voltage
V,
overall
current
I
and
the
two
resistance
values
R1
and
R2,
then
we
can
introduce
the
new
constraint,
paralel_resistors(V,
I,
R1,
R2),
A
literal
is
either
a
primitive
constraint
or
a
user-‐defined
constraint.
A
goal,
G,
is
a
sequence
of
literals.
That
is,
G
has
the
form
,
where
m
≥
0
and
each
is
a
litteral.
In
the
case
m
=
0,
the
goal
is
said
to
be
empty
and
is
represented
by
.
A
rule,
R,
is
of
the
form
A
:–
B
where
A
is
a
user-‐defined
constraint
and
B
is
a
goal.
A
is
called
the
head
of
R
and
B
is
called
the
body
of
R.
A
fact,
is
a
rule
with
the
empty
goal
as
body,
A
:–
[],
and
is
simply
written
as
A.
A
(constraint
logic)
program
is
a
sequence
of
rules.
The
definition
of
a
predicate
p
in
a
program
P
is
the
sequence
of
rules
apeearing
in
P
which
have
a
head
involving
predicate
p.
In
Example
3.1
above,
paralel_resistors
is
a
predicate,
which
is
defined
by
a
single
rule:
paralel_resistors(V, I, R1, R2) :– V = I1 * R1, V = I2 * R2, I1 + I2 = I.
3.2
Programming
with
rules
User-‐defined
constraints
are
more
powerful
than
the
example
of
the
parallel
resistor
suggests.
The
definition
of
rules
allows
predicates
to
be
defined
in
terms
of
other
predicates.
In
paticular,
this
allows
a
predicate
to
be
defined
recursively
in
terms
of
itself.
It
also
allows
a
predicate
to
be
defined
by
more
than
one
rule.
This
enables
us
to
model
choice
and
case
statements.
Consider
the
factorial
function
which
is
defined
by
We
can
write
a
predicate
fac(N,F)
which
is
true
if
F
is
N!
by
fac(0,1).
(R1)
fac(N,N*F)
:–
N≥1,
fac(N-‐1,F).
(R2)
We
can
use
this
predicate
to
compute
the
factorial
function
as
follow.
Imagine
we
wish
to
compute
this
factorial
of
2.
We
start
with
the
goal
fac(2,X).
We
obtain
the
exprected
answer,
X
=
2.
4
SIMPLE
MODELLING
WITH
CLP
4.1
Simple
Modelling
We
have
already
seen
examples
of
simple
modelling
in
which
a
problem
can
be
straightforwardly
described
by
a
conjunction
of
primitive
constraints.
Now
we
discuss
how
such
models
can
be
developed.
The
first
step
in
modelling
is
to
choose
the
variables
that
will
be
used
to
represent
the
parameters
of
the
problem.
In
many
cases
this
step
is
straightforward.
In
other
cases
there
may
be
a
number
of
different
ways
to
define
the
problem
and
the
choice
may
be
crucial
to
the
final
performance
of
the
model.
For
many
problems,
however,
determining
the
variables
of
the
problem
is
straight-‐
foward,
and
modelling
is
simply
a
matter
of
writing
the
constraints
defining
the
problem
in
the
right
form.
Consider
a
traveller
somwhere
in
the
tropical
rain
forest
of
northern
Australia.
She
wishs
to
paddle
cross
a
fast
flowing
ruver
in
an
inflatable
boat.
It
is
important
to
cross
the
river
as
quickly
as
possible
since
it
is
full
of
crocodiles.
Theere
is
a
single
small
clearing
on
the
other
side
of
the
river
at
which
she
must
land.Where
should
she
set
off
from
her
side
of
the
river
in
order
to
reach
the
clearing
in
the
least
time?
What
are
the
variables
of
the
problem?
Clearly
the
width,
W,
of
the
river
is
important
as
well
as
the
speed,
S,
at
which
it
flows.
The
rowing
speed,
R,
of
the
traveller
is
also
important
and
finally
we
need
a
variable,
P,
to
represent
the
distance
upstream–the
desired
anwser.
Let
all
measurements
be
in
metres
or
metres/second.
When
defining
the
constraint
of
the
problem,
we
reason
that
in
the
time
the
traveller
rows
the
width
of
the
river,
the
boat
floats
downstream
a
distance
given
by
the
speen
of
flow
by
the
time.
We
have
discovered
an
auxiliary
variable,
T,
the
time
to
cross.
We
may
not
be
in
interested
in
this
variable
but
we
need
it
to
fully
model
the
problem.
Now
we
can
write
the
constraints
modelling
the
problem
as
a
user-‐defined
constraint
river
river(W,
S,
R,
P).
Note
that,
since
we
are
not
interested
in
the
auxiliary
variable
T,
it
is
not
an
argument
of
the
user-‐defined
constraint.
river(W,
S,
R,
P)
:–
T
=
W/R,
P
=
S*T.
We
can
use
this
model
to
answer
the
traveller’s
question.
Suppose
the
traveller
can
row
at
1.5
m/s
and
the
river
flows
at
speed
1
m/s
and
is
24
m
wide.
The
goal:
The
underlying
solver
in
CLP
language
can
only
handle
conjuctions
of
primitive
constraints.
However,
problems
often
involve
relationships
which
are
not
directly
expressible
as
a
conjuction
of
primitive
constraints.
Multiple
rules
in
the
definition
of
a
predicate
allow
us
to
model
these
more
complex
relationships.
One
form
of
information
that
cannot
be
expressed
simply
a
conjuction
of
primitive
constraints
are
relations,
that
is
to
say
tables
of
data.
But
by
using
multiple
facts
it
is
simple
to
represent
a
relation
in
a
constraint
logic
program.
Consider
a
genelogical
database
describing
small
family.
The
father(X,Y)
relation
holds
when
X
is
the
father
of
Y.
Similarly,
the
mother(X,Y)
relation
holds
when
X
is
the
mother
of
Y.
The
following
program
defines
these
relations
for
a
fictional
family.
father(jim,edward).
mother(maggy,fi).
age(maggy,63).
father(bill,fi).
age(jim,85).
age(edward,60).
age(peter,33).
age(bill,65).
The
goal
father(edward,X)
can
be
used
to
find
the
children
of
Edward.
It
gives
the
answers
X
=
peter,
X
=
helen
and
X
=
kitty.
We
can
extand
the
simple
program
above
by
defining
the
predicates
parent,
sibling,
cousin
and
older
in
terms
of
father,
mother
and
age.
The
relation
parent(X,Y)
holds
if
X
is
parent
of
Y,
sibling(X,Y)
holds
if
X
and
Y
are
siblings...
They
may
be
defined
by:
parent(X,Y)
:–
fater(X,Y).
parent(X,Y)
:–
mother(X,Y).
REFERENCES
1. K.
Mariott
and
P.
J.
Stuckey.
Programming
with
Constraints.
MIT
Press,
1998.
2. L.
D.
Gaspero.
Modeling
and
Solving
Constrained
Optimization
Problems
lecture
slides.
2011.