use of jcog.sort.Top in project narchy by automenta.
the class DynamicBeliefTable method match.
@Override
public Task match(long start, long end, Term template, NAR nar, Predicate<Task> filter) {
Task x = super.match(start, end, template, nar, filter);
Task y = taskDynamic(start, end, template, nar);
if (x == null && y == null)
return null;
if (filter != null) {
if (x != null && !filter.test(x))
x = null;
if (y != null && !filter.test(y))
y = null;
}
if (y == null)
return x;
if (x == null)
return y;
if (x.equals(y))
return x;
int dur = nar.dur();
// choose highest confidence
Top<Task> top = new Top<>(t -> t.evi(start, end, dur));
if (x.term().equals(y.term()) && !Stamp.overlapping(x, y)) {
// try to revise
Task xy = Revision.mergeTemporal(nar, x, y);
if (xy != null && (filter == null || filter.test(xy)))
top.accept(xy);
}
top.accept(x);
top.accept(y);
return top.the;
}
use of jcog.sort.Top 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);
}
}
use of jcog.sort.Top in project narchy by automenta.
the class RTreeBeliefTable method mergeOrDelete.
private boolean mergeOrDelete(Space<TaskRegion> treeRW, Top2<Leaf<TaskRegion>> l, FloatFunction<Task> taskStrength, float inputStrength, FloatFunction<TaskRegion> weakestTasks, Consumer<Tasked> added, NAR nar) {
TaskRegion a, b;
Leaf<TaskRegion> la = l.a;
short sa = la.size;
if (sa > 2) {
Top2<TaskRegion> w = new Top2<>(weakestTasks);
la.forEach(w::add);
a = w.a;
b = w.b;
} else if (sa == 2) {
a = la.get(0);
b = la.get(1);
} else {
a = la.get(0);
Leaf<TaskRegion> lb = l.b;
if (lb != null) {
int sb = lb.size();
if (sb > 1) {
Top<TaskRegion> w = new Top<>(weakestTasks);
lb.forEach(w);
b = w.the;
} else if (sb == 1) {
b = lb.get(0);
} else {
// ??
b = null;
}
} else {
b = null;
}
}
assert (a != null);
Task at = a.task();
float aPri = at.pri();
treeRW.remove(at);
if (b != null) {
Task bt = b.task();
if (bt.isDeleted()) {
treeRW.remove(bt);
return true;
} else {
at.meta("@", bt);
}
if (// already deleted
aPri != aPri)
return true;
Task c = // HACK
(this instanceof Simple || (at.term().equals(bt.term()))) ? Revision.mergeTemporal(nar, at, bt) : // TODO remove this when the mergeTemporal fully supports CONJ and Temporal
Revision.merge(at, bt, nar.time(), c2wSafe(nar.confMin.floatValue()), nar);
if (c != null && !c.equals(a) && !c.equals(b)) {
boolean allowMerge;
if (inputStrength != inputStrength) {
allowMerge = true;
} else {
float strengthRemoved = taskStrength.floatValueOf(at) + taskStrength.floatValueOf(bt);
float strengthAdded = taskStrength.floatValueOf(c) + inputStrength;
allowMerge = strengthAdded >= strengthRemoved;
}
if (allowMerge) {
treeRW.remove(bt);
// forward
((NALTask) at).delete(c);
// forward
((NALTask) bt).delete(c);
if (treeRW.add(c))
added.accept(c);
return true;
} else {
// merge result is not strong enough
c.delete();
}
}
}
// TODO do this outside of the locked section
if (Param.ETERNALIZE_EVICTED_TEMPORAL_TASKS)
eternalize(at, added, nar);
at.delete();
return true;
}
Aggregations