use of org.eclipse.collections.api.block.function.primitive.FloatFunction in project eclipse-collections by eclipse.
the class ParallelIterableTestCase method sumOfFloatConsistentRounding.
@Test
public void sumOfFloatConsistentRounding() {
FloatFunction<Integer> roundingSensitiveElementFunction = i -> (i <= 99995) ? 1.0e-18f : 1.0f;
MutableList<Integer> list = Interval.oneTo(100_000).toList().shuffleThis();
double baseline = this.getExpectedWith(list.toArray(new Integer[] {})).sumOfFloat(roundingSensitiveElementFunction);
for (Integer batchSize : BATCH_SIZES) {
this.batchSize = batchSize;
ParallelIterable<Integer> testCollection = this.newWith(list.toArray(new Integer[] {}));
Assert.assertEquals("Batch size: " + this.batchSize, baseline, testCollection.sumOfFloat(roundingSensitiveElementFunction), 1.0e-15d);
}
}
use of org.eclipse.collections.api.block.function.primitive.FloatFunction in project narchy by automenta.
the class Optimize method run.
public Result<X> run(int maxIterations, int repeats, FloatFunction<Supplier<X>> eval) {
assert (repeats >= 1);
final int dim = tweaks.size();
double[] mid = new double[dim];
// double[] sigma = new double[n];
double[] min = new double[dim];
double[] max = new double[dim];
double[] inc = new double[dim];
// double[] range = new double[dim];
X example = subject.get();
int i = 0;
for (Tweak w : tweaks) {
TweakFloat s = (TweakFloat) w;
// initial guess: get from sample, otherwise midpoint of min/max range
Object guess = s.get(example);
mid[i] = guess != null ? ((float) guess) : ((s.getMax() + s.getMin()) / 2f);
min[i] = (s.getMin());
max[i] = (s.getMax());
inc[i] = s.getInc();
// range[i] = max[i] - min[i];
// sigma[i] = Math.abs(max[i] - min[i]) * 0.75f; //(s.getInc());
i++;
}
FasterList<DoubleObjectPair<double[]>> experiments = new FasterList<>(maxIterations);
final double[] maxScore = { Double.NEGATIVE_INFINITY };
ObjectiveFunction func = new ObjectiveFunction(point -> {
double score;
try {
double sum = 0;
for (int r = 0; r < repeats; r++) {
Supplier<X> x = () -> subject(point);
sum += eval.floatValueOf(x);
}
score = sum / repeats;
} catch (Exception e) {
logger.error("{} {} {}", this, point, e);
score = Float.NEGATIVE_INFINITY;
}
if (trace)
csv.out(ArrayUtils.add(point, (int) 0, score));
maxScore[0] = Math.max(maxScore[0], score);
// System.out.println(
// n4(score) + " / " + n4(maxScore[0]) + "\t" + n4(point)
// );
experiments.add(pair(score, point));
experimentIteration(point, score);
return score;
});
if (trace)
csv = new CSVOutput(System.out, Stream.concat(Stream.of("score"), tweaks.stream().map(t -> t.id)).toArray(String[]::new));
experimentStart();
try {
solve(dim, func, mid, min, max, inc, maxIterations);
} catch (Throwable t) {
logger.info("solve {} {}", func, t);
}
return new Result<>(experiments, tweaks);
}
use of org.eclipse.collections.api.block.function.primitive.FloatFunction in project narchy by automenta.
the class ExeCharts method metaGoalChart.
private static Surface metaGoalChart(NAgent a) {
return new TreeChart<Cause>() {
final DurService on;
final FasterList<ItemVis<Cause>> cache = new FasterList();
final Function<Cause, TreeChart.ItemVis<Cause>> builder = ((i) -> {
short id = i.id;
ItemVis<Cause> item;
if (cache.capacity() - 1 < id)
cache.ensureCapacity(id + 16);
else {
item = cache.get(id);
if (item != null)
return item;
}
String str = i.toString();
if (str.startsWith("class nars."))
// skip default toString
str = str.substring("class nars.".length());
if (str.startsWith("class "))
// skip default toString
str = str.substring(5);
item = new CauseVis(i, str);
cache.set(id, item);
return item;
});
{
on = a.onFrame(() -> {
update(a.nar().causes, (c, i) -> {
float v = c.value();
float r, g, b;
if (v < 0) {
r = 0.75f * Math.max(0.1f, Math.min(1f, -v));
g = 0;
} else {
g = 0.75f * Math.max(0.1f, Math.min(1f, +v));
r = 0;
}
float t = Util.sum(((FloatFunction<Traffic>) (p -> Math.abs(p.last))), c.goal);
b = Math.max(r, g) / 2f * Util.unitize(t);
i.update(v, r, g, b);
// i.updateMomentum(
// //0.01f + Util.sqr(Util.tanhFast(v)+1),
// //Math.signum(v) *(1+Math.abs(v))*(t),
// //Math.signum(v) * t,
// v,
// 0.25f,
// r, g, b);
}, builder);
});
}
@Override
public void stop() {
super.stop();
on.off();
}
};
}
Aggregations