use of nars.concept.TaskConcept in project narchy by automenta.
the class DynamicTruthBeliefTableTest method testDynamicConjunction2Temporal.
@Test
public void testDynamicConjunction2Temporal() throws Narsese.NarseseException {
NAR n = NARS.shell();
n.believe($("(x)"), (long) 0);
n.believe($("(y)"), (long) 4);
n.time.dur(8);
TaskConcept cc = (TaskConcept) n.conceptualize($("((x) && (y))"));
DynamicTruthBeliefTable xtable = (DynamicTruthBeliefTable) cc.beliefs();
Compound template = $("((x) &&+4 (y))");
DynTruth xt = xtable.truth(0, 0, template, n);
assertNotNull(xt);
// assertTrue($.t(1f, 0.81f).equals(xt.truth(n), 0.1f), xt.truth(n).toString());
// best match to the input
assertEquals(0.81f, xtable.taskDynamic(0, 0, $("((x) &&+4 (y))"), n).conf(), 0.05f);
assertEquals(0.74f, xtable.taskDynamic(0, 0, $("((x) &&+6 (y))"), n).conf(), 0.07f);
assertEquals(0.75f, xtable.taskDynamic(0, 0, $("((x) &&+2 (y))"), n).conf(), 0.07f);
assertEquals(0.75f, xtable.taskDynamic(0, 0, $("((x) &&+0 (y))"), n).conf(), 0.07f);
assertEquals(0.62f, xtable.taskDynamic(0, 0, $("((x) &&-32 (y))"), n).conf(), 0.2f);
}
use of nars.concept.TaskConcept in project narchy by automenta.
the class RTreeBeliefTableTest method testAccuracy.
static void testAccuracy(int dur, int period, int end, int cap, LongToFloatFunction func) {
NAR n = NARS.shell();
n.time.dur(dur);
Term term = $.p("x");
// 1. populate
// n.log();
TaskConcept c = (TaskConcept) n.conceptualize(term);
@NotNull BeliefTable cb = true ? c.beliefs() : c.goals();
cb.setCapacity(0, cap);
// int numTasks = 0;
System.out.println("points:");
long time;
long start = n.time();
while ((time = n.time()) < end) {
float f = func.valueOf(time);
System.out.print(time + "=" + f + "\t");
n.input($.task(term, BELIEF, f, 0.9f).time(time).setPriThen(0.5f).apply(n));
n.run(period);
c.beliefs().print();
System.out.println();
// numTasks++;
}
System.out.println();
System.out.println();
MultiStatistics<Task> m = new MultiStatistics<Task>().classify("input", (t) -> t.isInput()).classify("derived", (t) -> t instanceof DerivedTask).value("pri", (t) -> t.pri()).value2D("truth", (t) -> new float[] { t.freq(), t.conf() }).value("freqErr", (t) -> Math.abs(((t.freq() - 0.5f) * 2f) - func.valueOf(t.mid()))).add(c.beliefs().streamTasks().collect(toList()));
System.out.println();
m.print();
System.out.println();
c.beliefs().print();
// 2. validate and calculate error
CSVOutput csv = new CSVOutput(System.out, "time", "actual", "approx");
double errSum = 0;
for (long i = start; i < end; i++) {
float actual = func.valueOf(i);
Truth actualTruth = n.beliefTruth(term, i);
float approx, err;
if (actualTruth != null) {
approx = actualTruth.freq();
err = Math.abs(approx - actual);
} else {
approx = Float.NaN;
err = 1f;
}
errSum += err;
csv.out(i, actual, approx);
// System.out.println(n2(i) + "\t" + /*n2(err) + "\t" + */ n2(expected) + "\t" + n2(actual));
}
double avgErr = errSum / (end - start + 1);
System.out.println();
System.out.println(n4(avgErr) + " avg freq err per point");
assertTrue(avgErr < 0.1f);
}
use of nars.concept.TaskConcept in project narchy by automenta.
the class RTreeBeliefTableTest method testBasicOperations.
@Test
public void testBasicOperations() throws Narsese.NarseseException {
NAR n = NARS.shell();
Term ab = nars.$.$("a:b");
TaskConcept X = (TaskConcept) n.conceptualize(ab);
RTreeBeliefTable r = RTreeBeliefTable.build(ab);
r.setCapacity(4);
assertEquals(0, r.size());
Term x = X.term();
float freq = 1f;
float conf = 0.9f;
int creationTime = 1;
int start = 1, end = 1;
Task a = add(r, x, freq, conf, start, end, n);
assertEquals(1, r.size());
r.add(a, X, n);
r.print(System.out);
// no change for inserted duplicate
assertEquals(1, r.size());
// WEAKer
Task b = add(r, x, 0f, 0.5f, 1, 1, n);
assertEquals(2, r.size());
Task c = add(r, x, 0.1f, 0.9f, 2, 2, n);
assertEquals(3, r.size());
Task d = add(r, x, 0.1f, 0.9f, 3, 4, n);
assertEquals(4, r.size());
System.out.println("at capacity");
r.print(System.out);
// try capacity limit
Task e = add(r, x, 0.3f, 0.9f, 3, 4, n);
System.out.println("\nat capacity?");
r.print(System.out);
r.forEachTask(System.out::println);
// capacity limit unaffected
assertEquals(4, r.size());
System.out.println("after capacity compress inserting " + e.toString(true));
r.print(System.out);
}
use of nars.concept.TaskConcept in project narchy by automenta.
the class BeliefTableTest method assertDuration.
static void assertDuration(NAR n, String c, long start, long end) throws Narsese.NarseseException {
TaskConcept cc = (TaskConcept) n.conceptualize(c);
assertNotNull(cc, c + " unconceptualized");
List<Task> tt = cc.beliefs().streamTasks().collect(toList());
assertTrue(cc.beliefs() instanceof DynamicTruthBeliefTable || !tt.isEmpty(), c + " not believed");
if (!tt.isEmpty()) {
Task t = tt.get(0);
// System.out.println(sim.proof());
// System.out.println(sim.start() + ".." + /*sim.occurrence() + ".."*/ + sim.end());
assertEquals(start, t.start());
assertEquals(end, t.end());
}
}
use of nars.concept.TaskConcept in project narchy by automenta.
the class BeliefTableTest method testConceptualizationIntermpolation.
@Test
public void testConceptualizationIntermpolation() throws Narsese.NarseseException {
for (Tense t : new Tense[] { Present, Eternal }) {
NAR n = NARS.tmp();
n.dtMergeOrChoose.set(true);
n.believe("((a ==>+2 b)-->[pill])", t, 1f, 0.9f);
n.believe("((a ==>+6 b)-->[pill])", t, 1f, 0.9f);
// @NotNull Bag<Concept, PLink<Concept>> cb = n.focus.active;
// assertTrue(5 <= cb.size());
String abpill = "((a==>b)-->[pill])";
// iterator().next().get();//((ArrayBag<Concept>) cb).get(0).get();
TaskConcept cc = (TaskConcept) n.conceptualize(abpill);
assertNotNull(cc);
String correctMerge = "((a ==>+4 b)-->[pill])";
cc.beliefs().print();
// test belief match interpolated a result
long when = t == Present ? 0 : ETERNAL;
assertEquals(correctMerge, cc.beliefs().match(when, null, n).term().toString());
// test merge after capacity shrink:
// set to capacity=1 to force compression
cc.beliefs().setCapacity(1, 1);
cc.print();
// n.forEachTask(System.out::println);
// INTERMPOLATION APPLIED AFTER REVECTION:
assertEquals(correctMerge, cc.beliefs().match(0, null, n).term().toString());
}
}
Aggregations