1% BEGIN LICENSE BLOCK 2% Version: CMPL 1.1 3% 4% The contents of this file are subject to the Cisco-style Mozilla Public 5% License Version 1.1 (the "License"); you may not use this file except 6% in compliance with the License. You may obtain a copy of the License 7% at www.eclipse-clp.org/license. 8% 9% Software distributed under the License is distributed on an "AS IS" 10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 11% the License for the specific language governing rights and limitations 12% under the License. 13% 14% The Original Code is The ECLiPSe Constraint Logic Programming System. 15% The Initial Developer of the Original Code is Cisco Systems, Inc. 16% Portions created by the Initial Developer are 17% Copyright (C) 2006 Cisco Systems, Inc. All Rights Reserved. 18% 19% Contributor(s): 20% 21% END LICENSE BLOCK 22% 23% $Id: modelling.tex,v 1.1 2006/09/23 01:49:52 snovello Exp $ 24% 25 26%---------------------------------------------------------------------- 27\chapter{Problem Modelling} 28%HEVEA\cutdef[1]{section} 29%---------------------------------------------------------------------- 30 31 32%---------------------------------------------------------------------- 33\section{Constraint Logic Programming} 34%---------------------------------------------------------------------- 35 36\index{Constraint Logic Programming} 37\index{modelling} 38One of the main ambitions of Constraint Programming is the 39separation of Modelling, Algorithms and Search. 40This is best characterised by two pseudo-equations. 41\index{Kowalski} 42The first one is paraphrased from Kowalski \cite{kowalski79} 43\begin{quote}\begin{verbatim} 44Solution = Logic + Control 45\end{verbatim}\end{quote} 46and states that we intend to solve a problem by 47giving a logical, declarative description of the problem and 48adding control information that enables a computer to deduce a solution. 49 50\index{combinatorial problems} 51The second equation 52\begin{quote}\begin{verbatim} 53Control = Reasoning + Search 54\end{verbatim}\end{quote} 55is motivated by a fundamental difficulty we face when dealing with 56combinatorial problems: we do not have efficient algorithms 57for finding solutions, we have to resort to a combination of 58reasoning (via efficient algorithms) and (inefficient) search. 59 60We can consider every constraint program as an exercise in 61combining the 3 ingredients: 62\begin{itemize} 63\item {\bf Logic} - The design of a declarative {\em Model} of the problem. 64\item {\bf Reasoning} - The choice of clever {\em Constraint Propagation} 65 algorithms that reduce the need for search. 66\item {\bf Search} - The choice of search {\em strategies and heuristics} for 67 finding solutions quickly. 68\end{itemize} 69In this chapter we will focus on the first issue, {\bf Problem Modelling}, 70and how it is supported by \eclipse{}. 71 72 73%---------------------------------------------------------------------- 74\section{Issues in Problem Modelling} 75%---------------------------------------------------------------------- 76 77A good formalism for problem modelling should fulfil the following criteria: 78\begin{itemize} 79\item {\bf Expressive power} - 80 Can we write a formal model of the real world problem? 81\item {\bf Clarity for humans} - 82 How easily can the model be written, read, understood or modified? 83\item {\bf Solvability for computers} - 84 Are there good known methods to solve it? 85\end{itemize} 86%In \eclipse{}, we model problems using a high-level logic-based language. 87Higher-level models are typically 88closer to the user and close to the problem and therefore 89easier to understand and to trust, 90easier to debug and to verify, 91and easier to modify when customers change their mind. 92On the other hand, it is not necessarily easy to see how they can be 93solved, because high-level models contain 94high-level notions (e.g.\ sets, tasks) and 95heterogeneous constraints. 96 97The constraint programming approach also addresses one of the classical 98sources of error in application development with 99traditional programming languages: the transition from a 100{\em formal description} 101of the problem to the {\em final program} that solves it. 102%\begin{itemize} 103%\item Informal specification, which leads to a 104%\item Formal description, which leads to a 105%\item Program 106%\end{itemize} 107The question is: Can the final program be trusted? 108The Constraint (Logic) Programming solution is to 109\begin{itemize} 110\item Keep the initial formal model as part of the final program 111\item Enhance rather than rewrite 112\end{itemize} 113The process of enhancing the initial formal model involves for example 114\begin{itemize} 115\item Adding control annotations, e.g. 116 algorithmic information or heuristic information. 117\item Transformation: 118 Mapping high-level (problem) constraints into 119 low-level (solver) constraints, 120 possibly exploiting multiple, redundant mappings. 121\end{itemize} 122There are many other approaches to problem modelling software. 123The following is a brief comparison: 124\begin{description} 125\item[Formal specification languages (e.g. Z, VDM)] 126 \index{specification languages} 127 More expressive power than ECLiPSe, but not executable 128\item[Mathematical modelling languages (e.g. OPL, AMPL)] 129 \index{mathematical modelling languages} 130 Similar to ECLiPSe, but usually limited expressive power, 131 e.g.\ fixed set of constraints. 132\item[Mainstream programming languages (e.g. C++ plus solver library)] 133 \index{C++} 134 Variables and constraints are "aliens" in the language. 135 Specification is mixed with procedural control. 136\item[Other CLP/high-level languages (e.g. CHIP)] 137 \index{CHIP} 138 Most similar to ECLiPSe. Less support for hybrid problem solving. 139 Harder to define new constraints. 140\end{description} 141 142 143 144\section{Modelling with CLP and \eclipse{}} 145 146When modelling problems with constraints, the basic idea is to set up 147a network of variables and constraints. Figure \ref{figconsnet} shows 148such a constraint network. 149\begin{figure} 150\begin{center} 151\resizebox{0.7\textwidth}{!}{\includegraphics{consnet.eps}} 152\end{center} 153\caption{A Constraint Network} 154\label{figconsnet} 155\end{figure} 156It can be seen that the Constraint Logic Programming (CLP) formulation 157\begin{itemize} 158\item is a natural declarative description of the constraint network 159\item can serve as a program to set up the constraint network 160\end{itemize} 161\ignore{ 162The following properties make CLP langauges like 163\eclipse[] suited as a modelling language: 164\begin{description} 165\item[Simple language] 166 Logical variables, 167 logical connectives (and, or, implication), 168 predicates (relations, constraints), 169 a single compound data type (structure), 170 symbolic equality (unification) 171\item[Logical reading] 172 {\tt Program = Logic + Control} 173\item[Flexible syntax] 174 {\tt TaskA uses ResourceB} 175\item[Symbolic and meta-programming capability] 176 Easy to add domain-specific language features 177\end{description} 178} 179The main \eclipse{} language constructs used in modelling are 180\begin{description} 181\item[Built-in constraints]\ \\ 182 \verb?X #> Y? 183\item[Abstraction]\ \\ 184 \verb?before(task(Si,Di), task(Sj,Dj)) :- Si+Di #<= Sj.? 185\item[Conjunction]\ \\ 186 \verb?between(X,Y,Z) :- X #< Y, Y #< Z.? 187\item[Disjunction (but see below)]\ \\ 188 \verb?neighbour(X,Y) :- ( X #= Y+1 ; Y #= X+1 ).? 189\item[Iteration]\ \\ 190 \verb?not_among(X, L) :- ( foreach(Y,L),param(X) do X #\= Y ).? 191\item[Recursion]\ \\ 192 \verb?not_among(X, []).?\\ 193 \verb?not_among(X, [Y|Ys]) :- X #\= Y, not_among(X, Ys).? 194\end{description} 195 196 197%---------------------------------------------------------------------- 198\section{Same Problem - Different Model} 199%---------------------------------------------------------------------- 200 201There are often many ways of modelling a problem. 202Consider the famous "SEND + MORE = MONEY" example: 203\begin{code} 204sendmore(Digits) :- 205 Digits = [S,E,N,D,M,O,R,Y], 206 Digits :: [0..9], 207 alldifferent(Digits), 208 S #\verb.\.= 0, M #\verb.\.= 0, 209 1000*S + 100*E + 10*N + D 210 + 1000*M + 100*O + 10*R + E 211 #= 10000*M + 1000*O + 100*N + 10*E + Y. 212\end{code} 213An alternative model is based on the classical decimal addition algorithm with 214carries: 215\begin{code} 216sendmore(Digits) :- 217 Digits = [S,E,N,D,M,O,R,Y], 218 Digits :: [0..9], 219 Carries = [C1,C2,C3,C4], 220 Carries :: [0..1], 221 alldifferent(Digits), 222 S #\verb.\.= 0, 223 M #\verb.\.= 0, 224 C1 #= M, 225 C2 + S + M #= O + 10*C1, 226 C3 + E + O #= N + 10*C2, 227 C4 + N + R #= E + 10*C3, 228 D + E #= Y + 10*C4. 229\end{code} 230Both models work fine, but obviously involve different variables and 231constraints. Even though high-level models reduce the need for finding 232sophisticated encodings of problems, finding good models still requires 233substantial expertise and experience. 234 235\ignore{ 236Abstraction 237 238:- lib(ria). % Using interval library 239 240farm(F) :- 241 [A,B,C] :: 0.0..inf, % The 3 sides of the lake 242 triangle_area(A, B, C, 7), % The lake area is 7 243 244 [F,FA,FB,FC] :: 1..inf, % The square areas are integral 245 square_area(A, FA), 246 square_area(B, FB), 247 square_area(C, FC), 248 F *= FA+FB+FC, 249 250 FA *>= FB, FB *>= FC. % Avoid symmetric solutions 251 252triangle_area(A, B, C, Area) :- 253 S *>= 0, 254 S *= (A+B+C)/2, 255 Area *= sqrt(S*(S-A)*(S-B)*(S-C)). 256 257square_area(A, Area) :- 258 Area *= sqr(A). 259} 260 261 262\ignore{ 263\subsection{Using Data Structures} 264 265Structures 266Task = task(plumbing, S, 3, R, _) 267Task = task with [duration:3, resource:R, start:S, name:plumbing] 268Lists 269Digits = [S,E,N,D,M,O,R,Y] 270length(VarList, 100) 271Arrays 272dim(ChessBoard, [8,8]) 273VarArray =.. [_|VarList] 274 275In ECLiPSe, lists and arrays are just special structures! 276} 277 278 279 280\section{Rules for Modelling Code} 281 282In CLP, the declarative model is at the same time the constraint setup code. 283This code should therefore be deterministic and terminating, so: 284\begin{description} 285\item[Careful with disjunctions] 286 Don't leave choice-points (alternatives for backtracking). 287 Choices should be deferred until search phase. 288\item[Use only simple conditionals] 289 Conditions in \verb=(...->...;...)= must be true or false at modelling time! 290\item[Use only structural recursion and loops] 291 Termination conditions must be know at modelling time! 292\end{description} 293 294\subsection{Disjunctions} 295 296Disjunctions in the model should be avoided. Assume that a naive 297model would contain the following disjunction: 298\begin{code} 299% DO NOT USE THIS IN A MODEL 300no_overlap(S1,D1,S2,D2) :- S1 #>= S2 + D2. 301no_overlap(S1,D1,S2,D2) :- S2 #>= S1 + D1. 302\end{code} 303There are two basic ways of treating the disjunction: 304\begin{itemize} 305\item Deferring the choice until the search phase by introducing a 306 decision variable. 307\item Changing the behaviour of the disjunction so it becomes a constraint 308 (see also \ref{chapimpl} and \ref{chappropiachr}). 309\end{itemize} 310In the example, we can introduce a boolean variable \verb.B{0,1}. which represents 311the choice. 312The actual choice can be then be taken in search code by choosing a 313value for the variable. The model code must then be changed to observe 314the decision variable, either using the delay facility of \eclipse{}: 315\begin{code} 316delay no_overlap(S1,D1,S2,D2,B) if var(B). 317no_overlap(S1,D1,S2,D2,0) :- S1 #>= S2 + D2. 318no_overlap(S1,D1,S2,D2,1) :- S2 #>= S1 + D1. 319\end{code} 320or using an arithmetic encoding like in 321\begin{code} 322no_overlap(S1,D1,S2,D2,B) :- 323 B :: 0..1, 324 S1 + B*1000 #>= S2 + D2, 325 S2 + (1-B)*1000 #>= S1 + D1. 326\end{code} 327The alternative of turning the disjunction into a proper constraint is 328achieved most easily using {\em propia}'s infer-annotation 329(see \ref{chappropiachr}). The original formulation of neighbour/2 330is kept but it is used as follows: 331\begin{code} 332 ..., no_overlap(S1,D2,S2,D2) infers most, ... 333\end{code} 334 335 336\subsection{Conditionals} 337 338Similar considerations apply to conditionals where the condition is not 339decidable at constraint setup time. For example, suppose we want to 340impose a no-overlap constraint only if two tasks share the same resource. 341The following code is currently not safe in ECLiPSe: 342\begin{code} 343nos(Res1, Res2, Start1, Dur1, Start2, Dur2) :- 344 ( Res1 #= Res2 -> % WRONG!!! 345 no_overlap(Start1, Dur1, Start2, Dur2) 346 ; 347 true 348 ) 349\end{code} 350The reason is that (at constraint setup time) Res1 and Res2 will most 351likely be still uninstantiated. Therefore, the condition will in general 352delay (rather than succeed or fail), but the conditional construct 353will erroneously take this for a success and take the first alternative. 354 355Again, this can be handled using delay 356\begin{code} 357delay nos(Res1, Res2, _, _, _, _) if nonground([Res1,Res2]). 358nos(Res1, Res2, Start1, Dur1, Start2, Dur2) :- 359 ( Res1 == Res2 -> 360 no_overlap(Start1, Dur1, Start2, Dur2) 361 ; 362 true 363 ). 364\end{code} 365It might also be possible to compute a boolean variable indicating the 366truth of the condition. This is particularly easy when a reified 367constraint can be used to express the condition, like in this case: 368\begin{code} 369nos(Res1, Res2, Start1, Dur1, Start2, Dur2) :- 370 #=(Res1, Res2, Share), 371 cond_no_overlap(Start1, Dur1, Start2, Dur2, Share). 372 373delay cond_no_overlap(_,_,_,_,Share) if var(Share). 374cond_no_overlap(Start1, Dur1, Start2, Dur2, Share) :- 375 ( Share == 1 -> 376 no_overlap(Start1, Dur1, Start2, Dur2) 377 ; 378 true 379 ). 380\end{code} 381 382 383\section{Symmetries} 384 385Consider the following puzzle, where numbers from 1 to 19 have to be arranged 386in a hexagonal shape such that every diagonal sums up to 38: 387\begin{code} 388puzzle(Pattern) :- 389 Pattern = [ 390 A,B,C, 391 D,E,F,G, 392 H,I,J,K,L, 393 M,N,O,P, 394 Q,R,S 395 ], 396 Pattern :: 1 .. 19, 397 398 % Problem constraints 399 alldifferent(Pattern), 400 A+B+C #= 38, A+D+H #= 38, H+M+Q #= 38, 401 D+E+F+G #= 38, B+E+I+M #= 38, D+I+N+R #= 38, 402 H+I+J+K+L #= 38, C+F+J+N+Q #= 38, A+E+J+O+S #= 38, 403 M+N+O+P #= 38, G+K+O+R #= 38, B+F+K+P #= 38, 404 Q+R+S #= 38, L+P+S #= 38, C+G+L #= 38, 405 ... 406\end{code} 407In this formulation, the problem has 12 solutions, but it turns out they 408are just rotated and mirrored variants of each other. 409Removal of symmetries is still an area of active research, but a simple 410method is applicable in situations like this one. 411One can add constraints which require the solution to have certain 412additional properties, and so exclude many of the symmetric solutions: 413\begin{code} 414 ..., 415 % Optional anti-symmetry constraints 416 % Forbid rotated solutions: require A to be the smallest corner 417 A #< C, A #< H, A #< L, A #< S, A #< Q, 418 % Forbid solutions mirrored on the A-S diagonal 419 C #< H. 420\end{code} 421 422%HEVEA\cutend 423