Search in sources :

Example 1 with DurService

use of nars.control.DurService in project narchy by automenta.

the class ScalarBeliefTableTest method test1.

@Test
public void test1() {
    NAR n = NARS.shell();
    MutableFloat xx = new MutableFloat(0);
    Scalar x = new Scalar($.the("x"), xx, n);
    DurService xAuto = x.auto(n, 1);
    ScalarBeliefTable xb = (ScalarBeliefTable) x.beliefs();
    n.synch();
    n.run(1);
    assertEquals(1, xb.series.size());
    assertEquals(1, xb.size());
    xx.set(0.5f);
    n.run(1);
    assertEquals(2, xb.series.size());
    assertEquals(2, xb.size());
}
Also used : MutableFloat(org.apache.commons.lang3.mutable.MutableFloat) NAR(nars.NAR) Scalar(nars.concept.scalar.Scalar) DurService(nars.control.DurService) Test(org.junit.jupiter.api.Test)

Example 2 with DurService

use of nars.control.DurService in project narchy by automenta.

the class NAgent method runSynch.

/**
 * default rate = 1 dur/ 1 frame
 */
public void runSynch(int frames) {
    DurService d = DurService.on(nar, this);
    nar.run(frames * nar.dur() + 1);
    d.off();
}
Also used : DurService(nars.control.DurService)

Example 3 with DurService

use of nars.control.DurService in project narchy by automenta.

the class NARIn method taskWhile.

default DurService taskWhile(Term term, byte punc, Truth tru, Predicate<Task> cond) {
    // HACK
    NAR n = (NAR) this;
    long start = n.time();
    float activeFreq = tru.freq();
    float inactiveFreq = 0f;
    float evi = tru.evi();
    LongFunction<Truthlet> stepUntil = (toWhen) -> {
        return Truthlet.step(inactiveFreq, start, activeFreq, toWhen, activeFreq, evi);
    };
    TruthletTask t = new TruthletTask(term, punc, stepUntil.apply(start), n);
    float pri = n.priDefault(punc);
    t.priMax(pri);
    n.input(t);
    return DurService.onWhile(n, (nn) -> {
        // nn.runLater(()->{
        // t.concept(nn, false).goals().print();
        // System.out.println();
        // });
        long now = nn.time();
        boolean kontinue;
        Truthlet tt;
        if (!cond.test(t)) {
            // convert from step function to impulse function which
            // stops at the current time and end the service
            tt = Truthlet.impulse(start, now, activeFreq, inactiveFreq, evi);
            kontinue = false;
        } else {
            // stretch the step function to current time
            tt = stepUntil.apply(now);
            kontinue = true;
        }
        t.priMax(pri);
        t.truth(tt, true, nn);
        return kontinue;
    });
}
Also used : DurService(nars.control.DurService) LongFunction(java.util.function.LongFunction) Predicate(java.util.function.Predicate) ITask(nars.task.ITask) ActiveQuestionTask(nars.task.ActiveQuestionTask) Truthlet(nars.task.signal.Truthlet) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) TruthletTask(nars.task.signal.TruthletTask) Truth(nars.truth.Truth) BiConsumer(java.util.function.BiConsumer) BELIEF(nars.Op.BELIEF) NotNull(org.jetbrains.annotations.NotNull) Term(nars.term.Term) GOAL(nars.Op.GOAL) Truthlet(nars.task.signal.Truthlet) TruthletTask(nars.task.signal.TruthletTask)

Example 4 with DurService

use of nars.control.DurService in project narchy by automenta.

the class ExeCharts method metaGoalPlot.

public static Surface metaGoalPlot(NAR nar) {
    int s = nar.causes.size();
    FloatRange gain = new FloatRange(20f, 0f, 20f);
    BitmapMatrixView bmp = new BitmapMatrixView((i) -> Util.tanhFast(gain.floatValue() * nar.causes.get(i).value()), // Util.tanhFast(nar.causes.get(i).value()),
    s, Math.max(1, (int) Math.ceil(Math.sqrt(s))), Draw::colorBipolar) {

        DurService on;

        {
            on = DurService.on(nar, this::update);
        }

        @Override
        public void stop() {
            on.off();
            on = null;
            super.stop();
        }
    };
    return new Splitting(bmp, new AutoSurface<>(gain), 0.1f);
}
Also used : BitmapMatrixView(spacegraph.space2d.widget.meter.BitmapMatrixView) Splitting(spacegraph.space2d.container.Splitting) Draw(spacegraph.video.Draw) FloatRange(jcog.math.FloatRange) DurService(nars.control.DurService)

Example 5 with DurService

use of nars.control.DurService in project narchy by automenta.

the class ExeCharts method metaGoalChart.

private static Surface metaGoalChart(NAgent a) {
    return new TreeChart<Cause>() {

        final DurService on;

        final FasterList<ItemVis<Cause>> cache = new FasterList();

        final Function<Cause, TreeChart.ItemVis<Cause>> builder = ((i) -> {
            short id = i.id;
            ItemVis<Cause> item;
            if (cache.capacity() - 1 < id)
                cache.ensureCapacity(id + 16);
            else {
                item = cache.get(id);
                if (item != null)
                    return item;
            }
            String str = i.toString();
            if (str.startsWith("class nars."))
                // skip default toString
                str = str.substring("class nars.".length());
            if (str.startsWith("class "))
                // skip default toString
                str = str.substring(5);
            item = new CauseVis(i, str);
            cache.set(id, item);
            return item;
        });

        {
            on = a.onFrame(() -> {
                update(a.nar().causes, (c, i) -> {
                    float v = c.value();
                    float r, g, b;
                    if (v < 0) {
                        r = 0.75f * Math.max(0.1f, Math.min(1f, -v));
                        g = 0;
                    } else {
                        g = 0.75f * Math.max(0.1f, Math.min(1f, +v));
                        r = 0;
                    }
                    float t = Util.sum(((FloatFunction<Traffic>) (p -> Math.abs(p.last))), c.goal);
                    b = Math.max(r, g) / 2f * Util.unitize(t);
                    i.update(v, r, g, b);
                // i.updateMomentum(
                // //0.01f + Util.sqr(Util.tanhFast(v)+1),
                // //Math.signum(v) *(1+Math.abs(v))*(t),
                // //Math.signum(v) * t,
                // v,
                // 0.25f,
                // r, g, b);
                }, builder);
            });
        }

        @Override
        public void stop() {
            super.stop();
            on.off();
        }
    };
}
Also used : GL2(com.jogamp.opengl.GL2) IntStream(java.util.stream.IntStream) BaseSlider(spacegraph.space2d.widget.slider.BaseSlider) DurService(nars.control.DurService) Causable(nars.exe.Causable) Surface(spacegraph.space2d.Surface) TreeChart(spacegraph.space2d.widget.meter.TreeChart) StringUtils(org.apache.commons.lang3.StringUtils) Draw(spacegraph.video.Draw) Function(java.util.function.Function) AutoSurface(spacegraph.space2d.widget.meta.AutoSurface) Traffic(nars.control.Traffic) Gridding(spacegraph.space2d.container.Gridding) BitmapMatrixView(spacegraph.space2d.widget.meter.BitmapMatrixView) FloatSlider(spacegraph.space2d.widget.slider.FloatSlider) NAgent(nars.NAgent) FasterList(jcog.list.FasterList) FloatRange(jcog.math.FloatRange) nars.$(nars.$) LoopPanel(spacegraph.space2d.widget.meta.LoopPanel) Collectors(java.util.stream.Collectors) Util(jcog.Util) RectFloat2D(jcog.tree.rtree.rect.RectFloat2D) CheckBox(spacegraph.space2d.widget.button.CheckBox) List(java.util.List) Splitting(spacegraph.space2d.container.Splitting) NAR(nars.NAR) Cause(nars.control.Cause) MetaGoal(nars.control.MetaGoal) FloatFunction(org.eclipse.collections.api.block.function.primitive.FloatFunction) Label(spacegraph.space2d.widget.text.Label) Function(java.util.function.Function) FloatFunction(org.eclipse.collections.api.block.function.primitive.FloatFunction) FasterList(jcog.list.FasterList) Cause(nars.control.Cause) Traffic(nars.control.Traffic) TreeChart(spacegraph.space2d.widget.meter.TreeChart) DurService(nars.control.DurService)

Aggregations

DurService (nars.control.DurService)6 NAR (nars.NAR)3 Consumer (java.util.function.Consumer)2 Predicate (java.util.function.Predicate)2 FloatRange (jcog.math.FloatRange)2 nars.$ (nars.$)2 BELIEF (nars.Op.BELIEF)2 Term (nars.term.Term)2 Truth (nars.truth.Truth)2 Nullable (org.jetbrains.annotations.Nullable)2 Splitting (spacegraph.space2d.container.Splitting)2 BitmapMatrixView (spacegraph.space2d.widget.meter.BitmapMatrixView)2 Draw (spacegraph.video.Draw)2 GL2 (com.jogamp.opengl.GL2)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 LongFunction (java.util.function.LongFunction)1 Collectors (java.util.stream.Collectors)1