Search in sources :

Example 21 with Task

use of nars.Task in project narchy by automenta.

the class RuleInductionTest method test1.

@Test
public void test1() {
    int dur = 1;
    int loops = 10;
    int period = 2;
    int dutyPeriod = 1;
    NAR n = NARS.tmp();
    n.termVolumeMax.set(3);
    // n.log();
    // dense proressing
    Deriver.derivers(n).forEach(d -> d.conceptsPerIteration.set(64));
    n.time.dur(dur);
    Term aConjB = $$("(a &&+" + dutyPeriod + " b)");
    float lastAConjB_exp = 0;
    PairedStatsAccumulator aConjB_exp = new PairedStatsAccumulator();
    for (int i = 0; i < loops; i++) {
        // n.clear(); //distraction clear
        n.believe("a", Tense.Present, 1, 0.9f);
        if (i > 0) {
        // TODO test that the newest tasklink inserted into concept 'a' is the, or nearly the strongest
        // n.concept("a").tasklinks().print();
        }
        n.run(dutyPeriod);
        n.believe("b", Tense.Present, 1, 0.9f);
        // delay
        n.run(period - dutyPeriod);
        long now = n.time();
        System.out.println("\n" + now);
        Truth aConjB_truth = n.belief(aConjB, now).truth(now, n.dur());
        System.out.println(aConjB_truth);
        n.conceptualize(aConjB).beliefs().print();
        float exp = aConjB_truth.expectation();
        if (!(exp >= lastAConjB_exp)) {
            // for debug
            Task tt = n.belief(aConjB, now);
        }
        // assertTrue(exp > lastAConjB_exp); //increasing
        aConjB_exp.add(now, exp);
        lastAConjB_exp = exp;
    }
    System.out.println(aConjB_exp.yStats());
    System.out.println("slope=" + aConjB_exp.leastSquaresFit().slope());
    // rising confidence
    assertTrue(aConjB_exp.leastSquaresFit().slope() > 0);
}
Also used : Task(nars.Task) Term(nars.term.Term) PairedStatsAccumulator(com.google.common.math.PairedStatsAccumulator) NAR(nars.NAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Example 22 with Task

use of nars.Task in project narchy by automenta.

the class GoalActionConcept method update.

@Override
public Stream<ITask> update(long pPrev, long pNow, int dur, NAR nar) {
    // long pStart =
    // //now;
    // start - dur / 2;
    // long pEnd =
    // //now;
    // start + dur / 2;
    // //now + dur;
    float cur = curiosity.floatValue();
    Truth goal;
    long gStart = pNow - dur / 2;
    long gEnd = pNow + dur / 2;
    goal = this.goals().truth(gStart, gEnd, nar);
    // if (goals.size() > 0)
    // System.err.println(term + " " + goal.freq() + " " + goal.evi() + " " + goal.conf());
    boolean curi;
    if (nar.random().nextFloat() < cur * (1f - (goal != null ? goal.conf() : 0))) {
        // // curiosity override
        // 
        float curiConf = // nar.confDefault(GOAL)/4; //<- to max out expectation-driven action
        nar.confMin.floatValue() * 8;
        // Math.max(goal != null ? goal.conf() : 0, //match goal conf
        // //nar.confMin.floatValue() * 2
        // nar.confDefault(GOAL)
        // );
        // nar.confMin.floatValue() * 4;
        curi = true;
        // nar.confDefault(GOAL) * CURIOSITY_CONF_FACTOR;
        // Math.max(goal != null ? goal.conf() : 0,
        // nar.confDefault(GOAL) * CURIOSITY_CONF_FACTOR);
        // nar.confMin.floatValue()*2);
        // 
        // //            float cc =
        // //                    //curiConf;
        // //                    curiConf - (goal != null ? goal.conf() : 0);
        // //            if (cc > 0) {
        // 
        // //                    ((float)Math.sin(
        // //                        hashCode() /* for phase shift */
        // //                            + now / (curiPeriod * (2 * Math.PI) * dur)) + 1f)/2f;
        // 
        goal = Truth.theDithered(nar.random().nextFloat(), c2w(curiConf), nar);
    // curiosityGoal = null;
    // curious = true;
    // 
    // //                Truth ct = $.t(f, cc);
    // //                goal = ct; //curiosity overrides goal
    // 
    // //                if (goal == null) {
    // //                    goal = ct;
    // //                } else {
    // //                    goal = Revision.revise(goal, ct);
    // //                }
    } else {
        curi = false;
    // action.set(term(), null, stamper, now, dur, nar);
    // HACK EXPERIMENT combine belief and goal
    // if (belief!=null) {
    // float hope = belief.eviEternalized();
    // if (goal == null) {
    // goal = belief.withEvi(hope); //what one images will happen maybe is what one wants
    // } else {
    // goal = Revision.revise(goal, belief.withEvi(hope), Math.abs(belief.freq()-goal.freq()), 0 );
    // }
    // }
    // fg = action.set(this, goal, stamper, now, dur, nar);
    }
    Truth belief = this.beliefs().truth(gStart, gEnd, nar);
    Truth feedback = this.motor.apply(belief, goal);
    Task feedbackBelief = feedback != null ? this.feedback.add(feedback, gStart, gEnd, dur, nar) : null;
    Task curiosityGoal = null;
    if (curi && feedbackBelief != null) {
        curiosityGoal = this.curiosity(nar, goal, // Truth.theDithered(feedbackBelief.freqMean(dur, pStart, pEnd), goal.evi(), nar),
        term, gStart, gEnd, nar.time.nextStamp());
    }
    return Stream.of(feedbackBelief, (ITask) curiosityGoal).filter(Objects::nonNull);
// return Stream.of(fb, fg).filter(Objects::nonNull);
// return Stream.of(fb).filter(Objects::nonNull);
}
Also used : SignalTask(nars.task.signal.SignalTask) ITask(nars.task.ITask) Task(nars.Task) ITask(nars.task.ITask) Objects(java.util.Objects) Truth(nars.truth.Truth)

Example 23 with Task

use of nars.Task in project narchy by automenta.

the class DynamicBeliefTable method match.

@Override
public Task match(long start, long end, Term template, NAR nar, Predicate<Task> filter) {
    Task x = super.match(start, end, template, nar, filter);
    Task y = taskDynamic(start, end, template, nar);
    if (x == null && y == null)
        return null;
    if (filter != null) {
        if (x != null && !filter.test(x))
            x = null;
        if (y != null && !filter.test(y))
            y = null;
    }
    if (y == null)
        return x;
    if (x == null)
        return y;
    if (x.equals(y))
        return x;
    int dur = nar.dur();
    // choose highest confidence
    Top<Task> top = new Top<>(t -> t.evi(start, end, dur));
    if (x.term().equals(y.term()) && !Stamp.overlapping(x, y)) {
        // try to revise
        Task xy = Revision.mergeTemporal(nar, x, y);
        if (xy != null && (filter == null || filter.test(xy)))
            top.accept(xy);
    }
    top.accept(x);
    top.accept(y);
    return top.the;
}
Also used : Task(nars.Task) Top(jcog.sort.Top)

Example 24 with Task

use of nars.Task in project narchy by automenta.

the class GoalActionAsyncConcept method feedback.

public void feedback(@Nullable Truth f, @Nullable Truth g, NAR nar) {
    long now = nar.time();
    int dur = nar.dur();
    // long start =
    // now - dur/2;
    // long end =
    // now + dur/2;
    long start = now;
    long end = now + dur;
    Task fg;
    if (g != null) {
        // //fg = feedGoal.task(term, g, goalTime-dur, goalTime, nar.time.nextStamp()); //allow the feedback goal (Ex: curiosity) to override, otherwise use the current goal
        // fg = feedGoal.set(this, g, stamper, goalTime, dur, nar);
        // fg = GoalActionConcept.curiosity(nar, g, term, curiosityStamp);
        fg = null;
    } else
        fg = null;
    SignalTask fb = ((ScalarBeliefTable) beliefs()).add(f, start, end, dur, nar);
    in.input(fg, fb);
    PredictionFeedback.feedbackSignal(fb, /* in case stretched */
    beliefs, nar);
}
Also used : SignalTask(nars.task.signal.SignalTask) ITask(nars.task.ITask) Task(nars.Task) SignalTask(nars.task.signal.SignalTask) ScalarBeliefTable(nars.concept.dynamic.ScalarBeliefTable)

Example 25 with Task

use of nars.Task in project narchy by automenta.

the class Derivation method proto.

/**
 * setup the derivation for the ProtoDerivation stage
 *
 * must call reset() immediately before or after calling this.
 *
 * TODO move some of these to a superclass method which sets the variables in its scope
 */
public boolean proto(Task _task, final Task _belief, Term _beliefTerm) {
    final Task task = this.task = anon.put(this._task = _task, dur);
    if (task == null)
        throw new NullPointerException(_task + " could not be anonymized: " + _task.term().anon() + " , " + anon.put(this._task = _task, dur));
    final Term taskTerm = this.taskTerm = task.term();
    final Term beliefTerm;
    if (_belief != null) {
        if ((this.belief = anon.put(this._belief = _belief, dur)) == null)
            throw new NullPointerException(_belief + " could not be anonymized");
        beliefTerm = this.beliefTerm = this.belief.term();
    } else {
        this.belief = null;
        if ((beliefTerm = this.beliefTerm = anon.put(this._beliefTerm = _beliefTerm)) == null)
            throw new NullPointerException(_belief + " could not be anonymized");
    }
    this._taskStruct = taskTerm.structure();
    this._taskOp = taskTerm.op().id;
    this._beliefStruct = beliefTerm.structure();
    this._beliefOp = beliefTerm.op().id;
    return setTruth();
}
Also used : NALTask(nars.task.NALTask) Task(nars.Task) PrediTerm(nars.term.pred.PrediTerm) Term(nars.term.Term)

Aggregations

Task (nars.Task)50 Term (nars.term.Term)15 NALTask (nars.task.NALTask)13 Truth (nars.truth.Truth)12 Nullable (org.jetbrains.annotations.Nullable)11 SignalTask (nars.task.signal.SignalTask)9 Test (org.junit.jupiter.api.Test)8 ITask (nars.task.ITask)6 FasterList (jcog.list.FasterList)5 NAR (nars.NAR)5 PreciseTruth (nars.truth.PreciseTruth)4 Util (jcog.Util)3 Top (jcog.sort.Top)3 Op (nars.Op)3 Param (nars.Param)3 TaskRegion (nars.task.util.TaskRegion)3 List (java.util.List)2 Map (java.util.Map)2 Random (java.util.Random)2 VLink (jcog.pri.VLink)2