1% BEGIN LICENSE BLOCK
2% Version: CMPL 1.1
3%
4% The contents of this file are subject to the Cisco-style Mozilla Public
5% License Version 1.1 (the "License"); you may not use this file except
6% in compliance with the License.  You may obtain a copy of the License
7% at www.eclipse-clp.org/license.
8% 
9% Software distributed under the License is distributed on an "AS IS"
10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11% the License for the specific language governing rights and limitations
12% under the License. 
13% 
14% The Original Code is  The ECLiPSe Constraint Logic Programming System. 
15% The Initial Developer of the Original Code is  Cisco Systems, Inc. 
16% Portions created by the Initial Developer are
17% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18% 
19% Contributor(s): 
20% 
21% END LICENSE BLOCK
22
23%HEVEA\cutdef[1]{section}
24
25\index{mathematical programming, interface to|(}
26\index{mixed integer programming, interface to|(}
27\index{linear programming, interface to|(}
28\index{quadratic programming, interface to|(}
29\index{simplex solver, interface to|(}
30\section{Usage}
31This library allows the use of an external mathematical programming 
32(LP, MIP or quadratic) solver 
33from within {\eclipse}. It provides a largely solver-independent API
34to the programmer, so many programs will run with any supported external
35solver.
36
37The library interfaces to both commercial and open-source
38solvers. Commercial solver will probably require a license to use the
39solver, while the open-source solvers are available free of charge, but are
40govererned by their own open-source licenses separate from {\eclipse}'s.
41
42%With the kind agreement of Dash Associates Ltd., the
43%XPRESS-MP\footnote{XPRESS-MP is a product from Dash Associates
44%Ltd. (www.dashoptimization.com)} solver is now included with the library,
45%and is available for academic use under the {\eclipse} licence agreement.
46
47See section~\ref{specificsolver} for more details on the supported solvers.
48
49The most generic way to load the library is:
50\begin{verbatim}
51:- lib(eplex).
52\end{verbatim}
53\index{eplex}
54This will try to load an appropriate external solver available on
55the computer.
56
57It is also possible to request a specific solver explicitly,
58see section~\ref{specificsolver} for details.
59
60Note that the {\eclipse} library described here is just an interface
61to an external solver. In order to be able to use it, you need to have
62access to a solver supported by the library. For commercial solvers, this
63requires a licence for the solver on your machine. For more details,
64see section \ref{specificsolver}. For open source solvers, the required
65solver library may be distributed with {\eclipse} if its licence allows
66this.
67
68
69%\section{History}
70%The latest major addition to this library is support for adding
71%constraints to the solver, as far as this is supported by the
72%external solver.
73%
74%The library has been ported to work with XPRESS-MP as well as CPLEX.
75%
76%The third version of this library uses standard range-variables
77%provided by the \htmlref{range-library}{chaprange}, which should
78%facilitate interfacing to other solvers.
79%The interface to CPLEX has been extended such that more
80%state information can be retrieved, e.g.\ constraint slacks,
81%basis information, reduced costs etc.
82%A quite generic solver demon is provided which makes it easy to
83%use CPLEX within a data-driven CLP setting.
84%
85%The second major version made some things conceptually clearer.
86%The notion of solver handles is new. It removes the ugly
87%concept of a global state and hopefully encourages
88%experiments with multiple solvers.
89%The interface to the solver's state has been extended,
90%allowing to modify and query many parameters and extract
91%solution information.
92%A pair of predicates has been added to allow reading and writing
93%problem files in MPS or LP format.
94%
95
96
97%----------------------------------------------------------------------
98\section{Eplex Instances}
99%----------------------------------------------------------------------
100
101In this chapter, the problem passed to the external solver will be referred
102to as an {\it eplex problem}. An eplex problem consists of a set of linear
103arithmetic constraints, whose variables have bounds and may possibly have
104integrality constraints. The external solver will solve such a problem by optimising
105these constraints with respect to an objective function. 
106
107With the eplex library, it is possible to have more than one eplex problem
108within one program. The simplest way to write such programs with the
109library is through {\it Eplex Instances}. An eplex instance is an instance
110of the eplex solver, to which an eplex problem can be sent. An external
111{\it solver state} can be associated with each eplex instance, which can be
112invoked to solve the eplex problem. Declaratively, an eplex instance can be
113seen as a compound constraint consisting of all the variables, their bounds, and
114constraints of the eplex problem.
115
116Like other solvers, each eplex instance has its own module. To use an eplex
117instance, it must first be declared, so that the module can be created.
118This is done by:
119
120\subsubsection{\biptxtref{eplex_instance(+Name)}{eplex_instance/1}{../bips/lib/eplex/eplex_instance-1.html}}
121
122This predicate will initialise an eplex instance {\tt Name}. Once
123initialised, a {\tt Name} module will exist, to which the user can post the 
124constraints for the eplex problem and setup and use the external solver
125state to solve the eplex problem. Normally, this predicate should be issued
126as a directive in the user's program, so that the program code can refer to
127the instance directly in their code. For example:
128
129\begin{verbatim}
130   :- eplex_instance(instance).
131\end{verbatim}
132
133
134For convenience, the eplex library declares {\tt eplex} as an eplex instance 
135when the library is loaded. 
136
137\subsection{Linear Constraints}
138\label{linear-constraints}
139The constraints provided are equalities and inequalities over
140linear expressions. 
141Their operational behaviour is as follows:
142\begin{itemize}
143\item When they contain no variables, they simply succeed or fail.
144\item When they contain exactly one variable, they are translated into
145a bound update on that variable, which may in turn fail, succeed,
146or even instantiate the variable.
147Note that if the variable's type is integer, the bound will be adjusted
148to the next suitable integral value.
149\item Otherwise, the constraint is transferred to the external solver state
150if the state has been setup. If it has not, the constraint
151delays and is transferred to the external solver state when it is setup.
152This mechanism makes it possible to interface to a non-incremental
153black-box solver that requires all constraints at once,
154or to send constraints to the solver in batches
155\end{itemize}
156
157
158As with all predicates defined for an eplex instance, these constraints
159should be module-qualified with the name of the eplex instance. In the 
160following they are shown qualified with the {\tt eplex} instance, and with 
161brackets around the constraints when needed. Other instances can
162be used if they have been declared using {\bf eplex_instance/1}. 
163 
164\subsubsection{\biptxtrefni{EplexInstance: (X \$= Y)}{\$=/2!eplex}{../bips/lib/eplex/SE-2.html}}
165\index{\$=/2@\texttt{\$=/2}!eplex} X is equal to Y. X and Y are linear expressions.
166
167\subsubsection{\biptxtrefni{EplexInstance: (X \$>= Y)}{\$$>$=/2!eplex}{../bips/lib/eplex/SGE-2.html}}
168\index{\$>=/2@\texttt{\$>=/2}!eplex} X is greater or equal to Y. X and Y are linear expressions.
169
170\subsubsection{\biptxtrefni{EplexInstance: (X  \$=< Y)}{\$=$<$/2!eplex}{../bips/lib/eplex/SEL-2.html}}
171\index{\$=</2@\texttt{\$=</2}!eplex} X is less or equal to Y. X and Y are linear expressions.
172
173
174\subsection{Linear Expressions}
175
176The following arithmetic expression can be used inside the constraints:
177\begin{description}
178\item[{\bf X}]
179Variables. If X is not yet a problem variable, it is turned into one
180	via an implicit declaration {\tt X\ \$::\ -1.0Inf..1.0Inf}.
181
182\item[{\bf 123, 3.4}]
183Integer or floating point constants.
184
185\item[{\bf +}Expr]
186Identity.
187
188\item[{\bf -}Expr]
189Sign change.
190
191\item[E1{\bf +}E2]
192Addition.
193
194\item[{\bf sum}(ListOfExpr)]
195Equivalent to the sum of all list elements.
196
197\item[E1{\bf -}E2]
198Subtraction.
199
200\item[E1{\bf *}E2]
201Multiplication.
202
203\item[ListOfExpr1{\bf *}ListOfExpr2]
204Scalar product: The sum of the products of the corresponding
205elements in the two lists.  The lists must be of equal length.
206\end{description}
207
208\subsection{Bounds}
209Bounds for variables can be given to an eplex instance via the \verb'$::/2'
210constraint:
211\begin{description}
212    \item[\biptxtrefni{EplexIntance: Vars \$:: Lo..Hi}{\$::/2!eplex}{../bips/lib/eplex/SNN-2.html}]
213    \index{\$::/2@\texttt{\$::/2}!eplex}\label{eplex-coloncolon2}
214    Restrict the external solver to assign solution values for the eplex
215    problem within the bounds specified by Lo..Hi.  
216    Passes to the external solver the bounds for the variables in Vars.
217    Lo, Hi are the lower and upper bounds, respectively. Note that the
218    bounds are only passed to the external solver if they would narrow the
219    current bounds, and failure will occur if the resulting interval is empty. 
220    Note also that the external solver does not do any bound propagation
221    and will thus not change the bounds on its own. The default bounds for
222    variables are notionally -1.0Inf..1.0Inf (where infinity is actually
223    defined as the solver's notion of infinity).
224\end{description}
225
226\subsection{Integrality}
227The difference between using an LP vs.\ an MIP solver is made by
228declaring integrality to the solver via the integers/1 constraint:
229\begin{description}
230    \item[\biptxtrefni{EplexInstance:integers(Vars)}{integers/1!eplex}{../bips/lib/eplex/integers-1.html}]
231    \index{\$integers/1@\texttt{integers/1}!eplex}\label{eplex-integers1}
232	Inform the external solver to treat the variables Vars as integral.
233	It does not impose the integer type on Vars. However, when a 
234        typed_solution is retrieved (via lp_get/3 or
235        lp_var_get/3), this will be rounded to the nearest integer.
236        
237        Note that unless eplex:integers/1 (or lp_add/3, see
238        section~\ref{lp-add}) is invoked, any invocation
239        of the eplex external solver (via lp_solve/2, lp_probe/3 or
240	lp_demon_setup/5) will only solve a continuous relaxation, even
241        when problem variables have been declared as integers in other
242        solvers (e.g.\ ic).
243        
244\end{description}
245
246Note that all the above constraints are local to the eplex instance; they
247do not place any restrictions on the variables for other eplex instances or
248solvers. Failure will occur only when inconsistency is detected within the
249same eplex instance, unless the user explicitly try to merge the constraints
250from different solvers/eplex instance.
251
252A counterpart, \bipref{reals/1}{../bips/lib/eplex/reals-1.html} `constraint' also exists -- this simply declares the
253variables specified are problem variables, and does not actually place any
254other constraint on the variables.
255
256\subsection{Solving Simple Eplex Problems}
257\label{solving-eplex}
258In order to solve an eplex problem, the eplex instance must be set up
259for an external solver state. The solver state can then be invoked to
260solve the problem. The simplest way to do this is to use:
261
262\begin{description}
263    \item[\biptxtref{EplexInstance:eplex_solver_setup(+Objective)}{eplex_solver_setup/1}{../bips/lib/eplex/eplex_solver_setup-1.html}]
264    This predicate creates a new external solver state and associates it
265    with the eplex instance. Any arithmetic, integrality and bound
266    constraints posted for this eplex instance are collected to create the
267    external solver state. After this, the solver state can be invoked to 
268    solve the eplex problem.
269
270    Objective is either min(Expr) or max(Expr) where Expr is a linear
271    expression (or quadratic, if supported by the external solver).  
272
273    \item[\biptxtref{EplexInstance:eplex_solve(-Cost)}{eplex_solve/1}{../bips/lib/eplex/eplex_solve-1.html}]
274    Explicitly invokes the external solver state. Any new constraints
275    posted are taken into account. If the external solver can find an
276    optimal solution to the eplex problem, then the predicate succeeds and Cost is
277    instantiated to the optimal value. If the problem is infeasible (has no
278    solution), then the predicate fails (by default). If the problem is
279    unbounded (Cost is not bounded by the constraints), then the predicate
280    succeeds without producing any solution values for the variables. 
281
282\end{description}
283\subsection{Examples}
284
285Here is a simple linear program, handled by the predefined eplex instance 'eplex':
286\begin{verbatim}
287:- lib(eplex).
288
289lp_example(Cost) :-
290     eplex: eplex_solver_setup(min(X)),
291     eplex: (X+Y $>= 3),
292     eplex: (X-Y $= 0),
293     eplex: eplex_solve(Cost).
294\end{verbatim}
295
296The same example using a user-defined eplex instance:
297\begin{verbatim}
298:- lib(eplex).
299:- eplex_instance(my_instance).
300
301lp_example(Cost) :-
302     my_instance: eplex_solver_setup(min(X)),
303     my_instance: (X+Y $>= 3),
304     my_instance: (X-Y $= 0),
305     my_instance: eplex_solve(Cost).
306\end{verbatim}
307
308Running the program gives the optimal value for Cost:
309
310\begin{verbatim}
311[eclipse 2]: lp_example(Cost).
312
313Cost = 1.5
314\end{verbatim}
315
316Note that if the {\tt eplex} eplex instance is used instead of {\tt
317my_instance}, then the {\tt eplex_instance/1} declaration is not
318necessary.
319
320
321By declaring one variable as integer, we obtain a Mixed Integer Problem:
322
323\begin{verbatim}
324:- lib(eplex).
325:- eplex_instance(my_instance).
326
327mip_example(Cost) :-
328     my_instance: (X+Y $>= 3),
329     my_instance: (X-Y $= 0),
330     my_instance: integers([X]),
331     my_instance: eplex_solver_setup(min(X)),
332     my_instance: eplex_solve(Cost).
333
334....
335[eclipse 2]: mip_example(Cost).
336
337Cost = 2.0
338\end{verbatim}
339
340The cost is now higher because X is constrained to be an integer. Note also
341that in this example, we posted the constraints before setting up the 
342external solver, whereas in the previous example we set up the solver
343first. The solver set up and constraint posting can be done in
344any order. If {\tt integers/1} constraints are only posted after problem
345setup, the problem will be automatically converted from an LP to a MIP
346problem. 
347
348This section has introduced the most basic ways to use the eplex library. 
349We will discuss more advanced methods of using the eplex instances in
350section~\ref{eplex-instance-advanced}. 
351
352
353%----------------------------------------------------------------------
354\section{Advanced Use of Eplex Instances}
355%----------------------------------------------------------------------
356\label{eplex-instance-advanced}
357
358\subsection{Obtaining Solver State Information}
359\label{eplex-instance-solver-info}
360The black-box interface binds both the objective value (Cost) and the
361problem variables by bindings these variables. On the other hand, {\bf
362  eplex_solve/1} binds the objective value, but does not bind the problem
363variables. These values can be obtained by:
364
365\begin{sloppypar}
366\begin{description}
367\item[\biptxtref{EplexInstance:eplex_var_get(+Var, +What, -Value)}{eplex_var_get/3}{../bips/lib/eplex/eplex_var_get-3.html}]
368
369Retrieve information about the solver state associated with the eplex
370instance for the variable {\tt Var}. If {\tt What} is {\tt solution} or
371{\tt typed_solution}, then the value assigned to this variable by the
372solver state to obtain the optimal solution is returned in {\tt
373Value}. {\tt solution} returns the value as a float, and {\tt
374typed_solution} returns the value as either a float or a rounded integer, depending
375on if the variable was constrained to an integer in the eplex problem.
376
377\item[\biptxtref{EplexInstance:eplex_get(+What, -Value)}{eplex_get/2}{../bips/lib/eplex/eplex_get-2.html}]
378
379Retrieve information about solver state associated with the eplex instance.
380This returns information such as the problem type, the constraints for the
381eplex problem. See the reference manual for more details.
382
383\item[\biptxtref{EplexInstance:eplex_set(+What, +Value)}{eplex_set/2}{../bips/lib/eplex/eplex_set-2.html}]
384Set a solver option for the eplex instance. 
385\item[\biptxtref{EplexInstance:eplex_write(+Format, +File)}{eplex_write/2}{../bips/lib/eplex/eplex_write-2.html}]
386Write out the problem in the the eplex instance's solver state to the file
387File in format Format. The writing is done by the external solver. Use the
388use_var_name(yes) option in
389\bipref{eplex_solver_setup/4}{../bips/lib/eplex/eplex_solver_setup-4.html}
390so that the written file uses \eclipse variable names. Also the {\tt
391write_before_solve} option of eplex_solver_setup/4 can be used to write out
392a problem just before it is solved by the external solver: this allows
393problem to be written in places where eplex_write/2 cannot be added
394(e.g. for probing problems)..
395
396\item[\biptxtref{EplexInstance:eplex_read(+Format, +File)}{eplex_read/2}{../bips/lib/eplex/eplex_read-2.html}]
397Read a MP problem in the file File in format Format into a solver state,
398and associate the solver with the eplex instance. No solver must already be
399setup for the eplex instance. The solver state that is setup can only be
400triggered explicitly.
401
402\end{description}
403\end{sloppypar}
404
405So for the simple MIP example:
406\begin{verbatim}
407
408:- lib(eplex).
409:- eplex_instance(my_instance).
410
411mip_example2([X,Y], Cost) :-
412     my_instance: (X+Y $>= 3),
413     my_instance: (X-Y $= 0),
414     my_instance: integers([X]),
415     my_instance: eplex_solver_setup(min(X)),
416     my_instance: eplex_solve(Cost),
417     my_instance: eplex_var_get(X, typed_solution, X),
418     my_instance: eplex_var_get(Y, typed_solution, Y).
419
420....
421[eclipse 2]: mip_example2([X,Y],C).
422
423X = 2
424Y = 2.0
425C = 2.0
426
427\end{verbatim}
428
429In the example, only X is returned as an integer, as Y was not explicitly
430constrained to be an integer.
431
432Note that if there are multiple eplex instances, and a variable is shared
433between the instances, then the solver state for each instance can have a
434different optimal value to the variable.
435
436\subsection{Creating Eplex Instances Dynamically}
437\index{eplex!instance!eplex_instance/1}
438So far, we have shown the use of {\tt eplex_instance/1} as a directive to
439declare an eplex instance. For some applications, it might be necessary to
440create eplex instances dynamically at run-time. The can be done by calling
441{\tt eplex_instance/1} at run-time.
442In this case, the instance name should {\em not} be used to module-qualify
443any predicates in the code, since this will raise a compiler warning
444complaining about an unknown module.
445
446\begin{verbatim}
447   new_pool(X,Y) :-  % INCORRECT
448      eplex_instance(pool),
449      pool: (X $>= Y), % will generate a warning
450      ...
451\end{verbatim}
452\noindent
453Of course, in the above code, the instance name {\tt pool} is already known
454at compile time, so it can always be declared by a directive.
455
456If the name is truly generated dynamically, this can be done as follows:
457\begin{verbatim}
458   new_pool(Pool,X,Y) :-
459       eplex_instance(Pool),
460       Pool: (X $>= Y),
461       ....
462\end{verbatim}
463
464\subsubsection{Obtaining Bounds on the Objective Value}
465
466The external solver does not always return the optimal objective
467value, for example when the optimisation was aborted. However, even when
468the solver returns an optimal solution, it may actually not be the exact
469optimal, because of solver settings (e.g. for MIP problems, the MIP search
470will terminate when the solution found is within certain tolerance of the
471best possible solution value). In these cases, it may be useful to obtain
472some bounds on the optimal objective value. The best and worst bounds on
473the optimal objective can be obtained using the best_bound and worst_bound
474options of \bipref{eplex_get/2}{../bips/lib/eplex/eplex_get-2.html},
475respectively. 
476
477\subsection{Interface for CLP-Integration: Solver Demons}
478
479To implement hybrid algorithms where a run of a simplex/MIP solver is only
480a part of the global solving process, the black-box model presented above
481is not appropriate anymore. With eplex instances, we can call {\tt
482eplex_solve/1} repeatedly to re-solve the problem, perhaps after adding
483more constraints to the problem or after changes in the variable
484bounds. However, the solver must be invoked explicitly. We require more
485sophisticated methods of invoking the solver. This can be done by setting
486up a solver demon, and specifying the conditions in which the demon is to
487wake up and invoke the external solver.
488
489\subsubsection{\biptxtref{EplexInstance:eplex_solver_setup(+Objective, -Cost, +ListOfOptions, +TriggerModes)}{eplex_solver_setup/4}{../bips/lib/eplex/eplex_solver_setup-4.html}}
490This is a more sophisticated set up for a new solver state than
491{\tt eplex_solver_setup/1} (in fact eplex_solver_setup/1 is a special case
492of eplex_solver_setup/4).
493The main idea is that a list of trigger conditions
494are specified in {\tt TriggerModes}, and along with setting up the solver
495state, a demon goal is created which is woken up when one of the
496specified trigger condition is met. This demon goal will then invoke the
497solver, with any 
498constraints posted to the eplex instance since the solver was last invoked
499taken into account, to re-solve the problem. 
500
501The {\tt ListOfOptions} is a list of solver options for setting up the
502solver state. Some of these affect the way the external solver solves the
503problem, such as if presolve should be applied before solving the problem.
504See the reference manual for \bipref{eplex_solver_setup/4}{../bips/lib/eplex/eplex_solver_setup-4.html} for
505details on the available options and trigger modes.
506
507As the solver is designed to be invoked repeatedly, it is inappropriate to
508directly bind {\tt Cost} to the objective value. Instead, the objective
509value is exported as a bound to Cost:
510For a minimisation problem, each solution's
511cost becomes a lower bound, for maximisation an upper bound on Cost.
512This technique allows for repeated re-solving with reduced variable bounds
513or added constraints. Note that the bound update is done only if the
514solution is optimal. Note also that Cost is not automatically 
515made a problem variable, and thus may not have bounds associated
516with in. In order for the bounds information not to be lost, some bounds
517should be given to Cost (e.g. making it a problem variable (but
518this might introduce unnecessarily self-waking on bounds change), or via
519another solver with bounds (e.g. ic)). 
520
521
522\index{eplex!presolve}
523Note that when a solver demon runs frequently on relatively small problems,
524it can be important for efficiency to switch the external solver's
525presolving off for this demon as part of the {\tt ListOfOptions} during the
526setup of the problem to reduce overheads.
527
528\subsubsection{Example}
529
530The simplest case of having a simplex solver automatically cooperating
531with a CLP program, is to set up a solver demon which will repeatedly
532check whether the continuous relaxation of a set of constraints
533is still feasible.
534The code could look as follows (we use the eplex instance in this example):
535\begin{verbatim}
536simplex :-
537    eplex:eplex_solver_setup(min(0), C, 
538        [solution(no)], [bounds]).
539\end{verbatim}
540First, the constraints are normalised and checked for linearity.
541Then a solver with a dummy objective function is set up. The option
542{\tt solution(no)} indicates that we are not interested in solution values.
543Then we start a solver demon which will re-examine the problem
544whenever a change of variable bounds occurs.
545The demon can be regarded as a compound constraint implementing the
546conjunction of the individual constraints. It is able to detect
547some infeasibilities that for instance could not be detected by a
548finite domains solver, e.g.
549\begin{verbatim}
550[eclipse 2]: eplex:(X+Y+Z >= K), eplex:(X+Y+Z =< 1),
551    eplex:eplex_solver_setup(min(0), C, 
552        [solution(no)], [bounds]),
553    K = 2.
554
555No (0.00s cpu)
556\end{verbatim}
557In the example, the initial simplex is successful, but instantiating
558K wakes the demon again, and the simplex fails this time.
559
560A further step is to take advantage of the cost bound that the simplex
561procedure provides. To do this, we need to give the objective 
562The setup is similar to above, but we accept an objective function and
563add a cost variable. The bounds of the cost variable will be updated
564whenever a simplex invocation finds a better cost bound on the problem.
565In the example below, an upper bound for the cost of 1.5 is found
566initially:
567\begin{verbatim}
568[eclipse 5]: ic: (Cost $:: -1.0Inf..1.0Inf), 
569      eplex:(X+Y $=< 1), eplex:(Y+Z $=< 1), eplex:(X+Z $=< 1),
570      eplex:eplex_solver_setup(max(X+Y+Z), Cost, 
571          [solution(no)], [bounds]).
572
573X = X{-1e+20 .. 1e+20}
574Y = Y{-1e+20 .. 1e+20}
575Z = Z{-1e+20 .. 1e+20}
576Cost = Cost{-1.0Inf .. 1.500001}
577
578
579Delayed goals:
580        lp_demon(prob(...), ...)
581Yes (0.00s cpu)
582\end{verbatim}
583(Note that the ranges for X, Y and Z is -1e+20 .. 1e+20 as 1e+20 is this
584external solver's notion of infinity). 
585
586If the variable bounds change subsequently, the solver will be re-triggered,
587improving the cost bound to 1.3:
588\begin{verbatim}
589[eclipse 6]: ic: (Cost $:: -1.0Inf..1.0Inf), 
590      eplex:(X+Y $=< 1), eplex:(Y+Z $=< 1), eplex:(X+Z $=< 1),
591      eplex:eplex_solver_setup(max(X+Y+Z), Cost, 
592          [solution(no)], [bounds]), 
593      eplex:(Y =< 0.3).
594
595X = X{-1e+20 .. 1e+20}
596Z = Z{-1e+20 .. 1e+20}
597Cost = Cost{-1.0Inf .. 1.300001}
598Y = Y{-1e+20 .. 0.3}
599
600
601Delayed goals:
602        lp_demon(prob(...), ...)
603Yes (0.00s cpu)
604\end{verbatim}
605
606A further example is the implementation of a MIP-style branch-and-bound
607procedure. Source code is provided in the library file mip.pl.
608
609\subsection{Encapsulated Modification of the Problem: Probing}
610
611The external mathematical programming solvers often provides the facility
612for the user to change the problem being solved. This includes the addition
613or removal of constraints, and the changing of the objective function.
614We have already seen how extra constraints can be added. As {\eclipse} is a
615logic programming language, removal of constraints is automatically
616achieved by backtracking. We do not allow the user to explicitly remove
617constraints that have been collected by the external solver, as this makes 
618the problem non-monotonic.
619For the same reason, we do not allow the objective function to be
620changed.\footnote{However, some monotonic changes are allowed in the
621low-level interface, for implementing column generation, see
622section~\ref{coladd}.}
623However, we do allow the problem (including the objective function) to be
624{\it temporarily\/} changed in certain specified ways. This allows the
625problem to be `probed' with these changes:
626
627\subsubsection{\biptxtref{EplexInstance:eplex_probe(+Probes, -Cost)}{eplex_probe/2}{../bips/lib/eplex/eplex_probe-2.html}}
628Similar to eplex_solve/1, but the problem is first temporarily modified as
629specified in Probes before the optimisation. The Cost value
630is instantiated to the objective value for this new modified problem, and
631any solution state requested are also updated. 
632
633\subsection{Destroying the Solver State}
634\subsubsection{\biptxtref{EplexInstance:eplex_cleanup}{eplex_cleanup/0}{../bips/lib/eplex/eplex_cleanup-0.html}}
635
636Destroy the specified solver, free all memory, etc.
637Note that {\eclipse} will normally do the cleanup automatically,
638for instance when execution fails across the solver setup, or
639when a solver handle gets garbage collected. The solver is disassociated
640with the eplex instance, and any outstanding constraints not yet collected
641by the  solver are removed, with a warning to the user. In effect, the
642eplex instance is reinitialised.
643
644Note that this is a non-logical operation. Backtracking into code before 
645{\tt eplex_cleanup/0} will not restore the solver state, and any attempt to
646reuse the solver state will not be possible (the execution will abort with
647an error). Normally, it is recommended to let {\eclipse} perform the cleanup 
648automatically,
649for instance when execution fails across the solver setup, or
650when an unused solver state handle gets garbage collected.
651However, calling eplex_cleanup/0 may
652cause resources (memory and licence) to be freed earlier.
653
654\subsection{Eplex Instance Interface Example: definition of optimize/2:}
655\label{defoptimize}
656A black-box setup-and-solve predicate {\bf optimize/2} can be defined as:
657\begin{verbatim}
658optimize(OptExpr, ObjVal) :-
659        eplex:eplex_solver_setup(OptExpr),
660        eplex:eplex_solve(ObjVal),
661        eplex:eplex_get(vars, VArr),
662        eplex:eplex_get(typed_solution, SolutionVector),
663        VArr = SolutionVector,                  % do the bindings
664        eplex:eplex_cleanup.
665\end{verbatim}
666A solver state is set up for the eplex instance {\tt eplex}, to allow
667constraints that were previously posted to {\tt eplex} to be collected.
668This happens once the solver is invoked by {\tt eplex_solve/1}. If there
669is a solution, the solution vector is obtained, and the
670variables are instantiated to those solutions.
671
672
673%----------------------------------------------------------------------
674\section{Low-Level Solver Interface}
675%----------------------------------------------------------------------
676
677For many applications, the facilities presented so far should be
678appropriate for using Simplex/MIP through {\eclipse}.
679However, sometimes it may be more convenient or efficient
680to directly access the solver state
681instead of going through the abstraction of the eplex instances. 
682This section describes lower level operations like how to set up
683solvers manually. In fact, these lower level predicates are used to
684implement the predicates provided with eplex instances.
685
686These predicates accesses the external solver state via a handle, which is
687returned when the solver state is set up, and subsequently used to access a
688particular solver state by the other predicates. The handle should be
689treated as a opaque data structure that is used by the eplex library to
690refer to a particular solver state.
691
692\subsection{Setting Up a Solver State}
693
694\subsubsection{\biptxtref{lp_demon_setup(+Objective, -Cost, +ListOfOptions, 
695+TriggerModes, -Handle)}{lp_demon_setup/5}{../bips/lib/eplex/lp_demon_setup-5.html}}
696
697This is used to set up a demon solver, and {\tt eplex_solver_setup/4} calls
698this predicate. There is one extra argument compared to {\tt
699  eplex_solver_setup/4}: the solver state handle {\tt Handle}, which is
700returned by this predicate when the new solver state is created.
701The other arguments are the same as in {\tt eplex_solver_setup/4}, except
702that there is an additional option in {\tt ListOfOptions}: {\tt
703  collect_from/1}. This is used to specify which, if any, eplex instance
704the solver state should be collecting constraints from. If an eplex
705instance is specified (as {\tt pool(Instance)}), then the solver state is
706associated with that instance. If the eplex instance is {\it not\/} to be
707associated with an eplex instance, {\tt none} should be given as the
708argument to {\tt collect_from}. This allows a solver state to be set up
709without the overhead of an eplex instance. The solver state will not
710collect any constraints automatically when it is invoked; instead the
711constraints must be added explicitly via the handle (using {\tt
712  lp_add_constraints/3}).
713
714By default, the external solver is invoked once after set up
715by {\tt lp_demon_setup},
716if any {\tt TriggerModes} is specified. Otherwise, the solver is not
717invoked and the predicate returns after set up.
718
719
720\subsubsection{\biptxtref{lp_setup(+NormConstraints, +Objective, +ListOfOptions, -Handle)}{lp_setup/4}{../bips/lib/eplex/lp_setup-4.html}}
721\label{lpsetup}
722This is an even lower-level primitive, setting up a solver state
723without any automatic triggering.
724It creates a new solver state for the set of constraints NormConstraints
725(see \ahrefloc{constrcoll}{below} for how to obtain a set of
726normalised constraints).
727Apart from the explicitly listed constraints, the variable's ranges will
728be taken into account as the variable bounds for the simplex algorithm.
729Undeclared variables are implicitly declared as \bipref{reals/1}{../bips/lib/eplex/reals-1.html}.
730
731However, when variables have been declared integers in other solvers (e.g.\
732using \bipref{ic:integers/1}{../bips/lib/ic/integers-1.html}),
733that is not taken into account by the solver by default.
734This means that the solver will only work on the {\em relaxed problem}
735(ie.\ ignoring the integrality constraints),
736unless specified otherwise in the options.
737Objective is either {\tt min(Expr)} or {\tt max(Expr)}
738where Expr is a linear (or quadratic) expression.
739ListOfOptions is a list of solver options, the same as for
740\bipref{lp_demon_setup/5}{../bips/lib/eplex/lp_demon_setup-5.html} and \bipref{eplex_solver_setup/4}{../bips/lib/eplex/eplex_solver_setup-4.html}, except for the {\tt
741  collect_from} and {\tt initial_solve} options, which are specific for the
742demon solvers.
743
744\subsection{Adding Constraints to a Solver State}
745
746Constraints can be added directly to a solver state without posting them to
747an eplex instance. This is done by:
748
749\subsubsection{\biptxtref{lp_add_constraints(+Handle, +Constraints, +NewIntegers)}{lp_add_constraints/3}{../bips/lib/eplex/lp_add_constraints-3.html}}
750
751Add new constraints (with possibly new variables) to the solver state
752represented by Handle
753The new constraints will be taken into account the next time the
754solver is run.  The constraints will be removed on backtracking.
755
756The constraints are first normalised, and simple constraints filtered out (as
757discussed in section~\ref{linear-constraints}) before they are added
758to the external solver (by calling lp_add/3 described below).
759
760\subsubsection{\biptxtref{lp_add(+Handle, +NewNormConstraints, +NewIntegers)}{lp_add/3}{../bips/lib/eplex/lp_add-3.html}}
761\label{lp-add}
762
763This adds the constraints (both linear and integrality) to the
764external solver represented by Handle. The linear arithmetic constraints
765must be normalised. Note that it is possible to add trivial constraints,
766which would be filtered out by the higher level {\tt lp_add_constraints/3}
767using this predicate. Integrality constraints on non-problem variables are
768filtered out and a warning given.
769
770\subsubsection{\biptxtref{lp_add_vars(+Handle, +Vars)}{lp_add_vars/2}{../bips/lib/eplex/lp_add_vars-2.html}}
771
772This adds the variables in Vars to the external solver state represented by
773Handle. The variables should not contain variables which are already
774problem variables. The variables are given the default bounds of
775-infinity..infinity. 
776
777\subsubsection{\biptxtref{lp_var_set_bounds(+Handle, +Var, ++Lo,++Hi)}{lp_var_set_bounds/4}{../bips/lib/eplex/lp_var_set_bounds-4.html}}
778
779This updates the bounds for the problem variable Var in the external
780solver state represented by Handle. Failure occurs if Var is not a problem
781variable. 
782
783%----------------------------------------------------------------------
784\subsection{Running a Solver State Explicitly}
785%----------------------------------------------------------------------
786
787
788\subsubsection{\biptxtref{lp_solve(+Handle,
789-Cost)}{lp_solve/2}{../bips/lib/eplex/lp_solve-2.html}}
790
791Apply the external solver's LP or MIP solver to the problem represented by Handle.
792Precisely which method is used depends on the options given to lp_setup/4.
793lp_solve/2 fails (by default) if there is no solution or succeeds
794if an optimal solution is found, returning the solution's cost in Cost
795(unlike with lp_demon_setup/6, Cost gets instantiated to a number).
796After a success, various solution and status information can be retrieved
797using lp_get/3 and lp_var_get/4.
798
799\begin{sloppypar}
800The set of constraints considered by the solver is the one given when the
801solver was created plus any new constraints that were added
802(e.g  by lp_add_constraints/3) in the meantime.
803\end{sloppypar}
804
805If there was an error condition, or limits were exceeded,
806lp_solve/2 raises an event. See section \ref{lpevents} for details.
807
808\subsubsection{lp_probe(+Handle, +Probes, -Cost)}
809\index{eplex!lp_probe/3}
810Similar to lp_solve/2, but optimize for a modified problem as specified by
811Probes. This is the
812predicate called by \bipref{eplex_probe/2}{../bips/lib/eplex/eplex_probe-2.html}
813\subsection{Accessing the Solver State}
814
815In section~\ref{eplex-instance-solver-info}, we discussed how solver state
816information can be accessed via the eplex instance. Here are the lower
817level predicates that directly access this information via the solver
818state's handle:
819
820\subsubsection{\biptxtref{lp_get(+Handle, +What, -Value)}{lp_get/3}{../bips/lib/eplex/lp_get-3.html}}
821Retrieve information about solver state and results. See the reference
822manual description of {\tt lp_get/3} for a detailed description of the
823available values for {\tt What}.
824
825For example, it is possible to obtain the solution values from the last
826successful invocation of the external solver using the following:
827
828    \begin{verbatim}
829    instantiate_solution(Handle) :-
830        lp_get(Handle, vars, Vars),
831        lp_get(Handle, typed_solution, Values),
832        Vars = Values.
833    \end{verbatim}
834
835
836\subsubsection{\biptxtref{lp_var_get(+Handle,+Var, +What, -Value)}{lp_var_get/4}{../bips/lib/eplex/lp_var_get-4.html}}
837Retrieve information about solver state represented by Handle,
838related to a specific variable Var. Again, see the reference manual for the
839available parameters.
840
841\subsubsection{\biptxtref{lp_var_get_bounds(+Handle, +Var, -Lo, -Hi)}{lp_var_get_bounds/4}{../bips/lib/eplex/lp_var_get_bounds-4.html}}
842Retrieve the bounds of the problem variable Var from the solver state
843represented by Handle. 
844
845\subsubsection{\biptxtref{reduced_cost_pruning(+Handle,?GlobalCost)}{reduced_cost_pruning/2}{../bips/lib/eplex/reduced_cost_pruning-2.html}}
846This predicate implements a technique to prune variable bounds
847based on a global cost bound and the reduced costs of some solution to
848a problem relaxation.  The assumptions are that there is a global
849problem whose cost variable is GlobalCost, and that Handle refers to
850a linear relaxation of this global problem.
851The pruning potentially affects all variables involved in the relaxed
852problem.
853
854\subsection{Expandable Problem and Constraints}
855\label{coladd}
856
857We provide low-level primitives to `expand' an eplex problem. Such a problem is
858considered to have as yet unspecified components in the objective function
859and posted constraints. These constraints are known as expandable constraints.
860The as yet unspecified component involve variables
861that have not yet been added to the problem. When these variables are
862added, coefficients for the variables can be added to the expandable
863constraints, as well as the objective function. These primitives are the
864basis for implementing {\bf column generation}, and are used by the column
865generation library, lib(colgen). 
866
867These primitives modify an existing eplex problem {\it
868non-monotonically}, and can only be used on problems that are not
869represented by an eplex instance, and was not setup as a demon solver
870(i.e. no trigger conditions are specified). 
871
872\subsubsection{\biptxtref{lp_add_constraints(+Handle, +Constraints, +Ints, -Idxs)}{lp_add_constraints/4}{../bips/lib/eplex/lp_add_constraints-4.html}}
873\index{column generation!lp_add_constraints/4}
874This adds expandable constraints Constraints to the solver state
875represented by Handle. The predicate returns a list of indices for these
876constraints in Idxs. The indices are used to refer to the constraints when
877new variables are added to expand the problem.
878
879\subsubsection{\biptxtref{lp_add_columns(+Handle, +Columns)}{lp_add_columns/2}{../bips/lib/eplex/lp_add_columns-2.html}}
880\index{column generation!lp_add_columns/4}
881This expands the problem by adding new variables (columns) to the solver
882state represented by Handle. Columns is a list of
883variable:column-specification pair, where variable is the variable to be
884added as a new column, and column-specification the specification for the
885non-zero components of the column, i.e. coefficients for the expandable
886constraints (referred to using the index obtained from
887lp_add_constraints/4) and the objective for this variable.
888
889
890
891\subsection{Changing Solver State Settings}
892In addition to accessing information from the solver state, some options (a
893subset of those specified during solver set up) can be changed by:
894\subsubsection{\biptxtref{lp_set(+Handle, +What, +Value)}{lp_set/3}{../bips/lib/eplex/lp_set-3.html}}
895This primitive can be used to change some of the initial options
896even after setup. {\em Handle} refers to an existing solver state. See the
897reference manual for details.
898
899\subsection{Destroying a Solver State}
900\subsubsection{\biptxtref{lp_cleanup(+Handle)}{lp_cleanup/1}{../bips/lib/eplex/lp_cleanup-1.html}}
901
902Destroy the specified solver state, free all memory, etc. If the solver
903state is associated with an eplex handle, the solver state is disassociated
904with the eplex instance. However, unlike \bipref{eplex_cleanup/0}{../bips/lib/eplex/eplex_cleanup-0.html}, the
905outstanding constraints not yet collected by the solver is not removed.
906
907As with {\tt eplex_cleanup/0}, care should be taken before using this
908non-logical predicate. 
909
910\subsection{Miscellaneous Predicates}
911\subsubsection{\biptxtref{lp_read(+File, +Format, -Handle)}{lp_read/3}{../bips/lib/eplex/lp_read-3.html}}
912
913Read a problem from a file and setup a solver for it.  Format is
914{\tt lp} or {\tt mps}.
915The result is a handle similar to the one obtained by lp_setup/4.
916\subsubsection{\biptxtref{lp_write(+Handle, +Format, +File)}{lp_write/3}{../bips/lib/eplex/lp_write-3.html}}
917
918Write out the problem in the solver state represented by Handle to the file
919File in format Format.
920
921\aname{constrcoll}{}
922\subsubsection{\biptxtref{normalise_cstrs(+Constraints, -NormConstraints, -NonlinConstr)}{normalise_cstrs/3}{../bips/lib/eplex/normalise_cstrs-3.html}}
923where Constraints is a list of terms of the form
924X \verb.$=. Y, X \verb.$>=. Y or X \verb.$=<. Y 
925where X and Y are arithmetic expressions.
926The linear constraints are returned in normalised form in NormConstraints,
927the nonlinear ones are returned unchanged in NonlinConstr.
928
929\section{Cutpool Constraints}
930
931\index{cutpool constraints}
932\index{global cuts}
933In eplex, constraints added to a problem are removed on
934backtracking. However, it is sometimes possible to discover constraints
935that are valid for the whole problem, which the user wish to apply even
936after backtracking -- such constraints are referred to as `global cuts'. 
937
938In addition, the user may want to remove some constraints from
939the problem being solved, because they do not help to constrain the
940problem, but they slow down the solving of the problem. 
941
942To support this, eplex allow constraints to be added to a
943{\it cutpool} associated with a problem, instead of directly to the
944problem. The main differences from the normal problem constraints are:
945\begin{itemize}
946\item they are {\it not\/} removed on backtracking. Once
947added to a cutpool, a constraint exists until the problem itself is
948destroyed. 
949\item they are handled differently during solving, and the user has more
950control on how the external solver takes the constraints into account.
951\end{itemize}
952
953\subsection{Solving a Problem with Cutpool Constraints}
954Logically, cutpool constraints are always valid for the problem, and so
955should be considered when the problem is solved.  Unlike normal
956constraints, cutpool constraints are not necessarily added to the
957solver's problem matrix, and if they are added, they are added only for the
958solving, and removed from the problem matrix after the solving. 
959
960When the external solver 
961solves the problem, eplex ensures that the cutpool constraints are
962consistent with the problem, i.e. none of the constraints are
963violated. The cutpool constraints can either be added to the problem matrix
964immediately, or they can be checked for violation after the solver solves
965the problem. Any
966violated constraints are then added to the problem, and the problem
967resolved. This is repeated until either a fix-point is reached, where no
968constraints are violated, or if the external solver is unable to solve the
969problem. 
970
971If the external solver does not produce a solution, then:
972\begin{itemize}
973\item if the problem is unbounded, any outstanding cutpool constraints are
974added to the problem matrix without checking for violation,  and the
975external solver is invoked for one more time. This is because the extra
976constraints may make the problem bounded. 
977\item if the problem is infeasible, then failure occurs as normal (with the
978default infeasible handler behaviour). 
979\end{itemize}
980
981This multiple invocation of the solver occurs within an eplex's call to the
982external solver to solve a problem, and so the process should be
983transparent to the user, except that the setting of the timeout applies to
984each solver invocation, rather than to the whole solving process.
985
986The user can specify how the cutpool constraints are treated: they can be
987either added to the problem matrix before invoking the solver, or only
988added if violated. In addition, cutpool constraints
989can be made inactive, in which case they are not considered for adding to
990the problem matrix at all (and are not checked for violations). This is
991provided for efficiency reason -- if the user knows for certain
992constraints would not be violated by the solution, they can be made
993inactive. It is the user's responsibility to ensure the correctness in this
994case. 
995
996Unlike normal problem constraints, cutpool constraints cannot add new
997variables to the problem, i.e. the constraint must only involve problem
998variables that are present in the problem during solver set up. This is
999because cutpool constraints are globally valid, and so cannot involve
1000variables that may not exist after backtracking. Variables can be added to
1001a problem before solver set up by posting constraints involving them,
1002including \bipref{reals/1}{../bips/lib/eplex/reals-1.html}, which simply
1003declares variables as problem variables. 
1004
1005Additionally, each cutpool constraint belongs to a named
1006group, specified when the constraint is added.  This allows the user to
1007classify the cutpool constraints into different groups, and then
1008manipulate a groups of constraints as a whole, {\it e.g.\/} making them all
1009inactive. A default group is predefined, to which cutpool constraints
1010belongs unless specified otherwise. To add cutpool constraints to other
1011groups, the group name must first be created with the
1012{\tt cutpool_group} option of
1013\bipref{lp_get/3}{../bips/lib/eplex/lp_get-3.html}.
1014
1015\subsection{Predicate-specific Support}
1016Constraints are added to the cutpool using:
1017
1018\subsubsection{\biptxtref{lp_add_cutpool_constraints/4}{lp_add_cutpool_constraints/4}{../bips/lib/eplex/lp_add_cutpool_constraints-4.html}}
1019    Add the constraints to the cut-pool associated with the
1020    problem specified by the handle. By default, the constraints belong to
1021    the default group, and are active and have the `add initially' status
1022    set. These can be over-ridden by the Options argument. The predicate
1023    returns a list of indices for these constraints in Idxs.
1024
1025Information on cutpool constraints can be obtained using the 
1026{\tt cutpool_info}
1027option of \bipref{lp_get/3}{../bips/lib/eplex/lp_get-3.html}. The status of
1028a cutpool constraint, such as its active status,  can be set using the 
1029{\tt cutpool_option} option of
1030\bipref{lp_set/3}{../bips/lib/eplex/lp_set-3.html} -- the change is
1031non-logical, i.e. it is not undone on backtracking.
1032
1033Using lp_get/3 and lp_set/3, the user can program their own algorithms to
1034control how the cutpool constraints are treated when the problem is solved.
1035For example, the user may want to make a whole group of constraints
1036inactive because they seem to slow the solver down but do not produce
1037better solutions. In this case, the user can use lp_get/3 to obtain all the
1038current constraints in the group, and then use lp_set/3 to set these
1039constraints to inactive.
1040
1041As cutpool constraints are not added directly to the problem matrix, this
1042affects the library predicates that deals with the problem state:
1043
1044\begin{itemize}
1045\item row-wise solution states like dual and slack values include only the
1046cutpool constraints that are actually added to the problem. These are added
1047after the normal constraints, and their order in the matrix can be obtained
1048using the {\tt cutpool_info(last_added, index)} option of
1049\bipref{lp_get/3}{../bips/lib/eplex/lp_get-3.html}. 
1050\item the {\tt constraints} and {\tt constraints_norm} options of
1051\bipref{lp_get/3}{../bips/lib/eplex/lp_get-3.html} returns only the normal
1052constraints. Other options that returns information about the problem
1053(e.g. {\tt num_rows}) also do not include the cutpool constraints.  
1054\item 
1055\bipref{eplex_write/2}{../bips/lib/eplex/eplex_write-2.html} and 
1056\bipref{lp_write/3}{../bips/lib/eplex/lp_write-3.html} will dump all 
1057the active cutpool constraints with the problem. This may be different from
1058the actual problem solved by the external solver because not all active
1059cutpool constraints need be added to the problem, and the order of these
1060constraints could be different. To dump the exact problem solved by the
1061external solver, use the {\tt write_before_solve} option of the solver
1062instead. 
1063\end{itemize}
1064
1065\section{Multiple Solver States}
1066
1067This library allows multiple solver states to be maintained in the same
1068program. Each solver state represents an eplex problem. For the external
1069solver, each solver state is completely independent. For {\eclipse}, the
1070solver states may share variables in the constraints or objective
1071functions, but the constraints posted to the problem and to the cutpools
1072are specific to each solver state. The eplex library maintains separate
1073solution values for each solver state, and it is up to the user to
1074reconcile these solution values if they are different.
1075
1076\index{unification!eplex variables}
1077When two eplex variables are unified, then the library ensures that the
1078now single variable maintains the eplex values from both variables. The one
1079exception is when two variables from the same solver state is unified.In
1080this case, eplex will merge its representations of the two columns into one:
1081the bounds of the columns are merged (and failure will occur if the
1082merged bound is empty); all the unified columns are constrained to integers
1083if one of them was constrained to integer previously, and 
1084an equality constraint between the two variables is sent to the
1085solver state, but the user can only obtain one eplex value from the unified
1086variable, even though in the external solver, the variable is still
1087represented as two variables (columns in the matrix). 
1088
1089It is possible to turn off this automatic sending of the equality
1090constraints by specifying `no' for the option {\tt
1091post_equality_when_unified} (in solver setup, or via \verb'eplex_set/2'). 
1092The reason is that some solvers automatically perform unification
1093when they know that two variables are the same. For example, for
1094the constraint {\tt X \$= Y + Z}, if {\tt Y} becomes 0, then {\tt X} and
1095{\tt Z} may be unified by the solver maintaining the constraint. If the
1096same constraint was also posted to the eplex solver state, then there is no
1097need to send the redundant constraint. However, if the external solver
1098state did not have the constraint, then it can become inconsistent with
1099that of {\eclipse} if the equality constraint is not sent. Therefore, only
1100turn off sending of equality constraints if you are certain you know what
1101you are doing.
1102
1103The merging of the bounds may trigger the solver if the bounds trigger
1104condition was specified. Note however that the deviating_bounds trigger
1105condition is not tested for, because there is no longer a valid solution
1106value for the merged columns. 
1107
1108\section{External Solver Output and Log}
1109The external solver's output can be controlled using:
1110\begin{description}
1111\item[{\tt lp_set(SolverChannel, +(Stream))}]
1112Send output from SolverChannel to the \eclipse\ I/O stream Stream.
1113\item[{\tt lp_set(SolverChannel, -(Stream))}]
1114Stop sending output from SolverChannel to the \eclipse\ I/O stream Stream.
1115\end{description}
1116SolverChannel is one of
1117{\tt result_channel, error_channel, warning_channel, log_channel},
1118and Stream is an \eclipse\ stream identifier (e.g. {\tt output},
1119or the result of an open/3 operation).
1120By default, {\tt error_channel} is directed to \eclipse's {\tt error} stream,
1121{\tt warning_channel} to {\tt warning_output} stream,
1122while {\tt result_channel} and {\tt log_channel} are suppressed.
1123To see the output on these channels, do for instance
1124\begin{verbatim}
1125:- lp_set(result_channel, +output), lp_set(log_channel, +log_output).
1126\end{verbatim}
1127Similarly, to create a log file:
1128\begin{verbatim}
1129:- open("mylog.log", write, logstream), lp_set(log_channel, +logstream).
1130\end{verbatim}
1131and to stop logging:
1132\begin{verbatim}
1133:- lp_set(log_channel, -logstream), close(logstream).
1134\end{verbatim}
1135
1136
1137\section{Dealing with Large and Other Non-standard Numbers}
1138
1139In many external solvers, infinities or very large numbers are not handled
1140directly. Instead, these solvers define a large (floating point) number to
1141be infinity. However, the problem that is sent to the external solver may
1142contain values greater than the solver's notion of infinity. This is
1143handled in the following way:
1144
1145\begin{itemize}
1146\item If a variable's range extends beyond the solver's infinity, the range
1147is rounded down. 
1148\item If some coefficient (constant) in the problem is outside the solver's
1149range, an out of range error would be raised when this is detected (and the
1150problem is not passed to the external solver).
1151\end{itemize}
1152
1153In addition, {\eclipse} supports numeric types that are not generally
1154available, e.g.\ bounded real and rational. These are converted into
1155floating point numbers before they are passed to the external solver.
1156
1157\section{Error Handling}
1158
1159The external solver's optimisation can abort without completely solving the
1160problem, because of some error, or some resource limit was reached. Eplex
1161classifies these into the following cases, with default ways of handling them:
1162
1163\begin{description}
1164    \item[suboptimal]
1165    	This means that a solution was found but it may be suboptimal.
1166	The default behaviour is to print a warning and succeed.
1167    \item[unbounded]
1168	This means that the problem is unbounded.  The default
1169	behaviour is to bind Cost to infinity (positive or negative
1170	depending on the optimisation direction), print a warning and
1171	succeed.  CAUTION: No solution values are computed when the
1172	problem is unbounded, so unless the problem was set up with
1173	the solution(no) option, an error will occur when trying to
1174	continue as if the optimisation had succeeded.
1175   \item[infeasible]
1176        This means that the problem is infeasible. Note that it is possible
1177        for a problem that is on the boundary between feasible and
1178    	infeasible to be returned as feasible or infeasible, depending on
1179    	the solver and solving method used. The default behaviour is to
1180    	fail. 
1181    \item[unknown]
1182    	This means that due to the solution method chosen, it is unknown
1183	whether the problem is unbounded or infeasible. The default
1184	behaviour is to print a warning and fail (even though this
1185	may be logically wrong!).
1186    \item[abort]
1187    	Some other error condition occurred during optimisation.
1188	The default behaviour is to print an error and abort.
1189\end{description}
1190
1191The default behaviours can be overridden for each problem by giving a user
1192defined goal to handle each case during problem setup in
1193eplex_solver_setup/4 (lp_setup/4, lp_demon_setup/5, or later with
1194eplex_set/2 or lp_set/3) as an option. If given, the user defined goal will
1195be executed instead. The user defined handler could for instance change
1196parameter settings and call lp_solve again. 
1197
1198Redefining the default behaviour for infeasible problems allows the
1199infeasible problem state to be examined, e.g.\ by extracting the IIS
1200(Irreducible Infeasible Subsystem) from it with 
1201\bipref{eplex_get_iis/4}{../bips/lib/eplex/eplex_get_iis-4.html}.
1202It is recommended that the
1203user-defined handler should fail after examining the state to preserve
1204the correct logical behaviour.
1205 
1206\label{lpevents}
1207The default behaviour is implemented by raising the events {\tt
1208eplex_suboptimal}, {\tt eplex_unbounded}, {\tt eplex_infeasible}, {\tt eplex_unknown} and {\tt
1209eplex_abort} for the different abort cases. These events can themselves be
1210redefined to change the default behaviours. However, as this changes the
1211behaviour globally, it is not recommended.
1212
1213Note that in the user defined handlers, it may be possible to extract some
1214useful bound information on the optimal objective value using the
1215best_bound and worst_bound options of lp_get/3.
1216
1217%----------------------------------------------------------------------
1218\section{Solver Behaviour Differences}
1219%----------------------------------------------------------------------
1220In general, an MP problem can have more than one optimal solution (i.e.\ 
1221different sets of assignments to the problem variables that gives the
1222optimal objective value). Any of these solutions is correct, and the
1223external solver will return one of them. It is possible for a different
1224solver (or even a different version of the same solver) to return another
1225of these solutions. If the user's program uses the solution values, then it
1226is possible that the detailed behaviour of the program could depend on the
1227solver being used. 
1228
1229The solution that is returned can also depend on the detailed settings of
1230the floating point unit of the processor. Thus changing some of these
1231settings may change the solution that is returned. It is thus possible for
1232eplex to give different solutions on the same machine and solver if these
1233settings are changed (e.g.\ when {\eclipse} is embedded into a Java
1234application). 
1235
1236%----------------------------------------------------------------------
1237\section{Solver Specific Information}
1238%----------------------------------------------------------------------
1239\label{specificsolver}
1240
1241The external solvers currently supported by the eplex library are:
1242\begin{itemize}
1243\item XPRESS-MP, a product of Dash Optimisation (www.dashoptimization.com)
1244\item CPLEX, a product of ILOG (www.ilog.com)
1245\item Solvers accessed via OSI, an Open Source solver interface of the
1246COIN-OR project (www.coin-or.org). Various solvers are supported via OSI
1247using the generic interface. The following are currently distributed:
1248\begin{itemize}
1249\item clpcbc: COIN-OR's CLP linear solver in combination with the CBC
1250branch and cut framework for MIP problems. The CLP solver may be compiled
1251with third-party open-source packages, such as University of Florida's 
1252AMD package (www.cise.ufl.edu/research/sparse/amd/), used by CLP barrier
1253solve. Eplex provides specialised
1254support for this combination, which enhance the features provided and
1255improve performance for solving larger MIP problems. 
1256\item symclp: COIN-OR's SYMPHONY MILP solver (with CLP as the linear
1257solver). Supported via the generic OSI API.
1258\end{itemize}
1259\end{itemize}
1260Both Dash and ILOG offer academic licences at discounted prices, or academic
1261partner programs.
1262
1263To load a specific solver explicitly, use:
1264
1265\begin{verbatim}
1266:- lib(eplex_cplex).
1267:- lib(eplex_xpress).
1268:- lib(eplex_osi_clpcbc). 
1269:- lib(eplex_osi_symclp). 
1270\end{verbatim}
1271\index{eplex_cplex}
1272\index{eplex_xpress}
1273\index{XPRESS-MP}
1274\index{CPLEX}
1275The first line explicitly requests the CPLEX solver, the second line
1276explicitly requests the XPRESS-MP solver. Note that these solvers must be
1277available for your machine for the above to work. The third line requests
1278the CLP/CBC solver, and the fourth line requests the SYMPHONY solver.
1279
1280%To load a specific solver using ic for the interval representation, use:
1281%\begin{verbatim}
1282%:- lib(ic_eplex_cplex).
1283%:- lib(ic_eplex_xpress).
1284%\end{verbatim}
1285%\index{ic_eplex_cplex}
1286%\index{ic_eplex_xpress}
1287
1288\subsection{Versions and Licences}
1289
1290All the solvers supported by the library come in various versions.
1291The set of supported solver versions may vary between different
1292releases of {\eclipse}; please refer to the release notes.
1293%In addition,
1294%for XPRESS-MP, we distinguish versions included with {\eclipse}: the `OEM'
1295%versions, from versions obtained independently by the user. 
1296
1297For commercial solvers, you may require solver licence to use the solver, and
1298depending on which solver licence you have (for the commercial solvers), 
1299which version of it, and which hardware and operating system,
1300you need to use the matching version of this interface.\footnote{Note that
1301it is {\bf not} sufficient that you have a valid license and solver
1302libraries for a particular version of the solver. That solver version must
1303also be supported by the release of {\eclipse} you are using.}
1304Because an {\eclipse} installation can be shared between several
1305computers on a network, we have provided you with the possibility
1306to tell the system which licence you have on which machine.
1307To configure your local installation, simply add one line for
1308each computer with the appropriate licence to the file
1309\verb+<eclipsedir>/lib/eplex_lic_info.ecl+, where \verb+<eclipsedir>+
1310is the directory or folder where your {\eclipse} installation resides.
1311The file contains lines of the form
1312\begin{quote} \begin{verbatim}
1313licence(Hostname, Solver, Version, LicStr, LicNum).
1314\end{verbatim} \end{quote}
1315For example, if you have CPLEX version 7.5 on machine \verb+workhorse+,
1316%and both the OEM and non-OEM XPRESS-MP version 13.26 on machine
1317%\verb+mule+, and your internet
1318and XPRESS-MP version 15.20 (with the license file located in
1319\verb'/my/xpress/license') on machine \verb+mule+, and your Internet
1320domain is \verb'+icparc.ic.ac.uk',
1321you would add the lines
1322\begin{quote} \begin{verbatim}
1323licence('workhorse.icparc.ic.ac.uk', cplex, '75', '', 0).
1324licence('mule.icparc.ic.ac.uk', xpress, '1520', '/my/xpress/license', 0).         
1325\end{verbatim} \end{quote}
1326The hostname must match the result of get_flag(hostname,H),
1327converted to an atom (this is normally the complete Internet domain name,
1328rather than just the machine).
1329Version is formed from the concatenation of the major and minor version
1330numbers. 
1331%In the case of OEM XPRESS-MP, this is followed by the postfix
1332%\verb+icp+. 
1333The meaning of LicStr and LicNum depends on the optimiser:
1334For an open source solver, both LicStr and LicNum are unused, as no license
1335is required.
1336For CPLEX with normal licenses, they are unused (the environment
1337variable \verb'ILOG_LICENSE_FILE' should be set to the CPLEX
1338license file \verb'access.ilm' as usual). 
1339%LicStr is a string containing the environment settings
1340%for runtime licences, e.g.\ "CPLEXLICENSE=/usr/local/cplexlic.ptr",
1341%and LicNum is the serial number for runtime licences.
1342For XPRESS-MP,
1343%if the OEM version is used, LicStr should be the atom
1344%\verb+default+, as the licensing is handled internally by eplex. For other
1345%versions of the library,
1346LicStr is a string specifying the directory where
1347licence file is located (overrides value of XPRESS environment
1348variable). LicNum is unused. % in both cases.
1349If a machine has more than one licence and lib(eplex) is called, the
1350first one listed in \verb+eplex_lic_info.ecl+ will be used.
1351
1352%----------------------------------------------------------------------
1353\subsection{Solver Differences}
1354%----------------------------------------------------------------------
1355
1356While the eplex library allows the user to write code in a
1357solver-independent way, there are differences between the solvers that
1358the user should be aware of:
1359
1360\begin{itemize}
1361\item Different solvers may support different features. In particular,
1362solvers may support different methods of solving the problem, and solving
1363 of problems with a quadratic objective is not supported for all solvers.
1364At the very minimum, solvers must be able to solve (optimise) linear
1365 problems with a linear objective. Currently, all supported solvers can
1366 solve linear and MIP problems, with a Simplex solver.
1367\item Some features may be poorly supported by a particular solver, and
1368 some feature (such as the relaxed probe of 
1369\bipref{eplex_probe/2}{../bips/lib/eplex/eplex_probe-2.html} may be
1370supported slightly differently).
1371\item Performance for specific problem may vary very significantly (orders
1372of magnitude) between different solvers. This does not necessarily indicate 
1373that one solver would consistently out perform another. In addition, the
1374different solvers may return a different optimal solution to specific
1375problems, i.e.\ with the same objective value, but different solution
1376values for the variables.
1377\item The solvers have different solver parameters to control/tune the
1378solver. These can be accessed from eplex, and is one of the only places
1379 where the user code may need to be solver specific.
1380\end{itemize}
1381
1382\subsubsection{CPLEX}
1383CPLEX supports solving of linear and mixed integer problems, both with a
1384linear objective (LP and MILP), and also with a quadratic objective (QP and
1385MIQP). It supports various solving methods: simplex (primal and dual),
1386barrier, network simplex, and sifting. 
1387
1388In recent versions of CPLEX, the relaxed and fixed probes are implemented 
1389by removing
1390the integer information from the problem and solving the relaxed problem,
1391and then adding the interger information back. This is because the CPLEX
1392API does not provide access to the root node of the MIP solve.
1393
1394CPLEX have only global parameters.
1395  
1396\subsubsection{OSI}
1397The features provided by eplex OSI is determined by the actual
1398solver(s) used via OSI, and to a lesser extent by what the OSI API
1399supports. The OSI API is mainly designed to support solving of 
1400linear and, to a lesser extent, MILP problems. However, for specific
1401solvers, in particular the CLP/CBC combination, eplex
1402directly access the solvers' own API to provide some functionality not
1403supportable via OSI. Note however the OSI API is constantly evolving and
1404improving, so some of the features may be directly supported via OSI in the
1405future. 
1406
1407The sources for the porjects in COIN-OR can be downloaded from {\tt
1408www.coin-or.org/download.html}. 
1409Features {\bf not} supported by OSI: 
1410\begin{itemize}
1411\item changing of solving method (it does support specifying if the problem
1412should be solved as a primal or dual)
1413\item problems with a quadratic objective function
1414\item time-outs from solving a problem.
1415\item obtaining detailed information about the MIP solving process,
1416especially when the MIP search was not complete, such as
1417the best MIP objective bound.
1418\item determining if an aborted solve have a suboptimal solution 
1419\item writing out a problem with a maximising objective function in LP or
1420MPS format.
1421\item writing out or reading in an quadratic objective function in LP or (extended)
1422MPS format.
1423\item use of Special Order Set (SOS)
1424\item extracting an IIS for an infeasible problem
1425\end{itemize}
1426
1427\paragraph{osi_clpcbc} Supports primal, dual simplex and barrier (interior
1428point) methods for
1429solving linear and MIP problems with linear objective function, and linear
1430problems with a quadratic objective function. It also supports time-outs, and is better 
1431at determining the state of an aborted problem than using OSI on its own.
1432For the MIP related functionality, the MIP solver CBC is accessed directly
1433rather than through OSI, and this 
1434provides better MIP support: SOS (both SOS 1 and 2) are supported, and more information on the MIP
1435solver state is available, and a more sophisticated MIP search (using the
1436standalone CBC Solver) than the default is performed, generally
1437leading to faster MIP solves.
1438
1439For the barrier/interior point method, CLP can take advantage of
1440third-party packages to order a sparse matrix prior to Cholesky
1441factorisation. Such packages are crucial to the performance of the
1442barrier method, and currently the binary distribution of CLP is compiled
1443with University of Florida's AMD (Approximate Minimum Degree ordering) 
1444package. The source for this package needs to be downloaded separately from
1445the COIN-OR project at {\tt www.cise.ufl.edu/research/sparse/amd/}. 
1446
1447A quadratic objective needs to be in postive semi-definite form, but
1448currently CLP does not check this, and the result of trying to solve a
1449problem with a quadratic objective not in positive semi-definite form
1450is undefined.
1451
1452The problem representation is stored by CLP, and one performance issue 
1453when using CLP is that incrementally adding new constraints to
1454a problem after solver setup can be expensive, as the whole problem has to
1455be copied and expanded for each addition. It is therefore more efficient to 
1456either post the constraints before problem setup, or add a large number of
1457constraints in one go, e.g.\ by using 
1458\bipref{eplex_add_constraints/3}{../bips/lib/eplex/eplex_add_constraints-3.html}.
1459Unfortunately, this can be less memory efficient than incrementally adding
1460the constraints, if those constraints are only needed by eplex and not at
1461the {\eclipse} level.
1462
1463\paragraph{osi_symclp} Supports primal and dual simplex methods for
1464solving linear and MILP problems. MILP is currently performed via the 
1465standard OSI API, and so has the same restrictions. Special Order Sets
1466(SOS) are not supported. Time-outs are not supported.
1467 
1468Another restriction is due to SYMPHONY, which does not allow the objective
1469coefficients to be modified after a problem have been solved once. Thus the
1470objective changing probes are not supported.
1471
1472\subsubsection{XPRESS}
1473XPRESS supports solving of linear and mixed integer problems, both with a
1474linear objective (LP and MILP), and also with a quadratic objective (QP and
1475MIQP). It supports simplex (primal and dual) and barrier solving methods.
1476
1477XPRESS does not maintain an optimisation direction with the problem;
1478instead it requires this to be specified each time the problem is
1479solved. As such, the optimisation direction, given in a LP format
1480specification of the problem, is ignored when a problem is read in, and
1481when writing a problem, minimisation is assumed. 
1482
1483
1484
1485%----------------------------------------------------------------------
1486\subsection{Access to External Solver's Control Parameters}
1487%----------------------------------------------------------------------
1488
1489The external solver has a number of control
1490parameters that affect the way it works.
1491These can be queried and modified using the
1492\bipref{lp_get/2}{../bips/lib/eplex/lp_get-2.html},  
1493\bipref{eplex_get/2}{../bips/lib/eplex/eplex_get-2.html},
1494\bipref{lp_get/3}{../bips/lib/eplex/lp_get-3.html}, and
1495\bipref{lp_set/2}{../bips/lib/eplex/lp_set-2.html}, 
1496\bipref{eplex_set/2}{../bips/lib/eplex/eplex_set-2.html}, 
1497\bipref{lp_set/3}{../bips/lib/eplex/lp_set-3.html} predicates respectively:
1498
1499\subsubsection{\biptxtref{lp_get(+Handle, optimizer_param(+ParamName), -Value)}{lp_get/3}{../bips/lib/eplex/lp_get-3.html}}
1500Retrieve the value of a control parameter for the external solver for the
1501problem represented by Handle. These
1502parameters are solver specific; see
1503\bipref{lp_get/3}{../bips/lib/eplex/lp_get-3.html} for more details..
1504
1505\subsubsection{\biptxtref{EplexInstance:eplex_get(optimizer_param(+ParamName), -Value)}{eplex:eplex_get/2}{../bips/lib/eplex/eplex_get-2.html}}
1506Like lp_get/3, but get a control parameter for the external solver
1507associated with the specified eplex instance. 
1508
1509\subsubsection{\biptxtref{lp_get(optimizer_param(+ParamName), -Value)}{lp_get/2}{../bips/lib/eplex/lp_get-2.html}}
1510Retrieve the global value of a control parameter for the external solver. The
1511parameters and the exact meaning of `global' is solver specific: if the
1512solver does not have global parameters, this gets the global default value,
1513rather than the globally applicable value. The parameters are as in lp_get/3.
1514
1515\subsubsection{\biptxtref{lp_set(+Handle, optimizer_param(+ParamName), +Value)}{lp_set/3}{../bips/lib/eplex/lp_set-3.html}}
1516Set a control parameter for the external solver for the problem represented
1517by Handle. If the external solver does not have problem specific
1518parameters, this will raise an unimplemented functionality exception. 
1519The parameters are as in lp_get/3.
1520
1521\subsubsection{\biptxtref{EplexInstance:eplex_set(optimizer_param(+ParamName), +Value)}{eplex_set/2}{../bips/lib/eplex/eplex_set-2.html}}
1522Like lp_set/3, but set a control parameter for the external solver
1523associated with the specified eplex instance. 
1524
1525\subsubsection{\biptxtref{lp_set(optimizer_param(+ParamName), +Value)}{lp_set/2}{../bips/lib/eplex/lp_set-2.html}}
1526Set a control parameter for the external solver for the problem globally.
1527If the external solver does not have global parameters, this will set the
1528global default for the parameter. The parameters are as in lp_get/3.
1529
1530\subsubsection{\biptxtref{lp_get(optimizer, -Value)}{lp_get/2}{../bips/lib/eplex/lp_get-2.html}
1531and \biptxtref{lp_get(optimizer_version, -Value)}{lp_get/2}{../bips/lib/eplex/lp_get-2.html}}
1532Retrieve the name (currently 'cplex', 'xpress' or 'osi') and version of the 
1533external optimizer.
1534This can be used to write portable code even when using solver-specific settings:
1535\begin{verbatim}
1536\begin{verbatim}
1537    ( lp_get(optimizer, xpress) ->
1538        lp_set(Handler, optimize_param(maxnode), 100)
1539    ; lp_get(optimizer, cplex) ->
1540        lp_set(Handler, optimize_param(node_limit), 100)
1541    ; lp_get(optimizer, osi) ->
1542        (lp_get(optimizer_version, clpcbc) -> 
1543             lp_set(Handler, optimize_param(node_limit), 100)
1544        ;
1545             true
1546        )
1547    ), ...
1548\end{verbatim}
1549
1550\index{mathematical programming, interface to|)}
1551\index{mixed integer programming, interface to|)}
1552\index{linear programming, interface to|)}
1553\index{quadratic programming, interface to|)}
1554\index{simplex solver, interface to|)}
1555%HEVEA\cutend
1556