Search in sources :

Example 26 with Task

use of nars.Task in project narchy by automenta.

the class Taskify method test.

/**
 * note: the return value here shouldnt matter so just return true anyway
 */
@Override
public boolean test(Derivation d) {
    Truth tru = d.concTruth;
    if (tru != null) {
        float finalEvi = tru.evi() * d.concEviFactor;
        if (d.eviMin > finalEvi) {
            d.use(Param.TTL_EVI_INSUFFICIENT);
            return true;
        }
        tru = tru.withEvi(finalEvi);
    }
    Term x0 = d.derivedTerm.get();
    Term x = d.anon.get(x0).normalize();
    long[] occ = d.concOcc;
    byte punc = d.concPunc;
    assert (punc != 0) : "no punctuation assigned";
    DerivedTask t = (DerivedTask) Task.tryTask(x, punc, tru, (C, tr) -> {
        int dither = d.ditherTime;
        long start = Tense.dither(occ[0], dither);
        long end = Tense.dither(occ[1], dither);
        assert (end >= start) : "task has reversed occurrence: " + start + ".." + end;
        return Param.DEBUG ? new DebugDerivedTask(C, punc, tr, start, end, d) : new DerivedTask(C, punc, tr, start, end, d);
    });
    if (t == null) {
        d.nar.emotion.deriveFailTaskify.increment();
        return spam(d, Param.TTL_DERIVE_TASK_FAIL);
    }
    if (same(t, d._task, d.freqRes) || (d._belief != null && same(t, d._belief, d.freqRes))) {
        d.nar.emotion.deriveFailParentDuplicate.increment();
        return spam(d, Param.TTL_DERIVE_TASK_SAME);
    }
    if (d.single)
        t.setCyclic(true);
    float priority = d.deriver.prioritize.pri(t, d);
    if (priority != priority) {
        d.nar.emotion.deriveFailPrioritize.increment();
        return spam(d, Param.TTL_DERIVE_TASK_PRIORITIZE);
    }
    t.priSet(priority);
    t.cause = ArrayUtils.addAll(d.parentCause, channel.id);
    if (d.add(t) != t) {
        d.nar.emotion.deriveFailDerivationDuplicate.increment();
        spam(d, Param.TTL_DERIVE_TASK_REPEAT);
    } else {
        if (Param.DEBUG)
            t.log(channel.ruleString);
        d.use(Param.TTL_DERIVE_TASK_SUCCESS);
    }
    return true;
}
Also used : DebugDerivedTask(nars.task.DebugDerivedTask) Tense(nars.time.Tense) Logger(org.slf4j.Logger) DerivedTask(nars.task.DerivedTask) FILTER_SIMILAR_DERIVATIONS(nars.Param.FILTER_SIMILAR_DERIVATIONS) LoggerFactory(org.slf4j.LoggerFactory) ArrayUtils(org.apache.commons.lang3.ArrayUtils) BOOL(nars.Op.BOOL) nars.$(nars.$) Util(jcog.Util) NALTask(nars.task.NALTask) Param(nars.Param) AbstractPred(nars.term.pred.AbstractPred) Task(nars.Task) Truth(nars.truth.Truth) VAR_PATTERN(nars.Op.VAR_PATTERN) Term(nars.term.Term) Term(nars.term.Term) DebugDerivedTask(nars.task.DebugDerivedTask) DerivedTask(nars.task.DerivedTask) DebugDerivedTask(nars.task.DebugDerivedTask) Truth(nars.truth.Truth)

Example 27 with Task

use of nars.Task in project narchy by automenta.

the class Activate method premises.

/**
 * hypothesize premises, up to a max specified #
 */
/*@NotNull*/
public void premises(NAR nar, BiPredicate<Task, PriReference<Term>> each, int _tasklinks, int _termlinksPerTasklink) {
    nar.emotion.conceptFire.increment();
    Bag<?, TaskLink> tasklinks = id.tasklinks();
    float linkForgetting = nar.forgetRate.floatValue();
    tasklinks.commit(tasklinks.forget(linkForgetting));
    int ntasklinks = tasklinks.size();
    if (ntasklinks == 0)
        return;
    final Bag<Term, PriReference<Term>> termlinks = id.termlinks();
    termlinks.commit(termlinks.forget(linkForgetting));
    int ntermlinks = termlinks.size();
    if (ntermlinks == 0)
        // TODO when can this happen
        return;
    int[] ttl = { _tasklinks * _termlinksPerTasklink };
    Random rng = nar.random();
    // ((TaskLinkCurveBag)tasklinks).compress(nar);
    tasklinks.sample(rng, _tasklinks, tasklink -> {
        Task task = tasklink.get(nar);
        if (task != null) {
            // if (priApplied > Pri.EPSILON)
            Tasklinks.linkTaskTemplates(id, task, /*pri *  */
            task.priElseZero(), nar);
            termlinks.sample(rng, _termlinksPerTasklink, (termlink) -> {
                if (!each.test(task, termlink)) {
                    ttl[0] = 0;
                    return false;
                } else {
                    return (--ttl[0] > 0);
                }
            });
        } else {
            tasklink.delete();
            // safety misfire decrement
            --ttl[0];
        }
        // ? Bag.BagSample.Next : Bag.BagSample.Stop;
        return (ttl[0] > 0);
    });
}
Also used : Task(nars.Task) Random(java.util.Random) TaskLink(nars.link.TaskLink) Term(nars.term.Term) PriReference(jcog.pri.PriReference)

Example 28 with Task

use of nars.Task in project narchy by automenta.

the class EternalTable method weakest.

public Task weakest() {
    Object[] l = this.list;
    if (l.length == 0)
        return null;
    int n = l.length - 1;
    Task w = null;
    // scan upwards for first non-null
    while (n > 0 && (w = (Task) l[n]) != null) n--;
    return w;
}
Also used : NALTask(nars.task.NALTask) Task(nars.Task)

Example 29 with Task

use of nars.Task 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 30 with Task

use of nars.Task 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

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