use of nars.entity.BudgetValue in project opennars by opennars.
the class LocalRules method solutionEval.
/* ----- Functions used both in direct and indirect processing of tasks ----- */
/**
* Evaluate the quality of a belief as a solution to a problem, then reward
* the belief and de-prioritize the problem
*
* @param problem The problem (question or goal) to be solved
* @param solution The belief as solution
* @param task The task to be immediately processed, or null for continued
* process
* @return The budget for the new task which is the belief activated, if
* necessary
*/
public static BudgetValue solutionEval(final Task problem, final Sentence solution, Task task, final nars.control.DerivationContext nal) {
if (problem.sentence.punctuation != solution.punctuation && solution.term.hasVarQuery()) {
return null;
}
BudgetValue budget = null;
boolean feedbackToLinks = false;
if (task == null) {
task = nal.getCurrentTask();
feedbackToLinks = true;
}
boolean judgmentTask = task.sentence.isJudgment();
// here its whether its a what or where question for budget adjustment
boolean rateByConfidence = problem.getTerm().hasVarQuery();
final float quality = solutionQuality(rateByConfidence, problem, solution, nal.mem());
if (problem.sentence.isGoal()) {
nal.memory.emotion.adjustSatisfaction(quality, task.getPriority(), nal);
}
if (judgmentTask) {
task.incPriority(quality);
} else {
// +goal satisfication is a matter of degree - https://groups.google.com/forum/#!topic/open-nars/ZfCM416Dx1M
float taskPriority = task.getPriority();
budget = new BudgetValue(UtilityFunctions.or(taskPriority, quality), task.getDurability(), BudgetFunctions.truthToQuality(solution.truth));
task.setPriority(Math.min(1 - quality, taskPriority));
}
if (feedbackToLinks) {
TaskLink tLink = nal.getCurrentTaskLink();
tLink.setPriority(Math.min(1 - quality, tLink.getPriority()));
TermLink bLink = nal.getCurrentBeliefLink();
bLink.incPriority(quality);
}
return budget;
}
use of nars.entity.BudgetValue in project opennars by opennars.
the class LocalRules method trySolution.
/**
* Check if a Sentence provide a better answer to a Question or Goal
*
* @param belief The proposed answer
* @param task The task to be processed
* @param memory Reference to the memory
*/
public static boolean trySolution(Sentence belief, final Task task, final DerivationContext nal, boolean report) {
Sentence problem = task.sentence;
Memory memory = nal.mem();
Sentence oldBest = task.getBestSolution();
if (oldBest != null) {
boolean rateByConfidence = oldBest.getTerm().equals(belief.getTerm());
float newQ = solutionQuality(rateByConfidence, task, belief, memory);
float oldQ = solutionQuality(rateByConfidence, task, oldBest, memory);
if (oldQ >= newQ) {
if (problem.isGoal()) {
memory.emotion.adjustSatisfaction(oldQ, task.getPriority(), nal);
}
memory.emit(Unsolved.class, task, belief, "Lower quality");
return false;
}
}
task.setBestSolution(memory, belief);
// memory.logic.SOLUTION_BEST.commit(task.getPriority());
BudgetValue budget = solutionEval(task, belief, task, nal);
if ((budget != null) && budget.aboveThreshold()) {
// Solution Activated
if (task.sentence.punctuation == Symbols.QUESTION_MARK || task.sentence.punctuation == Symbols.QUEST_MARK) {
if (task.isInput() && report) {
// only show input tasks as solutions
memory.emit(Answer.class, task, belief);
} else {
// solution to quests and questions can be always showed
memory.emit(OutputHandler.class, task, belief);
}
} else {
// goal things only show silence related
memory.emit(OutputHandler.class, task, belief);
}
nal.addTask(nal.getCurrentTask(), budget, belief, task.getParentBelief());
return true;
} else {
memory.emit(Unsolved.class, task, belief, "Insufficient budget");
}
return false;
}
use of nars.entity.BudgetValue 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);
}
use of nars.entity.BudgetValue 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)));
}
}
}
use of nars.entity.BudgetValue in project opennars by opennars.
the class StructuralRules method flattenSequence.
/* --------------- Flatten sequence transform --------------- */
/**
* {(#,(#,A,B),C), (#,A,B)@(#,(#,A,B), C)} |- (#,A,B,C)
* (same for &/)
* @param compound The premise
* @param component The recognized component in the premise
* @param compoundTask Whether the compound comes from the task
* @param nal Reference to the memory
*/
static void flattenSequence(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
if (compound instanceof Conjunction && component instanceof Conjunction) {
Conjunction conjCompound = (Conjunction) compound;
Conjunction conjComponent = (Conjunction) component;
if (// since parallel conjunction and normal one already is flattened
conjCompound.getTemporalOrder() == TemporalRules.ORDER_FORWARD && conjComponent.getTemporalOrder() == TemporalRules.ORDER_FORWARD && conjCompound.getIsSpatial() == conjComponent.getIsSpatial()) {
// because also when both are tmporal
Term[] newTerm = new Term[conjCompound.size() - 1 + conjComponent.size()];
System.arraycopy(conjCompound.term, 0, newTerm, 0, index);
System.arraycopy(conjComponent.term, 0, newTerm, index + 0, conjComponent.size());
System.arraycopy(conjCompound.term, index + conjComponent.size() - conjComponent.size() + 1, newTerm, index + conjComponent.size(), newTerm.length - (index + conjComponent.size()));
Conjunction cont = (Conjunction) Conjunction.make(newTerm, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
TruthValue truth = nal.getCurrentTask().sentence.truth.clone();
BudgetValue budget = BudgetFunctions.forward(truth, nal);
nal.singlePremiseTask(cont, truth, budget);
}
}
}
Aggregations