use of org.eclipse.collections.api.tuple.Pair in project narchy by automenta.
the class ConjClustering method conjoinCentroid.
// /**
// * produces a parallel conjunction term consisting of all the task's terms
// */
// public Stream<List<Task>> chunk(Stream<Task> input, int maxComponentsPerTerm, int maxVolume) {
// final int[] group = {0};
// final int[] subterms = {0};
// final int[] currentVolume = {0};
// final float[] currentConf = {1};
// return input.filter(x -> !x.isDeleted())
// .collect(Collectors.groupingBy(x -> {
//
// int v = x.volume();
// float c = x.conf();
//
// if ((subterms[0] >= maxComponentsPerTerm) || (currentVolume[0] + v >= maxVolume) || (currentConf[0] * c < confMin)) {
// //next group
// group[0]++;
// subterms[0] = 1;
// currentVolume[0] = v;
// currentConf[0] = c;
// } else {
// subterms[0]++;
// currentVolume[0] += v;
// currentConf[0] *= c;
// }
//
// return group[0];
// }))
// .entrySet().stream()
// .map(c -> {
// List<Task> v = c.getValue();
// return c.getKey() >= 0 && //only batches of >1
// v.size() > 1 ? v : null; //ignore the -1 discard group
// })
// .filter(Objects::nonNull);
//
// }
// static final BiFunction<Task, Task, Task> termPointMerger = (prevZ, newZ) -> ((prevZ == null) || (newZ.conf() >= prevZ.conf())) ?
// newZ : prevZ;
private List<Task> conjoinCentroid(Stream<VLink<Task>> group, Pair<NAR, List<Task>> narAndTarget) {
NAR nar = narAndTarget.getOne();
// get only the maximum confidence task for each term at its given starting time
// in.input(
// chunk(group.filter(Objects::nonNull).takeWhile(kontinue)
// .map(x -> x.id), maxConjSize, volMax).takeWhile(kontinue).map(tasks -> {
Iterator<VLink<Task>> gg = group.filter(x -> x != null && !x.isDeleted()).iterator();
// Iterators.peekingIterator();
Map<LongObjectPair<Term>, Task> vv = new HashMap<>();
FasterList<Task> actualTasks = new FasterList();
int centroidGen = 0;
List<Task> gen = narAndTarget.getTwo();
main: while (gg.hasNext() && centroidGen < taskLimitPerCentroid) {
vv.clear();
actualTasks.clear();
long end = Long.MIN_VALUE;
long start = Long.MAX_VALUE;
int dur = nar.dur();
float freq = 1f;
float conf = 1f;
float priMax = Float.NEGATIVE_INFINITY, priMin = Float.POSITIVE_INFINITY;
int vol = 0;
int maxVolume = 0;
do {
if (!gg.hasNext())
break;
Task t = gg.next().id;
// gg.peek().id;
Term xt = t.term();
long zs = Tense.dither(t.start(), ditherTime);
long ze = Tense.dither(t.end(), ditherTime);
// assert (end >= start);
Truth tx = t.truth();
Term xtn = xt.neg();
if (tx.isNegative()) {
xt = xtn;
}
int xtv = xt.volume();
maxVolume = Math.max(maxVolume, xt.volume());
if (vol + xtv + 1 >= volMax || conf * tx.conf() < confMin) {
// cant go any further with this task
continue;
}
boolean involved = false;
LongObjectPair<Term> ps = pair(zs, xt);
Term xtNeg = xt.neg();
if (!vv.containsKey(pair(zs, xtNeg)) && null == vv.putIfAbsent(ps, t)) {
vol += xtv;
if (start > zs)
start = zs;
if (end < zs)
end = zs;
involved = true;
}
if (ze - zs >= dur) {
// endpoint
if (vol + xtv + 1 < volMax) {
LongObjectPair<Term> pe = pair(ze, xt);
if (!vv.containsKey(pair(ze, xtNeg)) && null == vv.putIfAbsent(pe, t)) {
// end point, if different from start
vol += xtv;
if (end < ze)
end = ze;
involved = true;
}
}
}
if (involved) {
actualTasks.add(t);
conf *= tx.conf();
float tf = tx.freq();
// since it will appear as a negated subterm
freq *= tx.isNegative() ? (1f - tf) : tf;
float p = t.priElseZero();
if (p < priMin)
priMin = p;
if (p > priMax)
priMax = p;
}
} while (vol < volMax - 1 && conf > confMin);
int vs = actualTasks.size();
if (vs < 2)
continue;
// the tasks which are actually involved
Task[] uu = actualTasks.toArrayRecycled(Task[]::new);
// TODO discount based on evidential overlap? needs N-way overlapFraction function
ObjectFloatPair<long[]> evidence = Stamp.zip(actualTasks, Param.STAMP_CAPACITY);
float overlap = evidence.getTwo();
float e = c2w(conf) * Param.overlapFactor(overlap);
if (e > 0) {
final Truth t = Truth.theDithered(freq, e, nar);
if (t != null) {
Term cj = Conj.conj(vv.keySet());
if (cj != null) {
cj = cj.normalize();
if (Math.abs(cj.dtRange() - (end - start)) < ditherTime) {
// test if merge collapse occurred and occurrence time needs recalculated
ObjectBooleanPair<Term> cp = Task.tryContent(cj, punc, true);
if (cp != null) {
// TODO use a truth calculated specific to this fixed-size batch, not all the tasks combined
NALTask m = new STMClusterTask(cp, t, start, start, evidence.getOne(), punc, now);
// if (evidence.getTwo() > 0) m.setCyclic(true);
m.cause = Cause.sample(Param.causeCapacity.intValue(), uu);
float p = // priMax;
priMin;
// (priMin + priMax) / 2f;
// complexity deduction
// how much more complex the conjunction is than the most complex of its ingredients
int v = cp.getOne().volume();
float cmplFactor = ((float) v) / (v + maxVolume);
m.priSet(Priority.fund(p * cmplFactor, true, uu));
gen.add(m);
centroidGen++;
}
} else {
// System.out.println("merge collapse, recalcu");
}
}
}
}
}
return gen.isEmpty() ? null : gen;
}
use of org.eclipse.collections.api.tuple.Pair in project narchy by automenta.
the class SpeechPlan method next.
public boolean next() {
// long start = nar.time();
float dur = nar.dur() * durationsPerWord;
long now = nar.time();
long startOfNow = now - (int) Math.ceil(dur);
long endOfNow = now + (int) Math.floor(dur);
FasterList<Pair<Term, Truth>> pending = new FasterList<>(0);
synchronized (vocalize) {
// vocalize.rowKeySet().tailSet(startOfNow-1).clear();
SortedSet<Long> tt = vocalize.rowKeySet().headSet(endOfNow);
if (!tt.isEmpty()) {
LongArrayList ll = new LongArrayList(tt.size());
// copy to array so we can modify the vocalize rows
tt.forEach(ll::add);
ll.forEach(t -> {
Set<Map.Entry<Term, TruthAccumulator>> entries = vocalize.row(t).entrySet();
if (t >= startOfNow) {
entries.forEach(e -> {
Truth x = e.getValue().commitSum();
if (x.expectation() > expectationThreshold)
pending.add(Tuples.pair(e.getKey(), x));
});
}
entries.clear();
});
}
}
if (pending.isEmpty())
return true;
// TODO decide word..
Term spoken = decide(pending);
if (spoken != null)
speak.accept(spoken);
return true;
}
use of org.eclipse.collections.api.tuple.Pair in project narchy by automenta.
the class Builtin method registerFunctors.
public static void registerFunctors(NAR nar) {
for (Concept t : Builtin.statik) {
nar.on(t);
}
nar.on(Functor.f1("varIntro", (x) -> {
Pair<Term, Map<Term, Term>> result = DepIndepVarIntroduction.the.apply(x, nar.random());
return result != null ? result.getOne() : Null;
}));
nar.on(Functor.f1((Atom) $.the("termlinkRandom"), (Term t) -> {
@Nullable Concept c = nar.conceptualize(t);
if (c == null)
return Null;
@Nullable PriReference<Term> tl = c.termlinks().sample(nar.random());
if (tl == null)
return Null;
return tl.get();
}));
// nar.on(Functor.f("service", (TermContainer c) ->
// $.sete(
// nar.services().map(
// (e) ->
// $.p(e, $.the(e.getValue().state())))
// .toArray(Term[]::new)
// )
// ));
/**
* subterm, but specifically inside an ellipsis. otherwise pass through
*/
nar.on(Functor.f("esubterm", (Subterms c) -> {
Term x = c.sub(0, null);
if (x == null)
return Null;
Term index = c.sub(1, Null);
if (index == Null)
return Null;
int which;
if (index != null) {
if (index instanceof Variable)
return Null;
which = $.intValue(index, -1);
if (which < 0) {
return Null;
}
} else {
// random
which = nar.random().nextInt(x.subs());
}
return x.sub(which);
}));
nar.on(Functor.f2((Atom) $.the("without"), (Term container, Term content) -> Op.without(container, x -> x.equals(content), nar.random())));
nar.on(Functor.f2((Atom) $.the("withoutPosOrNeg"), (Term container, Term content) -> Op.without(container, x -> x.unneg().equals(content), nar.random())));
/**
* TODO rename this to 'dropAnyCommutive'
* remove an element from a commutive conjunction (or set), at random, and try re-creating
* the compound. wont necessarily work in all situations.
* TODO move the type restriction to another functor to wrap this
*
* this also filter a single variable (depvar) from being a result
*/
nar.on(Functor.f1((Atom) $.the("dropAnySet"), (Term t) -> {
Op oo = t.op();
if (oo == INT) {
if (t instanceof Int.IntRange) {
// select random location in the int and split either up or down
Int.IntRange i = (Int.IntRange) t;
Random rng = nar.random();
if (i.min + 1 == i.max) {
// arity=2
return Int.the(rng.nextBoolean() ? i.min : i.max);
} else if (i.min + 2 == i.max) {
// arity=3
switch(rng.nextInt(4)) {
case 0:
return Int.the(i.min);
case 1:
return Int.range(i.min, i.min + 1);
case 2:
return Int.range(i.min + 1, i.min + 2);
case 3:
return Int.the(i.max);
default:
throw new UnsupportedOperationException();
}
} else {
int split = // midpoint, deterministic
(i.max + i.min) / 2;
// rng.nextInt(i.max-i.min-2);
return (rng.nextBoolean()) ? Int.range(i.min, split + 1) : Int.range(split + 1, i.max);
}
}
// cant drop int by itself
return Null;
}
if (!oo.in(SETi.bit | SETe.bit | SECTi.bit | SECTe.bit))
// returning the original value may cause feedback loop in callees expcting a change in value
return Null;
int size = t.subs();
switch(size) {
case 0:
assert (false) : "empty set impossible here";
return Null;
case 1:
return Null;
/* can't shrink below one element */
case 2:
int n = nar.random().nextInt(2);
return oo.the(t.sub(n));
default:
Term[] y = Terms.dropRandom(nar.random(), t.subterms());
return oo.the(y);
}
}));
/**
* depvar cleaning from commutive conj
*/
nar.on(Functor.f1((Atom) $.the("ifConjCommNoDepVars"), (Term t) -> {
if (!t.hasAny(VAR_DEP))
return t;
Op oo = t.op();
if (oo != CONJ)
return t;
SortedSet<Term> s = t.subterms().toSetSorted();
if (!s.removeIf(x -> x.unneg().op() == VAR_DEP))
return t;
return CONJ.the(t.dt(), s);
}));
/**
* drops a random contained event, whether at first layer or below
*/
nar.on(Functor.f1((Atom) $.the("dropAnyEvent"), (Term t) -> {
Op oo = t.op();
if (oo != CONJ)
// returning the original value may cause feedback loop in callees expcting a change in value
return Null;
FasterList<LongObjectPair<Term>> ee = Conj.eventList(t);
ee.remove(nar.random().nextInt(ee.size()));
return Conj.conj(ee);
// }
// if (r instanceof Variable /*&& r.op()!=VAR_DEP*/)
// return Null; //HACK dont allow returning a variable as an event during decomposition HACK TODO make more careful and return the only result if one subterm is a non-returnable variable
// return r;
}));
nar.on(Functor.f2((Atom) $.the("conjEvent"), (Term c, Term when) -> {
if (c.op() != CONJ || !(when instanceof Atom))
return Null;
// extract earliest or latest &| timeslice of events
throw new TODO();
// if (c.dt() == DTERNAL || c.dt() == 0) {
// return c.sub(nar.random().nextInt(c.subs())); //choose a subterm at random
// }
// assert (c.subs() == 2);
// int target;
// switch (when.toString()) {
// case "early":
// target = 0;
// break;
// case "late":
// target = 1;
// break;
// default:
// throw new UnsupportedOperationException();
// }
// if (c.dt() < 0)
// target = 1 - target;
// return c.sub(target);
}));
/**
* similar to without() but special handling for CONJ sub-events
*/
nar.on(Functor.f2((Atom) $.the("conjWithout"), (Term conj, Term event) -> {
if (conj.op() != CONJ || conj.impossibleSubTerm(event))
return Null;
FasterList<LongObjectPair<Term>> events = Conj.eventList(conj);
IntArrayList found = new IntArrayList(2);
int es = events.size();
assert (es > 1);
for (int i = 0; i < es; i++) {
if (event.equalsRoot(events.get(i).getTwo())) {
found.add(i);
}
}
int fs = found.size(), r;
switch(fs) {
case 0:
return Null;
case 1:
r = found.get(0);
break;
default:
r = found.get(nar.random().nextInt(fs));
break;
}
events.remove(r);
return Conj.conj(events);
// } else {
// return nullToNull(Op.without(conj, event::equalsRoot, nar.random()));
// }
}));
/**
* extracts only the events preceding the specified events
*/
nar.on(Functor.f2((Atom) $.the("conjDropIfLatest"), (Term conj, Term event) -> Conj.conjDrop(conj, event, false)));
nar.on(Functor.f2((Atom) $.the("conjDropIfEarliest"), (Term conj, Term event) -> Conj.conjDrop(conj, event, true)));
nar.on(Functor.f1Concept("belief", nar, (c, n) -> $.quote(n.belief(c, n.time()))));
nar.on(Functor.f1Concept("goal", nar, (c, n) -> $.quote(n.goal(c, n.time()))));
nar.on(f0("self", nar::self));
nar.on(Functor.f1("the", what -> {
if (what instanceof Atom) {
switch(what.toString()) {
case "sys":
return $.p($.quote(nar.emotion.summary()), $.quote(nar.concepts.summary()), $.quote(nar.emotion.summary()), $.quote(nar.exe.toString()));
}
}
Object x = nar.concept(what);
if (x == null)
x = what;
return $.quote($.p($.quote(x.getClass().toString()), $.quote(x.toString())));
}));
// /** slice(<compound>,<selector>)
// selector :-
// a specific integer value index, from 0 to compound size
// (a,b) pair of integers, a range of indices */
nar.on(Functor.f("slice", (args) -> {
if (args.subs() == 2) {
Term x = args.sub(0);
if (x.subs() > 0) {
int len = x.subs();
Term index = args.sub(1);
Op o = index.op();
if (o == INT) {
// specific index
int i = ((Int) index).id;
if (i >= 0 && i < len)
return x.sub(i);
else
return False;
} else if (o == PROD && index.subs() == 2) {
Term start = (index).sub(0);
if (start.op() == INT) {
Term end = (index).sub(1);
if (end.op() == INT) {
int si = ((Int) start).id;
if (si >= 0 && si < len) {
int ei = ((Int) end).id;
if (ei >= 0 && ei <= len) {
if (si == ei)
return Op.EmptyProduct;
if (si < ei) {
return $.p(Arrays.copyOfRange(x.subterms().arrayClone(), si, ei));
}
}
}
// TODO maybe reverse order will return reversed subproduct
return False;
}
}
}
}
}
return null;
}));
}
use of org.eclipse.collections.api.tuple.Pair in project eclipse-collections by eclipse.
the class AbstractSortedSetTestCase method zipWithIndex.
@Override
@Test
public void zipWithIndex() {
super.zipWithIndex();
MutableSortedSet<Integer> integers = this.newWith(Collections.reverseOrder(), 1, 3, 5, 2, 4);
Iterator<Pair<Integer, Integer>> zip = integers.zipWithIndex().iterator();
for (int i = 5; i > 0; --i) {
Assert.assertEquals(Tuples.pair(i, 5 - i), zip.next());
}
Person john = new Person("John", "Smith");
Person jane = new Person("Jane", "Smith");
Person johnDoe = new Person("John", "Doe");
MutableSortedSet<Person> people = this.newWith(Collections.reverseOrder(), john, johnDoe, jane);
MutableSortedSet<Pair<Person, Integer>> pairs = people.zipWithIndex();
Verify.assertListsEqual(Lists.mutable.with(Tuples.pair(john, 0), Tuples.pair(johnDoe, 1)), pairs.toList());
}
use of org.eclipse.collections.api.tuple.Pair in project eclipse-collections by eclipse.
the class AbstractSortedSetTestCase method zip.
@Override
@Test
public void zip() {
super.zip();
MutableSortedSet<Integer> revInt = this.newWith(Collections.reverseOrder(), 2, 3, 5, 1, 4);
MutableSortedSet<Integer> integers = this.newWith(1, 3, 2, 4, 5);
MutableList<Pair<Integer, Integer>> zip = integers.zip(revInt);
MutableList<Pair<Integer, Integer>> revZip = revInt.zip(integers);
Verify.assertSize(5, zip);
Verify.assertSize(5, revZip);
Iterator<Pair<Integer, Integer>> zipItr = zip.iterator();
Iterator<Pair<Integer, Integer>> revZipItr = revZip.iterator();
for (int i = 1; i < 6; ++i) {
Assert.assertEquals(Tuples.pair(i, 6 - i), zipItr.next());
Assert.assertEquals(Tuples.pair(6 - i, i), revZipItr.next());
}
Person john = new Person("John", "Smith");
Person jane = new Person("Jane", "Smith");
Person johnDoe = new Person("John", "Doe");
MutableSortedSet<Person> people = this.newWith(john, johnDoe);
MutableList<Holder> list = Lists.mutable.with(new Holder(1), new Holder(2), new Holder(3));
MutableList<Pair<Person, Holder>> pairs = people.zip(list);
Assert.assertEquals(Lists.mutable.with(Tuples.pair(johnDoe, new Holder(1)), Tuples.pair(john, new Holder(2))), pairs.toList());
Assert.assertTrue(pairs.add(Tuples.pair(new Person("Jack", "Baker"), new Holder(3))));
Assert.assertEquals(Tuples.pair(new Person("Jack", "Baker"), new Holder(3)), pairs.getLast());
}
Aggregations