Search in sources :

Example 46 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class LocalRules method revision.

/**
 * Belief revision
 * <p>
 * called from Concept.reviseTable and match
 *
 * @param newBelief The new belief in task
 * @param oldBelief The previous belief with the same content
 * @param feedbackToLinks Whether to send feedback to the links
 * @param memory Reference to the memory
 */
public static boolean revision(final Sentence newBelief, final Sentence oldBelief, final boolean feedbackToLinks, final DerivationContext nal) {
    if (newBelief.term == null) {
        return false;
    }
    newBelief.stamp.alreadyAnticipatedNegConfirmation = oldBelief.stamp.alreadyAnticipatedNegConfirmation;
    TruthValue newTruth = newBelief.truth.clone();
    TruthValue oldTruth = oldBelief.truth;
    boolean useNewBeliefTerm = false;
    if (newBelief.getTerm().hasInterval()) {
        Term cterm = replaceIntervals(newBelief.getTerm());
        Concept c = nal.memory.concept(cterm);
        ArrayList<Long> ivalOld = extractIntervals(nal.memory, oldBelief.getTerm());
        if (c.recent_intervals.size() == 0) {
            for (Long l : ivalOld) {
                c.recent_intervals.add((float) l);
            }
        }
        ArrayList<Long> ivalNew = extractIntervals(nal.memory, newBelief.getTerm());
        for (int i = 0; i < ivalNew.size(); i++) {
            // vote as one new entry, turtle style
            float Inbetween = (c.recent_intervals.get(i) + ivalNew.get(i)) / 2.0f;
            // less truth expectation, slower
            float speed = 1.0f / (float) (Parameters.INTERVAL_ADAPT_SPEED * (1.0f - newBelief.getTruth().getExpectation()));
            c.recent_intervals.set(i, c.recent_intervals.get(i) + speed * (Inbetween - c.recent_intervals.get(i)));
        }
        long AbsDiffSumNew = 0;
        for (int i = 0; i < ivalNew.size(); i++) {
            AbsDiffSumNew += Math.abs(ivalNew.get(i) - c.recent_intervals.get(i));
        }
        long AbsDiffSumOld = 0;
        for (int i = 0; i < ivalNew.size(); i++) {
            AbsDiffSumOld += Math.abs(ivalOld.get(i) - c.recent_intervals.get(i));
        }
        long AbsDiffSum = 0;
        for (int i = 0; i < ivalNew.size(); i++) {
            AbsDiffSum += Math.abs(ivalNew.get(i) - ivalOld.get(i));
        }
        // re-project, and it's safe:
        float a = temporalProjection(0, AbsDiffSum, 0);
        // we won't count more confidence than
        // when the second premise would have been shifted
        // to the necessary time in the first place
        // to build the hypothesis newBelief encodes
        newTruth.setConfidence(newTruth.getConfidence() * a);
        useNewBeliefTerm = AbsDiffSumNew < AbsDiffSumOld;
    }
    TruthValue truth = TruthFunctions.revision(newTruth, oldTruth);
    BudgetValue budget = BudgetFunctions.revise(newTruth, oldTruth, truth, feedbackToLinks, nal);
    if (budget.aboveThreshold()) {
        if (nal.doublePremiseTaskRevised(useNewBeliefTerm ? newBelief.term : oldBelief.term, truth, budget)) {
            // nal.mem().logic.BELIEF_REVISION.commit();
            return true;
        }
    }
    return false;
}
Also used : Concept(nars.entity.Concept) BudgetValue(nars.entity.BudgetValue) TruthValue(nars.entity.TruthValue) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 47 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class LocalRules method solutionQuality.

/**
 * Evaluate the quality of the judgment as a solution to a problem
 *
 * @param problem A goal or question
 * @param solution The solution to be evaluated
 * @return The quality of the judgment as the solution
 */
public static float solutionQuality(boolean rateByConfidence, final Task probT, final Sentence solution, Memory memory) {
    Sentence problem = probT.sentence;
    if ((probT.sentence.punctuation != solution.punctuation && solution.term.hasVarQuery()) || !matchingOrder(problem.getTemporalOrder(), solution.getTemporalOrder())) {
        return 0.0F;
    }
    TruthValue truth = solution.truth;
    if (problem.getOccurenceTime() != solution.getOccurenceTime()) {
        truth = solution.projectionTruth(problem.getOccurenceTime(), memory.time());
    }
    // so the previous handling to let whether the problem has query vars decide was wrong.
    if (!rateByConfidence) {
        return (float) (truth.getExpectation() / Math.sqrt(Math.sqrt(Math.sqrt(solution.term.getComplexity() * Parameters.COMPLEXITY_UNIT))));
    } else {
        return truth.getConfidence();
    }
}
Also used : TruthValue(nars.entity.TruthValue) Sentence(nars.entity.Sentence)

Example 48 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class LocalRules method inferToAsym.

/**
 * {<S <-> P>, <P --> S>} |- <S --> P> Produce an Inheritance/Implication
 * from a Similarity/Equivalence and a reversed Inheritance/Implication
 *
 * @param asym The asymmetric premise
 * @param sym The symmetric premise
 * @param nal Reference to the memory
 */
private static void inferToAsym(Sentence asym, Sentence sym, DerivationContext nal) {
    Statement statement = (Statement) asym.term;
    Term sub = statement.getPredicate();
    Term pre = statement.getSubject();
    Statement content = Statement.make(statement, sub, pre, statement.getTemporalOrder());
    if (content == null)
        return;
    TruthValue truth = TruthFunctions.reduceConjunction(sym.truth, asym.truth);
    BudgetValue budget = BudgetFunctions.forward(truth, nal);
    nal.doublePremiseTask(content, truth, budget, false, false);
}
Also used : BudgetValue(nars.entity.BudgetValue) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 49 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class RuleTables method goalFromWantBelief.

public static void goalFromWantBelief(final Task task, final short tIndex, short bIndex, final Term taskTerm, final DerivationContext nal, Term beliefTerm) {
    if (task.sentence.isJudgment() && tIndex == 0 && bIndex == 1 && taskTerm instanceof Operation) {
        Operation op = (Operation) taskTerm;
        if (op.getPredicate() == nal.memory.getOperator("^want")) {
            TruthValue newTruth = TruthFunctions.deduction(task.sentence.truth, Parameters.reliance);
            nal.singlePremiseTask(beliefTerm, Symbols.GOAL_MARK, newTruth, BudgetFunctions.forward(newTruth, nal));
        }
    }
}
Also used : TruthValue(nars.entity.TruthValue) Operation(nars.operator.Operation)

Example 50 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class RuleTables method goalFromQuestion.

private static void goalFromQuestion(final Task task, final Term taskTerm, final DerivationContext nal) {
    if (task.sentence.punctuation == Symbols.QUESTION_MARK && (taskTerm instanceof Implication || taskTerm instanceof Equivalence)) {
        // <a =/> b>? |- a!
        Term goalterm = null;
        Term goalterm2 = null;
        if (taskTerm instanceof Implication) {
            Implication imp = (Implication) taskTerm;
            if (imp.getTemporalOrder() != TemporalRules.ORDER_BACKWARD || imp.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT) {
                if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getSubject() instanceof Operation) {
                    goalterm = imp.getSubject();
                }
                if (goalterm instanceof Variable && goalterm.hasVarQuery() && (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getPredicate() instanceof Operation)) {
                    // overwrite, it is a how question, in case of <?how =/> b> it is b! which is desired
                    goalterm = imp.getPredicate();
                }
            } else if (imp.getTemporalOrder() == TemporalRules.ORDER_BACKWARD) {
                if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getPredicate() instanceof Operation) {
                    goalterm = imp.getPredicate();
                }
                if (goalterm instanceof Variable && goalterm.hasVarQuery() && (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getSubject() instanceof Operation)) {
                    // overwrite, it is a how question, in case of <?how =/> b> it is b! which is desired
                    goalterm = imp.getSubject();
                }
            }
        } else if (taskTerm instanceof Equivalence) {
            Equivalence qu = (Equivalence) taskTerm;
            if (qu.getTemporalOrder() == TemporalRules.ORDER_FORWARD || qu.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT) {
                if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || qu.getSubject() instanceof Operation) {
                    goalterm = qu.getSubject();
                }
                if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || qu.getPredicate() instanceof Operation) {
                    goalterm2 = qu.getPredicate();
                }
            }
        }
        TruthValue truth = new TruthValue(1.0f, Parameters.DEFAULT_GOAL_CONFIDENCE * Parameters.CURIOSITY_DESIRE_CONFIDENCE_MUL);
        if (goalterm != null && !(goalterm instanceof Variable) && goalterm instanceof CompoundTerm) {
            goalterm = ((CompoundTerm) goalterm).transformIndependentVariableToDependentVar((CompoundTerm) goalterm);
            Sentence sent = new Sentence(goalterm, Symbols.GOAL_MARK, truth, new Stamp(task.sentence.stamp, nal.memory.time()));
            nal.singlePremiseTask(sent, new BudgetValue(task.getPriority() * Parameters.CURIOSITY_DESIRE_PRIORITY_MUL, task.getDurability() * Parameters.CURIOSITY_DESIRE_DURABILITY_MUL, BudgetFunctions.truthToQuality(truth)));
        }
        if (goalterm2 != null && !(goalterm2 instanceof Variable) && goalterm2 instanceof CompoundTerm) {
            goalterm2 = ((CompoundTerm) goalterm).transformIndependentVariableToDependentVar((CompoundTerm) goalterm2);
            Sentence sent = new Sentence(goalterm2, Symbols.GOAL_MARK, truth.clone(), new Stamp(task.sentence.stamp, nal.memory.time()));
            nal.singlePremiseTask(sent, new BudgetValue(task.getPriority() * Parameters.CURIOSITY_DESIRE_PRIORITY_MUL, task.getDurability() * Parameters.CURIOSITY_DESIRE_DURABILITY_MUL, BudgetFunctions.truthToQuality(truth)));
        }
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Variable(nars.language.Variable) Equivalence(nars.language.Equivalence) Stamp(nars.entity.Stamp) TruthValue(nars.entity.TruthValue) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Operation(nars.operator.Operation) Implication(nars.language.Implication) Sentence(nars.entity.Sentence)

Aggregations

TruthValue (nars.entity.TruthValue)64 BudgetValue (nars.entity.BudgetValue)52 Term (nars.language.Term)46 Sentence (nars.entity.Sentence)39 CompoundTerm (nars.language.CompoundTerm)36 Task (nars.entity.Task)34 Statement (nars.language.Statement)20 Stamp (nars.entity.Stamp)18 Conjunction (nars.language.Conjunction)18 Inheritance (nars.language.Inheritance)12 Interval (nars.language.Interval)10 Implication (nars.language.Implication)9 Product (nars.language.Product)7 Variable (nars.language.Variable)7 Concept (nars.entity.Concept)5 SetExt (nars.language.SetExt)5 SetInt (nars.language.SetInt)5 Operation (nars.operator.Operation)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4