use of nars.concept.action.ActionConcept in project narchy by automenta.
the class NAct method actionTriStatePWM.
@Nullable
default ActionConcept actionTriStatePWM(@NotNull Term s, @NotNull IntConsumer i) {
ActionConcept m = new GoalActionConcept(s, this, (b, d) -> {
int ii;
if (d == null) {
ii = 0;
} else {
float f = d.freq();
if (f == 1f) {
ii = +1;
} else if (f == 0) {
ii = -1;
} else if (f > 0.5f) {
ii = nar().random().nextFloat() <= ((f - 0.5f) * 2f) ? +1 : 0;
} else if (f < 0.5f) {
ii = nar().random().nextFloat() <= ((0.5f - f) * 2f) ? -1 : 0;
} else
ii = 0;
}
i.accept(ii);
float f;
switch(ii) {
case 1:
f = 1f;
break;
case 0:
f = 0.5f;
break;
case -1:
f = 0f;
break;
default:
throw new RuntimeException();
}
return // d!=null ?
$.t(f, // d.conf()
nar().confDefault(BELIEF));
});
return addAction(m);
}
use of nars.concept.action.ActionConcept 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