use of jcog.event.On 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());
}
use of jcog.event.On in project narchy by automenta.
the class PhyWall method snake.
protected Snake snake(Wire wire, Runnable onRemove) {
Surface source = wire.a;
Surface target = wire.b;
assert (source != target);
float sa = source.bounds.area();
float ta = target.bounds.area();
float areaDiff = Math.abs(sa - ta) / (sa + ta);
// heuristic estimate: larger area difference = shorter snake
int segments = Util.lerp(areaDiff, 8, 6);
float EXPAND_SCALE_FACTOR = 4;
PushButton deleteButton = new PushButton("x");
Surface menu = new TabPane(ButtonSet.Mode.Multi, Map.of("o", () -> new Gridding(new Label(source.toString()), new Label(target.toString()), deleteButton)), (l) -> new CheckBox(l) {
@Override
protected String label(String text, boolean on) {
// override just display the 'o'
return text;
}
@Override
public ToggleButton set(boolean expanded) {
super.set(expanded);
synchronized (wire) {
PhyWindow w = parent(PhyWindow.class);
if (w == null)
return this;
float cx = w.cx();
float cy = w.cy();
float ww, hh;
if (expanded) {
// grow
ww = w.w() * EXPAND_SCALE_FACTOR;
hh = w.h() * EXPAND_SCALE_FACTOR;
} else {
// shrink
ww = w.w() / EXPAND_SCALE_FACTOR;
hh = w.h() / EXPAND_SCALE_FACTOR;
}
w.pos(cx - ww / 2, cy - hh / 2, cx + ww / 2, cy + hh / 2);
}
return this;
}
});
PhyWindow menuBody = put(menu, RectFloat2D.mid(source.bounds, target.bounds, 0.1f));
float mw = menuBody.radius();
Snake s = new Snake(source, target, segments, 1.618f * 2 * mw, mw) {
@Override
public void remove() {
onRemove.run();
super.remove();
}
};
s.attach(menuBody.body, segments / 2 - 1);
deleteButton.click(s::remove);
int jj = 0;
for (Joint j : s.joints) {
float p = ((float) jj) / (segments - 1);
// custom joint renderer: color coded indicate activity and type of data
j.setData((ObjectLongProcedure<GL2>) (g, now) -> {
int TIME_DECAY_MS = 250;
boolean side = p < 0.5f;
float activity = wire.activity(side, now, TIME_DECAY_MS);
// Util.lerp(p, wire.activity(false, now, TIME_DECAY_MS), wire.activity(true, now, TIME_DECAY_MS));
int th = wire.typeHash(side);
if (th == 0) {
g.glColor4f(0.5f, 0.5f, 0.5f, 0.5f);
} else {
Draw.colorHash(g, th, 0.9f, 0.5f + 0.5f * activity, 0.5f + 0.4f * activity);
}
g.glLineWidth(10f + activity * 10f);
// Draw.line(g, w.a.cx(), w.a.cy(), w.b.cx(), w.b.cy());
// return;
});
jj++;
}
return s;
}
Aggregations