Search in sources :

Example 1 with Truth

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

the class GoalActionAsyncConcept 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;
    long gStart = pNow - dur / 2;
    long gEnd = pNow + dur / 2;
    Truth goal = this.goals().truth(gStart, gEnd, nar);
    // //HACK EXPERIMENT combine belief and goal
    // int shift =
    // 0;
    // //+dur/2;
    // Truth belief = this.beliefs().truth(pStart+shift, pEnd+shift, nar);
    // 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 );
    // }
    // 
    // }
    this.motor.accept(this, goal);
    return null;
}
Also used : Truth(nars.truth.Truth)

Example 2 with Truth

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

the class DynTruth method eval.

/**
 * TODO make Task truth dithering optional
 */
public Truthed eval(@NotNull Term superterm, @Deprecated BiFunction<DynTruth, NAR, Truth> truthModel, boolean taskOrJustTruth, boolean beliefOrGoal, float freqRes, float confRes, float eviMin, NAR nar) {
    Truth t = truthModel.apply(this, nar);
    if (t == null)
        return null;
    float evi = t.evi();
    if (evi < eviMin)
        return null;
    // this may have already been done in truth calculation
    // //TODO compute max valid overlap to terminate the zip early
    ObjectFloatPair<long[]> ss = Stamp.zip((List) this, Param.STAMP_CAPACITY);
    // evi = evi * Param.overlapFactor(ss.getTwo());
    // if (evi < eviMin)
    // return null;
    float freq = t.freq();
    float f;
    long start, end;
    if (taskOrJustTruth) {
        if (size() > 1) {
            if (superterm.op() == CONJ) {
                long min = TIMELESS;
                long maxRange = 0;
                boolean eternals = false;
                for (int i = 0, thisSize = size(); i < thisSize; i++) {
                    LongInterval ii = get(i);
                    long iis = ii.start();
                    if (iis != ETERNAL) {
                        min = Math.min(min, iis);
                        maxRange = Math.max(maxRange, ii.end() - iis);
                    } else {
                        eternals = true;
                    }
                }
                if (eternals && min == TIMELESS) {
                    start = end = ETERNAL;
                } else {
                    assert (min != TIMELESS);
                    // if (min == Long.MAX_VALUE)
                    // start = end = ETERNAL; //all eternal
                    // else {
                    start = min;
                    end = (min + maxRange);
                }
            // }
            } else {
                // dilute the evidence in proportion to temporal sparseness for non-temporal results
                EviDensity se = new EviDensity(this);
                evi *= se.factor();
                if (evi != evi || evi < eviMin)
                    return null;
                start = se.unionStart;
                end = se.unionEnd;
            }
        } else {
            // only one task
            LongInterval only = get(0);
            start = only.start();
            end = only.end();
        }
        if (superterm.op() == NEG) {
            // unneg if constructing a task, but dont if just returning the truth
            superterm = superterm.unneg();
            f = 1f - freq;
        } else {
            f = freq;
        }
    } else {
        // not used
        start = end = XTERNAL;
        f = freq;
    }
    if (!taskOrJustTruth)
        return Truth.theDithered(f, freqRes, evi, confRes, w2cSafe(eviMin));
    // undithered until final step for max precision
    PreciseTruth tr = new PreciseTruth(f, evi, false);
    // then if the term is valid, see if it is valid for a task
    Term content = truthModel instanceof DynamicTruthModel ? ((DynamicTruthModel) truthModel).construct(superterm, this) : superterm;
    @Nullable ObjectBooleanPair<Term> r = Task.tryContent(// otherwise consider the superterm argument as the task content
    content, beliefOrGoal ? BELIEF : GOAL, !Param.DEBUG_EXTRA);
    if (r == null)
        return null;
    NALTask dyn = new DynamicTruthTask(r.getOne(), beliefOrGoal, tr.negIf(r.getTwo()).dither(freqRes, confRes, w2cSafe(eviMin)), nar, start, end, ss.getOne());
    // if (ss.getTwo() > 0) dyn.setCyclic(true);
    dyn.cause = cause();
    dyn.priSet(pri());
    if (Param.DEBUG_EXTRA)
        dyn.log("Dynamic");
    return dyn;
}
Also used : EviDensity(nars.task.EviDensity) Term(nars.term.Term) PreciseTruth(nars.truth.PreciseTruth) LongInterval(jcog.math.LongInterval) NALTask(nars.task.NALTask) Truth(nars.truth.Truth) PreciseTruth(nars.truth.PreciseTruth) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Truth

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

the class ConjClustering method conjoinCentroid.

// /**
// * produces a parallel conjunction term consisting of all the task's terms
// */
// public Stream<List<Task>> chunk(Stream<Task> input, int maxComponentsPerTerm, int maxVolume) {
// final int[] group = {0};
// final int[] subterms = {0};
// final int[] currentVolume = {0};
// final float[] currentConf = {1};
// return input.filter(x -> !x.isDeleted())
// .collect(Collectors.groupingBy(x -> {
// 
// int v = x.volume();
// float c = x.conf();
// 
// if ((subterms[0] >= maxComponentsPerTerm) || (currentVolume[0] + v >= maxVolume) || (currentConf[0] * c < confMin)) {
// //next group
// group[0]++;
// subterms[0] = 1;
// currentVolume[0] = v;
// currentConf[0] = c;
// } else {
// subterms[0]++;
// currentVolume[0] += v;
// currentConf[0] *= c;
// }
// 
// return group[0];
// }))
// .entrySet().stream()
// .map(c -> {
// List<Task> v = c.getValue();
// return c.getKey() >= 0 && //only batches of >1
// v.size() > 1 ? v : null;  //ignore the -1 discard group
// })
// .filter(Objects::nonNull);
// 
// }
// static final BiFunction<Task, Task, Task> termPointMerger = (prevZ, newZ) -> ((prevZ == null) || (newZ.conf() >= prevZ.conf())) ?
// newZ : prevZ;
private List<Task> conjoinCentroid(Stream<VLink<Task>> group, Pair<NAR, List<Task>> narAndTarget) {
    NAR nar = narAndTarget.getOne();
    // get only the maximum confidence task for each term at its given starting time
    // in.input(
    // chunk(group.filter(Objects::nonNull).takeWhile(kontinue)
    // .map(x -> x.id), maxConjSize, volMax).takeWhile(kontinue).map(tasks -> {
    Iterator<VLink<Task>> gg = group.filter(x -> x != null && !x.isDeleted()).iterator();
    // Iterators.peekingIterator();
    Map<LongObjectPair<Term>, Task> vv = new HashMap<>();
    FasterList<Task> actualTasks = new FasterList();
    int centroidGen = 0;
    List<Task> gen = narAndTarget.getTwo();
    main: while (gg.hasNext() && centroidGen < taskLimitPerCentroid) {
        vv.clear();
        actualTasks.clear();
        long end = Long.MIN_VALUE;
        long start = Long.MAX_VALUE;
        int dur = nar.dur();
        float freq = 1f;
        float conf = 1f;
        float priMax = Float.NEGATIVE_INFINITY, priMin = Float.POSITIVE_INFINITY;
        int vol = 0;
        int maxVolume = 0;
        do {
            if (!gg.hasNext())
                break;
            Task t = gg.next().id;
            // gg.peek().id;
            Term xt = t.term();
            long zs = Tense.dither(t.start(), ditherTime);
            long ze = Tense.dither(t.end(), ditherTime);
            // assert (end >= start);
            Truth tx = t.truth();
            Term xtn = xt.neg();
            if (tx.isNegative()) {
                xt = xtn;
            }
            int xtv = xt.volume();
            maxVolume = Math.max(maxVolume, xt.volume());
            if (vol + xtv + 1 >= volMax || conf * tx.conf() < confMin) {
                // cant go any further with this task
                continue;
            }
            boolean involved = false;
            LongObjectPair<Term> ps = pair(zs, xt);
            Term xtNeg = xt.neg();
            if (!vv.containsKey(pair(zs, xtNeg)) && null == vv.putIfAbsent(ps, t)) {
                vol += xtv;
                if (start > zs)
                    start = zs;
                if (end < zs)
                    end = zs;
                involved = true;
            }
            if (ze - zs >= dur) {
                // endpoint
                if (vol + xtv + 1 < volMax) {
                    LongObjectPair<Term> pe = pair(ze, xt);
                    if (!vv.containsKey(pair(ze, xtNeg)) && null == vv.putIfAbsent(pe, t)) {
                        // end point, if different from start
                        vol += xtv;
                        if (end < ze)
                            end = ze;
                        involved = true;
                    }
                }
            }
            if (involved) {
                actualTasks.add(t);
                conf *= tx.conf();
                float tf = tx.freq();
                // since it will appear as a negated subterm
                freq *= tx.isNegative() ? (1f - tf) : tf;
                float p = t.priElseZero();
                if (p < priMin)
                    priMin = p;
                if (p > priMax)
                    priMax = p;
            }
        } while (vol < volMax - 1 && conf > confMin);
        int vs = actualTasks.size();
        if (vs < 2)
            continue;
        // the tasks which are actually involved
        Task[] uu = actualTasks.toArrayRecycled(Task[]::new);
        // TODO discount based on evidential overlap? needs N-way overlapFraction function
        ObjectFloatPair<long[]> evidence = Stamp.zip(actualTasks, Param.STAMP_CAPACITY);
        float overlap = evidence.getTwo();
        float e = c2w(conf) * Param.overlapFactor(overlap);
        if (e > 0) {
            final Truth t = Truth.theDithered(freq, e, nar);
            if (t != null) {
                Term cj = Conj.conj(vv.keySet());
                if (cj != null) {
                    cj = cj.normalize();
                    if (Math.abs(cj.dtRange() - (end - start)) < ditherTime) {
                        // test if merge collapse occurred and occurrence time needs recalculated
                        ObjectBooleanPair<Term> cp = Task.tryContent(cj, punc, true);
                        if (cp != null) {
                            // TODO use a truth calculated specific to this fixed-size batch, not all the tasks combined
                            NALTask m = new STMClusterTask(cp, t, start, start, evidence.getOne(), punc, now);
                            // if (evidence.getTwo() > 0) m.setCyclic(true);
                            m.cause = Cause.sample(Param.causeCapacity.intValue(), uu);
                            float p = // priMax;
                            priMin;
                            // (priMin + priMax) / 2f;
                            // complexity deduction
                            // how much more complex the conjunction is than the most complex of its ingredients
                            int v = cp.getOne().volume();
                            float cmplFactor = ((float) v) / (v + maxVolume);
                            m.priSet(Priority.fund(p * cmplFactor, true, uu));
                            gen.add(m);
                            centroidGen++;
                        }
                    } else {
                    // System.out.println("merge collapse, recalcu");
                    }
                }
            }
        }
    }
    return gen.isEmpty() ? null : gen;
}
Also used : Tense(nars.time.Tense) Causable(nars.exe.Causable) ObjectFloatPair(org.eclipse.collections.api.tuple.primitive.ObjectFloatPair) HashMap(java.util.HashMap) VLink(jcog.pri.VLink) InvalidTaskException(nars.task.util.InvalidTaskException) Truth(nars.truth.Truth) Conj(nars.term.compound.util.Conj) Map(java.util.Map) PrimitiveTuples.pair(org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples.pair) Tuples(org.eclipse.collections.impl.tuple.Tuples) Pair(org.eclipse.collections.api.tuple.Pair) LongObjectPair(org.eclipse.collections.api.tuple.primitive.LongObjectPair) TruthFunctions.c2w(nars.truth.TruthFunctions.c2w) Term(nars.term.Term) Iterator(java.util.Iterator) ObjectBooleanPair(org.eclipse.collections.api.tuple.primitive.ObjectBooleanPair) Predicate(java.util.function.Predicate) FasterList(jcog.list.FasterList) Stamp(nars.truth.Stamp) Util(jcog.Util) ITask(nars.task.ITask) NALTask(nars.task.NALTask) Priority(jcog.pri.Priority) Param(nars.Param) Nullable(org.jetbrains.annotations.Nullable) Task(nars.Task) List(java.util.List) Stream(java.util.stream.Stream) NAR(nars.NAR) BagClustering(nars.bag.BagClustering) Cause(nars.control.Cause) CauseChannel(nars.control.CauseChannel) ITask(nars.task.ITask) NALTask(nars.task.NALTask) Task(nars.Task) HashMap(java.util.HashMap) FasterList(jcog.list.FasterList) Term(nars.term.Term) VLink(jcog.pri.VLink) LongObjectPair(org.eclipse.collections.api.tuple.primitive.LongObjectPair) ObjectBooleanPair(org.eclipse.collections.api.tuple.primitive.ObjectBooleanPair) ObjectFloatPair(org.eclipse.collections.api.tuple.primitive.ObjectFloatPair) NALTask(nars.task.NALTask) NAR(nars.NAR) Truth(nars.truth.Truth)

Example 4 with Truth

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

the class AutoConceptualizer method update.

protected void update(NAR n) {
    long now = n.time();
    float[] x = this.x;
    int inputs = in.size();
    for (int i = 0, inSize = inputs; i < inSize; i++) {
        Concept xx = in.get(i);
        Truth t = ((BeliefTable) xx.table(beliefOrGoal ? BELIEF : GOAL)).truth(now, n);
        float f;
        if (t == null) {
            f = 0.5f;
        // 0.5f + (learningRate * 2) * n.random().nextFloat() - learningRate;
        // n.random()
        } else {
            f = t.freq();
        }
        x[i] = f;
    }
    // System.out.println(n4(x));
    float err = ae.put(x, learningRate, noiseLevel, 0, true);
    // System.out.println("err=" + n4(err/inputs) + ": \t" + n4(ae.y));
    // decompile/unfactor the outputs
    // if err < thresh
    int outputs = ae.outputs();
    float[] b = new float[outputs];
    float thresh = n.freqResolution.floatValue();
    int[] order = new int[inputs];
    for (int i = 0; i < outputs; i++) {
        // basis vector for each output
        b[i] = 1;
        float[] a = ae.decode(b, true);
        // System.out.println("\tfeature " + i + "=" + n4(a));
        Term feature = conj(order, a, /* threshold, etc */
        5, /*a.length/2*/
        thresh);
        if (feature != null) {
            // System.out.println("\t  " + feature);
            onFeature(feature);
        }
        // clear
        b[i] = 0;
    }
}
Also used : Concept(nars.concept.Concept) BeliefTable(nars.table.BeliefTable) Term(nars.term.Term) Truth(nars.truth.Truth)

Example 5 with Truth

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

the class Implier method taskify.

private void taskify(Map<LongObjectPair<Term>, TruthAccumulator> truths, byte punc, List<Task> gen) {
    float freqRes = nar.freqResolution.floatValue();
    float confRes = nar.confResolution.floatValue();
    float strength = this.strength.floatValue();
    float confMin = nar.confMin.floatValue();
    truths.forEach((tw, a) -> {
        Term t = tw.getTwo();
        long w = tw.getOne();
        long wEnd = w + dur;
        @Nullable Truth uu = a.commitSum().dither(freqRes, confRes, confMin, strength);
        long stamp = nar.time.nextStamp();
        NALTask y;
        if (uu != null && uu.conf() >= confMin) {
            y = new SignalTask(t, punc, uu, now, w, wEnd, stamp);
        } else {
            y = new SignalTask(t, punc == GOAL ? QUEST : QUESTION, null, now, w, wEnd, stamp);
        }
        y.pri(nar.priDefault(y.punc));
        // if (Param.DEBUG)
        // y.log("")
        // System.out.println("\t" + y);
        gen.add(y);
    });
}
Also used : NALTask(nars.task.NALTask) SignalTask(nars.task.signal.SignalTask) Term(nars.term.Term) Nullable(org.jetbrains.annotations.Nullable) 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