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);
}
}
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;
}
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);
}
}
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));
}
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));
}
Aggregations