Search in sources :

Example 16 with MultivariateOptimizer

use of org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer in project tetrad by cmu-phil.

the class Lofs2 method optimizeRow.

private void optimizeRow(final int rowIndex, final TetradMatrix data, final double range, final List<List<Integer>> rows, final List<List<Double>> parameters) {
    System.out.println("A");
    final int numParams = rows.get(rowIndex).size();
    final double[] dLeftMin = new double[numParams];
    final double[] dRightMin = new double[numParams];
    double[] values = new double[numParams];
    double delta = 0.1;
    if (false) {
        // isEdgeCorrected()) {
        double min = -2;
        double max = 2;
        int[] dims = new int[values.length];
        int numBins = 5;
        for (int i = 0; i < values.length; i++) dims[i] = numBins;
        CombinationGenerator gen = new CombinationGenerator(dims);
        int[] comb;
        List<Double> maxParams = new ArrayList<>();
        for (int i = 0; i < values.length; i++) maxParams.add(0.0);
        double maxV = Double.NEGATIVE_INFINITY;
        while ((comb = gen.next()) != null) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            List<Double> params = new ArrayList<>();
            for (int i = 0; i < values.length; i++) {
                params.add(min + (max - min) * (comb[i] / (double) numBins));
            }
            parameters.set(rowIndex, params);
            double v = scoreRow(rowIndex, data, rows, parameters);
            if (v > maxV) {
                maxV = v;
                maxParams = params;
            }
        }
        System.out.println("maxparams = " + maxParams);
        parameters.set(rowIndex, maxParams);
        for (int i = 0; i < values.length; i++) {
            dLeftMin[i] = -range;
            dRightMin[i] = range;
            values[i] = maxParams.get(i);
        }
    } else if (false) {
        for (int i = 0; i < numParams; i++) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            parameters.get(rowIndex).set(i, -range);
            double vLeft = scoreRow(rowIndex, data, rows, parameters);
            double dLeft = -range;
            // Search from the left for the first valley; mark that as dleft.
            for (double d = -range + delta; d < range; d += delta) {
                if (Thread.currentThread().isInterrupted()) {
                    break;
                }
                parameters.get(rowIndex).set(i, d);
                double v = scoreRow(rowIndex, data, rows, parameters);
                if (Double.isNaN(v))
                    continue;
                if (v > vLeft)
                    break;
                vLeft = v;
                dLeft = d;
            }
            parameters.get(rowIndex).set(i, range);
            double vRight = scoreRow(rowIndex, data, rows, parameters);
            double dRight = range;
            // to avoid high scores at the boundaries.
            for (double d = range - delta; d > -range; d -= delta) {
                if (Thread.currentThread().isInterrupted()) {
                    break;
                }
                parameters.get(rowIndex).set(i, d);
                double v = scoreRow(rowIndex, data, rows, parameters);
                if (Double.isNaN(v))
                    continue;
                if (v > vRight)
                    break;
                vRight = v;
                dRight = d;
            }
            // If dleft dright ended up reversed, re-reverse them.
            if (dLeft > dRight) {
                double temp = dRight;
                dLeft = dRight;
                dRight = temp;
            }
            System.out.println("dLeft = " + dLeft + " dRight = " + dRight);
            dLeftMin[i] = dLeft;
            dRightMin[i] = dRight;
            values[i] = (dLeft + dRight) / 2.0;
        }
    } else {
        // Default case: search for the maximum score over the entire range.
        for (int i = 0; i < numParams; i++) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            dLeftMin[i] = -range;
            dRightMin[i] = range;
            values[i] = 0;
        }
    }
    MultivariateFunction function = new MultivariateFunction() {

        public double value(double[] values) {
            System.out.println(Arrays.toString(values));
            for (int i = 0; i < values.length; i++) {
                if (Thread.currentThread().isInterrupted()) {
                    break;
                }
                parameters.get(rowIndex).set(i, values[i]);
            }
            double v = scoreRow(rowIndex, data, rows, parameters);
            if (Double.isNaN(v)) {
                // was 10000
                return Double.POSITIVE_INFINITY;
            }
            return -v;
        }
    };
    try {
        MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
        PointValuePair pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
        values = pair.getPoint();
    } catch (Exception e) {
        e.printStackTrace();
        for (int i = 0; i < values.length; i++) {
            parameters.get(rowIndex).set(i, Double.NaN);
        }
    }
}
Also used : MultivariateOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PowellOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer) PointValuePair(org.apache.commons.math3.optim.PointValuePair) MultivariateFunction(org.apache.commons.math3.analysis.MultivariateFunction)

Example 17 with MultivariateOptimizer

use of org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer in project tetrad by cmu-phil.

the class GeneralizedSemEstimator method optimize.

private double[] optimize(MultivariateFunction function, double[] values, int optimizer) {
    PointValuePair pair;
    if (optimizer == 1) {
        // 0.01, 0.000001
        // 2.0D * FastMath.ulp(1.0D), 1e-8
        MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
        pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
    } else if (optimizer == 2) {
        MultivariateOptimizer search = new SimplexOptimizer(1e-7, 1e-7);
        pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000), new NelderMeadSimplex(values.length));
    } else if (optimizer == 3) {
        int dim = values.length;
        int additionalInterpolationPoints = 0;
        final int numIterpolationPoints = 2 * dim + 1 + additionalInterpolationPoints;
        BOBYQAOptimizer search = new BOBYQAOptimizer(numIterpolationPoints);
        pair = search.optimize(new MaxEval(100000), new ObjectiveFunction(function), GoalType.MINIMIZE, new InitialGuess(values), SimpleBounds.unbounded(dim));
    } else if (optimizer == 4) {
        MultivariateOptimizer search = new CMAESOptimizer(3000000, .05, false, 0, 0, new MersenneTwister(), false, new SimplePointChecker<PointValuePair>(0.5, 0.5));
        pair = search.optimize(new MaxEval(30000), new ObjectiveFunction(function), GoalType.MINIMIZE, new InitialGuess(values), new CMAESOptimizer.Sigma(new double[values.length]), new CMAESOptimizer.PopulationSize(1000));
    } else if (optimizer == 5) {
        // 0.01, 0.000001
        // 2.0D * FastMath.ulp(1.0D), 1e-8
        MultivariateOptimizer search = new PowellOptimizer(.05, .05);
        pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
    } else if (optimizer == 6) {
        MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
        pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MAXIMIZE, new MaxEval(10000));
    } else {
        throw new IllegalStateException();
    }
    return pair.getPoint();
}
Also used : MultivariateOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) MersenneTwister(org.apache.commons.math3.random.MersenneTwister)

Aggregations

PointValuePair (org.apache.commons.math3.optim.PointValuePair)16 MultivariateOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer)16 ObjectiveFunction (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction)16 InitialGuess (org.apache.commons.math3.optim.InitialGuess)15 MaxEval (org.apache.commons.math3.optim.MaxEval)15 PowellOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer)13 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)4 MultivariateFunction (org.apache.commons.math3.analysis.MultivariateFunction)3 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)2 OptimizationData (org.apache.commons.math3.optim.OptimizationData)2 SimpleBounds (org.apache.commons.math3.optim.SimpleBounds)2 SimpleValueChecker (org.apache.commons.math3.optim.SimpleValueChecker)2 ObjectiveFunctionGradient (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunctionGradient)2 CMAESOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer)2 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)2 Context (edu.cmu.tetrad.calculator.expression.Context)1 Expression (edu.cmu.tetrad.calculator.expression.Expression)1 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)1 RealMatrix (org.apache.commons.math3.linear.RealMatrix)1 BFGSOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.gradient.BFGSOptimizer)1