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%HEVEA\cutdef[1]{section} 24 25\newcommand{\ignore}[1]{} 26 27\index{library!ic|(} 28 29\section{Introduction} 30 31The IC (Interval Constraint) library is a hybrid integer/real interval 32arithmetic constraint solver. Its aim is to make it convenient for 33programmers to write hybrid solutions to problems, mixing together integer 34and real constraints and variables. 35 36Previously, if one wished to mix integer and real constraints, one had to 37import separate solvers for each, with the solvers using different 38representations for the domains of variables. This meant any variable 39appearing in both kinds of constraints would end up with two domain 40representations, with extra constraints necessary to keep the two 41representations synchronised. 42 43 44\subsection{What IC does} 45\index{ic} 46 47IC is a general interval propagation solver which can be used to solve 48problems over both integer and real variables. Integer variables behave 49much like those from the old finite domain solver FD, while real variables 50behave much like those from the old real interval arithmetic solver RIA. 51IC allows both kinds of variables to be mixed seamlessly in constraints, 52since (with a few exceptions) the same propagation algorithms are used 53throughout and all variables have the same underlying representation 54(indeed, a real variable can be turned into an integer variable simply by 55imposing an integrality constraint on it). 56 57IC replaces the `fd', `ria' and `range' libraries. Since IC does not 58support symbolic domains, there is a separate symbolic solver library 59`ic_symbolic', to provide the non-numeric functionality of `fd'. 60 61 62\subsection{Differences between IC and FD} 63 64\begin{itemize} 65\item IC supports real variables and constraints; FD does not. 66 67\item FD supports symbolic domains; IC does not (use the ic_symbolic 68 library). 69 70\item In FD, numeric domains are more or less limited to 71 -10000000..10000000 (this default domain can be modified, but the 72 larger one makes it, the more likely one is to run into machine 73 integer overflow problems). In IC there is no limit as such, and 74 bounds on integer variables can be infinite (though variables should 75 not be assigned infinite values). However, since floating point 76 numbers are used in the underlying implementation, not every integer 77 value is representable. Specifically, integer variables and 78 constraints ought to behave as expected until the values being 79 manipulated become large enough that they approach the precision 80 limit of a double precision floating point number ($2^{51}$ or so). 81 Beyond this, lack of precision may mean that the solver cannot be 82 sure which integer is intended, in which case the solver starts 83 behaving more like an interval solver than a traditional finite 84 domain solver. Note however that this precision limit is way beyond 85 what is normally supported by finite domain solvers (typically 86 substantially less than $2^{32}$). Note also that deliberately 87 working with integer variables in this kind of range is not 88 particularly recommended; the main intention is for the computation 89 to be ``safe'' if it strays up into this region by ensuring no 90 overflow problems. 91 92\item IC usually requires that expressions constructed at runtime be 93 wrapped in {\bf eval/1} when they appear in constraints; otherwise 94 the variable representing the express may be assumed to be an IC 95 variable, resulting in a type error. See section~\ref{sec:eval} for 96 more details. We hope to remove this limitation in a future release. 97 98\item IC does not support the \bipref{\#$<$=/2}{../bips/lib/fd/HLE-2.html} 99 syntax for less-than-or-equal constraints. Use 100 \bipref{\#=$<$/2}{../bips/lib/ic/HEL-2.html} (the standard 101 \eclipse{} operator for integer less-than-or-equal constraints) 102 instead. Similarly, use 103 \biprefnoidx{\#\bsl=/2}{../bips/lib/ic/HRE-2.html} 104 \index{\#\bsl=/2} 105 %\latex{{\bf #\bsl=/2}}\html{\htmladdnormallink{\#\bsl=/2}{../bips/lib/ic/HRE-2.html}} 106 %\index{\#\verb+\+=/2} 107 instead of \bipref{\#\#/2}{../bips/lib/fd/HH-2.html}. 108 109\item The reified connectives provided by the two solvers are different: 110 FD's 111 \biprefnoidx{\#\bsl+/1}{../bips/lib/fd/HRP-1.html}, 112 \index{\#\bsl+/1} 113 \biprefnoidx{\#\andsy/2}{../bips/lib/fd/HFR-2.html}, 114 \index{\#\andsy/2} 115 \biprefnoidx{\#\orsy/2}{../bips/lib/fd/HRF-2.html}, 116 \index{\#\orsy/2} 117 \bipref{\#=$>$/2}{../bips/lib/fd/HEG-2.html} and 118 \bipref{\#$<$=$>$/2}{../bips/lib/fd/HLEG-2.html} 119 (and their reified versions) 120 correspond to IC's 121 \bipref{neg/1}{../bips/lib/ic/neg-1.html}, 122 \bipref{and/2}{../bips/lib/ic/and-2.html}, 123 \bipref{or/2}{../bips/lib/ic/or-2.html}, 124 \bipref{=$>$/2}{../bips/lib/ic/EG-2.html} and 125 \bipref{\#=/2}{../bips/lib/ic/HE-2.html} 126 (and their reified versions). 127 Note that IC has better reification support, in that any constraint 128 may be embedded in any other constraint expression, evaluating to 129 that constraint's reified value. 130 131\item The primitives for accessing and manipulating the domains of 132 variables are different; see the section on variable query 133 predicates (section~\ref{domain-query}) 134 for details of IC's support for this. 135 136\end{itemize} 137 138 139\subsection{Differences between IC and RIA} 140 141The main difference between IC's interval solving and RIA's is that IC is 142aware of and utilises the bounded real numeric type. 143This means bounded reals may appear in IC constraints, and IC variables may 144be unified with bounded reals (though direct unification is not recommended: 145it is preferable to use an equality constraint to do the assignment). 146In contrast, RIA will fail with a type error if bounded reals are used in 147either of these cases. 148 149 150\subsection{Notes about interval arithmetic} 151 152The main problem with using floating point arithmetic instead of real 153arithmetic for doing any kind of numerical computation or constraint solving 154is that it is only approximate. Finite precision means a floating point 155value may only approximate the intended real; it also means there may be 156rounding errors when doing any computation. Worse is that one does not know 157from looking at an answer how much error has crept into the computation; it 158may be that the result one obtains is very close to the true solution, or it 159may be that the errors have accumulated to the point where they are 160significant. This means it can be hard to know whether or not the answer 161one obtains is actually a solution (it may have been unintentionally 162included due to errors), or worse, whether or not answers have been missed 163(unintentionally excluded due to errors). 164 165Interval arithmetic is one way to manage the error problem. Essentially 166each real number is represented by a pair of floating point bounds. The 167true value of the number may not be known, but it is definitely known to lie 168between the two bounds. Any arithmetic operation to be performed is then 169done using these bounds, with the resulting interval widened to take into 170account any possible error in the operation, thus ensuring that the resulting 171interval contains the true answer. This is the principle behind the bounded 172real arithmetic type. 173 174Note that interval arithmetic does not guarantee small errors, it just 175provides a way of knowing how large the error may have become. 176 177One drawback of the interval approach is that arithmetic comparisons can no 178longer always be answered with a simple ``yes'' or ``no''; sometimes the 179only possible answer is ``don't know''. This is reflected in the behaviour 180of arithmetic comparators (=:=, >=, etc.) when applied to bounded reals 181which overlap each other. In such a case, one cannot know whether the true 182value of one is greater than, less than, or equal to the other, and so a 183delayed goal is left behind. This delayed goal indicates that the 184computation succeeded, contingent on whether the condition in the delayed 185goal is true. For example, if the delayed goal left behind was 186\verb+0.2__0.4 >= 0.1__0.3+, this indicates that the computation should be 187considered a success only if the true value represented by the bounded real 188on the left is greater than or equal to that of the bounded real on the 189right. If the width of the intervals in any such delayed goals is 190non-trivial, then this indicates a problem with numerical accuracy. It is 191up to the user to decide how large an error is tolerable for any given 192application. 193 194 195\subsection{Interval arithmetic and IC} 196 197In order to ensure the soundness of the results it produces, the IC 198solver does almost all computation using interval arithmetic. As part 199of this, the first thing done to a constraint when it is given to the 200solver is to convert all non-integer numbers in it to bounded reals. 201Note that for this conversion, floating point numbers are assumed to 202refer to the closest representable float value, as per the type 203conversion predicate 204\bipref{breal/2}{../bips/kernel/arithmetic/breal-2.html}. 205This lack of widening when converting floating point numbers to 206bounded reals is fine if the floating point number is exactly 207the intended real number, but if there is any uncertainty, that 208uncertainty should be encoded by using a bounded real in the 209constraint instead of a float. 210 211One of the drawbacks of this approach is that the user is not 212protected from the fundamental inaccuracies that may occur when trying 213to represent decimal numbers with floating point values in binary. 214The user should be aware therefore that some numbers given explicitly 215as part of their program may \emph{not} be safely represented as a 216bounded real that spans the exact decimal value. e.g.\ 217\verb|X $= 0.1| or equivalently \verb|X is breal(0.1)|. 218 219This may lead to unexpected results such as 220\begin{verbatim} 221[eclipse 2]: X $= 0.1, Y $= 0.09999999999999999, X $> Y. 222 223X = 0.1__0.1 224Y = 0.099999999999999992__0.099999999999999992 225Yes (0.00s cpu) 226 227[eclipse 3]: X $= 0.1, Y $= 0.099999999999999999, X $> Y. 228 229No (0.00s cpu) 230\end{verbatim} 231 232This potential source of confusion arises only with values which are 233explicitly given within a program. By replacing the assignment to Y 234with an expression which evaluates to the same real value we get 235 236\begin{verbatim} 237[eclipse 4]: X $= 0.1, Y $= 0.1 - 0.000000000000000001, X $> Y. 238 239X = 0.1__0.1 240Y = 0.099999999999999992__0.1 241 242 243Delayed goals: 244 ic : (0 > -1.3877787807814457e-17__-0.0) 245Yes (0.00s cpu) 246\end{verbatim} 247 248Note the delayed goal indicating the conditions under which the original goal should be considered to have succeeded. 249 250 251\subsection{Usage} 252 253To load the IC library into your program, simply add the following directive 254at an appropriate point in your code. 255 256\begin{quote} 257\begin{verbatim} 258:- lib(ic). 259\end{verbatim} 260\end{quote} 261 262\subsection{Arithmetic Expressions} 263 264The IC library solves constraint problems over the reals. It is not 265limited to linear constraints, so it can be used to solve general problems 266like: 267\begin{verbatim} 268[eclipse 2]: ln(X) $>= sin(X). 269 270X = X{0.36787944117144228 .. 1.0Inf} 271 272 273Delayed goals: 274... 275Yes (0.01s cpu) 276\end{verbatim} 277The IC library treats linear and non-linear constraints 278differently. Linear constraints are handled by a single propagator, 279whereas non-linear constraints are broken down into simpler 280ternary/binary/unary propagators. 281 282Any relational constraint (\verb|$=|, \verb|$>=|, \verb|#=|, etc.) 283can be reified simply by including it in an expression where it will 284evaluate to its reified truth value. 285 286User-defined constraints may also be included in constraint expressions 287where they will be treated in a similar manner to user defined 288functions found in expressions handled by \verb|is/2|. That is to say 289they will be called at run-time with an extra argument to collect the 290result. Note, however, that user defined constraint/functions, when used 291in IC, should be deterministic. User defined constraints/functions which 292leave choice points may not behave as expected. 293 294Variables appearing in arithmetic IC constraints at compile-time are 295assumed to be IC variables unless they are wrapped in an {\bf eval/1} 296term. See section~\ref{sec:eval} for an more detailed explanation of 297usage. 298 299The following arithmetic expression can be used inside the constraints: 300\begin{description} 301\item[{\texttt X}] 302 \emph{Variables}. If \verb|X| is not yet a interval variable, 303 it is turned into one by implicitly constraining it to be a real 304 variable. 305 306\item[\texttt{123}] 307 Integer constants. They are assumed to be exact and are used 308 as is. 309 310\item[\texttt{0.1}] 311 Floating point constants. These are assumed to be exact and 312 are converted to a zero width bounded reals. 313 314\item[\texttt{pi, e}] 315 Intervals enclosing the constants $\pi$ and $e$ respectively. 316 317\item[\texttt{inf}] 318 Floating point infinity. 319 320\item[\texttt{+Expr}] 321 Identity. 322 323\item[\texttt{-Expr}] 324 Sign change. 325 326\item[\texttt{+-Expr}] 327 \verb|Expr| or \verb|-Expr|. The result is an interval 328 enclosing both. If however, either bound is infeasible then 329 the result is the bound that is feasible. If neither bound is 330 feasible, the goal fails. 331 332\item[\texttt{abs(Expr)}] 333 The absolute value of Expr. 334 335\item[\texttt{E1+E2}] 336 Addition. 337 338\item[\texttt{E1-E2}] 339 Subtraction. 340 341\item[\texttt{E1*E2}] 342 Multiplication. 343 344\item[\texttt{E1/E2}] 345 Division. 346 347\item[\texttt{E1}\textasciicircum{}\texttt{E2}] 348 Exponentiation. 349 350\item[\texttt{min(E1,E2)}] 351 Minimum. 352 353\item[\texttt{max(E1,E2)}] 354 Maximum. 355 356\item[\texttt{sqr(Expr)}] 357 Square. Logically equivalent to \verb|Expr*Expr|, but with better 358 operational behaviour. 359 360\item[\texttt{sqrt(Expr)}] 361 Square root (always positive). 362 363\item[\texttt{exp(Expr)}] 364 Same as \verb|e^Expr|. 365 366\item[\texttt{ln(Expr)}] 367 Natural logarithm, the reverse of the exp function. 368 369\item[\texttt{sin(Expr)}] 370 Sine. 371 372\item[\texttt{cos(Expr)}] 373 Cosine. 374 375\item[\texttt{atan(Expr)}] 376 Arcus tangens. (Returns value between -pi/2 and pi/2.) 377 378\item[\texttt{rsqr(Expr)}] 379 Reverse of the sqr function. Equivalent to \verb|+-sqrt(Expr)|. 380 381\item[\texttt{rpow(E1,E2)}] 382 Reverse of exponentiation. i.e.\ finds \verb|X| in \verb|E1 = X^E2|. 383 384\item[\texttt{sub(Expr)}] 385 A subinterval of Expr. 386 387\item[\texttt{sum(ExprList)}] 388 Sum of a list of expressions. 389 390\item[\texttt{min(ExprList)}] 391 Minimum of a list of expressions. 392 393\item[\texttt{max(ExprList)}] 394 Maximum of a list of expressions. 395 396\item[\texttt{and}] 397 Reified constraint conjunction. e.g. \verb|B #= (X$>3 and X$<8)| 398 399\item[\texttt{or}] 400 Reified constraint disjunction. e.g. \verb|B #= (X$>3 or X$<8) | 401 402\item[\texttt{=>}] 403 Reified constraint implication. e.g. \verb|B #= (X$>3 => X$<8) | 404 405\item[\texttt{neg}] 406 Reified constraint negation. e.g. \verb|B #= (neg X$>3) | 407 408\item[\texttt{\$>}, \texttt{\$>=}, \texttt{\$=}, \texttt{\$=<}, \texttt{\$<}, \texttt{\$\bsl=}, 409 \texttt{\#>}, \texttt{\#>=}, \texttt{\#=}, \texttt{\#=<}, \texttt{\#<}, 410 \texttt{\#\bsl=}, 411 \texttt{>}, \texttt{>=}, \texttt{=:=}, \texttt{=<}, \texttt{<}, \texttt{=\bsl=}, 412 \texttt{and}, \texttt{or}, \texttt{=>}, \texttt{neg}] 413 Any arithmetic or logical constraint that can be issued as a 414 goal may also appear within an expression. 415 416 Within the expression context, the constraint evaluates to its 417 reified truth value. If the constraint is entailed by the 418 state of the constraint store then the (sub-)expression 419 evaluates to \verb|1|. If it is dis-entailed by the state of 420 the constraint store then it evaluates to \verb|0|. If its 421 reified status is unknown then it evaluates to an integral 422 variable \verb|0..1|. 423 424 Note: The simple cases (e.g.\ \verb|Bool #= (X #> 5)|) are 425 equivalent to directly calling the reified forms of the basic 426 constraints (e.g.\ \verb|#>(X, 5, Bool)|). 427 428\item[\texttt{foo(Arg1, Arg2 ... ArgN), module:foo(Arg1, Arg2 ... ArgN)}] 429 Any terms found in the expression whose principle functor is 430 not listed above will be replaced in the expression by a newly 431 created auxiliary variable. This same variable will be 432 appended to the term as an extra argument, and then the term 433 will be called as \verb|call(foo(Arg1, Arg2 ... ArgN, Aux))|. 434 If no lookup module is specified, then the current 435 module will be used. 436 437 This behaviour mimics that of 438 \bipref{is/2}{../bips/kernel/arithmetic/is-2.html}. 439 440\item[\texttt{eval(Expr)}] 441 See section~\ref{sec:eval} for an explanation of {\bf eval/1} usage. 442\end{description} 443 444\subsubsection{{\bf eval}} 445\label{sec:eval} 446The {\bf eval/1} wrapper inside arithmetic constraints is used to 447indicate that a variable will be bound to an expression at run-time. 448This feature will only be used by programs which generate their 449constraints dynamically at run-time, for example. 450\begin{verbatim} 451broken_sum(Xs,Sum):- 452 ( 453 foreach(X,Xs), 454 fromto(Expr,S1,S2,0) 455 do 456 S1 = X + S2 457 ), 458 Sum $= Expr. 459\end{verbatim} 460The above implementation of a summation constraint will not work as 461intended because the variable \texttt{Expr} will be treated like an IC 462variable when it is in fact the term \texttt{+(X1,+(X2,+(...)))} 463which is constructed in the for-loop. 464In order to get the desired functionality, one must wrap the variable 465\texttt{Expr} in an {\bf eval/1}. 466\begin{verbatim} 467working_sum(Xs,Sum):- 468 ( 469 foreach(X,Xs), 470 fromto(Expr,S1,S2,0) 471 do 472 S1 = X + S2 473 ), 474 Sum $= eval(Expr). 475\end{verbatim} 476 477\section{Library Predicates} 478 479\subsection{Domain constraints} 480 481\begin{description} 482 483\item [\biptxtrefni{Vars :: Domain}{::/2!ic}{../bips/lib/ic/NN-2.html}] 484\index{::/2@\texttt{::/2}!ic} 485Constrains Vars to take only integer or real values from the domain 486specified by Domain. Vars may be a variable or a collection of variables 487(\`a la 488\bipref{collection_to_list/2}{../bips/lib/lists/collection_to_list-2.html}). 489Domain can be specified as a simple range Lo .. Hi, or as a list 490of subranges and/or individual elements. Multiple subranges and/or 491individual elements are allowed in integer domains only. If all subrange 492bounds and individual elements are integers the domain is considered an 493integer domain and the variables Vars are constrained to be integral; 494otherwise it is considered a real domain and the type of the variables is 495not constrained. Also allowed are the (untyped) symbolic bound values {\tt 496inf}, {\tt +inf} and {\tt -inf}. 497 498\item [\biptxtrefni{::(Var,Domain,Bool)}{::/3!ic}{../bips/lib/ic/NN-3.html}] 499\index{::/2@\texttt{::/3}!ic} 500 501Provides a reified form of the {\tt ::/2} domain assignment predicate. This 502reified {\tt ::/3} is defined only to work for one variable and only integer 503variables (unlike {\tt ::/2}), hence only the Domain formats suitable for 504integers may be supplied to this predicate. 505 506For a single variable, V, the Bool will be instantiated to 0 if the 507current domain of V does not intersect with Domain. It will be 508instantiated to 1 iff the domain of V is wholly contained within 509Domain. Finally the Boolean will remain an integer variable in the 510range {\tt 0..1} if neither of the above two conditions hold. 511 512Instantiating Bool to 1, will cause the constraint to behave exactly 513like {\tt ::/2}. Instantiating Bool to 0 will cause Domain to be 514excluded from the domain of all the variables in Vars where such an 515exclusion is representable. If such an integer domain is 516unrepresentable (e.g.\ {\tt -1.0Inf .. -5, 5..1.0Inf}), then a delayed 517goal will be setup to exclude values when the bounds become 518sufficiently narrow. 519 520Note that calling the reified form of {\tt ::} will result in the 521Variable becoming constrained to be integral, even if Bool is 522uninstantiated. 523 524Further note that, like other reified predicates, {\tt ::} can be used 525infix in an IC expression e.g.\ {\tt B \#= (X :: [1..10])} is equivalent 526to {\tt ::(X, [1..10], B)}. See section~\ref{sec:reified-constraints} 527for more information of reified constraints. 528 529\item [\biptxtrefni{Vars \#:: Domain}{\#::/2!ic}{../bips/lib/ic/HNN-2.html}] 530\index{\#::/2@\texttt{\#::/2}!ic} 531Constrains Vars to take only integer values from the domain 532specified by Domain. 533Vars may be a variable or a collection of variables (\`a la 534\bipref{collection_to_list/2}{../bips/lib/lists/collection_to_list-2.html}). 535Domain can be specified as a simple range Lo .. Hi, or as a list 536of subranges and/or individual elements (integer variables only). Also 537allowed are the (untyped) symbolic bound values {\tt inf}, {\tt +inf} and 538{\tt -inf}. 539 540\item [\biptxtrefni{Vars \$:: Domain}{\$::/2!ic}{../bips/lib/ic/SNN-2.html}] 541\index{\$::/2@\texttt{\$::/2}!ic} 542Constrains Vars to take real values from the domain specified by 543Domain. 544Vars may be a variable or a collection of variables (\`a la 545\bipref{collection_to_list/2}{../bips/lib/lists/collection_to_list-2.html}). 546Domain must represent one contiguous interval. 547 548\item [\biptxtrefni{reals(Vars)}{reals/1!ic}{../bips/lib/ic/reals-1.html}] 549\index{reals/1@\texttt{reals/1}!ic} 550Declares that the given variables are IC variables. 551 552\item [\biptxtrefni{integers(Vars)}{integers/1!ic}{../bips/lib/ic/integers-1.html}] 553\index{integers/1@\texttt{integers/1}!ic} 554Constrains the given variables to take integer values only. 555 556\end{description} 557 558 559\subsection{Arithmetic constraints} 560 561Note that the integer forms of the constraints are essentially the same as 562the general forms, except that they check that all constants are integers 563and generally constrain all variables \emph{and subexpressions} to be 564integral. 565Thus with integer constraints, the solver does very much behave like a 566traditional integer solver, with any temporary variables and intermediate 567results assumed to be integral. 568This means that it makes little sense to use many of the nonlinear functions 569available for use in expressions (e.g.\ sin, cos, ln, exp) in integer 570constraints. 571It also means that one should take care using such things as division: 572\bipnoidx{X/2 + Y/2 \#= 1} and \bipnoidx{X + Y \#= 2} are different 573constraints, with the former constraining X and Y to be even. 574That said, if all the constants and variables are integral already and the 575subexpressions clearly so as a consequence, then the integer (\#) constraints 576and general (\$) constraints may be used integerchangeably. 577 578\begin{description} 579 580\item [\biptxtrefni{ExprX \$= ExprY}{\$=/2!ic}{../bips/lib/ic/SE-2.html}, 581 \biptxtrefni{ic:(ExprX =:= ExprY)}{=:=/2!ic}{../bips/lib/ic/ENE-2.html}] 582\index{\$=/2@\texttt{\$=/2}!ic}\index{\$=:=/2@\texttt{\$=:=/2}!ic} 583ExprX is equal to ExprY. ExprX and ExprY are general expressions. 584 585\item [\biptxtrefni{ExprX \$>= ExprY}{\$$>$=/2!ic}{../bips/lib/ic/SGE-2.html}, 586 \biptxtrefni{ic:(ExprX $>$= ExprY)}{$>$=/2!ic}{../bips/lib/ic/GE-2.html}] 587\index{\$>=/2@\texttt{\$>=/2}!ic} 588ExprX is greater than or equal to ExprY. ExprX and ExprY are general 589expressions. 590 591\item [\biptxtrefni{ExprX \$=< ExprY}{\$=$<$/2!ic}{../bips/lib/ic/SEL-2.html}, 592 \biptxtrefni{ic:(ExprX =$<$ ExprY)}{=$<$/2!ic}{../bips/lib/ic/EL-2.html}] 593\index{\$=</2@\texttt{\$=</2}!ic} 594ExprX is less than or equal to ExprY. ExprX and ExprY are general expressions. 595 596\item [\biptxtrefni{ExprX \$$>$ ExprY}{\$$>$/2!ic}{../bips/lib/ic/SG-2.html}, 597 \biptxtrefni{ic:(ExprX $>$ ExprY)}{$>$/2!ic}{../bips/lib/ic/G-2.html}] 598\index{\$>/2@\texttt{\$>/2}!ic} 599ExprX is strictly greater than ExprY. ExprX and ExprY are general 600expressions. 601 602\item [\biptxtrefni{ExprX \$$<$ ExprY)}{\$$<$/2!ic}{../bips/lib/ic/SL-2.html}, 603 \biptxtrefni{ic:(ExprX $<$ ExprY)}{$<$/2!ic}{../bips/lib/ic/L-2.html}] 604\index{\$</2@\texttt{\$=/2}!ic} 605ExprX is strictly less than ExprY. ExprX and ExprY are general expressions. 606 607\item [\biprefnoidx{ExprX \$\bsl= ExprY}{../bips/lib/ic/SRE-2.html}, 608 \biprefnoidx{ic:(ExprX =\bsl= ExprY)}{../bips/lib/ic/ERE-2.html}] 609\index{\$\bsl=/2!ic} 610\index{=\bsl=/2!ic} 611ExprX is not equal to ExprY. ExprX and ExprY are general expressions. 612 613\item [\biptxtrefni{ExprX \#= ExprY}{\#=/2!ic}{../bips/lib/ic/HE-2.html}] 614\index{\#=/2@\texttt{\#=/2}!ic} 615ExprX is equal to ExprY. ExprX and ExprY are constrained to be integer 616expressions. 617 618\item [\biptxtrefni{ExprX \#$>$= ExprY}{\#$>$=/2!ic}{../bips/lib/ic/HGE-2.html}] 619\index{\$>=/2@\texttt{\$>=/2}!ic} 620ExprX is greater than or equal to ExprY. ExprX and ExprY are constrained to 621be integer expressions. 622 623\item [\biptxtrefni{ExprX \#=$<$ ExprY}{\#=$<$/2!ic}{../bips/lib/ic/HEL-2.html}] 624\index{\$=</2@\texttt{\$=</2}!ic} 625ExprX is less than or equal to ExprY. ExprX and ExprY are constrained to be 626integer expressions. 627 628\item [\biptxtrefni{ExprX \#$>$ ExprY}{\#$>$/2!ic}{../bips/lib/ic/HG-2.html}] 629\index{\$>/2@\texttt{\$>/2}!ic} 630ExprX is greater than ExprY. ExprX and ExprY are constrained to be integer 631expressions. 632 633\item [\biptxtrefni{ExprX \#$<$ ExprY}{\#$<$/2!ic}{../bips/lib/ic/HL-2.html}] 634\index{\$</2@\texttt{\$</2}!ic} 635ExprX is less than ExprY. ExprX and ExprY are constrained to be integer 636expressions. 637 638\item [\biprefnoidx{ExprX \#\bsl= ExprY}{../bips/lib/ic/HRE-2.html}] 639\index{\#\bsl=/2!ic} 640ExprX is not equal to ExprY. ExprX and ExprY are constrained to be integer 641expressions. 642 643\item [\biptxtref{ac_eq(X, Y, C)}{ac_eq/3}{../bips/lib/ic/ac_eq-3.html}] 644Arc-consistent implementation of \bipnoidx{X \#= Y + C}. X and Y are 645constrained to be integer variables and to have ``reasonable'' bounds. C 646must be an integer. 647 648\end{description} 649 650The comparison constraints \verb'=:=/2', \verb'>=/2', \verb'=</2' and 651\verb'=\=/2' have the same syntax as the standard \eclipse\ built-in 652comparison operators (and those of other constraint solvers). 653Unless explicitly qualified, the \eclipse\ built-ins are used. 654To use these constraints without the need to qualify them, use the 655alternative dollar-syntax. 656%This choice of default can be changed 657%by explicitly importing the desired version before its first use: 658% 659%\begin{verbatim} 660%:- import (=:=) / 2, (>=) / 2, (=<) / 2, (=\=) / 2 from ic. 661% 662%foo(X, Y) :- 663% X >= Y. % this will use ic's >=/2 664%\end{verbatim} 665% 666%In any event, the default can be overridden on any given call by explicitly 667%qualifying which is desired, for example \verb+ic:(A =:= B)+ or 668%\verb+eclipse_language:(A =:= B)+. 669 670 671\subsection{Reified constraints} 672\label{sec:reified-constraints} 673As mentioned earlier, when constraints appear in an expression 674context, then they evaluate to their reified truth value. Practically 675this means that the constraints are posted in a passive {\em check but 676do not propagate} mode, whereby no variable domains are modified but 677checks are made to see if the constraint has become entailed 678(necessarily true) or dis-entailed (necessarily false). 679 680The simplest and arguably most natural way to reify a constraint is to 681place it in an expression context (i.e.\ on either side of a \verb|$=|, 682\verb|$>=|, \verb|#=|, etc.) and assign its truth value to a variable. 683For example: 684 685\begin{verbatim} 686TruthValue #= (X $> 4). 687\end{verbatim} 688 689It is also possible to use the 3 argument form of the constraint predicates 690where the third argument is the reified truth value, for example: 691 692\begin{verbatim} 693$>(X, 4, TruthValue). 694\end{verbatim} 695 696But in general the previous form is recommended as it can be easily 697extended to handle the truth values of a combination of constraints, by 698using the infix operators \verb|and| (logical conjunction), \verb|or| 699(logical disjunction) and \verb|=>| (logical implication) or the prefix 700operator \verb|neg| (logical negation). e.g.: 701 702\begin{verbatim} 703TruthValue #= (X $> 4 and Y $< 6). 704\end{verbatim} 705 706Again, as mentioned earlier, there are a number of reified connectives 707which can be used to combine reified constraints using logical 708operations on their truth values. 709\begin{description} 710\item[\bipref{and/2}{../bips/lib/ic/and-2.html}] Reified constraint conjunction. e.g. 711\verb|B #= (X $> 3 and X $< 8)| or 712\verb|X $> 3 and X $< 8| 713 714\item[\bipref{or/2}{../bips/lib/ic/or-2.html}] Reified constraint disjunction. e.g. 715\verb|B #= (X $> 3 or X $< 8)| or 716\verb|X $> 3 or X $< 8| 717 718\item[\bipref{=$>$/2}{../bips/lib/ic/EG-2.html}] Reified constraint implication. e.g. 719\verb|B #= (X $> 3 => X $< 8)| or 720\verb|X $> 3 => X $< 8| 721 722\item[\bipref{neg/1}{../bips/lib/ic/neg-1.html}] Reified constraint negation. e.g. 723\verb|B #= (neg X $> 3)| or 724\verb|neg X $> 3| 725\end{description} 726 727 728 729 730\subsubsection{Enforcing constraints} 731The logical truth value of a constraint, when reified, can be used to 732enforce the constraint (or its negation) during search. 733 734The following three examples are equivalent: 735 736\begin{verbatim} 737X $> 4. 738\end{verbatim} 739 740\begin{verbatim} 741B #= (X $> 4), B=1. 742\end{verbatim} 743 744\begin{verbatim} 745B #= (X $=< 4), B=0. 746\end{verbatim} 747 748By unifying the value of the reified truth value, the constraint 749changes from being {\em passive} to being {\em active}. Once actively 750true (or actively false) the constraint will prune domains as though 751it had been posted as a simple non-reified constraint. 752 753\subsubsection{User-defined reified constraints} 754Reified constraints are implemented using the the 3 argument form of 755the constraint predicate if it exists (and it does exist for the 756arithmetic relation constraints). 757 758User-defined constraints will be treated as reifiable if they appear in an 759expression context and as such should provide forms where the last argument 760is the reified truth value reflected into a variable. 761 762The user-defined constraint should behave as follows depending on the 763state of the reified variable. 764 765\paragraph{Reified variable is unbound} When the reified variable is 766unbound, the constraint should not perform any domain reduction on its 767arguments, but should check to see if the constraint has become entailed or 768dis-entailed, setting the reified variable to \verb|1| or \verb|0| 769respectively. 770 771\paragraph{Reified variable is bound to 0} In the event that the reified 772variable becomes bound to \verb|0| then the constraint should actively 773propagate its negation. 774 775\paragraph{Reified variable is bound to 1} In the event that the reified 776variable becomes bound to \verb|1| then the constraint should actively 777propagate its normal semantics. 778 779\subsection{Miscellaneous constraints} 780 781\begin{description} 782 783\item 784[\biptxtrefni{alldifferent(Vars)}{alldifferent/1!ic}{../bips/lib/ic/alldifferent-1.html}] 785\index{alldifferent/1@\texttt{alldifferent/1}!ic} 786Constrains all elements of a list to be different from all other 787elements of the list. 788 789\item 790[\biptxtrefni{element(Index, List, Value)}{element/3!ic}{../bips/lib/ic/element-3.html}] 791\index{element/3@\texttt{element/3}!ic} 792Constrains Value to be the Index'th element of the list of integers List. 793 794\end{description} 795 796 797\subsection{Integer labeling predicates} 798 799These predicates can be used to enumerate solutions to a set of constraints 800over integer variables. For optimisation, see also the 801\bipref{branch_and_bound}{../bips/lib/branch_and_bound/index.html} library. 802 803\begin{description} 804 805\item [\biptxtrefni{indomain(Var)}{indomain/1!ic}{../bips/lib/ic/indomain-1.html}] 806\index{indomain/1@\texttt{indomain/1}!ic} 807Instantiates an integer IC variable to an element of its domain. 808 809\item [\biptxtrefni{labeling(Vars)}{labeling/1!ic}{../bips/lib/ic/labeling-1.html}] 810\index{labeling/1@\texttt{labeling/1}!ic} 811Instantiates all IC variables in a list to elements of their domains. 812 813\item [\biptxtrefni{indomain(Var,Choice)}{indomain/2!ic}{../bips/lib/ic/indomain-2.html}] 814A more flexible way to assign values to an integer IC variable. 815\item [\biptxtrefni{delete(V, Vars, Rest, Arg, Select)}{delete/5!ic}{../bips/lib/ic/delete-5.html}] 816A flexible way to select an integer IC variable from a list of variables Vars 817for labeling. 818\item [\biptxtrefni{search(Vars, Arg, Select, Choice, Method, Options)}{search/6!ic}{../bips/lib/ic/search-6.html}] 819\index{search/6@\texttt{search/6}!ic} 820Instantiates the variables Vars by performing a search based on the 821parameters provided by the user. 822 823\end{description} 824 825 826\subsection{Real domain refinement predicates} 827 828These predicates can be used to locate real solutions to a set of 829constraints. They are essentially the same as those that were available in 830RIA; more details of the algorithms used can be found in 831section~\ref{sec:real-solving}. 832 833\begin{description} 834 835\item [\biptxtrefni{locate(Vars, Precision)}{locate/2!ic}{../bips/lib/ic/locate-2.html}] 836\index{locate/2@\texttt{locate/2}!ic} 837Locate solution intervals for Vars by splitting and search. Precision 838indicates how accurate the intervals have to be (in absolute or relative 839terms) before splitting stops. 840 841\item [\biptxtrefni{locate(Vars, Precision, LinLog)}{locate/3!ic}{../bips/lib/ic/locate-3.html}] 842\index{locate/3@\texttt{locate/3}!ic} 843As per locate/2, but LinLog specifies wither linear (\texttt{lin}) or 844logarithmic (\texttt{log}) splitting should be used. (\bipref{locate/2}{../bips/lib/ic/locate-2.html} is 845equivalent to calling \bipref{locate/3}{../bips/lib/ic/locate-3.html} with \texttt{log} as the third 846argument.) 847 848\item [\biptxtrefni{locate(LocateVars, SquashVars, Precision, LinLog)}{locate/4!ic}{../bips/lib/ic/locate-4.html}] 849\index{locate/4@\texttt{locate/4}!ic} 850As per \bipref{locate/3}{../bips/lib/ic/locate-3.html}, but also applies the squashing algorithm to 851SquashVars both before splitting commences, and then again after each split. 852 853\item [\biptxtrefni{squash(Vars, Precision, LinLog)}{squash/3!ic}{../bips/lib/ic/squash-3.html}] 854\index{squash/3@\texttt{squash/3}!ic} 855Refine the intervals of Vars by the squashing algorithm. 856 857\end{description} 858 859 860\subsection{Variable query predicates} 861\label{domain-query} 862 863These predicates allow one to retrieve various properties of an IC variable 864(and usually work on ground numbers as well). 865 866\begin{description} 867 868\item [\biptxtref{is_solver_var(Var)}{is_solver_var/1}{../bips/lib/ic/is_solver_var-1.html}] 869Succeeds if an only if Var is an IC variable. 870 871\item [\biptxtref{is_solver_type(Term)}{is_solver_type/1}{../bips/lib/ic/is_solver_type-1.html}] 872Succeeds if an only if Term is an IC variable or a number. 873 874\item [\biptxtref{get_solver_type(Var, Type)}{get_solver_type/2}{../bips/lib/ic/get_solver_type-2.html}] 875Returns whether Var is an integer variable or a real variable. 876 877\item [\biptxtref{get_bounds(Var, Lo, Hi)}{get_bounds/3}{../bips/lib/ic/get_bounds-3.html}] 878Returns the current bounds of Var. 879 880\item [\biptxtref{get_min(Var, Lo)}{get_min/2}{../bips/lib/ic/get_min-2.html}] 881Returns the current lower bound of Var. 882 883\item [\biptxtref{get_max(Var, Hi)}{get_max/2}{../bips/lib/ic/get_max-2.html}] 884Returns the current upper bound of Var. 885 886\item [\biptxtref{get_float_bounds(Var, Lo, Hi)}{get_float_bounds/3}{../bips/lib/ic/get_float_bounds-3.html}] 887Returns the current bounds of Var as floats. 888 889\item [\biptxtref{get_integer_bounds(Var, Lo, Hi)}{get_integer_bounds/3}{../bips/lib/ic/get_integer_bounds-3.html}] 890Returns the current bounds of the integer variable Var (infinite bounds are 891returned as floats). Constrains Var to be integral if it isn't already. 892 893\item [\biptxtref{get_finite_integer_bounds(Var, Lo, Hi)}{get_finite_integer_bounds/3}{../bips/lib/ic/get_finite_integer_bounds-3.html}] 894Returns the current (finite) bounds of the integer variable Var. Constrains 895Var to be finite and integral if it isn't already. 896 897\item [\biptxtref{get_domain_size(Var, Size)}{get_domain_size/2}{../bips/lib/ic/get_domain_size-2.html}] 898Returns the number of elements in the IC domain for Var. Currently Var 899needs to be of type integer. 900 901\item [\biptxtref{get_domain(Var, Domain)}{get_domain/2}{../bips/lib/ic/get_domain-2.html}] 902Returns a ground representation of the current IC domain for Var. 903 904\item [\biptxtref{get_domain_as_list(Var, Domain)}{get_domain_as_list/2}{../bips/lib/ic/get_domain_as_list-2.html}] 905Returns a list of all the elements in the IC domain for Var. Currently Var 906needs to be of type integer. 907 908\item [\biptxtrefni{get_median(Var, Median)}{get_median/2!ic}{../bips/lib/ic/get_median-2.html}] 909\index{get_median/2@\texttt{get_median/2}!ic} 910Returns the median of the interval of Var. 911 912\item [\biptxtrefni{get_delta(Var, Delta)}{get_delta/2!ic}{../bips/lib/ic/get_delta-2.html}] 913\index{get_delta/2@\texttt{get_delta/2}!ic} 914Returns the width of the interval of Var. 915 916\item [\biptxtrefni{is_in_domain(Var, Value)}{is_in_domain/2!ic}{../bips/lib/ic/is_in_domain-2.html}] 917\index{is_in_domain/2@\texttt{is_in_domain/2}!ic} 918Succeeds if and only if Value is contained in the current domain of Var. 919 920\item [\biptxtrefni{is_in_domain(Var, Value, Result)}{is_in_domain/3!ic}{../bips/lib/ic/is_in_domain-3.html}] 921\index{is_in_domain/3@\texttt{is_in_domain/3}!ic} 922Binds Result to 'yes', 'no' or 'maybe' depending on whether Value is 923in the current domain of Var. 924 925\item [\biptxtref{delayed_goals_number(Var, Number)}{delayed_goals_number/2}{../bips/lib/ic/delayed_goals_number-2.html}] 926Returns the number of delayed goals suspended on the IC attribute. This 927approximates the number of IC constraints that Var is involved in. 928 929\end{description} 930 931 932\subsection{Propagation threshold predicates} 933\label{sec:propagation-threshold} 934 935With interval constraint propagation, it is sometimes useful to limit 936propagation for efficiency reasons. In IC, this is controlled by the 937propagation threshold. The way it works is that for non-integer variables, 938bounds are only changed if the absolute and relative changes of the bound 939exceed this threshold (i.e.\ small changes are suppressed). This means that 940constraints over real variables are only guaranteed to be consistent up to 941the current threshold (over and above any normal widening which occurs). 942 943Note that a higher threshold speeds up computations, but reduces precision 944and may in the extreme case prevent the system from being able to locate 945individual solutions. 946 947The default threshold is 1e-8. 948 949\begin{description} 950 951\item [\biptxtrefni{get_threshold(Threshold)}{get_threshold/1!ic}{../bips/lib/ic/get_threshold-1.html}] 952\index{get_threshold/1@\texttt{get_threshold/1}!ic} 953Returns the current propagation threshold. 954 955\item [\biptxtrefni{set_threshold(Threshold)}{set_threshold/1!ic}{../bips/lib/ic/set_threshold-1.html}] 956\index{set_threshold/1@\texttt{set_threshold/1}!ic} 957Sets the propagation threshold. Note that if the threshold is reduced using 958this predicate (requiring a higher level of precision), the current state of 959the system may not be consistent with respect to the new threshold. If it 960is important that the new level of precision be realised for all or part of 961the system before computation proceeds, \bipref{set_threshold/2}{../bips/lib/ic/set_threshold-2.html} should be used 962instead. 963 964\item [\biptxtrefni{set_threshold(Threshold, WakeVars)}{set_threshold/2!ic}{../bips/lib/ic/set_threshold-2.html}] 965\index{set_threshold/2@\texttt{set_threshold/2}!ic} 966Sets the propagation threshold, with re-computation. If the threshold has 967been reduced, all constraints suspended on the bounds of the variables in 968the list WakeVars are woken. 969 970\end{description} 971 972 973 974\subsection{Solving by Interval Propagation} 975\index{propagation} 976Some problems can be solved just by interval propagation, for example: 977\begin{quote} 978\begin{verbatim} 979[eclipse 9]: X :: 0.0..100.0, sqr(X) $= 7-X. 980 981X = X{2.1925824014821353 .. 2.1925824127108307} 982 983Delayed goals: 984 ... 985yes. 986\end{verbatim} 987\end{quote} 988There are two things to note here: 989\begin{itemize} 990\item The solver typically does not instantiate real variables; it only 991 reduces them to narrow ranges. 992\item In general, many delayed goals remain at the end of propagation. 993\index{delayed goals} 994 This reflects the fact that the variable's ranges could possibly 995 be further reduced later on during the computation. 996 It also reflects he fact that 997\item the solver does not guarantee the existence of solutions in the 998\index{existence of solutions} 999 computed ranges. However, it guarantees that there are no solutions 1000 outside these ranges. 1001\end{itemize} 1002Note that, since variables by default range from minus to plus infinity, 1003\index{default range} 1004we could have written the above example as: 1005\begin{quote} 1006\begin{verbatim} 1007[eclipse 2]: sqr(X) $= 7-X, X $>= 0. 1008 1009X = X{2.1925824014821353 .. 2.1925824127108307} 1010 1011Delayed goals: 1012 ... 1013yes. 1014\end{verbatim} 1015\end{quote} 1016If too little information is given, the interval propagation may not 1017be able to infer any interesting bounds: 1018\begin{quote} 1019\begin{verbatim} 1020[eclipse 2]: sqr(X) $= 7-X. 1021 1022X = X{-1.0Inf .. 7.0} 1023 1024Delayed goals: 1025 ... 1026yes. 1027\end{verbatim} 1028\end{quote} 1029 1030 1031 1032\subsection{Reducing Ranges Further} 1033\label{sec:real-solving} 1034There are two methods for further domain reduction. They both rely on 1035search and splitting the domains. There are two parameters to specify how 1036domains are to be split. 1037\index{domain splitting} 1038 1039The {\em Precision} \index{Precision} parameter is used to specify the 1040minimum required precision, i.e.\ the maximum size of the resulting 1041intervals (in either absolute or relative terms). 1042Note that the propagation threshold 1043(section~\ref{sec:propagation-threshold}) needs to be one or several orders 1044of magnitude smaller than {\em precision}, otherwise the solver may not be 1045able to achieve the required precision. 1046 1047The {\em lin/log} \index{lin}\index{log} parameter guides the way domains are split. 1048If it is set to {\em lin} then the split is in the arithmetic middle. 1049If it is set to {\em log}, the split is such as to have roughly the 1050same number of floats to either side of the split. This is to take 1051the logarithmic distribution of the floats into account. 1052 1053If the ranges of variables at the start of the squashing algorithm are 1054\index{squash} 1055known not to span several orders of magnitude ($|max| < 10 * |min|$) the 1056somewhat cheaper linear splitting may be used. In general, log splitting 1057is recommended. 1058 1059\begin{description} 1060\item[{\bf locate(+Vars, +Precision)}] 1061\item[{\bf locate(+Vars, +Precision, +lin/log)}] 1062\index{locate/2} 1063\index{locate/3} 1064Locate solution intervals for the given variables with the required 1065precision. This works well if the problem has a finite number of solutions. 1066locate/2,3 work by nondeterministically splitting the ranges of the variables 1067until they are narrower than Precision. 1068 1069\item[{\bf squash(+Vars, +Precision, +lin/log)}] 1070\index{squash/3} 1071Use the squash algorithm (see below) on these variables. 1072This is a deterministic reduction of the ranges of variables, done by 1073searching for domain restrictions which cause failure, and then reducing 1074the domain to the complement of that which caused the failure. 1075This algorithm is appropriate when the problem has continuous solution ranges 1076(where locate would return many adjacent solutions). 1077 1078\item[{\bf locate(+LocateVars,+SquashVars,+Precision,+lin/log)}] 1079\index{locate/4} 1080A variant of locate/2,3 with interleaved squashing: 1081The squash algorithm (see below) 1082is applied once to the SquashVars initially, 1083and then again after each splitting step, 1084i.e.\ each time one of the LocateVars has been split nondeterministically. 1085A variable may occur both in LocateVars and SquashVars. 1086\end{description} 1087 1088\subsubsection{Squash algorithm} 1089 1090\index{squash} 1091A stronger propagation algorithm is also included. This is built upon the 1092normal bound consistency. It guarantees that, if you take any variable 1093and restrict its range to a small domain near one of its bounds, 1094the original bound consistency solver will not find any constraint 1095unsatisfied. 1096 1097\begin{figure} 1098\includegraphics{example1.eps} 1099\caption{Propagation with Squash algorithm (example)} 1100\end{figure} 1101 1102All points (X,Y) Y $>$= X, lying within the intersection of 2 circles with 1103radius 2, one centred at (0,0) the other at (1,1). 1104\begin{quote} 1105\begin{verbatim} 1106[eclipse 2]: 4 $>= X^2 + Y^2, 4 $>= (X-1)^2+(Y-1)^2, Y $>= X. 1107 1108Y = Y{-1.0000000000000004 .. 2.0000000000000004} 1109X = X{-1.0000000000000004 .. 2.0000000000000004} 1110 1111Delayed goals: 1112 ... 1113yes. 1114\end{verbatim} 1115\end{quote} 1116The bound-consistency solution does not take into account the X $>$= Y 1117constraint. Intuitively this is because it passes through the corners 1118of the box denoting the solution to the problem of simply intersecting 1119the two circles. 1120 1121\begin{quote} 1122\begin{verbatim} 1123[eclipse 2]: 4 $>= X^2 + Y^2, 4 $>= (X-1)^2+(Y-1)^2, Y $>= X, 1124 squash([X,Y], 1e-5, lin). 1125 1126X = X{-1.0000000000000004 .. 1.4142135999632601} 1127Y = Y{-0.41421359996326 .. 2.0000000000000004} 1128 1129Delayed goals: 1130 ... 1131yes. 1132\end{verbatim} 1133\end{quote} 1134 1135\subsection{Obtaining Solver Statistics} 1136 1137(Using the facilities described in this section requires importing the 1138\bipref{ic_kernel}{../bips/lib/ic_kernel/index.html} module. Also, since 1139they depend on the internals of the IC library, the details presented here 1140are subject to change without notice.) 1141 1142Often it is difficult to know where the solver spends its time. 1143The library has built-in counters which keep track of the number of times 1144various events occur: 1145\begin{description} 1146 \item[ic_lin_create] 1147 The number of linear constraints set up. 1148 \item[ic_lin_prop] 1149 The number of times a linear constraint is propagated. 1150 \item[ic_uni_prop/ic_bin_prop/ic_tern_prop] 1151 The number of times a non-linear (unary/binary/ternary) operator is 1152 propagated. 1153 \item[ic_split] 1154 The number of domain splits in locate/2,3,4. 1155 \item[ic_squash] 1156 The number of squash attempts in squash/3 or locate/4. 1157\end{description} 1158 1159Users who wish to track activity within their own programs may (if they 1160wish) use the same mechanism. New event types can be registered (see 1161below) and actions recorded by calling the 1162\biptxtrefni{ic\_event(Event)}{ic_event/1!ic_kernel}{../bips/lib/ic_kernel/ic_event-1.html} 1163\index{ic_event/1@\texttt{ic_event/1}!ic_kernel} 1164predicate. 1165 1166The counters are controlled using the primitives: 1167\begin{description} 1168\item[\biptxtrefni{ic\_stat(on)}{ic_stat/1!ic_kernel}{../bips/lib/ic_kernel/ic_stat-1.html}] 1169\index{ic_stat/1@\texttt{ic_stat/1}!ic_kernel} 1170\item[\biptxtrefni{ic\_stat(off)}{ic_stat/1!ic_kernel}{../bips/lib/ic_kernel/ic_stat-1.html}] 1171Enables/disable collection of statistics. Default is off. 1172 1173\item[\biptxtrefni{ic\_stat(reset)}{ic_stat/1!ic_kernel}{../bips/lib/ic_kernel/ic_stat-1.html}] 1174Reset statistics counters. 1175 1176\item[\biptxtrefni{ic\_stat(print)}{ic_stat/1!ic_kernel}{../bips/lib/ic_kernel/ic_stat-1.html}] 1177Print statistics counters to the standard output stream. 1178 1179\item[\biptxtrefni{ic\_stat_get(-Stat)}{ic_stat_get/1!ic_kernel}{../bips/lib/ic_kernel/ic_stat_get-1.html}] 1180Returns a list of CounterName=CounterValue pairs, summarising the 1181computation since the last reset. 1182 1183\item[\biptxtrefni{ic\_event(+Name)}{ic_event/1!ic_kernel}{../bips/lib/ic_kernel/ic_event-1.html}] 1184Records the fact that the named event has happened. 1185 1186\item[\biptxtrefni{ic_stat\_register\_event(+Name,+Description)}{ic_stat_register_event/2!ic_kernel}{../bips/lib/ic_kernel/ic_stat_register_event-2.html}] 1187Registers a new event type and sets the counter to 0. This allows 1188user-defined predicates to record their own events within the same 1189framework. 1190 1191\end{description} 1192 1193 1194\section{General Guidelines for the Use of the IC library} 1195Whilst IC has been designed to provide a flexible, consistent and yet 1196powerful framework for many sorts of constraint satisfaction 1197problems, it can not be all things to all people. 1198 1199There are circumstances under which IC will not propagate all possible 1200information, for reasons of efficiency. 1201 1202It is the purpose of this section to point out ways that may help IC 1203to solve problems more efficiently. 1204 1205Typical constraint satisfaction problems are solved by iteratively 1206propagating information from basic constraints until no more 1207propagation can take place (i.e.\ a fixed point has been reached), and 1208then reducing the domain of a variable so as to prompt more 1209propagation. 1210 1211As with most constraint solvers the propagation provided by the 1212builtin constraints is rarely able to solve large problems completely 1213without the need for some form of search. A number of factors affect 1214the speed of the propagation phase. 1215 1216\begin{enumerate} 1217\item The size of the initial domains. 1218Smaller domains typically result in propagation reaching a fixed point 1219sooner. So give explicit initial domains to as many variables as possible. 1220\item Integer domains allow more propagation. 1221An important point to note here is that (as in mathematics) IC treats 1222integers as a strict subset of the reals, and as such the integer 1223domain \verb|0 .. 100| contains significantly fewer values than the 1224real domain \verb|0.0 .. 100.0|. With this in mind, IC attempts to 1225infer integrality where possible (e.g.\ the sum of two integer variables 1226is constrained to be integer), however integer domains (where 1227applicable) should be used in user code. 1228 1229The difference becomes apparent when dealing with strict inequalities, for example. 1230\begin{verbatim} 1231[eclipse 4]: reals([X]), X $> 5. 1232 1233X = X{5.0 .. 1.0Inf} 1234 1235 1236Delayed goals: 1237 ic : (X{5.0 .. 1.0Inf} > 5) 1238Yes (0.00s cpu) 1239\end{verbatim} 1240Note that the lower bound of X is still five despite the fact that X 1241has been constrained to be strictly greater than five. Further note 1242the presence of a delayed goal which will fail should X be constrained 1243to be exactly five. 1244 1245\begin{verbatim} 1246[eclipse 5]: integers([X]), X $> 5. 1247 1248X = X{6 .. 1.0Inf} 1249Yes (0.00s cpu) 1250\end{verbatim} 1251In this example since X is known to be integral, the lower bound of X 1252can be set to 6, as there are no integers between five and six. 1253\end{enumerate} 1254 1255 1256\section{User defined constraints} 1257 1258The library \bipref{ic_kernel}{../bips/lib/ic_kernel/index.html} provides a 1259number of facilities useful for implementing IC constraints or otherwise 1260extending the facilities provided by the standard IC library. 1261 1262While the \bipref{ic_kernel}{../bips/lib/ic_kernel/index.html} library 1263exposes the structure of the IC attribute to the programmer (see below), 1264accessing it directly is strongly discouraged (if for no other reason, 1265the internals of IC may continue to evolve). 1266For accessing information about a variable and its domain, use the 1267predicates described earlier in section~\ref{domain-query} ``Variable query 1268predicates''. 1269For modifying a variable, it is particularly important to go through the 1270access predicates, in order to make sure that the internal state remains 1271consistent, that appropriate constraints are scheduled for execution as a 1272result of the change, etc. 1273The predicates available for modifying a variable are discussed in the next 1274section. 1275 1276\subsection{Modifying variable domains} 1277 1278When using IC variables in normal code, one would typically use the 1279\verb|$\=|, \verb|$=<| and \verb|$>=| family of constraints to (resp.) 1280remove a value, reduce the upper bound or increase the lower bound of a 1281variable. 1282 1283While these constraints are good for normal CSP solving, they have a 1284number of properties which may be less desirable when writing new 1285constraints. In particular, they may leave unwanted delayed goals 1286behind and may perform extra propagation before returning (it may be 1287desirable to perform all required bound updates before allowing further 1288propagation to occur). 1289 1290To give the constraint writer more control over such matters, special 1291predicates exist in the \bipref{ic_kernel}{../bips/lib/ic_kernel/index.html} 1292module which allow direct modification of the domain without the waking of 1293goals (they are scheduled for execution but not actually executed). 1294These predicates generally accept an IC variable, a non-IC variable (which 1295will be constrained to make it a real IC variable) or a number. 1296 1297Full details on these predicates can be found in the reference manual; they 1298are listed here for completeness. Note that with the exception of 1299\bipref{impose_bounds/3}{../bips/lib/ic_kernel/impose_bounds-3.html} none of 1300the goals call \bipref{wake/0}{../bips/kernel/suspensions/wake-0.html}, so 1301the programmer is free to do so at a convenient time. 1302 1303\begin{description} 1304\item[\bipref{impose_min/2}{../bips/lib/ic_kernel/impose_min-2.html}] 1305Set the lowerbound. 1306\item[\bipref{impose_max/2}{../bips/lib/ic_kernel/impose_max-2.html}] 1307Set the upperbound. 1308\item[\bipref{impose_bounds/3}{../bips/lib/ic_kernel/impose_bounds-3.html}] 1309Sets both upper and lower bounds. 1310\item[\bipref{exclude/2}{../bips/lib/ic_kernel/exclude-2.html}] 1311Excludes an integer from an integral variable. 1312\item[\bipref{exclude_range/3}{../bips/lib/ic_kernel/exclude_range-3.html}] 1313Excludes a range of integers from an integral variable. 1314\item[\bipref{set_var_type/2}{../bips/lib/ic_kernel/set_var_type-2.html}] 1315Makes the variable be of the given type. 1316\item[\bipref{set_vars_type/2}{../bips/lib/ic_kernel/set_vars_type-2.html}] 1317Like set_var_type, but works for lists and submatrices of variables as well. 1318\end{description} 1319 1320\subsection{The IC attribute} 1321 1322The IC attribute is a meta-term which is attached to all variables which 1323take part in IC constraints. 1324\bipref{ic_kernel}{../bips/lib/ic_kernel/index.html} defines the IC 1325attribute as a structure of the following form: 1326\begin{verbatim} 1327ic{var_type:Type, 1328 lo:Lo, 1329 hi:Hi, 1330 bitmap:Bitmap, 1331 min:SuspMin, 1332 max:SuspMax, 1333 hole:SuspHole, 1334 type:SuspType 1335 } 1336\end{verbatim} 1337 1338 1339This structure holds: 1340 1341\begin{description} 1342\item[var_type] The type of the variable. This defaults to 'real' but may 1343become 'integer' after an explicit call to {\bf integers/1}, by being 1344included in an integer constraint (e.g.\ {\bf \#=}) or by inferences made 1345during constraint propagation. 1346\item[lo] The lower bound of the variable's domain, as a float. 1347\item[hi] The lower bound of the variable's domain, as a float. 1348\item[bitmap] Where relevant, a bitmap representation of the integer domain; 1349where not relevant it holds the atom \verb|undefined|. 1350\item[min] Suspension list of goals to be woken on lower bound changes. 1351\item[max] Suspension list of goals to be woken on upper bound changes. 1352\item[hole] Suspension list of goals to be woken when a value is removed 1353from the middle of a domain. Such removals only happen for integer 1354variables whose domain is finite. 1355\item[type] Suspension list of goals to be woken when a variable's type 1356becomes more constrained, i.e.\ when a variable goes from being real to 1357being integer. 1358\end{description} 1359 1360The suspension list names can be used in 1361\bipref{suspend/3}{../bips/kernel/suspensions/suspend-3.html} and 1362related predicates to denote an appropriate waking condition. 1363 1364The attribute of a domain variable can be accessed with the predicate 1365\bipref{get_ic_attr/2}{../bips/lib/ic_kernel/get_ic_attr-2.html}. 1366 1367As noted above, direct access and manipulation of the attribute is 1368discouraged; use the access predicates instead. 1369 1370\index{library!ic|)} 1371 1372%HEVEA\cutend 1373 1374