Search in sources :

Example 1 with ActionConcept

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);
}
Also used : GoalActionConcept(nars.concept.action.GoalActionConcept) GoalActionConcept(nars.concept.action.GoalActionConcept) ActionConcept(nars.concept.action.ActionConcept) BeliefActionConcept(nars.concept.action.BeliefActionConcept) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ActionConcept

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());
}
Also used : Iterables(com.google.common.collect.Iterables) DurService(nars.control.DurService) java.util(java.util) Bitmap2DSensor(nars.util.signal.Bitmap2DSensor) LoggerFactory(org.slf4j.LoggerFactory) Scalar(nars.concept.scalar.Scalar) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ETERNAL(nars.time.Tense.ETERNAL) Supplier(java.util.function.Supplier) Variable(nars.term.var.Variable) Truth(nars.truth.Truth) Tuples.pair(org.eclipse.collections.impl.tuple.Tuples.pair) Op(nars.Op) ActionConcept(nars.concept.action.ActionConcept) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) On(jcog.event.On) Activate(nars.control.Activate) FloatFloatToObjectFunction(org.eclipse.collections.api.block.function.primitive.FloatFloatToObjectFunction) Concept(nars.concept.Concept) Term(nars.term.Term) Util.compose(jcog.Util.compose) Loop(jcog.exe.Loop) Logger(org.slf4j.Logger) Texts.n2(jcog.Texts.n2) Predicate(java.util.function.Predicate) FasterList(jcog.list.FasterList) Stamp(nars.truth.Stamp) NARService(nars.control.NARService) ITask(nars.task.ITask) NALTask(nars.task.NALTask) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) TODO(jcog.TODO) FilteredScalar(nars.concept.scalar.FilteredScalar) Stream(java.util.stream.Stream) Atomic(nars.term.atom.Atomic) jcog.math(jcog.math) Termed(nars.term.Termed) CauseChannel(nars.control.CauseChannel) NotNull(org.jetbrains.annotations.NotNull) DigitizedScalar(nars.concept.scalar.DigitizedScalar) ITask(nars.task.ITask) CauseChannel(nars.control.CauseChannel) Truth(nars.truth.Truth) ActionConcept(nars.concept.action.ActionConcept)

Aggregations

ActionConcept (nars.concept.action.ActionConcept)2 Nullable (org.jetbrains.annotations.Nullable)2 Iterables (com.google.common.collect.Iterables)1 java.util (java.util)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1 Supplier (java.util.function.Supplier)1 Stream (java.util.stream.Stream)1 TODO (jcog.TODO)1 Texts.n2 (jcog.Texts.n2)1 Util.compose (jcog.Util.compose)1 On (jcog.event.On)1 Loop (jcog.exe.Loop)1 FasterList (jcog.list.FasterList)1 jcog.math (jcog.math)1 Op (nars.Op)1 Concept (nars.concept.Concept)1 BeliefActionConcept (nars.concept.action.BeliefActionConcept)1