Search in sources :

Example 1 with SimplePointChecker

use of org.apache.commons.math3.optim.SimplePointChecker in project TweetyProject by TweetyProjectTeam.

the class ApacheCommonsCMAESOptimizer method solve.

/* (non-Javadoc)
	 * @see org.tweetyproject.math.opt.Solver#solve(org.tweetyproject.math.opt.ConstraintSatisfactionProblem)
	 */
@Override
public Map<Variable, Term> solve(GeneralConstraintSatisfactionProblem problem) throws GeneralMathException {
    // only optimization problems
    if (!(problem instanceof OptimizationProblem))
        throw new IllegalArgumentException("Only optimization problems allowed for this solver.");
    OptimizationProblem p = (OptimizationProblem) problem;
    // only box constraints allowed (so far)
    if (!p.isEmpty())
        throw new IllegalArgumentException("Only optimization problems with box constraints on variables allowed for this solver (no other constraints.");
    final List<Variable> vars = new ArrayList<Variable>(p.getTargetFunction().getVariables());
    double[] lb = new double[vars.size()];
    double[] ub = new double[vars.size()];
    double[] s = new double[vars.size()];
    double[] sigma = new double[vars.size()];
    for (int i = 0; i < vars.size(); i++) {
        lb[i] = vars.get(i).getLowerBound();
        ub[i] = vars.get(i).getUpperBound();
        s[i] = (lb[i] + ub[i]) / 2;
        sigma[i] = ub[i] - lb[i];
    }
    final Term targetFunction = p.getTargetFunction();
    MultivariateFunction target = new MultivariateFunction() {

        @Override
        public double value(double[] arg0) {
            return targetFunction.replaceAllTerms(arg0, vars).doubleValue();
        }
    };
    // construct solver
    CMAESOptimizer optimizer = new CMAESOptimizer(this.maxIterations, this.stopFitness, this.isActiveCMA, this.diagonalOnly, this.checkFeasableCount, new JDKRandomGenerator(), true, new SimplePointChecker<PointValuePair>(this.precision, this.precision));
    PointValuePair val = optimizer.optimize(new CMAESOptimizer.Sigma(sigma), new ObjectiveFunction(target), new InitialGuess(s), p.getType() == OptimizationProblem.MAXIMIZE ? GoalType.MAXIMIZE : GoalType.MINIMIZE, new MaxEval(this.maxIterations), new SimpleBounds(lb, ub), new CMAESOptimizer.PopulationSize(this.populationSize));
    Map<Variable, Term> result = new HashMap<Variable, Term>();
    for (int i = 0; i < vars.size(); i++) result.put(vars.get(i), new FloatConstant(val.getPoint()[i]));
    return result;
}
Also used : InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) Variable(org.tweetyproject.math.term.Variable) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) HashMap(java.util.HashMap) CMAESOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer) FloatConstant(org.tweetyproject.math.term.FloatConstant) ArrayList(java.util.ArrayList) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) Term(org.tweetyproject.math.term.Term) PointValuePair(org.apache.commons.math3.optim.PointValuePair) MultivariateFunction(org.apache.commons.math3.analysis.MultivariateFunction) OptimizationProblem(org.tweetyproject.math.opt.problem.OptimizationProblem) JDKRandomGenerator(org.apache.commons.math3.random.JDKRandomGenerator)

Example 2 with SimplePointChecker

use of org.apache.commons.math3.optim.SimplePointChecker 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)

Example 3 with SimplePointChecker

use of org.apache.commons.math3.optim.SimplePointChecker in project TweetyProject by TweetyProjectTeam.

the class ApacheCommonsNonLinearConjugateGradientOptimizer method solve.

/* (non-Javadoc)
	 * @see org.tweetyproject.math.opt.Solver#solve(org.tweetyproject.math.opt.ConstraintSatisfactionProblem)
	 */
@Override
public Map<Variable, Term> solve(GeneralConstraintSatisfactionProblem problem) throws GeneralMathException {
    // only optimization problems
    if (!(problem instanceof OptimizationProblem))
        throw new IllegalArgumentException("Only optimization problems allowed for this solver.");
    OptimizationProblem p = (OptimizationProblem) problem;
    // no constraints allowed
    if (!p.isEmpty())
        throw new IllegalArgumentException("Only optimization problems without constraints allowed for this solver.");
    final Term target = p.getTargetFunction();
    final List<Variable> vars = new ArrayList<Variable>(target.getVariables());
    MultivariateFunction acTarget = new MultivariateFunction() {

        @Override
        public double value(double[] arg0) {
            return target.replaceAllTerms(arg0, vars).doubleValue();
        }
    };
    final Term[] targetGradient = new Term[vars.size()];
    for (int i = 0; i < vars.size(); i++) targetGradient[i] = target.derive(vars.get(i));
    MultivariateVectorFunction acTargetGradient = new MultivariateVectorFunction() {

        @Override
        public double[] value(double[] arg0) throws IllegalArgumentException {
            double[] result = new double[arg0.length];
            for (int i = 0; i < arg0.length; i++) result[i] = targetGradient[i].replaceAllTerms(arg0, vars).doubleValue();
            return result;
        }
    };
    // create solver
    NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.FLETCHER_REEVES, new SimplePointChecker<PointValuePair>(this.precision, this.precision));
    double[] s = new double[vars.size()];
    for (int i = 0; i < vars.size(); i++) s[i] = 0.5;
    PointValuePair val = optimizer.optimize(new ObjectiveFunction(acTarget), new ObjectiveFunctionGradient(acTargetGradient), new InitialGuess(s), p.getType() == OptimizationProblem.MAXIMIZE ? GoalType.MAXIMIZE : GoalType.MINIMIZE, new MaxEval(this.maxEval));
    Map<Variable, Term> result = new HashMap<Variable, Term>();
    for (int i = 0; i < vars.size(); i++) result.put(vars.get(i), new FloatConstant(val.getPoint()[i]));
    return result;
}
Also used : InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) Variable(org.tweetyproject.math.term.Variable) HashMap(java.util.HashMap) FloatConstant(org.tweetyproject.math.term.FloatConstant) ArrayList(java.util.ArrayList) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) Term(org.tweetyproject.math.term.Term) MultivariateVectorFunction(org.apache.commons.math3.analysis.MultivariateVectorFunction) PointValuePair(org.apache.commons.math3.optim.PointValuePair) ObjectiveFunctionGradient(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunctionGradient) MultivariateFunction(org.apache.commons.math3.analysis.MultivariateFunction) OptimizationProblem(org.tweetyproject.math.opt.problem.OptimizationProblem) NonLinearConjugateGradientOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.gradient.NonLinearConjugateGradientOptimizer)

Example 4 with SimplePointChecker

use of org.apache.commons.math3.optim.SimplePointChecker in project s1tbx by senbox-org.

the class ArcDataIntegration method integrateArcsL2.

/**
 * Integrates arc data to node data using weighted least squares (L2 norm).
 *     This function integrates arc `data` by finding `point_data` (the returned
 *     value), such that `point_data` minimizes:
 *         ||diag(weights) * (A * point_data - data)||_2
 *     where `A` is an incidence matrix of dimensions 'number of arcs' by
 *     'number of nodes'.
 *     Description of the algorithm:
 *         1. Apply weights
 *             A = diag(weights) * A
 *             b = diag(weights) * data
 *         2. Minimize
 *             ||A*x - b||_2
 *
 * @param arcs every row should contain two indices corresponding to the end nodes of a specific arc.
 * @param data data to be integrated.
 * @param weights quality of arc data.
 * @return
 */
public static double[] integrateArcsL2(int[][] arcs, double[] data, double[] weights) {
    // graph size
    int noOfNodes = Arrays.stream(arcs).flatMapToInt(Arrays::stream).max().getAsInt() + 1;
    int noOfArcs = arcs.length;
    SystemUtils.LOG.fine("Number of nodes: " + noOfNodes);
    SystemUtils.LOG.fine("Number of arcs: " + noOfArcs);
    // A: coefficients matrix
    double[][] incidenceMatrix = new double[noOfArcs][noOfNodes];
    // get column and set to zero reference node occurrences
    int[] sourceNodes = getColumnVector(arcs, 0);
    // get column and set to zero reference node occurrences
    int[] targetNodes = getColumnVector(arcs, 1);
    // source nodes
    setValues(incidenceMatrix, repeat(-1.0, noOfArcs), null, sourceNodes);
    // target nodes
    setValues(incidenceMatrix, repeat(1.0, noOfArcs), null, targetNodes);
    // remove reference node column
    if (!referenceNodeIsLast) {
        // remove first column
        shiftColumnsLeft(incidenceMatrix, 1);
    }
    dropLastColumn(incidenceMatrix);
    // problem
    ModelFunction problem = new ModelFunction(new MultivariateVectorFunction() {

        RealMatrix jacobian = new Array2DRowRealMatrix(incidenceMatrix);

        public double[] value(double[] params) {
            double[] values = jacobian.operate(params);
            return values;
        }
    });
    ModelFunctionJacobian problemJacobian = new ModelFunctionJacobian(new MultivariateMatrixFunction() {

        double[][] jacobian = incidenceMatrix;

        public double[][] value(double[] params) {
            return jacobian;
        }
    });
    // optimization
    ConvergenceChecker<PointVectorValuePair> checker = new SimplePointChecker(0.00001, -1.0);
    LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(checker);
    int maxIterations = 10000;
    PointVectorValuePair solution = optimizer.optimize(new MaxEval(maxIterations), problem, problemJacobian, new Target(data), new Weight(weights), new InitialGuess(new double[noOfNodes - 1]));
    // return solution
    double[] nodeData;
    if (referenceNodeIsLast) {
        nodeData = Arrays.copyOf(solution.getPoint(), noOfNodes);
        // add reference node back (last)
        nodeData[noOfNodes - 1] = 0;
    } else {
        nodeData = new double[noOfNodes];
        // reference node (first) is 0
        System.arraycopy(solution.getPoint(), 0, nodeData, 1, noOfNodes - 1);
    }
    return nodeData;
}
Also used : MaxEval(org.apache.commons.math3.optim.MaxEval) InitialGuess(org.apache.commons.math3.optim.InitialGuess) ModelFunction(org.apache.commons.math3.optim.nonlinear.vector.ModelFunction) MultivariateVectorFunction(org.apache.commons.math3.analysis.MultivariateVectorFunction) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) Weight(org.apache.commons.math3.optim.nonlinear.vector.Weight) PointVectorValuePair(org.apache.commons.math3.optim.PointVectorValuePair) Target(org.apache.commons.math3.optim.nonlinear.vector.Target) LevenbergMarquardtOptimizer(org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) ModelFunctionJacobian(org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian) MultivariateMatrixFunction(org.apache.commons.math3.analysis.MultivariateMatrixFunction) SimplePointChecker(org.apache.commons.math3.optim.SimplePointChecker)

Example 5 with SimplePointChecker

use of org.apache.commons.math3.optim.SimplePointChecker in project gdsc by aherbert.

the class CellOutliner_PlugIn method fitPolygonalCell.

/**
 * Find an ellipse that optimises the fit to the polygon detected edges.
 *
 * @param roi the roi
 * @param params the params
 * @param weightMap the weight map
 * @return the ellipse parameters
 */
@Nullable
private double[] fitPolygonalCell(PolygonRoi roi, double[] params, FloatProcessor weightMap) {
    // Get an estimate of the starting parameters using the current polygon
    final double[] startPoint = estimateStartPoint(roi, weightMap.getWidth(), weightMap.getHeight());
    final int maxEval = 2000;
    final DifferentiableEllipticalFitFunction func = new DifferentiableEllipticalFitFunction(roi, weightMap);
    final double relativeThreshold = 100 * Precision.EPSILON;
    final double absoluteThreshold = 100 * Precision.SAFE_MIN;
    final ConvergenceChecker<PointVectorValuePair> checker = new SimplePointChecker<>(relativeThreshold, absoluteThreshold);
    final double initialStepBoundFactor = 10;
    final double costRelativeTolerance = 1e-10;
    final double parRelativeTolerance = 1e-10;
    final double orthoTolerance = 1e-10;
    final double threshold = Precision.SAFE_MIN;
    final LevenbergMarquardtOptimizer optimiser = new LevenbergMarquardtOptimizer(initialStepBoundFactor, costRelativeTolerance, parRelativeTolerance, orthoTolerance, threshold);
    try {
        // @formatter:off
        final LeastSquaresProblem problem = new LeastSquaresBuilder().maxEvaluations(Integer.MAX_VALUE).maxIterations(maxEval).start(startPoint).target(func.calculateTarget()).weight(new DiagonalMatrix(func.calculateWeights())).model(func, func::jacobian).checkerPair(checker).build();
        // @formatter:on
        final Optimum solution = optimiser.optimize(problem);
        if (debug) {
            IJ.log(String.format("Eval = %d (Iter = %d), RMS = %f", solution.getEvaluations(), solution.getIterations(), solution.getRMS()));
        }
        return solution.getPoint().toArray();
    } catch (final Exception ex) {
        IJ.log("Failed to find an elliptical solution, defaulting to polygon: " + ex.getMessage());
    }
    return null;
}
Also used : Point(java.awt.Point) LeastSquaresBuilder(org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder) Optimum(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum) PointVectorValuePair(org.apache.commons.math3.optim.PointVectorValuePair) LevenbergMarquardtOptimizer(org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer) DiagonalMatrix(org.apache.commons.math3.linear.DiagonalMatrix) LeastSquaresProblem(org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem) SimplePointChecker(org.apache.commons.math3.optim.SimplePointChecker) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Aggregations

InitialGuess (org.apache.commons.math3.optim.InitialGuess)3 MaxEval (org.apache.commons.math3.optim.MaxEval)3 ObjectiveFunction (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 MultivariateFunction (org.apache.commons.math3.analysis.MultivariateFunction)2 MultivariateVectorFunction (org.apache.commons.math3.analysis.MultivariateVectorFunction)2 PointValuePair (org.apache.commons.math3.optim.PointValuePair)2 PointVectorValuePair (org.apache.commons.math3.optim.PointVectorValuePair)2 SimplePointChecker (org.apache.commons.math3.optim.SimplePointChecker)2 OptimizationProblem (org.tweetyproject.math.opt.problem.OptimizationProblem)2 FloatConstant (org.tweetyproject.math.term.FloatConstant)2 Term (org.tweetyproject.math.term.Term)2 Variable (org.tweetyproject.math.term.Variable)2 Point (java.awt.Point)1 MultivariateMatrixFunction (org.apache.commons.math3.analysis.MultivariateMatrixFunction)1 LeastSquaresBuilder (org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder)1 Optimum (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum)1 LeastSquaresProblem (org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem)1 LevenbergMarquardtOptimizer (org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer)1