use of jcog.TODO in project narchy by automenta.
the class TimeGraph method onAdd.
@Override
protected void onAdd(Node<nars.derive.time.TimeGraph.Event, nars.derive.time.TimeGraph.TimeSpan> x) {
Event event = x.id;
Term eventTerm = event.id;
if (byTerm.put(eventTerm, event)) {
if (autoNeg.test(eventTerm)) {
// link(event, 0, know(eventTerm.neg())); //WEAK
// WEAK
link(know(eventTerm), 0, know(eventTerm.neg()));
}
}
// Term tRoot = eventTerm.root();
// if (!tRoot.equals(eventTerm))
// byTerm.put(tRoot, event);
int edt = eventTerm.dt(), eventDT;
if (edt == DTERNAL && dternalAsZero)
eventDT = 0;
else
eventDT = edt;
switch(eventTerm.op()) {
// break;
case IMPL:
Term subj = eventTerm.sub(0);
Event se = know(subj);
Term pred = eventTerm.sub(1);
Event pe = know(pred);
if (eventDT == DTERNAL) {
link(se, ETERNAL, pe);
subj.eventsWhile((w, y) -> {
link(know(y), ETERNAL, pe);
return true;
}, 0, true, true, false, 0);
pred.eventsWhile((w, y) -> {
link(se, ETERNAL, know(y));
return true;
}, 0, true, true, false, 0);
} else if (eventDT != XTERNAL) {
int st = subj.dtRange();
link(se, (eventDT + st), pe);
subj.eventsWhile((w, y) -> {
link(know(y), eventDT + st - w, pe);
return true;
}, 0, true, true, false, 0);
pred.eventsWhile((w, y) -> {
link(se, eventDT + st + w, know(y));
return true;
}, 0, true, true, false, 0);
}
break;
case CONJ:
// Subterms tt = eventTerm.subterms();
// int s = tt.subs();
// if (et == TIMELESS) {
// //chain the sibling subevents
// if (s == 2) {
// Term se0 = tt.sub(0);
// Event e0 = know(se0);
// Term se1 = tt.sub(1);
// Event e1 = know(se1);
// int dt;
// Event earliest;
// if (eventDT == DTERNAL) {
// dt = DTERNAL;
// earliest = e0; //just use the first by default
// } else {
// long t0 = eventTerm.subTime(se0);
// long t1 = eventTerm.subTime(se1);
// long ddt = (int) (t1 - t0);
// assert (ddt < Integer.MAX_VALUE);
// dt = (int) ddt;
// earliest = t0 < t1 ? e0 : e1;
// }
// link(e0, dt, e1);
// link(earliest, 0, event);
//
// } else {
// throw new TODO();
// }
//
// } else
// locate the events and sub-events absolutely
long et = event.when();
switch(eventDT) {
case XTERNAL:
break;
case DTERNAL:
Subterms es = eventTerm.subterms();
int esn = es.subs();
Term prev = es.sub(0);
for (int i = 1; i < esn; i++) {
// dternal chain
Term next = es.sub(i);
link(knowComponent(et, 0, prev), ETERNAL, knowComponent(et, 0, next));
prev = next;
}
break;
case 0:
// eventTerm.subterms().forEach(this::know); //TODO can these be absolute if the event is?
boolean timed = et != ETERNAL;
for (Term s : eventTerm.subterms()) {
Event t = eventDT == 0 ? // 0
knowComponent(et, 0, s) : (// DTERNAL and TIMED
timed ? // DTERNAL and TIMED
know(s, et) : know(s));
if (t != null) {
link(event, (eventDT == 0 || timed) ? 0 : ETERNAL, // DTERNAL and TIMELESS
t);
} else {
// WHY?
}
}
break;
default:
eventTerm.eventsWhile((w, y) -> {
link(event, w, knowComponent(et, w, y));
return true;
}, 0, false, false, false, 0);
break;
}
break;
}
}
use of jcog.TODO in project narchy by automenta.
the class Bordering method doLayout.
@Override
public void doLayout(int dtMS) {
float X = x();
float Y = y();
float W = w();
float H = h();
float w2, h2;
boolean aspectEqual = true;
if (aspectEqual) {
w2 = h2 = Math.min(W, H) / 2;
} else {
w2 = W / 2;
h2 = H / 2;
}
Surface[] children = children();
for (int i = 0, childrenLength = children.length; i < childrenLength; i++) {
Surface c = children[i];
if (c instanceof EmptySurface)
continue;
float x1, y1, x2, y2;
switch(i) {
case C:
x1 = borderWest * w2;
y1 = borderSouth * h2;
x2 = W - borderEast * w2;
y2 = H - borderNorth * h2;
break;
case N:
x1 = borderWest * w2;
y1 = H - borderNorth * h2;
x2 = W - borderEast * w2;
y2 = H;
break;
case S:
x1 = borderWest * w2;
y1 = 0;
x2 = W - borderEast * w2;
y2 = borderSouth * h2;
break;
case Bordering.W:
x1 = 0;
y1 = borderSouth * h2;
x2 = borderWest * w2;
y2 = H - borderNorth * h2;
break;
case E:
x1 = W - borderEast * w2;
y1 = borderSouth * h2;
x2 = W;
y2 = H - borderNorth * h2;
break;
case NE:
x1 = W - borderEast * w2;
y1 = H - borderNorth * h2;
x2 = W;
y2 = H;
break;
case SW:
x1 = 0;
y1 = 0;
x2 = borderWest * w2;
y2 = borderSouth * h2;
break;
default:
throw new TODO();
}
assert (x2 >= x1 && y2 >= y1);
c.pos(X + x1, Y + y1, X + x2, Y + y2);
}
}
use of jcog.TODO in project narchy by automenta.
the class Splitting method doLayout.
@Override
public void doLayout(int dtMS) {
if (!vertical)
throw new TODO();
float X = x();
float Y = y();
float h = h();
float w = w();
float Ysplit = Y + split * h;
Surface top = T();
if (top != null) {
top.pos(X, Ysplit, X + w, Y + h);
}
Surface bottom = B();
if (bottom != null) {
bottom.pos(X, Y, X + w, Ysplit);
}
super.doLayout(dtMS);
}
use of jcog.TODO 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 jcog.TODO in project narchy by automenta.
the class NAgent method run.
@Override
public void run() {
if (!enabled.get())
return;
this.last = this.now;
this.now = nar.time();
if (now <= last)
return;
// stretched perceptual duration to the NAgent's effective framerate
int dur = Math.max(nar.dur(), (int) (now - last));
reward = act();
happy.update(last, now, dur, nar);
FloatFloatToObjectFunction<Truth> truther = (prev, next) -> $.t(next, nar.confDefault(BELIEF));
sensors.forEach((key, value) -> value.input(key.update(last, now, truther, dur, nar)));
always(motivation.floatValue());
// HACK TODO compile this to re-used array on init like before
Map.Entry<ActionConcept, CauseChannel<ITask>>[] aa = actions.entrySet().toArray(new Map.Entry[actions.size()]);
// fair chance of ordering to all motors
ArrayUtils.shuffle(aa, random());
for (Map.Entry<ActionConcept, CauseChannel<ITask>> ac : aa) {
Stream<ITask> s = ac.getKey().update(last, now, dur, NAgent.this.nar);
if (s != null)
ac.getValue().input(s);
}
Truth happynowT = nar.beliefTruth(happy, last, now);
float happynow = happynowT != null ? (happynowT.freq() - 0.5f) * 2f : 0;
nar.emotion.happy(motivation.floatValue() * dexterity(last, now) * happynow);
if (trace)
logger.info(summary());
}
Aggregations