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