Search in sources :

Example 6 with NALTask

use of nars.task.NALTask in project narchy by automenta.

the class RTreeBeliefTable method eternalize.

private void eternalize(Task x, Consumer<Tasked> added, NAR nar) {
    if ((x instanceof SignalTask)) {
        // ignore for now
        return;
    }
    float xPri = x.pri();
    if (xPri != xPri)
        // deleted already somehow
        return;
    float xc = x.conf();
    float e = x.eviEternalized((1 / xc) * size());
    float c = w2cSafe(e);
    if (c >= nar.confMin.floatValue()) {
        added.accept(() -> {
            // if (x.op().temporal) { //==IMPL /*x.op().statement */ /*&& !x.term().isTemporal()*/) {
            // //experimental eternalize
            Task eternalized = Task.clone(x, x.term(), Truth.theDithered(x.freq(), e, nar), x.punc(), x.creation(), ETERNAL, ETERNAL);
            if (eternalized != null) {
                eternalized.pri(xPri * c / xc);
                if (Param.DEBUG)
                    eternalized.log("Eternalized Temporal");
                nar.input(eternalized);
                if (!(eternalized.isDeleted()))
                    ((NALTask) x).delete(eternalized);
            }
            return null;
        });
    }
}
Also used : SignalTask(nars.task.signal.SignalTask) NALTask(nars.task.NALTask) Task(nars.Task) SignalTask(nars.task.signal.SignalTask)

Example 7 with NALTask

use of nars.task.NALTask in project narchy by automenta.

the class NAR method input.

public Task input(float pri, Term term, byte punc, long start, long end, float freq, float conf) throws InvalidTaskException {
    PreciseTruth tr = Truth.theDithered(freq, c2w(conf), this);
    @Nullable Task z = Task.tryTask(term, punc, tr, (c, truth) -> {
        Task y = new NALTask(c, punc, truth, time(), start, end, new long[] { time.nextStamp() });
        y.priSet(pri);
        return y;
    }, false);
    input(z);
    return z;
}
Also used : PreciseTruth(nars.truth.PreciseTruth) ITask(nars.task.ITask) NALTask(nars.task.NALTask) NALTask(nars.task.NALTask) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with NALTask

use of nars.task.NALTask in project narchy by automenta.

the class NAR method newCauseChannel.

// /**
// * bins a range of values into N equal levels
// */
// public static class ChannelRange<X extends Priority> {
// public final float min, max;
// public final Cause[] levels;
// transient private final float range; //cache
// 
// public ChannelRange(String id, int levels, Function<Object, CauseChannel<X>> src, float min, float max) {
// this.min = min;
// this.max = max;
// assert (max > min);
// this.range = max - min;
// assert (range > Prioritized.EPSILON);
// this.levels = Util.map(0, levels, (l) -> src.apply(id + l), Cause[]::new);
// }
// 
// public Cause get(float value) {
// return levels[Util.bin(Util.unitize((value - min) / range), levels.length)];
// }
// }
// 
// //    public final ImplicitTaskCauses taskCauses = new ImplicitTaskCauses(this);
// //
// //    static class ImplicitTaskCauses {
// //
// //        public final Cause causeBelief, causeGoal, causeQuestion, causeQuest;
// //        public final Cause causePast, causePresent, causeFuture, causeEternal;
// //        //public final ChannelRange causeConf, causeFreq;
// //        public final NAR nar;
// //
// //        ImplicitTaskCauses(NAR nar) {
// //            this.nar = nar;
// //            causeBelief = nar.newChannel(String.valueOf((char) BELIEF));
// //            causeGoal = nar.newChannel(String.valueOf((char) GOAL));
// //            causeQuestion = nar.newChannel(String.valueOf((char) QUESTION));
// //            causeQuest = nar.newChannel(String.valueOf((char) QUEST));
// //            causeEternal = nar.newChannel("Eternal");
// //            causePast = nar.newChannel("Past");
// //            causePresent = nar.newChannel("Present");
// //            causeFuture = nar.newChannel("Future");
// ////            causeConf = new ChannelRange("Conf", 7 /* odd */, nar::newChannel, 0f, 1f);
// ////            causeFreq = new ChannelRange("Freq", 7 /* odd */, nar::newChannel, 0f, 1f);
// //        }
// //
// //        /**
// //         * classifies the implicit / self-evident causes a task
// //         */
// //        public short[] get(Task x) {
// //            //short[] ii = ArrayPool.shorts().getExact(8);
// //
// //            short time;
// //            if (x.isEternal())
// //                time = causeEternal.id;
// //            else {
// //                long now = nar.time();
// //                long then = x.nearestTimeTo(now);
// //                if (Math.abs(now - then) <= nar.dur())
// //                    time = causePresent.id;
// //                else if (then > now)
// //                    time = causeFuture.id;
// //                else
// //                    time = causePast.id;
// //            }
// //
// //            short punc;
// //            switch (x.punc()) {
// //                case BELIEF:
// //                    punc = causeBelief.id;
// //                    break;
// //                case GOAL:
// //                    punc = causeGoal.id;
// //                    break;
// //                case QUESTION:
// //                    punc = causeQuestion.id;
// //                    break;
// //                case QUEST:
// //                    punc = causeQuest.id;
// //                    break;
// //                default:
// //                    throw new UnsupportedOperationException();
// //            }
// ////            if (x.isBeliefOrGoal()) {
// ////                short freq = causeFreq.get(x.freq()).id;
// ////                short conf = causeConf.get(x.conf()).id;
// ////                return new short[]{time, punc, freq, conf};
// ////            } else {
// //            return new short[]{time, punc};
// ////            }
// //        }
// //
// //    }
/**
 * automatically adds the cause id to each input
 */
public CauseChannel<ITask> newCauseChannel(Object id) {
    synchronized (causes) {
        // final short[] sharedOneElement = {ci};
        final short ci = (short) (causes.size());
        CauseChannel c = new CauseChannel.TaskChannel(this, ci, id, (x) -> {
            if (x instanceof NALTask) {
                NALTask t = (NALTask) x;
                int tcl = t.cause.length;
                if (tcl == 0) {
                    // assert (sharedOneElement[0] == ci);
                    // sharedOneElement;
                    t.cause = new short[] { ci };
                } else {
                    if (tcl == 1 && t.cause[0] == ci)
                        // already equivalent
                        return;
                    else {
                        // concat
                        t.cause = Arrays.copyOf(t.cause, tcl + 1);
                        t.cause[tcl] = ci;
                    }
                }
            }
        });
        causes.add(c);
        return c;
    }
}
Also used : NALTask(nars.task.NALTask)

Example 9 with NALTask

use of nars.task.NALTask in project narchy by automenta.

the class EternalTable method add.

@Override
public boolean add(/*@NotNull*/
Task input, TaskConcept c, /*@NotNull*/
NAR nar) {
    int cap = capacity();
    if (cap == 0) {
        /*if (input.isInput())
                throw new RuntimeException("input task rejected (0 capacity): " + input + " "+ this + " " + this.capacity());*/
        return false;
    }
    Task activated;
    float iPri = input.priElseZero();
    if ((input.conf() >= 1f) && (cap != 1) && (isEmpty() || (first().conf() < 1f))) {
        // AXIOMATIC/CONSTANT BELIEF/GOAL
        synchronized (this) {
            addEternalAxiom(input, this, nar);
            activated = input;
        }
    } else {
        Task revised = tryRevision(input, nar);
        if (revised != null) {
            if (revised == input) {
                // already present duplicate
                if (input.isInput()) {
                    activated = input;
                } else {
                    // so ignore if derived
                    return false;
                }
            } else if (revised.equals(input)) {
                // HACK todo avoid this duplcate equals which is already known from tryRevision
                // float maxActivation = 1f - revised.priElseZero();
                // activation = Math.min(maxActivation, input.priElseZero()); //absorb up to 1.0 max
                revised.priMax(input.priElseZero());
                activated = revised;
                input.delete();
                ((NALTask) revised).causeMerge(input);
            } else {
                // a novel revision
                if (insert(revised)) {
                    activated = revised;
                } else {
                    activated = null;
                    revised.delete();
                }
                boolean inputIns = insert(input);
                if (inputIns) {
                    if (activated == null) {
                        activated = input;
                    }
                    // revised will be activated, but at least emit a taskProcess for the input task
                    nar.eventTask.emit(activated);
                } else {
                    activated = null;
                    input.delete();
                }
            }
        } else {
            if (!input.isDeleted() && insert(input)) {
                activated = input;
            } else {
                activated = null;
                input.delete();
            }
        }
    }
    if (activated != null)
        Tasklinks.linkTask(activated, iPri, c, nar);
    return true;
}
Also used : NALTask(nars.task.NALTask) Task(nars.Task)

Example 10 with NALTask

use of nars.task.NALTask in project narchy by automenta.

the class EternalTable method tryRevision.

/**
 * @return null: no revision could be applied
 * ==newBelief: existing duplicate found
 * non-null: revised task
 */
@Nullable
private /*Revision*/
Task tryRevision(/*@NotNull*/
Task y, /* input */
@Nullable NAR nar) {
    Object[] list = this.list;
    int bsize = list.length;
    if (bsize == 0)
        // nothing to revise with
        return null;
    // Try to select a best revision partner from existing beliefs:
    Task oldBelief = null;
    Truth conclusion = null;
    Truth newBeliefTruth = y.truth();
    for (int i = 0; i < bsize; i++) {
        Task x = (Task) list[i];
        if (// the array has trailing nulls from having extra capacity
        x == null)
            break;
        if (x.equals(y)) {
            /*if (x!=y && x.isInput())
                    throw new RuntimeException("different input task instances with same stamp");*/
            return x;
        }
        // same conf, same stamp, both non-cyclic; interpolate to avoid one being preferred over another arbitrarily
        float xconf = x.conf();
        if (Util.equals(xconf, y.conf(), nar.confResolution.floatValue()) && (!x.isCyclic() && !y.isCyclic()) && Arrays.equals(x.stamp(), y.stamp())) {
            conclusion = new PreciseTruth(0.5f * (x.freq() + y.freq()), xconf);
        } else if (Stamp.overlapping(y, x)) {
            boolean FILTER_WEAKER_BUT_EQUAL = false;
            if (FILTER_WEAKER_BUT_EQUAL && !y.isInput() && xconf >= y.conf() && Util.equals(x.freq(), y.freq(), nar.freqResolution.floatValue()) && Arrays.equals(y.stamp(), x.stamp())) {
                y.delete();
                // subsume by stronger belief with same freq and stamp
                return null;
            }
            // unrevisable
            continue;
        } else {
            // 
            // float factor = tRel * freqMatch;
            // if (factor < best) {
            // //even with conf=1.0 it wouldnt be enough to exceed existing best match
            // continue;
            // }
            // float minValidConf = Math.min(newBeliefConf, x.conf());
            // if (minValidConf < bestConf) continue;
            // float minValidRank = BeliefTable.rankEternalByOriginality(minValidConf, totalEvidence);
            // if (minValidRank < bestRank) continue;
            Truth xt = x.truth();
            // TODO use overlappingFraction?
            Truth yt = Revision.revise(newBeliefTruth, xt, 1f, conclusion == null ? 0 : conclusion.evi());
            if (yt == null)
                continue;
            yt = yt.dither(nar);
            if (// //avoid a weak or duplicate truth
            yt == null || yt.equals(xt, nar) || yt.equals(newBeliefTruth, nar))
                continue;
            conclusion = yt;
        }
        oldBelief = x;
    }
    if (oldBelief == null)
        return null;
    final float newBeliefWeight = y.evi();
    // TODO use Task.tryContent in building the task:
    float aProp = newBeliefWeight / (newBeliefWeight + oldBelief.evi());
    Term t = Revision.intermpolate(y.term(), oldBelief.term(), aProp, nar);
    Task prevBelief = oldBelief;
    Task x = Task.tryTask(t, y.punc(), conclusion, (term, revisionTruth) -> new NALTask(term, y.punc(), revisionTruth, nar.time(), /* creation time */
    ETERNAL, ETERNAL, Stamp.zip(prevBelief.stamp(), y.stamp(), 0.5f)));
    if (x != null) {
        x.priSet(Priority.fund(Math.max(prevBelief.priElseZero(), y.priElseZero()), false, prevBelief, y));
        ((NALTask) x).cause = Cause.sample(Param.causeCapacity.intValue(), y, prevBelief);
        if (Param.DEBUG)
            x.log("Insertion Revision");
    // ((NALTask)y).meta("@",x);
    // ((NALTask)prevBelief).meta("@",x);
    }
    return x;
}
Also used : NALTask(nars.task.NALTask) Task(nars.Task) PreciseTruth(nars.truth.PreciseTruth) NALTask(nars.task.NALTask) Term(nars.term.Term) Truth(nars.truth.Truth) PreciseTruth(nars.truth.PreciseTruth) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

NALTask (nars.task.NALTask)17 Task (nars.Task)8 Term (nars.term.Term)8 Truth (nars.truth.Truth)7 Nullable (org.jetbrains.annotations.Nullable)6 ITask (nars.task.ITask)4 SignalTask (nars.task.signal.SignalTask)3 PreciseTruth (nars.truth.PreciseTruth)3 FasterList (jcog.list.FasterList)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 Util (jcog.Util)1 LongInterval (jcog.math.LongInterval)1 Priority (jcog.pri.Priority)1 VLink (jcog.pri.VLink)1 Top (jcog.sort.Top)1