Search in sources :

Example 31 with Max

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);
    }
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)

Example 32 with Max

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();
}
Also used : Param(nars.Param) Test(org.junit.jupiter.api.Test) NumberIsTooSmallException(org.apache.commons.math3.exception.NumberIsTooSmallException) Truth(nars.truth.Truth) Precision(org.apache.commons.math3.util.Precision) NotStrictlyPositiveException(org.apache.commons.math3.exception.NotStrictlyPositiveException) NARS(nars.NARS) NAR(nars.NAR) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) nars.$(nars.$) DoubleToDoubleFunction(org.eclipse.collections.api.block.function.primitive.DoubleToDoubleFunction) Term(nars.term.Term) Term(nars.term.Term) NAR(nars.NAR) Truth(nars.truth.Truth) Test(org.junit.jupiter.api.Test)

Example 33 with Max

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));
    }
}
Also used : MaxEval(org.apache.commons.math3.optim.MaxEval) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MultiDirectionalSimplex(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.MultiDirectionalSimplex) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) SimplexOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer) MersenneTwister(org.apache.commons.math3.random.MersenneTwister)

Example 34 with Max

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;
    }
}
Also used : NotStrictlyPositiveException(org.apache.commons.math3.exception.NotStrictlyPositiveException) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix)

Example 35 with Max

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);
    }
}
Also used : TDoubleList(gnu.trove.list.TDoubleList) TurnRestriction(org.opentripplanner.common.TurnRestriction) LoggerFactory(org.slf4j.LoggerFactory) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) StreetSpeedSnapshotSource(org.opentripplanner.traffic.StreetSpeedSnapshotSource) GraphUtils(org.opentripplanner.common.geometry.GraphUtils) StreetVertexIndexService(org.opentripplanner.routing.services.StreetVertexIndexService) Median(org.apache.commons.math3.stat.descriptive.rank.Median) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) Geometry(com.vividsolutions.jts.geom.Geometry) TraverseMode(org.opentripplanner.routing.core.TraverseMode) com.google.common.collect(com.google.common.collect) FeedInfo(org.onebusaway.gtfs.model.FeedInfo) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) MavenVersion(org.opentripplanner.common.MavenVersion) TransferTable(org.opentripplanner.routing.core.TransferTable) StreetNotesService(org.opentripplanner.routing.services.notes.StreetNotesService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SampleFactory(org.opentripplanner.analyst.request.SampleFactory) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) Stop(org.onebusaway.gtfs.model.Stop) Agency(org.onebusaway.gtfs.model.Agency) GraphUpdaterManager(org.opentripplanner.updater.GraphUpdaterManager) TimetableSnapshotSource(org.opentripplanner.updater.stoptime.TimetableSnapshotSource) StreetVertexIndexFactory(org.opentripplanner.routing.services.StreetVertexIndexFactory) java.util(java.util) AlertPatch(org.opentripplanner.routing.alertpatch.AlertPatch) WorldEnvelope(org.opentripplanner.util.WorldEnvelope) TDoubleList(gnu.trove.list.TDoubleList) TDoubleLinkedList(gnu.trove.list.linked.TDoubleLinkedList) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) NoFutureDates(org.opentripplanner.graph_builder.annotation.NoFutureDates) GraphUpdaterConfigurator(org.opentripplanner.updater.GraphUpdaterConfigurator) GraphBuilderAnnotation(org.opentripplanner.graph_builder.annotation.GraphBuilderAnnotation) GeometryIndex(org.opentripplanner.analyst.core.GeometryIndex) MortonVertexComparatorFactory(org.opentripplanner.routing.core.MortonVertexComparatorFactory) EdgeWithCleanup(org.opentripplanner.routing.edgetype.EdgeWithCleanup) Coordinate(com.vividsolutions.jts.geom.Coordinate) Logger(org.slf4j.Logger) Envelope(com.vividsolutions.jts.geom.Envelope) CalendarServiceImpl(org.onebusaway.gtfs.impl.calendar.CalendarServiceImpl) GraphBundle(org.opentripplanner.model.GraphBundle) DateTime(org.joda.time.DateTime) TransitStation(org.opentripplanner.routing.vertextype.TransitStation) Preferences(java.util.prefs.Preferences) java.io(java.io) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Deduplicator(org.opentripplanner.routing.trippattern.Deduplicator) PatternArriveVertex(org.opentripplanner.routing.vertextype.PatternArriveVertex) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Coordinate(com.vividsolutions.jts.geom.Coordinate) TDoubleLinkedList(gnu.trove.list.linked.TDoubleLinkedList) Median(org.apache.commons.math3.stat.descriptive.rank.Median)

Aggregations

ArrayList (java.util.ArrayList)26 List (java.util.List)19 Collectors (java.util.stream.Collectors)13 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)13 Arrays (java.util.Arrays)11 Map (java.util.Map)11 IntStream (java.util.stream.IntStream)10 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)10 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)9 RealMatrix (org.apache.commons.math3.linear.RealMatrix)9 Plot2 (ij.gui.Plot2)8 File (java.io.File)8 IOException (java.io.IOException)8 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)7 Test (org.testng.annotations.Test)7 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)6 Random (java.util.Random)6 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)6