001/* 002 * $Id$ 003 */ 004 005package edu.jas.application; 006 007 008import java.util.Arrays; 009 010 011/** 012 * Examples for Integer Programming. 013 * @author Maximilian Nohr 014 */ 015public class IntegerProgramExamples { 016 017 018 /** 019 * Execute all examples. 020 * @param args 021 */ 022 public static void main(String[] args) { 023 example1(); 024 example2(); 025 example3(); 026 example4(); 027 example5(); 028 example6(); 029 example7(); 030 example8(); 031 // too big: example9(); 032 // too big: example10(); 033 } 034 035 036 /** 037 * Example p.360 CLOII 038 */ 039 public static void example1() { 040 IntegerProgram IP = new IntegerProgram(); 041 042 long t0 = System.currentTimeMillis(); 043 044 //bsp. s.360 CLOII 045 long[][] A0 = { { 4, 5, 1, 0 }, { 2, 3, 0, 1 } }; 046 long[] B0 = { 37, 20 }; 047 long[] C0 = { -11, -15, 0, 0 }; 048 049 long[] sol = IP.solve(A0, B0, C0); 050 051 long t1 = System.currentTimeMillis(); 052 long t = t1 - t0; 053 System.out.println("\n" + IP); 054 System.out.println("The solution is: " + Arrays.toString(sol)); 055 System.out.println("The computation needed " + t + " milliseconds."); 056 057 058 long[] BW = { 1, 2 }; //,3}; 059 060 sol = IP.solve(BW); 061 062 int count = 0; 063 for (int i = 0; i < 5; i++) { 064 for (int j = 0; j < 5; j++) { 065 B0[0] = i; 066 B0[1] = j; 067 068 sol = IP.solve(A0, B0, C0); 069 if (IP.getSuccess()) { 070 count++; 071 } 072 } 073 } 074 System.out.println(count + " times successful!"); 075 } 076 077 078 /** 079 * Example p.374 CLOII 10a 080 */ 081 public static void example2() { 082 IntegerProgram IP = new IntegerProgram(); 083 084 long t0 = System.currentTimeMillis(); 085 086 //bsp. s.374 CLOII 10a 087 long[][] A = { { 3, 2, 1, 1 }, { 4, 1, 1, 0 } }; 088 long[] B = { 10, 5 }; 089 long[] C = { 2, 3, 1, 5 }; 090 091 long[] sol = IP.solve(A, B, C); 092 093 //10b 094 long[] Bb = { 20, 14 }; 095 sol = IP.solve(Bb); 096 097 long t1 = System.currentTimeMillis(); 098 long t = t1 - t0; 099 System.out.println("\n" + IP); 100 System.out.println("The solution is: " + Arrays.toString(sol)); 101 System.out.println("The computation needed " + t + " milliseconds."); 102 } 103 104 105 /** 106 * Example p.372 CLOII 107 */ 108 public static void example3() { 109 IntegerProgram IP = new IntegerProgram(); 110 111 long t0 = System.currentTimeMillis(); 112 113 //bsp. s.372 CLOII 114 long[][] A2 = { { 3, -2, 1, 0 }, { 4, 1, -1, -1 } }; 115 long[] B2 = { -1, 5 }; 116 long[] C2 = { 1, 1000, 1, 100 }; 117 118 long[] sol = IP.solve(A2, B2, C2); 119 120 long t1 = System.currentTimeMillis(); 121 long t = t1 - t0; 122 System.out.println("\n" + IP); 123 System.out.println("The solution is: " + Arrays.toString(sol)); 124 System.out.println("The computation needed " + t + " milliseconds."); 125 } 126 127 128 public static void example4() { 129 IntegerProgram IP = new IntegerProgram(); 130 131 long t0 = System.currentTimeMillis(); 132 133 //bsp. s.374 10c 134 long[][] A3 = { { 3, 2, 1, 1, 0, 0 }, { 1, 2, 3, 0, 1, 0 }, { 2, 1, 1, 0, 0, 1 } }; 135 long[] B3 = { 45, 21, 18 }; 136 long[] C3 = { -3, -4, -2, 0, 0, 0 }; 137 138 long[] sol = IP.solve(A3, B3, C3); 139 140 long t1 = System.currentTimeMillis(); 141 long t = t1 - t0; 142 System.out.println("\n" + IP); 143 System.out.println("The solution is: " + Arrays.toString(sol)); 144 System.out.println("The computation needed " + t + " milliseconds."); 145 } 146 147 148 /** 149 * Example p.138 AAECC-9 150 */ 151 public static void example5() { 152 IntegerProgram IP = new IntegerProgram(); 153 154 long t0 = System.currentTimeMillis(); 155 156 //bsp. s.138 AAECC-9 157 long[][] A4 = { { 32, 45, -41, 22 }, { -82, -13, 33, -33 }, { 23, -21, 12, -12 } }; 158 long[] B4 = { 214, 712, 331 }; //im Beispiel keine b genannt 159 long[] C4 = { 1, 1, 1, 1 }; 160 161 long[] sol = IP.solve(A4, B4, C4); 162 163 long t1 = System.currentTimeMillis(); 164 long t = t1 - t0; 165 System.out.println("\n" + IP); 166 System.out.println("The solution is: " + Arrays.toString(sol)); 167 System.out.println("The computation needed " + t + " milliseconds."); 168 } 169 170 171 /** 172 * Example from M. Nohr 173 */ 174 public static void example6() { 175 IntegerProgram IP = new IntegerProgram(); 176 177 long t0 = System.currentTimeMillis(); 178 179 //eigenes beispiel 180 //System.out.println("example from mnohr:"); 181 long[][] A5 = { { 4, 3, 1, 0 }, { 3, 1, 0, 1 } }; 182 long[] B5 = { 200, 100 }; 183 long[] C5 = { -5, -4, 0, 0 }; 184 185 long[] sol = IP.solve(A5, B5, C5); 186 187 long t1 = System.currentTimeMillis(); 188 long t = t1 - t0; 189 System.out.println("\n" + IP); 190 System.out.println("The solution is: " + Arrays.toString(sol)); 191 System.out.println("The computation needed " + t + " milliseconds."); 192 } 193 194 195 /** 196 * Example unsolvable 197 */ 198 public static void example7() { 199 IntegerProgram IP = new IntegerProgram(); 200 201 long t0 = System.currentTimeMillis(); 202 203 long[][] A9 = { { 1, 1, 1, 1 }, { -1, -1, -1, -1 } }; 204 long[] B9 = { 1, 1 }; 205 long[] C9 = { 1, 1, 0, 0 }; 206 207 long[] sol = IP.solve(A9, B9, C9); 208 209 long t1 = System.currentTimeMillis(); 210 long t = t1 - t0; 211 System.out.println("\nunsolvable: " + IP); 212 System.out.println("The solution is: " + Arrays.toString(sol)); 213 System.out.println("The computation needed " + t + " milliseconds."); 214 } 215 216 217 /** 218 * Example ? 219 */ 220 public static void example8() { 221 IntegerProgram IP = new IntegerProgram(); 222 223 long t0 = System.currentTimeMillis(); 224 225 long[][] A8 = { { 4, 3, 1, 0 }, { 3, 1, 0, 1 } }; 226 long[] B8 = { 200, 100 }; 227 long[] C8 = { -5, -4, 0, 0 }; 228 229 long[] sol = IP.solve(A8, B8, C8); 230 231 long t1 = System.currentTimeMillis(); 232 long t = t1 - t0; 233 System.out.println("\n" + IP); 234 System.out.println("The solution is: " + Arrays.toString(sol)); 235 System.out.println("The computation needed " + t + " milliseconds."); 236 } 237 238 239 /** 240 * Example p.137 AAECC-9, too many vars 241 */ 242 public static void example9() { 243 IntegerProgram IP = new IntegerProgram(); 244 245 long t0 = System.currentTimeMillis(); 246 247 // bsp s.137 AAECC-9 auch too many vars 248 long[][] A7 = { { 2, 5, -3, 1, -2 }, { 1, 7, 2, 3, 1 }, { 4, -2, -1, -5, 3 } }; 249 long[] B7 = { 214, 712, 331 }; 250 long[] C7 = { 1, 1, 1, 1, 1 }; 251 252 long[] sol = IP.solve(A7, B7, C7); 253 254 long t1 = System.currentTimeMillis(); 255 long t = t1 - t0; 256 System.out.println("\n" + IP); 257 System.out.println("The solution is: " + Arrays.toString(sol)); 258 System.out.println("The computation needed " + t + " milliseconds."); 259 } 260 261 262 /** 263 * Example, too big 264 */ 265 public static void example10() { 266 IntegerProgram IP = new IntegerProgram(); 267 268 long t0 = System.currentTimeMillis(); 269 //too many variables 270 long[][] A6 = { { 5, 11, 23, -5, 4, -7, -4 }, { 1, -5, -14, 15, 7, -8, 10 }, 271 { 3, 21, -12, 7, 9, 11, 8 } }; 272 long[] B6 = { 23, 19, 31 }; 273 long[] C6 = { 1, 1, 1, 1, 1, 1, 1 }; 274 275 long[] sol = IP.solve(A6, B6, C6); 276 277 long t1 = System.currentTimeMillis(); 278 long t = t1 - t0; 279 System.out.println("\n" + IP); 280 System.out.println("The solution is: " + Arrays.toString(sol)); 281 System.out.println("The computation needed " + t + " milliseconds."); 282 } 283 284}