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