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