use of nars.truth.Truth in project narchy by automenta.
the class ImplicationTest method testBelief.
@Test
public void testBelief() {
// Z, X==>Y
StringBuilder o = new StringBuilder();
for (float condFreq : new float[] { 0, 1, 0.5f }) {
for (boolean sp : B) {
Term z = sp ? x : y;
for (boolean xx : B) {
for (boolean yy : B) {
NAR n = NARS.tmp(6);
Term impl = IMPL.the(x.negIf(!xx), y.negIf(!yy));
n.believe(impl);
n.believe(z, condFreq, n.confDefault(BELIEF));
n.run(256);
Term nz = sp ? y : x;
// BeliefTable nzb = n.concept(nz).beliefs();
// int bs = nzb.size();
// if (bs == 2) {
// nzb.print();
// System.out.println();
// }
// assert(bs == 0 || bs == 1 || bs == 3); //either one answer, or something revised to 0.5 via 2
@Nullable Truth nzt = n.beliefTruth(nz, ETERNAL);
o.append(z + ". %" + n2(condFreq) + "% " + impl + ". " + nz + "=" + nzt + "\n");
}
}
}
}
String oo = o.toString();
System.out.println(oo);
assertContains(oo, "x. %0.0% ((--,x)==>y). y=%1.0;.81%");
assertContains(oo, "y. %0.0% ((--,x)==>y). x=%1.0;.45%");
assertContains(oo, "y. %0.0% (--,((--,x)==>y)). x=%0.0;.45%");
assertContains(oo, "y. %0.0% (--,((--,x)==>y)). x=%0.0;.45%");
// ...
}
use of nars.truth.Truth in project narchy by automenta.
the class CameraSensorTest method assertEquals.
static void assertEquals(Bitmap2DSensor c, float[][] f, long when, NAR n) {
final float tolerance = 0.47f;
for (int i = 0; i < c.width; i++) {
for (int j = 0; j < c.height; j++) {
Scalar p = c.get(i, j);
Truth t = n.beliefTruth(p, when);
if (t == null || Math.abs(f[i][j] - t.freq()) > tolerance) {
System.err.println("pixel " + p + " incorrect @ t=" + n.time());
n.beliefTruth(p, n.time());
p.beliefs().print(System.out);
}
assertNotNull(t, () -> p.term + " is null");
Assertions.assertEquals(f[i][j], t.freq(), tolerance, () -> p + " has inaccurate result @ t=" + n.time());
}
}
}
use of nars.truth.Truth in project narchy by automenta.
the class SpeechPlan method next.
public boolean next() {
// long start = nar.time();
float dur = nar.dur() * durationsPerWord;
long now = nar.time();
long startOfNow = now - (int) Math.ceil(dur);
long endOfNow = now + (int) Math.floor(dur);
FasterList<Pair<Term, Truth>> pending = new FasterList<>(0);
synchronized (vocalize) {
// vocalize.rowKeySet().tailSet(startOfNow-1).clear();
SortedSet<Long> tt = vocalize.rowKeySet().headSet(endOfNow);
if (!tt.isEmpty()) {
LongArrayList ll = new LongArrayList(tt.size());
// copy to array so we can modify the vocalize rows
tt.forEach(ll::add);
ll.forEach(t -> {
Set<Map.Entry<Term, TruthAccumulator>> entries = vocalize.row(t).entrySet();
if (t >= startOfNow) {
entries.forEach(e -> {
Truth x = e.getValue().commitSum();
if (x.expectation() > expectationThreshold)
pending.add(Tuples.pair(e.getKey(), x));
});
}
entries.clear();
});
}
}
if (pending.isEmpty())
return true;
// TODO decide word..
Term spoken = decide(pending);
if (spoken != null)
speak.accept(spoken);
return true;
}
use of nars.truth.Truth in project narchy by automenta.
the class CameraSensorView method update.
@Override
public int update(int x, int y) {
TaskConcept s = cam.get(x, y);
Truth b = s.beliefs().truth(start, end, nar);
float bf = b != null ? b.freq() : 0.5f;
Truth d = s.goals().truth(start, end, nar);
float R = bf * 0.75f, G = bf * 0.75f, B = bf * 0.75f;
if (d != null) {
float f = d.expectation();
// float c = d.conf();
if (f > 0.5f) {
G += 0.25f * (f - 0.5f) * 2f;
} else {
R += 0.25f * (0.5f - f) * 2f;
}
}
return Draw.rgbInt(Util.unitize(R), Util.unitize(G), Util.unitize(B));
}
use of nars.truth.Truth in project narchy by automenta.
the class GoalActionConcept method update.
@Override
public Stream<ITask> update(long pPrev, long pNow, int dur, NAR nar) {
// long pStart =
// //now;
// start - dur / 2;
// long pEnd =
// //now;
// start + dur / 2;
// //now + dur;
float cur = curiosity.floatValue();
Truth goal;
long gStart = pNow - dur / 2;
long gEnd = pNow + dur / 2;
goal = this.goals().truth(gStart, gEnd, nar);
// if (goals.size() > 0)
// System.err.println(term + " " + goal.freq() + " " + goal.evi() + " " + goal.conf());
boolean curi;
if (nar.random().nextFloat() < cur * (1f - (goal != null ? goal.conf() : 0))) {
// // curiosity override
//
float curiConf = // nar.confDefault(GOAL)/4; //<- to max out expectation-driven action
nar.confMin.floatValue() * 8;
// Math.max(goal != null ? goal.conf() : 0, //match goal conf
// //nar.confMin.floatValue() * 2
// nar.confDefault(GOAL)
// );
// nar.confMin.floatValue() * 4;
curi = true;
// nar.confDefault(GOAL) * CURIOSITY_CONF_FACTOR;
// Math.max(goal != null ? goal.conf() : 0,
// nar.confDefault(GOAL) * CURIOSITY_CONF_FACTOR);
// nar.confMin.floatValue()*2);
//
// // float cc =
// // //curiConf;
// // curiConf - (goal != null ? goal.conf() : 0);
// // if (cc > 0) {
//
// // ((float)Math.sin(
// // hashCode() /* for phase shift */
// // + now / (curiPeriod * (2 * Math.PI) * dur)) + 1f)/2f;
//
goal = Truth.theDithered(nar.random().nextFloat(), c2w(curiConf), nar);
// curiosityGoal = null;
// curious = true;
//
// // Truth ct = $.t(f, cc);
// // goal = ct; //curiosity overrides goal
//
// // if (goal == null) {
// // goal = ct;
// // } else {
// // goal = Revision.revise(goal, ct);
// // }
} else {
curi = false;
// action.set(term(), null, stamper, now, dur, nar);
// HACK EXPERIMENT combine belief and goal
// if (belief!=null) {
// float hope = belief.eviEternalized();
// if (goal == null) {
// goal = belief.withEvi(hope); //what one images will happen maybe is what one wants
// } else {
// goal = Revision.revise(goal, belief.withEvi(hope), Math.abs(belief.freq()-goal.freq()), 0 );
// }
// }
// fg = action.set(this, goal, stamper, now, dur, nar);
}
Truth belief = this.beliefs().truth(gStart, gEnd, nar);
Truth feedback = this.motor.apply(belief, goal);
Task feedbackBelief = feedback != null ? this.feedback.add(feedback, gStart, gEnd, dur, nar) : null;
Task curiosityGoal = null;
if (curi && feedbackBelief != null) {
curiosityGoal = this.curiosity(nar, goal, // Truth.theDithered(feedbackBelief.freqMean(dur, pStart, pEnd), goal.evi(), nar),
term, gStart, gEnd, nar.time.nextStamp());
}
return Stream.of(feedbackBelief, (ITask) curiosityGoal).filter(Objects::nonNull);
// return Stream.of(fb, fg).filter(Objects::nonNull);
// return Stream.of(fb).filter(Objects::nonNull);
}
Aggregations