Search in sources :

Example 31 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class DynamicBeliefTable method truth.

@Override
@Nullable
public final Truth truth(long start, long end, NAR nar) {
    Truth d = truthDynamic(start, end, nar);
    Truth e = truthStored(start, end, nar);
    if (e == null || d == e)
        return d;
    if (d == null)
        return e;
    // <- this is conservative disallowing any overlap
    return Truth.maxConf(d, e);
}
Also used : Truth(nars.truth.Truth) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class NAct method actionBipolarFrequencyDifferential.

default GoalActionAsyncConcept[] actionBipolarFrequencyDifferential(@NotNull Term s, boolean fair, boolean latchPreviousIfUndecided, @NotNull FloatToFloatFunction update) {
    Term pt = // $.p(s, PLUS);
    $.inh(s, PLUS);
    // $.prop(s,PLUS);
    // $.p(s, ZeroProduct);
    // $.p(s,$.the("\"+\""));
    Term nt = // $.p(s, NEG);
    $.inh(s, NEG);
    // $.prop(s, NEG);
    // $.p(ZeroProduct, s);
    // $.p(s,$.the("\"-\""));
    final float[] g = new float[2];
    final float[] c = new float[2];
    final long[] lastUpdate = { ETERNAL };
    final float[] lastX = { 0 };
    // hack
    GoalActionAsyncConcept[] CC = new GoalActionAsyncConcept[2];
    @NotNull BiConsumer<GoalActionAsyncConcept, Truth> u = (action, gg) -> {
        NAR n = nar();
        long now = n.time();
        if (now != lastUpdate[0]) {
            lastUpdate[0] = now;
            // reset
            CC[0] = CC[1] = null;
        }
        // float freqEps = n.freqResolution.floatValue();
        float confMin = n.confMin.floatValue();
        // float eviMin = c2wSafe(confMin);
        float feedbackConf = // fairly shared to sum to default
        w2c(c2w(n.confDefault(BELIEF)) / 2f);
        // n.confDefault(BELIEF);
        // n.confDefault(GOAL);
        // confMin * ...;
        boolean p = action.term().equals(pt);
        int ip = p ? 0 : 1;
        CC[ip] = action;
        g[ip] = gg != null ? // gg.freq()
        gg.expectation() : 0f;
        // 0.5f;
        c[ip] = gg != null ? // gg.evi()
        gg.conf() : 0f;
        // -1..+1
        float x;
        boolean curious;
        if (CC[0] != null && CC[1] != null) /* both ready */
        {
            float cMax = Math.max(c[0], c[1]);
            float cMin = Math.min(c[0], c[1]);
            float coherence = cMin / cMax;
            Random rng = n.random();
            float cur = curiosity().floatValue();
            if (cur > 0 && rng.nextFloat() <= cur) {
                x = (rng.nextFloat() - 0.5f) * 2f;
                // float curiEvi =
                // //c2w(n.confDefault(BELIEF));
                // //eviMin*2;
                // Math.max(c2wSafe(w2cSafe(eviMin)*2), Util.mean(c[0], c[1])); //match desire conf, min=2*minConf
                c[0] = c[1] = feedbackConf;
                coherence = 1f;
                curious = true;
            } else {
                curious = false;
                if (cMax < confMin) {
                    if (latchPreviousIfUndecided) {
                        x = lastX[0];
                    } else {
                        x = 0;
                    }
                } else {
                    // //expectation
                    // float g0 = g[0]-0.5f;
                    // float g1 = g[1]-0.5f;
                    // df = 2f * ((g0) - (g1));
                    // // /Math.max(Math.abs(g0), Math.abs(g1));
                    // frequency -======================
                    // A. subtraction
                    // subtract
                    x = ((g[0] - g[1]));
                    // experimental: lessen by a factor of how equally confident each goal is
                    if (fair) {
                        // fully fair
                        x *= coherence;
                    // x *= Math.sqrt(coherence); //less sharp than linear
                    // semi-fair
                    // df *= 0.5f + 0.5f * (eMin / eMax); //reduction by at most half
                    }
                // df *= 1f - Math.abs(e[0] - e[1]) / eMax;
                // df *= Util.sqr(eMin / eMax); //more cautious
                // df *= Math.min(w2cSafe(e[0]), w2cSafe(e[1])) / w2cSafe(eMax);
                }
            }
            x = Util.clamp(x, -1f, +1f);
            lastX[0] = x;
            // -1..+1
            float y = update.valueOf(x);
            // System.out.println(x + " " + y);
            // w2c(Math.abs(y) * c2w(restConf));
            PreciseTruth Nb, Ng, Pb, Pg;
            if (y == y) {
                // y: (-1..+1)
                float yp, yn;
                if (Math.abs(y) >= n.freqResolution.floatValue()) {
                    yp = 0.5f + y / 2f;
                    yn = 1f - yp;
                } else {
                    yp = yn = 0.5f;
                }
                // float yp = 0.5f + y/2f;
                // float yn = 1f - yp;
                float pbf = yp;
                float nbf = yn;
                Pb = $.t(pbf, feedbackConf);
                Nb = $.t(nbf, feedbackConf);
                // float goalEvi =
                // eviMin;
                // //max(eviMin, max(e[0], e[1]));
                // Pg = curious || e[0] == 0 ? new PreciseTruth(yp, goalEvi, false) : null;
                // Ng = curious || e[1] == 0 ? new PreciseTruth(yn, goalEvi, false) : null;
                // float confBase = confMin*4; //~ alpha, learning rate
                // float fThresh = Float.MIN_NORMAL;
                // float yp = y > +fThresh ? Util.lerp(+y, confBase, feedbackConf) : confBase;
                // float yn = y < -fThresh ? Util.lerp(-y, confBase, feedbackConf) : confBase;
                // Pb = $.t(y > +fThresh ? 1 : 0, y > +fThresh ? yp : feedbackConf - yp);
                // Nb = $.t(y < -fThresh ? 1 : 0, y < -fThresh ? yn : feedbackConf - yn);
                // //Pg = curious || e[0] == 0 ? new PreciseTruth(1, Util.lerp(+y, confMin2, feedbackConf)) : null;
                // Pg = null;
                // //Ng = curious || e[1] == 0 ? new PreciseTruth(1, Util.lerp(-y, confMin2, feedbackConf)) : null;
                // Ng = null;
                // float fThresh = nar().freqResolution.floatValue();
                // int sign = (y > fThresh ? +1 : (y < -fThresh ? -1 : 0));
                // 
                // float feedConf =
                // w2cSafe(c2wSafe(goalConf)/2f); //half/half
                // //goalConf;
                // //Math.max(confMin, goalConf * coherence);
                // switch (sign) {
                // case +1:
                // //Pb = $.t(1f, Util.lerp(+y, confBase, feedbackConf));
                // Pb = $.t(y/2f + 0.5f, feedConf);
                // Nb =
                // //null;
                // $.t(0, feedConf);
                // break;
                // case -1:
                // Pb =
                // //null;
                // $.t(0, feedConf);
                // 
                // Nb = $.t(-y/2f + 0.5f, feedConf);
                // break;
                // case 0:
                // //Pb = Nb = null; //no signal
                // Pb = Nb = $.t(0, feedConf);
                // //Math.max(confMin, feedConf);
                // //w2cSafe(c2wSafe(feedConf)/2f))); //zero
                // break;
                // default:
                // throw new UnsupportedOperationException();
                // }
                Pg = null;
                Ng = null;
            // if (curious) {
            // e[0] = e[1] = 0; //reset to get full evidence override
            // }
            // float g0 = eviMax - e[0];
            // Pg = g0 >= eviMin ? new PreciseTruth(yp, g0, false) : null;
            // float g1 = eviMax - e[1];
            // Ng = g1 >= eviMin ? new PreciseTruth(yn, g1, false) : null;
            } else {
                Pb = Nb = Pg = Ng = null;
            }
            // System.out.println(Pb + "," + Nb + " <- " + g[0] + ";" + c[0] + ", " + g[1] + ';' + c[1]);
            CC[0].feedback(Pb, Pg, n);
            CC[1].feedback(Nb, Ng, n);
        }
    };
    CauseChannel<ITask> cause = nar().newCauseChannel(s);
    GoalActionAsyncConcept p = new GoalActionAsyncConcept(pt, this, cause, u);
    GoalActionAsyncConcept n = new GoalActionAsyncConcept(nt, this, cause, u);
    addAction(p);
    addAction(n);
    nar().believe($.inh(s, SECTe.the(PLUS, NEG)).neg(), Tense.Eternal);
    CC[0] = p;
    CC[1] = n;
    return CC;
}
Also used : Tense(nars.time.Tense) IntConsumer(java.util.function.IntConsumer) Random(java.util.Random) FloatToFloatFunction(org.eclipse.collections.api.block.function.primitive.FloatToFloatFunction) ETERNAL(nars.time.Tense.ETERNAL) IntPredicate(java.util.function.IntPredicate) GoalActionConcept(nars.concept.action.GoalActionConcept) Truth(nars.truth.Truth) ActionConcept(nars.concept.action.ActionConcept) BeliefActionConcept(nars.concept.action.BeliefActionConcept) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) FloatConsumer(jcog.util.FloatConsumer) TruthFunctions.c2w(nars.truth.TruthFunctions.c2w) TruthFunctions.w2c(nars.truth.TruthFunctions.w2c) Term(nars.term.Term) BooleanProcedure(org.eclipse.collections.api.block.procedure.primitive.BooleanProcedure) Util.unitize(jcog.Util.unitize) FloatRange(jcog.math.FloatRange) PreciseTruth(nars.truth.PreciseTruth) SECTe(nars.Op.SECTe) Util(jcog.Util) ITask(nars.task.ITask) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) GoalActionAsyncConcept(nars.concept.action.GoalActionAsyncConcept) BELIEF(nars.Op.BELIEF) CauseChannel(nars.control.CauseChannel) NotNull(org.jetbrains.annotations.NotNull) ITask(nars.task.ITask) Term(nars.term.Term) GoalActionAsyncConcept(nars.concept.action.GoalActionAsyncConcept) NotNull(org.jetbrains.annotations.NotNull) PreciseTruth(nars.truth.PreciseTruth) Random(java.util.Random) Truth(nars.truth.Truth) PreciseTruth(nars.truth.PreciseTruth)

Example 33 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class NAgent method dexterity.

/**
 * average confidence of actions
 * see: http://www.dictionary.com/browse/dexterity?s=t
 */
public float dexterity(long start, long end) {
    int n = actions.size();
    if (n == 0)
        return 0;
    final float[] m = { 0 };
    actions.keySet().forEach(a -> {
        Truth g = nar.goalTruth(a, start, end);
        float c;
        if (g != null) {
            // c = g.evi();
            c = g.conf();
        } else {
            c = 0;
        }
        m[0] += c;
    });
    // return m[0] > 0 ? w2c(m[0] / n /* avg */) : 0;
    return m[0] > 0 ? m[0] / n : /* avg */
    0;
}
Also used : Truth(nars.truth.Truth)

Example 34 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class NAgent method run.

@Override
public void run() {
    if (!enabled.get())
        return;
    this.last = this.now;
    this.now = nar.time();
    if (now <= last)
        return;
    // stretched perceptual duration to the NAgent's effective framerate
    int dur = Math.max(nar.dur(), (int) (now - last));
    reward = act();
    happy.update(last, now, dur, nar);
    FloatFloatToObjectFunction<Truth> truther = (prev, next) -> $.t(next, nar.confDefault(BELIEF));
    sensors.forEach((key, value) -> value.input(key.update(last, now, truther, dur, nar)));
    always(motivation.floatValue());
    // HACK TODO compile this to re-used array on init like before
    Map.Entry<ActionConcept, CauseChannel<ITask>>[] aa = actions.entrySet().toArray(new Map.Entry[actions.size()]);
    // fair chance of ordering to all motors
    ArrayUtils.shuffle(aa, random());
    for (Map.Entry<ActionConcept, CauseChannel<ITask>> ac : aa) {
        Stream<ITask> s = ac.getKey().update(last, now, dur, NAgent.this.nar);
        if (s != null)
            ac.getValue().input(s);
    }
    Truth happynowT = nar.beliefTruth(happy, last, now);
    float happynow = happynowT != null ? (happynowT.freq() - 0.5f) * 2f : 0;
    nar.emotion.happy(motivation.floatValue() * dexterity(last, now) * happynow);
    if (trace)
        logger.info(summary());
}
Also used : Iterables(com.google.common.collect.Iterables) DurService(nars.control.DurService) java.util(java.util) Bitmap2DSensor(nars.util.signal.Bitmap2DSensor) LoggerFactory(org.slf4j.LoggerFactory) Scalar(nars.concept.scalar.Scalar) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ETERNAL(nars.time.Tense.ETERNAL) Supplier(java.util.function.Supplier) Variable(nars.term.var.Variable) Truth(nars.truth.Truth) Tuples.pair(org.eclipse.collections.impl.tuple.Tuples.pair) Op(nars.Op) ActionConcept(nars.concept.action.ActionConcept) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) On(jcog.event.On) Activate(nars.control.Activate) FloatFloatToObjectFunction(org.eclipse.collections.api.block.function.primitive.FloatFloatToObjectFunction) Concept(nars.concept.Concept) Term(nars.term.Term) Util.compose(jcog.Util.compose) Loop(jcog.exe.Loop) Logger(org.slf4j.Logger) Texts.n2(jcog.Texts.n2) Predicate(java.util.function.Predicate) FasterList(jcog.list.FasterList) Stamp(nars.truth.Stamp) NARService(nars.control.NARService) ITask(nars.task.ITask) NALTask(nars.task.NALTask) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) TODO(jcog.TODO) FilteredScalar(nars.concept.scalar.FilteredScalar) Stream(java.util.stream.Stream) Atomic(nars.term.atom.Atomic) jcog.math(jcog.math) Termed(nars.term.Termed) CauseChannel(nars.control.CauseChannel) NotNull(org.jetbrains.annotations.NotNull) DigitizedScalar(nars.concept.scalar.DigitizedScalar) ITask(nars.task.ITask) CauseChannel(nars.control.CauseChannel) Truth(nars.truth.Truth) ActionConcept(nars.concept.action.ActionConcept)

Example 35 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class Narsese method decodeTask.

/**
 * returns null if the Task is invalid (ex: invalid term)
 */
static Task decodeTask(NAR m, Object[] x) {
    if (x.length == 1 && x[0] instanceof Task) {
        return (Task) x[0];
    }
    Term content = ((Term) x[1]).normalize();
    /*if (!(content instanceof Compound)) {
                throw new NarseseException("Task term unnormalizable: " + contentRaw);
                //return Command.task($.func("log", content));
            } else */
    Object px = x[2];
    byte punct = px instanceof Byte ? (Byte) x[2] : (byte) (((Character) x[2]).charValue());
    Truth t = (Truth) x[3];
    if (t != null && !Float.isFinite(t.conf()))
        t = $.t(t.freq(), m.confDefault(punct));
    if (t == null && (punct == BELIEF || punct == GOAL)) {
        t = $.t(1, m.confDefault(punct));
    }
    Task y = Task.tryTask(content, punct, t, (C, tr) -> {
        // TODO construct directly and remove TaskBuilder
        TaskBuilder ttt = new TaskBuilder(C, punct, tr).time(// creation time
        m.time(), Tense.getRelativeOccurrence((Tense) x[4], m));
        if (x[0] == null)
            /* do not set, Memory will apply defaults */
            ttt.priSet(m.priDefault(punct));
        else
            ttt.priSet((Float) x[0]);
        return ttt.apply(m).log(NARSESE_TASK_TAG);
    });
    if (y == null) {
        throw new InvalidTaskException(content, "input: " + Arrays.toString(x));
    }
    return y;
}
Also used : TaskBuilder(nars.task.TaskBuilder) Tense(nars.time.Tense) InvalidTaskException(nars.task.util.InvalidTaskException) Term(nars.term.Term) Truth(nars.truth.Truth)

Aggregations

Truth (nars.truth.Truth)65 Term (nars.term.Term)31 Test (org.junit.jupiter.api.Test)22 Nullable (org.jetbrains.annotations.Nullable)20 NAR (nars.NAR)18 Task (nars.Task)14 BeliefTable (nars.table.BeliefTable)13 Concept (nars.concept.Concept)9 NALTask (nars.task.NALTask)9 TaskConcept (nars.concept.TaskConcept)7 PreciseTruth (nars.truth.PreciseTruth)7 NotNull (org.jetbrains.annotations.NotNull)7 FasterList (jcog.list.FasterList)6 ITask (nars.task.ITask)6 Tense (nars.time.Tense)6 Util (jcog.Util)5 BELIEF (nars.Op.BELIEF)5 Consumer (java.util.function.Consumer)4 Predicate (java.util.function.Predicate)4 Param (nars.Param)4