Search in sources :

Example 1 with CSVOutput

use of jcog.meter.event.CSVOutput 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);
}
Also used : Texts.n4(jcog.Texts.n4) nars(nars) LongToFloatFunction(org.eclipse.collections.api.block.function.primitive.LongToFloatFunction) Test(org.junit.jupiter.api.Test) BeliefTable(nars.table.BeliefTable) Truth(nars.truth.Truth) Collectors.toList(java.util.stream.Collectors.toList) RTreeBeliefTable(nars.table.RTreeBeliefTable) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) CSVOutput(jcog.meter.event.CSVOutput) BELIEF(nars.Op.BELIEF) Termed(nars.term.Termed) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TaskConcept(nars.concept.TaskConcept) NotNull(org.jetbrains.annotations.NotNull) MultiStatistics(jcog.math.MultiStatistics) Term(nars.term.Term) TaskConcept(nars.concept.TaskConcept) MultiStatistics(jcog.math.MultiStatistics) Term(nars.term.Term) NotNull(org.jetbrains.annotations.NotNull) BeliefTable(nars.table.BeliefTable) RTreeBeliefTable(nars.table.RTreeBeliefTable) Truth(nars.truth.Truth) CSVOutput(jcog.meter.event.CSVOutput)

Example 2 with CSVOutput

use of jcog.meter.event.CSVOutput in project narchy by automenta.

the class Optimize method run.

public Result<X> run(int maxIterations, int repeats, FloatFunction<Supplier<X>> eval) {
    assert (repeats >= 1);
    final int dim = tweaks.size();
    double[] mid = new double[dim];
    // double[] sigma = new double[n];
    double[] min = new double[dim];
    double[] max = new double[dim];
    double[] inc = new double[dim];
    // double[] range = new double[dim];
    X example = subject.get();
    int i = 0;
    for (Tweak w : tweaks) {
        TweakFloat s = (TweakFloat) w;
        // initial guess: get from sample, otherwise midpoint of min/max range
        Object guess = s.get(example);
        mid[i] = guess != null ? ((float) guess) : ((s.getMax() + s.getMin()) / 2f);
        min[i] = (s.getMin());
        max[i] = (s.getMax());
        inc[i] = s.getInc();
        // range[i] = max[i] - min[i];
        // sigma[i] = Math.abs(max[i] - min[i]) * 0.75f; //(s.getInc());
        i++;
    }
    FasterList<DoubleObjectPair<double[]>> experiments = new FasterList<>(maxIterations);
    final double[] maxScore = { Double.NEGATIVE_INFINITY };
    ObjectiveFunction func = new ObjectiveFunction(point -> {
        double score;
        try {
            double sum = 0;
            for (int r = 0; r < repeats; r++) {
                Supplier<X> x = () -> subject(point);
                sum += eval.floatValueOf(x);
            }
            score = sum / repeats;
        } catch (Exception e) {
            logger.error("{} {} {}", this, point, e);
            score = Float.NEGATIVE_INFINITY;
        }
        if (trace)
            csv.out(ArrayUtils.add(point, (int) 0, score));
        maxScore[0] = Math.max(maxScore[0], score);
        // System.out.println(
        // n4(score) + " / " + n4(maxScore[0]) + "\t" + n4(point)
        // );
        experiments.add(pair(score, point));
        experimentIteration(point, score);
        return score;
    });
    if (trace)
        csv = new CSVOutput(System.out, Stream.concat(Stream.of("score"), tweaks.stream().map(t -> t.id)).toArray(String[]::new));
    experimentStart();
    try {
        solve(dim, func, mid, min, max, inc, maxIterations);
    } catch (Throwable t) {
        logger.info("solve {} {}", func, t);
    }
    return new Result<>(experiments, tweaks);
}
Also used : MultiDirectionalSimplex(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.MultiDirectionalSimplex) Logger(org.slf4j.Logger) SortedSet(java.util.SortedSet) DoubleObjectPair(org.eclipse.collections.api.tuple.primitive.DoubleObjectPair) MathArrays(org.apache.commons.math3.util.MathArrays) FasterList(jcog.list.FasterList) LoggerFactory(org.slf4j.LoggerFactory) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) Supplier(java.util.function.Supplier) SimplexOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer) List(java.util.List) Stream(java.util.stream.Stream) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) GoalType(org.apache.commons.math3.optim.nonlinear.scalar.GoalType) Map(java.util.Map) PrimitiveTuples.pair(org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples.pair) CSVOutput(jcog.meter.event.CSVOutput) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) Pair(org.eclipse.collections.api.tuple.Pair) FloatFunction(org.eclipse.collections.api.block.function.primitive.FloatFunction) Joiner(com.google.common.base.Joiner) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) FasterList(jcog.list.FasterList) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) DoubleObjectPair(org.eclipse.collections.api.tuple.primitive.DoubleObjectPair) CSVOutput(jcog.meter.event.CSVOutput)

Aggregations

CSVOutput (jcog.meter.event.CSVOutput)2 Joiner (com.google.common.base.Joiner)1 List (java.util.List)1 Map (java.util.Map)1 SortedSet (java.util.SortedSet)1 Supplier (java.util.function.Supplier)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 Texts.n4 (jcog.Texts.n4)1 FasterList (jcog.list.FasterList)1 MultiStatistics (jcog.math.MultiStatistics)1 nars (nars)1 BELIEF (nars.Op.BELIEF)1 TaskConcept (nars.concept.TaskConcept)1 BeliefTable (nars.table.BeliefTable)1 RTreeBeliefTable (nars.table.RTreeBeliefTable)1 Term (nars.term.Term)1 Termed (nars.term.Termed)1 Truth (nars.truth.Truth)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1