use of nars.gui.NARSwing in project opennars by opennars.
the class SymRecognizer method jButton3ActionPerformed.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// GEN-FIRST:event_jButton3ActionPerformed
resetDetection();
StringBuilder build = new StringBuilder();
StringBuilder build2 = new StringBuilder();
build2.append("<(&|,");
for (int x = 0; x < SZ; x += 1) {
for (int y = 0; y < SZ; y += 1) {
int used_X = x;
int used_Y = y;
Color col1 = new Color(canvasIMG.getRGB(x, y));
float col = ((float) col1.getRed()) / 255.0f;
if (col > 0.0) {
float freq = 0.5f + (col - 0.5f);
if (invar.isSelected()) {
build.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>. :|: %" + String.valueOf(freq) + "%");
build.append("\n");
build2.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>,");
} else {
build.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>. :|: %" + String.valueOf(freq) + "%");
build.append("\n");
build2.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>,");
}
}
}
}
String s2 = build2.toString();
s2 = s2.substring(0, s2.length() - 1);
s2 = s2 + ")";
inputPanel.setText(build.toString());
if (evt == null) {
String question = "<{?what} --> [observed]>?";
additional[exid] = s2 + " ==> <{example" + exid + "} --> [observed]>>.";
inputPanel2.setText(additional[exid] + "\n" + question);
} else {
nar = new NAR();
if (invar1.isSelected()) {
gui = new NARSwing(nar);
}
int u = 0;
inputPanel2.setText("");
// for(String s : questions) {
String s = question;
{
if (s != null) {
AnswerHandler cur = new AnswerHandler() {
@Override
public void onSolution(Sentence belief) {
// System.out.println("solution: " + belief);
System.out.println(belief);
float howconf = belief.truth.getConfidence();
if (howconf > 0.1f) {
// only mark if above 0.1 confidence
// also mark image:
int maxu = Integer.valueOf(belief.getTerm().toString().split("example")[1].split("}")[0]);
clear();
for (int x = 0; x < SZ * scale_palette; x += 1) {
for (int y = 0; y < SZ * scale_palette; y += 1) {
Color col = new Color(canvasIMG.getRGB(x / scale_palette, y / scale_palette));
int k = getK[maxu];
int j = getJ[maxu];
exampleIMG.setRGB(x + k * scale_palette * SZ, y + (3 * j + 1) * scale_palette * SZ, new Color(col.getRed(), 0, 0).getRGB());
}
}
estimate.setIcon(new ImageIcon(fitimage(exampleIMG, estimate.getWidth(), estimate.getHeight())));
estimate.repaint();
}
}
};
q.add(cur);
try {
for (int h = 0; h < exid; h++) {
inputPanel2.setText(inputPanel2.getText() + additional[h] + "\n");
nar.addInput(additional[h]);
}
inputPanel2.setText(inputPanel2.getText() + s + "\n");
nar.ask(s.substring(0, s.length() - 1), cur);
} catch (Narsese.InvalidInputException ex) {
Logger.getLogger(SymRecognizer.class.getName()).log(Level.SEVERE, null, ex);
}
}
u++;
}
nar.param.noiseLevel.set(0);
nar.addInput(inputPanel.getText());
nar.start(0);
}
}
use of nars.gui.NARSwing in project opennars by opennars.
the class SymRecognizerWithVisionChannel method jButton3ActionPerformed.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// GEN-FIRST:event_jButton3ActionPerformed
resetDetection();
StringBuilder build = new StringBuilder();
StringBuilder build2 = new StringBuilder();
build2.append("<(&|,");
for (int x = 0; x < SZ; x += 1) {
for (int y = 0; y < SZ; y += 1) {
int used_X = x;
int used_Y = y;
Color col1 = new Color(canvasIMG.getRGB(x, y));
float col = ((float) col1.getRed()) / 255.0f;
if (col > 0.0) {
float freq = 0.5f + (col - 0.5f);
if (invar.isSelected()) {
build.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>. :|: %" + String.valueOf(freq) + "%");
build.append("\n");
build2.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>,");
} else {
build.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>. :|: %" + String.valueOf(freq) + "%");
build.append("\n");
build2.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>,");
}
}
}
}
String s2 = build2.toString();
s2 = s2.substring(0, s2.length() - 1);
s2 = s2 + ")";
inputPanel.setText(build.toString());
if (evt == null) {
String question = "<{?what} --> [observed]>?";
additional[exid] = s2 + " ==> <{example" + exid + "} --> [observed]>>.";
inputPanel2.setText(additional[exid] + "\n" + question);
} else {
// add vision channel for [on] property
nar = new NAR();
// to the nar instance, and nar is also the "next higher" sensory channel
// to report the results to
nar.addSensoryChannel("[on]", new SpatialSamplingVisionChannel(nar, nar, SZ, SZ));
if (invar1.isSelected()) {
gui = new NARSwing(nar);
}
int u = 0;
inputPanel2.setText("");
// for(String s : questions) {
String s = question;
{
if (s != null) {
AnswerHandler cur = new AnswerHandler() {
@Override
public void onSolution(Sentence belief) {
// System.out.println("solution: " + belief);
System.out.println(belief);
float howconf = belief.truth.getConfidence();
if (howconf >= 0.001f) {
// only mark if above 0.1 confidence
// also mark image:
int maxu = Integer.valueOf(belief.getTerm().toString().split("example")[1].split("}")[0]);
clear();
for (int x = 0; x < SZ * scale_palette; x += 1) {
for (int y = 0; y < SZ * scale_palette; y += 1) {
Color col = new Color(canvasIMG.getRGB(x / scale_palette, y / scale_palette));
int k = getK[maxu];
int j = getJ[maxu];
exampleIMG.setRGB(x + k * scale_palette * SZ, y + (3 * j + 1) * scale_palette * SZ, new Color(col.getRed(), 0, 0).getRGB());
}
}
estimate.setIcon(new ImageIcon(fitimage(exampleIMG, estimate.getWidth(), estimate.getHeight())));
estimate.repaint();
}
}
};
q.add(cur);
try {
for (int h = 0; h < exid; h++) {
inputPanel2.setText(inputPanel2.getText() + additional[h] + "\n");
nar.addInput(additional[h]);
}
inputPanel2.setText(inputPanel2.getText() + s + "\n");
nar.ask(s.substring(0, s.length() - 1), cur);
} catch (Narsese.InvalidInputException ex) {
Logger.getLogger(SymRecognizerWithVisionChannel.class.getName()).log(Level.SEVERE, null, ex);
}
}
u++;
}
nar.param.noiseLevel.set(0);
nar.addInput(inputPanel.getText());
nar.start(0);
}
}
use of nars.gui.NARSwing in project opennars by opennars.
the class KeyboardInputExample method main.
public static void main(String[] args) {
// NAR n = NAR.build(new Neuromorphic().realTime());
// NAR n = NAR.build(new Default().realTime());
// n.param.duration.set(100);
NARSwing.themeInvert();
NAR n = new NAR();
new NARSwing(n).themeInvert();
new NWindow("Direct Keyboard Input", new KeyboardInputPanel(n)).show(300, 100, false);
n.start(100);
}
use of nars.gui.NARSwing 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");
}*/
}
}
use of nars.gui.NARSwing 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;
}
}
Aggregations