Search in sources :

Example 51 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min in project xian by happyyangyuan.

the class TestDistributedAtomicLong method testSimulation.

@Test
public void testSimulation() throws Exception {
    final int threadQty = 20;
    final int executionQty = 50;
    final AtomicInteger optimisticTries = new AtomicInteger();
    final AtomicInteger promotedLockTries = new AtomicInteger();
    final AtomicInteger failures = new AtomicInteger();
    final AtomicInteger errors = new AtomicInteger();
    final SummaryStatistics timingStats = new SynchronizedSummaryStatistics();
    List<Future<Void>> procs = Lists.newArrayList();
    ExecutorService executorService = Executors.newFixedThreadPool(threadQty);
    for (int i = 0; i < threadQty; ++i) {
        Callable<Void> proc = new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                doSimulation(executionQty, timingStats, optimisticTries, promotedLockTries, failures, errors);
                return null;
            }
        };
        procs.add(executorService.submit(proc));
    }
    for (Future<Void> f : procs) {
        f.get();
    }
    System.out.println("OptimisticTries: " + optimisticTries.get());
    System.out.println("PromotedLockTries: " + promotedLockTries.get());
    System.out.println("Failures: " + failures.get());
    System.out.println("Errors: " + errors.get());
    System.out.println();
    System.out.println("Avg time: " + timingStats.getMean());
    System.out.println("Max time: " + timingStats.getMax());
    System.out.println("Min time: " + timingStats.getMin());
    System.out.println("Qty: " + timingStats.getN());
    Assert.assertEquals(errors.get(), 0);
    Assert.assertTrue(optimisticTries.get() > 0);
    Assert.assertTrue(promotedLockTries.get() > 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) SynchronizedSummaryStatistics(org.apache.commons.math3.stat.descriptive.SynchronizedSummaryStatistics) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) Future(java.util.concurrent.Future) SynchronizedSummaryStatistics(org.apache.commons.math3.stat.descriptive.SynchronizedSummaryStatistics) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test)

Example 52 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min in project narchy by automenta.

the class MyCMAESOptimizer method doOptimize.

// /**
// * {@inheritDoc}
// *
// * @param optData Optimization data. In addition to those documented in
// * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[])
// * MultivariateOptimizer}, this method will register the following data:
// * <ul>
// *  <li>{@link MyCMAESOptimizer.Sigma}</li>
// *  <li>{@link MyCMAESOptimizer.PopulationSize}</li>
// * </ul>
// * @return {@inheritDoc}
// * @throws TooManyEvaluationsException if the maximal number of
// * evaluations is exceeded.
// * @throws DimensionMismatchException if the initial guess, target, and weight
// * arguments have inconsistent dimensions.
// */
// @Override
// public PointValuePair optimize(OptimizationData... optData)
// throws TooManyEvaluationsException,
// DimensionMismatchException {
// // Set up base class and perform computation.
// return super.optimize(optData);
// }
/**
 * {@inheritDoc}
 */
@Override
protected PointValuePair doOptimize() {
    // -------------------- Initialization --------------------------------
    isMinimize = getGoalType().equals(GoalType.MINIMIZE);
    final MyCMAESOptimizer.FitnessFunction fitfun = new MyCMAESOptimizer.FitnessFunction();
    final double[] guess = getStartPoint();
    // number of objective variables/problem dimension
    dimension = guess.length;
    initializeCMA(guess);
    iterations = 0;
    MyCMAESOptimizer.ValuePenaltyPair valuePenalty = fitfun.value(guess);
    double bestValue = valuePenalty.value + valuePenalty.penalty;
    push(fitnessHistory, bestValue);
    PointValuePair optimum = new PointValuePair(guess, isMinimize ? bestValue : -bestValue);
    PointValuePair lastResult = null;
    final double[] lB = MyCMAESOptimizer.this.getLowerBound();
    final double[] uB = MyCMAESOptimizer.this.getUpperBound();
    generationLoop: for (iterations = 1; iterations <= maxIterations; iterations++) {
        incrementIterationCount();
        // Generate and evaluate lambda offspring
        final RealMatrix arz = randn1(dimension, lambda);
        final RealMatrix arx = zeros(dimension, lambda);
        final double[] fitness = new double[lambda];
        final MyCMAESOptimizer.ValuePenaltyPair[] valuePenaltyPairs = new MyCMAESOptimizer.ValuePenaltyPair[lambda];
        // generate random offspring
        for (int k = 0; k < lambda; k++) {
            RealMatrix arzK = arz.getColumnMatrix(k);
            RealMatrix arxk = null;
            for (int i = 0; i < checkFeasableCount + 1; i++) {
                if (diagonalOnly <= 0) {
                    arxk = xmean.add(BD.multiply(arzK).scalarMultiply(// m + sig * Normal(0,C)
                    sigma));
                } else {
                    arxk = xmean.add(times(diagD, arzK).scalarMultiply(sigma));
                }
                if (i >= checkFeasableCount || fitfun.isFeasible(arxk.getColumn(0), lB, uB)) {
                    break;
                }
                // regenerate random arguments for row
                arz.setColumn(k, randn(dimension));
            }
            copyColumn(arxk, 0, arx, k);
            try {
                // compute fitness
                valuePenaltyPairs[k] = fitfun.value(arx.getColumn(k));
            } catch (TooManyEvaluationsException e) {
                break generationLoop;
            }
        }
        // Compute fitnesses by adding value and penalty after scaling by value range.
        double valueRange = valueRange(valuePenaltyPairs);
        for (int iValue = 0; iValue < valuePenaltyPairs.length; iValue++) {
            fitness[iValue] = valuePenaltyPairs[iValue].value + valuePenaltyPairs[iValue].penalty * valueRange;
        }
        // Sort by fitness and compute weighted mean into xmean
        final int[] arindex = sortedIndices(fitness);
        // Calculate new xmean, this is selection and recombination
        // for speed up of Eq. (2) and (3)
        final RealMatrix xold = xmean;
        int[] arMu = MathArrays.copyOf(arindex, mu);
        final RealMatrix bestArx = selectColumns(arx, arMu);
        xmean = bestArx.multiply(weights);
        final RealMatrix bestArz = selectColumns(arz, arMu);
        final RealMatrix zmean = bestArz.multiply(weights);
        final boolean hsig = updateEvolutionPaths(zmean, xold);
        if (diagonalOnly <= 0) {
            updateCovariance(hsig, bestArx, arz, arindex, xold);
        } else {
            updateCovarianceDiagonalOnly(hsig, bestArz);
        }
        // Adapt step size sigma - Eq. (5)
        sigma *= Math.exp(Math.min(1, (normps / chiN - 1) * cs / damps));
        final double bestFitness = fitness[arindex[0]];
        final double worstFitness = fitness[arindex[arindex.length - 1]];
        if (bestValue > bestFitness) {
            bestValue = bestFitness;
            lastResult = optimum;
            optimum = new PointValuePair(fitfun.repair(bestArx.getColumn(0)), isMinimize ? bestFitness : -bestFitness);
            if (getConvergenceChecker() != null && getConvergenceChecker().converged(iterations, optimum, lastResult)) {
                break generationLoop;
            }
        }
        // Break, if fitness is good enough
        if (stopFitness == stopFitness && bestFitness < (isMinimize ? stopFitness : -stopFitness)) {
            break generationLoop;
        }
        final double[] sqrtDiagC = sqrt(diagC).getColumn(0);
        final double[] pcCol = pc.getColumn(0);
        for (int i = 0; i < dimension; i++) {
            if (sigma * Math.max(Math.abs(pcCol[i]), sqrtDiagC[i]) > stopTolX) {
                break;
            }
            if (i >= dimension - 1) {
                break generationLoop;
            }
        }
        for (int i = 0; i < dimension; i++) {
            if (sigma * sqrtDiagC[i] > stopTolUpX) {
                break generationLoop;
            }
        }
        final double historyBest = min(fitnessHistory);
        final double historyWorst = max(fitnessHistory);
        if (iterations > 2 && Math.max(historyWorst, worstFitness) - Math.min(historyBest, bestFitness) < stopTolFun) {
            break generationLoop;
        }
        if (iterations > fitnessHistory.length && historyWorst - historyBest < stopTolHistFun) {
            break generationLoop;
        }
        // condition number of the covariance matrix exceeds 1e14
        if (max(diagD) / min(diagD) > tENmILLION) {
            break generationLoop;
        }
        // user defined termination
        if (getConvergenceChecker() != null) {
            final PointValuePair current = new PointValuePair(bestArx.getColumn(0), isMinimize ? bestFitness : -bestFitness);
            if (lastResult != null && getConvergenceChecker().converged(iterations, current, lastResult)) {
                break generationLoop;
            }
            lastResult = current;
        }
        // Adjust step size in case of equal function values (flat fitness)
        if (bestValue == fitness[arindex[(int) (0.1 + lambda / 4.0)]]) {
            sigma *= Math.exp(0.2 + cs / damps);
        }
        if (iterations > 2 && Math.max(historyWorst, bestFitness) - Math.min(historyBest, bestFitness) == 0) {
            sigma *= Math.exp(0.2 + cs / damps);
        }
        // store best in history
        push(fitnessHistory, bestFitness);
        if (generateStatistics) {
            statisticsSigmaHistory.add(sigma);
            statisticsFitnessHistory.add(bestFitness);
            statisticsMeanHistory.add(xmean.transpose());
            statisticsDHistory.add(diagD.transpose().scalarMultiply(hUNDreDtHOUSAND));
        }
    }
    return optimum;
}
Also used : TooManyEvaluationsException(org.apache.commons.math3.exception.TooManyEvaluationsException) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Example 53 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min in project narchy by automenta.

the class MyCMAESOptimizer method updateBD.

/**
 * Update B and D from C.
 *
 * @param negccov Negative covariance factor.
 */
private void updateBD(double negccov) {
    if (ccov1 + ccovmu + negccov > 0 && (iterations % 1.0 / (ccov1 + ccovmu + negccov) / dimension / dimensionDivisorWTF) < 1) {
        // to achieve O(N^2)
        C = triu(C, 0).add(triu(C, 1).transpose());
        // enforce symmetry to prevent complex numbers
        final EigenDecomposition eig = new EigenDecomposition(C);
        // eigen decomposition, B==normalized eigenvectors
        B = eig.getV();
        D = eig.getD();
        diagD = diag(D);
        if (min(diagD) <= 0) {
            for (int i = 0; i < dimension; i++) {
                if (diagD.getEntry(i, 0) < 0) {
                    diagD.setEntry(i, 0, 0);
                }
            }
            final double tfac = max(diagD) / big_magic_number_WTF;
            C = C.add(eye(dimension, dimension).scalarMultiply(tfac));
            diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac));
        }
        if (max(diagD) > big_magic_number_WTF * min(diagD)) {
            final double tfac = max(diagD) / big_magic_number_WTF - min(diagD);
            C = C.add(eye(dimension, dimension).scalarMultiply(tfac));
            diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac));
        }
        diagC = diag(C);
        // D contains standard deviations now
        diagD = sqrt(diagD);
        // O(n^2)
        BD = times(B, repmat(diagD.transpose(), dimension, 1));
    }
}
Also used : EigenDecomposition(org.apache.commons.math3.linear.EigenDecomposition)

Example 54 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min 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);
}
Also used : MultiDirectionalSimplex(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.MultiDirectionalSimplex) Logger(org.slf4j.Logger) SortedSet(java.util.SortedSet) DoubleObjectPair(org.eclipse.collections.api.tuple.primitive.DoubleObjectPair) MathArrays(org.apache.commons.math3.util.MathArrays) FasterList(jcog.list.FasterList) LoggerFactory(org.slf4j.LoggerFactory) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) Supplier(java.util.function.Supplier) SimplexOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer) List(java.util.List) Stream(java.util.stream.Stream) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) GoalType(org.apache.commons.math3.optim.nonlinear.scalar.GoalType) Map(java.util.Map) PrimitiveTuples.pair(org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples.pair) CSVOutput(jcog.meter.event.CSVOutput) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) Pair(org.eclipse.collections.api.tuple.Pair) FloatFunction(org.eclipse.collections.api.block.function.primitive.FloatFunction) Joiner(com.google.common.base.Joiner) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) FasterList(jcog.list.FasterList) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) DoubleObjectPair(org.eclipse.collections.api.tuple.primitive.DoubleObjectPair) CSVOutput(jcog.meter.event.CSVOutput)

Example 55 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min in project narchy by automenta.

the class WeightedInterpolationFunction method learn.

@Override
public void learn(double[] xs, double y) {
    adjustBorders(xs);
    ArrayRealVector xs3 = new ArrayRealVector(xs);
    double min = Double.POSITIVE_INFINITY;
    Point nearest = null;
    for (Point p : points) {
        double d = p.xs.getL1Distance(xs3);
        if (d < min) {
            min = d;
            nearest = p;
        }
    }
    nearest.xs.setSubVector(0, xs3);
    nearest.y = y;
    // move the points away from each other to get a homogenous distribution
    for (Point a : points) {
        for (Point b : points) {
            if (a != b) {
                ArrayRealVector d = b.xs.subtract(a.xs);
                double l = d.getL1Norm();
                if (l == 0) {
                    l = 1;
                }
                d.mapMultiplyToSelf(gravity / l / l / l);
                b.velocity.setSubVector(0, b.velocity.add(d));
            }
        }
    }
    for (Point p : points) {
        p.velocity.mapMultiplyToSelf(decay);
        p.xs.setSubVector(0, p.xs.add(p.velocity));
        for (int i = 0; i < numberOfInputs(); ++i) {
            if (p.xs.getEntry(i) > maxInput.getEntry(i)) {
                p.xs.setEntry(i, maxInput.getEntry(i));
                p.velocity.setEntry(i, 0);
            }
            if (p.xs.getEntry(i) < minInput.getEntry(i)) {
                p.xs.setEntry(i, minInput.getEntry(i));
                p.velocity.setEntry(i, 0);
            }
        }
        p.y = value(p.xs.getDataRef());
    }
}
Also used : ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Aggregations

ArrayList (java.util.ArrayList)16 List (java.util.List)10 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)8 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)7 Map (java.util.Map)6 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)6 MaxEval (org.apache.commons.math3.optim.MaxEval)6 Collectors (java.util.stream.Collectors)5 ExpressionException (cbit.vcell.parser.ExpressionException)4 Plot2 (ij.gui.Plot2)4 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)4 InitialGuess (org.apache.commons.math3.optim.InitialGuess)4 PointValuePair (org.apache.commons.math3.optim.PointValuePair)4 RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)3 HashMap (java.util.HashMap)3 SimpsonIntegrator (org.apache.commons.math3.analysis.integration.SimpsonIntegrator)3 BrentOptimizer (org.apache.commons.math3.optim.univariate.BrentOptimizer)3 SearchInterval (org.apache.commons.math3.optim.univariate.SearchInterval)3