Search in sources :

Example 1 with DefaultHijackBag

use of jcog.bag.impl.hijack.DefaultHijackBag in project narchy by automenta.

the class HijackBagTest method testHijackFlatBagRemainsRandomInNormalizedSampler.

@Test
public void testHijackFlatBagRemainsRandomInNormalizedSampler() {
    int n = 256;
    Bag<String, PriReference<String>> a = new DefaultHijackBag<>(max, n, 4);
    for (int i = 0; i < n * 8; i++) {
        a.put(new PLink("x" + Integer.toString(Float.floatToIntBits(1f / i), 5), ((float) (i)) / (n)));
    }
    a.commit();
    int size = a.size();
    // assertTrue(size >= 20 && size <= 30);
    // TreeSet<String> keys = new TreeSet();
    // Iterators.transform(a.iterator(), x -> x.get()).forEachRemaining(keys::add);
    // System.out.println( keys.size() + " " + Joiner.on(' ').join(keys) );
    TreeSet<String> keys2 = new TreeSet();
    a.forEach((b) -> {
        if (!keys2.add(b.get()))
            throw new RuntimeException("duplicate detected");
    });
    System.out.println(keys2.size() + " " + Joiner.on(' ').join(keys2));
    assertEquals(size, keys2.size());
// int b = 20;
// EmpiricalDistribution e = BagTest.getSamplingPriorityDistribution(a, n * 500, b);
// 
// printDist(e);
// 
// //monotonically increasing:
// assertTrue(e.getBinStats().get(0).getMean() < e.getBinStats().get(b-1).getMean());
// assertTrue(e.getBinStats().get(0).getMean() < e.getBinStats().get(b/2).getMean());
// assertTrue(e.getBinStats().get(b/2).getMean() < e.getBinStats().get(b-2).getMean());
// a.print();
}
Also used : PLink(jcog.pri.PLink) TreeSet(java.util.TreeSet) DefaultHijackBag(jcog.bag.impl.hijack.DefaultHijackBag) PriReference(jcog.pri.PriReference) Test(org.junit.jupiter.api.Test) BagTest(jcog.bag.BagTest)

Example 2 with DefaultHijackBag

use of jcog.bag.impl.hijack.DefaultHijackBag in project narchy by automenta.

the class BagTest method testBasicInsertionRemoval.

public static void testBasicInsertionRemoval(Bag<String, PriReference<String>> c) {
    assertEquals(1, c.capacity());
    if (!(c instanceof DefaultHijackBag)) {
        assertEquals(0, c.size());
        assertTrue(c.isEmpty());
    }
    // insert an item with (nearly) zero budget
    PLink x0 = new PLink("x", 2 * Prioritized.EPSILON);
    PriReference added = c.put(x0);
    assertSame(added, x0);
    c.commit();
    assertEquals(1, c.size());
    assertEquals(0, c.priMin(), Prioritized.EPSILON * 2);
    PriReference<String> x = c.get("x");
    assertNotNull(x);
    assertSame(x, x0);
    assertTrue(Util.equals(Prioritized.Zero.priElseNeg1(), x.priElseNeg1(), 0.01f));
}
Also used : PLink(jcog.pri.PLink) DefaultHijackBag(jcog.bag.impl.hijack.DefaultHijackBag) PriReference(jcog.pri.PriReference)

Example 3 with DefaultHijackBag

use of jcog.bag.impl.hijack.DefaultHijackBag in project narchy by automenta.

the class HijackBagTest method testHijackSampling.

@Test
public void testHijackSampling() {
    for (int cap : new int[] { 63, 37 }) {
        int rep = 3;
        int batch = 4;
        int extraSpace = 5;
        final Random rng = new XorShift128PlusRandom(1);
        DefaultHijackBag bag = new DefaultHijackBag(plus, cap * extraSpace, rep) {

            @Override
            public void onRemove(Object value) {
                fail("");
            }

            @Override
            public void onReject(Object value) {
                fail("");
            }
        };
        fillLinear(bag, cap);
        testBagSamplingDistribution(bag, batch);
        bag.print();
    }
}
Also used : XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Random(java.util.Random) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) DefaultHijackBag(jcog.bag.impl.hijack.DefaultHijackBag) Test(org.junit.jupiter.api.Test) BagTest(jcog.bag.BagTest)

Example 4 with DefaultHijackBag

use of jcog.bag.impl.hijack.DefaultHijackBag in project narchy by automenta.

the class HijackBagTest method testHijackResize.

@Test
public void testHijackResize() {
    Random rng = rng();
    DefaultHijackBag b = new DefaultHijackBag(PriMerge.max, 0, 7);
    BagTest.populate(b, rng, 10, 20, 0f, 1f, 0.5f);
    // assertEquals(b.reprobes /*0*/, b.size());
    int dimensionality = 50;
    b.setCapacity(dimensionality * 2);
    BagTest.populate(b, rng, dimensionality * 5, dimensionality, 0f, 1f, 0.5f);
    // System.out.println("under capacity");
    b.print();
    assertApproximatelySized(b, dimensionality, 0.5f);
    b.setCapacity(dimensionality / 2 * 2);
    // System.out.println("half capacity");
    b.print();
    assertApproximatelySized(b, dimensionality / 2 * 2, 0.5f);
    BagTest.populate(b, rng, dimensionality * 3, dimensionality, 0f, 1f, 0.5f);
    // System.out.println("under capacity, refilled");
    b.print();
    // test
    b.setCapacity(dimensionality * 2);
    BagTest.populate(b, rng, dimensionality * 3, dimensionality, 0f, 1f, 0.5f);
    System.out.println("under capacity, expanded");
    b.print();
    assertApproximatelySized(b, dimensionality, 0.25f);
// test
}
Also used : XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Random(java.util.Random) DefaultHijackBag(jcog.bag.impl.hijack.DefaultHijackBag) Test(org.junit.jupiter.api.Test) BagTest(jcog.bag.BagTest)

Aggregations

DefaultHijackBag (jcog.bag.impl.hijack.DefaultHijackBag)4 BagTest (jcog.bag.BagTest)3 Test (org.junit.jupiter.api.Test)3 Random (java.util.Random)2 XorShift128PlusRandom (jcog.math.random.XorShift128PlusRandom)2 PLink (jcog.pri.PLink)2 PriReference (jcog.pri.PriReference)2 TreeSet (java.util.TreeSet)1