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();
}
}
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;
}
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);
}
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);
}
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());
}
Aggregations