use of nars.truth.Truth in project narchy by automenta.
the class BeliefTable method answer.
/**
* projects a match
*/
@Nullable
default Task answer(long start, long end, int dur, @Nullable Task question, Term template, NAR nar, Consumer<Task> withNovel) {
if (isEmpty())
return null;
Task answer = match(start, end, template, nar);
if (answer == null || answer.isDeleted())
return null;
// (answer instanceof AnswerTask); //includes: answers, revision, or dynamic
boolean novel = false;
// project if different occurrence
boolean relevantTime = start == ETERNAL || answer.intersects(start, end);
if (/*!answer.isEternal() && */
!relevantTime) {
long t = answer.nearestPointExternal(start, end);
Truth aProj = answer.truth(t, dur, nar);
if (aProj != null) {
final Task aa = answer;
Task a = Task.tryTask(answer.term(), answer.punc(), aProj, (content, truth) -> new NALTask(content, aa.punc(), truth, nar.time(), t, t, aa.stamp()));
// Stamp.zip(aa.stamp(), question.stamp(), 0.5f) : aa.stamp()));
if (a == null)
return null;
float confFrac = Util.unitize(aProj.evi() / answer.evi());
a.priSet(answer.priElseZero() * confFrac);
if (question != null)
((NALTask) a).cause = Cause.sample(Param.causeCapacity.intValue(), question, answer);
// if (Param.DEBUG)
// a.log("Answer Projected");
// because it was projected
novel = true;
// relevantTime = true;
answer = a;
}
}
if (novel && question != null && question.isQuestOrQuestion()) {
withNovel.accept(answer);
}
return answer;
}
use of nars.truth.Truth in project narchy by automenta.
the class TruthPolation method truth.
/**
* blends any result with an eternal "background" contribution
*/
public Truth truth(@Nullable Task eternalTask) {
Truth temporal = truth(true);
Truth eternal = eternalTask != null ? eternalTask.truth() : null;
if (eternal == null)
return temporal;
else if (temporal == null)
return eternal;
else {
return Revision.revise(temporal, eternal);
// float tempEvi = t.eviSum;
// boolean someEvi = tempEvi > 0f;
// if (topEternal != null) {
// if (!someEvi) {
// return new PreciseTruth(topEternal.truth()); //eternal the only authority
// } else {
//
// //long totalSpan = Math.max(1, t.spanEnd - t.spanStart);
// long totalCovered = Math.max(1, t.rangeSum); //estimate
// float temporalDensity = ((float) totalCovered) / Math.max(1, end - start);
// float eviDecay = 1 / ((1 + tempEvi * temporalDensity));
//
// float eteEvi = topEternal.evi();
//
// t.accept(topEternal.freq(), eteEvi * eviDecay);
// }
// }
//
// return !someEvi ? null : t.truth();
}
}
use of nars.truth.Truth in project narchy by automenta.
the class BooleanTest method testSAT2Individual.
void testSAT2Individual(int i, int j) throws Narsese.NarseseException {
final float confThresh = 0.7f;
Param.DEBUG = true;
// for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 2; j++) {
NAR d = NARS.tmp();
d.freqResolution.set(0.05f);
d.termVolumeMax.set(24);
String[] outcomes = { // "(x-->(0,0))",
"x:{0}", // "(x-->(0,1))",
"x:{1}", // "(x-->(1,0))",
"x:{2}", // "(x-->(1,1))"};
"x:{3}" };
// String expected = "(x --> (" + i + "," + j + "))";
d.believe("( (--(x-->i) && --(x-->j)) ==> " + outcomes[0] + ")");
d.believe("( (--(x-->i) && (x-->j)) ==> " + outcomes[1] + ")");
d.believe("( ((x-->i) && --(x-->j)) ==> " + outcomes[2] + ")");
d.believe("( ((x-->i) && (x-->j)) ==> " + outcomes[3] + ")");
Term I = $.$("(x-->i)").negIf(i == 0);
Term J = $.$("(x-->j)").negIf(j == 0);
// d.believe(I);
// d.believe(J);
d.believe(CONJ.the(I, J));
// for (String s : outcomes) {
// d.ask(s);
// }
d.run(1024);
System.out.println(i + " " + j + " ");
for (int k = 0, outcomesLength = outcomes.length; k < outcomesLength; k++) {
String s = outcomes[k];
Concept dc = d.conceptualize(s);
assertNotNull(dc);
@Nullable Task t = d.belief(dc.term(), d.time());
Truth b = t != null ? t.truth() : null;
System.out.println("\t" + s + "\t" + b + "\t" + outcomes[k]);
int ex = -1, ey = -1;
switch(k) {
case 0:
ex = 0;
ey = 0;
break;
case 1:
ex = 0;
ey = 1;
break;
case 2:
ex = 1;
ey = 0;
break;
case 3:
ex = 1;
ey = 1;
break;
}
boolean thisone = ((ex == i) && (ey == j));
if (thisone && b == null)
fail("unrecognized true case");
if (thisone && b.isNegative() && b.conf() > confThresh)
fail("wrong true case:\n" + t.proof());
if (!thisone && b != null && b.isPositive() && b.conf() > confThresh)
fail("wrong false case:\n" + t.proof());
}
// System.out.println();
// return;
// }
// }
}
use of nars.truth.Truth in project narchy by automenta.
the class NAL7ImplProjectionTest method test1.
@Test
public void test1() {
int implDT = 5;
int dur = 1;
/* eventTime, relative to impl belief */
for (int implTime = 0; implTime < 5; implTime += 2) {
for (int eventTime = 0; eventTime < 5; eventTime += 2) {
Term y = $.the("y");
Param.DEBUG = true;
NAR n = NARS.tmp();
// n.log();
n.time.dur(dur);
n.inputAt(eventTime, "x. :|:");
n.inputAt(implTime, "(x ==>+" + implDT + " y). :|:");
int end = Math.max(eventTime + implDT, implTime);
n.run(end + 1);
double[] max = new MyBrentOptimizer(0.001f, 0.01, 0, end, (t) -> {
Truth u = n.beliefTruth(y, Math.round(t));
if (u == null)
return -1f;
return u.conf();
}).max(0, end, end / 2.0);
long yTimeEstimate = Math.round(max[0]);
long yTimeActual = eventTime + implDT;
assertTrue(Math.abs(yTimeEstimate - yTimeActual) <= 1);
double yConfMax = max[1];
long eventBeliefDelta = Math.abs(eventTime - implTime);
System.out.println("+-" + eventBeliefDelta + " -> " + max[0] + "=" + max[1]);
}
}
// double zero = UnivariateSolverUtils.solve(t->{
// return 0;
// }, 0, end, 0.01f);
// n.concept(x).print();
// n.concept(y).print();
}
use of nars.truth.Truth in project narchy by automenta.
the class ImplicationNetworkTest method testEternal_A_PosBelief_ToBC.
@Test
public void testEternal_A_PosBelief_ToBC() {
NAR n = NARS.tmp();
Param.DEBUG = true;
n.believe(IMPL.the(a, b));
n.believe(IMPL.the(b, c));
n.believe(a);
n.run(100);
BeliefTable aBeliefs = n.concept(a).beliefs();
Truth aBelief = aBeliefs.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, aBeliefs.size());
BeliefTable bBeliefs = n.concept(b).beliefs();
Truth bBelief = bBeliefs.truth(ETERNAL, n);
// n.concept(b).print();
// assertEquals(1, bBeliefs.size());
// b should have less conf than a but higher than c
// same freq among all
Truth cBelief = n.concept(c).beliefs().truth(ETERNAL, n);
// n.concept(c).print();
System.out.println("a: " + aBelief);
System.out.println("b: " + bBelief);
System.out.println("c: " + cBelief);
assertEquals(aBelief.freq(), bBelief.freq(), n.freqResolution.floatValue());
assertEquals(bBelief.freq(), cBelief.freq(), n.freqResolution.floatValue());
assertTrue(aBelief.conf() - bBelief.conf() > n.confResolution.floatValue() * 2);
assertTrue(bBelief.conf() - cBelief.conf() > n.confResolution.floatValue() * 2);
}
Aggregations