examples/food2.mod
changeset 1 c445c931472f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/food2.mod	Mon Dec 06 13:09:21 2010 +0100
     1.3 @@ -0,0 +1,150 @@
     1.4 +/* Food Manufacture 2, section 12.2 in
     1.5 + * Williams, "Model Building in Mathematical Programming"
     1.6 + *
     1.7 + * Sebastian Nowozin <nowozin@gmail.com>
     1.8 + */
     1.9 +
    1.10 +set oils;
    1.11 +set month;
    1.12 +
    1.13 +/* Buying prices of the raw oils in the next six month. */
    1.14 +param buyingprices{month,oils};
    1.15 +
    1.16 +/* Actual amount bought in each month. */
    1.17 +var buys{month,oils} >= 0;
    1.18 +
    1.19 +/* Stock for each oil. */
    1.20 +var stock{month,oils} >= 0;
    1.21 +
    1.22 +/* Price of the produced product */
    1.23 +param productprice >= 0;
    1.24 +param storagecost;
    1.25 +
    1.26 +param oilhardness{oils} >= 0;
    1.27 +param M >= 0;
    1.28 +
    1.29 +/* Actual amount of output oil produced in each month */
    1.30 +var production{m in month} >= 0;
    1.31 +var useoil{m in month, o in oils} >= 0, <= M;
    1.32 +var useoilb{m in month, o in oils}, binary;
    1.33 +
    1.34 +maximize totalprofit:
    1.35 +    sum{m in month} productprice*production[m]
    1.36 +    - sum{m in month, o in oils} buyingprices[m,o]*buys[m,o]
    1.37 +    - sum{m in month, o in oils} storagecost*stock[m,o];
    1.38 +
    1.39 +/* Constraints */
    1.40 +
    1.41 +/* 1. Starting stock */
    1.42 +s.t. startstock{o in oils}:
    1.43 +    stock[1,o] = 500;
    1.44 +s.t. endstock{o in oils}:
    1.45 +    stock[6,o] + buys[6,o] - useoil[6,o] >= 500;
    1.46 +
    1.47 +/* 2. Stock constraints */
    1.48 +s.t. stocklimit{m in month, o in oils}:
    1.49 +    stock[m,o] <= 1000;
    1.50 +
    1.51 +s.t. production1{m in month, o in oils}:
    1.52 +    useoil[m,o] <= stock[m,o] + buys[m,o];
    1.53 +s.t. production2{m1 in month, m2 in month, o in oils : m2 = m1+1}:
    1.54 +    stock[m2,o] = stock[m1,o] + buys[m1,o] - useoil[m1,o];
    1.55 +
    1.56 +s.t. production3a{m in month}:
    1.57 +    sum{o in oils} oilhardness[o]*useoil[m,o] >= 3*production[m];
    1.58 +s.t. production3b{m in month}:
    1.59 +    sum{o in oils} oilhardness[o]*useoil[m,o] <= 6*production[m];
    1.60 +
    1.61 +s.t. production4{m in month}:
    1.62 +    production[m] = sum{o in oils} useoil[m,o];
    1.63 +
    1.64 +/* 3. Refining constraints */
    1.65 +s.t. refine1{m in month}:
    1.66 +    useoil[m,"VEG1"]+useoil[m,"VEG2"] <= 200;
    1.67 +s.t. refine2{m in month}:
    1.68 +    useoil[m,"OIL1"]+useoil[m,"OIL2"]+useoil[m,"OIL3"] <= 250;
    1.69 +
    1.70 +/* 4. Additional conditions:
    1.71 + *    i) The food may never be made up of more than three oils every month
    1.72 + */
    1.73 +s.t. useoilb_calc{m in month, o in oils}:
    1.74 +    M*useoilb[m,o] >= useoil[m,o];
    1.75 +s.t. useoilb_limit{m in month}:
    1.76 +    sum{o in oils} useoilb[m,o] <= 3;
    1.77 +
    1.78 +/* ii) If an oil is used in a month, at least 20 tons must be used.
    1.79 + */
    1.80 +s.t. useminimum{m in month, o in oils}:
    1.81 +    20*useoilb[m,o] <= useoil[m,o];
    1.82 +
    1.83 +/* iii) If either of VEG1 or VEG2 is used in a month, OIL2 must also be used
    1.84 + */
    1.85 +s.t. use_oil2a{m in month}:
    1.86 +    useoilb[m,"VEG1"] <= useoilb[m,"OIL3"];
    1.87 +s.t. use_oil2b{m in month}:
    1.88 +    useoilb[m,"VEG2"] <= useoilb[m,"OIL3"];
    1.89 +
    1.90 +solve;
    1.91 +
    1.92 +for {m in month} {
    1.93 +    printf "Month %d\n", m;
    1.94 +    printf "PRODUCE %4.2f tons, hardness %4.2f\n", production[m],
    1.95 +        (sum{o in oils} oilhardness[o]*useoil[m,o]) / (sum{o in oils} useoil[m,o]);
    1.96 +
    1.97 +    printf "\tVEG1\tVEG2\tOIL1\tOIL2\tOIL3\n";
    1.98 +    printf "STOCK";
    1.99 +    printf "%d", m;
   1.100 +    for {o in oils} {
   1.101 +        printf "\t%4.2f", stock[m,o];
   1.102 +    }
   1.103 +    printf "\nBUY";
   1.104 +    for {o in oils} {
   1.105 +        printf "\t%4.2f", buys[m,o];
   1.106 +    }
   1.107 +    printf "\nUSE";
   1.108 +    printf "%d", m;
   1.109 +    for {o in oils} {
   1.110 +        printf "\t%4.2f", useoil[m,o];
   1.111 +    }
   1.112 +    printf "\n";
   1.113 +    printf "\n";
   1.114 +}
   1.115 +printf "Total profit: %4.2f\n",
   1.116 +    (sum{m in month} productprice*production[m]
   1.117 +    - sum{m in month, o in oils} buyingprices[m,o]*buys[m,o]
   1.118 +    - sum{m in month, o in oils} storagecost*stock[m,o]);
   1.119 +printf "      turnover: %4.2f\n",
   1.120 +    sum{m in month} productprice*production[m];
   1.121 +printf "      buying costs: %4.2f\n",
   1.122 +    sum{m in month, o in oils} buyingprices[m,o]*buys[m,o];
   1.123 +printf "      storage costs: %4.2f\n",
   1.124 +    sum{m in month, o in oils} storagecost*stock[m,o];
   1.125 +
   1.126 +
   1.127 +data;
   1.128 +
   1.129 +param : oils : oilhardness :=
   1.130 +    VEG1    8.8
   1.131 +    VEG2    6.1
   1.132 +    OIL1    2.0
   1.133 +    OIL2    4.2
   1.134 +    OIL3    5.0 ;
   1.135 +
   1.136 +set month := 1 2 3 4 5 6;
   1.137 +
   1.138 +param buyingprices
   1.139 +
   1.140 +:           VEG1    VEG2    OIL1    OIL2    OIL3    :=
   1.141 +
   1.142 +1           110     120     130     110     115
   1.143 +2           130     130     110     90      115
   1.144 +3           110     140     130     100     95
   1.145 +4           120     110     120     120     125
   1.146 +5           100     120     150     110     105
   1.147 +6           90      100     140     80      135 ;
   1.148 +
   1.149 +param productprice := 150;
   1.150 +param storagecost := 5;
   1.151 +param M := 1000;
   1.152 +
   1.153 +end;