Search in sources :

Example 6 with TreeMLData

use of automenta.vivisect.TreeMLData in project opennars by opennars.

the class LineChart method getStart.

@Override
public int getStart() {
    start = Integer.MAX_VALUE;
    end = 0;
    for (TreeMLData s : data) {
        int ss = s.getStart();
        int se = s.getEnd();
        if (start > ss)
            start = ss;
        if (end < se)
            end = se;
    }
    return start;
}
Also used : TreeMLData(automenta.vivisect.TreeMLData)

Example 7 with TreeMLData

use of automenta.vivisect.TreeMLData in project opennars by opennars.

the class SpectrumChart method update.

protected void update() {
    TreeMLData chart = data.get(0);
    float length = chart.getEnd() - chart.getStart();
    ;
    int numWindows = (int) Math.ceil(length / windowSize);
    // TODO dont remove existing windows
    windows.clear();
    int t = 0;
    for (int w = 0; w < numWindows; w++) {
        final int vl = windowSize;
        double[] input = new double[vl];
        for (int i = 0; i < vl; i++) {
            input[i] = (float) chart.getData(t++);
        }
        Complex[] c = fft.transform(input, TransformType.FORWARD);
        // phase = atan2( imaginary , real )
        // magnitude = sqrt( real<sup>2</sup> + imaginary<sup>2</sup> )
        windows.add(new Window(c));
    }
}
Also used : TreeMLData(automenta.vivisect.TreeMLData) Complex(org.apache.commons.math3.complex.Complex)

Example 8 with TreeMLData

use of automenta.vivisect.TreeMLData in project opennars by opennars.

the class StackedPercentageChart method drawData.

@Override
protected void drawData(TimelineVis l, float timeScale, float yScale, float y) {
    l.g.noStroke();
    for (int t = l.cycleStart; t < l.cycleEnd; t++) {
        float total = 0;
        for (TreeMLData chart : data) {
            float v = (float) chart.getData(t);
            if (Float.isNaN(v)) {
                continue;
            }
            total += v;
        }
        if (total == 0) {
            continue;
        }
        float sy = y;
        float gap = yScale * (1.0f - barHeight) / data.size();
        l.g.strokeWeight(1f);
        for (TreeMLData chart : data) {
            int ccolor = chart.getColor();
            float x = (t - l.cycleStart) * timeScale;
            float v = (float) chart.getData(t);
            if (Float.isNaN(v)) {
                continue;
            }
            float p = v / total;
            float px = width * x;
            float h = p * yScale;
            l.g.fill(ccolor, 255f * (0.5f + 0.5f * p));
            l.g.rect(px, sy + gap / 2, width * timeScale * barWidth, h - gap / 2);
            sy += h;
        }
    }
}
Also used : TreeMLData(automenta.vivisect.TreeMLData)

Example 9 with TreeMLData

use of automenta.vivisect.TreeMLData 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

TreeMLData (automenta.vivisect.TreeMLData)9 NWindow (automenta.vivisect.swing.NWindow)1 PCanvas (automenta.vivisect.swing.PCanvas)1 LineChart (automenta.vivisect.timeline.LineChart)1 TimelineVis (automenta.vivisect.timeline.TimelineVis)1 Graphics2D (java.awt.Graphics2D)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 DerivationContext (nars.control.DerivationContext)1 Sentence (nars.entity.Sentence)1 Task (nars.entity.Task)1 NARSwing (nars.gui.NARSwing)1 Narsese (nars.io.Narsese)1 AnswerHandler (nars.io.events.AnswerHandler)1 TaskImmediateProcess (nars.io.events.Events.TaskImmediateProcess)1 ChangedTextInput (nars.lab.ioutils.ChangedTextInput)1 Term (nars.language.Term)1 NAR (nars.main.NAR)1 Complex (org.apache.commons.math3.complex.Complex)1