List of usage examples for org.apache.commons.math.optimization RealPointValuePair getValue
public double getValue()
From source file:com.polytech4A.cuttingstock.core.method.LinearResolutionMethod.java
/** * Resolve linear programming problem when minimizing the equation with current constraints. Returns * * @param solution Solution to minimize the objective the function from. * @return Number of printings and cost value. */// ww w.j a v a 2 s . c o m public Result minimize(Solution solution) { updateFunction(solution); updateConstraints(solution); try { RealPointValuePair result = new SimplexSolver().optimize(function, constraints, GoalType.MINIMIZE, true); double[] point = result.getPoint(); if (result.getValue() < 0) { return null; } for (int i = 0; i < point.length; ++i) { if (point[i] < 0) { return null; } } return new Result(point, context.getSheetCost(), context.getPatternCost()); } catch (OptimizationException e) { logger.debug("LinearResolutionMethod.minimize: " + e.getMessage()); } return null; }
From source file:fi.smaa.libror.UTAGMSSolver.java
/** * Check whether relation holds.//from w w w. j a v a 2s . c o m * * @param i index of first alternative, PRECOND: >= 0 * @param j index of the second alternative, PRECOND: >= 0 * @param rorConstraints base constraints E_{ROR}^{A^R} * @param necessary true if the relation solved is the necessary one, false otherwise * @return */ private boolean solveRelation(int i, int j, List<LinearConstraint> rorConstraints, boolean necessary) { if (i < 0 && j < 0) { throw new IllegalArgumentException("PRECOND violation"); } if (i == j) { return true; } List<LinearConstraint> constraints = new ArrayList<LinearConstraint>(rorConstraints); addNecOrPrefConstraint(i, j, necessary, constraints); LinearObjectiveFunction goalFunction = buildObjectiveFunction(); try { RealPointValuePair res = solver.optimize(goalFunction, constraints, GoalType.MAXIMIZE, true); if (necessary) { return res.getValue() <= 0.0; } else { // possible return res.getValue() > 0.0; } } catch (NoFeasibleSolutionException e) { if (necessary) { return true; } else { // possible return false; } } catch (OptimizationException e) { throw new IllegalStateException("Invalid OptimizationException: " + e.getMessage()); } }
From source file:fi.smaa.libror.UTAGMSSolver.java
public void solve(RelationsType rel) throws InfeasibleConstraintsException { necessaryRelation = new Array2DRowRealMatrix(model.getNrAlternatives(), model.getNrAlternatives()); possibleRelation = new Array2DRowRealMatrix(model.getNrAlternatives(), model.getNrAlternatives()); List<LinearConstraint> baseConstraints = buildRORConstraints(); // check that the set of constraints is feasible try {/*from ww w .j a v a2s . c o m*/ RealPointValuePair res = solver.optimize(buildObjectiveFunction(), baseConstraints, GoalType.MAXIMIZE, true); if (res.getValue() <= 0.0) { throw new InfeasibleConstraintsException( "Preference information leading to infeasible constraints, epsilon <= 0.0"); } } catch (OptimizationException e) { throw new InfeasibleConstraintsException( "Preference information leading to infeasible constraints: " + e.getMessage()); } for (int i = 0; i < model.getNrAlternatives(); i++) { for (int j = 0; j < model.getNrAlternatives(); j++) { boolean necHolds = false; if (rel.equals(RelationsType.NECESSARY) || rel.equals(RelationsType.BOTH)) { necHolds = solveRelation(i, j, baseConstraints, true); necessaryRelation.setEntry(i, j, necHolds ? 1.0 : 0.0); } if (rel.equals(RelationsType.POSSIBLE) || rel.equals(RelationsType.BOTH)) { if (necHolds) { possibleRelation.setEntry(i, j, 1.0); } else { possibleRelation.setEntry(i, j, solveRelation(i, j, baseConstraints, false) ? 1.0 : 0.0); } } } } }
From source file:Align_Projections.java
public boolean converged(final int iteration, final RealPointValuePair previous, final RealPointValuePair current) { if (apObject.getStopTuning()) { return true; }/*from www. j a v a 2 s . com*/ final double p = previous.getValue(); final double c = current.getValue(); final double difference = FastMath.abs(p - c); final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c)); return (difference <= (size * relativeThreshold)) || (difference <= absoluteThreshold); }
From source file:Align_Projections.java
public void run() { detectorAngle = Double.valueOf(detectorAngleText.getText()).doubleValue(); centerPixel = Double.valueOf(centerPixelText.getText()).doubleValue(); horizontalBorder = Integer.valueOf(horizontalBorderText.getText()).intValue(); topBorder = Integer.valueOf(topBorderText.getText()).intValue(); bottomBorder = Integer.valueOf(bottomBorderText.getText()).intValue(); // IJ.log("Starting worker thread"); int count = 0; double[] x = new double[2]; x[0] = centerPixel * tuningWeights[0]; x[1] = detectorAngle * tuningWeights[1]; PowellOptimizer maximizer = new PowellOptimizer(1E-4); maximizer.setConvergenceChecker(new ConvergenceCheckerWithManualCancel(this, 1E-4, 1E-4)); maximizer.setMaxEvaluations(1000000); maximizer.setMaxIterations(1000000); try {//from w w w. ja va2 s. c o m // IJ.log("Starting optimization first round"); RealPointValuePair result = maximizer.optimize(this, GoalType.MAXIMIZE, x); centerPixel = result.getPoint()[0] / tuningWeights[0]; detectorAngle = result.getPoint()[1] / tuningWeights[1]; centerPixelText.setText(IJ.d2s(centerPixel, 6)); detectorAngleText.setText(IJ.d2s(detectorAngle, 6)); crossCorrelation = result.getValue(); updateCrossCorrelation(); } catch (GetMeOuttaHereException e) { } catch (Exception e) { IJ.log("Exception occurred in optimizer."); stopTuning = true; } // Now do the whole thing again, but with narrower tolerances (the defaults, which are roughly machine precision) if (!stopTuning) { maximizer = new PowellOptimizer(); maximizer.setConvergenceChecker(new ConvergenceCheckerWithManualCancel(this)); maximizer.setMaxEvaluations(1000000); maximizer.setMaxIterations(1000000); try { // IJ.log("Starting optimization second round"); RealPointValuePair result = maximizer.optimize(this, GoalType.MAXIMIZE, x); centerPixel = result.getPoint()[0] / tuningWeights[0]; detectorAngle = result.getPoint()[1] / tuningWeights[1]; centerPixelText.setText(IJ.d2s(centerPixel, 6)); detectorAngleText.setText(IJ.d2s(detectorAngle, 6)); crossCorrelation = result.getValue(); updateCrossCorrelation(); } catch (GetMeOuttaHereException e) { } catch (Exception e) { IJ.log("Exception occurred in optimizer."); } } UpdateOverlayAndControls(); optimizeButton.setLabel("Optimize"); optimizeButton.setEnabled(true); updateButton.setEnabled(true); applyButton.setEnabled(true); resetButton.setEnabled(true); detectorAngleText.setEnabled(true); centerPixelText.setEnabled(true); horizontalBorderText.setEnabled(true); topBorderText.setEnabled(true); bottomBorderText.setEnabled(true); // IJ.log("Exiting worker thread"); }
From source file:ch.algotrader.simulation.SimulationExecutorImpl.java
/** * {@inheritDoc}/* w w w .j a v a 2s .c o m*/ */ @Override public void optimizeMultiParam(final StrategyGroup strategyGroup, final String[] parameters, final double[] starts) { Validate.notNull(parameters, "Parameter is null"); Validate.notNull(starts, "Starts is null"); RealPointValuePair result; try { MultivariateRealFunction function = new MultivariateFunction(this, strategyGroup, parameters); MultivariateRealOptimizer optimizer = new MultiDirectional(); optimizer.setConvergenceChecker(new SimpleScalarValueChecker(0.0, 0.01)); result = optimizer.optimize(function, GoalType.MAXIMIZE, starts); if (RESULT_LOGGER.isInfoEnabled()) { for (int i = 0; i < result.getPoint().length; i++) { RESULT_LOGGER.info("optimal value for {}={}", parameters[i], format.format(result.getPoint()[i])); } RESULT_LOGGER.info("functionValue: {} needed iterations: {})", format.format(result.getValue()), optimizer.getEvaluations()); } } catch (MathException ex) { throw new SimulationExecutorException(ex); } }
From source file:org.rascalmpl.library.analysis.linearprogramming.LinearProgramming.java
public IValue llOptimize(IBool minimize, IBool nonNegative, ISet constraints, IConstructor f) { SimplexSolver solver = new SimplexSolver(); ArrayList<LinearConstraint> constraintsJ = new ArrayList<LinearConstraint>(constraints.size()); for (IValue v : constraints) { constraintsJ.add(convertConstraint((IConstructor) v)); }/*from w w w . j ava 2 s .c o m*/ LinearObjectiveFunction fJ = convertLinObjFun(f); GoalType goal = minimize.getValue() ? GoalType.MINIMIZE : GoalType.MAXIMIZE; IValueFactory vf = values; boolean nonNegativeJ = nonNegative.getValue(); try { RealPointValuePair res = solver.optimize(fJ, constraintsJ, goal, nonNegativeJ); return vf.constructor(Maybe.Maybe_just, vf.constructor(LLSolution_llSolution, convertToRealList(res.getPoint(), vf), vf.real(res.getValue()))); } catch (Exception e) { return vf.constructor(Maybe.Maybe_nothing); } }
From source file:org.renjin.primitives.optimize.Optimizations.java
/** * General-purpose optimization based on NelderMead, quasi-Newton and conjugate-gradient algorithms. * It includes an option for box-constrained * optimization and simulated annealing. * * @param par initial parameters/*w ww . j ava 2 s . c o m*/ * @param fn * @param gradientFunction * @param method * @param controlParameters * @param lower * @param upper * @return */ @Primitive public static ListVector optim(@Current Context context, @Current Environment rho, DoubleVector par, Function fn, SEXP gradientFunction, String method, ListVector controlParameters, DoubleVector lower, DoubleVector upper) { MultivariateRealClosure g = new MultivariateRealClosure(context, rho, fn); if (method.equals("Nelder-Mead")) { NelderMead optimizer = new NelderMead(); try { RealPointValuePair res = optimizer.optimize(g, GoalType.MINIMIZE, par.toDoubleArray()); ListVector.Builder result = new ListVector.Builder(); result.add(new DoubleArrayVector(res.getPoint())); result.add(new DoubleArrayVector(res.getValue())); result.add(new IntArrayVector(IntVector.NA, IntVector.NA)); result.add(new IntArrayVector(0)); result.add(Null.INSTANCE); return result.build(); } catch (FunctionEvaluationException e) { throw new EvalException(e); } catch (OptimizationException e) { throw new EvalException(e); } } else { throw new EvalException("method '" + method + "' not implemented."); } }
From source file:org.renjin.stats.internals.optimize.Optimizations.java
/** * General-purpose optimization based on NelderMead, quasi-Newton and conjugate-gradient algorithms. * It includes an option for box-constrained * optimization and simulated annealing. * * @param par initial parameters//from ww w. ja v a 2 s. c o m * @param fn * @param gradientFunction * @param method * @param controlParameters * @param lower * @param upper * @return */ @Internal public static ListVector optim(@Current Context context, @Current Environment rho, DoubleVector par, Function fn, SEXP gradientFunction, String method, ListVector controlParameters, DoubleVector lower, DoubleVector upper) { MultivariateRealClosure g = new MultivariateRealClosure(context, rho, fn); if (method.equals("Nelder-Mead")) { NelderMead optimizer = new NelderMead(); try { RealPointValuePair res = optimizer.optimize(g, GoalType.MINIMIZE, par.toDoubleArray()); ListVector.Builder result = new ListVector.Builder(); result.add(new DoubleArrayVector(res.getPoint())); result.add(new DoubleArrayVector(res.getValue())); result.add(new IntArrayVector(IntVector.NA, IntVector.NA)); result.add(new IntArrayVector(0)); result.add(Null.INSTANCE); return result.build(); } catch (FunctionEvaluationException e) { throw new EvalException(e); } catch (OptimizationException e) { throw new EvalException(e); } } else { throw new EvalException("method '" + method + "' not implemented."); } }
From source file:r.base.optimize.Optimizations.java
/** * General-purpose optimization based on NelderMead, quasi-Newton and conjugate-gradient algorithms. * It includes an option for box-constrained * optimization and simulated annealing. * * @param par initial parameters/*from ww w. jav a2 s.c o m*/ * @param fn * @param gradientFunction * @param method * @param controlParameters * @param lower * @param upper * @return */ @Primitive public static ListVector optim(@Current Context context, @Current Environment rho, DoubleVector par, Function fn, SEXP gradientFunction, String method, ListVector controlParameters, DoubleVector lower, DoubleVector upper) { MultivariateRealClosure g = new MultivariateRealClosure(context, rho, fn); if (method.equals("Nelder-Mead")) { NelderMead optimizer = new NelderMead(); try { RealPointValuePair res = optimizer.optimize(g, GoalType.MINIMIZE, par.toDoubleArray()); ListVector.Builder result = new ListVector.Builder(); result.add(new DoubleVector(res.getPoint())); result.add(new DoubleVector(res.getValue())); result.add(new IntVector(IntVector.NA, IntVector.NA)); result.add(new IntVector(0)); result.add(Null.INSTANCE); return result.build(); } catch (FunctionEvaluationException e) { throw new EvalException(e); } catch (OptimizationException e) { throw new EvalException(e); } } else { throw new EvalException("method '" + method + "' not implemented."); } }