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