Java Code Examples for org.apache.commons.math3.linear.RealMatrix#scalarMultiply()

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#scalarMultiply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Arja_0028_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 2
Source File: Arja_0079_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 3
Source File: Arja_0079_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 4
Source File: Arja_00166_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 5
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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) {
        // XXX Line below to replace the current one in 4.0 (MATH-879).
        // throw new NotStrictlyPositiveException(lambda);
        lambda = 4 + (int) (3 * Math.log(dimension));
    }
    // initialize sigma
    final double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        // XXX Line below to replace the current one in 4.0 (MATH-868).
        // sigmaArray[i][0] = inputSigma[i];
        sigmaArray[i][0] = inputSigma == null ? 0.3 : inputSigma[i];
    }
    final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4 + mueff / dimension) /
            (dimension + 4 + 2 * mueff / dimension);
    cs = (mueff + 2) / (dimension + mueff + 3.);
    damps = (1 + 2 * Math.max(0, Math.sqrt((mueff - 1) /
                                           (dimension + 1)) - 1)) *
        Math.max(0.3,
                 1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment
    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 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables
    diagD = insigma.scalarMultiply(1 / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 6
Source File: Cardumen_00163_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 7
Source File: KalmanFilterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testConstantAcceleration() {
    // simulates a vehicle, accelerating at a constant rate (0.1 m/s)

    // discrete time interval
    double dt = 0.1d;
    // position measurement noise (meter)
    double measurementNoise = 10d;
    // acceleration noise (meter/sec^2)
    double accelNoise = 0.2d;

    // A = [ 1 dt ]
    //     [ 0  1 ]
    RealMatrix A = new Array2DRowRealMatrix(new double[][] { { 1, dt }, { 0, 1 } });

    // B = [ dt^2/2 ]
    //     [ dt     ]
    RealMatrix B = new Array2DRowRealMatrix(
            new double[][] { { FastMath.pow(dt, 2d) / 2d }, { dt } });

    // H = [ 1 0 ]
    RealMatrix H = new Array2DRowRealMatrix(new double[][] { { 1d, 0d } });

    // x = [ 0 0 ]
    RealVector x = new ArrayRealVector(new double[] { 0, 0 });

    RealMatrix tmp = new Array2DRowRealMatrix(
            new double[][] { { FastMath.pow(dt, 4d) / 4d, FastMath.pow(dt, 3d) / 2d },
                             { FastMath.pow(dt, 3d) / 2d, FastMath.pow(dt, 2d) } });

    // Q = [ dt^4/4 dt^3/2 ]
    //     [ dt^3/2 dt^2   ]
    RealMatrix Q = tmp.scalarMultiply(FastMath.pow(accelNoise, 2));

    // P0 = [ 1 1 ]
    //      [ 1 1 ]
    RealMatrix P0 = new Array2DRowRealMatrix(new double[][] { { 1, 1 }, { 1, 1 } });

    // R = [ measurementNoise^2 ]
    RealMatrix R = new Array2DRowRealMatrix(
            new double[] { FastMath.pow(measurementNoise, 2) });

    // constant control input, increase velocity by 0.1 m/s per cycle
    RealVector u = new ArrayRealVector(new double[] { 0.1d });

    ProcessModel pm = new DefaultProcessModel(A, B, Q, x, P0);
    MeasurementModel mm = new DefaultMeasurementModel(H, R);
    KalmanFilter filter = new KalmanFilter(pm, mm);

    Assert.assertEquals(1, filter.getMeasurementDimension());
    Assert.assertEquals(2, filter.getStateDimension());

    assertMatrixEquals(P0.getData(), filter.getErrorCovariance());

    // check the initial state
    double[] expectedInitialState = new double[] { 0.0, 0.0 };
    assertVectorEquals(expectedInitialState, filter.getStateEstimation());

    RandomGenerator rand = new JDKRandomGenerator();

    RealVector tmpPNoise = new ArrayRealVector(
            new double[] { FastMath.pow(dt, 2d) / 2d, dt });

    // iterate 60 steps
    for (int i = 0; i < 60; i++) {
        filter.predict(u);

        // Simulate the process
        RealVector pNoise = tmpPNoise.mapMultiply(accelNoise * rand.nextGaussian());

        // x = A * x + B * u + pNoise
        x = A.operate(x).add(B.operate(u)).add(pNoise);

        // Simulate the measurement
        double mNoise = measurementNoise * rand.nextGaussian();

        // z = H * x + m_noise
        RealVector z = H.operate(x).mapAdd(mNoise);

        filter.correct(z);

        // state estimate shouldn't be larger than the measurement noise
        double diff = FastMath.abs(x.getEntry(0) - filter.getStateEstimation()[0]);
        Assert.assertTrue(Precision.compareTo(diff, measurementNoise, 1e-6) < 0);
    }

    // error covariance of the velocity should be already very low (< 0.1)
    Assert.assertTrue(Precision.compareTo(filter.getErrorCovariance()[1][1],
                                          0.1d, 1e-6) < 0);
}
 
Example 8
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 9
Source File: Arja_00181_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 10
Source File: Elixir_0021_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 11
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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) {
        // XXX Line below to replace the current one in 4.0 (MATH-879).
        // throw new NotStrictlyPositiveException(lambda);
        lambda = 4 + (int) (3 * FastMath.log(dimension));
    }
    // initialize sigma
    final double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        // XXX Line below to replace the current one in 4.0 (MATH-868).
        // sigmaArray[i][0] = inputSigma[i];
        sigmaArray[i][0] = inputSigma == null ? 0.3 : inputSigma[i];
    }
    final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    logMu2 = FastMath.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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4 + mueff / dimension) /
            (dimension + 4 + 2 * mueff / dimension);
    cs = (mueff + 2) / (dimension + mueff + 3.);
    damps = (1 + 2 * FastMath.max(0, FastMath.sqrt((mueff - 1) /
                                                   (dimension + 1)) - 1)) *
        FastMath.max(0.3,
                     1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment
    ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff);
    ccovmu = FastMath.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) /
                          ((dimension + 2) * (dimension + 2) + mueff));
    ccov1Sep = FastMath.min(1, ccov1 * (dimension + 1.5) / 3);
    ccovmuSep = FastMath.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3);
    chiN = FastMath.sqrt(dimension) *
        (1 - 1 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables
    diagD = insigma.scalarMultiply(1 / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 12
Source File: Cardumen_00213_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 13
Source File: Arja_00104_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 14
Source File: Math_6_CMAESOptimizer_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4 + mueff / dimension) /
            (dimension + 4 + 2 * mueff / dimension);
    cs = (mueff + 2) / (dimension + mueff + 3.);
    damps = (1 + 2 * Math.max(0, Math.sqrt((mueff - 1) /
                                           (dimension + 1)) - 1)) *
        Math.max(0.3,
                 1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment
    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 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables
    diagD = insigma.scalarMultiply(1 / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 15
Source File: KalmanFilterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testConstantAcceleration() {
    // simulates a vehicle, accelerating at a constant rate (0.1 m/s)

    // discrete time interval
    double dt = 0.1d;
    // position measurement noise (meter)
    double measurementNoise = 10d;
    // acceleration noise (meter/sec^2)
    double accelNoise = 0.2d;

    // A = [ 1 dt ]
    //     [ 0  1 ]
    RealMatrix A = new Array2DRowRealMatrix(new double[][] { { 1, dt }, { 0, 1 } });

    // B = [ dt^2/2 ]
    //     [ dt     ]
    RealMatrix B = new Array2DRowRealMatrix(
            new double[][] { { Math.pow(dt, 2d) / 2d }, { dt } });

    // H = [ 1 0 ]
    RealMatrix H = new Array2DRowRealMatrix(new double[][] { { 1d, 0d } });

    // x = [ 0 0 ]
    RealVector x = new ArrayRealVector(new double[] { 0, 0 });

    RealMatrix tmp = new Array2DRowRealMatrix(
            new double[][] { { Math.pow(dt, 4d) / 4d, Math.pow(dt, 3d) / 2d },
                             { Math.pow(dt, 3d) / 2d, Math.pow(dt, 2d) } });

    // Q = [ dt^4/4 dt^3/2 ]
    //     [ dt^3/2 dt^2   ]
    RealMatrix Q = tmp.scalarMultiply(Math.pow(accelNoise, 2));

    // P0 = [ 1 1 ]
    //      [ 1 1 ]
    RealMatrix P0 = new Array2DRowRealMatrix(new double[][] { { 1, 1 }, { 1, 1 } });

    // R = [ measurementNoise^2 ]
    RealMatrix R = new Array2DRowRealMatrix(
            new double[] { Math.pow(measurementNoise, 2) });

    // constant control input, increase velocity by 0.1 m/s per cycle
    RealVector u = new ArrayRealVector(new double[] { 0.1d });

    ProcessModel pm = new DefaultProcessModel(A, B, Q, x, P0);
    MeasurementModel mm = new DefaultMeasurementModel(H, R);
    KalmanFilter filter = new KalmanFilter(pm, mm);

    Assert.assertEquals(1, filter.getMeasurementDimension());
    Assert.assertEquals(2, filter.getStateDimension());

    assertMatrixEquals(P0.getData(), filter.getErrorCovariance());

    // check the initial state
    double[] expectedInitialState = new double[] { 0.0, 0.0 };
    assertVectorEquals(expectedInitialState, filter.getStateEstimation());

    RandomGenerator rand = new JDKRandomGenerator();

    RealVector tmpPNoise = new ArrayRealVector(
            new double[] { Math.pow(dt, 2d) / 2d, dt });

    RealVector mNoise = new ArrayRealVector(1);

    // iterate 60 steps
    for (int i = 0; i < 60; i++) {
        filter.predict(u);

        // Simulate the process
        RealVector pNoise = tmpPNoise.mapMultiply(accelNoise * rand.nextGaussian());

        // x = A * x + B * u + pNoise
        x = A.operate(x).add(B.operate(u)).add(pNoise);

        // Simulate the measurement
        mNoise.setEntry(0, measurementNoise * rand.nextGaussian());

        // z = H * x + m_noise
        RealVector z = H.operate(x).add(mNoise);

        filter.correct(z);

        // state estimate shouldn't be larger than the measurement noise
        double diff = Math.abs(x.getEntry(0) - filter.getStateEstimation()[0]);
        Assert.assertTrue(Precision.compareTo(diff, measurementNoise, 1e-6) < 0);
    }

    // error covariance of the velocity should be already very low (< 0.1)
    Assert.assertTrue(Precision.compareTo(filter.getErrorCovariance()[1][1],
                                          0.1d, 1e-6) < 0);
}
 
Example 16
Source File: Arja_0055_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 17
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    logMu2 = FastMath.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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4 + mueff / dimension) /
            (dimension + 4 + 2 * mueff / dimension);
    cs = (mueff + 2) / (dimension + mueff + 3.);
    damps = (1 + 2 * FastMath.max(0, FastMath.sqrt((mueff - 1) /
                                                   (dimension + 1)) - 1)) *
        FastMath.max(0.3,
                     1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment
    ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff);
    ccovmu = FastMath.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) /
                          ((dimension + 2) * (dimension + 2) + mueff));
    ccov1Sep = FastMath.min(1, ccov1 * (dimension + 1.5) / 3);
    ccovmuSep = FastMath.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3);
    chiN = FastMath.sqrt(dimension) *
            (1 - 1 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables
    diagD = insigma.scalarMultiply(1 / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 18
Source File: GaussianDPMM.java    From DPMM-Clustering with GNU General Public License v3.0 4 votes vote down vote up
private RealMatrix calculateMeanError(RealMatrix Psi, int kappa, int nu) {
    //Reference: page 18, equation 228 at http://www.cs.ubc.ca/~murphyk/Papers/bayesGauss.pdf
    return Psi.scalarMultiply(1.0/(kappa*(nu-dimensionality+1.0)));
}
 
Example 19
Source File: 1_CMAESOptimizer.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}
 
Example 20
Source File: JGenProg2017_00111_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * 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) {
        lambda = 4 + (int) (3. * Math.log(dimension));
    }
    // initialize sigma
    double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        final double range =  (boundaries == null) ? 1.0 : boundaries[1][i] - boundaries[0][i];
        sigmaArray[i][0]   = ((inputSigma == null) ? 0.3 : inputSigma[i]) / range;
    }
    RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    sigma = max(insigma); // overall standard deviation

    // initialize termination criteria
    stopTolUpX = 1e3 * max(insigma);
    stopTolX = 1e-11 * max(insigma);
    stopTolFun = 1e-12;
    stopTolHistFun = 1e-13;

    // initialize selection strategy parameters
    mu = lambda / 2; // number of parents/points for recombination
    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);
    mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

    // initialize dynamic strategy parameters and constants
    cc = (4. + mueff / dimension) /
            (dimension + 4. + 2. * mueff / dimension);
    cs = (mueff + 2.) / (dimension + mueff + 3.);
    damps = (1. + 2. * Math.max(0, Math.sqrt((mueff - 1.) /
            (dimension + 1.)) - 1.)) *
            Math.max(0.3, 1. - dimension /
                    (1e-6 + Math.min(maxIterations, getMaxEvaluations() /
                            lambda))) + cs; // minor increment
    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. * dimension) + 1 / (21. * dimension * dimension));
    // intialize CMA internal values - updated each generation
    xmean = MatrixUtils.createColumnRealMatrix(guess); // objective
                                                       // variables
    diagD = insigma.scalarMultiply(1. / sigma);
    diagC = square(diagD);
    pc = zeros(dimension, 1); // evolution paths for C and sigma
    ps = zeros(dimension, 1); // B defines the coordinate system
    normps = ps.getFrobeniusNorm();

    B = eye(dimension, dimension);
    D = ones(dimension, 1); // diagonal D defines the scaling
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
    historySize = 10 + (int) (3. * 10. * dimension / lambda);
    fitnessHistory = new double[historySize]; // history of fitness values
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.MAX_VALUE;
    }
}