Search in sources :

Example 1 with BeliefTable

use of nars.table.BeliefTable in project narchy by automenta.

the class Premise method match.

/**
 * resolve the most relevant belief of a given term/concept
 * <p>
 * patham9 project-eternalize
 * patham9 depending on 4 cases
 * patham9 https://github.com/opennars/opennars2/blob/a143162a559e55c456381a95530d00fee57037c4/src/nal/deriver/projection_eternalization.clj
 * sseehh__ ok ill add that in a bit
 * patham9 you need  project-eternalize-to
 * sseehh__ btw i disabled immediate eternalization entirely
 * patham9 so https://github.com/opennars/opennars2/blob/a143162a559e55c456381a95530d00fee57037c4/src/nal/deriver/projection_eternalization.clj#L31
 * patham9 especially try to understand the "temporal temporal" case
 * patham9 its using the result of higher confidence
 * <p>
 * returns ttl used, -1 if failed before starting
 *
 * @param matchTime - temporal focus control: determines when a matching belief or answer should be projected to
 */
@Nullable
public Derivation match(Derivation d, long[] focus, int matchTTL) {
    NAR n = d.nar;
    if (task == null || task.isDeleted()) {
        // }
        return null;
    }
    // n.conceptualize(task.term(), (c)->{});
    int dur = d.dur;
    Term beliefTerm = term();
    Term taskTerm = task.term();
    Term taskConcept = task.term().concept();
    final boolean[] beliefConceptCanAnswerTaskConcept = { false };
    boolean unifiedBelief = false;
    Op to = taskTerm.op();
    Op bo = beliefTerm.op();
    if (to == bo) {
        if (taskConcept.equals(beliefTerm.concept())) {
            beliefConceptCanAnswerTaskConcept[0] = true;
        } else {
            if ((bo.conceptualizable) && (beliefTerm.hasAny(var) || taskTerm.hasAny(var))) {
                Term _beliefTerm = beliefTerm;
                final Term[] unifiedBeliefTerm = new Term[] { null };
                UnifySubst u = new UnifySubst(/*null*/
                VAR_QUERY, n, (y) -> {
                    if (y.op().conceptualizable) {
                        y = y.normalize();
                        beliefConceptCanAnswerTaskConcept[0] = true;
                        if (!y.equals(_beliefTerm)) {
                            unifiedBeliefTerm[0] = y;
                            // stop
                            return false;
                        }
                    }
                    // keep going
                    return true;
                }, matchTTL);
                u.varSymmetric = true;
                u.varCommonalize = true;
                u.unify(taskTerm, beliefTerm, true);
                if (unifiedBeliefTerm[0] != null) {
                    beliefTerm = unifiedBeliefTerm[0];
                    unifiedBelief = true;
                }
            }
        }
    }
    // HACK ?? assert(beliefTerm.op()!=NEG);
    beliefTerm = beliefTerm.unneg();
    // QUESTION ANSWERING and TERMLINK -> TEMPORALIZED BELIEF TERM projection
    Task belief = null;
    // float timeFocus = n.timeFocus.floatValue();
    // int fRad = Math.round(Math.max(1,dur * timeFocus));
    final Concept beliefConcept = beliefTerm.op().conceptualizable ? // conceptualize in case of dynamic concepts
    n.conceptualize(beliefTerm) : null;
    if (beliefConcept != null) {
        if (!beliefTerm.hasVarQuery()) {
            // doesnt make sense to look for a belief in a term with query var, it will have none
            final BeliefTable bb = beliefConcept.beliefs();
            if (task.isQuestOrQuestion()) {
                if (beliefConceptCanAnswerTaskConcept[0]) {
                    final BeliefTable answerTable = (task.isGoal() || task.isQuest()) ? beliefConcept.goals() : bb;
                    // //see if belief unifies with task (in reverse of previous unify)
                    // if (questionTerm.varQuery() == 0 || (unify((Compound)beliefConcept.term(), questionTerm, nar) == null)) {
                    // 
                    // } else {
                    // 
                    // }
                    Task match = answerTable.answer(task.start(), task.end(), dur, task, beliefTerm, n, d::add);
                    if (match != null) {
                        assert (task.isQuest() || match.punc() == BELIEF) : "quest answered with a belief but should be a goal";
                        @Nullable Task answered = task.onAnswered(match, n);
                        if (answered != null) {
                            n.emotion.onAnswer(task, answered);
                        }
                        if (match.isBelief()) {
                            belief = match;
                        }
                    }
                }
            }
            if ((belief == null) && !bb.isEmpty()) {
                belief = bb.match(focus[0], focus[1], beliefTerm, n, taskConcept.equals(beliefConcept.term()) ? x -> !x.equals(task) : null);
            }
        }
        if (unifiedBelief) {
            Concept originalBeliefConcept = n.conceptualize(term());
            if (originalBeliefConcept != null)
                linkVariable(originalBeliefConcept, beliefConcept);
        }
    }
    if (belief != null) {
        // use the belief's actual possibly-temporalized term
        beliefTerm = belief.term().unneg();
    // if (belief.equals(task)) { //do not repeat the same task for belief
    // belief = null; //force structural transform; also prevents potential inductive feedback loop
    // }
    }
    if (beliefTerm instanceof Bool) {
        // logger.warn("{} produced Bool beliefTerm", this);
        return null;
    }
    if (!d.reset().proto(task, belief, beliefTerm))
        return null;
    return d;
}
Also used : Concept(nars.concept.Concept) Op(nars.Op) Task(nars.Task) Term(nars.term.Term) Bool(nars.term.atom.Bool) BeliefTable(nars.table.BeliefTable) UnifySubst(nars.term.subst.UnifySubst) NAR(nars.NAR) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with BeliefTable

use of nars.table.BeliefTable in project narchy by automenta.

the class AutoConceptualizer method update.

protected void update(NAR n) {
    long now = n.time();
    float[] x = this.x;
    int inputs = in.size();
    for (int i = 0, inSize = inputs; i < inSize; i++) {
        Concept xx = in.get(i);
        Truth t = ((BeliefTable) xx.table(beliefOrGoal ? BELIEF : GOAL)).truth(now, n);
        float f;
        if (t == null) {
            f = 0.5f;
        // 0.5f + (learningRate * 2) * n.random().nextFloat() - learningRate;
        // n.random()
        } else {
            f = t.freq();
        }
        x[i] = f;
    }
    // System.out.println(n4(x));
    float err = ae.put(x, learningRate, noiseLevel, 0, true);
    // System.out.println("err=" + n4(err/inputs) + ": \t" + n4(ae.y));
    // decompile/unfactor the outputs
    // if err < thresh
    int outputs = ae.outputs();
    float[] b = new float[outputs];
    float thresh = n.freqResolution.floatValue();
    int[] order = new int[inputs];
    for (int i = 0; i < outputs; i++) {
        // basis vector for each output
        b[i] = 1;
        float[] a = ae.decode(b, true);
        // System.out.println("\tfeature " + i + "=" + n4(a));
        Term feature = conj(order, a, /* threshold, etc */
        5, /*a.length/2*/
        thresh);
        if (feature != null) {
            // System.out.println("\t  " + feature);
            onFeature(feature);
        }
        // clear
        b[i] = 0;
    }
}
Also used : Concept(nars.concept.Concept) BeliefTable(nars.table.BeliefTable) Term(nars.term.Term) Truth(nars.truth.Truth)

Example 3 with BeliefTable

use of nars.table.BeliefTable in project narchy by automenta.

the class BeliefAnalysis method print.

public void print(boolean beliefOrGoal) {
    BeliefTable table = table(beliefOrGoal);
    System.out.println((beliefOrGoal ? "Beliefs" : "Goals") + "[@" + nar.time() + "] " + table.size() + '/' + table.capacity());
    table.print(System.out);
// System.out.println();
}
Also used : BeliefTable(nars.table.BeliefTable)

Example 4 with BeliefTable

use of nars.table.BeliefTable in project narchy by automenta.

the class ImplicationNetworkTest method testEternal_A_PosBelief_ToBC.

@Test
public void testEternal_A_PosBelief_ToBC() {
    NAR n = NARS.tmp();
    Param.DEBUG = true;
    n.believe(IMPL.the(a, b));
    n.believe(IMPL.the(b, c));
    n.believe(a);
    n.run(100);
    BeliefTable aBeliefs = n.concept(a).beliefs();
    Truth aBelief = aBeliefs.truth(ETERNAL, n);
    // n.concept(a).print();
    // a belief state should not exceed the input (default confidence) and freq remain stable
    // additional beliefs are not helpful
    // assertEquals(1, aBeliefs.size());
    BeliefTable bBeliefs = n.concept(b).beliefs();
    Truth bBelief = bBeliefs.truth(ETERNAL, n);
    // n.concept(b).print();
    // assertEquals(1, bBeliefs.size());
    // b should have less conf than a but higher than c
    // same freq among all
    Truth cBelief = n.concept(c).beliefs().truth(ETERNAL, n);
    // n.concept(c).print();
    System.out.println("a: " + aBelief);
    System.out.println("b: " + bBelief);
    System.out.println("c: " + cBelief);
    assertEquals(aBelief.freq(), bBelief.freq(), n.freqResolution.floatValue());
    assertEquals(bBelief.freq(), cBelief.freq(), n.freqResolution.floatValue());
    assertTrue(aBelief.conf() - bBelief.conf() > n.confResolution.floatValue() * 2);
    assertTrue(bBelief.conf() - cBelief.conf() > n.confResolution.floatValue() * 2);
}
Also used : BeliefTable(nars.table.BeliefTable) NAR(nars.NAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Example 5 with BeliefTable

use of nars.table.BeliefTable in project narchy by automenta.

the class ImplicationNetworkTest method testEternal_A_NegBelief_NegToBC.

@Test
public void testEternal_A_NegBelief_NegToBC() {
    NAR n = NARS.tmp(6);
    n.termVolumeMax.set(16);
    Param.DEBUG = true;
    n.believe(IMPL.the(a.neg(), b));
    n.believe(IMPL.the(b, c));
    n.believe(a.neg());
    n.run(800);
    BeliefTable aa = n.concept(a).beliefs();
    BeliefTable bb = n.concept(b).beliefs();
    BeliefTable cc = n.concept(c).beliefs();
    aa.print();
    bb.print();
    // cc.print();
    cc.forEachTask(x -> System.out.println(x.proof()));
    // assertEquals(1, aBeliefs.size());
    Truth bBelief = bb.truth(ETERNAL, n);
    Truth cBelief = cc.truth(ETERNAL, n);
    assertEquals("%1.0;.81%", bBelief.toString());
    assertEquals("%1.0;.73%", cBelief.toString());
}
Also used : BeliefTable(nars.table.BeliefTable) NAR(nars.NAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Aggregations

BeliefTable (nars.table.BeliefTable)17 Truth (nars.truth.Truth)12 Test (org.junit.jupiter.api.Test)12 NAR (nars.NAR)10 Term (nars.term.Term)5 TestNAR (nars.test.TestNAR)3 NotNull (org.jetbrains.annotations.NotNull)3 Concept (nars.concept.Concept)2 TaskConcept (nars.concept.TaskConcept)2 DynamicTruthBeliefTable (nars.concept.dynamic.DynamicTruthBeliefTable)2 BeliefAnalysis (nars.test.analyze.BeliefAnalysis)2 Collectors.toList (java.util.stream.Collectors.toList)1 Texts.n4 (jcog.Texts.n4)1 MultiStatistics (jcog.math.MultiStatistics)1 CSVOutput (jcog.meter.event.CSVOutput)1 nars (nars)1 Op (nars.Op)1 BELIEF (nars.Op.BELIEF)1 Task (nars.Task)1 RTreeBeliefTable (nars.table.RTreeBeliefTable)1