Search in sources :

Example 1 with Interval

use of nars.language.Interval in project opennars by opennars.

the class PerceptionAccel method perceive.

public void perceive(DerivationContext nal) {
    // implement Peis idea here now
    // we start with length 2 compounds, and search for patterns which are one longer than the longest observed one
    boolean longest_result_derived_already = false;
    for (int Len = cur_maxlen + 1; Len >= 2; Len--) {
        // ok, this is the length we have to collect, measured from the end of event buffer
        // there is a interval term for every event
        Term[] relterms = new Term[2 * Len - 1];
        // measuring its distance to the next event, but for the last event this is obsolete
        // thus it are 2*Len-1] terms
        Task newEvent = eventbuffer.get(eventbuffer.size() - 1);
        TruthValue truth = newEvent.sentence.truth;
        Stamp st = new Stamp(nal.memory);
        ArrayList<Long> evBase = new ArrayList<Long>();
        int k = 0;
        for (int i = 0; i < Len; i++) {
            // we go till to the end of the event buffer
            int j = eventbuffer.size() - 1 - (Len - 1) + i;
            if (j < 0) {
                // but the mechanism already looks for length 2 patterns on the occurence of the first event
                break;
            }
            Task current = eventbuffer.get(j);
            for (long l : current.sentence.stamp.evidentialBase) {
                evBase.add(l);
            }
            relterms[k] = current.sentence.term;
            if (i != Len - 1) {
                // if its not the last one, then there is a next one for which we have to put an interval
                truth = TruthFunctions.deduction(truth, current.sentence.truth);
                Task next = eventbuffer.get(j + 1);
                relterms[k + 1] = new Interval(next.sentence.getOccurenceTime() - current.sentence.getOccurenceTime());
            }
            k += 2;
        }
        long[] evB = new long[evBase.size()];
        int u = 0;
        for (long l : evBase) {
            evB[u] = l;
            u++;
        }
        st.baseLength = evB.length;
        st.evidentialBase = evB;
        boolean eventBufferDidNotHaveSoMuchEvents = false;
        for (int i = 0; i < relterms.length; i++) {
            if (relterms[i] == null) {
                eventBufferDidNotHaveSoMuchEvents = true;
            }
        }
        if (eventBufferDidNotHaveSoMuchEvents) {
            continue;
        }
        // decide on the tense of &/ by looking if the first event happens parallel with the last one
        // Todo refine in 1.6.3 if we want to allow input of difference occurence time
        boolean after = newEvent.sentence.stamp.after(eventbuffer.get(eventbuffer.size() - 1 - (Len - 1)).sentence.stamp, Parameters.DURATION);
        // critical part: (not checked for correctness yet):
        // we now have to look at if the first half + the second half already exists as concept, before we add it
        Term[] firstHalf;
        Term[] secondHalf;
        if (relterms[Len - 1] instanceof Interval) {
            // the middle can be a interval, for example in case of a,+1,b , in which case we dont use it
            // so we skip the middle here
            firstHalf = new Term[Len - 1];
            // as well as here
            secondHalf = new Term[Len - 1];
            // make index mapping easier by counting
            int h = 0;
            for (int i = 0; i < Len - 1; i++) {
                firstHalf[i] = relterms[h];
                h++;
            }
            // we have to overjump the middle element this is why
            h += 1;
            for (int i = 0; i < Len - 1; i++) {
                secondHalf[i] = relterms[h];
                h++;
            }
        } else {
            // it is a event so its fine
            // 2*Len-1 in total
            firstHalf = new Term[Len];
            // but the middle is also used in the second one
            secondHalf = new Term[Len];
            // make index mapping easier by counting
            int h = 0;
            for (int i = 0; i < Len; i++) {
                firstHalf[i] = relterms[h];
                h++;
            }
            // we have to use the middle twice this is why
            h--;
            for (int i = 0; i < Len; i++) {
                secondHalf[i] = relterms[h];
                h++;
            }
        }
        Term firstC = Conjunction.make(firstHalf, after ? ORDER_FORWARD : ORDER_CONCURRENT);
        Term secondC = Conjunction.make(secondHalf, after ? ORDER_FORWARD : ORDER_CONCURRENT);
        Concept C1 = nal.memory.concept(firstC);
        Concept C2 = nal.memory.concept(secondC);
        if (C1 == null || C2 == null) {
            if (debugMechanism) {
                System.out.println("one didn't exist: " + firstC.toString() + " or " + secondC.toString());
            }
            // the components were not observed, so don't allow creating this compound
            continue;
        }
        if (C1.getPriority() < partConceptsPrioThreshold || C2.getPriority() < partConceptsPrioThreshold) {
            // too less priority
            continue;
        }
        Conjunction C = (Conjunction) Conjunction.make(relterms, after ? ORDER_FORWARD : ORDER_CONCURRENT);
        // importance "summation"
        Sentence S = new Sentence(C, Symbols.JUDGMENT_MARK, truth, st);
        Task T = new Task(S, new BudgetValue(BudgetFunctions.or(C1.getPriority(), C2.getPriority()), Parameters.DEFAULT_JUDGMENT_DURABILITY, truth), true);
        if (debugMechanism) {
            System.out.println("success: " + T.toString());
        }
        if (longest_result_derived_already) {
            T.setElemOfSequenceBuffer(false);
        }
        longest_result_derived_already = true;
        // lets make the new event the parent task, and derive it
        nal.derivedTask(T, false, false, false);
    }
}
Also used : Concept(nars.entity.Concept) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Stamp(nars.entity.Stamp) ArrayList(java.util.ArrayList) Term(nars.language.Term) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 2 with Interval

use of nars.language.Interval in project opennars by opennars.

the class GlobalAnticipation method temporalPredictionsAdapt.

// check all predictive statements, match them with last events
public void temporalPredictionsAdapt(DerivationContext nal) {
    if (TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV == 0.0f) {
        return;
    }
    ArrayList<Task> lastEvents = new ArrayList<Task>();
    for (Task stmLast : stm) {
        lastEvents.add(stmLast);
    }
    if (lastEvents.isEmpty()) {
        return;
    }
    final long duration = Parameters.DURATION;
    ArrayList<Task> derivetasks = new ArrayList<Task>();
    for (final Task c : current_tasks) {
        // a =/> b or (&/ a1...an) =/> b
        boolean concurrent_conjunction = false;
        Term[] args = new Term[1];
        Implication imp = (Implication) c.sentence.term.clone();
        boolean concurrent_implication = imp.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT;
        args[0] = imp.getSubject();
        if (imp.getSubject() instanceof Conjunction) {
            Conjunction conj = (Conjunction) imp.getSubject();
            if (!conj.isSpatial) {
                if (conj.temporalOrder == TemporalRules.ORDER_FORWARD || conj.temporalOrder == TemporalRules.ORDER_CONCURRENT) {
                    concurrent_conjunction = conj.temporalOrder == TemporalRules.ORDER_CONCURRENT;
                    // in case of &/ this are the terms
                    args = conj.term;
                }
            }
        }
        int i = 0;
        boolean matched = true;
        int off = 0;
        long expected_time = lastEvents.get(0).sentence.getOccurenceTime();
        for (i = 0; i < args.length; i++) {
            // handling of intervals:
            if (args[i] instanceof Interval) {
                if (!concurrent_conjunction) {
                    expected_time += ((Interval) args[i]).time;
                }
                off++;
                continue;
            }
            if (i - off >= lastEvents.size()) {
                break;
            }
            if (!Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, args[i], lastEvents.get(i - off).sentence.term)) {
                // it didnt match, instead sth different unexpected happened
                // whether intermediate events should be tolerated or not was a important question when considering this,
                matched = false;
                // if it should be allowed, the sequential match does not matter only if the events come like predicted.
                break;
            } else {
                if (lastEvents.get(i - off).sentence.truth.getExpectation() <= 0.5f) {
                    // it matched according to sequence, but is its expectation bigger than 0.5? todo: decide how truth values of the expected events
                    // it didn't happen
                    matched = false;
                    break;
                }
                long occurence = lastEvents.get(i - off).sentence.getOccurenceTime();
                boolean right_in_time = Math.abs(occurence - expected_time) < ((double) duration) / TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV;
                if (!right_in_time) {
                    // it matched so far, but is the timing right or did it happen when not relevant anymore?
                    matched = false;
                    break;
                }
            }
            if (!concurrent_conjunction) {
                expected_time += duration;
            }
        }
        if (concurrent_conjunction && !concurrent_implication) {
            // implication is not concurrent
            // so here we have to add duration
            expected_time += duration;
        } else if (!concurrent_conjunction && concurrent_implication) {
            expected_time -= duration;
        }
        // ok it matched, is the consequence also right?
        if (matched && lastEvents.size() > args.length - off) {
            long occurence = lastEvents.get(args.length - off).sentence.getOccurenceTime();
            boolean right_in_time = Math.abs(occurence - expected_time) < ((double) duration) / TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV;
            if (right_in_time && Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, imp.getPredicate(), lastEvents.get(args.length - off).sentence.term)) {
                // it matched and same consequence, so positive evidence
                // c.sentence.truth=TruthFunctions.revision(c.sentence.truth, new TruthValue(1.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE));
                Sentence s2 = new Sentence(c.sentence.term.clone(), Symbols.JUDGMENT_MARK, new TruthValue(1.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(nal.memory));
                Task t = new Task(s2, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, s2.truth), true);
                derivetasks.add(t);
            } else {
                // it matched and other consequence, so negative evidence
                // c.sentence.truth=TruthFunctions.revision(c.sentence.truth, new TruthValue(0.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE));
                Sentence s2 = new Sentence(c.sentence.term.clone(), Symbols.JUDGMENT_MARK, new TruthValue(0.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(nal.memory));
                Task t = new Task(s2, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, s2.truth), true);
                derivetasks.add(t);
            }
        // todo use derived task with revision instead
        }
    }
    for (Task t : derivetasks) {
        if (nal.derivedTask(t, false, false, false)) {
            boolean debug = true;
        }
    }
    ArrayList<Task> toDelete = new ArrayList<Task>();
    for (Task t : current_tasks) {
        Concept w = nal.memory.concept(t.sentence.term);
        if (w == null) {
            // concept does not exist anymore, delete
            toDelete.add(t);
        }
    }
    for (Task t : toDelete) {
        current_tasks.remove(t);
    }
}
Also used : BudgetValue(nars.entity.BudgetValue) Concept(nars.entity.Concept) Task(nars.entity.Task) Stamp(nars.entity.Stamp) ArrayList(java.util.ArrayList) Term(nars.language.Term) Implication(nars.language.Implication) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 3 with Interval

use of nars.language.Interval in project opennars by opennars.

the class Memory method conceptualize.

/**
 * Get the Concept associated to a Term, or create it.
 *
 *   Existing concept: apply tasklink activation (remove from bag, adjust budget, reinsert)
 *   New concept: set initial activation, insert
 *   Subconcept: extract from cache, apply activation, insert
 *
 * If failed to insert as a result of null bag, returns null
 *
 * A displaced Concept resulting from insert is forgotten (but may be stored in optional  subconcept memory
 *
 * @param term indicating the concept
 * @return an existing Concept, or a new one, or null
 */
public Concept conceptualize(final BudgetValue budget, Term term) {
    if (term instanceof Interval) {
        return null;
    }
    term = CompoundTerm.replaceIntervals(term);
    // see if concept is active
    Concept concept = concepts.take(term);
    if (concept == null) {
        // create new concept, with the applied budget
        concept = new Concept(budget, term, this);
        // if (memory.logic!=null)
        // memory.logic.CONCEPT_NEW.commit(term.getComplexity());
        emit(Events.ConceptNew.class, concept);
    } else if (concept != null) {
        // apply budget to existing concept
        // memory.logic.CONCEPT_ACTIVATE.commit(term.getComplexity());
        BudgetFunctions.activate(concept.budget, budget, BudgetFunctions.Activating.TaskLink);
    } else {
        // unable to create, ex: has variables
        return null;
    }
    Concept displaced = concepts.putBack(concept, cycles(param.conceptForgetDurations), this);
    if (displaced == null) {
        // added without replacing anything
        return concept;
    } else if (displaced == concept) {
        // not able to insert
        conceptRemoved(displaced);
        return null;
    } else {
        conceptRemoved(displaced);
        return concept;
    }
}
Also used : Concept(nars.entity.Concept) Events(nars.io.events.Events) Interval(nars.language.Interval)

Example 4 with Interval

use of nars.language.Interval in project opennars by opennars.

the class CompositionalRules method decomposeCompound.

/**
 * {<(S|P) ==> M>, <P ==> M>} |- <S ==> M>
 *
 * @param implication The implication term to be decomposed
 * @param componentCommon The part of the implication to be removed
 * @param term1 The other term in the contentInd
 * @param index The location of the shared term: 0 for subject, 1 for
 * predicate
 * @param compoundTask Whether the implication comes from the task
 * @param nal Reference to the memory
 */
private static void decomposeCompound(CompoundTerm compound, Term component, Term term1, int index, boolean compoundTask, int order, DerivationContext nal) {
    if ((compound instanceof Statement) || (compound instanceof ImageExt) || (compound instanceof ImageInt)) {
        return;
    }
    Term term2 = reduceComponents(compound, component, nal.mem());
    if (term2 == null) {
        return;
    }
    long delta = 0;
    while ((term2 instanceof Conjunction) && (((CompoundTerm) term2).term[0] instanceof Interval)) {
        Interval interval = (Interval) ((CompoundTerm) term2).term[0];
        delta += interval.time;
        term2 = ((CompoundTerm) term2).setComponent(0, null, nal.mem());
    }
    Task task = nal.getCurrentTask();
    Sentence sentence = task.sentence;
    Sentence belief = nal.getCurrentBelief();
    Statement oldContent = (Statement) task.getTerm();
    TruthValue v1, v2;
    if (compoundTask) {
        v1 = sentence.truth;
        v2 = belief.truth;
    } else {
        v1 = belief.truth;
        v2 = sentence.truth;
    }
    TruthValue truth = null;
    Term content;
    if (index == 0) {
        content = Statement.make(oldContent, term1, term2, order);
        if (content == null) {
            return;
        }
        if (oldContent instanceof Inheritance) {
            if (compound instanceof IntersectionExt) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof IntersectionInt) {
                truth = reduceDisjunction(v1, v2);
            } else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
                truth = reduceConjunction(v1, v2);
            } else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
                truth = reduceDisjunction(v1, v2);
            } else if (compound instanceof DifferenceExt) {
                if (compound.term[0].equals(component)) {
                    truth = reduceDisjunction(v2, v1);
                } else {
                    truth = reduceConjunctionNeg(v1, v2);
                }
            }
        } else if (oldContent instanceof Implication) {
            if (compound instanceof Conjunction) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof Disjunction) {
                truth = reduceDisjunction(v1, v2);
            }
        }
    } else {
        content = Statement.make(oldContent, term2, term1, order);
        if (content == null) {
            return;
        }
        if (oldContent instanceof Inheritance) {
            if (compound instanceof IntersectionInt) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof IntersectionExt) {
                truth = reduceDisjunction(v1, v2);
            } else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
                truth = reduceConjunction(v1, v2);
            } else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
                truth = reduceDisjunction(v1, v2);
            } else if (compound instanceof DifferenceInt) {
                if (compound.term[1].equals(component)) {
                    truth = reduceDisjunction(v2, v1);
                } else {
                    truth = reduceConjunctionNeg(v1, v2);
                }
            }
        } else if (oldContent instanceof Implication) {
            if (compound instanceof Disjunction) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof Conjunction) {
                truth = reduceDisjunction(v1, v2);
            }
        }
    }
    if (truth != null) {
        BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
        if (delta != 0) {
            long baseTime = task.sentence.getOccurenceTime();
            if (baseTime != Stamp.ETERNAL) {
                baseTime += delta;
                nal.getTheNewStamp().setOccurrenceTime(baseTime);
            }
        }
        // (allow overlap), a form of detachment
        nal.doublePremiseTask(content, truth, budget, false, true);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) ImageInt(nars.language.ImageInt) SetInt(nars.language.SetInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) IntersectionInt(nars.language.IntersectionInt) Implication(nars.language.Implication) DifferenceInt(nars.language.DifferenceInt) Disjunction(nars.language.Disjunction) TruthFunctions.reduceDisjunction(nars.inference.TruthFunctions.reduceDisjunction) IntersectionExt(nars.language.IntersectionExt) TruthValue(nars.entity.TruthValue) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) SetExt(nars.language.SetExt) ImageExt(nars.language.ImageExt) DifferenceExt(nars.language.DifferenceExt) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 5 with Interval

use of nars.language.Interval in project opennars by opennars.

the class CompositionalRules method decomposeStatement.

/**
 * {(||, S, P), P} |- S {(&&, S, P), P} |- S
 *
 * @param implication The implication term to be decomposed
 * @param componentCommon The part of the implication to be removed
 * @param compoundTask Whether the implication comes from the task
 * @param nal Reference to the memory
 */
static void decomposeStatement(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
    boolean isTemporalConjunction = (compound instanceof Conjunction) && !((Conjunction) compound).isSpatial;
    if (isTemporalConjunction && (compound.getTemporalOrder() == TemporalRules.ORDER_FORWARD) && (index != 0)) {
        return;
    }
    long occurrence_time = nal.getTheNewStamp().getOccurrenceTime();
    if (isTemporalConjunction && (compound.getTemporalOrder() == TemporalRules.ORDER_FORWARD)) {
        if (!nal.getCurrentTask().sentence.isEternal() && compound.term[index + 1] instanceof Interval) {
            long shift_occurrence = ((Interval) compound.term[1]).time;
            occurrence_time = nal.getCurrentTask().sentence.getOccurenceTime() + shift_occurrence;
        }
    }
    Task task = nal.getCurrentTask();
    Sentence taskSentence = task.sentence;
    Sentence belief = nal.getCurrentBelief();
    Term content = reduceComponents(compound, component, nal.mem());
    if (content == null) {
        return;
    }
    TruthValue truth = null;
    BudgetValue budget;
    if (taskSentence.isQuestion() || taskSentence.isQuest()) {
        budget = BudgetFunctions.compoundBackward(content, nal);
        nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
        nal.doublePremiseTask(content, truth, budget, false, false);
        // special inference to answer conjunctive questions with query variables
        if (taskSentence.term.hasVarQuery()) {
            Concept contentConcept = nal.mem().concept(content);
            if (contentConcept == null) {
                return;
            }
            Sentence contentBelief = contentConcept.getBelief(nal, task);
            if (contentBelief == null) {
                return;
            }
            Task contentTask = new Task(contentBelief, task.budget, false);
            nal.setCurrentTask(contentTask);
            Term conj = Conjunction.make(component, content);
            truth = intersection(contentBelief.truth, belief.truth);
            budget = BudgetFunctions.compoundForward(truth, conj, nal);
            nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
            nal.doublePremiseTask(conj, truth, budget, false, false);
        }
    } else {
        TruthValue v1, v2;
        if (compoundTask) {
            v1 = taskSentence.truth;
            v2 = belief.truth;
        } else {
            v1 = belief.truth;
            v2 = taskSentence.truth;
        }
        if (compound instanceof Conjunction) {
            if (taskSentence.isGoal()) {
                if (compoundTask) {
                    truth = intersection(v1, v2);
                } else {
                    return;
                }
            } else {
                // isJudgment
                truth = reduceConjunction(v1, v2);
            }
        } else if (compound instanceof Disjunction) {
            if (taskSentence.isGoal()) {
                if (compoundTask) {
                    truth = reduceConjunction(v2, v1);
                } else {
                    return;
                }
            } else {
                // isJudgment
                truth = reduceDisjunction(v1, v2);
            }
        } else {
            return;
        }
        budget = BudgetFunctions.compoundForward(truth, content, nal);
    }
    nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
    nal.doublePremiseTask(content, truth, budget, false, false);
}
Also used : BudgetValue(nars.entity.BudgetValue) Concept(nars.entity.Concept) Task(nars.entity.Task) Disjunction(nars.language.Disjunction) TruthFunctions.reduceDisjunction(nars.inference.TruthFunctions.reduceDisjunction) TruthValue(nars.entity.TruthValue) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Aggregations

Interval (nars.language.Interval)13 TruthValue (nars.entity.TruthValue)10 BudgetValue (nars.entity.BudgetValue)9 Sentence (nars.entity.Sentence)9 Task (nars.entity.Task)9 Term (nars.language.Term)9 Conjunction (nars.language.Conjunction)8 CompoundTerm (nars.language.CompoundTerm)7 Stamp (nars.entity.Stamp)5 ArrayList (java.util.ArrayList)4 Concept (nars.entity.Concept)4 Statement (nars.language.Statement)4 Implication (nars.language.Implication)3 Inheritance (nars.language.Inheritance)3 Product (nars.language.Product)3 TruthFunctions.reduceConjunction (nars.inference.TruthFunctions.reduceConjunction)2 TruthFunctions.reduceDisjunction (nars.inference.TruthFunctions.reduceDisjunction)2 Disjunction (nars.language.Disjunction)2 ImageInt (nars.language.ImageInt)2 Variable (nars.language.Variable)2