use of nars.language.Interval in project opennars by opennars.
the class Anticipate method updateAnticipations.
public void updateAnticipations() {
if (anticipations.isEmpty())
return;
long now = nal.memory.time();
// share stamps created by tasks in this cycle
boolean hasNewTasks = !newTasks.isEmpty();
Iterator<Map.Entry<Vector2Int, LinkedHashSet<Term>>> aei = anticipations.entrySet().iterator();
while (aei.hasNext()) {
Map.Entry<Vector2Int, LinkedHashSet<Term>> ae = aei.next();
long aTime = ae.getKey().predictedOccurenceTime;
long predictionstarted = ae.getKey().predictionCreationTime;
if (aTime < predictionstarted) {
// its about the past..
return;
}
// lets say a and <(&/,a,+4) =/> b> leaded to prediction of b with specific occurence time
// this indicates that this interval can be reconstructed by looking by when the prediction
// happened and for what time it predicted, Only when the happening would already lead to <(&/,a,+5) =/> b>
// we are allowed to apply CWA already, i think this is the perfect time to do this
// since there is no way anymore that the observation would support <(&/,a,+4) =/> b> at this time,
// also this way it is not applied to early, it seems to be the perfect time to me,
// making hopeExpirationWindow parameter entirely osbolete
Interval Int = new Interval(aTime - predictionstarted);
// ok we know the magnitude now, let's now construct a interval with magnitude one higher
// (this we can skip because magnitudeToTime allows it without being explicitly constructed)
// ok, and what predicted occurence time would that be? because only if now is bigger or equal, didnt happen is true
double expiredate = predictionstarted + Int.time * Parameters.ANTICIPATION_TOLERANCE;
//
boolean didntHappen = (now >= expiredate);
boolean maybeHappened = hasNewTasks && !didntHappen;
if ((!didntHappen) && (!maybeHappened))
continue;
LinkedHashSet<Term> terms = ae.getValue();
Iterator<Term> ii = terms.iterator();
while (ii.hasNext()) {
Term aTerm = ii.next();
boolean remove = false;
if (didntHappen) {
deriveDidntHappen(aTerm, aTime);
remove = true;
}
if (maybeHappened) {
if (newTasks.remove(aTerm)) {
// in case it happened, temporal induction will do the rest, else deriveDidntHappen occurred
if (!remove) {
nal.memory.emit(CONFIRM.class, aTerm);
}
remove = true;
hasNewTasks = !newTasks.isEmpty();
}
}
if (remove)
ii.remove();
}
if (terms.isEmpty()) {
// remove this time entry because its terms have been emptied
aei.remove();
}
}
newTasks.clear();
}
use of nars.language.Interval in project opennars by opennars.
the class InternalExperience method beliefReason.
/**
* used in full internal experience mode only
*/
protected void beliefReason(Sentence belief, Term beliefTerm, Term taskTerm, DerivationContext nal) {
Memory memory = nal.memory;
if (Memory.randomNumber.nextDouble() < INTERNAL_EXPERIENCE_RARE_PROBABILITY) {
// the operators which dont have a innate belief
// also get a chance to reveal its effects to the system this way
Operator op = memory.getOperator(nonInnateBeliefOperators[Memory.randomNumber.nextInt(nonInnateBeliefOperators.length)]);
Product prod = new Product(new Term[] { belief.term });
if (op != null && prod != null) {
Term new_term = Inheritance.make(prod, op);
Sentence sentence = new Sentence(new_term, Symbols.GOAL_MARK, // a naming convension
new TruthValue(1, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(memory));
float quality = BudgetFunctions.truthToQuality(sentence.truth);
BudgetValue budget = new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY * INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_GOAL_DURABILITY * INTERNAL_EXPERIENCE_DURABILITY_MUL, quality);
Task newTask = new Task(sentence, budget, true);
nal.derivedTask(newTask, false, false, false);
}
}
if (beliefTerm instanceof Implication && Memory.randomNumber.nextDouble() <= INTERNAL_EXPERIENCE_PROBABILITY) {
Implication imp = (Implication) beliefTerm;
if (imp.getTemporalOrder() == TemporalRules.ORDER_FORWARD) {
// 1. check if its (&/,term,+i1,...,+in) =/> anticipateTerm form:
boolean valid = true;
if (imp.getSubject() instanceof Conjunction) {
Conjunction conj = (Conjunction) imp.getSubject();
if (!conj.term[0].equals(taskTerm)) {
// the expected needed term is not included
valid = false;
}
for (int i = 1; i < conj.term.length; i++) {
if (!(conj.term[i] instanceof Interval)) {
valid = false;
break;
}
}
} else {
if (!imp.getSubject().equals(taskTerm)) {
valid = false;
}
}
if (valid) {
Operator op = memory.getOperator("^anticipate");
if (op == null)
throw new RuntimeException(this + " requires ^anticipate operator");
Product args = new Product(new Term[] { imp.getPredicate() });
Term new_term = Operation.make(args, op);
Sentence sentence = new Sentence(new_term, Symbols.GOAL_MARK, // a naming convension
new TruthValue(1, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(memory));
float quality = BudgetFunctions.truthToQuality(sentence.truth);
BudgetValue budget = new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY * INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_GOAL_DURABILITY * INTERNAL_EXPERIENCE_DURABILITY_MUL, quality);
Task newTask = new Task(sentence, budget, true);
nal.derivedTask(newTask, false, false, false);
}
}
}
}
use of nars.language.Interval in project opennars by opennars.
the class SyllogisticRules method abdIndCom.
/**
* {<M ==> S>, <M ==> P>} |- {<S ==> P>, <P ==> S>, <S <=> P>}
*
* @param term1 Subject of the first new task
* @param term2 Predicate of the first new task
* @param sentence1 The first premise
* @param sentence2 The second premise
* @param figure Locations of the shared term in premises --- can be
* removed?
* @param nal Reference to the memory
*/
static void abdIndCom(Term term1, Term term2, final Sentence sentence1, final Sentence sentence2, final int figure, final DerivationContext nal) {
if (Statement.invalidStatement(term1, term2) || Statement.invalidPair(term1, term2)) {
return;
}
int order1 = sentence1.term.getTemporalOrder();
int order2 = sentence2.term.getTemporalOrder();
int order = abdIndComOrder(order1, order2);
Statement taskContent = (Statement) sentence1.term;
TruthValue truth1 = null;
TruthValue truth2 = null;
TruthValue truth3 = null;
BudgetValue budget1, budget2, budget3;
TruthValue value1 = sentence1.truth;
TruthValue value2 = sentence2.truth;
if (sentence1.isQuestion()) {
budget1 = BudgetFunctions.backward(value2, nal);
budget2 = BudgetFunctions.backwardWeak(value2, nal);
budget3 = BudgetFunctions.backward(value2, nal);
} else if (sentence1.isQuest()) {
budget1 = BudgetFunctions.backwardWeak(value2, nal);
budget2 = BudgetFunctions.backward(value2, nal);
budget3 = BudgetFunctions.backwardWeak(value2, nal);
} else {
if (sentence1.isGoal()) {
// P --> S
truth1 = TruthFunctions.desireStrong(value1, value2);
// S --> P
truth2 = TruthFunctions.desireWeak(value2, value1);
// S <-> P
truth3 = TruthFunctions.desireStrong(value1, value2);
} else {
// isJudgment
// P --> S
truth1 = TruthFunctions.abduction(value1, value2);
// S --> P
truth2 = TruthFunctions.abduction(value2, value1);
// S <-> P
truth3 = TruthFunctions.comparison(value1, value2);
}
budget1 = BudgetFunctions.forward(truth1, nal);
budget2 = BudgetFunctions.forward(truth2, nal);
budget3 = BudgetFunctions.forward(truth3, nal);
}
long delta2 = 0;
while ((term2 instanceof Conjunction) && (((CompoundTerm) term2).term[0] instanceof Interval)) {
Interval interval = (Interval) ((CompoundTerm) term2).term[0];
delta2 += interval.time;
term2 = ((CompoundTerm) term2).setComponent(0, null, nal.mem());
}
long delta1 = 0;
while ((term1 instanceof Conjunction) && (((CompoundTerm) term1).term[0] instanceof Interval)) {
Interval interval = (Interval) ((CompoundTerm) term1).term[0];
delta1 += interval.time;
term1 = ((CompoundTerm) term1).setComponent(0, null, nal.mem());
}
if (order != ORDER_INVALID) {
nal.getTheNewStamp().setOccurrenceTime(delta1);
nal.doublePremiseTask(Statement.make(taskContent, term1, term2, order), truth1, budget1, false, false);
nal.getTheNewStamp().setOccurrenceTime(delta2);
nal.doublePremiseTask(Statement.make(taskContent, term2, term1, reverseOrder(order)), truth2, budget2, false, false);
nal.getTheNewStamp().setOccurrenceTime(delta1);
nal.doublePremiseTask(Statement.makeSym(taskContent, term1, term2, order), truth3, budget3, false, false);
}
if (Parameters.BREAK_NAL_HOL_BOUNDARY && order1 == order2 && taskContent.isHigherOrderStatement() && sentence2.term.isHigherOrderStatement()) {
/* if(truth1!=null)
truth1=truth1.clone();
if(truth2!=null)
truth2=truth2.clone();*/
if (truth3 != null)
truth3 = truth3.clone();
/* nal.doublePremiseTask(
Statement.make(NativeOperator.INHERITANCE, term1, term2),
truth1, budget1.clone(),false, false);
nal.doublePremiseTask(
Statement.make(NativeOperator.INHERITANCE, term2, term1),
truth2, budget2.clone(),false, false);*/
nal.doublePremiseTask(Statement.make(NativeOperator.SIMILARITY, term1, term2, TemporalRules.ORDER_NONE), truth3, budget3.clone(), false, false);
}
}
Aggregations