use of org.apache.commons.math3.stat.descriptive.rank.Max in project narchy by automenta.
the class Focus method update.
protected void update(NAR nar) {
if (!updating.compareAndSet(false, true))
return;
try {
int n = choice.size();
if (n == 0)
return;
if (sliceIters.length != n) {
// weight = new float[n];
time = new DescriptiveStatistics[n];
timeMean = new double[n];
done = new DescriptiveStatistics[n];
for (int i = 0; i < n; i++) {
time[i] = new DescriptiveStatistics(WINDOW);
done[i] = new DescriptiveStatistics(WINDOW);
}
// assert (n < 32) : "TODO make atomic n>32 bitset";
value = new float[n];
doneMean = new double[n];
doneMax = new long[n];
// last
sliceIters = new int[n];
}
revaluator.update(nar);
double jiffy = nar.loop.jiffy.floatValue();
double throttle = nar.loop.throttle.floatValue();
// / (n / concurrency);
timesliceNS = nar.loop.periodNS() * jiffy * throttle;
for (int i = 0; i < n; i++) {
Causable c = choice.get(i);
if (c == null)
// ?
continue;
c.can.commit(commiter);
long timeNS = committed[0];
if (timeNS > 0) {
DescriptiveStatistics t = this.time[i];
t.addValue(timeNS);
double timeMeanNS = this.timeMean[i] = t.getMean();
DescriptiveStatistics d = this.done[i];
d.addValue(committed[1]);
this.doneMean[i] = d.getMean();
this.doneMax[i] = Math.round(d.getMax());
// value per time
value[i] = (float) (c.value() / (Math.max(1E3, /* 1uS in nanos */
timeMeanNS) / 1E9));
} else {
// value[i] = unchanged
// slowly forget
value[i] *= 0.99f;
}
}
// double[] tRange = Util.minmax(timeMean);
// double tMin = tRange[0];
// double tMax = tRange[1];
// for (int i = 0; i < n; i++) {
// double tNorm = normalize(timeMean[i], tMin, tMax);
// value[i] /= ((float)tNorm);
// }
// weight[] = normalize(value[]) , with margin so the minimum value is non-zero some marginal amoutn (Margin-Max)
float[] vRange = Util.minmax(value);
float vMin = vRange[0];
float vMax = vRange[1];
// float lowMargin = (minmax[1] - minmax[0]) / n;
for (int i = 0; i < n; i++) {
double vNormPerTime = normalize(value[i], vMin, vMax);
// ;
int pri = (int) Util.clampI((PRI_GRANULARITY * vNormPerTime), 1, AtomicRoulette.PRI_GRANULARITY);
// the priority determined by the value primarily affects the probability of the choice being selected as a timeslice
priGetAndSet(i, pri);
// the iters per timeslice is determined by past measurements
long doneMost = doneMax[i];
double timePerIter = timeMean[i];
if (doneMost < 1 || !Double.isFinite(timePerIter)) {
// assume only one iteration will consume entire timeslice
timePerIter = timesliceNS;
}
sliceIters[i] = (int) Math.max(1, Math.ceil(// modulate growth by the normalized value
(/*vNormPerTime * */
timesliceNS / timePerIter) * Util.lerp(vNormPerTime, IterGrowthRateMin, IterGrowthRateMax) + IterGrowthIncrement));
// System.out.println(this.choice.get(i) + " "+ vNormPerTime + " " + sliceIters[i]);
}
// System.out.println();
} finally {
updating.set(false);
}
}
use of org.apache.commons.math3.stat.descriptive.rank.Max in project narchy by automenta.
the class NAL7ImplProjectionTest method test1.
@Test
public void test1() {
int implDT = 5;
int dur = 1;
/* eventTime, relative to impl belief */
for (int implTime = 0; implTime < 5; implTime += 2) {
for (int eventTime = 0; eventTime < 5; eventTime += 2) {
Term y = $.the("y");
Param.DEBUG = true;
NAR n = NARS.tmp();
// n.log();
n.time.dur(dur);
n.inputAt(eventTime, "x. :|:");
n.inputAt(implTime, "(x ==>+" + implDT + " y). :|:");
int end = Math.max(eventTime + implDT, implTime);
n.run(end + 1);
double[] max = new MyBrentOptimizer(0.001f, 0.01, 0, end, (t) -> {
Truth u = n.beliefTruth(y, Math.round(t));
if (u == null)
return -1f;
return u.conf();
}).max(0, end, end / 2.0);
long yTimeEstimate = Math.round(max[0]);
long yTimeActual = eventTime + implDT;
assertTrue(Math.abs(yTimeEstimate - yTimeActual) <= 1);
double yConfMax = max[1];
long eventBeliefDelta = Math.abs(eventTime - implTime);
System.out.println("+-" + eventBeliefDelta + " -> " + max[0] + "=" + max[1]);
}
}
// double zero = UnivariateSolverUtils.solve(t->{
// return 0;
// }, 0, end, 0.01f);
// n.concept(x).print();
// n.concept(y).print();
}
use of org.apache.commons.math3.stat.descriptive.rank.Max in project narchy by automenta.
the class Optimize method solve.
public void solve(int dim, ObjectiveFunction func, double[] mid, double[] min, double[] max, double[] inc, int maxIterations) {
if (dim == 1) {
// use a solver capable of 1 dim
new SimplexOptimizer(1e-10, 1e-30).optimize(new MaxEval(maxIterations), func, GoalType.MAXIMIZE, new InitialGuess(mid), // new NelderMeadSimplex(inc)
new MultiDirectionalSimplex(inc));
} else {
int popSize = // 4 + 3 ln(n)
(int) Math.ceil(4 + 3 * Math.log(tweaks.size()));
double[] sigma = MathArrays.scale(1f, inc);
MyCMAESOptimizer m = new MyCMAESOptimizer(maxIterations, Double.NaN, true, 0, 1, new MersenneTwister(System.nanoTime()), true, null, popSize, sigma);
m.optimize(func, GoalType.MAXIMIZE, new MaxEval(maxIterations), new SimpleBounds(min, max), new InitialGuess(mid));
m.print(System.out);
// final int numIterpolationPoints = 3 * dim; //2 * dim + 1 + 1;
// new BOBYQAOptimizer(numIterpolationPoints,
// dim * 2.0,
// 1.0E-4D /* ? */).optimize(
// MaxEval.unlimited(), //new MaxEval(maxIterations),
// new MaxIter(maxIterations),
// func,
// GoalType.MAXIMIZE,
// new SimpleBounds(min, max),
// new InitialGuess(mid));
}
}
use of org.apache.commons.math3.stat.descriptive.rank.Max in project narchy by automenta.
the class MyCMAESOptimizer method initializeCMA.
/**
* Initialization of the dynamic search parameters
*
* @param guess Initial guess for the arguments of the fitness function.
*/
private void initializeCMA(double[] guess) {
if (lambda <= 0) {
throw new NotStrictlyPositiveException(lambda);
}
// initialize sigma
final double[][] sigmaArray = new double[guess.length][1];
for (int i = 0; i < guess.length; i++) {
sigmaArray[i][0] = inputSigma[i];
}
final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
// overall standard deviation
sigma = max(insigma);
// initialize termination criteria
stopTolUpX = oNEtHOUSAND * max(insigma);
stopTolX = epsilonWTF11 * max(insigma);
this.stopTolFun = EPSILON_WTF12;
this.stopTolHistFun = epsilonwtf13;
// initialize selection strategy parameters
// number of parents/points for recombination
mu = lambda / 2;
/* log(mu + 0.5), stored for efficiency. */
double logMu2 = Math.log(mu + 0.5);
weights = log(sequence(1, mu, 1)).scalarMultiply(-1).scalarAdd(logMu2);
double sumw = 0;
double sumwq = 0;
for (int i = 0; i < mu; i++) {
double w = weights.getEntry(i, 0);
sumw += w;
sumwq += w * w;
}
weights = weights.scalarMultiply(1 / sumw);
// variance-effectiveness of sum w_i x_i
mueff = sumw * sumw / sumwq;
// initialize dynamic strategy parameters and constants
cc = (4 + mueff / dimension) / (dimension + 4 + 2 * mueff / dimension);
cs = (mueff + 2) / (dimension + mueff + 3.0);
damps = (1 + 2 * Math.max(0, Math.sqrt((mueff - 1) / (dimension + 1)) - 1)) * Math.max(0.3, 1 - dimension / (epsilon6WTF + maxIterations)) + // minor increment
cs;
ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff);
ccovmu = Math.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) / ((dimension + 2) * (dimension + 2) + mueff));
ccov1Sep = Math.min(1, ccov1 * (dimension + 1.5) / 3);
ccovmuSep = Math.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3);
chiN = Math.sqrt(dimension) * (1 - 1 / (4.0 * dimension) + 1 / (21.0 * dimension * dimension));
// intialize CMA internal values - updated each generation
// objective variables
xmean = MatrixUtils.createColumnRealMatrix(guess);
diagD = insigma.scalarMultiply(1 / sigma);
diagC = square(diagD);
// evolution paths for C and sigma
pc = zeros(dimension, 1);
// B defines the coordinate system
ps = zeros(dimension, 1);
normps = ps.getFrobeniusNorm();
B = eye(dimension, dimension);
// diagonal D defines the scaling
D = ones(dimension, 1);
BD = times(B, repmat(diagD.transpose(), dimension, 1));
// covariance
C = B.multiply(diag(square(D)).multiply(B.transpose()));
/* Size of history queue of best values. */
int historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
// history of fitness values
fitnessHistory = new double[historySize];
for (int i = 0; i < historySize; i++) {
fitnessHistory[i] = Double.POSITIVE_INFINITY;
}
}
use of org.apache.commons.math3.stat.descriptive.rank.Max in project OpenTripPlanner by opentripplanner.
the class Graph method calculateTransitCenter.
/**
* Calculates Transit center from median of coordinates of all transitStops if graph
* has transit. If it doesn't it isn't calculated. (mean walue of min, max latitude and longitudes are used)
*
* Transit center is saved in center variable
*
* This speeds up calculation, but problem is that median needs to have all of latitudes/longitudes
* in memory, this can become problematic in large installations. It works without a problem on New York State.
* @see GraphEnvelope
*/
public void calculateTransitCenter() {
if (hasTransit) {
TDoubleList latitudes = new TDoubleLinkedList();
TDoubleList longitudes = new TDoubleLinkedList();
Median median = new Median();
getVertices().stream().filter(v -> v instanceof TransitStop).forEach(v -> {
latitudes.add(v.getLat());
longitudes.add(v.getLon());
});
median.setData(latitudes.toArray());
double medianLatitude = median.evaluate();
median = new Median();
median.setData(longitudes.toArray());
double medianLongitude = median.evaluate();
this.center = new Coordinate(medianLongitude, medianLatitude);
}
}
Aggregations