Search in sources :

Example 31 with Task

use of nars.Task in project narchy by automenta.

the class TaskRegion method mbr.

@Override
default TaskRegion mbr(HyperRegion r) {
    if (contains(r))
        return this;
    else {
        if (r instanceof Task) {
            // accelerated mbr
            Task er = (Task) r;
            float ef = er.freq();
            float ec = er.conf();
            long es = er.start();
            long ee = er.end();
            if (this instanceof Task) {
                Task tr = (Task) this;
                float tf = tr.freq();
                float f0, f1;
                if (tf <= ef) {
                    f0 = tf;
                    f1 = ef;
                } else {
                    f0 = ef;
                    f1 = tf;
                }
                float c0;
                float c1;
                float tc = tr.conf();
                if (tc <= ec) {
                    c0 = tc;
                    c1 = ec;
                } else {
                    c0 = ec;
                    c1 = tc;
                }
                long ts = start();
                long te = end();
                // }
                return new TasksRegion(Math.min(ts, es), Math.max(te, ee), f0, f1, c0, c1);
            } else {
                return new TasksRegion(Math.min(start(), es), Math.max(end(), ee), Util.min(coordF(false, 1), ef), Util.max(coordF(true, 1), ef), Util.min(coordF(false, 2), ec), Util.max(coordF(true, 2), ec));
            }
        } else {
            TaskRegion er = (TaskRegion) r;
            return new TasksRegion(Math.min(start(), er.start()), Math.max(end(), er.end()), Util.min(coordF(false, 1), er.coordF(false, 1)), Util.max(coordF(true, 1), er.coordF(true, 1)), Util.min(coordF(false, 2), er.coordF(false, 2)), Util.max(coordF(true, 2), er.coordF(true, 2)));
        }
    }
}
Also used : Task(nars.Task)

Example 32 with Task

use of nars.Task in project narchy by automenta.

the class TaskTable method match.

default void match(TaskMatch m, Consumer<Task> target) {
    if (isEmpty())
        return;
    // TODO expand here
    Random rng = m.sample();
    if (rng == null) {
        // strongest
        // prefer temporally relevant, and original
        int limit = m.limit();
        assert (limit > 0);
        if (limit == 1) {
            Top<Task> q = new Top<>(m.value());
            forEachTask(q::accept);
            Task the = q.the;
            if (the != null)
                target.accept(the);
        } else {
            TopN<Task> q = new CachedTopN<>(new Task[limit], m.value());
            forEachTask(q::accept);
            q.forEach(target);
        }
    } else {
        // sampled
        Task[] t = toArray();
        if (t.length == 0)
            return;
        int limit = m.limit();
        if (t.length <= limit) {
            // provide all
            for (Task x : t) target.accept(x);
            return;
        }
        // roulette selection
        FloatFunction<Task> value = m.value();
        float[] w = Util.map(t, value);
        final int[] remain = { limit };
        Roulette.RouletteUnique.run(w, (x) -> {
            target.accept(t[x]);
            return --remain[0] > 0;
        }, rng);
    }
}
Also used : Task(nars.Task) Random(java.util.Random) Top(jcog.sort.Top) CachedTopN(jcog.sort.CachedTopN)

Example 33 with Task

use of nars.Task in project narchy by automenta.

the class ActiveQuestionTask method onAnswered.

@Override
@Nullable
public Task onAnswered(Task answer, NAR nar) {
    Task x = super.onAnswered(answer, nar);
    onAnswer(answer);
    return x;
}
Also used : Task(nars.Task) Nullable(org.jetbrains.annotations.Nullable)

Example 34 with Task

use of nars.Task in project narchy by automenta.

the class TruthPolation method truth.

public Truth truth(boolean filterCyclic) /*float eviFactor, float eviMin*/
{
    if (isEmpty())
        return null;
    // project temporally, removing if no evidence provided
    {
        removeIf(tc -> {
            Task t = tc.task;
            Truth tt = t.truth(start, end, dur, Truth.EVI_MIN);
            if (tt != null) {
                // not necessarily the task's reported "average" freq in case of Truthlets
                tc.freq = tt.freq();
                tc.evi = tt.evi();
                return false;
            } else {
                // removed
                return true;
            }
        });
    }
    // remove overlapping evidence, preferring the strongest contributors of each
    if (filterCyclic) {
        int s = size();
        if (s == 0)
            return null;
        else if (s > 1) {
            // descending by strength
            sortThisByFloat(tc -> -tc.evi);
            // TODO maybe factor in originality to reduce overlap so evidence can be combined better
            // remove the weaker holder of any overlapping evidence
            LongHashSet e = new LongHashSet(s * 2);
            removeIf(tc -> {
                long[] stamp = tc.task.stamp();
                for (long ss : stamp) {
                    if (!e.add(ss))
                        // overlap
                        return true;
                }
                return false;
            });
        }
    }
    {
        int s = size();
        switch(s) {
            case 0:
                return null;
            case 1:
                {
                    TaskComponent only = get(0);
                    return new PreciseTruth(only.freq, only.evi, false);
                }
            default:
                {
                    // interpolate
                    // float eviSum = 0, confSum = 0, wFreqSum = 0;
                    float eviSum = 0, wFreqSum = 0;
                    for (int i = 0, thisSize = this.size(); i < thisSize; i++) {
                        TaskComponent x = this.get(i);
                        float ee = x.evi;
                        eviSum += ee;
                        // float ce = w2cSafe(ee);
                        // confSum += ce;
                        // wFreqSum += ce * x.freq;
                        wFreqSum += ee * x.freq;
                    }
                    assert (Float.isFinite(eviSum));
                    float c = w2cSafe(eviSum);
                    if (c < Param.TRUTH_EPSILON)
                        return null;
                    else {
                        // float f = (wFreqSum / confSum);
                        float f = (wFreqSum / eviSum);
                        return new PreciseTruth(f, c);
                    }
                }
        }
    }
}
Also used : Param(nars.Param) Nullable(org.jetbrains.annotations.Nullable) Task(nars.Task) TaskRegion(nars.task.util.TaskRegion) Truth(nars.truth.Truth) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) TruthFunctions.w2cSafe(nars.truth.TruthFunctions.w2cSafe) FasterList(jcog.list.FasterList) PreciseTruth(nars.truth.PreciseTruth) NaN(java.lang.Float.NaN) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Task(nars.Task) PreciseTruth(nars.truth.PreciseTruth) Truth(nars.truth.Truth) PreciseTruth(nars.truth.PreciseTruth)

Example 35 with Task

use of nars.Task in project narchy by automenta.

the class NarseseBaseTest method testIncompleteTask.

// @Test public void testTruthFreqOnly() {
// testTruth("%0.0%", 0f, 0.9f);
// testTruth("%1.0%", 1f, 0.9f);
// }
@Test
public void testIncompleteTask() throws Narsese.NarseseException {
    Task t = task("<a --> b>.");
    assertNotNull(t);
    assertEquals(Op.INH, t.op());
    Term i = t.term();
    assertEquals("a", i.sub(0).toString());
    assertEquals("b", i.sub(1).toString());
    assertEquals('.', t.punc());
    // assertEquals(Global.DEFAULT_JUDGMENT_PRIORITY, t.getPriority(), 0.001);
    // assertEquals(Global.DEFAULT_JUDGMENT_DURABILITY, t.getDurability(), 0.001);
    assertEquals(1.0f, t.truth().freq(), 0.001);
// assertEquals(Global.DEFAULT_JUDGMENT_CONFIDENCE, t.getTruth().getConfidence(), 0.001);
}
Also used : Task(nars.Task) Term(nars.term.Term) Test(org.junit.jupiter.api.Test)

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