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;
}
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();
}
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;
}
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;
}
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;
}
Aggregations