Search in sources :

Example 1 with CachedTopN

use of jcog.sort.CachedTopN 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)

Aggregations

Random (java.util.Random)1 CachedTopN (jcog.sort.CachedTopN)1 Top (jcog.sort.Top)1 Task (nars.Task)1