Search in sources :

Example 6 with BeliefTable

use of nars.table.BeliefTable in project narchy by automenta.

the class ImplicationNetworkTest method testEternal_A_PosGoal_ToBC.

@Test
public void testEternal_A_PosGoal_ToBC() {
    NAR n = NARS.tmp();
    Param.DEBUG = true;
    n.believe(IMPL.the(a, b));
    n.believe(IMPL.the(b, c));
    n.goal(a);
    n.run(100);
    BeliefTable aGoals = n.concept(a).goals();
    Truth aGoal = aGoals.truth(ETERNAL, n);
    // n.concept(a).print();
    // a belief state should not exceed the input (default confidence) and freq remain stable
    // additional beliefs are not helpful
    assertEquals(1, aGoals.size());
    BeliefTable bGoals = n.concept(b).goals();
    Truth bGoal = bGoals.truth(ETERNAL, n);
    n.concept(b).print();
    assertEquals(1, bGoals.size());
    // b should have less conf than a but higher than c
    // same freq among all
    BeliefTable cGoals = n.concept(c).goals();
    Truth cGoal = cGoals.truth(ETERNAL, n);
    n.concept(c).print();
    // assertEquals(1, cGoals.size());
    System.out.println("a: " + aGoal);
    System.out.println("b: " + bGoal);
    System.out.println("c: " + cGoal);
    assertEquals(aGoal.freq(), bGoal.freq(), n.freqResolution.floatValue());
    assertEquals(bGoal.freq(), cGoal.freq(), n.freqResolution.floatValue());
    assertTrue(aGoal.conf() - bGoal.conf() > n.confResolution.floatValue() * 2);
    assertTrue(bGoal.conf() - cGoal.conf() > n.confResolution.floatValue() * 2);
}
Also used : BeliefTable(nars.table.BeliefTable) NAR(nars.NAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Example 7 with BeliefTable

use of nars.table.BeliefTable 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 8 with BeliefTable

use of nars.table.BeliefTable in project narchy by automenta.

the class TruthletTaskTest method testImpulseTruthlet.

@Test
public void testImpulseTruthlet() {
    NAR n = NARS.shell();
    Term x = $.the("x");
    float conf = 0.9f;
    n.input(new TruthletTask(x, BELIEF, Truthlet.impulse(1, 2, 1f, 0f, c2w(conf)), n));
    BeliefTable xb = n.truths(x, BELIEF);
    for (int i = -1; i < 5; i++) {
        Truth ti = xb.truth(i, n);
        assertEquals((i <= 0 || i >= 3) ? 0 : 1, ti.freq());
        if (i >= 1 && i <= 2)
            assertEquals(conf, ti.conf());
        else
            assertTrue(conf > ti.conf());
        System.out.println(i + ": " + ti);
    }
}
Also used : BeliefTable(nars.table.BeliefTable) Term(nars.term.Term) NAR(nars.NAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Example 9 with BeliefTable

use of nars.table.BeliefTable in project narchy by automenta.

the class BeliefTableTest method testEternalBeliefRanking.

@Test
public void testEternalBeliefRanking() {
    Param.DEBUG = true;
    int cap = 10;
    NAR n = NARS.shell();
    BeliefAnalysis b = new BeliefAnalysis(n, x);
    b.believe(1.0f, 0.5f);
    b.print();
    BeliefTable beliefs = b.concept().beliefs();
    assertEquals(0.5, beliefs.match(ETERNAL, null, n).conf(), 0.001);
    Truth bt = n.beliefTruth(b, n.time());
    assertNotNull(bt);
    assertEquals(0.5, bt.conf(), 0.001);
    assertEquals(1, beliefs.size());
    b.believe(1.0f, 0.5f);
    n.run();
    b.print();
    assertEquals(3, /* revision */
    beliefs.size());
    assertEquals(0.669, beliefs.match(ETERNAL, null, n).conf(), 0.01);
    b.believe(1.0f, 0.5f);
    n.run();
    b.print();
    assertEquals(5, beliefs.size());
    @NotNull BeliefTable bb = beliefs;
    assertEquals(0.75, bb.match(ETERNAL, null, n).conf(), 0.001);
    assertEquals(0.75, n.beliefTruth(b, n.time()).conf(), 0.01);
    b.believe(1.0f, 0.5f);
    n.run();
    b.print();
    assertEquals(0.79, beliefs.match(ETERNAL, null, n).conf(), 0.02);
    assertEquals(6, beliefs.size());
}
Also used : DynamicTruthBeliefTable(nars.concept.dynamic.DynamicTruthBeliefTable) BeliefTable(nars.table.BeliefTable) BeliefAnalysis(nars.test.analyze.BeliefAnalysis) NotNull(org.jetbrains.annotations.NotNull) TestNAR(nars.test.TestNAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Example 10 with BeliefTable

use of nars.table.BeliefTable in project narchy by automenta.

the class TemporalInductionTest method testTemporalRevision.

@Test
public void testTemporalRevision() throws Narsese.NarseseException {
    NAR n = NARS.tmp();
    n.time.dur(1);
    n.log();
    // TextOutput.out(n);
    n.input("a:b. %1.0|0.9%");
    n.run(5);
    n.input("a:b. %0.0|0.9%");
    n.run(5);
    n.input("a:b. %0.5|0.9%");
    n.run(1);
    // n.forEachConcept(Concept::print);
    TaskConcept c = (TaskConcept) n.conceptualize("a:b");
    assertNotNull(c);
    // assertEquals("(b-->a). 5+0 %.50;.95%", c.getBeliefs().top(n.time()).toStringWithoutBudget());
    BeliefTable b = c.beliefs();
    b.print();
    assertTrue(3 <= b.size());
    // when originality is considered:
    // assertEquals("(b-->a). 5+0 %0.0;.90%", c.beliefs().top(n.time()).toStringWithoutBudget());
    // most current relevant overall:
    assertEquals("(b-->a). 5 %0.0;.90%", n.belief(c.term(), 5).toStringWithoutBudget());
    // least relevant
    assertEquals("(b-->a). 0 %1.0;.90%", n.belief(c.term(), 0).toStringWithoutBudget());
}
Also used : TaskConcept(nars.concept.TaskConcept) BeliefTable(nars.table.BeliefTable) TestNAR(nars.test.TestNAR) NAR(nars.NAR) Test(org.junit.jupiter.api.Test)

Aggregations

BeliefTable (nars.table.BeliefTable)17 Truth (nars.truth.Truth)12 Test (org.junit.jupiter.api.Test)12 NAR (nars.NAR)10 Term (nars.term.Term)5 TestNAR (nars.test.TestNAR)3 NotNull (org.jetbrains.annotations.NotNull)3 Concept (nars.concept.Concept)2 TaskConcept (nars.concept.TaskConcept)2 DynamicTruthBeliefTable (nars.concept.dynamic.DynamicTruthBeliefTable)2 BeliefAnalysis (nars.test.analyze.BeliefAnalysis)2 Collectors.toList (java.util.stream.Collectors.toList)1 Texts.n4 (jcog.Texts.n4)1 MultiStatistics (jcog.math.MultiStatistics)1 CSVOutput (jcog.meter.event.CSVOutput)1 nars (nars)1 Op (nars.Op)1 BELIEF (nars.Op.BELIEF)1 Task (nars.Task)1 RTreeBeliefTable (nars.table.RTreeBeliefTable)1