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