001/*
002 * $Id: GroebnerBaseDistHybridECTest.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.kern.ComputerThreads;
023import edu.jas.poly.GenPolynomial;
024import edu.jas.poly.GenPolynomialRing;
025import edu.jas.poly.GenPolynomialTokenizer;
026import edu.jas.poly.PolynomialList;
027import edu.jas.util.ExecutableServer;
028
029
030/**
031 * Distributed hybrid GroebnerBase tests with JUnit.
032 * @author Heinz Kredel
033 */
034
035public class GroebnerBaseDistHybridECTest extends TestCase {
036
037
038
039    /**
040     * main
041     */
042    public static void main(String[] args) {
043        junit.textui.TestRunner.run(suite());
044        //ComputerThreads.terminate();
045    }
046
047
048    /**
049     * Constructs a <CODE>GroebnerBaseDistHybridECTest</CODE> object.
050     * @param name String.
051     */
052    public GroebnerBaseDistHybridECTest(String name) {
053        super(name);
054    }
055
056
057    /**
058     * suite.
059     */
060    public static Test suite() {
061        TestSuite suite = new TestSuite(GroebnerBaseDistHybridECTest.class);
062        return suite;
063    }
064
065
066    int port = 55711;
067
068
069    String host = "localhost";
070
071
072    String mfile = "examples/machines.localhost"; // contains localhost
073
074
075    GenPolynomialRing<BigRational> fac;
076
077
078    List<GenPolynomial<BigRational>> L;
079
080
081    PolynomialList<BigRational> F;
082
083
084    List<GenPolynomial<BigRational>> G;
085
086
087    GroebnerBase<BigRational> bbseq;
088
089
090    GroebnerBaseAbstract<BigRational> bbdist;
091
092
093    GroebnerBaseAbstract<BigRational> bbdists;
094
095
096    GenPolynomial<BigRational> a;
097
098
099    GenPolynomial<BigRational> b;
100
101
102    GenPolynomial<BigRational> c;
103
104
105    GenPolynomial<BigRational> d;
106
107
108    GenPolynomial<BigRational> e;
109
110
111    int rl = 3; //4; //3; 
112
113
114    int kl = 4;
115
116
117    int ll = 7;
118
119
120    int el = 3;
121
122
123    float q = 0.2f; //0.4f
124
125
126    int threads = 2;
127
128
129    ExecutableServer es1;
130
131
132    ExecutableServer es2;
133
134
135    @Override
136    protected void setUp() {
137        es1 = new ExecutableServer(4712); // == machines.localhost:4712
138        es1.init();
139        es2 = new ExecutableServer(4711); // == machines.localhost:4711
140        es2.init();
141        BigRational coeff = new BigRational(9);
142        fac = new GenPolynomialRing<BigRational>(coeff, rl);
143        a = b = c = d = e = null;
144        bbseq = new GroebnerBaseSeq<BigRational>();
145        bbdist = new GroebnerBaseDistributedHybridEC<BigRational>(mfile, threads, port);
146        //bbdists = new GroebnerBaseDistributedHybridEC<BigRational>(mfile, threads, new OrderedSyzPairlist<BigRational>(), port);
147    }
148
149
150    @Override
151    protected void tearDown() {
152        a = b = c = d = e = null;
153        fac = null;
154        bbseq = null;
155        bbdist.terminate();
156        bbdist = null;
157        //bbdists.terminate();
158        //bbdists = null;
159        es1.terminate();
160        es2.terminate();
161        es1 = null;
162        es2 = null;
163        ComputerThreads.terminate();
164    }
165
166
167    /**
168     * Test distributed GBase corner cases.
169     */
170    public void testDistributedGBaseCorner() {
171        L = new ArrayList<GenPolynomial<BigRational>>();
172
173        a = fac.getZERO();
174
175        L.add(a);
176        L = bbdist.GB(L);
177        assertTrue("isGB( { a } ): " + L, bbseq.isGB(L));
178        assertTrue("L == {}: " + L, L.isEmpty());
179
180        b = fac.getONE();
181
182        L.add(b);
183        L = bbdist.GB(L);
184        assertTrue("isGB( { a } ): " + L, bbseq.isGB(L));
185        assertTrue("L == {1}: " + L, L.size() == 1);
186    }
187
188
189    /**
190     * Test distributed GBase.
191     */
192    public void testDistributedGBase() {
193        L = new ArrayList<GenPolynomial<BigRational>>();
194
195        a = fac.random(kl, ll, el, q);
196        b = fac.random(kl, ll, el, q);
197        c = fac.random(kl, ll, el, q);
198        d = fac.random(kl, ll, el, q);
199        e = d; //fac.random(kl, ll, el, q );
200
201        L.add(a);
202        L = bbdist.GB(L);
203        assertTrue("isGB( { a } ): " + L, bbseq.isGB(L));
204
205        L.add(b);
206        //System.out.println("L = " + L.size() );
207        L = bbdist.GB(L);
208        assertTrue("isGB( { a, b } ): " + L, bbseq.isGB(L));
209
210        L.add(c);
211        L = bbdist.GB(L);
212        assertTrue("isGB( { a, b, c } ): " + L, bbseq.isGB(L));
213
214        L.add(d);
215        L = bbdist.GB(L);
216        assertTrue("isGB( { a, b, c, d } ): " + L, bbseq.isGB(L));
217
218        L.add(e);
219        L = bbdist.GB(L);
220        assertTrue("isGB( { a, b, c, d, e } ): " + L, bbseq.isGB(L));
221    }
222
223
224    /**
225     * Test compare sequential with distributed GBase.
226     */
227    public void testSequentialDistributedGBase() {
228        List<GenPolynomial<BigRational>> Gs, Gp = null;
229        L = new ArrayList<GenPolynomial<BigRational>>();
230
231        a = fac.random(kl, ll, el, q);
232        b = fac.random(kl, ll, el, q);
233        c = fac.random(kl, ll, el, q);
234        d = fac.random(kl, ll, el, q);
235        e = d; //fac.random(kl, ll, el, q );
236
237        L.add(a);
238        Gs = bbseq.GB(L);
239        Gp = bbdist.GB(L);
240        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
241        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
242
243        L = Gs;
244        L.add(b);
245        Gs = bbseq.GB(L);
246        Gp = bbdist.GB(L);
247        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
248        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
249
250        L = Gs;
251        L.add(c);
252        Gs = bbseq.GB(L);
253        Gp = bbdist.GB(L);
254        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
255        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
256
257        L = Gs;
258        L.add(d);
259        Gs = bbseq.GB(L);
260        Gp = bbdist.GB(L);
261        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
262        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
263
264        L = Gs;
265        L.add(e);
266        Gs = bbseq.GB(L);
267        Gp = bbdist.GB(L);
268        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
269        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
270    }
271
272
273    /**
274     * Test Trinks7 GBase.
275     */
276    @SuppressWarnings("unchecked")
277    public void testTrinks7GBase() {
278        List<GenPolynomial<BigRational>> Gs, Gp = null;
279        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
280                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
281                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
282                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
283        //exam = "(x3,x4,x5) L " + 
284        //       "( (x3^2 - 13974703710478159/3775194259200) , (x4 - 34297/840), (x5^2 - 6389/480), (-4/3 x5^2 + x3^2 + x3 - 833/180) ) ";
285        Reader source = new StringReader(exam);
286        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
287        try {
288            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
289        } catch (IOException e) {
290            fail("" + e);
291        }
292        //System.out.println("F = " + F);
293
294        Gs = bbseq.GB(F.list);
295        //System.out.println("Gs = " + Gs);
296        Gp = bbdist.GB(F.list);
297        //System.out.println("Gp = " + Gp);
298
299        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(Gp));
300        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(Gs));
301        //assertEquals("#GB(Trinks7) == 6", 6, G.size());
302        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + F, Gs.containsAll(Gp));
303        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + F, Gp.containsAll(Gs));
304        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, Gp);
305        //System.out.println("G = " + trinks);
306    }
307
308}