examples/food2.mod
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

- Generated files and doc/notes are removed
alpar@1
     1
/* Food Manufacture 2, section 12.2 in
alpar@1
     2
 * Williams, "Model Building in Mathematical Programming"
alpar@1
     3
 *
alpar@1
     4
 * Sebastian Nowozin <nowozin@gmail.com>
alpar@1
     5
 */
alpar@1
     6
alpar@1
     7
set oils;
alpar@1
     8
set month;
alpar@1
     9
alpar@1
    10
/* Buying prices of the raw oils in the next six month. */
alpar@1
    11
param buyingprices{month,oils};
alpar@1
    12
alpar@1
    13
/* Actual amount bought in each month. */
alpar@1
    14
var buys{month,oils} >= 0;
alpar@1
    15
alpar@1
    16
/* Stock for each oil. */
alpar@1
    17
var stock{month,oils} >= 0;
alpar@1
    18
alpar@1
    19
/* Price of the produced product */
alpar@1
    20
param productprice >= 0;
alpar@1
    21
param storagecost;
alpar@1
    22
alpar@1
    23
param oilhardness{oils} >= 0;
alpar@1
    24
param M >= 0;
alpar@1
    25
alpar@1
    26
/* Actual amount of output oil produced in each month */
alpar@1
    27
var production{m in month} >= 0;
alpar@1
    28
var useoil{m in month, o in oils} >= 0, <= M;
alpar@1
    29
var useoilb{m in month, o in oils}, binary;
alpar@1
    30
alpar@1
    31
maximize totalprofit:
alpar@1
    32
    sum{m in month} productprice*production[m]
alpar@1
    33
    - sum{m in month, o in oils} buyingprices[m,o]*buys[m,o]
alpar@1
    34
    - sum{m in month, o in oils} storagecost*stock[m,o];
alpar@1
    35
alpar@1
    36
/* Constraints */
alpar@1
    37
alpar@1
    38
/* 1. Starting stock */
alpar@1
    39
s.t. startstock{o in oils}:
alpar@1
    40
    stock[1,o] = 500;
alpar@1
    41
s.t. endstock{o in oils}:
alpar@1
    42
    stock[6,o] + buys[6,o] - useoil[6,o] >= 500;
alpar@1
    43
alpar@1
    44
/* 2. Stock constraints */
alpar@1
    45
s.t. stocklimit{m in month, o in oils}:
alpar@1
    46
    stock[m,o] <= 1000;
alpar@1
    47
alpar@1
    48
s.t. production1{m in month, o in oils}:
alpar@1
    49
    useoil[m,o] <= stock[m,o] + buys[m,o];
alpar@1
    50
s.t. production2{m1 in month, m2 in month, o in oils : m2 = m1+1}:
alpar@1
    51
    stock[m2,o] = stock[m1,o] + buys[m1,o] - useoil[m1,o];
alpar@1
    52
alpar@1
    53
s.t. production3a{m in month}:
alpar@1
    54
    sum{o in oils} oilhardness[o]*useoil[m,o] >= 3*production[m];
alpar@1
    55
s.t. production3b{m in month}:
alpar@1
    56
    sum{o in oils} oilhardness[o]*useoil[m,o] <= 6*production[m];
alpar@1
    57
alpar@1
    58
s.t. production4{m in month}:
alpar@1
    59
    production[m] = sum{o in oils} useoil[m,o];
alpar@1
    60
alpar@1
    61
/* 3. Refining constraints */
alpar@1
    62
s.t. refine1{m in month}:
alpar@1
    63
    useoil[m,"VEG1"]+useoil[m,"VEG2"] <= 200;
alpar@1
    64
s.t. refine2{m in month}:
alpar@1
    65
    useoil[m,"OIL1"]+useoil[m,"OIL2"]+useoil[m,"OIL3"] <= 250;
alpar@1
    66
alpar@1
    67
/* 4. Additional conditions:
alpar@1
    68
 *    i) The food may never be made up of more than three oils every month
alpar@1
    69
 */
alpar@1
    70
s.t. useoilb_calc{m in month, o in oils}:
alpar@1
    71
    M*useoilb[m,o] >= useoil[m,o];
alpar@1
    72
s.t. useoilb_limit{m in month}:
alpar@1
    73
    sum{o in oils} useoilb[m,o] <= 3;
alpar@1
    74
alpar@1
    75
/* ii) If an oil is used in a month, at least 20 tons must be used.
alpar@1
    76
 */
alpar@1
    77
s.t. useminimum{m in month, o in oils}:
alpar@1
    78
    20*useoilb[m,o] <= useoil[m,o];
alpar@1
    79
alpar@1
    80
/* iii) If either of VEG1 or VEG2 is used in a month, OIL2 must also be used
alpar@1
    81
 */
alpar@1
    82
s.t. use_oil2a{m in month}:
alpar@1
    83
    useoilb[m,"VEG1"] <= useoilb[m,"OIL3"];
alpar@1
    84
s.t. use_oil2b{m in month}:
alpar@1
    85
    useoilb[m,"VEG2"] <= useoilb[m,"OIL3"];
alpar@1
    86
alpar@1
    87
solve;
alpar@1
    88
alpar@1
    89
for {m in month} {
alpar@1
    90
    printf "Month %d\n", m;
alpar@1
    91
    printf "PRODUCE %4.2f tons, hardness %4.2f\n", production[m],
alpar@1
    92
        (sum{o in oils} oilhardness[o]*useoil[m,o]) / (sum{o in oils} useoil[m,o]);
alpar@1
    93
alpar@1
    94
    printf "\tVEG1\tVEG2\tOIL1\tOIL2\tOIL3\n";
alpar@1
    95
    printf "STOCK";
alpar@1
    96
    printf "%d", m;
alpar@1
    97
    for {o in oils} {
alpar@1
    98
        printf "\t%4.2f", stock[m,o];
alpar@1
    99
    }
alpar@1
   100
    printf "\nBUY";
alpar@1
   101
    for {o in oils} {
alpar@1
   102
        printf "\t%4.2f", buys[m,o];
alpar@1
   103
    }
alpar@1
   104
    printf "\nUSE";
alpar@1
   105
    printf "%d", m;
alpar@1
   106
    for {o in oils} {
alpar@1
   107
        printf "\t%4.2f", useoil[m,o];
alpar@1
   108
    }
alpar@1
   109
    printf "\n";
alpar@1
   110
    printf "\n";
alpar@1
   111
}
alpar@1
   112
printf "Total profit: %4.2f\n",
alpar@1
   113
    (sum{m in month} productprice*production[m]
alpar@1
   114
    - sum{m in month, o in oils} buyingprices[m,o]*buys[m,o]
alpar@1
   115
    - sum{m in month, o in oils} storagecost*stock[m,o]);
alpar@1
   116
printf "      turnover: %4.2f\n",
alpar@1
   117
    sum{m in month} productprice*production[m];
alpar@1
   118
printf "      buying costs: %4.2f\n",
alpar@1
   119
    sum{m in month, o in oils} buyingprices[m,o]*buys[m,o];
alpar@1
   120
printf "      storage costs: %4.2f\n",
alpar@1
   121
    sum{m in month, o in oils} storagecost*stock[m,o];
alpar@1
   122
alpar@1
   123
alpar@1
   124
data;
alpar@1
   125
alpar@1
   126
param : oils : oilhardness :=
alpar@1
   127
    VEG1    8.8
alpar@1
   128
    VEG2    6.1
alpar@1
   129
    OIL1    2.0
alpar@1
   130
    OIL2    4.2
alpar@1
   131
    OIL3    5.0 ;
alpar@1
   132
alpar@1
   133
set month := 1 2 3 4 5 6;
alpar@1
   134
alpar@1
   135
param buyingprices
alpar@1
   136
alpar@1
   137
:           VEG1    VEG2    OIL1    OIL2    OIL3    :=
alpar@1
   138
alpar@1
   139
1           110     120     130     110     115
alpar@1
   140
2           130     130     110     90      115
alpar@1
   141
3           110     140     130     100     95
alpar@1
   142
4           120     110     120     120     125
alpar@1
   143
5           100     120     150     110     105
alpar@1
   144
6           90      100     140     80      135 ;
alpar@1
   145
alpar@1
   146
param productprice := 150;
alpar@1
   147
param storagecost := 5;
alpar@1
   148
param M := 1000;
alpar@1
   149
alpar@1
   150
end;