List of usage examples for org.apache.commons.math3.optim PointValuePair getPoint
public double[] getPoint()
From source file:com.opengamma.strata.math.impl.util.CommonsMathWrapper.java
/** * Unwraps a pair./* w w w .jav a2 s .c om*/ * * @param x a Commons pair of <i>(x, f(x))</i> * @return a matrix of double with the <i>x</i> as the first element and <i>f(x)</i> the second */ public static double[] unwrap(PointValuePair x) { ArgChecker.notNull(x, "x"); return x.getPoint(); }
From source file:com.datumbox.framework.core.mathematics.linearprogramming.LPSolver.java
/** * Solves the LP problem and returns the result. * // ww w . j av a2 s. co m * @param linearObjectiveFunction * @param linearConstraintsList * @param nonNegative * @param maximize * @return */ public static LPResult solve(double[] linearObjectiveFunction, List<LPSolver.LPConstraint> linearConstraintsList, boolean nonNegative, boolean maximize) { int m = linearConstraintsList.size(); List<LinearConstraint> constraints = new ArrayList<>(m); for (LPSolver.LPConstraint constraint : linearConstraintsList) { String sign = constraint.getSign(); Relationship relationship = null; if (LPSolver.GEQ.equals(sign)) { relationship = Relationship.GEQ; } else if (LPSolver.LEQ.equals(sign)) { relationship = Relationship.LEQ; } else if (LPSolver.EQ.equals(sign)) { relationship = Relationship.EQ; } constraints .add(new LinearConstraint(constraint.getContraintBody(), relationship, constraint.getValue())); } SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(new LinearObjectiveFunction(linearObjectiveFunction, 0.0), new LinearConstraintSet(constraints), maximize ? GoalType.MAXIMIZE : GoalType.MINIMIZE, new NonNegativeConstraint(nonNegative), PivotSelectionRule.BLAND); LPResult result = new LPResult(); result.setObjectiveValue(solution.getValue()); result.setVariableValues(solution.getPoint()); return result; }
From source file:com.wwidesigner.optimization.ObjectiveFunctionOptimizer.java
/** * Use a specified optimizer type to optimize a specified objective * function.//from w w w .j a v a2 s.com * * @param objective * - objective function to optimize * @param optimizerType * - type of optimizer to use */ public static boolean optimizeObjectiveFunction(BaseObjectiveFunction objective, BaseObjectiveFunction.OptimizerType optimizerType) { System.out.print("\nSystem has "); System.out.print(objective.getNrDimensions()); System.out.print(" optimization variables and "); System.out.print(objective.getNrNotes()); System.out.print(" target notes."); long startTime = System.currentTimeMillis(); double[] startPoint = objective.getInitialPoint(); double[] errorVector = objective.getErrorVector(startPoint); initialNorm = objective.calcNorm(errorVector); System.out.println(); printErrors("Initial error: ", initialNorm, errorVector); finalNorm = initialNorm; try { if (objective.isMultiStart()) { // Make the startPoint from the optimization result of a // DIRECT/BOBYQA run. PointValuePair outcome; MultivariateOptimizer optimizer = new DIRECTCOptimizer(6.0e-6); outcome = runDirect(optimizer, objective, startPoint); System.out.println("After global optimizer, error: " + outcome.getValue()); outcome = runBobyqa(objective, outcome.getPoint()); System.out.println("Refined start, error: " + outcome.getValue()); outcome = optimizeMultiStart(objective, outcome.getPoint()); if (outcome == null) { // Restore starting point. objective.setGeometryPoint(startPoint); } else { objective.setGeometryPoint(outcome.getPoint()); } } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.BrentOptimizer)) { // Univariate optimization. UnivariatePointValuePair outcome = runBrent(objective, startPoint); double[] geometry = new double[1]; geometry[0] = outcome.getPoint(); objective.setGeometryPoint(geometry); } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.PowellOptimizer)) { // Multivariate optimization, without bounds. PointValuePair outcome = runPowell(objective, startPoint); objective.setGeometryPoint(outcome.getPoint()); } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.SimplexOptimizer)) { // Multivariate optimization, without bounds. PointValuePair outcome = runSimplex(objective, startPoint); objective.setGeometryPoint(outcome.getPoint()); } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.CMAESOptimizer)) { // Multivariate optimization, with bounds. PointValuePair outcome = runCmaes(objective, startPoint); objective.setGeometryPoint(outcome.getPoint()); } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.DIRECTOptimizer)) { // Multivariate DIRECT optimization, with bounds. // Convergence threshold about 3^-15. MultivariateOptimizer optimizer = new DIRECTCOptimizer(7.0e-8); PointValuePair outcome = runDirect(optimizer, objective, startPoint); System.out.println("After " + objective.getNumberOfEvaluations() + " evaluations, global optimizer found optimum " + outcome.getValue()); // Use BOBYQA to refine global optimum found. PointValuePair outcome2 = runBobyqa(objective, outcome.getPoint()); if (outcome.getValue() < outcome2.getValue()) { // Don't use second-stage optimum if it isn't better. System.out.println("Second-stage optimizer found optimum " + outcome2.getValue()); objective.setGeometryPoint(outcome.getPoint()); } else { objective.setGeometryPoint(outcome2.getPoint()); } } else { // Multivariate BOBYQA optimization, with bounds. PointValuePair outcome = runBobyqa(objective, startPoint); objective.setGeometryPoint(outcome.getPoint()); } } catch (TooManyEvaluationsException e) { System.out.println("Exception: " + e.getMessage()); } catch (OperationCancelledException e) { if (objective.isMultiStart()) { System.out.println("\nOptimization cancelled.\n"); return false; } System.out.println("\nOptimization cancelled.\nPartially-optimized result returned.\n"); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); e.printStackTrace(); return false; } System.out.print("Performed "); System.out.print(objective.getNumberOfTunings()); System.out.print(" tuning calculations in "); System.out.print(objective.getNumberOfEvaluations()); System.out.println(" error norm evaluations."); errorVector = objective.getErrorVector(objective.getInitialPoint()); finalNorm = objective.calcNorm(errorVector); printErrors("Final error: ", finalNorm, errorVector); System.out.print("Residual error ratio: "); System.out.println(finalNorm / initialNorm); long elapsedTime = System.currentTimeMillis() - startTime; double elapsedSeconds = 0.001 * (double) elapsedTime; System.out.print("Elapsed time: "); System.out.printf("%3.1f", elapsedSeconds); System.out.println(" seconds."); return true; }
From source file:com.wwidesigner.optimization.ObjectiveFunctionOptimizer.java
protected static PointValuePair runBobyqa(BOBYQAOptimizer optimizer, BaseObjectiveFunction objective, double[] startPoint, int maxEvaluations) throws TooManyEvaluationsException { PointValuePair outcome; EvaluatorInterface originalEvaluator = objective.getEvaluator(); if (objective.isRunTwoStageOptimization()) { objective.setEvaluator(objective.getFirstStageEvaluator()); outcome = optimizer.optimize(GoalType.MINIMIZE, new ObjectiveFunction(objective), new MaxEval(maxEvaluations), MaxIter.unlimited(), new InitialGuess(startPoint), new SimpleBounds(objective.getLowerBounds(), objective.getUpperBounds())); objective.setGeometryPoint(outcome.getPoint()); startPoint = objective.getInitialPoint(); }// ww w.j a va 2 s .com objective.setEvaluator(originalEvaluator); outcome = optimizer.optimize(GoalType.MINIMIZE, new ObjectiveFunction(objective), new MaxEval(maxEvaluations - optimizer.getEvaluations()), MaxIter.unlimited(), new InitialGuess(startPoint), new SimpleBounds(objective.getLowerBounds(), objective.getUpperBounds())); return outcome; }
From source file:net.java.jinterval.field.MultivariateFunctionAdapterTest.java
/** * Test of optimize method, of class MultivariateFunctionAdapter. *///from w w w.j a v a2s .c om @Test public void testOptimize() { System.out.println("optimize"); OptimizationProblem problem = FireRisk.createOptimizationProblemTempMaxFv(); PointValuePair pv = MultivariateFunctionAdapter.optimize(problem); assertEquals(-0.08021443147650907, pv.getValue(), 1e-16); assertEquals(170.0, pv.getPoint()[0], 0); assertEquals(299.0, pv.getPoint()[1], 0); assertEquals(9.0, pv.getPoint()[2], 0); assertEquals(34.8, pv.getPoint()[3], 0); }
From source file:edu.ucsf.valelab.saim.calculations.SaimErrorFunctionFitter.java
public double[] fit(Collection<WeightedObservedPoint> observedPoints) { SaimErrorFunction ser = new SaimErrorFunction(data_, observedPoints); MultivariateOptimizer optimizer = new BOBYQAOptimizer(6, 10, 1.0E-8); double[] lb = { 0.0, 0.0, 0.0 }; double[] ub = { 64000, 64000, 1000 }; SimpleBounds sb = new SimpleBounds(lb, ub); PointValuePair results = optimizer.optimize(new MaxEval(20000), GoalType.MINIMIZE, new InitialGuess(guess_), new ObjectiveFunction(ser), sb); System.out.println("Value: " + results.getValue()); return results.getPoint(); }
From source file:fr.imag.ppplib.calc.opti.ACMLinearProgrammingSolver.java
@Override public void solve(double[] dir) { /* Check the dimension of the vectorspace where lives dir */ if (dir.length != d) throw new LinearProgrammingSolverException(dddirMessage); /* Update the constraints set of the simplex solver if modification */ if (lcListModified) { List<LinearConstraint> lcList = new ArrayList<LinearConstraint>(); int n = dirList.size(); for (int i = 0; i < n; i++) lcList.add(new LinearConstraint(dirList.get(i), Relationship.LEQ, valList.get(i))); lcSet = new LinearConstraintSet(lcList); }/* w w w. j ava 2 s . c om*/ /* Evaluation */ PointValuePair res = solver.optimize(new LinearObjectiveFunction(dir, 0), lcSet, GoalType.MAXIMIZE); /* Update the results and the flags */ point = res.getPoint(); value = res.getSecond(); evaluated = true; lcListModified = false; }
From source file:de.bund.bfr.math.MultivariateOptimization.java
private Result getResults(PointValuePair optimizerResults) { Result r = new Result(); r.logLikelihood = optimizerResults.getValue(); for (int i = 0; i < parameters.size(); i++) { if (parameters.get(i).equals(sdParam)) { r.sdValue = optimizerResults.getPoint()[i]; } else {/*w w w . java 2 s .c om*/ r.parameterValues.put(parameters.get(i), optimizerResults.getPoint()[i]); } } return r; }
From source file:hulo.localization.models.obs.GaussianProcessLDPLMeanMulti.java
public void optimizeLDPLMultiHyperParams() { final GaussianProcessLDPLMeanMulti gpLDPLMulti = this; MultivariateFunction costFunc = new MultivariateFunction() { @Override/*from w w w . jav a 2s .com*/ public double value(double[] point) { double[] params = gpLDPLMulti.getParams(); params[0] = point[0]; params[1] = point[1]; gpLDPLMulti.setParams(params); double[] lmdRegu = gpLDPLMulti.getStabilizeParameter(); lmdRegu[0] = Math.pow(10, point[2]); lmdRegu[1] = Math.pow(10, point[3]); gpLDPLMulti.setStabilizeParameter(lmdRegu); int ns = X.length; double aveLOOMSE = 0.0; for (int k = 0; k < ns; k++) { gpLDPLMulti.fit(X, Y); double looMSE = gpLDPLMulti.looErrorLDPLMultiPart(k); aveLOOMSE += looMSE / ns; } final StringBuilder sb = new StringBuilder(); sb.setLength(0); sb.append("optimizeLDPLMultiHyperParams: "); sb.append("aveLOOMSE=" + aveLOOMSE + ", "); sb.append("n=" + params[0] + ",A=" + params[1] + ", lmdn=" + lmdRegu[0] + ",lmdA=" + lmdRegu[1]); sb.append(", ns=" + ns); System.out.println(sb.toString()); return aveLOOMSE; } }; double[] pointInit = { params[0], params[1], Math.log10(lambdas[0]), Math.log10(lambdas[1]) }; double[] dPointInit = { 0.1, 1.0, 0.1, 0.1 }; PointValuePair pair = GaussianProcessLDPLMean.minimize(costFunc, pointInit, dPointInit); double[] point = pair.getPoint(); double[] params = gpLDPLMulti.getParams(); params[0] = point[0]; params[1] = point[1]; gpLDPLMulti.setParams(params); double[] lambdas = gpLDPLMulti.getStabilizeParameter(); lambdas[0] = Math.pow(10, point[2]); lambdas[1] = Math.pow(10, point[3]); gpLDPLMulti.setStabilizeParameter(lambdas); }
From source file:edu.cmu.tetrad.sem.SemOptimizerPowell.java
public void optimize(SemIm semIm) { double min = Double.POSITIVE_INFINITY; double[] point = null; for (int count = 0; count < numRestarts + 1; count++) { System.out.println("Trial " + (count + 1)); SemIm _sem2 = new SemIm(semIm); List<Parameter> freeParameters = _sem2.getFreeParameters(); double[] p = new double[freeParameters.size()]; for (int i = 0; i < freeParameters.size(); i++) { if (freeParameters.get(i).getType() == ParamType.VAR) { p[i] = RandomUtil.getInstance().nextUniform(0, 1); } else { p[i] = RandomUtil.getInstance().nextUniform(-1, 1); }/*from w w w . j av a 2 s.c o m*/ } _sem2.setFreeParamValues(p); MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7); PointValuePair pair = search.optimize(new InitialGuess(_sem2.getFreeParamValues()), new ObjectiveFunction(fittingFunction(semIm)), GoalType.MINIMIZE, new MaxEval(100000)); double chisq = _sem2.getChiSquare(); System.out.println("chisq = " + chisq); if (chisq < min) { min = chisq; point = pair.getPoint(); } } System.arraycopy(point, 0, semIm.getFreeParamValues(), 0, point.length); }