Search in sources :

Example 61 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class BeliefTableTest method testPolation0.

@Test
public void testPolation0() {
    int spacing = 4;
    float conf = 0.9f;
    float[] freqPattern = // new float[]{0, 0.25f, 0.5f, 0.75f, 1f};
    { 0, 0.5f, 1f };
    long[] timing = { 0, 2, 4 };
    int dur = 1;
    NAR n = NARS.shell();
    n.time.dur(dur);
    BeliefAnalysis b = new BeliefAnalysis(n, x);
    assertEquals(timing.length, freqPattern.length);
    int k = 0;
    for (float f : freqPattern) {
        // create linear gradient of belief across time, freq beginning at 0 and increasing to 1
        b.believe(0.5f, freqPattern[k], conf, timing[k]);
        k++;
    }
    int c = freqPattern.length;
    assertEquals(c, b.size(true));
    @NotNull BeliefTable table = b.concept().beliefs();
    b.print();
    int margin = spacing * (c / 2);
    for (int i = -margin; i < spacing * c + margin; i++) System.out.println(i + "\t" + table.truth(i, /* relative to zero */
    n));
    // measure exact timing
    for (int i = 0; i < c; i++) {
        long w = timing[i];
        Truth truth = table.truth(w, n);
        float fExpected = freqPattern[i];
        assertEquals(fExpected, truth.freq(), 0.01f, "exact truth @" + w + " == " + fExpected);
        Task match = table.match(w, null, n);
        assertEquals(fExpected, match.freq(), 0.01f, "exact belief @" + w + " == " + fExpected);
    }
    // measure midpoint interpolation
    for (int i = 1; i < c - 1; i++) {
        float f = (freqPattern[i - 1] + freqPattern[i] + freqPattern[i + 1]) / 3f;
        long w = timing[i];
        assertEquals(f, table.truth(w, n).freq(), 0.1f, () -> "t=" + w);
    }
// /* first */
// @Nullable Truth firstBeliefTruth = table.truth((long) 0, n);
// assertEquals(0.43f, firstBeliefTruth.freq(), 0.1f);
// 
// /* last */
// @Nullable Truth lastBeliefTruth = table.truth((long) (spacing * (c - 1)), n);
// assertEquals(0.56f, lastBeliefTruth.freq(), 0.1f);
// 
// @Nullable Truth endTruth = table.truth((long) (spacing * (c - 1) + margin), n);
// assertEquals(0.55f, endTruth.freq(), 0.2f);
// assertTrue(lastBeliefTruth.conf() >= endTruth.conf());
// 
// @Nullable Truth startTruth = table.truth((long) (0 - margin), n);
// assertEquals(0.44f, startTruth.freq(), 0.2f);
// assertTrue(firstBeliefTruth.conf() >= startTruth.conf());
}
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 62 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class RevectionTest method testRevisionInequivalenceDueToTemporalSeparation.

@Test
public void testRevisionInequivalenceDueToTemporalSeparation() throws Narsese.NarseseException {
    TaskBuilder a = t(1f, 0.5f, -4).evidence(1);
    TaskBuilder b = t(0f, 0.5f, 4).evidence(2);
    int dur = 9;
    Truth pt = new TruthPolation((long) 0, (long) 0, dur, Lists.newArrayList(a.apply(n), b.apply(n))).truth((Task) null);
    @Nullable Truth rt = Revision.revise(a, b);
    assertEquals(pt.freq(), rt.freq(), 0.01f);
    // revection result will be less than eternal revision
    assertTrue(pt.conf() < rt.conf());
}
Also used : Truth(nars.truth.Truth) Nullable(org.jetbrains.annotations.Nullable) Test(org.junit.jupiter.api.Test)

Example 63 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class RevectionTest method testConfidenceAccumulation.

public void testConfidenceAccumulation(int repeats, float freq, float inConf) {
    int maxBeliefs = repeats * 4;
    NAR n = newNAR(maxBeliefs);
    n.log();
    long at = 5;
    float outConf = w2c(c2w(inConf) * repeats);
    BeliefAnalysis b = null;
    try {
        b = new BeliefAnalysis(n, "<a-->b>");
    } catch (Narsese.NarseseException e) {
        fail(e);
    }
    for (int i = 0; i < repeats; i++) {
        b.believe(0.5f, freq, inConf, at);
    }
    b.run(1);
    b.print();
    assertTrue(repeats <= b.size(true));
    @Nullable Truth result = n.beliefTruth(b, at);
    assertEquals(freq, result.freq(), 0.25f);
    assertEquals(outConf, result.conf(), 0.25f);
}
Also used : BeliefAnalysis(nars.test.analyze.BeliefAnalysis) RevisionTest.newNAR(nars.task.RevisionTest.newNAR) Nullable(org.jetbrains.annotations.Nullable) Truth(nars.truth.Truth)

Example 64 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class ThermostatTest method main.

// @Test
// @Disabled
public static void main(String[] args) {
    // void test1() {
    // Param.DEBUG = true;
    final int DUR = 1;
    final int subTrainings = 2;
    // pause between episodes
    final int thinkDurs = 4;
    NAR n = NARS.tmp();
    n.time.dur(DUR);
    n.timeFocus.set(2);
    n.termVolumeMax.set(34);
    // n.freqResolution.set(0.05f);
    // n.confResolution.set(0.01f);
    n.activationRate.set(0.5f);
    n.goalPriDefault.set(1f);
    // n.forgetRate.set(2f);
    // n.deep.set(0.8);
    // n.emotion.want(MetaGoal.Desire, 0.2f);
    // n.want(MetaGoal.Believe, 0.1f);
    // n.want(MetaGoal.Perceive, -0.01f);
    float exeThresh = 0.51f;
    // new ArithmeticIntroduction(8, n);
    new ConjClustering(n, BELIEF, (t) -> true, 8, 32);
    // n.priDefault(BELIEF, 0.3f);
    // n.logPriMin(System.out, 0.5f);
    // n.logWhen(System.out, false, true, true);
    // n.log();
    boolean[] training = new boolean[] { true };
    Opjects op = new Opjects(n) {

        // {
        // pretend = true;
        // }
        @Override
        @Nullable
        protected synchronized Object invoked(Object obj, Method wrapped, Object[] args, Object result) {
            if (training[0]) {
                n.synch();
            // n.runLater(nn -> nn.run(DUR)); //queue some thinking cycles
            }
            Object y = super.invoked(obj, wrapped, args, result);
            if (training[0])
                n.run(DUR * thinkDurs);
            return y;
        }
    };
    Teacher<Thermostat> env = new Teacher<>(op, Thermostat.class);
    Consumer<Thermostat> hotToCold = Thermostat.change(true, false), coldToCold = Thermostat.change(false, false), coldToHot = Thermostat.change(false, true), hotToHot = Thermostat.change(true, true);
    Predicate<Thermostat> isCold = x -> x.is() == Thermostat.cold;
    Predicate<Thermostat> isHot = x -> x.is() == Thermostat.hot;
    n.logWhen(System.out, true, true, true);
    boolean stupid = true;
    training: do {
        training[0] = true;
        op.exeThresh.set(1f);
        for (int i = 0; i < subTrainings; i++) {
            for (Consumer<Thermostat> condition : new Consumer[] { hotToCold, coldToCold }) {
                System.out.println("EPISODE START");
                n.clear();
                env.teach("down", condition, (Thermostat x) -> {
                    // x.up(); //demonstrate no change
                    // x.report();
                    n.run(1);
                    while (x.is() > Thermostat.cold) {
                        x.down();
                        n.run(1);
                    }
                    x.report();
                    n.run(1);
                // x.down(); //demonstrate no change
                // x.report();
                }, isCold);
                System.out.println("EPISODE END");
                n.run(thinkDurs * n.dur());
            // n.concept("do(down)").print();
            }
            for (Consumer<Thermostat> condition : new Consumer[] { coldToHot, hotToHot }) {
                System.out.println("EPISODE START");
                n.clear();
                env.teach("up", condition, x -> {
                    // x.down(); //demonstrate no change
                    // x.report();
                    n.run(1);
                    while (!isHot.test(x)) {
                        x.up();
                        n.run(1);
                    }
                    x.report();
                    n.run(1);
                // x.up(); //demonstrate no change
                // x.report();
                }, isHot);
                System.out.println("EPISODE END");
                n.run(thinkDurs * n.dur());
            }
        }
        System.out.println("VALIDATING");
        System.out.println();
        training[0] = false;
        op.exeThresh.set(exeThresh);
        // n.log();
        // n.run(100);
        // new Implier(n, new float[] { 1f },
        // $.$("a_Thermostat(down,())"),
        // $.$("a_Thermostat(up,())")
        // //$.$("a_Thermostat(is,(),#x)")
        // );
        // try {
        // make cold
        // n.input(new NALTask($.$("a_Thermostat(should,(),0)"),
        // BELIEF, $.t(1f, 0.99f),
        // n.time(), n.time(), n.time()+1000,
        // n.time.nextInputStamp()).pri(1f));
        Thermostat t = env.x;
        {
            // n.clear();
            t.is(3);
            t.should(0);
            n.run(thinkDurs * n.dur());
            Term cold = $.$$("is(a_Thermostat,0)");
            // Term cold = $.$safe("(a_Thermostat(is,(),0) &| --a_Thermostat(is,(),3))");
            Term hot = $.$$("is(a_Thermostat,3)");
            Truth goalTruth = $.t(1f, 0.9f);
            DurService xPos = n.wantWhile(cold, goalTruth, new TaskConceptLogger(n, (w) -> (t.current != t.target)));
            DurService xNeg = n.wantWhile(hot, goalTruth.neg(), new TaskConceptLogger(n, (w) -> t.current != t.target));
            n.run(1);
            for (int i = 0; i < 16 && xPos.isOn(); i++) {
                int period = 100;
                // t.report();
                // n.run(period, pause);
                n.run(period);
            }
            xPos.off();
            xNeg.off();
            t.report();
            if (t.is() == t.should()) {
                System.out.println("good job nars!");
                n.believe($.$$("(learn(up) && learn(down))"), Tense.Present);
                stupid = false;
            } else {
                System.out.println("bad job nars! try again");
                n.believe($.$$("(--learn(up) && --learn(down))"), Tense.Present);
            }
        // n.input(new NALTask($.$safe("a_Thermostat(is,(),0)"),
        // GOAL, $.t(1f, 0.95f),
        // n.time(), n.time(), n.time() + periods,
        // n.time.nextInputStamp()).pri(1f));
        // n.input(new NALTask($.$safe("a_Thermostat(is,(),3)"),
        // GOAL, $.t(0f, 0.95f),
        // n.time(), n.time(), n.time() + periods,
        // n.time.nextInputStamp()).pri(1f));
        }
    } while (false);
    // n.run(thinkDurs * n.dur());
    {
    // n.input(new NALTask($.$safe("a_Thermostat(is,(),3)"),
    // GOAL, $.t(0f, 0.99f),
    // n.time(), n.time(), n.time()+1000,
    // n.time.nextInputStamp()).pri(1f));
    }
    // while (t.is() != t.should()) {
    // int period = 1000;
    // t.report();
    // n.run(period);
    // }
    n.tasks().forEach(t -> {
        if (!t.isInput())
            System.out.println(t);
    });
}
Also used : DurService(nars.control.DurService) Tense(nars.time.Tense) NARS(nars.NARS) Predicate(java.util.function.Predicate) nars.$(nars.$) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) Task(nars.Task) Truth(nars.truth.Truth) NAR(nars.NAR) BELIEF(nars.Op.BELIEF) ConjClustering(nars.op.stm.ConjClustering) Method(java.lang.reflect.Method) Term(nars.term.Term) Method(java.lang.reflect.Method) Term(nars.term.Term) Consumer(java.util.function.Consumer) ConjClustering(nars.op.stm.ConjClustering) NAR(nars.NAR) Truth(nars.truth.Truth) DurService(nars.control.DurService)

Example 65 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class DigitizedScalarTest method testServiceAndFluidEncoder.

@Test
public void testServiceAndFluidEncoder() throws Narsese.NarseseException {
    NAR n = NARS.tmp();
    FloatRange x = new FloatRange(0f, 0f, 1f);
    DigitizedScalar xc = new DigitizedScalar(x, DigitizedScalar.Fluid, n, $.$("x(0)"), $.$("x(1)"));
    int dt = 20;
    for (float v : new float[] { 0f, 0.5f, 1f }) {
        x.set(v);
        xc.update(n.time() - n.dur() / 2, n.time() + n.dur() / 2, n.dur(), n);
        n.run(1);
        System.out.println("\n" + n.time() + " x=" + x);
        xc.forEach(d -> {
            Truth bt = n.beliefTruth(d, n.time());
            System.out.println(d + "\t" + bt);
        });
        int m = (dt - 1) / 2;
        n.run(m);
        Truth[] f = xc.belief(n.time(), n);
        float tolerance = 0.18f;
        if (v == 0) {
            assertEquals(0.0f, f[0].freq(), tolerance);
            assertEquals(0.0f, f[1].freq(), tolerance);
        } else if (v == 0.5f) {
            assertEquals(1.0f, f[0].freq(), tolerance);
            assertEquals(0.0f, f[1].freq(), tolerance);
        } else if (v == 1f) {
            assertEquals(1.0f, f[0].freq(), tolerance);
            assertEquals(1.0f, f[1].freq(), tolerance);
        }
        n.run(dt - 1 - m);
    }
}
Also used : DigitizedScalar(nars.concept.scalar.DigitizedScalar) FloatRange(jcog.math.FloatRange) NAR(nars.NAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Aggregations

Truth (nars.truth.Truth)65 Term (nars.term.Term)31 Test (org.junit.jupiter.api.Test)22 Nullable (org.jetbrains.annotations.Nullable)20 NAR (nars.NAR)18 Task (nars.Task)14 BeliefTable (nars.table.BeliefTable)13 Concept (nars.concept.Concept)9 NALTask (nars.task.NALTask)9 TaskConcept (nars.concept.TaskConcept)7 PreciseTruth (nars.truth.PreciseTruth)7 NotNull (org.jetbrains.annotations.NotNull)7 FasterList (jcog.list.FasterList)6 ITask (nars.task.ITask)6 Tense (nars.time.Tense)6 Util (jcog.Util)5 BELIEF (nars.Op.BELIEF)5 Consumer (java.util.function.Consumer)4 Predicate (java.util.function.Predicate)4 Param (nars.Param)4