Search in sources :

Example 1 with TaskConcept

use of nars.concept.TaskConcept in project narchy by automenta.

the class DynamicTruthBeliefTableTest method testDynamicConjunction2Temporal.

@Test
public void testDynamicConjunction2Temporal() throws Narsese.NarseseException {
    NAR n = NARS.shell();
    n.believe($("(x)"), (long) 0);
    n.believe($("(y)"), (long) 4);
    n.time.dur(8);
    TaskConcept cc = (TaskConcept) n.conceptualize($("((x) && (y))"));
    DynamicTruthBeliefTable xtable = (DynamicTruthBeliefTable) cc.beliefs();
    Compound template = $("((x) &&+4 (y))");
    DynTruth xt = xtable.truth(0, 0, template, n);
    assertNotNull(xt);
    // assertTrue($.t(1f, 0.81f).equals(xt.truth(n), 0.1f), xt.truth(n).toString());
    // best match to the input
    assertEquals(0.81f, xtable.taskDynamic(0, 0, $("((x) &&+4 (y))"), n).conf(), 0.05f);
    assertEquals(0.74f, xtable.taskDynamic(0, 0, $("((x) &&+6 (y))"), n).conf(), 0.07f);
    assertEquals(0.75f, xtable.taskDynamic(0, 0, $("((x) &&+2 (y))"), n).conf(), 0.07f);
    assertEquals(0.75f, xtable.taskDynamic(0, 0, $("((x) &&+0 (y))"), n).conf(), 0.07f);
    assertEquals(0.62f, xtable.taskDynamic(0, 0, $("((x) &&-32 (y))"), n).conf(), 0.2f);
}
Also used : TaskConcept(nars.concept.TaskConcept) Compound(nars.term.Compound) Test(org.junit.jupiter.api.Test)

Example 2 with TaskConcept

use of nars.concept.TaskConcept in project narchy by automenta.

the class RTreeBeliefTableTest method testAccuracy.

static void testAccuracy(int dur, int period, int end, int cap, LongToFloatFunction func) {
    NAR n = NARS.shell();
    n.time.dur(dur);
    Term term = $.p("x");
    // 1. populate
    // n.log();
    TaskConcept c = (TaskConcept) n.conceptualize(term);
    @NotNull BeliefTable cb = true ? c.beliefs() : c.goals();
    cb.setCapacity(0, cap);
    // int numTasks = 0;
    System.out.println("points:");
    long time;
    long start = n.time();
    while ((time = n.time()) < end) {
        float f = func.valueOf(time);
        System.out.print(time + "=" + f + "\t");
        n.input($.task(term, BELIEF, f, 0.9f).time(time).setPriThen(0.5f).apply(n));
        n.run(period);
        c.beliefs().print();
        System.out.println();
    // numTasks++;
    }
    System.out.println();
    System.out.println();
    MultiStatistics<Task> m = new MultiStatistics<Task>().classify("input", (t) -> t.isInput()).classify("derived", (t) -> t instanceof DerivedTask).value("pri", (t) -> t.pri()).value2D("truth", (t) -> new float[] { t.freq(), t.conf() }).value("freqErr", (t) -> Math.abs(((t.freq() - 0.5f) * 2f) - func.valueOf(t.mid()))).add(c.beliefs().streamTasks().collect(toList()));
    System.out.println();
    m.print();
    System.out.println();
    c.beliefs().print();
    // 2. validate and calculate error
    CSVOutput csv = new CSVOutput(System.out, "time", "actual", "approx");
    double errSum = 0;
    for (long i = start; i < end; i++) {
        float actual = func.valueOf(i);
        Truth actualTruth = n.beliefTruth(term, i);
        float approx, err;
        if (actualTruth != null) {
            approx = actualTruth.freq();
            err = Math.abs(approx - actual);
        } else {
            approx = Float.NaN;
            err = 1f;
        }
        errSum += err;
        csv.out(i, actual, approx);
    // System.out.println(n2(i) + "\t" + /*n2(err) + "\t" + */ n2(expected) + "\t" + n2(actual));
    }
    double avgErr = errSum / (end - start + 1);
    System.out.println();
    System.out.println(n4(avgErr) + " avg freq err per point");
    assertTrue(avgErr < 0.1f);
}
Also used : Texts.n4(jcog.Texts.n4) nars(nars) LongToFloatFunction(org.eclipse.collections.api.block.function.primitive.LongToFloatFunction) Test(org.junit.jupiter.api.Test) BeliefTable(nars.table.BeliefTable) Truth(nars.truth.Truth) Collectors.toList(java.util.stream.Collectors.toList) RTreeBeliefTable(nars.table.RTreeBeliefTable) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) CSVOutput(jcog.meter.event.CSVOutput) BELIEF(nars.Op.BELIEF) Termed(nars.term.Termed) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TaskConcept(nars.concept.TaskConcept) NotNull(org.jetbrains.annotations.NotNull) MultiStatistics(jcog.math.MultiStatistics) Term(nars.term.Term) TaskConcept(nars.concept.TaskConcept) MultiStatistics(jcog.math.MultiStatistics) Term(nars.term.Term) NotNull(org.jetbrains.annotations.NotNull) BeliefTable(nars.table.BeliefTable) RTreeBeliefTable(nars.table.RTreeBeliefTable) Truth(nars.truth.Truth) CSVOutput(jcog.meter.event.CSVOutput)

Example 3 with TaskConcept

use of nars.concept.TaskConcept in project narchy by automenta.

the class RTreeBeliefTableTest method testBasicOperations.

@Test
public void testBasicOperations() throws Narsese.NarseseException {
    NAR n = NARS.shell();
    Term ab = nars.$.$("a:b");
    TaskConcept X = (TaskConcept) n.conceptualize(ab);
    RTreeBeliefTable r = RTreeBeliefTable.build(ab);
    r.setCapacity(4);
    assertEquals(0, r.size());
    Term x = X.term();
    float freq = 1f;
    float conf = 0.9f;
    int creationTime = 1;
    int start = 1, end = 1;
    Task a = add(r, x, freq, conf, start, end, n);
    assertEquals(1, r.size());
    r.add(a, X, n);
    r.print(System.out);
    // no change for inserted duplicate
    assertEquals(1, r.size());
    // WEAKer
    Task b = add(r, x, 0f, 0.5f, 1, 1, n);
    assertEquals(2, r.size());
    Task c = add(r, x, 0.1f, 0.9f, 2, 2, n);
    assertEquals(3, r.size());
    Task d = add(r, x, 0.1f, 0.9f, 3, 4, n);
    assertEquals(4, r.size());
    System.out.println("at capacity");
    r.print(System.out);
    // try capacity limit
    Task e = add(r, x, 0.3f, 0.9f, 3, 4, n);
    System.out.println("\nat capacity?");
    r.print(System.out);
    r.forEachTask(System.out::println);
    // capacity limit unaffected
    assertEquals(4, r.size());
    System.out.println("after capacity compress inserting " + e.toString(true));
    r.print(System.out);
}
Also used : TaskConcept(nars.concept.TaskConcept) RTreeBeliefTable(nars.table.RTreeBeliefTable) Term(nars.term.Term) Test(org.junit.jupiter.api.Test)

Example 4 with TaskConcept

use of nars.concept.TaskConcept in project narchy by automenta.

the class BeliefTableTest method assertDuration.

static void assertDuration(NAR n, String c, long start, long end) throws Narsese.NarseseException {
    TaskConcept cc = (TaskConcept) n.conceptualize(c);
    assertNotNull(cc, c + " unconceptualized");
    List<Task> tt = cc.beliefs().streamTasks().collect(toList());
    assertTrue(cc.beliefs() instanceof DynamicTruthBeliefTable || !tt.isEmpty(), c + " not believed");
    if (!tt.isEmpty()) {
        Task t = tt.get(0);
        // System.out.println(sim.proof());
        // System.out.println(sim.start() + ".." + /*sim.occurrence() + ".."*/ + sim.end());
        assertEquals(start, t.start());
        assertEquals(end, t.end());
    }
}
Also used : TaskConcept(nars.concept.TaskConcept) DynamicTruthBeliefTable(nars.concept.dynamic.DynamicTruthBeliefTable)

Example 5 with TaskConcept

use of nars.concept.TaskConcept in project narchy by automenta.

the class BeliefTableTest method testConceptualizationIntermpolation.

@Test
public void testConceptualizationIntermpolation() throws Narsese.NarseseException {
    for (Tense t : new Tense[] { Present, Eternal }) {
        NAR n = NARS.tmp();
        n.dtMergeOrChoose.set(true);
        n.believe("((a ==>+2 b)-->[pill])", t, 1f, 0.9f);
        n.believe("((a ==>+6 b)-->[pill])", t, 1f, 0.9f);
        // @NotNull Bag<Concept, PLink<Concept>> cb = n.focus.active;
        // assertTrue(5 <= cb.size());
        String abpill = "((a==>b)-->[pill])";
        // iterator().next().get();//((ArrayBag<Concept>) cb).get(0).get();
        TaskConcept cc = (TaskConcept) n.conceptualize(abpill);
        assertNotNull(cc);
        String correctMerge = "((a ==>+4 b)-->[pill])";
        cc.beliefs().print();
        // test belief match interpolated a result
        long when = t == Present ? 0 : ETERNAL;
        assertEquals(correctMerge, cc.beliefs().match(when, null, n).term().toString());
        // test merge after capacity shrink:
        // set to capacity=1 to force compression
        cc.beliefs().setCapacity(1, 1);
        cc.print();
        // n.forEachTask(System.out::println);
        // INTERMPOLATION APPLIED AFTER REVECTION:
        assertEquals(correctMerge, cc.beliefs().match(0, null, n).term().toString());
    }
}
Also used : Tense(nars.time.Tense) TaskConcept(nars.concept.TaskConcept) TestNAR(nars.test.TestNAR) Test(org.junit.jupiter.api.Test)

Aggregations

TaskConcept (nars.concept.TaskConcept)16 Test (org.junit.jupiter.api.Test)11 Concept (nars.concept.Concept)5 Term (nars.term.Term)4 Truth (nars.truth.Truth)4 RTreeBeliefTable (nars.table.RTreeBeliefTable)3 Collectors.toList (java.util.stream.Collectors.toList)2 nars (nars)2 NAR (nars.NAR)2 BELIEF (nars.Op.BELIEF)2 BeliefTable (nars.table.BeliefTable)2 Termed (nars.term.Termed)2 TestNAR (nars.test.TestNAR)2 Tense (nars.time.Tense)2 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 GL2 (com.jogamp.opengl.GL2)1 java.awt (java.awt)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1