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