Search in sources :

Example 71 with NAR

use of nars.NAR in project narchy by automenta.

the class DynamicTruthBeliefTable method matchDT.

/**
 * returns an appropriate dt for the root term
 * of beliefs held in the table.  returns 0 if no other value can
 * be computed.
 */
protected int matchDT(long start, long end, boolean commutive, NAR nar) {
    int s = size();
    if (s == 0)
        return 0;
    int dur = nar.dur();
    IntFloatHashMap dtEvi = new IntFloatHashMap(s);
    forEachTask(t -> {
        int tdt = t.dt();
        if (tdt != DTERNAL) {
            if (tdt == XTERNAL)
                throw new RuntimeException("XTERNAL should not be present in " + t);
            if ((t.term().subs() > 2) == commutive)
                // maybe evi
                dtEvi.addToValue(tdt, t.evi(start, end, dur));
        }
    });
    int n = dtEvi.size();
    if (n == 0) {
        return 0;
    } else {
        MutableList<IntFloatPair> ll = dtEvi.keyValuesView().toList();
        int selected = n != 1 ? Roulette.decideRoulette(ll.size(), (i) -> ll.get(i).getTwo(), nar.random()) : 0;
        return ll.get(selected).getOne();
    }
}
Also used : CONJ(nars.Op.CONJ) DTERNAL(nars.time.Tense.DTERNAL) ArrayUtils(org.apache.commons.lang3.ArrayUtils) MutableList(org.eclipse.collections.api.list.MutableList) IntFloatPair(org.eclipse.collections.api.tuple.primitive.IntFloatPair) Roulette(jcog.decide.Roulette) Nullable(org.jetbrains.annotations.Nullable) Task(nars.Task) Truth(nars.truth.Truth) IntFloatHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntFloatHashMap) NAR(nars.NAR) Op(nars.Op) TemporalBeliefTable(nars.table.TemporalBeliefTable) Bool(nars.term.atom.Bool) XTERNAL(nars.time.Tense.XTERNAL) Term(nars.term.Term) IntFloatHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntFloatHashMap) IntFloatPair(org.eclipse.collections.api.tuple.primitive.IntFloatPair)

Example 72 with NAR

use of nars.NAR in project narchy by automenta.

the class Derivation method cycle.

public Derivation cycle(NAR nar, Deriver deri, DeriveRules deriverRoot) {
    NAR pnar = this.nar;
    if (pnar != nar) {
        init(nar);
    }
    long now = nar.time();
    if (now != this.time) {
        this.time = now;
        this.dur = nar.dur();
        this.ditherTime = nar.dtDitherCycles();
        this.freqRes = nar.freqResolution.floatValue();
        this.confRes = nar.confResolution.floatValue();
        this.confMin = nar.confMin.floatValue();
        this.eviMin = c2wSafe(confMin);
        this.termVolMax = nar.termVolumeMax.intValue();
    // transformsCache.cleanUp();
    }
    this.deriver = deri;
    return this;
}
Also used : NAR(nars.NAR)

Example 73 with NAR

use of nars.NAR in project narchy by automenta.

the class NAL6MultistepTest method testBurglarEarthquake1.

/**
 * https://dtai.cs.kuleuven.be/problog/tutorial/basic/02_bayes.html
 */
@Test
public void testBurglarEarthquake1() throws Narsese.NarseseException {
    // 0.7::burglary.
    // 0.2::earthquake.
    // 0.9::p_alarm1.
    // 0.8::p_alarm2.
    // 0.1::p_alarm3.
    // 
    // alarm :- burglary, earthquake, p_alarm1.
    // alarm :- burglary, \+earthquake, p_alarm2.
    // alarm :- \+burglary, earthquake, p_alarm3.
    // 
    // evidence(alarm,true).
    // 
    // query(burglary).
    // query(earthquake).
    NAR n = NARS.tmp();
    // d.log();
    n.input("(burglary). %0.7;0.9%", "(earthquake). %0.2;0.9%", "(p_alarm1). %0.9;0.9%", "(p_alarm2). %0.8;0.9%", "(p_alarm3). %0.1;0.9%", "((&&, (burglary), (earthquake), (p_alarm1)) ==> (alarm)). %1.0;0.95%", "((&&, (burglary), (--,(earthquake)), (p_alarm2)) ==> (alarm)). %1.0;0.95%", "((&&, (--,(burglary)), (earthquake), (p_alarm3)) ==> (alarm)). %1.0;0.95%", "(alarm).", "(burglary)?", "(earthquake)?");
    Concept burglary = null, earthquake = null;
    for (int i = 0; i < 5; i++) {
        // long now = d.time();
        n.run(100);
        burglary = n.conceptualize("(burglary)");
        // burglary.print();  earthquake.print();
        earthquake = n.conceptualize("(earthquake)");
    // System.out.println("burglary=" + burglary.belief(Tense.ETERNAL,0, nar) + "\tearthquake=" + earthquake.belief(Tense.ETERNAL,0, nar));
    }
    n.stats(System.out);
    // result from Probcog:  earthquake=23%, burglary=99%
    Truth burgTruth = n.beliefTruth(burglary, Tense.ETERNAL);
    assertNotNull(burgTruth);
    assertEquals(0.99f, burgTruth.freq(), 0.4f);
    assertEquals(0.31f, n.beliefTruth(earthquake, Tense.ETERNAL).freq(), 0.2f);
}
Also used : TaskConcept(nars.concept.TaskConcept) Concept(nars.concept.Concept) NAR(nars.NAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Example 74 with NAR

use of nars.NAR in project narchy by automenta.

the class NAL6MultistepTest method testBurglarEarthquake2.

/**
 * https://dtai.cs.kuleuven.be/problog/tutorial/basic/02_bayes.html
 */
@Test
public void testBurglarEarthquake2() throws Narsese.NarseseException {
    // 0.7::burglary.
    // 0.2::earthquake.
    // 
    // 0.9::alarm :- burglary, earthquake.
    // 0.8::alarm :- burglary, \+earthquake.
    // 0.1::alarm :- \+burglary, earthquake.
    // 
    // evidence(alarm,true).
    // query(burglary).
    // query(earthquake).
    NAR n = new NARS().get();
    // d.log();
    n.input("(burglary).   %0.7;0.9%", "(earthquake). %0.2;0.9%", "((&&, (burglary), (earthquake)) ==> (alarm)).      %0.9;0.9%", "((&&, (burglary), (--,(earthquake))) ==> (alarm)). %0.8;0.9%", "((&&, (--,(burglary)), (earthquake)) ==> (alarm)). %0.1;0.9%", "(alarm).", "(burglary)?", "(earthquake)?");
    TaskConcept burglary = null, earthquake = null;
    for (int i = 0; i < 5; i++) {
        // burglary.print();  earthquake.print();
        // long now = d.time();
        n.run(100);
        burglary = (TaskConcept) n.conceptualize("(burglary)");
        // burglary.print();  earthquake.print();
        earthquake = (TaskConcept) n.conceptualize("(earthquake)");
        System.out.println("burglary=" + n.beliefTruth(burglary, 0) + "\tearthquake=" + earthquake.beliefs().truth(Tense.ETERNAL, n));
    }
    // result from Probcog:  earthquake=23%, burglary=99%
    assertEquals(0.99f, n.beliefTruth(burglary, 0).freq(), 0.33f);
    assertEquals(0.23f, n.beliefTruth(earthquake, 0).freq(), 0.1f);
}
Also used : NARS(nars.NARS) TaskConcept(nars.concept.TaskConcept) NAR(nars.NAR) Test(org.junit.jupiter.api.Test)

Example 75 with NAR

use of nars.NAR in project narchy by automenta.

the class QueryVariableTest method testQuestionAnswer.

void testQuestionAnswer(@NotNull String beliefString, @NotNull String question) throws Narsese.NarseseException {
    int time = 512;
    AtomicBoolean valid = new AtomicBoolean();
    NAR nar = NARS.tmp();
    Term beliefTerm = $.$(beliefString);
    assertNotNull(beliefTerm);
    // nar.believe(beliefTerm, 1f, 0.9f);
    // assertEquals(1, nar.tasks().count());
    // nar.log();
    nar.question(question, Tense.ETERNAL, (q, a) -> {
        // if (a.term().equals(beliefTerm)) {
        valid.set(true);
        q.delete();
    // }
    });
    nar.believe(beliefTerm, 1f, 0.9f);
    nar.run(time);
    assertTrue(valid.get());
// assertTrue(2 <= nar.tasks().count());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Term(nars.term.Term) NAR(nars.NAR)

Aggregations

NAR (nars.NAR)124 Test (org.junit.jupiter.api.Test)92 NARS (nars.NARS)23 Term (nars.term.Term)20 Truth (nars.truth.Truth)18 TestNAR (nars.test.TestNAR)16 BeliefTable (nars.table.BeliefTable)10 Disabled (org.junit.jupiter.api.Disabled)9 PrologCore (nars.op.prolog.PrologCore)8 Nullable (org.jetbrains.annotations.Nullable)8 Task (nars.Task)7 Concept (nars.concept.Concept)6 Param (nars.Param)5 Termed (nars.term.Termed)5 List (java.util.List)4 FasterList (jcog.list.FasterList)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 nars.$ (nars.$)3 ITask (nars.task.ITask)3 NALTest (nars.util.NALTest)3