Search in sources :

Example 6 with DurService

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

the class ThermostatTest method main.

// @Test
// @Disabled
public static void main(String[] args) {
    // void test1() {
    // Param.DEBUG = true;
    final int DUR = 1;
    final int subTrainings = 2;
    // pause between episodes
    final int thinkDurs = 4;
    NAR n = NARS.tmp();
    n.time.dur(DUR);
    n.timeFocus.set(2);
    n.termVolumeMax.set(34);
    // n.freqResolution.set(0.05f);
    // n.confResolution.set(0.01f);
    n.activationRate.set(0.5f);
    n.goalPriDefault.set(1f);
    // n.forgetRate.set(2f);
    // n.deep.set(0.8);
    // n.emotion.want(MetaGoal.Desire, 0.2f);
    // n.want(MetaGoal.Believe, 0.1f);
    // n.want(MetaGoal.Perceive, -0.01f);
    float exeThresh = 0.51f;
    // new ArithmeticIntroduction(8, n);
    new ConjClustering(n, BELIEF, (t) -> true, 8, 32);
    // n.priDefault(BELIEF, 0.3f);
    // n.logPriMin(System.out, 0.5f);
    // n.logWhen(System.out, false, true, true);
    // n.log();
    boolean[] training = new boolean[] { true };
    Opjects op = new Opjects(n) {

        // {
        // pretend = true;
        // }
        @Override
        @Nullable
        protected synchronized Object invoked(Object obj, Method wrapped, Object[] args, Object result) {
            if (training[0]) {
                n.synch();
            // n.runLater(nn -> nn.run(DUR)); //queue some thinking cycles
            }
            Object y = super.invoked(obj, wrapped, args, result);
            if (training[0])
                n.run(DUR * thinkDurs);
            return y;
        }
    };
    Teacher<Thermostat> env = new Teacher<>(op, Thermostat.class);
    Consumer<Thermostat> hotToCold = Thermostat.change(true, false), coldToCold = Thermostat.change(false, false), coldToHot = Thermostat.change(false, true), hotToHot = Thermostat.change(true, true);
    Predicate<Thermostat> isCold = x -> x.is() == Thermostat.cold;
    Predicate<Thermostat> isHot = x -> x.is() == Thermostat.hot;
    n.logWhen(System.out, true, true, true);
    boolean stupid = true;
    training: do {
        training[0] = true;
        op.exeThresh.set(1f);
        for (int i = 0; i < subTrainings; i++) {
            for (Consumer<Thermostat> condition : new Consumer[] { hotToCold, coldToCold }) {
                System.out.println("EPISODE START");
                n.clear();
                env.teach("down", condition, (Thermostat x) -> {
                    // x.up(); //demonstrate no change
                    // x.report();
                    n.run(1);
                    while (x.is() > Thermostat.cold) {
                        x.down();
                        n.run(1);
                    }
                    x.report();
                    n.run(1);
                // x.down(); //demonstrate no change
                // x.report();
                }, isCold);
                System.out.println("EPISODE END");
                n.run(thinkDurs * n.dur());
            // n.concept("do(down)").print();
            }
            for (Consumer<Thermostat> condition : new Consumer[] { coldToHot, hotToHot }) {
                System.out.println("EPISODE START");
                n.clear();
                env.teach("up", condition, x -> {
                    // x.down(); //demonstrate no change
                    // x.report();
                    n.run(1);
                    while (!isHot.test(x)) {
                        x.up();
                        n.run(1);
                    }
                    x.report();
                    n.run(1);
                // x.up(); //demonstrate no change
                // x.report();
                }, isHot);
                System.out.println("EPISODE END");
                n.run(thinkDurs * n.dur());
            }
        }
        System.out.println("VALIDATING");
        System.out.println();
        training[0] = false;
        op.exeThresh.set(exeThresh);
        // n.log();
        // n.run(100);
        // new Implier(n, new float[] { 1f },
        // $.$("a_Thermostat(down,())"),
        // $.$("a_Thermostat(up,())")
        // //$.$("a_Thermostat(is,(),#x)")
        // );
        // try {
        // make cold
        // n.input(new NALTask($.$("a_Thermostat(should,(),0)"),
        // BELIEF, $.t(1f, 0.99f),
        // n.time(), n.time(), n.time()+1000,
        // n.time.nextInputStamp()).pri(1f));
        Thermostat t = env.x;
        {
            // n.clear();
            t.is(3);
            t.should(0);
            n.run(thinkDurs * n.dur());
            Term cold = $.$$("is(a_Thermostat,0)");
            // Term cold = $.$safe("(a_Thermostat(is,(),0) &| --a_Thermostat(is,(),3))");
            Term hot = $.$$("is(a_Thermostat,3)");
            Truth goalTruth = $.t(1f, 0.9f);
            DurService xPos = n.wantWhile(cold, goalTruth, new TaskConceptLogger(n, (w) -> (t.current != t.target)));
            DurService xNeg = n.wantWhile(hot, goalTruth.neg(), new TaskConceptLogger(n, (w) -> t.current != t.target));
            n.run(1);
            for (int i = 0; i < 16 && xPos.isOn(); i++) {
                int period = 100;
                // t.report();
                // n.run(period, pause);
                n.run(period);
            }
            xPos.off();
            xNeg.off();
            t.report();
            if (t.is() == t.should()) {
                System.out.println("good job nars!");
                n.believe($.$$("(learn(up) && learn(down))"), Tense.Present);
                stupid = false;
            } else {
                System.out.println("bad job nars! try again");
                n.believe($.$$("(--learn(up) && --learn(down))"), Tense.Present);
            }
        // n.input(new NALTask($.$safe("a_Thermostat(is,(),0)"),
        // GOAL, $.t(1f, 0.95f),
        // n.time(), n.time(), n.time() + periods,
        // n.time.nextInputStamp()).pri(1f));
        // n.input(new NALTask($.$safe("a_Thermostat(is,(),3)"),
        // GOAL, $.t(0f, 0.95f),
        // n.time(), n.time(), n.time() + periods,
        // n.time.nextInputStamp()).pri(1f));
        }
    } while (false);
    // n.run(thinkDurs * n.dur());
    {
    // n.input(new NALTask($.$safe("a_Thermostat(is,(),3)"),
    // GOAL, $.t(0f, 0.99f),
    // n.time(), n.time(), n.time()+1000,
    // n.time.nextInputStamp()).pri(1f));
    }
    // while (t.is() != t.should()) {
    // int period = 1000;
    // t.report();
    // n.run(period);
    // }
    n.tasks().forEach(t -> {
        if (!t.isInput())
            System.out.println(t);
    });
}
Also used : DurService(nars.control.DurService) Tense(nars.time.Tense) NARS(nars.NARS) Predicate(java.util.function.Predicate) nars.$(nars.$) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) Task(nars.Task) Truth(nars.truth.Truth) NAR(nars.NAR) BELIEF(nars.Op.BELIEF) ConjClustering(nars.op.stm.ConjClustering) Method(java.lang.reflect.Method) Term(nars.term.Term) Method(java.lang.reflect.Method) Term(nars.term.Term) Consumer(java.util.function.Consumer) ConjClustering(nars.op.stm.ConjClustering) NAR(nars.NAR) Truth(nars.truth.Truth) 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