Search in sources :

Example 1 with PLink

use of jcog.pri.PLink in project narchy by automenta.

the class ArrayBagTest method populated.

@NotNull
private CurveBag<PLink<String>> populated(int n, @NotNull DoubleSupplier random) {
    CurveBag<PLink<String>> a = curveBag(n, plus);
    // fill with uniform randomness
    for (int i = 0; i < n; i++) {
        a.put(new PLink("x" + i, (float) random.getAsDouble()));
    }
    a.commit();
    return a;
}
Also used : PLink(jcog.pri.PLink) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PLink

use of jcog.pri.PLink in project narchy by automenta.

the class ArrayBagTest method testInsertOrBoostDoesntCauseSort.

@Disabled
@Test
public void testInsertOrBoostDoesntCauseSort() {
    final int[] sorts = { 0 };
    @NotNull CurveBag<PLink<String>> x = new CurveBag(PriMerge.plus, new HashMap<>(), 4) {

        @Override
        protected void sort(int from, int to) {
            sorts[0]++;
            super.sort(from, to);
        }
    };
    x.put(new PLink("x", 0.2f));
    x.put(new PLink("y", 0.1f));
    x.put(new PLink("z", 0f));
    assertEquals(0, sorts[0]);
    x.commit();
    assertEquals(0, sorts[0]);
    assertSorted(x);
}
Also used : PLink(jcog.pri.PLink) CurveBag(jcog.bag.impl.CurveBag) NotNull(org.jetbrains.annotations.NotNull) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with PLink

use of jcog.pri.PLink in project narchy by automenta.

the class ArrayBagTest method testBudgetMerge.

@Test
public void testBudgetMerge() {
    PLinkArrayBag<String> a = new PLinkArrayBag<String>(4, plus, new HashMap<>(4));
    assertEquals(0, a.size());
    a.put(new PLink("x", 0.1f));
    a.put(new PLink("x", 0.1f));
    a.commit(null);
    assertEquals(1, a.size());
    PriReference<String> agx = a.get("x");
    Pri expect = new Pri(0.2f);
    assertTrue(Util.equals(expect.priElseNeg1(), agx.priElseNeg1(), 0.01f), agx + "==?==" + expect);
}
Also used : PLink(jcog.pri.PLink) PLinkArrayBag(jcog.bag.impl.PLinkArrayBag) Pri(jcog.pri.Pri) Test(org.junit.jupiter.api.Test)

Example 4 with PLink

use of jcog.pri.PLink in project narchy by automenta.

the class ArrayBagTest method testCapacity.

@Test
public void testCapacity() {
    PLinkArrayBag a = new PLinkArrayBag(2, plus, new HashMap<>(2));
    a.put(new PLink("x", 0.1f));
    a.put(new PLink("y", 0.2f));
    a.print();
    System.out.println();
    assertEquals(2, a.size());
    a.commit(null);
    assertSorted(a);
    assertEquals(0.1f, a.priMin(), 0.01f);
    a.put(new PLink("z", 0.05f));
    a.print();
    System.out.println();
    assertEquals(2, a.size());
    assertTrue(a.contains("x") && a.contains("y"));
    assertFalse(a.contains("z"));
}
Also used : PLink(jcog.pri.PLink) PLinkArrayBag(jcog.bag.impl.PLinkArrayBag) Test(org.junit.jupiter.api.Test)

Example 5 with PLink

use of jcog.pri.PLink 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)

Aggregations

PLink (jcog.pri.PLink)16 Test (org.junit.jupiter.api.Test)6 ArrayBag (jcog.bag.impl.ArrayBag)4 PriReference (jcog.pri.PriReference)4 PLinkArrayBag (jcog.bag.impl.PLinkArrayBag)3 DefaultHijackBag (jcog.bag.impl.hijack.DefaultHijackBag)3 Term (nars.term.Term)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 Sets (com.google.common.collect.Sets)1 PhantomReference (java.lang.ref.PhantomReference)1 ReferenceQueue (java.lang.ref.ReferenceQueue)1 SoftReference (java.lang.ref.SoftReference)1 WeakReference (java.lang.ref.WeakReference)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 java.util.concurrent (java.util.concurrent)1