Search in sources :

Example 41 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class Implier method imply.

private void imply(Task impl) {
    if (ArrayUtils.indexOf(impl.cause(), in.id) != -1)
        // avoid cyclical logic by any implications at least partially resulting from this cause
        return;
    int implDT = impl.dt();
    if (implDT == DTERNAL)
        implDT = 0;
    Term implTerm = impl.term();
    Term S = implTerm.sub(0);
    boolean Sneg = false;
    if (S.op() == NEG) {
        Sneg = true;
        S = S.unneg();
    }
    Term P = implTerm.sub(1);
    /*
        C = condition
        S = subj
        P = pred
        */
    {
        /*
            S, (S ==> P) |- P (Belief:Deduction)
            P_belief(C,S) = ded(S_belief, impl_belief)
         */
        long when = then;
        Truth implTruth = impl.truth(when, dur, nar);
        if (implTruth != null) {
            Truth S_belief = nar.beliefTruth(S, when);
            if (S_belief != null) {
                if (implTruth.isPositive()) {
                    // IMPL+
                    Truth P_belief_pos = ded.apply(S_belief.negIf(Sneg), implTruth, nar, Float.MIN_NORMAL);
                    if (P_belief_pos != null)
                        believe(P, then + implDT, P_belief_pos);
                } else {
                    // IMPL-
                    Truth P_belief_neg = ded.apply(S_belief.negIf(Sneg), implTruth.neg(), nar, Float.MIN_NORMAL);
                    if (P_belief_neg != null)
                        believe(P, then + implDT, P_belief_neg.neg());
                }
            }
        }
    }
    {
        /*
            S, (S ==> P) |- P (Goal:Induction)
            P_goal(C,S) = ind(S_goal, impl_belief)
         */
        long when = then;
        Truth implTruth = impl.truth(when, dur, nar);
        if (implTruth != null) {
            Truth S_goal = nar.goalTruth(S, when);
            if (S_goal != null) {
                if (implTruth.isPositive()) {
                    // IMPL+
                    Truth P_goal_pos = ind.apply(S_goal.negIf(Sneg), implTruth, nar, Float.MIN_NORMAL);
                    if (P_goal_pos != null)
                        goal(P, then + implDT, P_goal_pos);
                } else {
                    // IMPL-
                    Truth P_goal_neg = ind.apply(S_goal.negIf(Sneg), implTruth.neg(), nar, Float.MIN_NORMAL);
                    if (P_goal_neg != null)
                        goal(P, then + implDT, P_goal_neg.neg());
                }
            }
        }
    }
    {
        /*
            P, (S ==> P) |- S (Goal:Deduction)
            S_goal(C,S) = ded(P_goal, impl_belief)
         */
        long when = then;
        Truth implTruth = impl.truth(when, dur, nar);
        if (implTruth != null) {
            Truth P_goal = nar.goalTruth(P, when + implDT);
            if (P_goal != null) {
                if (implTruth.isPositive()) {
                    // IMPL+
                    Truth S_goal_pos = ded.apply(P_goal, implTruth, nar, Float.MIN_NORMAL);
                    if (S_goal_pos != null)
                        goal(P, then, S_goal_pos);
                } else {
                    // IMPL-
                    Truth S_goal_neg = ded.apply(P_goal.neg(), implTruth.neg(), nar, Float.MIN_NORMAL);
                    if (S_goal_neg != null)
                        goal(P, then, S_goal_neg);
                }
            }
        }
    }
// TODO
// P,   (S ==> P) |-   S (Belief:Abduction)
// P, (--S ==> P) |- --S (Belief:Abduction)
// x P,   (S ==> P) |-   S (Goal:Deduction)  ^^above
// P, (--S ==> P) |- --S (Goal:Deduction)
}
Also used : Term(nars.term.Term) Truth(nars.truth.Truth)

Example 42 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class EternalTable method tryRevision.

/**
 * @return null: no revision could be applied
 * ==newBelief: existing duplicate found
 * non-null: revised task
 */
@Nullable
private /*Revision*/
Task tryRevision(/*@NotNull*/
Task y, /* input */
@Nullable NAR nar) {
    Object[] list = this.list;
    int bsize = list.length;
    if (bsize == 0)
        // nothing to revise with
        return null;
    // Try to select a best revision partner from existing beliefs:
    Task oldBelief = null;
    Truth conclusion = null;
    Truth newBeliefTruth = y.truth();
    for (int i = 0; i < bsize; i++) {
        Task x = (Task) list[i];
        if (// the array has trailing nulls from having extra capacity
        x == null)
            break;
        if (x.equals(y)) {
            /*if (x!=y && x.isInput())
                    throw new RuntimeException("different input task instances with same stamp");*/
            return x;
        }
        // same conf, same stamp, both non-cyclic; interpolate to avoid one being preferred over another arbitrarily
        float xconf = x.conf();
        if (Util.equals(xconf, y.conf(), nar.confResolution.floatValue()) && (!x.isCyclic() && !y.isCyclic()) && Arrays.equals(x.stamp(), y.stamp())) {
            conclusion = new PreciseTruth(0.5f * (x.freq() + y.freq()), xconf);
        } else if (Stamp.overlapping(y, x)) {
            boolean FILTER_WEAKER_BUT_EQUAL = false;
            if (FILTER_WEAKER_BUT_EQUAL && !y.isInput() && xconf >= y.conf() && Util.equals(x.freq(), y.freq(), nar.freqResolution.floatValue()) && Arrays.equals(y.stamp(), x.stamp())) {
                y.delete();
                // subsume by stronger belief with same freq and stamp
                return null;
            }
            // unrevisable
            continue;
        } else {
            // 
            // float factor = tRel * freqMatch;
            // if (factor < best) {
            // //even with conf=1.0 it wouldnt be enough to exceed existing best match
            // continue;
            // }
            // float minValidConf = Math.min(newBeliefConf, x.conf());
            // if (minValidConf < bestConf) continue;
            // float minValidRank = BeliefTable.rankEternalByOriginality(minValidConf, totalEvidence);
            // if (minValidRank < bestRank) continue;
            Truth xt = x.truth();
            // TODO use overlappingFraction?
            Truth yt = Revision.revise(newBeliefTruth, xt, 1f, conclusion == null ? 0 : conclusion.evi());
            if (yt == null)
                continue;
            yt = yt.dither(nar);
            if (// //avoid a weak or duplicate truth
            yt == null || yt.equals(xt, nar) || yt.equals(newBeliefTruth, nar))
                continue;
            conclusion = yt;
        }
        oldBelief = x;
    }
    if (oldBelief == null)
        return null;
    final float newBeliefWeight = y.evi();
    // TODO use Task.tryContent in building the task:
    float aProp = newBeliefWeight / (newBeliefWeight + oldBelief.evi());
    Term t = Revision.intermpolate(y.term(), oldBelief.term(), aProp, nar);
    Task prevBelief = oldBelief;
    Task x = Task.tryTask(t, y.punc(), conclusion, (term, revisionTruth) -> new NALTask(term, y.punc(), revisionTruth, nar.time(), /* creation time */
    ETERNAL, ETERNAL, Stamp.zip(prevBelief.stamp(), y.stamp(), 0.5f)));
    if (x != null) {
        x.priSet(Priority.fund(Math.max(prevBelief.priElseZero(), y.priElseZero()), false, prevBelief, y));
        ((NALTask) x).cause = Cause.sample(Param.causeCapacity.intValue(), y, prevBelief);
        if (Param.DEBUG)
            x.log("Insertion Revision");
    // ((NALTask)y).meta("@",x);
    // ((NALTask)prevBelief).meta("@",x);
    }
    return x;
}
Also used : NALTask(nars.task.NALTask) Task(nars.Task) PreciseTruth(nars.truth.PreciseTruth) NALTask(nars.task.NALTask) Term(nars.term.Term) Truth(nars.truth.Truth) PreciseTruth(nars.truth.PreciseTruth) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class TruthPolation method truth.

public Truth truth(boolean filterCyclic) /*float eviFactor, float eviMin*/
{
    if (isEmpty())
        return null;
    // project temporally, removing if no evidence provided
    {
        removeIf(tc -> {
            Task t = tc.task;
            Truth tt = t.truth(start, end, dur, Truth.EVI_MIN);
            if (tt != null) {
                // not necessarily the task's reported "average" freq in case of Truthlets
                tc.freq = tt.freq();
                tc.evi = tt.evi();
                return false;
            } else {
                // removed
                return true;
            }
        });
    }
    // remove overlapping evidence, preferring the strongest contributors of each
    if (filterCyclic) {
        int s = size();
        if (s == 0)
            return null;
        else if (s > 1) {
            // descending by strength
            sortThisByFloat(tc -> -tc.evi);
            // TODO maybe factor in originality to reduce overlap so evidence can be combined better
            // remove the weaker holder of any overlapping evidence
            LongHashSet e = new LongHashSet(s * 2);
            removeIf(tc -> {
                long[] stamp = tc.task.stamp();
                for (long ss : stamp) {
                    if (!e.add(ss))
                        // overlap
                        return true;
                }
                return false;
            });
        }
    }
    {
        int s = size();
        switch(s) {
            case 0:
                return null;
            case 1:
                {
                    TaskComponent only = get(0);
                    return new PreciseTruth(only.freq, only.evi, false);
                }
            default:
                {
                    // interpolate
                    // float eviSum = 0, confSum = 0, wFreqSum = 0;
                    float eviSum = 0, wFreqSum = 0;
                    for (int i = 0, thisSize = this.size(); i < thisSize; i++) {
                        TaskComponent x = this.get(i);
                        float ee = x.evi;
                        eviSum += ee;
                        // float ce = w2cSafe(ee);
                        // confSum += ce;
                        // wFreqSum += ce * x.freq;
                        wFreqSum += ee * x.freq;
                    }
                    assert (Float.isFinite(eviSum));
                    float c = w2cSafe(eviSum);
                    if (c < Param.TRUTH_EPSILON)
                        return null;
                    else {
                        // float f = (wFreqSum / confSum);
                        float f = (wFreqSum / eviSum);
                        return new PreciseTruth(f, c);
                    }
                }
        }
    }
}
Also used : Param(nars.Param) Nullable(org.jetbrains.annotations.Nullable) Task(nars.Task) TaskRegion(nars.task.util.TaskRegion) Truth(nars.truth.Truth) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) TruthFunctions.w2cSafe(nars.truth.TruthFunctions.w2cSafe) FasterList(jcog.list.FasterList) PreciseTruth(nars.truth.PreciseTruth) NaN(java.lang.Float.NaN) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Task(nars.Task) PreciseTruth(nars.truth.PreciseTruth) Truth(nars.truth.Truth) PreciseTruth(nars.truth.PreciseTruth)

Example 44 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class NAL6MultistepTest method testBurglarEarthquake1.

/**
 * https://dtai.cs.kuleuven.be/problog/tutorial/basic/02_bayes.html
 */
@Test
public void testBurglarEarthquake1() throws Narsese.NarseseException {
    // 0.7::burglary.
    // 0.2::earthquake.
    // 0.9::p_alarm1.
    // 0.8::p_alarm2.
    // 0.1::p_alarm3.
    // 
    // alarm :- burglary, earthquake, p_alarm1.
    // alarm :- burglary, \+earthquake, p_alarm2.
    // alarm :- \+burglary, earthquake, p_alarm3.
    // 
    // evidence(alarm,true).
    // 
    // query(burglary).
    // query(earthquake).
    NAR n = NARS.tmp();
    // d.log();
    n.input("(burglary). %0.7;0.9%", "(earthquake). %0.2;0.9%", "(p_alarm1). %0.9;0.9%", "(p_alarm2). %0.8;0.9%", "(p_alarm3). %0.1;0.9%", "((&&, (burglary), (earthquake), (p_alarm1)) ==> (alarm)). %1.0;0.95%", "((&&, (burglary), (--,(earthquake)), (p_alarm2)) ==> (alarm)). %1.0;0.95%", "((&&, (--,(burglary)), (earthquake), (p_alarm3)) ==> (alarm)). %1.0;0.95%", "(alarm).", "(burglary)?", "(earthquake)?");
    Concept burglary = null, earthquake = null;
    for (int i = 0; i < 5; i++) {
        // long now = d.time();
        n.run(100);
        burglary = n.conceptualize("(burglary)");
        // burglary.print();  earthquake.print();
        earthquake = n.conceptualize("(earthquake)");
    // System.out.println("burglary=" + burglary.belief(Tense.ETERNAL,0, nar) + "\tearthquake=" + earthquake.belief(Tense.ETERNAL,0, nar));
    }
    n.stats(System.out);
    // result from Probcog:  earthquake=23%, burglary=99%
    Truth burgTruth = n.beliefTruth(burglary, Tense.ETERNAL);
    assertNotNull(burgTruth);
    assertEquals(0.99f, burgTruth.freq(), 0.4f);
    assertEquals(0.31f, n.beliefTruth(earthquake, Tense.ETERNAL).freq(), 0.2f);
}
Also used : TaskConcept(nars.concept.TaskConcept) Concept(nars.concept.Concept) NAR(nars.NAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Example 45 with Truth

use of nars.truth.Truth in project narchy by automenta.

the class RevisionTest method testRevision2TemporalImpl.

@Test
public void testRevision2TemporalImpl() throws Narsese.NarseseException {
    NAR n = newNAR(3).input("(x ==> y). :|: %1.0;0.9%", "(x ==> y). :|: %0.0;0.9%");
    n.run(1);
    Concept c = n.concept($.$("(x ==> y)"));
    assertEquals(2, c.beliefs().size());
    Truth t = n.belief($.$("(x ==> y)"), 0).truth();
    assertEquals(0.5f, t.freq(), 0.01f);
    assertEquals(0.947f, t.conf(), 0.01f);
}
Also used : TaskConcept(nars.concept.TaskConcept) Concept(nars.concept.Concept) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Aggregations

Truth (nars.truth.Truth)65 Term (nars.term.Term)31 Test (org.junit.jupiter.api.Test)22 Nullable (org.jetbrains.annotations.Nullable)20 NAR (nars.NAR)18 Task (nars.Task)14 BeliefTable (nars.table.BeliefTable)13 Concept (nars.concept.Concept)9 NALTask (nars.task.NALTask)9 TaskConcept (nars.concept.TaskConcept)7 PreciseTruth (nars.truth.PreciseTruth)7 NotNull (org.jetbrains.annotations.NotNull)7 FasterList (jcog.list.FasterList)6 ITask (nars.task.ITask)6 Tense (nars.time.Tense)6 Util (jcog.Util)5 BELIEF (nars.Op.BELIEF)5 Consumer (java.util.function.Consumer)4 Predicate (java.util.function.Predicate)4 Param (nars.Param)4