constraint logic programming declarative modeling … · • integrates various logic programming...
TRANSCRIPT
1
Costin Bădică
University of Craiova
Department of Computers and Information Technology
CONSTRAINT LOGIC PROGRAMMING:
FROM DECLARATIVE MODELING TO
PROBLEM SOLVING
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
2
UNIVERSITY OF CRAIOVA
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
3
IDS RESEARCH GROUP
• Intelligent Distributed Systems Research
Group
• http://ids.software.ucv.ro/
• Synergies between:
– Intelligent computing
– Distributed computing
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
4
OVERVIEW
I. Basics of CSP, LP & CLP
CSP
Prolog
ECLiPSe-CLP
II. Examples
Basic Combinatorics
Logic Puzzles
Logistics Scheduling
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
5
EXAMPLE: MAP COLORING
• Variables: color of each region; 5 variables
• Domains: set of colors (green, red, yellow, blue)
• Constraints: any two neighbor regions have a different color;
e.g. color of region 3 ≠ color of region 2
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
6
CONSTRAINT SATISFACTION PROBLEM – CSP
01.08.2018
• CSP: triple 𝒫 = ⟨𝑋, 𝐷, 𝐶⟩ such that:
– 𝑋 = (𝑋1, 𝑋2, … , 𝑋𝑛) an 𝑛-tuple of (decision) variables
– 𝐷 = (𝐷1, 𝐷2, … , 𝐷𝑛) an 𝑛-tuple of domains such that
domain of 𝑋𝑖 is 𝐷𝑖
– 𝐶 = (𝐶1, 𝐶2, … , 𝐶𝑡) an 𝑡-tuple of constraints (relations)
such that:
𝐶𝑗 = (𝑅𝑆𝑗 , 𝑆𝑗)
where:
𝑆𝑗 = 𝑠𝑐𝑜𝑝𝑒 𝐶𝑗
are the variables occurring in 𝐶𝑗.
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
7
CSP FEASIBLE SOLUTION
01.08.2018
• Feasible solution of a CSP 𝒫 = ⟨𝑋, 𝐷, 𝐶⟩ is an 𝑛-
tuple 𝐴 = (𝑋1 = 𝑎1, 𝑋2 = 𝑎2, … , 𝑋𝑛 = 𝑎𝑛) such that:
– 𝑎𝑖 ∈ 𝐷𝑖
– For each constraint 𝐶𝑗 the projection of tuple 𝐴 on
𝑠𝑐𝑜𝑝𝑒(𝐶𝑗) satisfies 𝑅𝑆𝑗.
• Note that a feasible solution:
𝐴 = 𝑋1 = 𝑎1, 𝑋2 = 𝑎2, … , 𝑋𝑛 = 𝑎𝑛
is often written as:
𝐴 = (𝑎1, 𝑎2, … , 𝑎𝑛)
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
8
CSP OPTIMAL SOLUTION
01.08.2018
• Let 𝒫 = ⟨𝑋, 𝐷, 𝐶⟩ be a CSP. We define a cost
function:
𝑓: 𝐷𝑖 → ℝ𝑛𝑖<1
• Optimal solution of a CSP 𝒫 = ⟨𝑋, 𝐷, 𝐶⟩ according
to cost function 𝑓 is an 𝑛-tuple 𝐴 = (𝑋1 = 𝑎1, 𝑋2 =𝑎2, … , 𝑋𝑛 = 𝑎𝑛) such that:
– 𝐴 is a feasible solution of 𝒫
– 𝑓(𝐴) is minimum (optimal)
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
9
TRIANGLE AREAS
01.08.2018
𝑌
𝑋 𝑂
𝑀
𝑁
𝐴
2
• There are given 3 integers: 𝑁,𝑀, 𝐴 ∈ ℕ.
• Find a triangle 𝑇 with vertices 𝑋𝑖 , 𝑌𝑖 , 𝑖 = 1,2,3 such that:
– Vertices satisfy 0 ≤ 𝑋𝑖 ≤ 𝑁 and 0 ≤ 𝑌𝑖 ≤ M
– Area of 𝑇 is 𝐴
2.
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
10
SIMPLIFY PROBLEM
We can assume
that one vertex is
0,0 !
01.08.2018
𝑌
𝑋 𝑂
𝑀
𝑁
𝐴
2
𝑌
𝑋 𝑂
𝑀
𝑁
𝐴
2
𝑌
𝑋 𝑂
𝑀
𝑁
𝐴
2
𝑌
𝑋 𝑂
𝑀
𝑁
𝐴
2
Flip horizontal
Translate
Flip vertical
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
11
TRIANGLE AREAS AS CSP • Problem variables are coordinates of triangle vertices:
𝑋1, 𝑌1 = 0,0 , 𝑋2, 𝑌2 , (𝑋3, 𝑌3)
• Domains:
𝑋2, 𝑋3 ∈ ,0,𝑁- 𝑌2, 𝑌3 ∈ ,0,𝑀-
• Constraints:
Triangle area =𝐴
2 i.e.
1
2det
0 0 1𝑋2 𝑌2 1𝑋3 𝑌3 1
=𝐴
2 i.e.
𝑋2𝑌3 − 𝑋3𝑌2 = 𝐴
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
12
CRYPT-ARITHMETIC PUZZLES
• Variables: digits 𝑇,𝑊,𝑂, 𝐹, 𝑈, 𝑅 ∈ *0,1,2,… , 9+.
• Constraints:
𝑎𝑙𝑙𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑡( 𝑇,𝑊,𝑂, 𝐹, 𝑈, 𝑅 )
𝑇 ≠ 0
𝐹 ≠ 0
200𝑇 + 20𝑊 + 2𝑂 = 1000𝐹 + 100𝑂 + 10𝑈 + 𝑅
• Additional variables: carries 𝐶1, 𝐶2, 𝐶3 ∈ *0,1+ • Replace last constraint with 4 constraints:
2𝑂 = 𝑅 + 10𝐶1
2𝑊 + 𝐶1 = 𝑈 + 10𝐶2
2𝑇 + 𝐶2 = 𝑂 + 10𝐶3
𝐹 = 𝐶3
01.08.2018
T W O +
T W O
F O U R Global constraint
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
13
4-QUEENS PROBLEM
• Variables: 𝑄1, 𝑄2, 𝑄3, 𝑄4 such that 𝑄𝑖 is the row of the queen
placed on column 𝑖.
• Domains: *1,2,3,4+
• Constraints: no two distinct queens can attack on rows, on
main diagonals, on secondary diagonals.
– On rows: 𝑄𝑖 ≠ 𝑄𝑗 for all 𝑖 ≠ 𝑗
– On main diagonals: 𝑄𝑖 − 𝑄𝑗 ≠ 𝑖 − 𝑗
– On secondary diagonals: 𝑄𝑖 − 𝑄𝑗 ≠ 𝑗 − 𝑖
– On whatever diagonals: 𝑄𝑖 − 𝑄𝑗 ≠ |𝑖 − 𝑗|
01.08.2018
4 Q
3 Q
2 Q
1 Q
1 2 3 4 𝑄1 = 3
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
14
LOGIC PROGRAMMING
01.08.2018
Logic Program =
Facts + Rules +
Queries
Knowledge
Base
Logic Data =
Constants + Variables +
Compound Terms Terms
123 𝑝𝑟𝑜𝑙𝑜𝑔 𝑎 𝑁𝑢𝑚𝑏𝑒𝑟 𝐿𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑋 _𝑥
𝑙𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑝𝑟𝑜𝑙𝑜𝑔 1,2,3 𝑓(𝑎, 𝑔(𝑋))
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
15
PROLOG PROGRAMMING should_i_learn(Language) :-
curious_about_programming_languages,
already_own_book_covering(Language).
curious_about_programming_languages :-
reading(seven_languages_book).
already_own_book_covering(Language) :-
covers(Book, Language),
own(Book).
reading(seven_languages_book).
own(seven_languages_book).
covers(seven_languages_book, clojure).
covers(seven_languages_book, haskell).
covers(seven_languages_book, io).
covers(seven_languages_book, prolog).
covers(seven_languages_book, scala).
covers(seven_languages_book, erlang).
covers(seven_languages_book, ruby).
01.08.2018
“If you're curious about
programming languages and
you already own a book that
covers whatever language
you're asking about then yes,
you should learn it“
(Jeff Dallien, Quora, 2011)
| ?- should_i_learn(prolog).
yes
Rules
Facts Query
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
16
CRYPT-ARITHMETIC IN PROLOG sol([T,W,O,F,U,R]) :-
digit(T),
T \= 0,
digit(W),
W \= T,
digit(O),
O \= T, O \= W,
TWO is 100*T+10*W+O,
digit(F),
F \= 0, F \= T, F \= W, F \= O,
digit(U),
U \= T, U \= W, U \= O, U \= F,
digit(R),
R \= T, R \= W, R \= O, R \= F, R \= U,
FOUR is 1000*F+100*O+10*U+R,
TWO+TWO =:= FOUR.
01.08.2018
digit(0).
digit(1).
digit(2).
digit(3).
digit(4).
digit(5).
digit(6).
digit(7).
digit(8).
digit(9).
Can be replaced with: \+ U = F
OR not U = F
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
17
LISTS • A list is a compound term representing sequence of
elements (terms) 𝑡1, 𝑡2, … , 𝑡𝑛:
,𝑡1, 𝑡2, … , 𝑡𝑛-
• A list is either a compound term written ,𝐻|𝑇- composed of:
– Head = first element 𝐻
– Tail = list of rest of elements after removing the head 𝑇
OR an empty list denoted by constant , -.
• For example the list:
,𝑎, 𝑏, 2-
represents terms:
,𝑎| 𝑏 2 - and ,𝑎| 𝑏, 2 - and ,𝑎|,𝑏| 2 -- and ,𝑎, 𝑏|,2--
01.08.2018
All 5 versions are equivalent !
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
18
RECURSION
• I know you if
We met
OR
I met someone that
knows you
01.08.2018
met(alice,bob).
met(bob,carol).
met(alice,john).
knows_by_meeting(A,B) :-
met(A,B);met(B,A).
knows(A,B) :-
knows_by_meeting(A,B).
knows(A,B) :-
knows_by_meeting(A,C),
knows(C,B).
top :-
knows(A,B),
printf("%q knows %q\n",[A,B]),
fail.
top.
Failure driven loop
Infinite loop !
WHY?
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
19
SOLUTION – LIMIT SEARCH ...
knows(A,B,N) :-
N>=0,
knows_by_meeting(A,B).
knows(A,B,N) :-
N>0,
M is N-1,
knows_by_meeting(A,C),
knows(C,B,M).
knows(A,B) :-
knows(A,B,3).
...
01.08.2018
• I know you if
We met
OR
I know you through at
most 𝑁 intermediaries
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
20
CONSTRAINT LOGIC PROGRAMMING
• CLP program = set of facts and rules built with:
i. Normal Prolog predicates handled by Prolog's search
engine.
ii. Constraints handled by specialized constraint solvers.
• CLP program structure:
i. Definition of variables and domains
ii. Definition of constraints
iii. Definition of cost variable
iv. Search for optimal solution
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
21
ECLIPSE-CLP
• Open-source software system with support from
CISCO
• Integrates various Logic Programming extensions
• It contains:
• several constraint solver libraries
• a high-level modeling and control language – extension
of Prolog
• interfaces to third-party solvers
• API for embedding into host environments
• Tk/Tcl-based IDE
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
22
CARTESIAN PRODUCTS
• Let 𝐴1, 𝐴2, … , 𝐴𝑚 be 𝑚 finite sets:
𝐴𝑖 = 1, 2, … , 𝑛𝑖 , 1 ≤ 𝑖 ≤ 𝑚
• Generate all the Π𝑖<1𝑚 𝑛𝑖 elements of the Cartesian
product 𝑃 = 𝐴1 × 𝐴2 ×⋯× 𝐴𝑚.
• Decision variables: a vector 𝐸 of 𝑚 elements.
• Domains: 𝐸𝑖 ∈ 𝐴𝑖
• Constraints: ∅
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
23
CARTESIAN PRODUCTS IN CLP :-lib(ic).
no_of_sets(3).
top :-
Max = [](2,3,2),
no_of_sets(NSets),
dim(E,[NSets]),
(for(I,1,NSets), param(E,Max) do
arg(I,E,EI),
EI :: 1..Max[I]
),
labeling(E),
printf(output,"Solution: %q \n",[E]),
fail.
top.
01.08.2018
There are 3 sets
No. of. elems. of each set
Declarative loop to define
domains of each decision
variable
Default search
Define decision variables
E and Max are global
to the loop
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
24
ORDER OF SEARCH
• Variables are instantiated in the order of appearance in the collection:
𝐸1, 𝐸2, 𝐸3
• Values are considered according to their domain ordering: 1,2,3
01.08.2018
1 2
1 3 2
1 2 1 2 1 2
1 3 2
1 2 1 2 1 2
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
25
LIMITED DISCREPANCY SEARCH
• Replace labeling(E) with search(E,0,input_order,indomain,lds(2),[]).
• lds(Disc) = Disc “discrepancies” from the heuristic / first value chosen.
• Incomplete search !
01.08.2018
2
1 2
1 3 2
1 2 1 2 1
1
1 2 1
1 2 3 4 5 6 7 8
Order of generating
solutions
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
26
SUBSETS • Let 𝐴 be a set with 𝑛 elements:
𝐴 = *1,2,… , 𝑛+
• Subsets 𝑆 ⊆ 𝐴 can be represented using a vector 𝑋 =(𝑥1, 𝑥2, … , 𝑥𝑛) with elements in *0,1+ such that:
𝑥𝑖 = 1 𝑖𝑓 𝑖 ∈ 𝑆0 𝑖𝑓 𝑖 ∉ 𝑆
• For example the subsets of *1,2,3+ are 0,0,0 , (0,0,1), (0,1,0), 0,1,1 , (1,0,0), (1,0,1), (1,1,0), 1,1,1 .
• Subsets with this representation can be generated by
adapting the method for Cartesian products. HOW?
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
27
SUBSETS-2 • Let 𝐴 be a set with 𝑛 elements:
𝐴 = *1,2,… , 𝑛+
• Subsets 𝑆 ⊆ 𝐴 can be represented using a vector 𝑋 =(𝑥1, 𝑥2, … , 𝑥𝑛) with elements in *0,1,… , 𝑛+ such that:
𝑆 = 𝑥𝑖 𝑥𝑖 > 0, 1 ≤ 𝑖 ≤ 𝑛+
• To ensure uniqueness of representation of each set:
– Vector 𝑋 is increasingly sorted: 𝑥1 ≤ 𝑥2 ≤ ⋯ ≤ 𝑥𝑛
– Each element of 𝑆 occurrs uniquely in 𝑋
• For example the subsets of *1,2,3+ are 0,0,0 , (0,0,1), (0,0,2), 0,0,3 , (0,1,2), (0,1,3), (0,2,3), 1,2,3 .
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
28
SUBSETS-2 IN CLP :-lib(ic).
no_of_elems(4).
top :-
no_of_elems(NElems),
dim(S,[NElems]),
S :: 0..NElems,
(for(I,2,NElems), param(S) do
S[I-1] #=< S[I],
(S[I] #> 0) => (S[I-1] #< S[I])
),
labeling(S),
printf(output,"Solution: %q \n",[S]),
fail.
top.
01.08.2018
Ensures that:
𝑆 1 ≤ 𝑆 2 ≤ ⋯ ≤ 𝑆,𝑁-
Reified constraint.
Ensures that any two
non-zero elements are
distinct.
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
29
COMBINATIONS
• Let 𝐴 be a set with 𝑛 elements:
𝐴 = *1,2, … , 𝑛+
• Subsets 𝑆 ⊆ 𝐴 with 𝑘 elements can be represented
using a vector 𝑋 = (𝑥1, 𝑥2, … , 𝑥𝑘) with elements in
*1, … , 𝑘+ such that:
𝑥𝑖;1 < 𝑥𝑖 for all 2 ≤ 𝑖 ≤ 𝑛
• Think about an alternative representation using the
characteristic vector ! HOW?
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
30
COMBINATIONS IN CLP :-lib(ic).
no_of_elems(4). % N
no_of_selected_elems(2). % K
top :-
no_of_elems(N),
no_of_selected_elems(K),
dim(S,[K]),
S :: 1..N,
(for(I,2,K), param(S) do
S[I-1] #< S[I]
),
labeling(S),
printf(output,"Subset: %q \n",[S]),
fail.
top.
01.08.2018
Ensures that:
𝑆 1 < 𝑆 2 < ⋯ < 𝑆,𝐾-
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
31
PERMUTATIONS
• Let 𝐴 be a set with 𝑛 elements:
𝐴 = *1,2, … , 𝑛+
• A permutation is a one-to-one mapping:
𝜎: 𝐴 → 𝐴
• A permutation can be represented using a vector
𝑋 = (𝑥1, 𝑥2, … , 𝑥𝑛) with elements in *1, … , 𝑛+ such
that:
𝑎𝑙𝑙𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑡(,𝑥1, 𝑥2, … , 𝑥𝑛-)
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
32
PERMUTATIONS IN CLP :-lib(ic).
no_of_elems(4).
top :-
no_of_elems(N),
dim(P,[N]),
P :: 1..N,
alldifferent(P),
labeling(S),
printf(output,"Solution: %q \n",[P]),
fail.
top.
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
33
TRIANGLE AREAS IN CLP :- lib(ic).
model(N, M, A, [X2, Y2, X3, Y3]) :-
[X2, X3] :: 0..N,
[Y2, Y3] :: 0..M,
A #= abs(X2 * Y3 - X3 * Y2).
do_case(Case_num, N, M, A) :-
printf("Case #%w: ", [Case_num]),
( model(N, M, A, Points), labeling(Points) ->
printf("0 0 %w %w %w %w", Points)
;
write("IMPOSSIBLE")
),
nl.
top :-
read_token(C,integer),
( for(Case_num, 1, C) do
read_token(N,integer), read_token(M,integer),
read_token(A,integer),
do_case(Case_num, N, M, A) ).
01.08.2018
Input:
1000
1 1 1
1 2 64
10 10 1
1 1 1
...
No.of.cases (C)
Case 1 (N,M,A)
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
34
CRYPT-ARITHMETIC IN CLP :- lib(ic). % library for arithmetic constraints
twotwofour(Digits) :-
Digits = [T,W,O,F,U,R], % Variables ... digits
Digits :: [0..9], % Their domains {0,1,...,9}
Carries = [C1,C2,C3], % Other variables ... carries
Carries :: [0..1], % Their domains {0,1}
alldifferent(Digits), % Global constraint
T #\= 0, % Constraint “not equal”
F #\= 0,
C3 #= F, % Constraint “equal”
C2 + 2*T #= O + 10*C3,
C1 + 2*W #= U + 10*C2,
2*O #= R + 10*C1,
labeling(Carries), % Search for assignments
labeling(Digits).
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
35
LOGIC PUZZLES • On a street there are five houses of different colors, occupied by five
individuals of different nationalities, who own five different pets, drink five
different beverages, and smoke five different brands of cigarettes. Also: 1. The occupant of the red house is English.
2. The Spaniard owns a dog.
3. The coffee drinker lives in a green house.
4. The Ukrainian drinks tea.
5. The ivory house is to the left of the green one.
6. The snails owner smokes Winston’s.
7. The person in the yellow house smokes Kool’s.
8. The occupant in the middle house drinks milk.
9. The Norwegian lives in the leftmost house.
10. The Chesterfield smoker lives next to the fox owner.
11. The Kool smoker lives next to the horse owner.
12. The orange juice drinker smokes Lucky Strike’s.
13. The Parliament smoker is Japanese.
14. The Norwegian lives next to a blue house.
• Who owns the zebra? Who drinks water?
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
36
LOGIC PUZZLES IN CLP :- lib(ic).
top :-
% Define variables and their domains
...
% Define constraints
...
% Search for solutions
...
% Decode and print out solutions
...
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
37
DEFINE VARIABLES AND DOMAINS
% Variables: we use 5 arrays of 5 variables each
Nat = [](English, Spaniard, Ukrainian, Norwegian,
Japanese),
Color = [](Red, Green, Ivory, Yellow, Blue),
Cigarettes = [](Winston, Kool, Chesterfield, Lucky,
Parliament),
Pet = [](Dog, Snails, Fox, Horse, Zebra),
Drink = [](Coffee, Tea, Milk, Juice, Water),
% Domains: all variables range over house numbers 1 to 5
Nat :: 1..5,
Color :: 1..5,
Cigarettes :: 1..5,
Pet :: 1..5,
Drink :: 1..5,
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
38
DEFINE CONSTRAINTS % Constraints: the values in each list are exclusive
alldifferent(Nat), alldifferent(Color), alldifferent(Cigarettes),
alldifferent(Pet),alldifferent(Drink),
% The actual 14 constraints
English = Red, % 1
Spaniard = Dog, % 2
Coffee = Green, % 3
Ukrainian = Tea, % 4
Green #= Ivory + 1, % 5
Snails = Winston, % 6
Yellow = Kool, % 7
Milk = 3, % 8
Norwegian = 1, % 9
Dist1 #= Chesterfield - Fox, Dist1 :: [-1, 1], % 10
Dist2 #= Kool - Horse, Dist2 :: [-1, 1], % 11
Juice = Lucky, % 12
Parliament = Japanese, % 13
Dist3 #= Norwegian - Blue, Dist3 :: [-1, 1], % 14
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
39
SEARCH AND PRINT OUT SOLUTIONS
% Search: label all variables with values
labeling([](Nat, Color, Cigarettes, Pet, Drink)),
% Print out solutions: we need to do some decoding
NatNames = [English-english, Spaniard-spaniard, Japanese-japanese,
Ukrainian-ukrainian, Norwegian-norwegian],
member(Zebra-ZebraNat, NatNames),
member(Water-WaterNat, NatNames),
printf("The %w owns the zebra%n", [ZebraNat]),
printf("The %w drinks water%n", [WaterNat]).
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
40
FREIGHT TRANSPORTATION EXCHANGES
• Opportunity:
– need of transporting goods
– availability of free vehicles
• Goal:
– capturing transportation
opportunities
– matchmaking of owners of
goods with freight
transportation providers
01.08.2018
• New business model: virtual logistics platforms
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
41
MAS FOR FREIGHT BROKERING
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
42
AGENT INTERACTION
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
43
FREIGHT BROKER AGENT
• Declarative optimization model of the
FBAgent:
– Defined as type of Vehicle Routing with
Pickup and Delivery Problem – VR-PDP
– Computing optimal schedules using
Constraint Logic Programming – CLP.
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
44
VR-PDP
• Well-known problem in operation research:
Given:
– A set of customers
– A vehicle pool to service deliveries
Determine:
– A minimum cost set of vehicle routes that services
all customers
• Service:
– Pickup = freight loading point
– Delivery = freight unload point
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
45
DECLARATIVE MODELING
• AIM: The definition of a declarative optimization
agent – OAgent.
– Declarative constraint model as a special type of
vehicle routing with pickup and delivery problem.
– Augmenting this model with a declarative
representation of optimal transport schedules.
– Computing optimal schedules using Constraint Logic
Programming – ECLiPSe-CLP.
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
46
PROBLEM Tuple ℒ, 𝒪, 𝒯, Δ s.t.:
1. ℒ = locations of interest = 𝒫 ∪ℋ, 𝒫 = *1,2,… , 𝑘+ = pickup
and delivery points, 𝑘 > 0. ℋ = *𝑘 + 1,… , 𝑘 + + = truck
home locations, ≥ 0
2. 𝒪 = customer orders, |𝒪| = 𝑛. 𝑂𝑖 = (𝑂𝑆𝑖 , 𝑂𝐷𝑖 , 𝐶𝑖) s.t.
𝑂𝑆𝑖 , 𝑂𝐷𝑖 ∈ 𝒫, 𝑂𝑆𝑖 ≠ 𝑂𝐷𝑖 are pickup and delivery points,
𝐶𝑖 > 0 = requested capacity of order 𝑖. Obs: 2 ≤ 𝑘 ≤ 2𝑛.
3. 𝒯 = set of trucks, |𝒯| = 𝑡. 𝑇𝑖 = (𝐻𝑖 , Γ𝑖) s.t. 𝐻𝑖 ∈ ℋ and Γ𝑖 > 0
are home location and provided capacity of truck 𝑖.
4. Δ = 𝑘 + × (𝑘 + ) real positive matrix s.t. Δ𝑖𝑗 > 0 are
distances between locations 1 ≤ 𝑖 ≠ 𝑗 ≤ 𝑘 + .
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
47
EXAMPLE PROBLEM • 𝑘 = 3 pickup and delivery points
• 𝑛 = 3 orders: 𝒪 = * 1 → 2,5 , 1 → 3,2 , 2 → 3,6 +
• 𝑡 = 2 trucks: 𝒯 = *(4,7), (4,5)+. Trucks home location is
4, so there are = 1 home locations
01.08.2018
4
1
2
3
2
5
6
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
48
SCHEDULE
Tuple ⟨𝑋,𝑀, 𝑆, 𝐷⟩ s.t.:
1. 𝑋 ∈ 1,2,… , 𝑘 𝑚 = hops sequence of truck routes. Each
location is visited, each order needs two hops, 𝑘 ≤ 𝑚 ≤ 2𝑛.
2. 𝑀 ∈ *0,1,… ,𝑚+𝑡 s.t. 𝑀𝑙 is the number of hops of each truck
𝑙. Total number of hops: 𝑚 = ∑𝑙<1𝑡 𝑀𝑙.
– 𝑀𝑙 ≥ 0 allows solutions of “at most” 𝑡 trucks.
– 𝑀𝑙 ≥ 1 constraints solutions to use “exactly” 𝑡 trucks.
3. 𝑆, 𝐷 ∈ *0,1+𝑚×𝑛 are two Boolean matrices s.t. 𝑆𝑖𝑗 = 1 iff 𝑋𝑖 is
the pickup point of order 𝑗, otherwise 𝑆𝑖𝑗 = 0. 𝐷𝑖𝑗 = 1 iff 𝑋𝑖 is
the delivery point of order 𝑗, otherwise 𝐷𝑖𝑗 = 0.
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
49
EXAMPLE SCHEDULE • 𝑘 = 3 pickup and delivery points
• 𝑛 = 3 orders: 𝒪 = * 1 → 2,5 , 1 → 3,2 , 2 → 3,6 +
• 𝑡 = 2 trucks: 𝒯 = *(4,7), (4,5)+. Trucks home location is
4, so there are = 1 home locations
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
50
STATE SPACE SIZE
• 𝑋 = 𝑚, 𝑋𝑖 ∈ 1,2, … , 𝑘 , 𝑘 ≤ 𝑚 ≤ 2𝑛
• |𝑆| = 2𝑚𝑛, 𝐷 = 2𝑚𝑛
• 𝑆𝑖𝑧𝑒 = ∑ 2𝑚𝑛2𝑚𝑛𝑚𝑘2𝑛𝑚<𝑘
• 𝑛 = 5, 𝑘 = 6
• 𝑆𝑖𝑧𝑒 = 26066 + 27076 + 28086 + 29096 + 2100106 >𝟏𝟎𝟑𝟔 !!
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
51
PICKUP & DELIVERY DEFINITION
• Constraints (1), (2). For each hop 𝑖 and order 𝑗 the
pickup point of order 𝑗 is 𝑂𝑆𝑗 and the delivery point
of order 𝑗 is 𝑂𝐷𝑗 so:
(∀𝑖, 𝑗)((𝑆𝑖𝑗 = 1) ⇒ (𝑋𝑖 = 𝑂𝑆𝑗))
(∀𝑖, 𝑗)((𝐷𝑖𝑗 = 1) ⇒ (𝑋𝑖 = 𝑂𝐷𝑗))
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
52
NON-REDUNDANT HOPS
• Constraint (3). There exists at least one load or
unload in each hop 𝑖 so:
∀𝑖 ∑𝑖<1𝑛 𝑆𝑖𝑗 + 𝐷𝑖𝑗 ≥ 1
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
53
PICKUP PRECEDES DELIVERY
• Constraint (4). For all hops 𝑖, 𝑘 if there exists an
order 𝑗 such that 𝑖 is the pickup point of 𝑗 and 𝑘 is
the delivery point of 𝑗 then 𝑖 precedes 𝑘, so:
(∀𝑖, 𝑘) ( ∃𝑗 ((𝑆𝑖𝑗 = 1) ∧ (𝐷𝑖𝑗 = 1)) ⇒ 𝑖 < 𝑘 )
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
54
SERVICE COMPLETENESS
• Constraint (5). For each order 𝑗 there is a unique
load point and a unique unload point, so:
(∀𝑗)((∑𝑖<1𝑚 𝑆𝑖𝑗 = 1) ∧ (∑𝑖<1
𝑚 𝐷𝑖𝑗 = 1))
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
55
TRUCK ASSIGNMENTS • Constraints (6). Each order is completely served by a
unique truck 𝑙, so order with load point assigned to 𝑙 has the
unload point assigned to 𝑙 :
∀𝑙, 𝑗 ∑𝑖<𝐴𝑙
𝐵𝑙 𝑆𝑖𝑗 = ∑𝑖<𝐴𝑙
𝐵𝑙 𝐷𝑖𝑗
• where
𝐴𝑙 = 1 + ∑𝛼< 1𝑙;1 𝑀𝛼 for 1 ≤ 𝑙 ≤ 𝑡
𝐵𝑙 = ∑𝛼<1𝑙 𝑀𝛼 for 1 ≤ 𝑙 ≤ 𝑡
• Constraints (7). Non-redundancy constraint:
∀𝑖 ∈ 𝐴𝑙 , 𝐵𝑙 − 1 𝑋𝑖≠ 𝑋𝑖:1
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
56
CAPACITY CONSTRAINTS
• Constraints (8). For each truck hop the
transportation capacity does not overflow, so:
𝑇0 = 0
𝑇𝑖 = 𝑇𝑖;1 + ∑𝑗 <1𝑛 𝑆𝑖𝑗 − 𝐷𝑖𝑗 𝐶𝑗 for 1 ≤ 𝑙 ≤ 𝑡 and
𝐴𝑙 ≤ 𝑖 ≤ 𝐵𝑙
𝑇𝑖 ≤ Γ𝑖 for 1 ≤ 𝑙 ≤ 𝑡
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
57
OPTIMIZATION COST • Cost. The total distance 𝐷𝑙 travelled by truck 𝑙 = 1,2, … , 𝑡, as well as the
total distance 𝐷𝐼𝑆𝑇 travelled by all trucks, is:
𝐷𝑙 =
0 if 𝐼𝑙 = 𝐴𝑙 , 𝐵𝑙 = ∅
Δ𝐻𝑙𝑋𝐴𝑙+ Δ𝑋𝐵𝑙𝐻𝑙
+ ∑ Δ𝑋𝑙𝑋𝑙+1𝐵𝑙;1𝑖<𝐴𝑙
otherwise
𝐷𝐼𝑆𝑇 = ∑𝑙<1𝑡 𝐷𝑙
• For CLP mapping, variables Δ𝐻𝑙𝑋𝐴𝑙, Δ𝑋𝐵𝑙𝐻𝑙
, and Δ𝑋𝑖𝑋𝑖+1 are rewritten:
Δ𝐻𝑙𝑋𝐴𝑙= ∑𝑥<1
𝑘 Δ𝐻𝑙𝑥(𝑥 = 𝑋𝐴𝑙)
Δ𝑋𝐵𝑙𝐻𝑙= ∑ Δ𝐻𝑙𝑥(𝑥 = 𝑋𝐵𝑙)
𝑘𝑥<1
Δ𝑋𝑖𝑋𝑖+1 = ∑ Δ𝑥𝑦(𝑥 = 𝑋𝑖)(𝑦 = 𝑋𝑖:1)𝑘𝑥,𝑦<1
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
58
OPTIMIZATION AGENT – OAGENT
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
59
SCHEDULING PROBLEM AS PROLOG FACTS number_of_orders(3).
number_of_cities(3).
number_of_trucks(3).
number_of_homes(1).
% order(OrderIdx, LoadIdx, UnloadIdx, Capacity)
order(1,1,2,5).
order(2,1,3,2).
order(3,2,3,6).
% truck(TruckIdx, HomeIdx, MaxCapacity)
truck(1,4,7).
truck(2,4,4).
truck(3,4,5).
% distance(LocationI, LocationJ, DistanceIJ)
distance(1,2,10).
distance(2,1,10).
% ...
distance(4,3,10).
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
60
SOLUTION PREDICATE
solution(_m,_n,_t,M,S,D,X,Ds) :-
domains_and_variables(_m,_n,_t,M,S,D,X,Delta),
constraints(_m,_n,_t,M,S,D,X),
search_query(X,S,D),
compute_distances(_m,_t,M,Delta,X,Ds).
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
61
DECLARATIVE LOOPS
( 𝐼𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑆𝑝𝑒𝑐𝑠 do 𝐺𝑜𝑎𝑙 )
• Iteration specifiers:
– for(𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒 , 𝑀𝑖𝑛𝐸𝑥𝑝𝑟 , 𝑀𝑎𝑥𝐸𝑥𝑝𝑟)
– param(𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒1 , 𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒2 , …)
– fromto(𝐹𝑖𝑟𝑠𝑡 , 𝐼𝑛 , 𝑂𝑢𝑡 , 𝐿𝑎𝑠𝑡)
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
62
IMPLEMENTATION OF CONSTRAINTS
• Using 𝑝 ⇒ 𝑞 ≡ ≦𝑞 ⇒ ≦𝑝, constraint:
(∀𝑖, 𝑘) ( ∃𝑗 ((𝑆𝑖𝑗 = 1) ∧ (𝐷𝑖𝑗 = 1)) ⇒ 𝑖 < 𝑘 )
is transformed to equivalent form:
(∀𝑖, 𝑗, 𝑘)( 𝑖 ≥ 𝑘 ⇒ ((𝑆𝑖𝑗 = 0) ∨ (𝐷𝑖𝑗 = 0))
• In ECLiPSe-CLP:
constraint_2(M,N,S,D) :-
( for(J,1,N), param(M,S,D) do
( for(I,1,M), param(J,S,D) do
( for(K,1,I), param(I,J,S,D) do
S[I,J] #= 0 or D[K,J] #= 0
)
)
).
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
63
SEARCH PROCESS
01.08.2018
• Top level search
– Instantiates number of hops 𝑚 and vector 𝑀 of
route lengths of each truck such that:
∑ 𝑀𝑙 = 𝑚𝑡𝑙<1
• Main search
– Searches for problem solutions 𝑋, 𝑆, 𝐷 for fixed
values of 𝑚 and 𝑀.
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
64
SEARCH QUERY
search_query(X,S,D) :-
search([](X,S,D),0,
most_constrained,
indomain_random,
lds(2),
[nodes(28000)]
).
01.08.2018
Limited
discrepancy
search
Stochastic
search
Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
65
DATA SET & SEARCH CONFIGURATION
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
66
SOLUTIONS OF 10-4 PROBLEM
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
67
REFERENCES • Costin Bǎdicǎ, Florin Leon, Amelia Bǎdicǎ. Freight transportation broker
agent based on constraint logic programming. Evolving Systems.
Springer (2018). doi: 10.1007/s12530-018-9230-3
• Sergii Dymchenko, Mariia Mykhailova. Declaratively solving tricky
google code jam problems with prolog-based ECLiPSe CLP system.
Proc.30th Annual ACM Symposium on Applied Computing (SAC '15).
2122-2124, ACM (2015). doi: 10.1145/2695664.2696032. Extended
version: https://arxiv.org/abs/1412.2304v2
• Hector J. Levesque. Thinking as Computation. A First Course. MIT
Press (2012)
• Antoni Niederliński. A gentle guide to constraint logic programming via
ECLiPSe, 3rd ed. Jacek Skalmierski Computer Studio, Gliwice (2014)
• ECLiPSe Web page: http://eclipseclp.org. Follow the ‘Examples’ link.
01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018
68 01.08.2018 Problem Solving in Real World Systems and Games. Frankfurt am Main, 2018