Search in sources :

Example 26 with Concept

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

the class DefaultGraphizer method onFinish.

@Override
public void onFinish(NARGraph g) {
    if (includeSyntax > 0) {
        for (final Term a : terms.keySet()) {
            if (a instanceof CompoundTerm) {
                CompoundTerm c = (CompoundTerm) a;
                g.addVertex(c.operator());
                g.addEdge(c.operator(), c, new NARGraph.TermType());
                if (includeSyntax - 1 > 0) {
                    recurseTermComponents(g, c, includeSyntax - 1);
                }
            }
        }
    }
    if (includeTermContent) {
        for (final Term a : terms.keySet()) {
            for (final Term b : terms.keySet()) {
                if (a == b) {
                    continue;
                }
                if (a.containsTerm(b)) {
                    g.addVertex(a);
                    g.addVertex(b);
                    g.addEdge(a, b, new NARGraph.TermContent());
                }
                if (b.containsTerm(a)) {
                    g.addVertex(a);
                    g.addVertex(b);
                    g.addEdge(b, a, new NARGraph.TermContent());
                }
            }
        }
    }
    if (includeDerivations && includeBeliefs) {
        for (final Map.Entry<Sentence, Concept> s : sentenceTerms.entrySet()) {
            final Sentence derivedSentence = s.getKey();
            final Concept derived = s.getValue();
            for (final Map.Entry<Sentence, Concept> t : sentenceTerms.entrySet()) {
                if (s == t) {
                    continue;
                }
                final Sentence deriverSentence = t.getKey();
                final Concept deriver = t.getValue();
                if (derived == deriver) {
                    continue;
                }
            }
        }
    }
    if (includeTermLinks) {
        for (Map.Entry<TermLink, Concept> et : termLinks.entrySet()) {
            TermLink t = et.getKey();
            Concept from = et.getValue();
            Concept to = terms.get(t.target);
            if (to != null) {
                g.addEdge(from, to, new NARGraph.TermLinkEdge(t));
            }
        }
    }
    if (includeTaskLinks) {
        for (Map.Entry<TaskLink, Concept> et : taskLinks.entrySet()) {
            TaskLink t = et.getKey();
            if (this.taskPriorityThreshold != null && t.getPriority() > this.taskPriorityThreshold.get()) {
                Concept from = et.getValue();
                if (t.targetTask != null) {
                    Task theTask = t.targetTask;
                    if (!g.containsVertex(theTask)) {
                        g.addVertex(theTask);
                        Term term = theTask.getTerm();
                        if (term != null) {
                            Concept c = terms.get(term);
                            if (c != null) {
                                if (g.containsVertex(c)) {
                                    g.addVertex(c);
                                }
                                g.addEdge(c, theTask, new NARGraph.TermContent());
                            }
                        }
                        onTask(theTask);
                    }
                    g.addEdge(from, t.targetTask, new NARGraph.TaskLinkEdge(t));
                }
            }
        }
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) Concept(nars.entity.Concept) TermLink(nars.entity.TermLink) Task(nars.entity.Task) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) TaskLink(nars.entity.TaskLink) Map(java.util.Map) HashMap(java.util.HashMap) Sentence(nars.entity.Sentence)

Example 27 with Concept

use of nars.entity.Concept 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 28 with Concept

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

the class GeneralInferenceControl method selectConceptForInference.

public static void selectConceptForInference(Memory mem) {
    Concept currentConcept = mem.concepts.takeNext();
    if (currentConcept == null)
        return;
    if (currentConcept.taskLinks.size() == 0) {
        // remove concepts without tasklinks and without termlinks
        mem.concepts.take(currentConcept.getTerm());
        mem.conceptRemoved(currentConcept);
        return;
    }
    if (currentConcept.termLinks.size() == 0) {
        // remove concepts without tasklinks and without termlinks
        mem.concepts.take(currentConcept.getTerm());
        mem.conceptRemoved(currentConcept);
        return;
    }
    DerivationContext cont = new DerivationContext(mem);
    cont.setCurrentConcept(currentConcept);
    fireConcept(cont, 1);
}
Also used : Concept(nars.entity.Concept)

Example 29 with Concept

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

the class TemporalInferenceControl method addToSequenceTasks.

public static void addToSequenceTasks(DerivationContext nal, final Task newEvent) {
    // multiple versions are necessary, but we do not allow duplicates
    List<Task> removals = new LinkedList<Task>();
    for (Task s : nal.memory.seq_current) {
        if (CompoundTerm.replaceIntervals(s.getTerm()).equals(CompoundTerm.replaceIntervals(newEvent.getTerm()))) {
            // check term indices
            if (s.getTerm().term_indices != null && newEvent.getTerm().term_indices != null) {
                boolean differentTermIndices = false;
                for (int i = 0; i < s.getTerm().term_indices.length; i++) {
                    if (s.getTerm().term_indices[i] != newEvent.getTerm().term_indices[i]) {
                        differentTermIndices = true;
                    }
                }
                if (differentTermIndices) {
                    continue;
                }
            }
            removals.add(s);
            break;
        }
    }
    for (Task removal : removals) {
        nal.memory.seq_current.take(removal);
    }
    // making sure we do not mess with budget of the task:
    if (!(newEvent.sentence.getTerm() instanceof Operation)) {
        Concept c = nal.memory.concept(newEvent.getTerm());
        float event_quality = BudgetFunctions.truthToQuality(newEvent.sentence.truth);
        float event_priority = event_quality;
        if (c != null) {
            event_priority = Math.max(event_quality, c.getPriority());
        }
        Task t2 = new Task(newEvent.sentence, new BudgetValue(event_priority, 1.0f / (float) newEvent.sentence.term.getComplexity(), event_quality), newEvent.getParentBelief(), newEvent.getBestSolution());
        nal.memory.seq_current.putIn(t2);
    }
}
Also used : Concept(nars.entity.Concept) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Operation(nars.operator.Operation) LinkedList(java.util.LinkedList)

Example 30 with Concept

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

the class TemporalInferenceControl method eventInference.

public static boolean eventInference(final Task newEvent, DerivationContext nal) {
    if (newEvent.getTerm() == null || newEvent.budget == null || !newEvent.isElemOfSequenceBuffer()) {
        // todo refine, add directbool in task
        return false;
    }
    nal.emit(Events.InduceSucceedingEvent.class, newEvent, nal);
    if (!newEvent.sentence.isJudgment() || newEvent.sentence.isEternal() || !newEvent.isInput()) {
        return false;
    }
    HashSet<Task> already_attempted = new HashSet<Task>();
    HashSet<Task> already_attempted_ops = new HashSet<Task>();
    // Sequence formation:
    for (int i = 0; i < Parameters.SEQUENCE_BAG_ATTEMPTS; i++) {
        Task takeout = nal.memory.seq_current.takeNext();
        if (takeout == null) {
            // there were no elements in the bag to try
            break;
        }
        if (already_attempted.contains(takeout) || Stamp.baseOverlap(newEvent.sentence.stamp.evidentialBase, takeout.sentence.stamp.evidentialBase)) {
            nal.memory.seq_current.putBack(takeout, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
            continue;
        }
        already_attempted.add(takeout);
        try {
            proceedWithTemporalInduction(newEvent.sentence, takeout.sentence, newEvent, nal, true, true, true);
        } catch (Exception ex) {
            if (Parameters.DEBUG) {
                System.out.println("issue in temporal induction");
            }
        }
        nal.memory.seq_current.putBack(takeout, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
    }
    // Conditioning:
    if (nal.memory.lastDecision != null && newEvent != nal.memory.lastDecision) {
        already_attempted_ops.clear();
        for (int k = 0; k < Parameters.OPERATION_SAMPLES; k++) {
            // todo move into k loop
            already_attempted.clear();
            Task Toperation = k == 0 ? nal.memory.lastDecision : nal.memory.recent_operations.takeNext();
            if (Toperation == null) {
                // there were no elements in the bag to try
                break;
            }
            if (already_attempted_ops.contains(Toperation)) {
                // put opc back into bag
                // (k>0 holds here):
                nal.memory.recent_operations.putBack(Toperation, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
                continue;
            }
            already_attempted_ops.add(Toperation);
            Concept opc = nal.memory.concept(Toperation.getTerm());
            if (opc != null) {
                if (opc.seq_before == null) {
                    opc.seq_before = new LevelBag<>(Parameters.SEQUENCE_BAG_LEVELS, Parameters.SEQUENCE_BAG_SIZE);
                }
                for (int i = 0; i < Parameters.CONDITION_BAG_ATTEMPTS; i++) {
                    Task takeout = opc.seq_before.takeNext();
                    if (takeout == null) {
                        // there were no elements in the bag to try
                        break;
                    }
                    if (already_attempted.contains(takeout)) {
                        opc.seq_before.putBack(takeout, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
                        continue;
                    }
                    already_attempted.add(takeout);
                    try {
                        long x = Toperation.sentence.getOccurenceTime();
                        long y = takeout.sentence.getOccurenceTime();
                        if (y > x) {
                            // something wrong here?
                            System.out.println("analyze case in TemporalInferenceControl!");
                            continue;
                        }
                        List<Task> seq_op = proceedWithTemporalInduction(Toperation.sentence, takeout.sentence, nal.memory.lastDecision, nal, true, false, true);
                        for (Task t : seq_op) {
                            if (!t.sentence.isEternal()) {
                                // TODO do not return the eternal here probably..;
                                // only =/> </> ..
                                List<Task> res = proceedWithTemporalInduction(newEvent.sentence, t.sentence, newEvent, nal, true, true, false);
                            /*DEBUG: for(Task seq_op_cons : res) {
                                        System.out.println(seq_op_cons.toString());
                                    }*/
                            }
                        }
                    } catch (Exception ex) {
                        if (Parameters.DEBUG) {
                            System.out.println("issue in temporal induction");
                        }
                    }
                    opc.seq_before.putBack(takeout, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
                }
            }
            // put Toperation back into bag if it was taken out
            if (k > 0) {
                nal.memory.recent_operations.putBack(Toperation, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
            }
        }
    }
    addToSequenceTasks(nal, newEvent);
    return true;
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) Events(nars.io.events.Events) HashSet(java.util.HashSet)

Aggregations

Concept (nars.entity.Concept)30 Task (nars.entity.Task)18 Term (nars.language.Term)14 Sentence (nars.entity.Sentence)10 BudgetValue (nars.entity.BudgetValue)7 Events (nars.io.events.Events)6 CompoundTerm (nars.language.CompoundTerm)6 TruthValue (nars.entity.TruthValue)5 Interval (nars.language.Interval)4 ArrayList (java.util.ArrayList)3 Conjunction (nars.language.Conjunction)3 Color (java.awt.Color)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 DerivationContext (nars.control.DerivationContext)2 Item (nars.entity.Item)2 Stamp (nars.entity.Stamp)2 Implication (nars.language.Implication)2 NAR (nars.main.NAR)2 Memory (nars.storage.Memory)2