Search in sources :

Example 1 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class EllipsisTest method testCombinations.

static void testCombinations(Compound _X, Compound Y, int expect) {
    Compound X = (Compound) new PatternIndex().pattern(_X);
    for (int seed = 0; seed < 3; /*expect*5*/
    seed++) {
        Set<String> results = $.newHashSet(0);
        Random rng = new XorShift128PlusRandom(seed);
        Unify f = new Unify(VAR_PATTERN, rng, Param.UnificationStackMax, 128) {

            @Override
            public void tryMatch() {
                results.add(xy.toString());
            }
        };
        f.unify(X, Y, true);
        results.forEach(System.out::println);
        assertEquals(expect, results.size(), () -> "insufficient permutations for: " + X + " .. " + Y);
    }
}
Also used : Unify(nars.term.subst.Unify) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Random(java.util.Random) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Compound(nars.term.Compound) PatternIndex(nars.index.term.PatternIndex)

Example 2 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class UnifyTest method testUnify.

/*
    //// second level variable handling rules ////////////////////////////////////////////////////////////////////////////////////
    //second level variable elimination (termlink level2 growth needed in order for these rules to work)

    (B --> K), (&&,(#X --> L),(($Y --> K) ==> A)) |- substitute((&&, (#X --> L), A), $Y,B), (Truth:Deduction)
    (B --> K), (&&,(#X --> L),(($Y --> K) ==> (&&,A..+))) |- substitute((&&,(#X --> L),A..+),$Y,B), (Truth:Deduction)
     */
Subst testUnify(Compound a, Compound b, boolean matches) {
    AtomicBoolean matched = new AtomicBoolean(false);
    Unify f = new Unify(Op.VAR_QUERY, new XorShift128PlusRandom(1), Param.UnificationStackMax, 128) {

        @Override
        public void tryMatch() {
            assertTrue(matches);
            matched.set(true);
            // identifier: punctuation, mapA, mapB
            assertEquals("{?1=a}", xy.toString());
            // output
            assertEquals("(a-->b) (?1-->b) -?>", a + " " + b + " -?>");
        }
    };
    f.unify(b, a, true);
    assertEquals(matched.get(), matches);
    return f;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Unify(nars.term.subst.Unify) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom)

Example 3 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class UnifyTest method test.

Unify test(/**/
Op type, String s1, String s2, boolean shouldSub) {
    // NAR nar = NARS.shell();
    try {
        Compound t2 = (Compound) Narsese.the().term(s2, true);
        assertNotNull(t2);
        Compound t1;
        if (type == Op.VAR_PATTERN) {
            // special handling for ellipsis
            t1 = pattern(s1);
            assertNotNull(t1);
        } else {
            t1 = (Compound) Narsese.the().term(s1, true);
        }
        // nar.question(s2);
        // nar.run(cycles);
        Set<Term> t1u = t1.recurseTermsToSet(type);
        // assertTrue(t1u.size() > 0);
        Set<Term> t2u = t2.recurseTermsToSet(type);
        // assertTrue(t2u.size() == 0);
        // Sets.difference(t1u, t2u).size();
        int n1 = t1u.size();
        // int n2 = Sets.difference(t2u, t1u).size();
        // a somewhat strict lower bound
        // int power = 4 * (1 + t1.volume() * t2.volume());
        // power*=power;
        AtomicBoolean subbed = new AtomicBoolean(false);
        Unify sub = new Unify(type, new XorShift128PlusRandom(1), Param.UnificationStackMax, INITIAL_TTL) {

            @Override
            public void tryMatch() {
                if (shouldSub) {
                    this.xy.forEachVersioned((k, v) -> {
                        if (matchType(k.op()))
                            assertNotNull(v);
                        return true;
                    });
                    if (/*((n2) <= (yx.size())*/
                    (n1) <= (xy.size())) {
                        subbed.set(true);
                    }
                /*else {
                            System.out.println("incomplete:\n\t" + xy);
                        }*/
                // assertFalse("incomplete: " + toString(), this.isEmpty());
                } else {
                    // HACK there should be incomplete assignments even though this says it matched
                    // || (n2) <= (yx.size()));
                    assertTrue((n1) > (xy.size()), "why matched?: " + xy);
                // assertFalse("match found but should not have", true);
                }
            }
        };
        sub.unify(t1.term(), t2.term(), true);
        // for testing
        sub.revert(0);
        assertEquals(shouldSub, subbed.get());
        return sub;
    } catch (Narsese.NarseseException e) {
        throw new RuntimeException(e);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Unify(nars.term.subst.Unify) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Compound(nars.term.Compound) Term(nars.term.Term)

Example 4 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class RouletteTest method testDecideRouletteTriangular.

@Test
public void testDecideRouletteTriangular() {
    XorShift128PlusRandom rng = new XorShift128PlusRandom(1);
    int uniques = 10;
    int samples = 5000;
    Frequency f = new Frequency();
    for (int i = 0; i < samples; i++) f.addValue(Roulette.decideRoulette(uniques, (k) -> (k + 1f) / (uniques), rng));
    System.out.println(f);
    assertEquals(f.getUniqueCount(), uniques);
    float total = f.getSumFreq();
    for (int i = 0; i < uniques; i++) assertEquals(f.getCount(i) / total, (i + 1f) / (uniques * uniques / 2), 1f / (4 * uniques));
}
Also used : XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Frequency(org.apache.commons.math3.stat.Frequency) Test(org.junit.jupiter.api.Test)

Example 5 with XorShift128PlusRandom

use of jcog.math.random.XorShift128PlusRandom in project narchy by automenta.

the class RouletteTest method testDecideRouletteFlat.

@Test
public void testDecideRouletteFlat() {
    XorShift128PlusRandom rng = new XorShift128PlusRandom(1);
    int uniques = 4;
    int samples = 100;
    Frequency f = new Frequency();
    for (int i = 0; i < samples; i++) f.addValue(Roulette.decideRoulette(uniques, (k) -> 0.5f, rng));
    // System.out.println(f);
    assertEquals(f.getUniqueCount(), uniques);
    float total = f.getSumFreq();
    for (int i = 0; i < uniques; i++) assertEquals(f.getCount(i) / total, 1f / uniques, 1f / (4 * uniques));
}
Also used : XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Frequency(org.apache.commons.math3.stat.Frequency) Test(org.junit.jupiter.api.Test)

Aggregations

XorShift128PlusRandom (jcog.math.random.XorShift128PlusRandom)12 Random (java.util.Random)6 Test (org.junit.jupiter.api.Test)6 Unify (nars.term.subst.Unify)3 Frequency (org.apache.commons.math3.stat.Frequency)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 DistractedSequenceRecall (jcog.learn.lstm.DistractedSequenceRecall)2 SimpleLSTM (jcog.learn.lstm.SimpleLSTM)2 Compound (nars.term.Compound)2 Joiner (com.google.common.base.Joiner)1 BagTest (jcog.bag.BagTest)1 DefaultHijackBag (jcog.bag.impl.hijack.DefaultHijackBag)1 PriReference (jcog.pri.PriReference)1 PatternIndex (nars.index.term.PatternIndex)1 Term (nars.term.Term)1 Assertions (org.junit.jupiter.api.Assertions)1