001/*
002 * $Id$
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, b, c, d, e;
097
098
099    int rl = 3; //4; //3; 
100
101
102    int kl = 4;
103
104
105    int ll = 7;
106
107
108    int el = 3;
109
110
111    float q = 0.2f; //0.4f
112
113
114    int threads = 2;
115
116
117    ExecutableServer es1;
118
119
120    ExecutableServer es2;
121
122
123    @Override
124    protected void setUp() {
125        es1 = new ExecutableServer(4712); // == machines.localhost:4712
126        es1.init();
127        es2 = new ExecutableServer(4711); // == machines.localhost:4711
128        es2.init();
129        BigRational coeff = new BigRational(9);
130        fac = new GenPolynomialRing<BigRational>(coeff, rl);
131        a = b = c = d = e = null;
132        bbseq = new GroebnerBaseSeq<BigRational>();
133        bbdist = new GroebnerBaseDistributedHybridEC<BigRational>(mfile, threads, port);
134        //bbdists = new GroebnerBaseDistributedHybridEC<BigRational>(mfile, threads, new OrderedSyzPairlist<BigRational>(), port);
135    }
136
137
138    @Override
139    protected void tearDown() {
140        a = b = c = d = e = null;
141        fac = null;
142        bbseq = null;
143        bbdist.terminate();
144        bbdist = null;
145        //bbdists.terminate();
146        //bbdists = null;
147        es1.terminate();
148        es2.terminate();
149        es1 = null;
150        es2 = null;
151        ComputerThreads.terminate();
152    }
153
154
155    /**
156     * Test distributed GBase corner cases.
157     */
158    public void testDistributedGBaseCorner() {
159        L = new ArrayList<GenPolynomial<BigRational>>();
160
161        a = fac.getZERO();
162
163        L.add(a);
164        L = bbdist.GB(L);
165        assertTrue("isGB( { a } ): " + L, bbseq.isGB(L));
166        assertTrue("L == {}: " + L, L.isEmpty());
167
168        b = fac.getONE();
169
170        L.add(b);
171        L = bbdist.GB(L);
172        assertTrue("isGB( { a } ): " + L, bbseq.isGB(L));
173        assertTrue("L == {1}: " + L, L.size() == 1);
174    }
175
176
177    /**
178     * Test distributed GBase.
179     */
180    public void testDistributedGBase() {
181        L = new ArrayList<GenPolynomial<BigRational>>();
182
183        a = fac.random(kl, ll, el, q);
184        b = fac.random(kl, ll, el, q);
185        c = fac.random(kl, ll, el, q);
186        d = fac.random(kl, ll, el, q);
187        e = d; //fac.random(kl, ll, el, q );
188
189        L.add(a);
190        L = bbdist.GB(L);
191        assertTrue("isGB( { a } ): " + L, bbseq.isGB(L));
192
193        L.add(b);
194        //System.out.println("L = " + L.size() );
195        L = bbdist.GB(L);
196        assertTrue("isGB( { a, b } ): " + L, bbseq.isGB(L));
197
198        L.add(c);
199        L = bbdist.GB(L);
200        assertTrue("isGB( { a, b, c } ): " + L, bbseq.isGB(L));
201
202        L.add(d);
203        L = bbdist.GB(L);
204        assertTrue("isGB( { a, b, c, d } ): " + L, bbseq.isGB(L));
205
206        L.add(e);
207        L = bbdist.GB(L);
208        assertTrue("isGB( { a, b, c, d, e } ): " + L, bbseq.isGB(L));
209    }
210
211
212    /**
213     * Test compare sequential with distributed GBase.
214     */
215    public void testSequentialDistributedGBase() {
216        List<GenPolynomial<BigRational>> Gs, Gp = null;
217        L = new ArrayList<GenPolynomial<BigRational>>();
218
219        a = fac.random(kl, ll, el, q);
220        b = fac.random(kl, ll, el, q);
221        c = fac.random(kl, ll, el, q);
222        d = fac.random(kl, ll, el, q);
223        e = d; //fac.random(kl, ll, el, q );
224
225        L.add(a);
226        Gs = bbseq.GB(L);
227        Gp = bbdist.GB(L);
228        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
229        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
230
231        L = Gs;
232        L.add(b);
233        Gs = bbseq.GB(L);
234        Gp = bbdist.GB(L);
235        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
236        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
237
238        L = Gs;
239        L.add(c);
240        Gs = bbseq.GB(L);
241        Gp = bbdist.GB(L);
242        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
243        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
244
245        L = Gs;
246        L.add(d);
247        Gs = bbseq.GB(L);
248        Gp = bbdist.GB(L);
249        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
250        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
251
252        L = Gs;
253        L.add(e);
254        Gs = bbseq.GB(L);
255        Gp = bbdist.GB(L);
256        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp));
257        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs));
258    }
259
260
261    /**
262     * Test Trinks7 GBase.
263     */
264    @SuppressWarnings("unchecked")
265    public void testTrinks7GBase() {
266        List<GenPolynomial<BigRational>> Gs, Gp = null;
267        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
268                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
269                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
270                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
271        //exam = "(x3,x4,x5) L " + 
272        //       "( (x3^2 - 13974703710478159/3775194259200) , (x4 - 34297/840), (x5^2 - 6389/480), (-4/3 x5^2 + x3^2 + x3 - 833/180) ) ";
273        Reader source = new StringReader(exam);
274        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
275        try {
276            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
277        } catch (IOException e) {
278            fail("" + e);
279        }
280        //System.out.println("F = " + F);
281
282        Gs = bbseq.GB(F.list);
283        //System.out.println("Gs = " + Gs);
284        Gp = bbdist.GB(F.list);
285        //System.out.println("Gp = " + Gp);
286
287        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(Gp));
288        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(Gs));
289        //assertEquals("#GB(Trinks7) == 6", 6, G.size());
290        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + F, Gs.containsAll(Gp));
291        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + F, Gp.containsAll(Gs));
292        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, Gp);
293        //System.out.println("G = " + trinks);
294    }
295
296}