Search in sources :

Example 1 with TaskStatistics

use of nars.task.util.TaskStatistics in project narchy by automenta.

the class QuestionTest method questionDrivesInference.

// @Test public void testQuestionHandler() throws Narsese.NarseseException {
// NAR nar = NARS.shell();
// 
// final int[] s = {0};
// new TaskMatch("add(%1, %2, #x)", nar) {
// 
// @Override public boolean test(@NotNull Task task) { return task.isQuestOrQuestion(); }
// 
// @Override
// protected void accept(Task task, Map<Term, Term> xy) {
// System.out.println(task + " " + xy);
// s[0] = xy.size();
// }
// };
// 
// nar.ask($.$("add(1, 2, #x)"));
// 
// assertEquals(3, s[0]);
// 
// }
// @Test public void testOperationHandler() throws Narsese.NarseseException {
// NAR nar = NARS.shell();
// 
// final int[] s = {0};
// StringBuilder match = new StringBuilder();
// new OperationTaskMatch( $.$("add(%1, %2, #x)"), nar) {
// 
// @Override public boolean test(@NotNull Task task) { return task.isQuestOrQuestion(); }
// 
// @Override
// protected void onMatch(Term[] args) {
// match.append(Arrays.toString(args)).append(' ');
// }
// };
// 
// nar.ask($.$("add(1, 2, #x)"));
// 
// assertTrue(match.toString().contains("[1, 2, #1026]"));
// 
// nar.ask($.$("add(1, #x)"));
// nar.ask($.$("(#x --> add)"));
// 
// assertFalse(match.toString().contains("[1, #1026]"));
// }
/**
 * tests whether the use of a question guides inference as measured by the speed to reach a specific conclusion
 */
@Test
public void questionDrivesInference() {
    final int[] dims = { 3, 2 };
    final int timelimit = 2400;
    TaskStatistics withTasks = new TaskStatistics();
    TaskStatistics withoutTasks = new TaskStatistics();
    DoubleSummaryStatistics withTime = new DoubleSummaryStatistics();
    DoubleSummaryStatistics withOutTime = new DoubleSummaryStatistics();
    IntFunction<NAR> narProvider = (seed) -> {
        NAR d = NARS.tmp(1);
        d.random().setSeed(seed);
        d.termVolumeMax.set(16);
        d.freqResolution.set(0.1f);
        return d;
    };
    BiFunction<Integer, Integer, TestNAR> testProvider = (seed, variation) -> {
        NAR n = narProvider.apply(seed);
        TestNAR t = new TestNAR(n);
        switch(variation) {
            case 0:
                new DeductiveMeshTest(t, dims, timelimit);
                break;
            case 1:
                new DeductiveMeshTest(t, dims, timelimit) {

                    @Override
                    public void ask(@NotNull TestNAR n, Term term) {
                    // disabled
                    }
                };
                break;
        }
        return t;
    };
    for (int i = 0; i < 10; i++) {
        int seed = i + 1;
        TestNAR withQuestion = testProvider.apply(seed, 0);
        withQuestion.test(true);
        withTime.accept(withQuestion.time());
        withTasks.add(withQuestion.nar);
        TestNAR withoutQuestion = testProvider.apply(seed, 1);
        withoutQuestion.test(true);
        withOutTime.accept(withoutQuestion.time());
        withoutTasks.add(withoutQuestion.nar);
    }
    withTasks.print();
    withoutTasks.print();
    assertNotEquals(withTime, withOutTime);
    System.out.println("with: " + withTime);
    System.out.println("withOut: " + withOutTime);
// assertTrue(withTime.getSum() < withOutTime.getSum());
// assertTrue(withTime.getSum() < 2 * withOutTime.getSum()); //less than half, considering that a search "diameter" becomes a "radius" by providing the answer end-point
}
Also used : nars(nars) BiFunction(java.util.function.BiFunction) DeductiveMeshTest(nars.test.DeductiveMeshTest) TestNAR(nars.test.TestNAR) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Disabled(org.junit.jupiter.api.Disabled) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) ETERNAL(nars.time.Tense.ETERNAL) Test(org.junit.jupiter.api.Test) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TaskStatistics(nars.task.util.TaskStatistics) NotNull(org.jetbrains.annotations.NotNull) Term(nars.term.Term) IntFunction(java.util.function.IntFunction) TestNAR(nars.test.TestNAR) DeductiveMeshTest(nars.test.DeductiveMeshTest) Term(nars.term.Term) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskStatistics(nars.task.util.TaskStatistics) TestNAR(nars.test.TestNAR) DeductiveMeshTest(nars.test.DeductiveMeshTest) Test(org.junit.jupiter.api.Test)

Aggregations

DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BiFunction (java.util.function.BiFunction)1 IntFunction (java.util.function.IntFunction)1 nars (nars)1 TaskStatistics (nars.task.util.TaskStatistics)1 Term (nars.term.Term)1 DeductiveMeshTest (nars.test.DeductiveMeshTest)1 TestNAR (nars.test.TestNAR)1 ETERNAL (nars.time.Tense.ETERNAL)1 NotNull (org.jetbrains.annotations.NotNull)1 Assertions.assertNotEquals (org.junit.jupiter.api.Assertions.assertNotEquals)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 Disabled (org.junit.jupiter.api.Disabled)1 Test (org.junit.jupiter.api.Test)1