use of jcog.math.MutableInteger in project narchy by automenta.
the class LivePredictorTest method assertCorrect.
static void assertCorrect(IntToFloatFunction ii, IntToFloatFunction oo, LivePredictor.Predictor model, int iHistory, int errorWindow, int totalTime, float maxMeanError) {
MutableInteger m = new MutableInteger();
FloatSupplier[] in = { () -> ii.valueOf(m.intValue()), () -> oo.valueOf(m.intValue() - 1) };
FloatSupplier[] out = { () -> oo.valueOf(m.intValue()) };
LivePredictor.HistoryFramer ih = new LivePredictor.HistoryFramer(in, iHistory, out);
LivePredictor l = new LivePredictor(model, ih);
DescriptiveStatistics error = new DescriptiveStatistics(errorWindow);
for (int i = 0; i < totalTime; i++, m.increment()) {
double[] prediction = l.next();
// test time shift preseves previous value;
{
float[] i0 = ih.data.get(0).data;
assertEquals(i0[0], in[0].asFloat(), 0.001f);
if (i > 1)
assertEquals(i0[1], ii.valueOf(m.intValue() - 1), 0.001f);
}
double predicted = prediction[0];
double actual = oo.valueOf(m.intValue() + 1);
// absolute error
double e = Math.abs(actual - predicted);
error.addValue(e);
// System.out.println( n4(predicted) + "\t" + n4(actual));
}
double eMean = error.getMean();
assertTrue(eMean < maxMeanError, () -> "mean error: " + eMean);
}
use of jcog.math.MutableInteger in project narchy by automenta.
the class NAR method outputText.
@NotNull
public NAR outputText(@NotNull OutputStream o, @NotNull Function<Task, Task> each) {
// runLater(() -> {
// SnappyFramedOutputStream os = new SnappyFramedOutputStream(o);
PrintStream ps = new PrintStream(o);
MutableInteger total = new MutableInteger(0), wrote = new MutableInteger(0);
StringBuilder sb = new StringBuilder();
tasks().map(each).filter(Objects::nonNull).forEach(x -> {
total.increment();
if (x.truth() != null && x.conf() < confMin.floatValue())
// ignore task if it is below confMin
return;
sb.setLength(0);
ps.println(x.appendTo(sb, true));
});
return this;
}
use of jcog.math.MutableInteger in project narchy by automenta.
the class NAR method outputBinary.
/**
* byte codec output of matching concept tasks (blocking)
* <p>
* the each function allows transforming each task to an optional output form.
* if this function returns null it will not output that task (use as a filter).
*/
public NAR outputBinary(OutputStream o, Function<Task, Task> each) {
// runLater(() -> {
DataOutputStream oo = new DataOutputStream(o);
MutableInteger total = new MutableInteger(0), wrote = new MutableInteger(0);
tasks().map(each).filter(Objects::nonNull).forEach(x -> {
total.increment();
byte[] b = IO.taskToBytes(x);
if (Param.DEBUG) {
// HACK temporary until this is debugged
Task xx = IO.taskFromBytes(b);
if (xx == null || !xx.equals(x)) {
throw new RuntimeException("task serialization problem: " + x + " != " + xx);
}
}
try {
oo.write(b);
} catch (IOException e) {
throw new RuntimeException(e);
}
wrote.increment();
});
logger.debug("{} output {}/{} tasks ({} bytes)", o, wrote, total, oo.size());
try {
oo.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
return this;
}
Aggregations