Difference between revisions of "ApCoCoA-1:GLPK.LPSolve"

From ApCoCoAWiki
Line 31: Line 31:
 
First we want to discuss a rather easy example.  
 
First we want to discuss a rather easy example.  
 
<example>
 
<example>
We want to maximize the Function y = - 1/2x, with the two conditions y ≤ 6 - 3/4x and y ≥ 1 - x and the bounds 0 ≤ x ≤ 6 and 1/3 ≤ y ≤ 4.
+
We want to maximize the Function y = - 1/2x,  
 +
with the two conditions y ≤ 6 - 3/4x and y ≥ 1 - x and the bounds 0 ≤ x ≤ 6 and 1/3 ≤ y ≤ 4.
  
 
We prename the input of GLPK.LPSol-function.
 
We prename the input of GLPK.LPSol-function.
Line 51: Line 52:
  
 
<example>
 
<example>
<em>Linear programming example 1996 MBA exam</em>
+
''Linear programming example 1996 MBA exam''
  
 
A cargo plane has three compartments for storing cargo: front, centre and rear.  
 
A cargo plane has three compartments for storing cargo: front, centre and rear.  
Line 75: Line 76:
 
(if any) of each cargo C1, C2, C3 and C4 should be accepted and how to distribute each  
 
(if any) of each cargo C1, C2, C3 and C4 should be accepted and how to distribute each  
 
among the compartments so that the total profit for the flight is maximised.  
 
among the compartments so that the total profit for the flight is maximised.  
 +
  
 
To solve this problem we had to compose a linear program.
 
To solve this problem we had to compose a linear program.
Line 80: Line 82:
 
Variables
 
Variables
  
We need to decide how much of each of the four cargoes to put in each of the three compartments. Hence let:
+
We need to decide how much of each of the four cargoes to put in each of the three compartments.  
 +
Hence let:
  
xij be the number of tonnes of cargo i (i=1,2,3,4 for C1, C2, C3 and C4 respectively) that is put into compartment j (j=1 for Front, j=2 for Centre and j=3 for Rear) where xij >=0 i=1,2,3,4; j=1,2,3
+
xij be the number of tonnes of cargo i (i=1,2,3,4 for C1, C2, C3 and C4 respectively) that is put into  
 +
compartment j (j=1 for Front, j=2 for Centre and j=3 for Rear) where xij >=0 i=1,2,3,4; j=1,2,3
  
 
Note here that we are explicitly told we can split the cargoes into any proportions (fractions) that we like.
 
Note here that we are explicitly told we can split the cargoes into any proportions (fractions) that we like.
 +
 
Constraints
 
Constraints
  
    * cannot pack more of each of the four cargoes than we have available
+
* cannot pack more of each of the four cargoes than we have available
  
x11 + x12 + x13 <= 18
+
x11 + x12 + x13 18
x21 + x22 + x23 <= 15
+
x21 + x22 + x23 15
x31 + x32 + x33 <= 23
+
x31 + x32 + x33 23
x41 + x42 + x43 <= 12
+
x41 + x42 + x43 12
  
    * the weight capacity of each compartment must be respected
+
* the weight capacity of each compartment must be respected
  
x11 + x21 + x31 + x41 <= 10
+
x11 + x21 + x31 + x41 10
x12 + x22 + x32 + x42 <= 16
+
x12 + x22 + x32 + x42 16
x13 + x23 + x33 + x43 <= 8
+
x13 + x23 + x33 + x43 8
  
    * the volume (space) capacity of each compartment must be respected
+
* the volume (space) capacity of each compartment must be respected
  
480x11 + 650x21 + 580x31 + 390x41 <= 6800
+
480x11 + 650x21 + 580x31 + 390x41 6800
480x12 + 650x22 + 580x32 + 390x42 <= 8700
+
480x12 + 650x22 + 580x32 + 390x42 8700
480x13 + 650x23 + 580x33 + 390x43 <= 5300
+
480x13 + 650x23 + 580x33 + 390x43 5300
  
    * the weight of the cargo in the respective compartments must be the same proportion of that compartment's weight capacity to maintain the balance of the plane
+
* the weight of the cargo in the respective compartments must be the same proportion  
 +
  of that compartment's weight capacity to maintain the balance of the plane
  
 
[x11 + x21 + x31 + x41]/10 = [x12 + x22 + x32 + x42]/16 = [x13 + x23 + x33 + x43]/8
 
[x11 + x21 + x31 + x41]/10 = [x12 + x22 + x32 + x42]/16 = [x13 + x23 + x33 + x43]/8
 +
 
Objective
 
Objective
  
Line 117: Line 124:
 
The basic assumptions are:
 
The basic assumptions are:
  
    * that each cargo can be split into whatever proportions/fractions we desire
+
* that each cargo can be split into whatever proportions/fractions we desire
    * that each cargo can be split between two or more compartments if we so desire
+
* that each cargo can be split between two or more compartments if we so desire
    * that the cargo can be packed into each compartment (for example if the cargo was spherical it would not be possible to pack a compartment to volume capacity, some free space is inevitable in sphere packing)
+
* that the cargo can be packed into each compartment (for example if the cargo was spherical it would  
    * all the data/numbers given are accurate
+
  not be possible to pack a compartment to volume capacity, some free space is inevitable in sphere packing)
 +
* all the data/numbers given are accurate
  
The advantages of using a software package to solve the above linear program, rather than a judgemental approach are:
+
The advantages of using a software package to solve the above linear program, rather than a judgemental  
 +
approach are:
  
    * actually maximise profit, rather than just believing that our judgemental solution maximises profit (we may have bad judgement, even if we have an MBA!)
+
* actually maximise profit, rather than just believing that our judgemental solution maximises profit  
    * makes the cargo loading the decision one that we can solve in a routine operational manner on a computer, rather than having to exercise judgement each and every time we want to solve it
+
  (we may have bad judgement, even if we have an MBA!)
    * problems that can be appropriately formulated as linear programs are almost always better solved by computers than by people
+
* makes the cargo loading the decision one that we can solve in a routine operational manner on a computer,  
    * can perform sensitivity analysis very easily using a computer
+
  rather than having to exercise judgement each and every time we want to solve it
 +
* problems that can be appropriately formulated as linear programs are almost always better solved by computers  
 +
  than by people
 +
* can perform sensitivity analysis very easily using a computer
  
 
</example>
 
</example>

Revision as of 17:03, 13 November 2008

LPSolve

solve linear programms

Syntax

GLPK.LPSolv(Objective_Function:POLYNOM, EQ_Polynomials:LIST, LE_Polynomials:LIST, GE_Polynomials:LIST, Bounds:LIST, Methode:STRING, MinMax:STRING)

Description


The basic idea behind this package is to make the linear optimization program GLPK usable in/with ApCoCoA.

The package GLPK contains various functions that let you make use of the GLPK library, rather the stand-alone LP/MIP Solver glpsol.

Objective_Function: A linear polynomial which is equivalent to the linear objective Function.

EQ_Polynomials: List of linear polynomials, which are equivalent to the equality-part in the list of conditions.

LE_Polynomials: List of linear polynomials, which are equivalent to the lower or equal-part in the list of conditions.

GE_Polynomials: List of linear polynomials, which are equivalent to the greater or equal-part in the list of conditions.

Bounds: List of lists with two elements. Each List contains the lower and upper bounds for each variable.

Methode: You can choose between the inner-point-method (InnerP) or the simplex-algorithm (Simplex).

MinMax: Minimization (Min) or maximization (Max), that's the question.


When you execute the package, you had to specify the path of the LP solver glpsol, i.e. GlpsolPath:="/usr/bin/";

and the working path, i.e. WorkingPath:="/home/user/";


First we want to discuss a rather easy example.

Example

We want to maximize the Function y = - 1/2x, 
with the two conditions y ≤ 6 - 3/4x and y ≥ 1 - x and the bounds 0 ≤ x ≤ 6 and 1/3 ≤ y ≤ 4.

We prename the input of GLPK.LPSol-function.
OF := 1/2x + y;
LE := 3/4x + y - 6;
GE := x + y - 1;
Bounds:=[[0,6], [1/3,4]];

Then we compute the solution with
Use S::=Q[x,y];
$GLPK.LPSol(OF, LE, GE, Bounds, Simplex, Max);

And we achieve x = 8/3 and y = 4.

LinOpt.jpg


Example

''Linear programming example 1996 MBA exam''

A cargo plane has three compartments for storing cargo: front, centre and rear. 
These compartments have the following limits on both weight and space:

Compartment   Weight capacity (tonnes)   Space capacity (cubic metres)
Front         10                         6800
Centre        16                         8700
Rear          8                          5300

Furthermore, the weight of the cargo in the respective compartments must be the same 
proportion of that compartment's weight capacity to maintain the balance of the plane.

The following four cargoes are available for shipment on the next flight:

Cargo   Weight (tonnes)   Volume (cubic metres/tonne)  Profit (£/tonne)
C1      18                480                          310
C2      15                650                          380
C3      23                580                          350
C4      12                390                          285

Any proportion of these cargoes can be accepted. The objective is to determine how much 
(if any) of each cargo C1, C2, C3 and C4 should be accepted and how to distribute each 
among the compartments so that the total profit for the flight is maximised. 


To solve this problem we had to compose a linear program.

Variables

We need to decide how much of each of the four cargoes to put in each of the three compartments. 
Hence let:

xij be the number of tonnes of cargo i (i=1,2,3,4 for C1, C2, C3 and C4 respectively) that is put into 
compartment j (j=1 for Front, j=2 for Centre and j=3 for Rear) where xij >=0 i=1,2,3,4; j=1,2,3

Note here that we are explicitly told we can split the cargoes into any proportions (fractions) that we like.

Constraints

* cannot pack more of each of the four cargoes than we have available

x11 + x12 + x13 ≤ 18
x21 + x22 + x23 ≤ 15
x31 + x32 + x33 ≤ 23
x41 + x42 + x43 ≤ 12

* the weight capacity of each compartment must be respected

x11 + x21 + x31 + x41 ≤ 10
x12 + x22 + x32 + x42 ≤ 16
x13 + x23 + x33 + x43 ≤ 8

* the volume (space) capacity of each compartment must be respected

480x11 + 650x21 + 580x31 + 390x41 ≤ 6800
480x12 + 650x22 + 580x32 + 390x42 ≤ 8700
480x13 + 650x23 + 580x33 + 390x43 ≤ 5300

* the weight of the cargo in the respective compartments must be the same proportion 
  of that compartment's weight capacity to maintain the balance of the plane

[x11 + x21 + x31 + x41]/10 = [x12 + x22 + x32 + x42]/16 = [x13 + x23 + x33 + x43]/8

Objective

The objective is to maximise total profit, i.e.

maximise 310[x11+ x12+x13] + 380[x21+ x22+x23] + 350[x31+ x32+x33] + 285[x41+ x42+x43]

The basic assumptions are:

* that each cargo can be split into whatever proportions/fractions we desire
* that each cargo can be split between two or more compartments if we so desire
* that the cargo can be packed into each compartment (for example if the cargo was spherical it would 
  not be possible to pack a compartment to volume capacity, some free space is inevitable in sphere packing)
* all the data/numbers given are accurate

The advantages of using a software package to solve the above linear program, rather than a judgemental 
approach are:

* actually maximise profit, rather than just believing that our judgemental solution maximises profit 
  (we may have bad judgement, even if we have an MBA!)
* makes the cargo loading the decision one that we can solve in a routine operational manner on a computer, 
  rather than having to exercise judgement each and every time we want to solve it
* problems that can be appropriately formulated as linear programs are almost always better solved by computers 
  than by people
* can perform sensitivity analysis very easily using a computer