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%\documentstyle[epsf,a4wide]{article}
24\documentclass[a4wide]{article}
25\usepackage{hevea}
26\usepackage{graphics}
27\usepackage{color}
28\usepackage{heveaonly}
29
30%\documentstyle[chicago,epsf]{article}
31%\newtheorem{proposition}{Proposition}
32%\newtheorem{mdef}{Definition}
33\newcommand{\ECL}{\mbox{ECLiPSe\ }{\hspace{1mm}}}
34%\newcommand{\ECLII}{\mbox{ECL$^i$PS$^e$II}{\hspace{1mm}}}
35\date{August 1997}
36\title{ \ECL : A Platform for\\
37       Constraint Logic Programming}
38 
39\author{Mark Wallace, Stefano Novello, Joachim Schimpf\\
40Contact address: IC-Parc,\\ 
41William Penney Laboratory, Imperial College, LONDON SW7 2AZ.\\  
42email: mgw@doc.ic.ac.uk}
43
44\begin{document}
45\maketitle
46
47%
48% NOTICE THAT ALL LINES STARTING WITH A % ARE COMMENTED OUT !!!
49%
50
51\begin{abstract}
52This paper introduces the Constraint Logic Programming (CLP) platform \ECL.
53\ECL is designed to be more than an implementation of CLP: it
54also supports mathematical programming and stochastic programming
55techniques.
56The crucial advantage of \ECL  is that it enables the programmer to
57use a combination of algorithms appropriate to the application at hand. 
58This benefit results from the \ECL facility to support fine-grained
59hybridisation.
60
61\ECL is designed for solving difficult "combinatorial" industrial
62problems in the areas of planning, scheduling and resource allocation.
63The platform offers a conceptual modelling language for specifying the problem
64clearly and simply, in a way that is neutral as to the algorithm which
65will be used to solve it.
66Based on the conceptual model it is easy to construct alternative
67design models, also expressed in \ECL.
68A design model is a runnable program, whose execution in \ECL
69employs  a specific combination of algorithms.
70Thus the platform supports experimentation with different hybrid
71algorithms.
72
73Technically the different classes of algorithms mentioned above
74have two aspects: constraint handling, and search.
75Various different constraint handling facilities are available as
76\ECL libraries.
77These include finite domain propagation, interval propagation and
78linear constraint solving.
79In \ECL the same constraint can be treated concurrently by several
80different handlers.
81
82With regard to search behaviour,
83CLP and also mathematical programming typically impose new
84constraints at lower levels in the search tree.
85By contrast, stochastic techniques search for good solutions by
86locally repairing an original solution, and repeating the process
87again and again.
88\ECL supports both kinds of search, and allows them to be combined
89into hybrid search techniques.
90\end{abstract}
91
92\section{Introduction: The \ECL Philosophy}
93The first generation of constraint programming languages focussed on a
94single technique: constraint propagation, as described in section 4 of
95\cite{consprog}.
96Whilst constraint propagation has proved itself on a variety of
97applications, 
98it cannot alone suffice to efficiently produce solutions for typical
99practical industrial problems.
100
101Over the years Operations Researchers have designed highly efficient
102algorithms for several classes of problems, such as set partitioning,
103matching, knapsack, and network flow problems,
104using techniques based on Mixed Integer Programming (MIP).
105More recently stochastic techniques, such as Simulated Annealing,
106have achieved striking results on optimisation problems such as the
107travelling salesman problem.\footnote{The travelling salesman problem
108is to find the shortest route which
109starts at a certain point, visits a given set of destinations
110(customers), and returns to the starting point at the end.}
111
112\ECL is designed to take advantage of all these results, by supporting
113industrial scale MIP functionality, and
114stochastic techniques, as well as constraint propagation and solving.
115
116More importantly, real industrial problems seldom fit into a specific
117class: the pure travelling salesman problem rarely comes up in real
118life because there are typically many salesmen available to cover the
119different customers, certain customers can only be visited at certain times of
120day, also roads are busier at certain times of day so the journey time
121may vary with the time of day, and anyway the poor salesmen need  some
122time to rest - they can't 
123usually complete their circuits before lunchtime!
124These ``side constraints'' may belong to another problem class - such
125as the class of set covering problems, or scheduling problems.
126
127Industrial problems typically have constraints that belong to different
128problem classes - they are in a sense ``hybrid''.  
129Accordingly it is
130not only necessary to offer a wide choice of algorithms for solving such
131problems, but also the facility to mix and match the
132algorithms, i.e. to build hybrid algorithms.
133
134\ECL is designed to support the fast development of specific hybrid
135algorithms tuned to the problem at hand.  It is not assumed that the
136first algorithm implemented by the application developer is guaranteed
137to be the 
138best one: rather \ECL provides a platform supporting experimentation
139with different hybrid algorithms until an appropriate one is found
140which suits the particular features of the application.
141
142In the next section we shall explore
143\ECL as a problem modelling language.  
144We distinguish two kinds of model: the {\em conceptual} model, which
145captures the problem specification, and the {\em design} model, which
146is tuned for efficient solving on a computer.
147\ECL is designed to support both kinds of models, and the mapping
148between them.
149
150In the following two  sections we shall examine the \ECL facilities for
151handling constraints.  
152In \cite{consprog} we
153encountered different kinds of constraints - {\em primitive}
154constraints, {\em propagation} constraints and {\em constraint
155agents}.
156\ECL supports various classes of built-in constraints, both {\em
157primitive} constraints and 
158{\em propagation} constraints.
159\ECL also allows complex constraints and constraint behaviours to be
160constructed from the built-in classes, thus supporting {\em constraint
161agents}.
162
163%Once the notion of a constraint is well-established, we shall explore
164%\ECL as a problem modelling language.  
165%We shall examine \ECL as a conceptual modelling language, and the
166%mapping from a conceptual model to alternative design models.
167
168
169%Although modelling should come
170%before constraints and their different behaviours, this ordering of
171%sections has been chosen for pedagogical purposes.
172%It is hard to tell whether a given modelling formalism is appropriate
173%for the range of applications in mind.
174%Hopefully some understanding of the functionality of the underlying
175%\ECL system, will give the reader an intuitive feeling for the range
176%of problems that need to be modelled.
177
178%Readers are assumed to have some
179%experience of different programming languages, and for such readers,
180%myself included, it is impossible to
181%study any formal language - even if it is intended as a modelling
182%language - without making assumptions about its execution
183%behaviour!
184%To avoid any possible wrong assumptions, it is therefore best to study
185%at least some aspects of behaviour first.
186
187After constraint handling we return to the second major aspect of
188problem solving: the search for solutions.
189
190We will separate this discussion into two subsections.  The first is
191concerned with {\em constructive} search, and the second with {\em
192repair-based} search.  
193Constructive search explores the consequences of making choices for
194decision variables one-at-a-time.  Each choice reduces the set of
195viable choices for the remaining decisions.
196By contrast repair-based search explores the consequences, not of
197making decisions, but of {\em changing} them.
198In this case the new choice is typically compared with the previous
199one, in the context of other suggested choices for the other decision
200variables. 
201Initially it is not expected that the suggested choices are
202necessarily consistent with the constraints.  The idea of changing the
203choices is to reduce the number of constraint violations until all
204the constraints are finally satisfied.
205
206%The next section will introduce two rather general hybrid
207%algorithms which have been developed in \ECL.
208%These algorithms are currently being encapsulated as \ECL libraries.
209%The main purpose of this section is not so much to describe two
210%particular hybrid algorithms, but to illustrate ways of working
211%supported by the \ECL platform.
212
213Finally there is a brief section on the \ECL system, its external
214communication facilities, embeddability, documentation and how to
215obtain the system. 
216
217
218\section{\ECL as a Modelling Language}
219
220\label{conceptual}
221\subsection{Overview of \ECL as a Modelling Language}
222\ECL is tailored for solving combinatorial problems.  Such problems
223are characterised by a set of decisions which have to be made (where
224each decision has a set of alternative choices) and a set of
225constraints between the decisions (so a certain choice for one
226decision may preclude, or entail, certain choices for other
227decisions).
228 
229In \ECL each decision is modelled by a variable, and each choice
230by a possible value for that variable.  The constraints are modelled
231by relations between the variables.
232As an example consider the map colouring program, with four countries
233to colour.
234\begin{quote}
235\begin{verbatim}
236coloured(Countries) :-
237    Countries = [A,B,C,D],
238    ( foreach(Country, Countries) do value(Country) ),
239    ne(A,B), ne(A,C), ne(A,D), ne(B,C), ne(B,D), ne(C,D).
240
241value(red).
242value(green).
243value(blue).
244\end{verbatim}
245{\bf A Generic Logic Program for Map Colouring}
246\label{mapconceptual}
247\end{quote}
248
249This program was also used to illustrate constraint logic programming
250in \cite{consprog}.
251\ECL is a constraint logic programming language, and it uses a
252syntax similar to Prolog.
253Hopefully this syntax will already be familiar to many readers.
254At the same time, we also hope that any readers who have suffered from
255the limitations of Prolog will not conclude that \ECL therefore
256suffers from the same limitations!
257
258The problem involves four decisions, one for each country.  These are
259modelled by the variables $A$, $B$, $C$ and $D$.  $Countries$ is just
260a name for the list of four variables.
261Each decision variable,
262in this problem, has the same set of choices, modelled as possible values
263for the variables ($red$, $green$ and $blue$).
264There are six constraints, each of which is modelled by the same
265relation ($ne$ meaning {\em not equal to}).
266
267%Much of the functionality of \ECL is held in different libraries, some
268%of which will be introduced in the next section.
269%The library {\em apply\_macros} holds the definition of the {\em
270%applist} predicate, which applies a predicate to each element of a
271%list.
272%{\em applist(value,Countries)} is equivalent to {\em value(A),
273%value(B), value(C), value(D)}.
274The foreach-do construct is an \ECL language extension which provides
275a logical form of loops. In the example, it is equvalent to
276{\em value(A), value(B), value(C), value(D)}.
277
278
279\subsection{Why Logic Programming}
280
281The requirements on \ECL are of two kinds: to enable
282such problems to be modelled simply and naturally; 
283and to enable the resulting problem model to be solved efficiently.
284The separation of modelling and solving is supported in \ECL by
285distinguishing the conceptual model, expressed as a ``pure'' logical
286\ECL program, from the design model, which is constructed from the
287conceptual model by adding control to the \ECL program.
288
289This combination of requirements is difficult to satisfy - perhaps
290impossible if a completely general modelling language is required,
291suitable for every kind of application. 
292However the applications for which \ECL is designed are decision
293support applications involving combinatorial problems.
294
295Logic programming is peculiarly apt for modelling problems of this
296kind for two reasons.
297\begin{itemize}
298\item
299It is based on relations
300\item
301It supports logical variables
302\end{itemize}
303Since every combinatorial problem is naturally modelled as a set of
304variables and a set of constraints (i.e. relations) on those
305variables, the facilities of logic programming precisely match the
306requirements for modelling combinatorial problems.
307
308Every predicate in a logic program defines a relation, either
309explicitly as a set of facts, or implicitly in terms of rules.
310We can recall the example from \cite{consprog}.
311The predicate $meat$ was defined by two facts:
312\begin{quote}
313\begin{verbatim}
314meat(beef,5).
315meat(pork,7).
316\end{verbatim}
317\end{quote}
318whilst the predicate $main$ (meaning ``main course'') was defined by
319two rules:
320\begin{quote}
321\begin{verbatim}
322main(M,I) :- meat(M,I).
323main(M,I) :- fish(M,I).
324\end{verbatim}
325\end{quote}
326
327Variables in logic programming are logical variables.  Thus it is
328entirely natural to initialise the problem variables (for example by
329writing $Countries = [A,B,C,D]$) and then to constrain them (for
330example by writing $ne(A,B)$ and so on).
331
332We briefly compare \ECL as a modelling language with formal
333specification languages, mathematical modelling languages,
334mainstream programming languages and object oriented languages.
335
336\subsubsection{Formal Specification Languages}
337Formal specification language are designed for formality, but not for
338execution.  Consequently they include constructs, such as universal
339quantification, which are precisely defined but are not constructive.
340In other words there are constructs which cannot be mapped onto any
341(practical) algorithm. 
342
343Luckily the class of problems for which \ECL is designed have a finite
344set of decision variables each of which admits only finitely many
345alternatives.  Consequently it is only necessary to support a
346restricted form of logic\footnote{technically called Horn clauses}
347which is easier to understand and easier to implement.
348The nearest thing \ECL offers to universal quantification is iteration
349over finite sets, as for example the goal
350%{\em applist(value,Countries)}
351{\em foreach(Country, Countries) do value(Country)},
352in figure \pageref{mapconceptual}.
353
354The restricted logic of \ECL has a benefit that the mapping from the
355conceptual model of the problem to the design model is an extension of
356the conceptual model rather than a rewriting.
357This means that when problem requirements change it is natural to
358capture this change in the conceptual model, and then carry them
359through to the design model.
360The result is that during application development the conceptual model
361and the design model remain in step.
362This avoids many of the pitfalls which await developers working on
363applications whose specifications are changing even during application
364development!
365
366\subsubsection{Mathematical Modelling Languages}
367\label{mathconceptual}
368There already exists a class of modelling languages designed for
369combinatorial problems.
370These are the mathematical modelling languages typically used as input
371to mixed integer programming (MIP) packages.
372We further  discuss MIP, and how to use it through \ECL, in section
373\ref{eplex} below. 
374
375Although the syntax is different, mathematical modelling languages
376share many of the features of logic programming.
377They support logical variables, and constraints.
378They support numerical constraints which, though not supported in
379traditional logic programs, are supported by constraint logic programs
380as we shall see in the following section.
381They support named constraints, which is achieved in constraint logic
382programming by introducing a predicate name, eg
383\begin{quote}\begin{verbatim}
384precede(T1,T2) :- T1 >= T2.
385\end{verbatim}\end{quote}
386
387There are two facilities in constraint logic programming which are not
388available in mathematical modelling languages.
389The main one is quite simple: in constraint logic programs it is
390possible to define a constraint which involves a disjunction.
391Mathematical programming cannot handle disjunction directly.
392The second difference is that logic programming allows new constraints
393to be defined in terms of existing ones, even recursively.
394In mathematical programming the model is essentially flat, which not
395only complicates the model but also reduces reuseability within an
396application and across applications.
397
398To illustrate the advantage of handling disjunction in the modelling
399language, we take a toy example and present two models: a
400mathematical programming model and a constraint logic programming
401model.
402
403Consider the constraint that two tasks sharing a
404single resource cannot be done at the same time.
405The constraint involves six variables: the start times $S_1, S_2$, end times
406$E_1, E_2$ and resources $R_1, R_2$ of the two tasks.
407The specification of this constraint is as follows:
408\begin{quotation}
409{\em Either} the two tasks use distinct resource ( $R_1\ \  ne\ \  R_2$)
410{\em or} task$_1$ ends before task$_2$ starts ($E_1 \leq S_2$)
411{\em or else} task$_2$ ends before task$_1$ starts ($ E_2 \leq S_1$).
412\end{quotation}
413First we shall show how it can expressed as a mathematical model
414without disjunctions.
415For this purpose it must be encoded using numerical equations
416and inequalities, together with integer constraints. 
417
418The disjunctions can be captured by introducing three {\em 0/1} variables,
419$B_{r1}$, $B_{r2}$, and $B_t$, and using some large constant, say
420$100000$, larger than any possible values for any of the six variables.
421Now we can express the constraint in terms of numerical inequalities as
422follows:
423\begin{quotation} 
424$R_1 + 100000*B_{r1} + 100000*B_{r2} \geq R_2 +1$\\
425$R_2 + 100000*B_{r1} + 100000*(1-B_{r2}) \geq R_1 + 1$\\
426$S_1 + 100000*(1-B_{r1}) + 100000*B_t \geq E_2$\\
427$S_2 + 100000*(1-B_{r1}) + 100000*(1-B_t) \geq E_1$
428\end{quotation}
429If $B_{r1}=0$ then the two
430tasks use different resources.
431In this case, if also $B_{r2}=0$ then $R_1 \geq R_2 + 1$, 
432otherwise $B_{r2}=1$ and  $R_2 \geq R_1 + 1$.
433It is an exercise for the reader to prove that if $B_{r1}=0$ then the
434tasks can overlap. 
435Otherwise, if $B_{r1}=1$, then $B_t=0$ entails $S_1 \geq E_2$ and $B_t=1$
436entails $S_2 \geq E_1$.
437
438In \ECL this constraint can be modelled directly in logic, as
439illustrated below.  
440\begin{quote}
441\begin{verbatim}
442taskResource(S1,E1,R1,S2,E2,R2) :- 
443        ne(R1,R2).
444taskResource(S1,E1,R1,S2,E2,R2) :-
445        R1=R2, S1 >= E2.
446taskResource(S1,E1,R1,S2,E2,R2) :-
447        R1=R2, S2 >= E1.
448\end{verbatim}
449{\bf Specifying a Resource Contention Constraint in \ECL}
450\label{nooverlap}
451\end{quote} 
452
453We note that the \ECL model is a {\em conceptual} model, whilst the
454mathematical model is a {\em design} model.
455The point here is that in \ECL both models can be expressed, whilst
456mathematical modelling can only express a design model.
457Indeed we shall show in section \ref{propia} below a design model
458written in \ECL that is very close to the conceptual model.
459
460Another \ECL design model, which is also close to the conceptual
461model, is handled in \ECL by an automatic translator which  builds the
462MIP model  
463and passes it to the MIP solver of \ECL.
464This translator is described in \cite{clpmip} which is available from
465the IC-Parc 
466home page (whose URL is given in section \ref{icparcurl} below).
467
468Whilst the above example shows that such complex constraints can be
469expressed in terms of numerical inequalities, as required for MIP, the
470encoding is awkward and difficult to debug. 
471It becomes increasingly difficult as the constraints become more
472complex (eg the current example immediately becomes harder still if the
473resources have a finite capacity greater than one).
474
475Notice, finally, that the mathematical model requires resources to be
476identified by numbers, whilst the constraint logic programming model
477imposes no such restriction as we shall show in section
478\ref{complexconstraints} below.
479
480\subsubsection{Mainstream Programming Languages}
481Naturally the implemented solution to an industrial problem must be
482delivered into the industrial computing environment.
483It is sometimes argued that this is only possible if the solution is
484implemented in a mainstream programming language such as \verb0C0,
485\verb0C++0 or even \verb0Java0.
486There are two arguments supporting this view, firstly that of
487embeddability (it is easier and more efficient to pass data and
488control between modules written in the same programming language), and
489secondly that of system support (mainstream language programmers are
490much easier to find and replace than specialist programmers).
491
492Whilst this argument only supports a mainstream programming language
493being used for implementation, and not conceptual modelling, it has
494consequences for the modelling language as well on the assumption,
495which we discussed above, that the conceptual model should be close to
496the design model.
497Thus if the design model is encoded in a mainstream programming
498language, then either the conceptual model must be compromised,
499becoming more like a design model, or the gap between the conceptual
500model and design model grows very wide.
501
502Sadly the attempt to tackle combinatorial problems with mainstream
503programming languages has too often foundered because 
504the implemented solution has proved not to solve the actual industrial
505requirement (often because requirements change during application
506development). 
507The solution cannot then be modified to meet the actual, or new,
508requirements within a reasonable cost and timescale.
509
510Given that the core combinatorial optimisation problem is best solved
511by a specialised programming platform (either mathematical or
512constraint-based), the problem of embedding has to be solved.
513 
514One approach is to embed constraint solving in a mainstream
515programming language.
516As we shall see in section \ref{search} below, search and
517constraint handling are closely interdependent.
518Even if the search is encoded in a mainstream programming language, the
519programmer is required to understand in detail not only the data
520structures used by the constraint handlers, but their operational 
521behaviour.
522
523In practice packages providing an embedding of constraints in
524mainstream programming languages also encapsulate search within the
525package.
526The application developer is required to control the search.
527To
528avoid any mismatch between the host programming language and search
529control within the package, a popular approach is to implement the
530package as a library of the host programming language.
531
532The result is that the separation of conceptual
533modelling and design modelling is given up, in favour of staying 
534within the confines of the expressive capabilities of the host
535programming language.
536This approach not only requires specialist programmers to develop and
537support 
538the application, but it also sacrifices the modelling advantages of
539mathematical and constraint logic programming.
540
541In fact the problem of embedding has been overcome, though first
542generation constraint logic programming languages were deficient in
543this area.
544\ECL is fully embeddable in \verb0C0 and \verb0C++0, and indeed uses
545an external solver, written in \verb0C0 to handle linear constraints,
546since the runtime cost of such an interface is  perfectly acceptable
547even for a tightly integrated component such as a constraint handler!
548
549\subsubsection{Object Oriented Languages}
550\ECL supports object-orientation through two distinct features, {\em
551modules} and {\em structures}.
552Modules support {\em behavioural} object orientation, and 
553structures support {\em structural} object orientation.
554
555Because of the nature of combinatorial problems, the only
556requirement for behavioural object orientation is in the constraint
557handlers.
558The implementation of each constraints library is hidden inside a
559module, and 
560access to the internal data structures is only through predicates
561exported from the module.
562
563The remaining objects that can occur in an \ECL model have attributes
564but no behaviour, and so they require only
565structural object orientation. 
566
567In our first example we modelled a map colouring problem using only
568variables and constraints.
569It can be argued, however, that for more complex applications, the
570conceptual model can benefit from 
571a notion of object, into which variables can be built.
572For example in modelling a resource scheduling problem the notion of a
573{\em task} with certain attributes is useful.
574A task might have an {\em identifier}, a {\em start time},
575{\em end time} and a {\em duration}.
576
577After declaring structures for {\em tasks} and {\em times}, as
578below, the 
579programmer can access any of their attributes independently.
580
581\begin{quote}
582\begin{verbatim}
583[eclipse 1]: local struct( task(id, start, end, duration) ).
584*       yes.
585
586[eclipse 2]: local struct( time(hour, minute) ).
587*       yes.
588
589[eclipse 3]:   T=task with [id:a,duration:10].
590*       T = task(a, _, _, 10)
591*       yes.
592
593[eclipse 4]:  T1=task with [id:a3,start:S3,end:(time with hour:H3)],
594              T2=task with [id:a4,start:S3,end:(time with hour:H4)],
595              H3>H4.
596*       T1 = task(a3, S3, time(H3, _), _)
597*       T2 = task(a4, S3, time(H4, _), _)
598*       yes.
599
600\end{verbatim}
601{\bf Defining a Task Structure}
602\label{structures}
603\end{quote}
604Each \ECL prompt (eg \verb0[eclipse 1]:0) is followed by a user query 
605(eg \verb0lib(structures).0).
606In the rest of the article, ``query {\em N}'' always refers to the query
607which is preceded 
608by the prompt \verb0[eclipse N]:0.
609Moreover, we have added a star to the beginning of each line showing a system
610response. 
611
612Query 1 and 2 define the attributes for objects in the classes $task$ and
613$time$.
614Query 3 shows how the user can equate a variable with a structured
615object (i.e. the variable is instantiated to the structure).
616\ECL automatically constructs unknown values (written \verb0_0) for the
617unspecified attributes.
618 
619Query 4 illustrates something of the expressive power needed in a
620constraint programming language which supports objects.
621Not only do the objects $T1$ and $T2$ share the attribute value $S3$ - this
622is a shared subobject - but they also have non-shared subobjects 
623$H3$ and $H4$ whose attributes are connected by a constraint.
624Such a constraint, between distinct objects, is typically not expressible
625within the traditional 
626object-oriented framework.
627
628
629\subsection{The Conceptual Model and the Design Model}
630The main benefit of constraint logic programming over other platforms
631for solving combinatorial problems is in the closeness between the
632conceptual model and the design model.
633\ECL takes full advantage of this by offering facilities to choose
634different annotations of the same conceptual model to achieve design
635models which, whilst syntactically similar, can have radically
636different behaviour.
637
638\subsubsection{Map Colouring}
639\label{mapcolour}
640Let us start by mapping the conceptual model for the map colouring
641example illustrated in figure
642\ref{mapconceptual} into a design model which uses the finite domain
643constraint handler of \ECL.
644
645The design model is encoded as shown below.
646
647\begin{quote}
648\begin{verbatim}
649:- lib(fd).
650
651coloured(Countries) :-
652    Countries=[A,B,C,D],
653    Countries :: [red,green,blue,yellow],
654    ne(A,B), ne(A,C), ne(A,D), ne(B,C), ne(B,D), ne(C,D),
655    labeling(Countries).
656
657ne(X,Y) :- X##Y.
658
659\end{verbatim}
660{\bf A Finite Domain CLP Program for Map Colouring}
661\label{mapdesign}
662\end{quote}
663
664The design model extends the conceptual model in four ways.
665\begin{enumerate}
666\item
667The \ECL finite domain library is loaded (using {\em :- lib(fd)}).
668\item
669An explicit finite domain is associated with  each decision variable
670(using {\em Countries :: [red, green, blue]}).
671\item
672The finite domain built-in disequality constraint is used to
673implement the $ne$ constraint 
674(using {\em ne(X,Y) :- X\verb0##0Y}).
675{\em \verb0##0} is a special syntax for disequality used by the finite domain
676constraint solver.
677\item
678This program includes a search algorithm, invoked by the goal
679{\em labeling(Countries)}.
680As we shall see later, this predicate tries choosing, for each of the
681variables $A$, $B$, $C$ and $D$ in turn, a value from its domain.  It
682succeeds when a combination of values has been found that satisfies
683the constraints.
684\end{enumerate}
685
686Naturally this is a toy example, and it is not always so easy to turn
687a conceptual model, such as the \ECL program in figure
688\ref{mapconceptual}, into a design 
689model, such as the program in figure \pageref{mapdesign}.
690Nevertheless constraint logic programming, and in particular \ECL,
691have made a lot of progress in achieving a close relationship between
692the conceptual model and the design model.
693The different components of the \ECL system all support the
694separate development of a clear, correct conceptual model, and an
695efficient design model, and they also support the mapping between the two.
696
697
698\subsubsection{Having Enough Change in Your Pocket}
699Let us now take a more interesting problem, which has been set as a
700recent challenge within the MIP community.
701The problem is apparently rather simple: what is the minimum number of
702coins a purchaser needs in their pocket in order to be able to buy any one item
703costing less than one pound, and guarantee to be able to pay the exact
704amount?
705
706The problem involves only six decision variables, one for the number of
707coins of each denomination held in the pocket (the denominations are
7081,2,5,10,20,50).
709
710The conceptual model for this problem is shown in figure
711\pageref{coinsconceptual}.
712
713\begin{quote}
714\begin{verbatim}
715
716solve(PocketCoins, AllCoins, Min) :-
717        PocketCoins = [P,Tw,Fv,Te,Twe,Ff],
718        Min =:= P+Tw+Fv+Te+Twe+Ff,
719	( foreach(X,[Min|PocketCoins]) do
720	    0 =< X, X =< 99
721	),
722	( for(Total,1,99), foreach(Coins,AllCoins), param(PocketCoins) do
723	    Coins = [P1,Tw1,Fv1,Te1,Twe1,Ff1],
724	    Total =:= P1+2*Tw1+5*Fv1+10*Te1+20*Twe1+50*Ff1,
725	    ( foreach(C,Coins), foreach(PC,PocketCoins) do
726		0 =< C, C =< 99,
727		C =< PC
728	    )
729	).
730
731\end{verbatim}
732{\bf Conceptual Model for the Coins Problem}
733\label{coinsconceptual}
734\end{quote}
735The lines are numbered, using the syntax \verb0%N0, as \verb0%0 is a
736comment symbol in \ECL.
737We describe this program line by line.
738\begin{enumerate}
739
740\item 		      
741The variable {\em PocketCoins} is just a shorthand for the list of six
742variables,
743{\em [P, Tw, Fv, Te, Twe, Ff]}
744which denote the number of coins of each denomination held in the
745pocket.
746\item
747{\em [A,B,C]} is a list, but \ECL allows lists to be written in an
748alternative syntax {\em [Head \verb0|0 Tail]}.
749Thus {\em [Min \verb0|0 PocketCoins]} is simply another way of writing the list of
750seven variables,
751{\em [Min, P, Tw, Fv, Te, Twe, Ff]}.
752The command {\em applist(range(0,99), [Min \verb0|0 PocketCoins])}
753associates a range (between 0 and 99) with each of the variables.
754\item
755{\em Min} is the total number of coins in the pocket, as enforced by
756the equation 
757{\em Min \verb0=0 P+Tw+Fv+Te+Twe+Ff}.  
758\item
759To ensure that these coins are enough to make up any total between 1
760and 99, we now impose 99 further constraints, one for each total.
761{\em genc(PocketCoins,Total)} is called for each value of $Total$
762between 1 and 99.
763\item
764{\em minimize(Min)} simply specifies that
765the best feasible solution to the problem is one which minimises the
766value of the variable $Min$.
767
768\item
769{\em genc(PocketCoins,Total)} initialises
770another set of coins {\em [P1, Tw1, Fv1, Te1, Twe1, Ff1]} needed to
771make up the total $Total$.
772\item
773This set of coins is also initialised to range between 0 and 99
774\item
775Their total value is constrained to be equal to $Total$.
776This constraint is enforced by the equation
777{\em Total \verb0=0 P1+ 2*Tw1 + 5*Fv1 + 10*Te1 + 20*Twe1 + 50*Ff1}.
778\item
779Finally the constraint that the required coins of
780each denomination must be less than, or equal to, the number of coins
781of that denomination in the pocket, is enforced by the constraints:
782{\em P1 \verb0<=0 P}, 
783{\em Tw1 \verb0<=0 Tw},
784{\em Fv1 \verb0<=0 Fv},
785{\em Te1 \verb0<=0 Te},
786{\em Twe1 \verb0<=0 Twe},
787{\em Ff1 \verb0<=0 Ff}.
788
789These constraints are generated by the single command
790{\em maplist( \verb0<=0, Coins, PocketCoins)}.
791\end{enumerate}
792
793Let's start by trying mixed integer programming on this problem.
794To do this we add {\em integer} declarations for each of the integer
795variables, and change the constraints to use the syntax recognised by
796the (external) MIP solver accessed via the \ECL library {\em eplex}.
797For equations we use the syntax \verb0$=0, and for inequalities we use
798\verb0$>=0.
799The design model is shown below.
800
801\begin{quote}
802\begin{verbatim}
803
804:- lib(apply_macros).
805:- lib(eplex).
806
807solve(PocketCoins,Cost) :-
808        PocketCoins=[P,Tw,Fv,Te,Twe,Ff],
809        applist(range(0,99),[Min|PocketCoins]),
810        Min $= P+Tw+Fv+Te+Twe+Ff,
811        fromto(1,99,genc(PocketCoins)),
812        optimize(min(Min),Cost).
813
814genc(PocketCoins,Total) :-
815        Coins=[P1,Tw1,Fv1,Te1,Twe1,Ff1],
816        applist(range(0,99),Coins),
817        Total $= P1+2*Tw1+5*Fv1+10*Te1+20*Twe1+50*Ff1,
818        maplist( '$=<',Coins,PocketCoins).
819
820range(Min,Max,Var) :-
821        integers(Var),
822        Var $>= Min,
823        Var $=< Max.
824
825\end{verbatim}
826{\bf Conceptual Model for the Coins Problem}
827\label{eplexcoins}
828\end{quote}
829
830This program passes all the \verb0$=0 and \verb0$>=0  constraints to
831the {\em CPLEX} mixed 
832integer programming package \cite{CPLEX}, and invokes the CPLEX branch
833and bound 
834solver, to minimise the value of the variable $Min$.
835This minimum is placed in the variable $Cost$.
836
837As such this model can only solve the problem of producing the exact
838change up to 59 pence (replacing 99 with 59 in the above program).
839For the full problem the system runs out of memory.
840There are standard MIP solutions to the problem which run overnight,
841but it is a 
842tough challenge to reduce this time from hours to minutes!
843
844In figure \pageref{fdcoins} we illustrate an \ECL program for solving the
845``Coins'' problem using the facilities of the 
846\ECL finite domain constraint solver implemented in the \ECL
847library {\em fd}.
848
849\begin{quote}
850\begin{verbatim}
851
852:- lib(apply_macros).
853:- lib(fd).
854
855solve(PocketCoins,Min) :-
856        PocketCoins=[P,Tw,Fv,Te,Twe,Ff],
857        applist(range(0,99),[Min|PocketCoins]),
858        Min #= P+Tw+Fv+Te+Twe+Ff,
859        fromto(1,99,1,genc(PocketCoins)),
860        minimize(labeling(PocketCoins),Min).
861
862
863genc(PocketCoins,Total) :-
864        Coins=[P1,Tw1,Fv1,Te1,Twe1,Ff1],
865        applist(range(0,99),Coins),
866        Total #= P1+2*Tw1+5*Fv1+10*Te1+20*Twe1+50*Ff1,
867        maplist( '#<=',Coins,PocketCoins).
868
869range(Min,Max,Var) :-
870        Var::Min..Max
871\end{verbatim}
872{\bf {\em fd} Constraints for the Coins Problem}
873\label{fdcoins}
874\end{quote}
875
876In this case the \verb0#=0 and \verb0#>=0 constraints and the
877optimisation predicate {\em minimize} are implemented in the \ECL
878finite domain library.
879This program proves within a few seconds that the minimum number of
880coins a purchaser needs in their pocket to make up any total between 1
881and 99 is eight coins!  One solution is:
882$P=1, Tw=2, Fv=1, Te=1, Twe=2, Ff=1$.
883
884We have shown how the same
885underlying model for the ``Coins'' problem can be passed to different
886solvers so as to use the best one.
887However in \ECL it is not a choice of either/or: the same constraints
888can easily be passed to several solvers at the same time!
889For instance we can define \verb0X $#= Y0 to be both \verb0X $= Y0
890and \verb0X #= Y0 and replace \verb0=0 in the above model with
891\verb0$#=0, and we can treat \verb0>=0 similarly.
892
893Whilst for this problem the finite domain solver alone solves the
894problem most efficiently, we have encountered practical examples where
895the combination of both solvers outperforms each on its own.
896
897
898\section{Solvers and Syntax}
899\ECL offers several different libraries for handling symbolic and
900numeric constraints. 
901They are the {\em fd} (finite domain) library,
902the {\em range} library,
903the {\em ria} (real number interval) library, and finally the {\em eplex} 
904(MIP) library.
905
906\subsection{The {\em fd} (Finite Domain) Library}
907\label{fd}
908The finite domain library has been used and refined over a 10 year period. 
909As a result it has a great many constraint handling facilities.
910It is best seen as three libraries.
911
912The first is a library for handling symbolic finite domains, with
913values like {\em red, machine\_1} etc.
914The built-in constraints on symbolic finite domain variables are
915equations and 
916disequalities: these constraints can only hold between expressions which are
917either constants or variables.
918These constraints can also be used when the domains are numeric.
919
920The second is a library for handling integer variables, and
921numerical constraints on those variables.
922The library propagates equations and inequalities between linear expressions. 
923A linear numeric expression is one that can be written in the form
924$Term_1 + Term_2 + \ldots + Term_n$, where each term can, in turn, be
925written $Number$ or $Number * Variable$.
926The number can be positive or negative.
927An example is the expression $3*X + (-4)*Y + 3$ (which we would
928normally write $3*X - 4*Y + 3$).
929
930The third is a library supporting some built-in complex constraints.
931Two examples of such constraints are the \verb0alldistinct0 constraint, which
932constraints a set of variables to take values which are pairwise distinct, and
933the \verb0atmost0 constraint, which constrains at most $N$ variables from a
934given set to take a certain value.
935
936\subsubsection{The {\em fd} Symbolic Finite Domain Facilities}
937In figures \ref{mapconceptual} and \ref{mapdesign}, above,  we showed a
938map colouring problem and its 
939solution.
940The domains associated with the countries were {\em red, green} and
941{\em blue}.
942These were declared as finite domains, with the usual syntax:
943{\em X :: [red, green, blue]}.
944
945The problem could have been modelled using numbers to represent
946colours, so there is no extra power in allowing symbolic finite
947domains as well as numeric ones.  However when developing \ECL
948programs for real problems, it is a very great help to use meaningful
949names so as to distinguish different types of finite domain variables.
950In particular it is crucial during debugging!
951
952figure \pageref{fdsymbolic} illustrates the basic constraints on finite
953domain variables, and predicates for accessing and searching these
954domains.
955\begin{quote}
956\begin{verbatim}
957
958[eclipse 1]: lib(fd).
959*       fd loaded
960
961[eclipse 2]:  X::[a,b,c].
962*       X = X{[a, b, c]}
963*       yes.
964
965[eclipse 3]: X::[a, 3.1, 7].
966*       X = X{[3.0999999, 7, a]}
967*       yes.
968
969[eclipse 4]: X::[a,b,c], dom(X,List).
970*       X = X{[a, b, c]}
971*       List = [a, b, c]
972*       yes.
973
974[eclipse 5]: X::[a,b,c], Y::[b,c,d], X#=Y.
975*       X = X{[b, c]}
976*       Y = X{[b, c]}
977*       yes.
978
979[eclipse 6]: X::[a,b,c], X##b.
980*       X = X{[a, c]}
981*       yes.
982
983
984[eclipse 7]: X::[a,b,c], indomain(X).
985*       X = a     More? (;) 
986*       X = b     More? (;) 
987*       X = c
988*       yes.
989
990[eclipse 8]: [X,Y,Z]::[a,b,c], X##Y, Y##Z, X##Z, labeling([X,Y,Z]).
991*       X = a
992*       Y = b
993*       Z = c     More? (;) 
994
995*       X = a
996*       Y = c
997*       Z = b     More? (;) 
998*       yes.
999
1000[eclipse 9]:  [X,Z]::[a,b,c], Y::[a,c], 
1001              deleteff(Var,[X,Y,Z],Rest), indomain(Var).
1002
1003*       X = X{[a, b, c]}
1004*       Y = a
1005*       Z = Z{[a, b, c]}
1006*       Rest = [X{[a, b, c]}, Z{[a, b, c]}]
1007*       Var = a     More? (;) 
1008*       yes.
1009
1010\end{verbatim}
1011{\bf Using Symbolic Finite Domains}
1012\label{fdsymbolic}
1013\end{quote}
1014
1015The second query
1016associates a symbolic finite domain with the variable $X$.
1017In response \ECL prints out the variable name and its newly assigned
1018domain. 
1019The fact that the variable has an associated domain does not require
1020any changes in other parts of the program, where $X$ may  be treated as
1021an ordinary variable.
1022
1023Query 3 shows that symbolic domains can include values of different
1024types.
1025
1026Query 4 shows the use of the {\em dom} predicate to  retrieve the
1027domain associated with a variable. 
1028
1029Queries 5 and 6 illustrate the equality and disequality constraints,
1030and their effects on the domains of the variables involved.
1031Finite domain constraints use a special syntax to make
1032explicit which constraint library is to handle the constraint, for
1033example it uses \verb0#=0 instead of \verb0=0.
1034
1035Queries 7, 8 and 9 illustrate search.
1036Strictly one would not expect search predicates to belong to a
1037constraint library, but in fact search and constraint propagation are
1038closely connected.
1039
1040Query 7 shows the {\em indomain} predicate instantiating a domain
1041variable $X$ to a value in its domain.
1042\ECL asks if more answers are required, and when the user does indeed
1043ask for more, another value from the domain of $X$ is chosen, and $X$
1044is instantiated to that value instead.
1045When the user asks for more again, $X$ is instantiated to the third
1046and last value in its domain, and this time \ECL doesn't offer the
1047user any further choices, but simply outputs {\em yes}.
1048 
1049Query 8 illustrates the built-in finite domain {\em labeling}
1050predicate.
1051This predicate simply invokes {\em indomain} on each variable in turn
1052in its argument.
1053In this case it calls {\em indomain} first on $X$, then $Y$ and then
1054$Z$.
1055However the variables are constrained to take different values by
1056three disequality constraints, and only those labelings that satisfy
1057the constraints are admitted.  Consequently this query has six
1058different answers, though the user stops asking for more after the
1059second answer.
1060
1061Query 9 illustrates a heuristic based on the {\em fail first}
1062principle.
1063In choosing the next decision to make, when solving a problem, it is
1064often best to make the choice with the fewest alternatives first.  
1065The predicate {\em deleteff} selects a variable from a set of
1066variables which has the fewest alternatives: i.e. the smallest finite
1067domain.
1068In the example there are three variables, $X$, $Y$ and $Z$
1069representing three decisions, {\em deleteff} picks out $Y$ because it
1070has the smallest domain, and then {\em indomain} selects a value for
1071$Y$.
1072The third argument of {\em deleteff} is an output argument: 
1073{\em Rest} returns the remaining variables after the selected one has
1074been removed.  These are the decisions yet to be made.
1075
1076\subsubsection{The {\em fd} Integer Arithmetic Facilities}
1077For numeric finite domains the {\em fd} library admits equations,
1078inequalities and disequalities over numeric expressions.
1079
1080Additionally the {\em fd} library includes some built-in optimisation
1081predicates. 
1082These are all illustrated below.
1083\begin{quote}
1084\begin{verbatim}
1085[eclipse 1]: lib(fd).
1086*       fd loaded
1087
1088[eclipse 2]: X::1..10.
1089*       X = X{[1..10]}
1090*       yes.
1091
1092[eclipse 3]: X::1..10, mindomain(X,Min).
1093*       X = X{[1..10]}
1094*       Min = 1
1095*       yes.
1096
1097[eclipse 4]: [X,Y]::1..10, X#>Y+1.
1098*       X = X{[3..10]}
1099*       Y = Y{[1..8]}
1100*       yes.
1101
1102[eclipse 5]: [X,Y]::1..10, X#>Y+1, Y#=6.
1103*       X = X{[8..10]}
1104*       Y = 6
1105*       yes.
1106
1107[eclipse 6]: [X,Y,Z]::1..10, X #= 2*(Y+Z).
1108*       X = X{[4..10]}
1109*       Y = Y{[1..4]}
1110*       Z = Z{[1..4]}
1111*       yes.
1112
1113[eclipse 7]: X::1..10, mindomain(X,Min).
1114*       X = X{[1..10]}
1115*       Min = 1
1116*       yes.
1117
1118[eclipse 8]: [X,Y,Z]::1..10, X #= 2*(Y+Z), Y##Z, 
1119        minimize(labeling([X,Y,Z]),X).
1120*       Found a solution with cost 6
1121*       Y = 2
1122*       Z = 1
1123*       X = 6
1124*       yes.
1125
1126\end{verbatim}
1127{\bf Numeric Finite Domains}
1128\label{fdnum}
1129\end{quote}
1130
1131Query 2 illustrates how a numeric finite domain can be initialised
1132just by giving lower and upper bounds, instead of the whole list of
1133members.
1134In fact, internally, finite domains are stored as lists of intervals
1135(for example {\em [1..5, 8..10, 15]}).
1136
1137Query 3 shows how the user can find out the lower bound of a
1138variable's numeric finite domain. 
1139There is a similar predicate for retrieving the upper bound.
1140
1141Queries 4, 5 and 6 illustrate some features of finite domain constraint
1142propagation.
1143
1144Query 4 shows the pruning achieved by a simple numerical finite
1145domain constraint.
1146Notice that both the domains of $X$ and $Y$ are pruned - constraints
1147work in all directions!
1148
1149Query 5 illustrates that a finite domain constraint remains active
1150even after it has achieved some pruning.  
1151This query is the same as query 3, with an extra constraint imposed
1152subsequently. 
1153The $X \verb0#0 >Y+1$ constraint is still active, and prunes the
1154domain of $X$ still further from {\em [3..10]} to {\em [8..10]}.
1155
1156Query 6 shows that, in the interest of computational efficiency, the
1157mathematical constraints only narrow the bounds of the finite domains.
1158In this example the domain of $X$ could theoretically be reduced to
1159{\em [4,6,8,10]}, but this would require much more computation -
1160especially if the finite domains were quite large!
1161
1162Query 7 is an example of the use of the built-in {\em minimize}
1163predicate.
1164This predicate returns an admissible labeling of the variables $X$,
1165$Y$ and  $Z$ which yields the smallest value for $X$.
1166In general any search procedure can be substituted for {\em
1167labeling([X,Y,Z])} as the first argument to {\em minimize}.
1168For example we could have used 
1169{\em minimize( (indomain(X), indomain(Y), indomain(Z)), X)}.
1170
1171\subsubsection{The {\em fd} Complex Constraints}
1172There are two motivations for supporting complex constraints.
1173One is to simplify problem modelling.
1174It is shorter, and more natural, to use a single $alldistinct$
1175constraint on $N$ variables than to use $n*(n-1)/2$ (pairwise)
1176disequalities!
1177
1178The second motivation is to achieve specialised constraint propagation
1179behaviour.
1180The \verb0alldistinct0 constraint on $N$ variables, has the same
1181semantics as $n*(n-1)/2$ (pairwise) disequalities, but it can also achieve better
1182propagation than would be possible with the disequalities.
1183For example if any $M$ of the variables have the same domain, and its size is
1184less 
1185than $M$, then the \verb0alldistinct0 constraint can immediately fail.
1186However if two variables $X$ and $Y$ have the same domain, with $M>1$
1187elements, the constraint {\em X \verb0##0 Y} can achieve no
1188propagation at all.
1189Thus the pairwise disequalities are unable to achieve the same
1190propagation as the $alldistinct$ constraint.
1191
1192
1193The constraint $atmost(Number, List, Val)$ constrains atmost $Number$
1194of the variables in the list $List$ to take the value $Val$.
1195This is a difficult constraint to express using logic.
1196One way is to constrain each sublist of length $Number+1$ to contain a
1197variable with value different from $Val$, but the resulting number of
1198constraints can be very large!
1199
1200A more natural way is to constrain all the variables to take a value
1201different from $Val$, and to allow the constraint to be violated up to
1202$N$ times.
1203The {\em fd} library supports such a facility with the constraint
1204{\em \verb0#=0(T1, T2, B)}.
1205This constraint makes $B=1$ if $T1=T2$ and $B=0$ otherwise.
1206It is possible to express {\em atmost} by imposing the constraint
1207$\verb0#=0(Var_i,Val,B_i)$ for each variable $Var_i$ in the list and
1208then adding the constraint
1209$B_1 + \ldots + B_m \verb0#<=0 N$.
1210The built-in {\em atmost} constraint is essentially implemented like
1211this. 
1212
1213The other {\em fd} constraints (\verb0#<0, \verb0#>0, etc.) can be
1214extended with an extra {\em 0/1} variable in the same way.
1215
1216The {\em fd} library includes a great variety of facilities, which
1217are best explored by obtaining the \ECL extensions manual
1218\cite{eclipseext} and looking at the
1219programming examples in the section on the {\em fd} library there.
1220
1221
1222\subsection{The {\em range} Library}
1223The range library does very little itself, but it provides a common basis for
1224the interval and the MIP libraries.
1225By contrast with the finite domain library, the {\em range} library
1226admits ranges whose lower and upper bound are either real numbers or
1227integers.
1228The library enables the programmer to associate a range with one or
1229more variables, as illustrated below.
1230\begin{quote}
1231\begin{verbatim}
1232[eclipse 1]: lib(range).
1233*       range loaded
1234
1235[eclipse 2]: X::0.0..9.5, lwb(X,4.5).
1236*       X = X{4.5 .. 9.5}
1237*       yes.
1238[eclipse 3]: X::4.5..9.5, X=6.0.
1239*       X = 6.0
1240*       yes.
1241[eclipse 4]: X::4.5..9.5, X=1.0.
1242*       no (more) solution.
1243[eclipse 5]: X::0.0..9.5, lwb(X,4.5), integers([X]).
1244*       X = X{5 .. 9}
1245*       yes.
1246\end{verbatim}
1247{\bf Example Queries Using the {\em range} Library}
1248\label{rangefig}
1249\end{quote}
1250
1251In query 2, the programmer enters
1252\verb7X::0.0..9.5, lwb(X,4.5).7,
1253and the system responds by printing out the resulting range.
1254When the variable is instantiated, the range is checked for
1255compatibility, as shown by queries 3 and 4.
1256
1257Finally, what might be treated as type information in other
1258programming paradigms, can be treated as a constraint in the
1259constraint programming paradigm.
1260Thus we can add a constraint that something is an integer in the
1261middle of a program, as shown by query 5.
1262
1263
1264\subsection{The {\em ria} (Real Interval Arithmetic) Library}
1265The {\em ria} library supports numeric constraints which may involve
1266several variables. 
1267Throughout program execution, {\em ria} continually narrows
1268the ranges associated with the variables as far as possible based on
1269these constraints.
1270In other words {\em ria} supports propagation of intervals, using the
1271range library to record the current ranges, and to detect
1272inconsistencies. 
1273
1274The constraints handled by {\em ria} are equations and inequalities
1275between numerical expressions. 
1276The expressions can be quite complex, they can include polynomials and
1277trigonometrical functions.
1278This functionality is quite similar to that offered by {\em fd},
1279except that {\em fd} can only propagate linear constraints.
1280On the other hand, the finite domain library uses integer arithmetic
1281instead of real number 
1282arithmetic, so it is in general more efficient than {\em ria}.
1283
1284We shall confine ourselves here to a single example showing {\em ria}
1285at work.
1286
1287Suppose we wish to build a garden house, whose corners
1288must lie on a given circle.  The house should be a regular polygon,
1289but may have any number of sides.
1290It should be as large as possible within these limitations.
1291(Note that the more sides the larger the area covered, until it covers
1292practically the whole of the circle.)
1293However each extra side incurs a fixed cost.
1294The problem is to decide how many sides the garden house should have?
1295
1296If it had six sides, the house would look as illustrated in figure
1297\ref{house}. 
1298
1299\begin{figure}
1300%\vspace{0.5cm}
1301
1302%\epsfbox{house.ps}
1303\includegraphics{house.ps}
1304
1305\caption{The Garden House}
1306%\vspace{0.5cm}
1307\label{house}
1308\end{figure}
1309
1310The area of the house is {\em 2*6*A} where {\em A} is the area of the triangle
1311in the illustration.  The area of an N-sided house can be modelled in
1312\ECL as shown below.
1313\begin{quote}
1314\begin{verbatim}
1315:- lib(ria).
1316
1317area(N,Rad,Area) :-
1318        X*>=0, Y*>=0, N*>=3, integers(N),
1319        Rad *>=0, Area*>=0,
1320        Area *=< pi*sqr(Rad),
1321        cos(pi/N) *= Y/Rad,
1322        sqr(Y)+sqr(X) *= sqr(Rad),
1323        Area *= N*X*Y.
1324
1325cost(N,Rad,W1,W2,Cost) :-
1326        W1*>=1, W2*>=1, Cost *>=0,
1327        area(N,Rad,Area),
1328        Cost *= W1*Area-W2*N.
1329
1330tcost(N,Cost) :-
1331        cost(N,10,1,10,Cost).
1332
1333\end{verbatim}
1334{\bf The Area, and Cost-Benefit, of a Garden House}
1335\label{housearea}
1336\end{quote}
1337$N$ is the number of sides, and $Area$ is the area of the house.
1338The variable $Rad$ denotes the radius of the circle, and $X$ and $Y$
1339are the lengths of the sides of the 
1340triangle, as illustrated in 
1341figure \ref{house}.
1342
1343{\em ria} requires its constraints to be written with a specific
1344syntax (eg \verb0X *>= Y0 instead of just \verb0X >= Y0).
1345This distinguishes {\em ria} constraints from linear and finite domain
1346constraints, which each have their own special syntax.
1347
1348To work out the payoff between the area and the number of sides, we
1349define the cost of the house to be $W1*Area-W2*N$, where $W1$ and $W2$
1350are weighting factors that we can chose to reflect the relative costs
1351and benefits of the area against the number of sides.  
1352In the model shown in figure \pageref{housearea}, $tcost$ returns the
1353cost-benefit of an N-sided house
1354in case the radius of the circle is 10, and the weights are $W1=1$
1355and $W2=10$.
1356
1357We can place this program in a file called {\em house.pl}, and then use
1358\ECL to find out some costs by first ``consulting'' the file, as
1359illustrated below, query 1.
1360
1361\begin{quote}
1362\begin{verbatim}
1363[eclipse 1]: [house].
1364*       range  loaded 
1365*       house.pl   compiled
1366*       yes.
1367
1368[eclipse 2]: tcost(3,C).
1369*       C = C{99.90 .. 99.91}
1370*       yes.
1371[eclipse 3]: tcost(4,C).
1372*       C = C{159.9 .. 160.0}
1373*       yes
1374[eclipse 4]: tcost(6,C).
1375*       C = C{199.8 .. 199.9}
1376*       yes.
1377[eclipse 5]: tcost(7,C).
1378*       C = C{203.6 .. 203.7}
1379*       yes.
1380[eclipse 6]: tcost(8, C).
1381*       C = C{202.8 .. 202.9}
1382*       yes.
1383
1384[eclipse 7]: tcost(N,C).
1385*       N = N{3 .. 31}
1386*       C = C{0.0 .. 284.2}
1387*       yes.
1388[eclipse 8]: tcost(N,C), squash([C],1e-2,lin).
1389*       N = N{3 .. 31}
1390*       C = C{0.0 .. 224.2}
1391\end{verbatim}
1392{\bf Finding the Optimal Shape for a Garden House}
1393\label{housequeries}
1394\end{quote}
1395
1396Queries 2-6, above, would seem to indicate
1397that the seven sided house is best for the given cost
1398weightings.\footnote{The intervals returned from {\em ria} are much
1399narrower than this, but for this paper we have reduced the output to
1400three significant figures.}
1401
1402However it is also interesting to see whether the interval reasoning
1403system itself can achieve useful propagation without even knowing the
1404number of sides of the house.
1405We show this in query 7.
1406
1407An upper bound on the number of sides is extracted due to the
1408constraint that the cost-benefit must be positive,
1409but the propagation on the cost-benefit is rather weak. 
1410In cases like this, propagation can be augmented by a technique known
1411as squashing, as 
1412illustrated in query 8.
1413
1414We now give two short examples showing limitations
1415of interval reasoning in general.
1416This will motivate the introduction of a linear constraint solver in
1417\ECL, described in section \ref{eplex}.
1418
1419The two limitations are that interval reasoning cannot, even in some
1420quite simple examples, detect inconsistency among the constraints; and
1421in cases where the constraints have only one solution, interval
1422reasoning often fails to reflect this in the results of propagation. 
1423
1424This is illustrated by a couple of simple examples as follows. 
1425\begin{quote}
1426\begin{verbatim}
1427[eclipse 1]: lib(ria).
1428*       ria loaded
1429
1430[eclipse 2]: [X,Y,Z]:: -100.0..100.0, 
1431             X+Y *=<1, Z+X*=<1, Y+Z*=<1, X+Y+Z*>=2.
1432*       X = X{-100.1 .. 100.1}
1433*       Y = Y{-100.1 .. 100.1}
1434*       Z = Z{-100.1 .. 100.1}
1435*       yes
1436
1437[eclipse 3]: [X,Y]:: -100.0 .. 100.0, X+Y *= 2, X-Y *= 0.
1438*       X = X{-98.1 .. 100.1}
1439*       Y = Y{-98.1 .. 100.1}
1440*       yes
1441\end{verbatim}
1442{\bf Poor Interval Propagation Behaviour}
1443\label{badria}
1444\end{quote}
1445In this case the system failed to detect the
1446inconsistency in query 2, and did not deduce that only one value
1447was possible for $X$ and $Y$ in query 3.
1448The answer is not incorrect, as $ria$ only guarantees that any
1449possible answers must lie in the intervals returned - it does not
1450guarantee the existence of an answer in that interval.
1451Nevertheless it would be useful to have available a more powerful
1452solver to recognise cases such as these.
1453
1454\subsection{The {\em eplex} (External CPLEX Solver Interface) Library}
1455\label{eplex}
1456Equations and inequalities between linear numeric expressions, as
1457defined in section \ref{fd} above, are a
1458subset of the constraints which can be handled by {\em ria}.
1459However this class can be handled very powerfully, so much so that any
1460inconsistency between the constraints is guaranteed to be detected.
1461Techniques for solving linear constraints have been at the heart of
1462operations research for half a century, and highly efficient linear
1463solvers have been developed.
1464
1465One of the most widely distributed, scaleable and efficient packages
1466incorporating a linear constraint solver is the CPLEX MIP package
1467\cite{CPLEX}. 
1468CPLEX offers several algorithms for solving linear constraints
1469including the Simplex and Dual Simplex algorithms.
1470These algorithms are supported by sophisticated data structures, and
1471the package can handle problems involving ten of thousands of linear
1472constraints over ten of thousands of variables. 
1473
1474In the discussion so far, we have not yet mentioned an important
1475aspect of most industrial combinatorial problems.
1476Not only is it required to make decisions that satisfy the
1477constraints, but they must also be chosen to optimise some measure.
1478In the travelling salesman problem for example, the decisions of what
1479order to visit the cities are based on optimising the total distance
1480travelled by the salesman.
1481
1482One feature of available packages for solving linear and mixed integer
1483problems, is support for optimisation.
1484figure \pageref{transportation} is a design model for a transportation
1485problem, which uses the {\em eplex} library to pass the constraints
1486to the CPLEX package.
1487
1488\begin{quote}
1489\begin{verbatim}
1490:- lib(eplex).
1491
1492main(Cost, Vars) :-
1493
1494        % Transportation problem:  clients A,B,C,D, plants 1,2,3.
1495        % Variables represent amount delivered from plant to client.
1496
1497        Vars = [A1, B1, C1, D1, A2, B2, C2, D2, A3, B3, C3, D3],
1498        Vars :: 0.0..10000.0,                   % variables
1499
1500        A1 + A2 + A3 $= 200,            % client demand constraints
1501        B1 + B2 + B3 $= 400,
1502        C1 + C2 + C3 $= 300,
1503        D1 + D2 + D3 $= 100,
1504
1505        A1 + B1 + C1 + D1 $=< 500,      % plant capacity constraints
1506        A2 + B2 + C2 + D2 $=< 300,
1507        A3 + B3 + C3 + D3 $=< 400,
1508
1509        optimize(min(                   % solve minimizing
1510            10*A1 + 7*A2 + 11*A3 +      % transportation costs
1511             8*B1 + 5*B2 + 10*B3 +
1512             5*C1 + 5*C2 +  8*C3 +
1513             9*D1 + 3*D2 +  7*D3), Cost).
1514
1515\end{verbatim}
1516{\bf A Design Model for a Transportation Problem}
1517\label{transportation}
1518\end{quote}
1519Note that, where {\em fd} uses a \verb0#0 and {\em ria} uses a \verb0*0, {\em
1520eplex} uses a \verb0$0.
1521
1522The answer returned by \ECL is
1523\begin{quotation}
1524C = 6600.0
1525V = [0.0, 200.0, 300.0, 0.0, 0.0, 200.0, 0.0, 100.0, 200.0, 0.0, 0.0, 0.0]
1526\end{quotation}
1527
1528In fact MIP packages typically not only offer optimisation as a
1529facility, they insist on an optimisation function in the design model.
1530Therefore in illustrating the two examples where {\em ria} performed
1531badly, using instead the {\em eplex} library, we shall
1532insert a dummy optimisation function!
1533The use of {\em eplex} on these examples is shown in figure
1534\ref{goodeplex}. 
1535
1536\begin{quote}
1537\begin{verbatim}
1538[eclipse 1]: lib(eplex).
1539*       eplex loaded
1540
1541[eclipse 2]: X+Y $=< 1, Z+X $=< 1, Y+Z $=< 1, X+Y+Z $>= 2,
1542             Opt $= 0, optimize(min(Opt),Cost).
1543*       no (more) solution.
1544
1545[eclipse 3]: X+Y $= 2, X-Y $= 0, optimize(min(X),Cost).
1546*       X = 1.0
1547*       Y = 1.0
1548*       Cost = 0.0
1549*       yes.
1550
1551[eclipse 4]: X+Y $= 2, X-Y $= 0, optimize(max(X),Cost).
1552*       X = 1.0
1553*       Y = 1.0
1554*       Cost = 0.0
1555*       yes.
1556
1557\end{verbatim}
1558{\bf Linear Constraint Solving}
1559\label{goodeplex}
1560\end{quote}
1561
1562
1563Query 2 is the same set of constraints whose inconsistency is not
1564detected by {\em ria}.
1565{\em eplex}, however, recognises their inconsistency.
1566
1567In order to establish that there is only one possible value for $X$ we
1568have had to use two queries, 3 and 4, first finding the minimum value
1569for $X$ and then the maximum.
1570Although the same value for $Y$ was returned in both solutions, the
1571{\em eplex} library has still not proved that $1$ is the only
1572possible value for $Y$.
1573
1574For problems involving only real number (or {\em continuous}) variables,
1575linear constraint solving alone suffices to solve the problem.
1576However industrial problems typically include a mixture of real number
1577and integer variables.
1578For example in problems involving discrete resources the
1579decision as to 
1580which resource to use for a task cannot be modelled as a continuous
1581variable. 
1582Traditionally operational researchers will use a binary (or {\em 0/1})
1583variable or an integer variable.
1584Most resources are discrete, for example machines for jobs, vehicles
1585for deliveries, rooms for meetings, berths for ships, people for
1586projects, and so on.
1587Another fundamental use of discrete variables is in modelling the
1588decision as to which order to do things in - for example visiting
1589cities in the travelling salesman problem, or performing tasks on the
1590same machine.
1591
1592>From the point of view of the programmer, adding the constraint that a
1593variable is integer-valued is straightforward.
1594However the effect of such a constraint on the performance of the
1595solver can be disastrous, because mixed integer problems are much
1596harder to solve than linear problems!
1597\begin{quote}
1598\begin{verbatim}
1599[eclipse 1]: lib(eplex).
1600*       eplex loaded
1601
1602[eclipse 2]: X+Y $>= 3, X-Y $= 0, optimize(min(X), C).
1603*       Y = 1.5
1604*       X = 1.5
1605*       C = 1.5
1606*       yes.
1607
1608[eclipse 3]: integers([X]), X+Y $>= 3, X-Y $= 0, optimize(min(X), C).
1609*       Y = 2.0
1610*       X = 2
1611*       C = 2.0
1612*       yes.
1613
1614\end{verbatim}
1615{\bf Mixed Integer Programming}
1616\label{mixint}
1617\end{quote}
1618
1619
1620The  {\em eplex} library uses standard range-variables provided by the
1621range-library, 
1622which facilitates interfacing to other solvers. The interface to CPLEX
1623enables 
1624state information to be retrieved,  such as constraint slacks, basis
1625information,  and reduced costs. 
1626Also many parameters can be queried and modified.
1627A quite generic solver demon is provided which makes it easy to use
1628CPLEX   
1629within a data-driven CLP setting. 
1630
1631The notion of solver handles encourages experiments with
1632multiple solvers. 
1633A pair of predicates make it possible to read and write problem files in MPS
1634or LP format.  
1635
1636MIP packages such as CPLEX and XPRESS , which is also
1637currently being
1638integrated into the {\em eplex} package, are surprisingly effective
1639even for some problems involving many discrete variables.
1640Their underlying linear solvers reflect a carefully chosen balance
1641between flexibility and scaleability.
1642They offer less flexibility than the linear solvers which are usually
1643built 
1644into constraint programming systems, such as $CLP(R)$, but much better
1645scaleability. 
1646
1647It has proved possible, within \ECL, to achieve much of the
1648flexibility of $CLP(R)$ within the restrictions imposed by
1649MIP solvers \cite{clpmip}.
1650
1651
1652\section{Complex Constraints}
1653\label{complexconstraints}
1654Whilst constraint programming languages offer a broad selection of
1655built-in constraints, each new industrial application typically
1656requires a number of application-specific constraints which aren't
1657among the built-in constraints.
1658
1659Let us take, as an ongoing example, the constraint that two tasks sharing a
1660single resource cannot be done at the same time.
1661This constraint was introduced in section \ref{mathconceptual}
1662above. 
1663
1664The constraint involves six variables: the start times $S_1, S_2$, end times
1665$E_1, E_2$ and resources $R_1, R_2$ of the two tasks.
1666The specification of this constraint is as follows:
1667\begin{quotation}
1668{\em either} the two tasks use distinct resource ($ R_1 \ \  ne \ \ R_2$)
1669{\em or} task$_1$ ends before task$_2$ starts ($E_1 \leq S_2$)
1670{\em or else} task$_2$ ends before task$_1$ starts ($ E_2 \leq S_1$).
1671\end{quotation}
1672We shall compare three different ways of handling this constraint.
1673
1674First we recall how it can be encoded using numerical equations
1675and inequalities, together with integer constraints. 
1676This is the encoding necessary to allow it to be solved using MIP
1677algorithms, as available through the {\em eplex} library.
1678However the MIP package is not necessarily the
1679best algorithm for handling such a constraint.
1680
1681Indeed experience with practical applications suggests that the
1682more {\em 0/1} variables it is necessary to introduce to handle each
1683constraint, the less
1684efficient MIP becomes.
1685The inefficiency comes partly because the MIP constraints are handled
1686globally, and the cost of handling extra constraints and boolean
1687variables increases very fast with their number.\footnote{Using the
1688Simplex or Dual Simplex algorithms this cost goes up, in the worst
1689case, exponentially with the number of constraints and variables.}
1690It also comes because, until the boolean variables take a value very
1691close to $0$ they have very little effect on the
1692search.\footnote{Technically they are rarely facet-inducing cuts.}
1693
1694By contrast we shall show how it can be handled using two further
1695libraries of \ECL - the {\em propia} library and the {\em chr}
1696library.
1697These libraries allow the constraint to be modelled much more simply
1698and handled more efficiently.
1699
1700
1701\subsection{The {\em propia} (Generalised Propagation) Library}
1702\label{propia}
1703A major issue in defining complex constraints is how to handle
1704disjunction.
1705The resource constraint of our running example can be quite easily
1706expressed using a disjunction of finite domain constraints.
1707Indeed \ECL allows us to express disjunction as alternative clauses
1708defining a predicate, so the constraint can be expressed as a single
1709\ECL predicate thus:
1710\begin{verbatim}
1711fdTaskResource(S1,E1,R1,S2,E2,R2) :- 
1712        R1 ## R2.
1713fdTaskResource(S1,E1,R1,S2,E2,R2) :-
1714        R1#=R2, S1 #>= E2.
1715fdTaskResource(S1,E1,R1,S2,E2,R2) :-
1716        R1#=R2, S2 #>= E1.
1717\end{verbatim}
1718
1719The purpose of the {\em propia} library is to take exactly such
1720disjunctive definitions and turn them into constraints!
1721
1722This is illustrated below.
1723\begin{quote}
1724\begin{verbatim}
1725:- lib(propia).
1726
1727propiaTR(S1,R1,S2,R2) :-
1728        [S1,S2]::0..100, [R1,R2]::[r1,r2,r3],
1729        E1 = S1+50,  E2 = S2+70,
1730        fdTaskResource(S1,E1,R1,S2,E2,R2) infers most.
1731
1732fdTaskResource(S1,E1,R1,S2,E2,R2) :- 
1733        R1 ## R2.
1734fdTaskResource(S1,E1,R1,S2,E2,R2) :-
1735        R1#=R2, S1 #>= E2.
1736fdTaskResource(S1,E1,R1,S2,E2,R2) :-
1737        R1#=R2, S2 #>= E1.
1738\end{verbatim}
1739{\bf Specifying a Resource Contention Constraint in \ECL}
1740\label{nolapinfersmost}
1741\end{quote} 
1742The syntax {\em Goal infers most} turns any \ECL goal into a constraint.
1743It is supported by the {\em propia} library.
1744
1745The behaviour of this constraint is to find which values for each
1746variable are consistent with the constraint.
1747The constraint has the propagation behaviour described in
1748\cite{consprog}: 
1749it repeatedly attempts to reduce the domains of its variables further
1750every time 
1751any other constraints reduce any of these domains.
1752figure \pageref{nolapbehaviour} shows some examples of this behaviour.
1753In query 2, the constraint deduces that, since the tasks cannot
1754overlap, task$_1$ cannot start between $51$ and $69$, and task$_2$ cannot
1755start between $31$ and $49$.
1756In query 3, since the tasks are bound to overlap, the constraint
1757deduces that task$_2$ must use either resource $r_2$ or $r_3$.
1758
1759\begin{quote}
1760\begin{verbatim}
1761[eclipse 1]: [fdTaskResource].
1762*     propia     loaded
1763*     fdTaskResource.pl compiled.
1764*     yes.
1765
1766[eclipse 2]: propiaTR(S1, R1, S2, R2), R1#=r1, R2#=r1.
1767*     S1 = S1{[0..50, 70..100]}
1768*     R1 = r1,
1769*     S2 = S2{[0..30, 50..100]}, 
1770*     R2 = r1
1771*     yes.
1772
1773[eclipse 3]: propiaTR(S1,R1,S2,R2), R1=r1, S2#>=35, S2#<=45.
1774*     S1 = S1{[0..100]}
1775*     R1 = r1,
1776*     S2 = S2{[35..45]}, 
1777*     R2 = R2{[r2, r3]}
1778*     yes.
1779
1780\end{verbatim}
1781{\bf The Behaviour of {\em Goal infers most}}
1782\label{nolapbehaviour}
1783\end{quote} 
1784
1785Other behaviour can be achieved by writing {\em Goal infers
1786consistent}
1787or {\em Goal infers ground} instead.
1788These behaviours, together with other facilities of the {\em propia}
1789library are described in the \ECL extensions manual \cite{eclipseext}.
1790
1791\subsection{The {\em chr} (Constraint Handling Rules) Library}
1792The \ECL programmer has little control over the behaviour of complex
1793predicates using the {\em propia} library.
1794For example in the fdTaskResource query 2, illustrated in figure
1795\ref{nolapbehaviour}, the constraint detects
1796``holes'' inside the domains of the variables $S1$ and $S2$.
1797However experience in solving scheduling problems suggests that the
1798computational effort expended in 
1799detecting such holes is rarely compensated by any reduction the amount of
1800search necessary to find solutions.
1801Whilst this propagation is too powerful, the other alternatives
1802available in the  {\em propia} library are too weak.
1803
1804The most useful behaviour for the constraint is to do nothing until
1805one of the following conditions hold:
1806\begin{itemize}
1807\item
1808If the tasks are guaranteed to overlap, constrain them to use distinct
1809resources
1810\item
1811If the tasks must use the same resource, and one of the tasks cannot
1812precede the other, constrain that task not to start until the other
1813task has ended.
1814\end{itemize}
1815Notice that this is, unfortunately,  not the behaviour achieved by the
1816MIP encoding, either!
1817
1818This behaviour can be expressed in \ECL using the Constraint Handling
1819Rules {\em chr} library.
1820The required \ECL encoding remains quite logical, but it needs a new
1821concept, that of a {\em guard}.
1822A rule with a guard is not executed until its guard is entailed, until
1823then it does nothing.
1824The data-driven implementation of guarded rules uses the same
1825mechanisms as the
1826data-driven implementation of constraints discussed in the following
1827section.
1828
1829The syntax for guarded rules is rather different from the syntax for
1830\ECL clauses encountered so far.
1831This syntax is illustrated by the encoding of the tsakResources
1832constraint in figure \pageref{chrtaskResources}.
1833In this example the constraint handling rules use finite domain
1834constraints in their definitions.
1835
1836\begin{quote}
1837\begin{verbatim}
1838
1839chrTR(S1,R1,S2,R2) :-
1840        [S1,S2]::0..100, [R1,R2]::[r1,r2,r3],
1841        E1 = S1+50,  E2 = S2+70,
1842        chrTaskResource(S1,E1,R1,S2,E2,R2).
1843
1844constraints chrTaskResource/6.
1845
1846chrTaskResource(S1,E1,R1,S2,E2,R2) <==> 
1847        R1 #= R2, E1 #> S2   |   E2 #<= S1.
1848chrTaskResource(S1,E1,R1,S2,E2,R2) <==> 
1849        R1 #= R2, E2 #> S1   |   E1 #<= S2.
1850chrTaskResource(S1,E1,R1,S2,E2,R2) <==> 
1851        E1 #> S2, E2 #> S1   |   R1 ## R2.
1852\end{verbatim}
1853{\bf Constraint Handling Rules for the Task Resources Constraint}
1854\label{chrtaskResources}
1855\end{quote} 
1856
1857Logically each of these three rules states the same constraint: either
1858$R1 \neq R2$ or $S2 \geq E1$ or $S1 \geq E2$.
1859However each rule uses a different ``if...then'' statement.
1860For example the first rule says that if $R1=R2$ and $E1>S2$ then $S1
1861\geq E2$.
1862
1863In order to use constraint handling rules, it is necessary to
1864translate them into the underlying \ECL language using an automatic
1865translator.
1866The constraints must be written to a file called {\em file.chr} - in
1867our example we shall use {\em chrTaskResource.chr}.
1868To illustrate the loading and use of constraint handling rules,  we
1869give some example queries below.
1870\begin{quote}
1871\begin{verbatim}
1872[eclipse 1]: lib(chr), lib(fd).
1873*     chr loaded
1874*     fd loaded
1875
1876[eclipse 2]: chr(chrTaskResource).
1877*       chrTaskResource.chr compiled.
1878*       yes.
1879
1880[eclipse 3]: chrTR(S1, R1, S2, R2), R1#=r1, R2#=r1.
1881*       S1 = S1{[0..100]}
1882*       S2 = S2{[0..100]}
1883*       R1 = r1
1884*       R2 = r1
1885*       yes.
1886
1887[eclipse 4]: chrTR(S1, R1, S2, R2), R1=r1, R2=r1, S1#<=65.
1888*       S2 = S2{[50..100]}
1889*       R1 = r1
1890*       R2 = r1
1891*       S1 = S1{[0..50]}
1892*       yes.
1893
1894[eclipse 5]: chrTR(S1,R1,S2,R2), R1=r1, S2#>=35, S2#<=45.
1895*       S1 = S1{[0..100]}
1896*       R2 = R2{[r2, r3]}
1897*       R1 = r1
1898*       S2 = S2{[35..45]}
1899*       yes.
1900
1901\end{verbatim}
1902{\bf The Behaviour of {\em chrTaskResource}}
1903\label{chrnolapbehaviour}
1904\end{quote} 
1905
1906Query 3 yields less propagation than {\em propiaTR} because this
1907implementation does not punch holes in the variables' domains.
1908
1909Query 4 does, however, produce new information, because not only do
1910both tasks use the same resource, but also the constraint $S1 \leq 65$
1911means that task$_1$ must precede task$_2$. 
1912The constraint deduces that the latest start time for $S1$ is actually
1913$50$, and the earliest start time for $S2$ is also (by coincidence)
1914$50$.
1915
1916Query 5 uses the fact that the tasks must overlap to remove $r_1$ from
1917the domain of $R2$.
1918
1919The {\em chr} library offers many more facilities, including
1920multi-headed rules, and augmentation rules.
1921These facilities can be explored in detail by studying the relevant
1922chapter in \cite{eclipseext}, and trying out the example constraint
1923handling rule programs which are distributed with \ECL.
1924
1925\subsection{Explicit Data Driven Control}
1926The {\em propia} and {\em chr} libraries are implemented using a set
1927of underlying facilities in \ECL which support data-driven computation.
1928The main feature supporting data-driven computation is the {\em
1929suspension}. 
1930This is illustrated below.
1931\begin{quote}
1932\begin{verbatim}
1933
1934[eclipse 1]:    lib(fd).
1935*       fd loaded
1936
1937[eclipse 2]:    suspend(writeln("Wake up!"),1,X->inst),
1938                writeln("Do this first"), 
1939                X=1.
1940*       Do this first
1941*       Wake up!
1942*       X = 1
1943*       yes.
1944
1945[eclipse 3]:    suspend(writeln("Wake up!"),1,X->inst),
1946                current_suspension(S),
1947                suspension_to_goal(S,Goal,M),
1948                kill_suspension(S),
1949                call(Goal,M).
1950*       Wake up!
1951*       ...
1952*       yes.
1953
1954[eclipse 4]:    X::1..10,
1955                suspend(writeln("Wake up!"),1,X->min),
1956                X#>3,
1957*       Wake up!
1958*       X = X{[4..10]}
1959*       yes.
1960
1961\end{verbatim}
1962{\bf Handling Suspensions}
1963\label{suspensions}
1964\end{quote}
1965
1966A suspension is a goal that waits to be executed until a
1967certain event occurs.
1968Each suspension is associated with a set of variables, and as soon as
1969a relevant event occurs to any one of the variables in the set, the
1970suspension ``wakes up'' and the goal is activated.
1971One such event is instantiation: all the suspensions on a variable
1972wake up when the variable is instantiated.
1973
1974In figure \pageref{suspensions} the first query loads the {\em fd}
1975library, which will be used in 
1976the last example.  
1977It is preferable to load all the libraries that may be needed at the
1978start of the session.
1979
1980Query 2 suspends a goal {\em writeln(``Wake up!'')} on one variable
1981$X$.  The goal will be executed as soon as $X$ becomes instantiated
1982($ X \rightarrow inst$).  When woken the goal will be scheduled with a
1983certain priority. The priority is given as the second argument of {\em
1984suspend}.  In this case the priority is $1$, which is the highest
1985priority. 
1986The remainder of query 2 performs another write statement and then
1987instantiates $X$. 
1988The output from \ECL shows that the suspended goal is not executed,
1989until $X$ is instantiated, after the system has already output {\em Do
1990this first}.
1991
1992Query 3 shows various facilities for explicitly handling a suspension.
1993The current suspensions can be accessed.
1994(It is also possible to access the
1995just the suspensions on a particular variable.)
1996A suspension can be converted to a goal.\footnote{The variable $M$
1997denotes the module in which $writeln$ 
1998is defined.}
1999A suspension can be ``killed'', so it is no longer accessible or
2000wakeable.
2001The suspension has no connection to the goal, however, which can still
2002be executed.
2003
2004To save space the output of variable values is omitted
2005here.
2006
2007Finally query  illustrates another kind of event that can wake up a
2008suspended goal.
2009In this case the goal is suspended until the lower bound of the finite
2010domain associated with $X$ is tightened ($X \rightarrow min$).
2011
2012There are other events which can wake suspended goals associated with
2013other constraint handlers, but the most general event is that the
2014variable becomes more constrained in {\em any} way (expressed as $X
2015\rightarrow constrained$).
2016Goals suspended in this way will wake when any new constraint on $X$
2017is added (an {\em fd} constraint, a {\em ria} constraint, or an {\em
2018eplex} 
2019constraint. 
2020
2021Finally it is also possible to retrieve goals suspended on a given
2022variable, or those associated with a given event on a given variable.
2023
2024Based on this simple idea it is possible to define a constraint
2025behaviour explicitly.
2026As a simple example let us make a constraint that two variable differ
2027by more than some input number $N$.
2028We will call the constraint {\em ndiff(N,X,Y)}, where $N$ is the
2029difference, and $X$ and $Y$ the two variables.
2030Its behaviour will be to tighten the finite domains of the variables.
2031\begin{quote}
2032\begin{verbatim}
2033:- lib(fd).
2034:- suspend.
2035
2036ndiff(N,X,Y) :-
2037    mindomain(X,XMin),
2038    maxdomain(Y,YMax),
2039    YMax<XMin+N, !,
2040    X#>=Y+N.
2041
2042ndiff(N,X,Y) :-
2043    mindomain(Y,YMin),
2044    maxdomain(X,XMax),
2045    XMax<YMin+N, !,
2046    Y#>=X+N.
2047    
2048ndiff(N,X,Y) :-
2049    suspend(ndiff(N,X,Y),3,[X,Y] -> any).
2050
2051\end{verbatim}
2052{\bf Using Suspensions to Implement Constraints}
2053\label{suspcons}
2054\end{quote}
2055In figure \pageref{suspcons} we implement a behaviour for our {\em ndiff}
2056constraint.
2057Since we use underlying {\em fd} constraints, we load the {\em fd}
2058library.\footnote{The {\em fd} library automatically loads the {\em
2059suspend} library, so it is not actually necessary to load {\em
2060suspend} explicitly.}
2061
2062The first clause for {\em ndiff} checks if the lower bound for $X$ is
2063so close to the upper bound for $Y$, that $X$ cannot be less than $Y$
2064(if it was, then to satisfy the {\em ndiff} constraint we would need to
2065have $Y>=X+N$).
2066In this case it imposes the constraint that $X \verb0#>=0 Y+N$.
2067
2068The second clause does the symmetrical test on the lower bound of $Y$
2069and the upper bound of $X$.
2070
2071If neither of these conditions are satisfied, then {\em ndiff} doesn't
2072do anything.
2073It just suspends itself until the finite domains of $X$ or $Y$ are
2074tightened ($[X,Y] -> any$).
2075
2076This very same mechanism of suspended goals is used to implement all
2077the built-in constraints of \ECL.
2078For example the constraint $X \verb0#>0 Y$ is implemented using a
2079goal which is suspended on two events: 
2080a change in the maximum of the domain of $X$,
2081and a change in the minimum of the domain of $Y$. 
2082Typically all the finite domain built-in constraints are suspended on
2083events which occur to the finite domains of their variables.
2084
2085Before concluding this subsection, we should observe that the
2086different constraint libraries of \ECL are supported by a very
2087flexible facility.
2088The information about each kind of constraint on a variable is held in
2089a data structure which is attached to the variable called an
2090{\em attribute}.
2091When the {\em fd} library is loaded, each variable in \ECL has a
2092finite domain attribute.
2093If the variable has no finite domain, this attribute contains nothing,
2094and the behaviour of the variable is just as if it had no attribute.
2095On the other hand if the variable does have a finite domain, then the
2096attribute stores the finite domain, as well as pointers to all the
2097suspended goals which are waiting for an event to occur to the finite
2098domain.
2099
2100Naturally {\em ria} constraints and {\em eplex} constraints are stored
2101in other attributes, and they have their own suspended goals attached
2102to them.
2103
2104Any \ECL user can define and implement a completely new constraint
2105handling library in three steps.  
2106\begin{enumerate}
2107\item
2108A new attribute storing information about
2109the new class of constraints, must be defined.
2110\item
2111Events specific to this class of constraints must be specified.
2112\item
2113New constraint behaviours must be implemented in terms of
2114goals which suspend themselves on these events.
2115\end{enumerate}
2116The \ECL extensions  manual \cite{eclipseext} gives an example of
2117defining such a new constraint 
2118library. 
2119
2120
2121\section{Search}
2122\label{search}
2123
2124\subsection{Constructive Search}
2125
2126\subsubsection{Branch and Bound}
2127In the preceding sections we have encountered two optimisation
2128procedures, the finite 
2129domain procedure {\em minimize} and the MIP procedure {\em optimize}.
2130Both optimisation procedures implement an algorithm called {\em branch
2131and bound}, which posts a new constraint, each time it finds a
2132solution, that the cost of future solutions must be better than the
2133cost of the current best solution.
2134Eventually the new constraint will be unsatisfiable, and the algorithm
2135will have proved that it has found the optimum.
2136
2137\subsubsection{Depth-First Search and Backtracking}
2138We have also encountered the finite domain search
2139procedure {\em labeling}, which successively instantiates a list of
2140finite domain variables to values in their domains.
2141In \ECL the default search method is depth-first search and
2142backtracking on failure.
2143Of the complete search methods available, this is in practice the best
2144because search algorithms with a breadth-first component quickly grow
2145to occupy too much memory.
2146We will discuss some incomplete search methods below.
2147
2148\subsubsection{{\em Guesses} - Constraints Imposed During Search}
2149Search is, of course, much more general than just labelling.
2150Certainly, for combinatorial problems, it involves making guesses that
2151may later turn out to have been bad.
2152However a guess need not involve guessing a value for a variable, as
2153is done in labelling.
2154For example if a variable $X$ has range {\em [0..100]}, instead of
2155guessing a precise value for $X$, it may be useful to perform a binary
2156chop, first guessing that $X \geq 50$, and then, if the guess turns
2157out to be bad, guessing that $X < 50$.
2158A guess in the most general sense is the posting of a new
2159(non-redundant) constraint which narrows the search space.
2160However there is no guarantee that such a guess does not rule out
2161solutions to the problem, therefore the system must also explore the
2162remainder of the search space on backtracking.
2163Typically this is done by imposing the negation of the constraint.
2164However the negation of an inequality $\geq$ is a strict inequality
2165$<$, which can't be handled by linear programming.
2166However in case $X$ is an integer variable, and $N$ an integer, the
2167negation of $X \geq N$ is $X \leq N-1$ which can be handled.
2168
2169\subsubsection{MIP Search}
2170Finite domain propagation only narrows domains, and does not guarantee
2171to detect all inconsistencies.
2172Thus there is no
2173guarantee that a partial labelling (which assigns consistent values to
2174some of the variables) can always be extended to a complete
2175consistent labelling.
2176However the linear constraint solver available through {\em eplex}
2177does indeed guarantee to 
2178detect all inconsistencies between the linear constraints.
2179On the other hand a linear solver does not take into account the
2180constraint that certain variables can only take integer values, thus
2181it can return proposed solutions in which non-integer values are
2182proposed for integer variables.
2183The linear solver can efficiently find an optimal solution to the
2184problem in which integrality constraints on the variables are ignored.
2185Such an optimum is termed an optimum of the ``continuous relaxation''
2186of the problem, or just the ``relaxed optimum'' for short.
2187  
2188This suggests a different search mechanism, in which a new constraint
2189is added to exclude the non-integer value in the relaxed optimum 
2190returned by the linear constraint solver.
2191If the value for integer variable $X$ was $0.5$ in the relaxed
2192optimum, for example,
2193a new constraint $X \geq 1$ might be added.
2194Since this excludes other feasible solutions such as $X=0$, this new
2195constraint is only a guess, and if it turns out to be a bad guess then
2196the alternative constraint $X \leq 0$ is posted instead.
2197
2198This is the search method used in MIP when {\em optimize} is called in
2199the {\em eplex} library.
2200
2201\subsubsection{Search Heuristics based on Hybrid Solvers {\em fdplex}}
2202MIP search can be duplicated in \ECL by
2203passing the linear constraints to CPLEX and using the proposed
2204solutions to decide which new constraint to impose (i.e. guess) next.
2205Whilst there is little point in precisely duplicating the MIP search
2206control with \ECL, it allows the \ECL programmer to define new search
2207techniques using information from both the {\em fd} library and from
2208{\em eplex}.
2209
2210For example the size of the finite domain, as recorded in the
2211finite domain library, can be used when choosing the next variable on
2212which to guess a constraint.
2213Then the value of this variable in the relaxed optimum returned from
2214{\em eplex} can be used when choosing to which value to label it
2215first. 
2216
2217This search technique is supported by the \ECL library {\em fdplex},
2218and is illustrated below.
2219\begin{quote}
2220\begin{verbatim}
2221:- lib(fdplex).
2222
2223mylabeling([]).
2224mylabeling(Vars) :-
2225        deleteff(Var,Vars,Rest),
2226        indomain(Var),
2227        mylabeling(Rest).
2228
2229
2230solve(X,Y,Z,W) :-
2231        [X,Y]::1..5,
2232        [Z,W]::1..100,
2233        10*Z+7*W+4*X+Y #= 49,
2234        Cost #= Z-2*W+X-2*Y,
2235        minimize(mylabeling([X,Y,Z,W]),Cost).
2236\end{verbatim}
2237{\bf Search with the {\em fdplex} Library}
2238\label{fdplexsearch}
2239\end{quote} 
2240%This example shows an explicit use of \ECL modules, in the goal 
2241%{\em call(indomain(Var), fdplex)} which invokes the version of the
2242%built-in predicate {\em indomain} exported from the {\em fdplex}
2243%library, as opposed to the one we encountered in section \ref{fd}
2244%above, which is exported from the library {\em fd}.
2245The {\em fdplex} version of {\em indomain} selects the value closest
2246to the value at the relaxed optimum returned by {\em eplex}.
2247Indeed it is instructive to watch the search taking place using the
2248\ECL tracing facilities,
2249so we shall load the program of figure \pageref{fdplexsearch} into a file
2250called {\em fdplexsearch.pl}.
2251Now we shall run it as shown below.
2252\begin{quote}
2253\begin{verbatim}
2254[eclipse 1]: [fdplexsearch].
2255*       fd loaded 
2256*       range loaded
2257*       eplex loaded
2258*       fdplex loaded
2259*       yes.
2260
2261[eclipse 2]: spy(mylabeling), spy(indomain).
2262*       spypoint added to mylabeling / 1.
2263*       spypoint added to indomain / 1.
2264*       yes.
2265
2266
2267[eclipse 9]: solve(X, Y, Z, W).
2268*       CALL   mylabeling([X{eplex:1.0, range : 1..5, fd:[1..5]}, 
2269*                          Y{eplex:5.0, range : 1..5, fd:[1..5]}, 
2270*                          Z{eplex:1.2, range : 1..3, fd:[1..3]}, 
2271*                          W{eplex:4.0, range : 1..4, fd:[1..4]}]) (dbg)?- leap
2272*       CALL   indomain(Z{eplex:1.2, range : 1..3, fd:[1..3]}) (dbg)?- leap
2273*       EXIT   indomain(1) (dbg)?- leap
2274*       CALL   mylabeling([Y{eplex:5.0, range : 1..5, fd:[1..5]}, 
2275*                          X{eplex:2.0, range : 1..5, fd:[2..5]}, 
2276*                          W{eplex:3.7, range : 1..4, fd:[2..4]}]) (dbg)?- leap
2277*       CALL   indomain(W{eplex:3.7, range : 1..4, fd:[2..4]}) (dbg)?- leap
2278*       EXIT   indomain(4) (dbg)?- leap
2279*       CALL   mylabeling([3, 2]) (dbg)?- no debug
2280
2281*       Found a solution with cost -11
2282*       X = 2
2283*       Y = 3
2284*       Z = 1
2285*       W = 4
2286*       yes.
2287
2288
2289\end{verbatim}
2290{\bf Tracing {\em fdplex} Search}
2291\label{fdplextrace}
2292\end{quote} 
2293In query 1 the {\em fdplex} is loaded, and it automatically loads the
2294other libraries which are needed.
2295Query 2 sets spypoints on two predicates.  Now each time either of
2296these predicates are called, and when they exit, the debugger stops
2297and allows the programmer to study the state of the program execution.
2298Query 3 calls the program defined in figure \pageref{fdplexsearch}.
2299Before labelling starts the domains of the variables have already been
2300reduced by finite domain propagation.
2301The reduced domains are automatically communicated to the {\em range}
2302library, and passed into the linear solver.
2303The linear solver (CPLEX) has already been invoked by {\em eplex} and
2304has returned the values of the variables $X,Y,Z,W$ at the relaxed
2305optimum.
2306
2307Now {\em deleteff} selects the variable with the smallest
2308domain, which is $Z$.
2309The {\em fdplex indomain} predicate labels $Z$ to the integer value
2310nearest to its value at 
2311the relaxed optimum.
2312This wakes the {\em fd} constraint handler which tightens the domain
2313of $X$, and it wakes the linear solver which returns a new relaxed
2314optimum with new suggested values for the other variables. 
2315
2316This time the variable with the smallest domain is $W$, and this is
2317the one selected for instantiation.
2318Once this has been instantiated to the integer value closest to its
2319suggested value, {\em fd} propagation immediately instantiates the
2320remaining values.
2321
2322At the next spy point the user enters {\em no debug} and tracing is
2323switched off.
2324The optimal solution is indeed the one found first, which testifies to
2325the usefulness of the combined heuristic used in the search.
2326
2327\subsubsection{Incomplete Constructive Search}
2328For real industrial applications, the search space is usually too large
2329for complete search to be possible.
2330The branch and bound search yields better solutions with longer and
2331longer delays until, in many cases, it fails to yield any new
2332solutions but continues searching fruitlessly!
2333
2334In cases where complete search is impractical, the heuristics guiding
2335the search become very important.
2336If bad heuristics are chosen the search may methodically explore some
2337unpromising corner of the search space yielding very poor solutions
2338which fail to drive the branch and bound search into more fruitful
2339areas. 
2340Good heuristics depend on good constraint handling: the information
2341returned from the constraint handlers is crucial in enabling the
2342heuristics to focus  search
2343on promising regions.
2344Moreover once some good choices have been made, propagation can
2345achieve even better results supporting even better heuristics for
2346future choices.
2347This positive feedback produces  a virtuous spiral.
2348
2349Received wisdom suggests that local search techniques, based on
2350solution repair, achieve faster convergence on good solutions than
2351constructive search.
2352However on several industrial applications our experience has
2353shown the contrary.
2354Good heuristics tailored to the application at hand has proved more
2355effective in yielding high quality solutions than techniques based on
2356solution repair.
2357
2358\subsubsection{Intelligent Backtracking and {\em nogood} Learning}
2359\ECL offers facilities for programmers to define specific constructive
2360search algorithms.
2361Intelligent backtracking has been implemented in \ECL.
2362However it is
2363not offered as a library, because in practice any reduction in the
2364amount of 
2365search due to intelligent backtracking is dominated by the cost of
2366accessing and updating the necessary data structures.
2367
2368The information about which constraints are involved, when a
2369failure occurs during search, is useful for recording combinations of
2370variable values which are mutually inconsistent.
2371Such conflict sets can be used to impose extra constraints called {\em
2372nogoods} which are learned during search.
2373
2374{\em nogood} learning has also been implemeted in \ECL and is proving
2375useful on some benchmark examples, but as yet no library supporting
2376{\em nogoods} is available.  A paper describing this work
2377\cite{nogoods} is available from the 
2378IC-Parc home page 
2379(whose URL is given in section \ref{icparcurl} below).
2380
2381\subsection{Solution Repair}
2382At the end of the previous section we suggested that even for
2383incomplete search, constructive search with good heuristics can
2384outperform solution repair.
2385However there are many important examples,  such as job-shop
2386scheduling and travelling salesman problems, where repair performs
2387better than constructive search.
2388Moreover repair is very important in handling {\em dynamic} problems,
2389which change after an initial solution has been found.
2390The problem may be changed because the user is unsatisfied with the
2391solution for reasons which are not captured in the implementation, and
2392adds new constraints to exclude this solution.
2393Otherwise the change may be due to external circumstances such as
2394unplanned delays, rush orders, cancellations, and so on.
2395
2396\ECL uses the concept of the {\em tentative} value to
2397support solution repair.  
2398This is the same concept that is used to return proposed values for
2399variables from the linear solver, as discussed in the preceding
2400section.
2401In the case of repair, however, the tentative value comes not from a
2402constraint handler, but from the original solution to the original
2403problem.
2404
2405When the problem changes, some of the tentative values may no longer
2406satisfy some of the new constraints.
2407Indeed the simplest change is to constrain a variable to take a new
2408value. 
2409In this case the tentative value violates the new constraint.
2410In case there is no violation, of course, the tentative values
2411comprise a feasible solution to the new problem and there is no need
2412to repair the solution at all!
2413
2414The purpose of the \ECL repair library is to support the process of
2415detecting a variable whose tentative value is in conflict with a
2416constraint, and in detecting further violations that result from
2417choosing a value for a variable that differs from its tentative value.
2418
2419\subsubsection{``Constructive'' Repair}
2420There are several very different repair algorithms that arise from
2421different choices of how to change the value of a variable from its
2422tentative value.
2423The algorithm most similar to constructive search simply instantiates
2424the variable to the chosen new value.
2425In this case the tentative values do no more than support a specific
2426heuristic within a constructive search algorithm.
2427Notice that the heuristic can do more than simply choosing the
2428tentative value as the first guess for each variable during labelling.
2429It can also take into account for each value for a variable the number
2430of other tentative values with which it conflicts according to the
2431constraints.
2432Thus when a variable is labelled to a new value, the value is chosen
2433so as to minimise disruption to the original solution.
2434
2435The \ECL {\em repair} library defines primitives for setting a
2436tentative value for a variable ({\em tent\_set}) and for looking it up
2437({\em tent\_get}).
2438It also supports a special annotation which 
2439changes the behaviour of a constraint from propagation to simply
2440checking against the
2441tentative values of their uninstantiated variables. 
2442The annotation is written {\em Constraint r}, where {\em Constraint}
2443can be any built-in or user-defined constraint.
2444Whenever the check fails, the constraint is recorded as a {\em
2445conflict constraint}, and full propagation on the constraint is
2446switched on.
2447The set of conflict constraints can be accessed via the predicate {\em
2448conflict\_constraints}.
2449This can be used in the search procedure to decide which variable to
2450label next.
2451
2452A built-in search predicate called {\em repair} is provided which
2453selects a variable whose tentative value violates a repair constraint,
2454labels it and succeeds when all the remaining variables have
2455consistent tentative values.
2456
2457We illustrate this repair algorithm (with an example from the IC-Parc
2458\ECL library manual \cite{eclipseicparc}) in figure
2459\ref{constructiverepair}.
2460\begin{quote}
2461\begin{verbatim}
2462
2463solve(X,Y,Z) :-
2464        [X,Y,Z]::1..3,                    % the problem variables
2465        Y ## X r,                         % state the constraints
2466        Y ## Z r,
2467        Y #= 3 r,
2468        [X,Y,Z] tent_set [1,2,3],              % set existing solution
2469        repair,                           % invoke repair labeling
2470        [X,Y,Z] tent_get [NewX,NewY,NewZ].  % get repaired solution
2471\end{verbatim}
2472{\bf The ``Constructive'' Repair Algorithm}
2473\label{constructiverepair}
2474\end{quote} 
2475The solutions found are $[1,3,1]$ and $[1,3,2]$, which means that only
2476$Z$ 
2477has been repaired.  
2478Initially only the constraint $Y \verb0#=0 3$ is inconsistent with the
2479solution 
2480so variable $Y$ is repaired to take the value $3$.  
2481This now affects the constraint $Y \verb0##0 Z$, 
2482and $Z$ must be repaired to either $1$ or $2$.  
2483
2484The constraint $Y \verb0##0 X$ is not affected by the update. In
2485particular, $X$
2486keeps the value of the existing solution, and is 
2487not even being labeled by {\em repair/0}. 
2488
2489Constructive repair is also known as {\em informed backtracking} and
2490has been used successfully on a variety of benchmarks
2491\cite{minton}. 
2492
2493\subsubsection{Weak Commitment}
2494Instead of instantiating a variable in order to repair it, an
2495alternative method is simply to change its tentative value.
2496This approach requires no backtracking, since every conflict can be
2497fixed by just changing tentative values.
2498The disadvantage is that cycles can easily occur in which two
2499variables repeatedly switch their tentative values.
2500
2501A very successful algorithm based on repairing tentative values is
2502called {\em Weak Commitment} \cite{yokoo}.
2503On starting all the variables have tentative values.
2504Variables in conflict are repaired - by instantiating them - until
2505either there are no more conflicts and the algorithm terminates, or
2506the remaining conflicts cannot be repaired.
2507The latter situation occurs when some variable in conflict cannot
2508be instantiated to any value that is consistent with the variables
2509instantiated so far.
2510
2511When such a dead-end is encountered, the weak commitment algorithm
2512simply uninstantiates all the variables, setting their tentative
2513values to the values they had when they were instantiated.
2514Then the algorithm restarts, fixing conflicts as before.
2515
2516\subsubsection{Local Improvement}
2517Constructive repair and weak commitment are two algorithms designed to
2518find feasible solutions to a problem.
2519In case the problem additionally requires some cost to be minimised,
2520the repair must be adapted to return better and better solutions.
2521
2522For unconstrained problems, local improvement can be achieved by just
2523changing the value of some variable, having chosen the variable and
2524value such that the cost of the new solution is better than the cost
2525of the previous solution.
2526This idea underlies the various hill-climbing algorithms as well as
2527stochastic techniques such as Simulated Annealing and Tabu search.
2528
2529For problems with constraints, changing the value of a variable will
2530not necessarily yield a feasible solution.
2531The \ECL repair library can be used, however, to find a feasible
2532solution which incorporates the change.
2533
2534A simulated annealing program has been written in \ECL which ensures
2535that moves respect the problem constraints.
2536The program has been compared with a pure simulated annealing approach
2537which simply associates a cost with violated constraints and otherwise
2538treats the problem as unconstrained.
2539Experiments showed that the ``constrained simulated annealing''
2540program outperformed the pure one.
2541
2542For an industrial application the repair library has been used
2543together with the {\em eplex} linear constraint library.
2544In the algorithm used for this application, the relaxed optimum is
2545checked against the repair 
2546constraints, and at each step a violated constraint is strengthened in
2547such a 
2548way that the next solution returned from {\em eplex} must satisfy it.
2549The algorithm outperforms standard MIP search because the problem is a
2550dynamic constraint problem: there is an original solution and the
2551requirement is to modify that solution to satisfy some new
2552constraints.
2553
2554Details of these algorithms are beyond the scope of this article, but
2555hopefully this brief survey has offered a glimpse of the power of
2556repair-based search in combination with the different solvers of \ECL.
2557
2558\section{The \ECL System}
2559\ECL is jointly owned by ICL and IC-Parc, which is  an ICL-supported
2560research centre at Imperial College.  The system can be obtained by
2561ftp from IC-Parc by emailing
2562\begin{verbatim}
2563eclipse-request@icparc.ic.ac.uk
2564\end{verbatim}
2565
2566\ECL runs under the Unix operating system (specifically SunOS 4 on
2567Sun-4 hardware, Solaris on Sparc machines and Linux on PC's), and will
2568be available under Windows-NT (version 4.0) by the end of 1997.
2569
2570\ECL is embeddable in \verb0C0 and \verb0C++0 programs.
2571It is
2572available in the form of a linkable library, and a number of
2573facilities are available to pass data between the different 
2574environments, to make the integration as close as possible. 
2575Naturally facilities are also provided to allow \ECL to invoke \verb0C0
2576and \verb0C++0. 
2577
2578A tightly integrated graphical system is very useful for program
2579development, and \ECL offers such an integration to the Tcl/Tk
2580toolkit, which is public domain software available under Unix and
2581Windows. 
2582Typically \ECL is invoked from Tcl which is driven directly
2583by user interactions.
2584An example graphical environment for \ECL developers is the graphical
2585constraint environment $Grace$, available as an \ECL library.
2586Grace is implemented using \ECL and Tcl.
2587
2588
2589
2590The manuals and other documentation include a manual covering the
2591non-constraint facilities of \ECL \cite{eclipseuser}, manuals
2592covering the facilities 
2593supporting constraints \cite{eclipseext,eclipseicparc}, and
2594information covering the  graphical user 
2595interface library, and embeddability in \verb0C0 and \verb0C++0.
2596
2597\label{icparcurl}
2598Background references can be found in the list of publications
2599reachable from the IC-Parc home page at 
2600\begin{verbatim} http://www.icparc.ic.ac.uk/ 
2601\end{verbatim}
2602
2603\section{Conclusion}
2604The \ECL platform has been under development for over ten years.
2605During that time constraint programming has established itself not
2606only as an important research area, but also in live industrial
2607applications.
2608The market for constraint technology is growing dramatically, to the
2609point that the major vendor of MIP technology (CPLEX) has been
2610recently taken 
2611over by a constraint technology vendor (ILOG).
2612
2613Over the last five years \ECL has moved on from its early roots in
2614logic programming and constraint propagation, to  a focus on hybrid
2615algorithms.
2616A tight integration between MIP and CLP has been developed and hybrid
2617algorithms based on this combination have proved their efficiency on
2618industrial applications.
2619However hybrid search algorithms, in particular utilising solution
2620repair, 
2621have also been a focus of research and development.
2622
2623Based on growing experience with hybrid algorithms, we have been able
2624to separate the features of the different algorithms both from each
2625other, and from the underlying problem model.
2626Consequently we have reached the point where \ECL can be used to
2627express a clear, precise and neutral  conceptual model of an
2628application, and this model can then be extended and annotated
2629at the implementation stage.
2630The result of implementation is
2631a design model which implements fine-grained hybrid
2632algorithms tailored to the application at hand.
2633
2634This work has been based on experience on a variety of industrial
2635applications.
2636IC-Parc has developed applications for several of its industrial
2637partners, and each application has contributed to the final
2638architecture  of the \ECL platform.
2639Ongoing applications, with partners such as 
2640British Airways, Wincanton Transport
2641and Bouygues, 
2642continually give rise to new hybrid techniques,
2643and these results will feed back into \ECL, as the algorithms
2644are encapsulated and added as new libraries.
2645
2646Nevertheless the real benefit of \ECL  comes not from the algorithms
2647that are already encapsulated as libraries, but from the ease with
2648which new hybrid algorithms can be developed and validated, and
2649delivered into the industrial computing environment.
2650
2651\newpage 
2652
2653%\bibliographystyle{alpha}
2654%\bibliography{eclipse}
2655\begin{thebibliography}{MJPL92}
2656
2657\bibitem[Ae97]{eclipseuser}
2658Abderrahamane Aggoun and {et.al.}
2659\newblock {\em {ECLiPSe user manual}}.
2660\newblock IC-Parc, 1997.
2661
2662\bibitem[Be97]{eclipseext}
2663Pascal Brisset and {et.al.}
2664\newblock {\em {ECLiPSe Extensions manual}}.
2665\newblock IC-Parc, 1997.
2666
2667\bibitem[CPL93]{CPLEX}
2668CPLEX.
2669\newblock Using the cplex callable library and cplex mixed integer library.
2670\newblock Technical Report Version 2.1, CPLEX Optimisation Inc., 1993.
2671
2672\bibitem[MJPL92]{minton}
2673S.~Minton, M.~D. Johnston, A.~B. Philips, and P.~Laird.
2674\newblock Minimizing conflicts: a heuristic repair method for constraint
2675  satisfaction and scheduling problems.
2676\newblock {\em Artificial Intelligence}, 58, 1992.
2677
2678\bibitem[RR96]{nogoods}
2679Tom Richards and Barry Richards.
2680\newblock Nogood learning for constraint satisfaction.
2681\newblock Technical report, IC-Parc, 1996.
2682\newblock In Proceedings CP 96 Workshop on Constraint Programming Application.
2683
2684\bibitem[RWH97]{clpmip}
2685Robert Rodosek, Mark Wallace, and Mozafian Hajian.
2686\newblock A new approach to integrating mixed integer programming with
2687  constraint logic programming.
2688\newblock Technical report, IC-Parc, 1997.
2689\newblock To appear in {Annals of Operations Research}.
2690
2691\bibitem[SNE97]{eclipseicparc}
2692Joachim Schimpf, Stefano Novello, and Hani {El Sakkout}.
2693\newblock {\em {IC-Parc ECLiPSe Library Manual}}.
2694\newblock IC-Parc, 1997.
2695
2696\bibitem[Wal97]{consprog}
2697Mark Wallace.
2698\newblock Constraint programming.
2699\newblock Chapter 17 of {The Handbook of Applied Expert Systems}, CRC Press, 1997.
2700
2701\bibitem[Yok94]{yokoo}
2702M.~Yokoo.
2703\newblock Weak-commitment search for solving constraint satisfaction problems.
2704\newblock In {\em Proc. 12th National Conference on Artificial Intelligence},
2705  pages 313--318, 1994.
2706
2707\end{thebibliography}
2708
2709\newpage
2710
2711\tableofcontents
2712\end{document}
2713
2714
2715
2716