<Section id="lp.tutorial.intro">
    <Title/Introduction/

<P>
The introduction of linear programming solvers in Oz is based on
real-interval constraints introduced in <Ptr to="ct.casestudy">.

<P> The modules <<LP>> and <<RI>> are provided as contribution (being
part of the &moz3; distribution<note foot>The modules <<LP>> and <<RI>>
are <Em/not/ provided on any Windows platform.</note>) and can be accessed
either by <<< declare [LP RI] = {Module.link ['x-oz://contrib/LP'
'x-oz://contrib/RI']}>>> or by <<< import RI at 'x-oz://contrib/RI' LP
at 'x-oz://contrib/LP'>>> as part of a functor definition.


<P> The module <<LP>> uses per default <EM/LP_SOLVE 2.x/ as linear
programming solver. A version compatible with Mozart Oz can be
downloaded via:

<list>
<item><ref.extern
to="ftp://ftp.mozart-oz.org/pub/mozart/extras/lp_solve_2.3_mozart.tar.gz">ftp://ftp.mozart-oz.org/pub/mozart/extras/lp_solve_2.3_mozart.tar.gz</ref.extern>.
</list>

Unpack the archive and make it. You will be told what else has to be
done. Please note that we are not able to sort out any problems
concerning the actual <EM/LP_SOLVE 2.x/ solver and that we are not
responsible for that kind of problems &resp; bugs.

<P>
Linear programming solver (LP solver) handle problems of the following
kind <Ptr to="Chvantal:83">:

<Math display type=latex id="lp.tutorial.lp.general">
\begin{array}{lcccccclr}
\multicolumn{9}{l}{\mbox{\bf minimize resp. maximize:}} \\
& c_1 x_1 & + & \ldots & + & c_n x_n &   &   & \mbox{objective function}\\ \\
\multicolumn{9}{l}{\mbox{\bf subject to:}} \\
& a_{1,1} x_1 & + & \cdots & + & a_{n,1} x_n & \diamond & b_1 & \mbox{constraints}\\
& \vdots      &   &        &   & \vdots      &          & \vdots &  \\    
& a_{1,m} x_1 &  + & \cdots & + & a_{n,m} x_n & \diamond & b_m & \\
&             &    &         &  &            & & & (\diamond \in \{\le, = , \ge\}) \\ \\
& \multicolumn{7}{l}{l_i \le x_i \le u_i \;\;\;\;(i = 1,\ldots,n)} & \mbox{bound constraints}.
\end{array}
</Math>

<P>
The module <<LP>> provides a procedure <<LP.solve>> to call an LP
solver. Further, a procedure to configure the LP solver is provided
(see <Ptr.extern to="ozdoc:cpiref" key="lp.reference">).

<Para><Title/A simple example./
A simple example explains best how the LP solver is invoked:

<Code.Extern display to="simp_ex.oz" class=linenumbers>

<P>
The corresponding linear program is as follows:

<Math display type=latex id="lp.tutorial.lp.simp_ex">
\begin{array}{lcccc}
\mbox{\bf maximize:}   & & 8x_1 + 5x_2 &      &   \\
\mbox{\bf subject to:} &     & x_1 + x_2   & \le & 6 \\ 
                  &     & 9x_1 + 5x_2   & \le & 45 \\
& \multicolumn{4}{l}{x_1,x_2 \ge 0} 
\end{array}
</Math>

Note that the bound constraints for the LP solver are derived from the
current bounds of the real-interval variables. Further, when
minimizing the objective function the following constraint <Math
inline type=latex>c_1 x_1 + \ldots + c_n x_n \le \overline{\tt Sol}
</Math> is added.  On the other hand, the constraint <Math inline
type=latex> c_1 x_1 + \ldots + c_n x_n \ge \underline{\tt Sol}</Math>
is added when maximizing.

<P>
Before running the LP solver, the variables are constrained to
<<<X1&lt;real interval:[0, 1.79769e+308]&gt;>>> and 
<<<X2&lt;real interval:[0,1.79769e+308]&gt;.>>> 
The LP solver binds the
variables  to: <<X1=3.75>>, <<X2=2.25>>,
<<Sol=41.25>>, and <<Ret=optimal>>.

<Para><Title/The tutorial problem: Solving a multiknapsack problem./
This tutorial uses a multiknapsack problem to demonstrate the benefits
of combining finite domain constraint programming and linear
programming. First, we tackle the problem with finite domain
constraints and linear programming separately (see <Ptr
to="lp.tutorial.fd"> and <Ptr to="lp.tutorial.lp">). One difficulty
arises for linear programming: since integral solutions are required
and the LP solver returns non-integral solution, we have to implement
a branch&amp;bound solver to obtain an integral solution. Finally, we
combine both solvers.

<P>
Throughout this tutorial, we use a multi-knapsack problem (taken from
<Ptr to="BeringerDeBacker:95a">. The problem variables <Math>\mbox{\bf
x}</Math> represent the number of goods to be produced. Each good
requires certain resources: man power, materials, and machines
(represented by matrix <Math>\mbox{\bf A}</Math>) where a given
capacity per resource (<Math>\mbox{\bf b}</Math>) may not be
exceeded. Each good generates a profit according to <Math>\mbox{\bf
c}</Math> where the overall profit shall be maximal.

<P>
<Math display type=latex id="lp.tutorial.lp.det">
\begin{array}{l}
\begin{array}{llccccc}
\mbox{\bf maximize:}   & \mbox{\bf c} & \times & \mbox{\bf x} & & \\
\mbox{\bf subject to:} & \mbox{\bf A} & \times & \mbox{\bf x} & \le & \mbox{\bf b}\\
& \multicolumn{6}{l}{\mbox{{\bf x} are integral.}}
\end{array} \\
\mbox{where}\\
\begin{array}{l}
\mbox{\bf A}=\left[
\begin{array}{lcccccccccccccc}
\mbox{\textit{man:}} & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1\\
\mbox{\textit{material 1:}} & 0 & 4 & 5 & 0 & 0 & 0 & 0 & 4 & 5 & 0 & 0 & 0 & 0\\
\mbox{\textit{material 2:}} & 4 & 0 & 3 & 0 & 0 & 0 & 3 & 0 & 4 & 0 & 0 & 0 & 0\\
\mbox{\textit{machine 1:}} & 7 & 0 & 0 & 6 & 0 & 0 & 7 & 0 & 0 & 6 & 0 & 0 & 0\\
\mbox{\textit{machine 2:}} & 0 & 0 & 0 & 4 & 5 & 0 & 0 & 0 & 0 & 5 & 4 & 0 & 0\\
\mbox{\textit{machine 3:}} & 0 & 0 & 0 & 0 & 4 & 3 & 0 & 0 & 0 & 0 & 4 & 2 & 1\\
\mbox{\textit{machine 4:}} & 0 & 3 & 0 & 0 & 0 & 5 & 0 & 3 & 0 & 0 & 0 & 3 & 3\\
\end{array}
\right]
\mbox{\bf b}=\left[
\begin{array}{c}
14\\
17\\
20\\
34\\
26\\
16\\
16\\
\end{array}
\right]
\\ \\
\mbox{\bf c}=\left[
\begin{array}{ccccccccccccc}
5 & 7 & 5 & 11 & 8 & 10 & 6 & 8 & 3 & 12 & 9 & 8 & 4\\
\end{array}
\right]\\ \\
\mbox{\bf x}=\left[
\begin{array}{ccccccccccccc}
x_1 & x_2 & x_3 & x_4 & x_5 & x_6 & x_7 & x_8 & x_9 & x_{10} & x_{11} & x_{12} & x_{13}\\
\end{array}
\right]\\
\end{array}\\
\end{array}
</Math>

<P>
The problem specification as Oz term is as follows:

<Code.Extern display to="mks_prob.oz" class=linenumbers>


</Section>
