Search in sources :

Example 1 with Unify

use of nars.term.subst.Unify in project narchy by automenta.

the class Choose2 method mutate.

@Override
public void mutate(Unify versioneds, Termutator[] chain, int current) {
    Combinations ccc = this.comb;
    ccc.reset();
    boolean phase = true;
    int start = f.now();
    ShuffledSubterms yy = this.yy;
    Ellipsis xEllipsis = this.xEllipsis;
    Unify f = this.f;
    Term[] x = this.x;
    int[] c = null;
    while (ccc.hasNext() || !phase) {
        c = phase ? ccc.next() : c;
        phase = !phase;
        byte c0 = (byte) c[0];
        byte c1 = (byte) c[1];
        // swap to try the reverse next iteration
        ArrayUtils.reverse(c);
        Term y1 = yy.sub(c0);
        if (x[0].unify(y1, f)) {
            Term y2 = yy.sub(c1);
            if (x[1].unify(y2, f) && xEllipsis.unify(EllipsisMatch.matchExcept(yy, c0, c1), f)) {
                if (!f.tryMutate(chain, current))
                    break;
            }
        }
        if (!f.revertLive(start))
            break;
    }
}
Also used : Unify(nars.term.subst.Unify) Combinations(jcog.math.Combinations) Ellipsis(nars.derive.match.Ellipsis) Term(nars.term.Term) ShuffledSubterms(nars.subterm.ShuffledSubterms)

Example 2 with Unify

use of nars.term.subst.Unify 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 3 with Unify

use of nars.term.subst.Unify 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 4 with Unify

use of nars.term.subst.Unify 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 5 with Unify

use of nars.term.subst.Unify in project narchy by automenta.

the class TermutatorTest method assertTermutatorProducesUniqueResults.

String assertTermutatorProducesUniqueResults(@NotNull Termutator t, int num) {
    // assertEquals(num, t.getEstimatedPermutations());
    // record the order
    Set<String> s = new LinkedHashSet();
    final int[] actual = { 0 };
    // int blocked = 0;
    final int[] duplicates = { 0 };
    unifier.setTTL(TTL);
    // unifier.freeCount.set( Integer.MAX_VALUE ); //MOCK
    t.mutate(unifier, new Termutator[] { t, new Termutator() {

        @Override
        public void mutate(@NotNull Unify f, Termutator[] chain, int current) {
            // use treemap for sorted keys
            TreeMap t = new TreeMap();
            f.xy.map.forEach(t::put);
            if (s.add(t.toString())) {
                actual[0]++;
            } else {
                duplicates[0]++;
            }
        }
    } }, 0);
    String res = s.toString();
    System.out.println(res);
    assertEquals(num, s.size());
    assertEquals(num, actual[0]);
    assertEquals(0, duplicates[0]);
    return res;
}
Also used : Unify(nars.term.subst.Unify) Termutator(nars.derive.mutate.Termutator)

Aggregations

Unify (nars.term.subst.Unify)6 XorShift128PlusRandom (jcog.math.random.XorShift128PlusRandom)3 Compound (nars.term.Compound)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Term (nars.term.Term)2 Random (java.util.Random)1 Combinations (jcog.math.Combinations)1 Ellipsis (nars.derive.match.Ellipsis)1 Termutator (nars.derive.mutate.Termutator)1 PatternIndex (nars.index.term.PatternIndex)1 ShuffledSubterms (nars.subterm.ShuffledSubterms)1