001 /*
002 * $Id: GroebnerBaseDistTest.java 3391 2010-12-05 13:18:42Z kredel $
003 */
004
005 package edu.jas.gb;
006
007
008 //import edu.jas.poly.GroebnerBase;
009
010 import java.io.IOException;
011 import java.io.Reader;
012 import java.io.StringReader;
013 import java.util.ArrayList;
014 import java.util.List;
015
016 import junit.framework.Test;
017 import junit.framework.TestCase;
018 import junit.framework.TestSuite;
019
020 import org.apache.log4j.BasicConfigurator; //import org.apache.log4j.Logger;
021
022 import edu.jas.kern.ComputerThreads;
023 import edu.jas.arith.BigRational;
024
025 import edu.jas.poly.GenPolynomial;
026 import edu.jas.poly.GenPolynomialRing;
027 import edu.jas.poly.GenPolynomialTokenizer;
028 import edu.jas.poly.PolynomialList;
029
030 import edu.jas.structure.RingElem;
031
032
033 /**
034 * Distributed GroebnerBase tests with JUnit.
035 * @author Heinz Kredel
036 */
037
038 public class GroebnerBaseDistTest extends TestCase {
039
040
041 //private static final Logger logger = Logger.getLogger(GroebnerBaseDistTest.class);
042
043 /**
044 * main
045 */
046 public static void main(String[] args) {
047 BasicConfigurator.configure();
048 junit.textui.TestRunner.run(suite());
049 //ComputerThreads.terminate();
050 }
051
052
053 /**
054 * Constructs a <CODE>GroebnerBaseDistTest</CODE> object.
055 * @param name String.
056 */
057 public GroebnerBaseDistTest(String name) {
058 super(name);
059 }
060
061
062 /**
063 * suite.
064 */
065 public static Test suite() {
066 TestSuite suite = new TestSuite(GroebnerBaseDistTest.class);
067 return suite;
068 }
069
070
071 int port = 4711;
072
073
074 String host = "localhost";
075
076
077 GenPolynomialRing<BigRational> fac;
078
079
080 List<GenPolynomial<BigRational>> L;
081
082
083 PolynomialList<BigRational> F;
084
085
086 List<GenPolynomial<BigRational>> G;
087
088
089 GroebnerBase<BigRational> bbseq;
090
091
092 GroebnerBaseDistributed<BigRational> bbdist;
093
094
095 GroebnerBaseDistributed<BigRational> bbdists;
096
097
098 GenPolynomial<BigRational> a;
099
100
101 GenPolynomial<BigRational> b;
102
103
104 GenPolynomial<BigRational> c;
105
106
107 GenPolynomial<BigRational> d;
108
109
110 GenPolynomial<BigRational> e;
111
112
113 int rl = 3; //4; //3;
114
115
116 int kl = 4;
117
118
119 int ll = 7;
120
121
122 int el = 3;
123
124
125 float q = 0.2f; //0.4f
126
127
128 int threads = 2;
129
130
131 @Override
132 protected void setUp() {
133 BigRational coeff = new BigRational(9);
134 fac = new GenPolynomialRing<BigRational>(coeff, rl);
135 a = b = c = d = e = null;
136 bbseq = new GroebnerBaseSeq<BigRational>();
137 bbdists = new GroebnerBaseDistributed<BigRational>(threads, port);
138 bbdist = new GroebnerBaseDistributed<BigRational>(threads, new OrderedSyzPairlist<BigRational>(), port);
139 }
140
141
142 @Override
143 protected void tearDown() {
144 a = b = c = d = e = null;
145 fac = null;
146 bbseq = null;
147 bbdist.terminate();
148 bbdist = null;
149 bbdists.terminate();
150 bbdists = null;
151 ComputerThreads.terminate();
152 }
153
154
155 /**
156 * Helper method to start threads with distributed clients.
157 *
158 */
159 Thread[] startThreads() {
160 Thread[] clients = new Thread[threads];
161 for (int t = 0; t < threads; t++) {
162 clients[t] = new Thread(new JunitClient(host, port));
163 clients[t].start();
164 }
165 return clients;
166 }
167
168
169 /**
170 * Helper method to stop threads with distributed clients.
171 *
172 */
173 void stopThreads(Thread[] clients) {
174 for (int t = 0; t < threads; t++) {
175 try {
176 clients[t].join();
177 } catch (InterruptedException e) {
178 }
179 }
180 }
181
182
183 /**
184 * Test distributed GBase.
185 *
186 */
187 public void testDistributedGBase() {
188
189 Thread[] clients;
190
191 L = new ArrayList<GenPolynomial<BigRational>>();
192
193 a = fac.random(kl, ll, el, q);
194 b = fac.random(kl, ll, el, q);
195 c = fac.random(kl, ll, el, q);
196 d = fac.random(kl, ll, el, q);
197 e = d; //fac.random(kl, ll, el, q );
198
199 assertTrue("not isZERO( a )", !a.isZERO());
200 L.add(a);
201
202 clients = startThreads();
203 L = bbdist.GB(L);
204 stopThreads(clients);
205 assertTrue("isGB( { a } )", bbseq.isGB(L));
206
207 assertTrue("not isZERO( b )", !b.isZERO());
208 L.add(b);
209 //System.out.println("L = " + L.size() );
210
211 clients = startThreads();
212 L = bbdist.GB(L);
213 stopThreads(clients);
214 assertTrue("isGB( { a, b } )", bbseq.isGB(L));
215
216 assertTrue("not isZERO( c )", !c.isZERO());
217 L.add(c);
218
219 clients = startThreads();
220 L = bbdist.GB(L);
221 stopThreads(clients);
222 assertTrue("isGB( { a, b, c } )", bbseq.isGB(L));
223
224 assertTrue("not isZERO( d )", !d.isZERO());
225 L.add(d);
226
227 clients = startThreads();
228 L = bbdist.GB(L);
229 stopThreads(clients);
230 assertTrue("isGB( { a, b, c, d } )", bbseq.isGB(L));
231
232 assertTrue("not isZERO( e )", !e.isZERO());
233 L.add(e);
234
235 clients = startThreads();
236 L = bbdist.GB(L);
237 stopThreads(clients);
238 assertTrue("isGB( { a, b, c, d, e } )", bbseq.isGB(L));
239 }
240
241
242 /**
243 * Test compare sequential with distributed GBase.
244 *
245 */
246 public void testSequentialDistributedGBase() {
247
248 Thread[] clients;
249
250 List<GenPolynomial<BigRational>> Gs, Gp = null;
251
252 L = new ArrayList<GenPolynomial<BigRational>>();
253
254 a = fac.random(kl, ll, el, q);
255 b = fac.random(kl, ll, el, q);
256 c = fac.random(kl, ll, el, q);
257 d = fac.random(kl, ll, el, q);
258 e = d; //fac.random(kl, ll, el, q );
259
260 L.add(a);
261 Gs = bbseq.GB(L);
262 clients = startThreads();
263 Gp = bbdist.GB(L);
264 stopThreads(clients);
265
266 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
267 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
268
269 L = Gs;
270 L.add(b);
271 Gs = bbseq.GB(L);
272 clients = startThreads();
273 Gp = bbdist.GB(L);
274 stopThreads(clients);
275 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
276 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
277
278 L = Gs;
279 L.add(c);
280 Gs = bbseq.GB(L);
281 clients = startThreads();
282 Gp = bbdist.GB(L);
283 stopThreads(clients);
284
285 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
286 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
287
288 L = Gs;
289 L.add(d);
290 Gs = bbseq.GB(L);
291 clients = startThreads();
292 Gp = bbdist.GB(L);
293 stopThreads(clients);
294
295 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
296 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
297
298 L = Gs;
299 L.add(e);
300 Gs = bbseq.GB(L);
301 clients = startThreads();
302 Gp = bbdist.GB(L);
303 stopThreads(clients);
304
305 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
306 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
307 }
308
309
310 /**
311 * Test Trinks7 GBase.
312 *
313 */
314 public void testTrinks7GBase() {
315 Thread[] clients;
316 String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
317 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
318 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
319 + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
320 Reader source = new StringReader(exam);
321 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
322 try {
323 F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
324 } catch (IOException e) {
325 fail("" + e);
326 }
327 //System.out.println("F = " + F);
328
329 clients = startThreads();
330 G = bbdist.GB(F.list);
331 stopThreads(clients);
332
333 assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G));
334 assertEquals("#GB(Trinks7) == 6", 6, G.size());
335 PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
336 //System.out.println("G = " + trinks);
337
338 }
339
340 }
341
342
343 /**
344 * Unit Test client to be executed by test threads.
345 */
346
347 class JunitClient<C extends RingElem<C>> implements Runnable {
348
349
350 private final String host;
351
352
353 private final int port;
354
355
356 JunitClient(String host, int port) {
357 this.host = host;
358 this.port = port;
359 }
360
361
362 public void run() {
363 GroebnerBaseDistributed<C> bbd;
364 bbd = new GroebnerBaseDistributed<C>(1, null, null, port);
365 try {
366 bbd.clientPart(host);
367 } catch (IOException ignored) {
368 }
369 bbd.terminate();
370 }
371 }