|
| 1 | +/* -------------------------------------------------------------------------- |
| 2 | + * File: FoodManufact.java |
| 3 | + * Version 12.10.0 |
| 4 | + * -------------------------------------------------------------------------- |
| 5 | + * Licensed Materials - Property of IBM |
| 6 | + * 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 5655-Y21 |
| 7 | + * Copyright IBM Corporation 2001, 2019. All Rights Reserved. |
| 8 | + * |
| 9 | + * US Government Users Restricted Rights - Use, duplication or |
| 10 | + * disclosure restricted by GSA ADP Schedule Contract with |
| 11 | + * IBM Corp. |
| 12 | + * -------------------------------------------------------------------------- |
| 13 | + * |
| 14 | + * FoodManufact.java - An implementation of an example from H.P. |
| 15 | + * Williams' book Model Building in Mathematical |
| 16 | + * Programming. This example solves a |
| 17 | + * food production planning problem. It |
| 18 | + * demonstrates the use of CPLEX's |
| 19 | + * linearization capability. |
| 20 | + */ |
| 21 | + |
| 22 | +import com.ibm.wmlconnector.WMLConnector; |
| 23 | +import ilog.concert.*; |
| 24 | +import ilog.cplex.*; |
| 25 | + |
| 26 | +public class FoodManufact { |
| 27 | + final static int v1 = 0; |
| 28 | + final static int v2 = 1; |
| 29 | + final static int o1 = 2; |
| 30 | + final static int o2 = 3; |
| 31 | + final static int o3 = 4; |
| 32 | + |
| 33 | + final static double[][] cost = {{110.0, 120.0, 130.0, 110.0, 115.0}, |
| 34 | + {130.0, 130.0, 110.0, 90.0, 115.0}, |
| 35 | + {110.0, 140.0, 130.0, 100.0, 95.0}, |
| 36 | + {120.0, 110.0, 120.0, 120.0, 125.0}, |
| 37 | + {100.0, 120.0, 150.0, 110.0, 105.0}, |
| 38 | + { 90.0, 100.0, 140.0, 80.0, 135.0}}; |
| 39 | + |
| 40 | + public static void main (String args[]) { |
| 41 | + int nMonths = cost.length; |
| 42 | + int nProducts = cost[0].length; |
| 43 | + |
| 44 | + //try (IloCplex cplex = new IloCplex()) { |
| 45 | + try (IloCplex cplex = new CplexWithWML(new MyProdBetaV4Credentials())) { |
| 46 | + IloNumVar[] produce = cplex.numVarArray(nMonths, 0, Double.MAX_VALUE); |
| 47 | + IloNumVar[][] use = new IloNumVar[nMonths][]; |
| 48 | + IloNumVar[][] buy = new IloNumVar[nMonths][]; |
| 49 | + IloNumVar[][] store = new IloNumVar[nMonths][]; |
| 50 | + |
| 51 | + for (int i = 0; i < nMonths; i++) { |
| 52 | + use[i] = cplex.numVarArray(nProducts, 0, Double.MAX_VALUE); |
| 53 | + buy[i] = cplex.numVarArray(nProducts, 0, Double.MAX_VALUE); |
| 54 | + store[i] = cplex.numVarArray(nProducts, 0, 1000.0); |
| 55 | + } |
| 56 | + |
| 57 | + for (int p = 0; p < nProducts; p++) { |
| 58 | + store[nMonths-1][p].setLB(500.0); |
| 59 | + store[nMonths-1][p].setUB(500.0); |
| 60 | + } |
| 61 | + |
| 62 | + IloNumExpr profit = cplex.numExpr(); |
| 63 | + for (int i = 0; i < nMonths; i++) { |
| 64 | + // Not more than 200 tons of vegetable oil can be refined |
| 65 | + cplex.addLe(cplex.sum(use[i][v1], use[i][v2]), 200.0); |
| 66 | + |
| 67 | + // Not more than 250 tons of non-vegetable oil can be refined |
| 68 | + cplex.addLe(cplex.sum(use[i][o1], use[i][o2], use[i][o3]), 250.0); |
| 69 | + |
| 70 | + // Constraints on food composition |
| 71 | + cplex.addLe(cplex.prod(3., produce[i]), |
| 72 | + cplex.sum(cplex.prod(8.8, use[i][v1]), |
| 73 | + cplex.prod(6.1, use[i][v2]), |
| 74 | + cplex.prod(2.0, use[i][o1]), |
| 75 | + cplex.prod(4.2, use[i][o2]), |
| 76 | + cplex.prod(5.0, use[i][o3]))); |
| 77 | + cplex.addGe(cplex.prod(6., produce[i]), |
| 78 | + cplex.sum(cplex.prod(8.8, use[i][v1]), |
| 79 | + cplex.prod(6.1, use[i][v2]), |
| 80 | + cplex.prod(2.0, use[i][o1]), |
| 81 | + cplex.prod(4.2, use[i][o2]), |
| 82 | + cplex.prod(5.0, use[i][o3]))); |
| 83 | + cplex.addEq(produce[i], cplex.sum(use[i])); |
| 84 | + |
| 85 | + // Raw oil can be stored for later use |
| 86 | + if (i == 0) { |
| 87 | + for (int p = 0; p < nProducts; p++) |
| 88 | + cplex.addEq(cplex.sum(500.0, buy[i][p]), |
| 89 | + cplex.sum(use[i][p], store[i][p])); |
| 90 | + } |
| 91 | + else { |
| 92 | + for (int p = 0; p < nProducts; p++) |
| 93 | + cplex.addEq(cplex.sum(store[i-1][p], buy[i][p]), |
| 94 | + cplex.sum(use[i][p], store[i][p])); |
| 95 | + } |
| 96 | + |
| 97 | + // Logical constraints: |
| 98 | + // The food cannot use more than 3 oils |
| 99 | + // (or at least two oils must not be used) |
| 100 | + cplex.addGe(cplex.sum(cplex.eq(use[i][v1], 0), |
| 101 | + cplex.eq(use[i][v2], 0), |
| 102 | + cplex.eq(use[i][o1], 0), |
| 103 | + cplex.eq(use[i][o2], 0), |
| 104 | + cplex.eq(use[i][o3], 0)), 2); |
| 105 | + |
| 106 | + // When an oil is used, the quantity must be at least 20 tons |
| 107 | + for (int p = 0; p < nProducts; p++) |
| 108 | + cplex.add(cplex.or(cplex.eq(use[i][p], 0), |
| 109 | + cplex.ge(use[i][p], 20))); |
| 110 | + |
| 111 | + // If products v1 or v2 are used, then product o3 is also used |
| 112 | + cplex.add(cplex.ifThen(cplex.or(cplex.ge(use[i][v1], 20), |
| 113 | + cplex.ge(use[i][v2], 20)), |
| 114 | + cplex.ge(use[i][o3], 20))); |
| 115 | + |
| 116 | + // Objective function |
| 117 | + profit = cplex.sum (profit, cplex.prod(150, produce[i])); |
| 118 | + profit = cplex.diff(profit, cplex.scalProd(cost[i], buy[i])); |
| 119 | + profit = cplex.diff(profit, cplex.prod(5, cplex.sum(store[i]))); |
| 120 | + } |
| 121 | + |
| 122 | + cplex.addMaximize(profit); |
| 123 | + |
| 124 | + if ( cplex.solve() ) { |
| 125 | + System.out.println("Solution status: " + cplex.getStatus()); |
| 126 | + System.out.println(" Maximum profit = " + cplex.getObjValue()); |
| 127 | + for (int i = 0; i < nMonths; i++) { |
| 128 | + System.out.println(" Month " + i); |
| 129 | + System.out.print(" . buy "); |
| 130 | + for (int p = 0; p < nProducts; p++) |
| 131 | + System.out.print(cplex.getValue(buy[i][p]) + "\t "); |
| 132 | + System.out.println(); |
| 133 | + System.out.print(" . use "); |
| 134 | + for (int p = 0; p < nProducts; p++) |
| 135 | + System.out.print(cplex.getValue(use[i][p]) + "\t "); |
| 136 | + System.out.println(); |
| 137 | + System.out.print(" . store "); |
| 138 | + for (int p = 0; p < nProducts; p++) |
| 139 | + System.out.print(cplex.getValue(store[i][p]) + "\t "); |
| 140 | + System.out.println(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + catch (IloException e) { |
| 146 | + System.err.println("Concert exception caught: " + e); |
| 147 | + System.exit(-1); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments