Search in sources :

Example 1 with LineChart

use of automenta.vivisect.timeline.LineChart in project narchy by automenta.

the class NARCompare1 method main.

public static void main(String[] args) {
    int cycles = 128;
    String input = "<a --> b>.\n" + "<b --> c>.\n" + "<a --> c>?\n";
    NAR a = new Default().build();
    setA(a.param);
    NARTrace at = new NARTrace(a);
    a.addInput(input);
    a.run(cycles);
    NAR b = new Default().build();
    setB(b.param);
    NARTrace bt = new NARTrace(b);
    b.addInput(input);
    b.run(cycles);
    new NWindow("_", new PCanvas(new TimelineVis(new LineChart(at.getCharts("task.novel.add", "task.immediate_processed")).height(3), new LineChart(at.getCharts("task.goal.process", "task.question.process", "task.judgment.process")).height(3), new LineChart(at.getCharts("concept.priority.mean")).height(2), new LineChart(bt.getCharts("task.novel.add", "task.immediate_processed")).height(3), new LineChart(bt.getCharts("task.goal.process", "task.question.process", "task.judgment.process")).height(3), new LineChart(bt.getCharts("concept.priority.mean")).height(2)))).show(900, 800, true);
}
Also used : TimelineVis(automenta.vivisect.timeline.TimelineVis) NARTrace(nars.util.NARTrace) NWindow(automenta.vivisect.swing.NWindow) PCanvas(automenta.vivisect.swing.PCanvas) Default(nars.model.Default) NAR(nars.core.NAR) LineChart(automenta.vivisect.timeline.LineChart)

Example 2 with LineChart

use of automenta.vivisect.timeline.LineChart in project narchy by automenta.

the class Predict3 method main.

public static void main(String[] args) throws Narsese.InvalidInputException, InterruptedException {
    Parameters.DEBUG = true;
    int duration = 8;
    float freq = 1.0f / duration * 0.15f;
    int minCyclesAhead = 0;
    double missingDataRate = 0.1;
    double noiseRate = 0.02;
    boolean onlyNoticeChange = false;
    int thinkInterval = onlyNoticeChange ? 1 : 2;
    int discretization = 3;
    NAR n = new NAR(new Default().setInternalExperience(null));
    n.param.duration.set(duration);
    // n.param.duration.setLinear(0.5);
    n.param.conceptBeliefsMax.set(64);
    // n.param.conceptForgetDurations.set(16);
    Discretize discretize = new Discretize(n, discretization);
    TreeMLData observed = new TreeMLData("value", Color.WHITE).setRange(0, 1f);
    TreeMLData[] predictions = new TreeMLData[discretization];
    TreeMLData[] reflections = new TreeMLData[discretization];
    for (int i = 0; i < predictions.length; i++) {
        predictions[i] = new TreeMLData("Pred" + i, Color.getHSBColor(0.25f + i / 4f, 0.85f, 0.85f));
        // predictions[i].setDefaultValue(0.0);
        reflections[i] = new TreeMLData("Refl" + i, Color.getHSBColor(0.25f + i / 4f, 0.85f, 0.85f));
        reflections[i].setDefaultValue(0.0);
    // predictions[i].setRange(0, 0.5);
    }
    TimelineVis tc = new TimelineVis(new LineChart(observed).thickness(16f).height(128), new LineChart(predictions).thickness(16f).height(128), new LineChart(reflections).thickness(16f).height(128));
    // new BarChart(error).height(4)
    new NWindow("_", new PCanvas(tc)).show(800, 800, true);
    new TextOutput(n, System.out) {

        /**
         * dt = relative to center
         */
        public double getPredictionEnvelope(double dt, double duration) {
            // guassian curve width=duration
            // e^(-(4*x/(dur))^2)
            double p = (4 * dt / duration);
            return Math.exp(-(p * p));
        }

        /**
         * only allow future predictions
         */
        protected boolean allowTask(Task t) {
            if (t.sentence.isEternal()) {
                return false;
            }
            boolean future = false;
            if ((t.sentence.getOccurenceTime() > n.time() + minCyclesAhead)) {
                System.out.print(n.time() + ".." + t.sentence.getOccurenceTime() + ": ");
                future = true;
            }
            Term term = t.getTerm();
            int time = (int) t.sentence.getOccurenceTime();
            int value = -1;
            float conf = t.sentence.truth.getConfidence();
            float expect = 2f * (t.sentence.truth.getFrequency() - 0.5f) * conf;
            String ts = term.toString();
            if (ts.startsWith("<x_t0 --> y")) {
                char cc = ts.charAt("<x_t0 --> y".length());
                value = cc - '0';
            }
            if (value != -1) {
                // predictions[(int)value].addPlus(time, expect);
                for (int tt = time - duration / 2; tt <= time + duration / 2; tt++) {
                    double smooth = 1;
                    expect *= getPredictionEnvelope(time - tt, smooth * duration * 2f);
                /*
                            if (future)
                                predictions[value].addPlus(tt, expect);
                            else
                                reflections[value].addPlus(tt, expect);
                            */
                }
            }
            return true;
        }
    };
    for (Term t : discretize.getValueTerms("x")) n.believe(t.toString(), Tense.Present, 0.5f, 0.5f);
    // TODO move this to discretize.getDisjunctionBelief
    n.believe("<(||,y0,y1,y2,y3,y4,y5,y6,y7) --> y>", Tense.Eternal, 1.0f, 0.95f);
    n.run(discretization * 4);
    // new TextOutput(n, System.out);
    Concept[] valueBeliefConcepts = discretize.getValueConcepts("x");
    NARSwing.themeInvert();
    new NWindow("x", new ConceptsPanel(n, valueBeliefConcepts)).show(900, 600, true);
    RNNBeliefPrediction predictor = new RNNBeliefPrediction(n, valueBeliefConcepts) {

        @Override
        public double[] getTrainedPrediction(double[] input) {
            return input;
        }

        @Override
        public int getPredictionSize() {
            return getInputSize();
        // return 1;
        }

        @Override
        protected double[] predict() {
            double[] x = super.predict();
            if (x == null)
                return null;
            long t = n.time();
            for (int i = 0; i < x.length; i++) {
                // - x[i*2+1]);
                predictions[i].add((int) t, x[i]);
            }
            return x;
        }
    };
    // new NARSwing(n);
    int prevY = -1, curY = -1;
    long prevT = n.time();
    while (true) {
        n.run(thinkInterval);
        Thread.sleep(3);
        // n.memory.addSimulationTime(1);
        signal = (float) Math.max(0, Math.min(1.0, Math.tan(freq * n.time()) * 0.5f + 0.5f));
        // signal  = (float)Math.sin(freq * n.time()) * 0.5f + 0.5f;
        // signal = ((float) Math.sin(freq * n.time()) > 0 ? 1f : -1f) * 0.5f + 0.5f;
        signal *= 1.0 + (Math.random() - 0.5f) * 2f * noiseRate;
        if (Math.random() > missingDataRate)
            observed.add((int) n.time(), signal);
        prevY = curY;
        curY = discretize.i(signal);
        if ((curY == prevY) && (onlyNoticeChange)) {
            continue;
        }
        discretize.believe("x", signal, 0);
        // input(prevT, Term.get("x_t0"), prevY, Term.get("x_t0"), curY, 1f, n.memory);
        // input(prevT, Term.get("x_t0"), prevY, Term.get("x_t0"), 1f-curY, 0.5f, n.memory);
        // n.addInput("notice(x_t0," + value + ",(||,y0,y1))!");
        // input(prevT, Term.get("x_tMin1"), prevY, Term.get("x_t0"), 1f-y, 0f, n.memory);
        // input(Term.get("x_t0"), Term.get(value), 0.0f, 0.0f, n.memory);              //input(Term.get("x_t0"), Term.get(otherValue), 1.0f, 0f, n.memory);  */
        /*
            n.believe(xFuncEq0, Tense.Present, 1.0f, y);
            n.believe(xFuncEq0, Tense.Present, 0.0f, 1f - y);
            n.believe(xFuncEq1, Tense.Present, 1.0f, 1f - y);
            n.believe(xFuncEq1, Tense.Present, 0.0f, y);
                    */
        prevT = n.time();
    }
}
Also used : TextOutput(nars.io.TextOutput) Concept(nars.nal.entity.Concept) Task(nars.nal.entity.Task) NWindow(automenta.vivisect.swing.NWindow) Term(nars.nal.language.Term) Default(nars.model.Default) TimelineVis(automenta.vivisect.timeline.TimelineVis) TreeMLData(nars.util.TreeMLData) PCanvas(automenta.vivisect.swing.PCanvas) ConceptsPanel(nars.gui.output.ConceptsPanel) NAR(nars.core.NAR) LineChart(automenta.vivisect.timeline.LineChart)

Example 3 with LineChart

use of automenta.vivisect.timeline.LineChart in project narchy by automenta.

the class Predict_NARS_Core method main.

public static void main(String[] args) throws Narsese.InvalidInputException, InterruptedException {
    Parameters.DEBUG = true;
    int duration = 8;
    float freq = 1.0f / duration * 0.1f;
    int thinkInterval = 50;
    double discretization = 3;
    NAR n = new NAR(new Default());
    n.param.duration.set(duration);
    n.param.noiseLevel.set(0);
    // n.param.conceptForgetDurations.set(16);
    n.on(TaskImmediateProcess.class, new TaskImmediateProcess() {

        int curmax = 0;

        @Override
        public void onProcessed(Task t, NAL n) {
            if (t.sentence.getOccurenceTime() >= n.memory.time() && t.sentence.truth.getExpectation() > 0.5) {
                Term term = t.getTerm();
                int time = (int) t.sentence.getOccurenceTime();
                int value = -1;
                String ts = term.toString();
                if (ts.startsWith("<{x} --> y")) {
                    char cc = ts.charAt("<{x} --> y".length());
                    value = cc - '0';
                    if (time >= curmax) {
                        curmax = time;
                    }
                    maxval = Math.max(maxval, (value) / 10.0);
                    predictions[0].add(time, (value) / 10.0);
                }
            }
        }
    });
    TreeMLData observed = new TreeMLData("value", Color.WHITE).setRange(0, 1f);
    predictions = new TreeMLData[(int) discretization];
    TreeMLData[] reflections = new TreeMLData[(int) discretization];
    for (int i = 0; i < predictions.length; i++) {
        predictions[i] = new TreeMLData("Pred" + i, Color.getHSBColor(0.25f + i / 4f, 0.85f, 0.85f));
        reflections[i] = new TreeMLData("Refl" + i, Color.getHSBColor(0.25f + i / 4f, 0.85f, 0.85f));
        reflections[i].setDefaultValue(0.0);
    }
    TimelineVis tc = new TimelineVis(new LineChart(0, 1, observed).thickness(16f).height(128), new LineChart(predictions[0]).thickness(16f).height(128));
    new NWindow("_", new PCanvas(tc)).show(800, 800, true);
    n.run((int) discretization * 4);
    NARSwing.themeInvert();
    new NARSwing(n);
    ChangedTextInput chg = new ChangedTextInput(n);
    double lastsignal = 0;
    double lasttime = 0;
    while (true) {
        n.run(thinkInterval);
        Thread.sleep(30);
        // signal  = (float)Math.max(0, Math.min(1.0, Math.tan(freq * n.time()) * 0.5f + 0.5f));
        signal = (float) Math.sin(freq * n.time()) * 0.5f + 0.5f;
        // signal = ((float) Math.sin(freq * n.time()) > 0 ? 1f : -1f) * 0.5f + 0.5f;
        // signal *= 1.0 + (Math.random()-0.5f)* 2f * noiseRate;
        // this
        observed.removeData((int) (lasttime + 1));
        // is not good practice
        observed.removeData((int) (lasttime + 2));
        observed.add((int) n.time(), signal);
        // but is fine
        observed.add((int) n.time() + 1, -1);
        // for now (just wanted a line at the end)
        observed.add((int) n.time() + 2, 1);
        lastsignal = signal;
        lasttime = n.time();
        predictions[0].setData(0, maxval);
        // if(cnt<1000) { //switch to see what NARS does when observations end :)
        int val = (int) (((int) ((signal * discretization)) * (10.0 / discretization)));
        chg.set("<{x} --> y" + val + ">. :|:");
    // System.out.println(val);
    /*} else if (cnt==1000){
                System.out.println("observation phase end, residual predictions follow");
            }*/
    }
}
Also used : Task(nars.nal.entity.Task) ChangedTextInput(nars.io.ChangedTextInput) NWindow(automenta.vivisect.swing.NWindow) Term(nars.nal.language.Term) Default(nars.model.Default) TimelineVis(automenta.vivisect.timeline.TimelineVis) TaskImmediateProcess(nars.core.Events.TaskImmediateProcess) NAL(nars.cycle.NAL) NARSwing(nars.gui.NARSwing) PCanvas(automenta.vivisect.swing.PCanvas) NAR(nars.core.NAR) LineChart(automenta.vivisect.timeline.LineChart)

Example 4 with LineChart

use of automenta.vivisect.timeline.LineChart in project narchy by automenta.

the class BudgetExpense method main.

public static void main(String[] args) throws Exception {
    int cycles = 1000;
    NAR nar = new Default().build();
    NARTrace t = new NARTrace(nar);
    nar.addInput("<a --> b>.");
    nar.addInput("<b --> c>.");
    nar.addInput("<(^pick,x) =\\> a>.");
    nar.addInput("<(*, b, c) <-> x>.");
    nar.addInput("a!");
    TimelineVis tc = new TimelineVis(new LineChart(t.getCharts("task.novel.add", "task.immediate_processed")).height(3), new LineChart(t.getCharts("task.goal.process", "task.question.process", "task.judgment.process")).height(3), new LineChart(t.getCharts("emotion.busy")).height(1));
    nar.run(cycles);
    new NWindow("_", new PCanvas(tc)).show(800, 800, true);
}
Also used : TimelineVis(automenta.vivisect.timeline.TimelineVis) NARTrace(nars.util.NARTrace) NWindow(automenta.vivisect.swing.NWindow) PCanvas(automenta.vivisect.swing.PCanvas) Default(nars.model.Default) NAR(nars.core.NAR) LineChart(automenta.vivisect.timeline.LineChart)

Example 5 with LineChart

use of automenta.vivisect.timeline.LineChart in project opennars by opennars.

the class Predict_NARS_Core method main.

public static void main(String[] args) throws Narsese.InvalidInputException, InterruptedException {
    Parameters.DEBUG = false;
    int duration = 4;
    float freq = 1.0f / duration * 0.03f;
    double discretization = 10;
    n = new NAR();
    n.param.noiseLevel.set(0);
    Random rnd = new Random();
    n.on(TaskImmediateProcess.class, new TaskImmediateProcess() {

        @Override
        public void onProcessed(Task t, DerivationContext n) {
            // return;
            if (t.sentence.getOccurenceTime() >= n.memory.time() && t.sentence.truth.getExpectation() > 0.5) {
                Term term = t.getTerm();
                int time = (int) t.sentence.getOccurenceTime() / thinkInterval;
                /*if(positionTruthExp.containsKey(time)) {
                        if(positionTruthExp.get(time) > t.sentence.truth.getExpectation()) {
                            return;
                        }
                    }*/
                int value = -1;
                String ts = term.toString();
            // Prediction(t.sentence, ts, time);
            }
        }
    });
    TreeMLData observed = new TreeMLData("value", Color.WHITE).setRange(0, 1f);
    // predictions = new TreeMLData[(int)discretization];
    predicted = new TreeMLData("seen", Color.WHITE).setRange(0, 1f);
    // for (int i = 0; i < predictions.length; i++) {
    // predictions[i] = new TreeMLData("Pred" + i,
    // Color.getHSBColor(0.25f + i / 4f, 0.85f, 0.85f));
    // }
    pred = (LineChart) new LineChart(predicted).thickness(16f).height(128).drawOverlapped();
    TimelineVis tc = new TimelineVis(pred, new LineChart(observed).thickness(16f).height(128).drawOverlapped());
    new NWindow("_", new PCanvas(tc)).show(800, 800, true);
    n.cycles((int) discretization * 4);
    NARSwing.themeInvert();
    new NARSwing(n);
    ChangedTextInput chg = new ChangedTextInput(n);
    int k = 0;
    String lastInput = "";
    boolean pause = false;
    HashSet<String> qus = new HashSet<String>();
    int truecnt = 0;
    String saved = "";
    int lastOrival = 0;
    while (true) {
        int steps = 40;
        int h = 0;
        do {
            truecnt++;
            if (truecnt % 100 == 0) {
            // qus.clear();
            }
            for (int i = 0; i < thinkInterval; i++) {
                n.cycles(1);
            }
            Thread.sleep(10);
            h++;
            int repeat = 500;
            signal = (float) Math.sin(freq * (k / 2 % 500) - Math.PI / 2) * 0.5f + 0.5f;
            int time = (int) (n.time() / thinkInterval);
            int val2 = (int) (((int) (((signal) * discretization)) * (10.0 / discretization)));
            // System.out.println("observed "+val);
            int tmp = val2;
            if (!pause && val2 != lastOrival) {
                float noise_amp = 0.5f;
                // noise
                val2 += (rnd.nextDouble() * noise_amp * 0.5f - noise_amp * 0.25f);
            }
            lastOrival = tmp;
            final int val = val2;
            lastInput = "<{" + val + "} --> value>. :|:";
            if (k % repeat == 0 && k != 0) {
                pause = true;
                // new run
                n.memory.seq_current.clear();
            }
            if (!pause && !saved.isEmpty()) {
                chg.set(saved);
                saved = "";
            }
            observed.add((int) time, val / 10.0);
            int curval = val;
            if (QUAnswers.containsKey(val)) {
                curval = QUAnswers.get(val);
            }
            int curtime = time;
            int hh = 0;
            // QUAnswers.put(8, 0); //end to start link is fixed
            while (QUAnswers.containsKey(curval)) {
                int shift = QUShift.get(curval) / thinkInterval;
                for (int i = 0; i < shift; i++) {
                    predicted.add((int) curtime + i, (curval) / 10.0);
                    pred.customColor.put(curtime + i, Color.RED.getRGB());
                    pred.customColor.put(curtime + i + 1, Color.RED.getRGB());
                }
                curtime = curtime + shift;
                curval = QUAnswers.get(curval);
                if (curval == 0) {
                    break;
                }
                hh++;
                if (hh > discretization * 2) {
                    break;
                }
            }
            // if(!positionTruthExp.containsKey(time)) { //keep pred line up to date
            // but don't overwrite predictions
            predicted.add((int) time, val / 10.0);
            if (true) {
                chg.set(lastInput);
                if (!pause) {
                } else {
                    saved = lastInput;
                }
                // n.addInput(lastInput);
                String S = "<(&/," + "<{" + val + "} --> value>,?I1) =/> ?what>";
                if (!qus.contains(S)) {
                    // n.addInput(S);
                    AnswerHandler cur = new AnswerHandler() {

                        @Override
                        public void onSolution(Sentence belief) {
                            // System.out.println("solution: " + belief);
                            System.out.println(belief);
                            String rpart = belief.toString().split("=/>")[1];
                            Prediction(belief, rpart, -1, val);
                        }
                    };
                    try {
                        // if(truecnt%10 == 0) {
                        qus.add(S);
                        n.askNow(S, cur);
                    // }
                    } catch (Narsese.InvalidInputException ex) {
                        Logger.getLogger(Predict_NARS_Core.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            if (h > 20) {
                pause = false;
            }
        } while (pause);
        k += steps;
    }
}
Also used : Task(nars.entity.Task) ChangedTextInput(nars.lab.ioutils.ChangedTextInput) NWindow(automenta.vivisect.swing.NWindow) Narsese(nars.io.Narsese) AnswerHandler(nars.io.events.AnswerHandler) TimelineVis(automenta.vivisect.timeline.TimelineVis) Random(java.util.Random) TreeMLData(automenta.vivisect.TreeMLData) PCanvas(automenta.vivisect.swing.PCanvas) Sentence(nars.entity.Sentence) HashSet(java.util.HashSet) Term(nars.language.Term) DerivationContext(nars.control.DerivationContext) TaskImmediateProcess(nars.io.events.Events.TaskImmediateProcess) NARSwing(nars.gui.NARSwing) NAR(nars.main.NAR) LineChart(automenta.vivisect.timeline.LineChart)

Aggregations

NWindow (automenta.vivisect.swing.NWindow)5 PCanvas (automenta.vivisect.swing.PCanvas)5 LineChart (automenta.vivisect.timeline.LineChart)5 TimelineVis (automenta.vivisect.timeline.TimelineVis)5 NAR (nars.core.NAR)4 Default (nars.model.Default)4 NARSwing (nars.gui.NARSwing)2 Task (nars.nal.entity.Task)2 Term (nars.nal.language.Term)2 NARTrace (nars.util.NARTrace)2 TreeMLData (automenta.vivisect.TreeMLData)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 DerivationContext (nars.control.DerivationContext)1 TaskImmediateProcess (nars.core.Events.TaskImmediateProcess)1 NAL (nars.cycle.NAL)1 Sentence (nars.entity.Sentence)1 Task (nars.entity.Task)1 ConceptsPanel (nars.gui.output.ConceptsPanel)1 ChangedTextInput (nars.io.ChangedTextInput)1