@Test public void testRosenbrockFunction() { QNMinimizer minimizer = new QNMinimizer(); Function f = new Rosenbrock(); double[] x = minimizer.minimize(f); double minValue = f.valueAt(x); Assert.assertEquals(x[0], 1.0, 1e-5); Assert.assertEquals(x[1], 1.0, 1e-5); Assert.assertEquals(minValue, 0, 1e-10); }
public double[] minimize(Function function) { Function l2RegFunction = new L2RegFunction(function, l2Cost); this.dimension = l2RegFunction.getDimension(); this.updateInfo = new UpdateInfo(this.m, this.dimension); double[] currPoint = new double[dimension]; double currValue = l2RegFunction.valueAt(currPoint); double[] currGrad = new double[dimension]; System.arraycopy(l2RegFunction.gradientAt(currPoint), 0, currGrad, 0, dimension); double[] pseudoGrad = null; if (l1Cost > 0) { currValue += l1Cost * ArrayMath.l1norm(currPoint); pseudoGrad = new double[dimension]; computePseudoGrad(currPoint, currGrad, pseudoGrad); } LineSearchResult lsr; if (l1Cost > 0) { lsr = LineSearchResult.getInitialObjectForL1( currValue, currGrad, pseudoGrad, currPoint); } else { lsr = LineSearchResult.getInitialObject( currValue, currGrad, currPoint); } if (verbose) { display("\nSolving convex optimization problem."); display("\nObjective function has " + dimension + " variable(s)."); display("\n\nPerforming " + iterations + " iterations with " + "L1Cost=" + l1Cost + " and L2Cost=" + l2Cost + "\n"); } double[] direction = new double[dimension]; long startTime = System.currentTimeMillis(); double initialStepSize = l1Cost > 0 ? ArrayMath.invL2norm(lsr.getPseudoGradAtNext()) : ArrayMath.invL2norm(lsr.getGradAtNext()); for (int iter = 1; iter <= iterations; iter++) { if (l1Cost > 0) { System.arraycopy(lsr.getPseudoGradAtNext(), 0, direction, 0, direction.length); } else { System.arraycopy(lsr.getGradAtNext(), 0, direction, 0, direction.length); } computeDirection(direction); if (l1Cost > 0) { pseudoGrad = lsr.getPseudoGradAtNext(); for (int i = 0; i < dimension; i++) { if (direction[i] * pseudoGrad[i] >= 0) { direction[i] = 0; } } LineSearch.doConstrainedLineSearch(l2RegFunction, direction, lsr, l1Cost, initialStepSize); computePseudoGrad(lsr.getNextPoint(), lsr.getGradAtNext(), pseudoGrad); lsr.setPseudoGradAtNext(pseudoGrad); } else { LineSearch.doLineSearch(l2RegFunction, direction, lsr, initialStepSize); } updateInfo.update(lsr); if (verbose) { if (iter < 10) display(" " + iter + ": "); else if (iter < 100) display(" " + iter + ": "); else display(iter + ": "); if (evaluator != null) { display("\t" + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\t" + evaluator.evaluate(lsr.getNextPoint()) + "\n"); } else { display("\t " + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\n"); } } if (isConverged(lsr)) break; initialStepSize = INITIAL_STEP_SIZE; } if (l1Cost > 0 && l2Cost > 0) { double[] x = lsr.getNextPoint(); for (int i = 0; i < dimension; i++) { x[i] = StrictMath.sqrt(1 + l2Cost) * x[i]; } } long endTime = System.currentTimeMillis(); long duration = endTime - startTime; display("Running time: " + (duration / 1000.) + "s\n"); this.updateInfo = null; System.gc(); double[] parameters = new double[dimension]; System.arraycopy(lsr.getNextPoint(), 0, parameters, 0, dimension); return parameters; }
QNMinimizer { public double[] minimize(Function function) { Function l2RegFunction = new L2RegFunction(function, l2Cost); this.dimension = l2RegFunction.getDimension(); this.updateInfo = new UpdateInfo(this.m, this.dimension); double[] currPoint = new double[dimension]; double currValue = l2RegFunction.valueAt(currPoint); double[] currGrad = new double[dimension]; System.arraycopy(l2RegFunction.gradientAt(currPoint), 0, currGrad, 0, dimension); double[] pseudoGrad = null; if (l1Cost > 0) { currValue += l1Cost * ArrayMath.l1norm(currPoint); pseudoGrad = new double[dimension]; computePseudoGrad(currPoint, currGrad, pseudoGrad); } LineSearchResult lsr; if (l1Cost > 0) { lsr = LineSearchResult.getInitialObjectForL1( currValue, currGrad, pseudoGrad, currPoint); } else { lsr = LineSearchResult.getInitialObject( currValue, currGrad, currPoint); } if (verbose) { display("\nSolving convex optimization problem."); display("\nObjective function has " + dimension + " variable(s)."); display("\n\nPerforming " + iterations + " iterations with " + "L1Cost=" + l1Cost + " and L2Cost=" + l2Cost + "\n"); } double[] direction = new double[dimension]; long startTime = System.currentTimeMillis(); double initialStepSize = l1Cost > 0 ? ArrayMath.invL2norm(lsr.getPseudoGradAtNext()) : ArrayMath.invL2norm(lsr.getGradAtNext()); for (int iter = 1; iter <= iterations; iter++) { if (l1Cost > 0) { System.arraycopy(lsr.getPseudoGradAtNext(), 0, direction, 0, direction.length); } else { System.arraycopy(lsr.getGradAtNext(), 0, direction, 0, direction.length); } computeDirection(direction); if (l1Cost > 0) { pseudoGrad = lsr.getPseudoGradAtNext(); for (int i = 0; i < dimension; i++) { if (direction[i] * pseudoGrad[i] >= 0) { direction[i] = 0; } } LineSearch.doConstrainedLineSearch(l2RegFunction, direction, lsr, l1Cost, initialStepSize); computePseudoGrad(lsr.getNextPoint(), lsr.getGradAtNext(), pseudoGrad); lsr.setPseudoGradAtNext(pseudoGrad); } else { LineSearch.doLineSearch(l2RegFunction, direction, lsr, initialStepSize); } updateInfo.update(lsr); if (verbose) { if (iter < 10) display(" " + iter + ": "); else if (iter < 100) display(" " + iter + ": "); else display(iter + ": "); if (evaluator != null) { display("\t" + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\t" + evaluator.evaluate(lsr.getNextPoint()) + "\n"); } else { display("\t " + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\n"); } } if (isConverged(lsr)) break; initialStepSize = INITIAL_STEP_SIZE; } if (l1Cost > 0 && l2Cost > 0) { double[] x = lsr.getNextPoint(); for (int i = 0; i < dimension; i++) { x[i] = StrictMath.sqrt(1 + l2Cost) * x[i]; } } long endTime = System.currentTimeMillis(); long duration = endTime - startTime; display("Running time: " + (duration / 1000.) + "s\n"); this.updateInfo = null; System.gc(); double[] parameters = new double[dimension]; System.arraycopy(lsr.getNextPoint(), 0, parameters, 0, dimension); return parameters; } }
QNMinimizer { public double[] minimize(Function function) { Function l2RegFunction = new L2RegFunction(function, l2Cost); this.dimension = l2RegFunction.getDimension(); this.updateInfo = new UpdateInfo(this.m, this.dimension); double[] currPoint = new double[dimension]; double currValue = l2RegFunction.valueAt(currPoint); double[] currGrad = new double[dimension]; System.arraycopy(l2RegFunction.gradientAt(currPoint), 0, currGrad, 0, dimension); double[] pseudoGrad = null; if (l1Cost > 0) { currValue += l1Cost * ArrayMath.l1norm(currPoint); pseudoGrad = new double[dimension]; computePseudoGrad(currPoint, currGrad, pseudoGrad); } LineSearchResult lsr; if (l1Cost > 0) { lsr = LineSearchResult.getInitialObjectForL1( currValue, currGrad, pseudoGrad, currPoint); } else { lsr = LineSearchResult.getInitialObject( currValue, currGrad, currPoint); } if (verbose) { display("\nSolving convex optimization problem."); display("\nObjective function has " + dimension + " variable(s)."); display("\n\nPerforming " + iterations + " iterations with " + "L1Cost=" + l1Cost + " and L2Cost=" + l2Cost + "\n"); } double[] direction = new double[dimension]; long startTime = System.currentTimeMillis(); double initialStepSize = l1Cost > 0 ? ArrayMath.invL2norm(lsr.getPseudoGradAtNext()) : ArrayMath.invL2norm(lsr.getGradAtNext()); for (int iter = 1; iter <= iterations; iter++) { if (l1Cost > 0) { System.arraycopy(lsr.getPseudoGradAtNext(), 0, direction, 0, direction.length); } else { System.arraycopy(lsr.getGradAtNext(), 0, direction, 0, direction.length); } computeDirection(direction); if (l1Cost > 0) { pseudoGrad = lsr.getPseudoGradAtNext(); for (int i = 0; i < dimension; i++) { if (direction[i] * pseudoGrad[i] >= 0) { direction[i] = 0; } } LineSearch.doConstrainedLineSearch(l2RegFunction, direction, lsr, l1Cost, initialStepSize); computePseudoGrad(lsr.getNextPoint(), lsr.getGradAtNext(), pseudoGrad); lsr.setPseudoGradAtNext(pseudoGrad); } else { LineSearch.doLineSearch(l2RegFunction, direction, lsr, initialStepSize); } updateInfo.update(lsr); if (verbose) { if (iter < 10) display(" " + iter + ": "); else if (iter < 100) display(" " + iter + ": "); else display(iter + ": "); if (evaluator != null) { display("\t" + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\t" + evaluator.evaluate(lsr.getNextPoint()) + "\n"); } else { display("\t " + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\n"); } } if (isConverged(lsr)) break; initialStepSize = INITIAL_STEP_SIZE; } if (l1Cost > 0 && l2Cost > 0) { double[] x = lsr.getNextPoint(); for (int i = 0; i < dimension; i++) { x[i] = StrictMath.sqrt(1 + l2Cost) * x[i]; } } long endTime = System.currentTimeMillis(); long duration = endTime - startTime; display("Running time: " + (duration / 1000.) + "s\n"); this.updateInfo = null; System.gc(); double[] parameters = new double[dimension]; System.arraycopy(lsr.getNextPoint(), 0, parameters, 0, dimension); return parameters; } QNMinimizer();  QNMinimizer(double l1Cost, double l2Cost);  QNMinimizer(double l1Cost, double l2Cost, int iterations);  QNMinimizer(double l1Cost, double l2Cost,
      int iterations, int m, int maxFctEval);  QNMinimizer(double l1Cost, double l2Cost, int iterations,
      int m, int maxFctEval, boolean verbose); }
QNMinimizer { public double[] minimize(Function function) { Function l2RegFunction = new L2RegFunction(function, l2Cost); this.dimension = l2RegFunction.getDimension(); this.updateInfo = new UpdateInfo(this.m, this.dimension); double[] currPoint = new double[dimension]; double currValue = l2RegFunction.valueAt(currPoint); double[] currGrad = new double[dimension]; System.arraycopy(l2RegFunction.gradientAt(currPoint), 0, currGrad, 0, dimension); double[] pseudoGrad = null; if (l1Cost > 0) { currValue += l1Cost * ArrayMath.l1norm(currPoint); pseudoGrad = new double[dimension]; computePseudoGrad(currPoint, currGrad, pseudoGrad); } LineSearchResult lsr; if (l1Cost > 0) { lsr = LineSearchResult.getInitialObjectForL1( currValue, currGrad, pseudoGrad, currPoint); } else { lsr = LineSearchResult.getInitialObject( currValue, currGrad, currPoint); } if (verbose) { display("\nSolving convex optimization problem."); display("\nObjective function has " + dimension + " variable(s)."); display("\n\nPerforming " + iterations + " iterations with " + "L1Cost=" + l1Cost + " and L2Cost=" + l2Cost + "\n"); } double[] direction = new double[dimension]; long startTime = System.currentTimeMillis(); double initialStepSize = l1Cost > 0 ? ArrayMath.invL2norm(lsr.getPseudoGradAtNext()) : ArrayMath.invL2norm(lsr.getGradAtNext()); for (int iter = 1; iter <= iterations; iter++) { if (l1Cost > 0) { System.arraycopy(lsr.getPseudoGradAtNext(), 0, direction, 0, direction.length); } else { System.arraycopy(lsr.getGradAtNext(), 0, direction, 0, direction.length); } computeDirection(direction); if (l1Cost > 0) { pseudoGrad = lsr.getPseudoGradAtNext(); for (int i = 0; i < dimension; i++) { if (direction[i] * pseudoGrad[i] >= 0) { direction[i] = 0; } } LineSearch.doConstrainedLineSearch(l2RegFunction, direction, lsr, l1Cost, initialStepSize); computePseudoGrad(lsr.getNextPoint(), lsr.getGradAtNext(), pseudoGrad); lsr.setPseudoGradAtNext(pseudoGrad); } else { LineSearch.doLineSearch(l2RegFunction, direction, lsr, initialStepSize); } updateInfo.update(lsr); if (verbose) { if (iter < 10) display(" " + iter + ": "); else if (iter < 100) display(" " + iter + ": "); else display(iter + ": "); if (evaluator != null) { display("\t" + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\t" + evaluator.evaluate(lsr.getNextPoint()) + "\n"); } else { display("\t " + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\n"); } } if (isConverged(lsr)) break; initialStepSize = INITIAL_STEP_SIZE; } if (l1Cost > 0 && l2Cost > 0) { double[] x = lsr.getNextPoint(); for (int i = 0; i < dimension; i++) { x[i] = StrictMath.sqrt(1 + l2Cost) * x[i]; } } long endTime = System.currentTimeMillis(); long duration = endTime - startTime; display("Running time: " + (duration / 1000.) + "s\n"); this.updateInfo = null; System.gc(); double[] parameters = new double[dimension]; System.arraycopy(lsr.getNextPoint(), 0, parameters, 0, dimension); return parameters; } QNMinimizer();  QNMinimizer(double l1Cost, double l2Cost);  QNMinimizer(double l1Cost, double l2Cost, int iterations);  QNMinimizer(double l1Cost, double l2Cost,
      int iterations, int m, int maxFctEval);  QNMinimizer(double l1Cost, double l2Cost, int iterations,
      int m, int maxFctEval, boolean verbose); Evaluator getEvaluator(); void setEvaluator(Evaluator evaluator); double[] minimize(Function function); }
QNMinimizer { public double[] minimize(Function function) { Function l2RegFunction = new L2RegFunction(function, l2Cost); this.dimension = l2RegFunction.getDimension(); this.updateInfo = new UpdateInfo(this.m, this.dimension); double[] currPoint = new double[dimension]; double currValue = l2RegFunction.valueAt(currPoint); double[] currGrad = new double[dimension]; System.arraycopy(l2RegFunction.gradientAt(currPoint), 0, currGrad, 0, dimension); double[] pseudoGrad = null; if (l1Cost > 0) { currValue += l1Cost * ArrayMath.l1norm(currPoint); pseudoGrad = new double[dimension]; computePseudoGrad(currPoint, currGrad, pseudoGrad); } LineSearchResult lsr; if (l1Cost > 0) { lsr = LineSearchResult.getInitialObjectForL1( currValue, currGrad, pseudoGrad, currPoint); } else { lsr = LineSearchResult.getInitialObject( currValue, currGrad, currPoint); } if (verbose) { display("\nSolving convex optimization problem."); display("\nObjective function has " + dimension + " variable(s)."); display("\n\nPerforming " + iterations + " iterations with " + "L1Cost=" + l1Cost + " and L2Cost=" + l2Cost + "\n"); } double[] direction = new double[dimension]; long startTime = System.currentTimeMillis(); double initialStepSize = l1Cost > 0 ? ArrayMath.invL2norm(lsr.getPseudoGradAtNext()) : ArrayMath.invL2norm(lsr.getGradAtNext()); for (int iter = 1; iter <= iterations; iter++) { if (l1Cost > 0) { System.arraycopy(lsr.getPseudoGradAtNext(), 0, direction, 0, direction.length); } else { System.arraycopy(lsr.getGradAtNext(), 0, direction, 0, direction.length); } computeDirection(direction); if (l1Cost > 0) { pseudoGrad = lsr.getPseudoGradAtNext(); for (int i = 0; i < dimension; i++) { if (direction[i] * pseudoGrad[i] >= 0) { direction[i] = 0; } } LineSearch.doConstrainedLineSearch(l2RegFunction, direction, lsr, l1Cost, initialStepSize); computePseudoGrad(lsr.getNextPoint(), lsr.getGradAtNext(), pseudoGrad); lsr.setPseudoGradAtNext(pseudoGrad); } else { LineSearch.doLineSearch(l2RegFunction, direction, lsr, initialStepSize); } updateInfo.update(lsr); if (verbose) { if (iter < 10) display(" " + iter + ": "); else if (iter < 100) display(" " + iter + ": "); else display(iter + ": "); if (evaluator != null) { display("\t" + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\t" + evaluator.evaluate(lsr.getNextPoint()) + "\n"); } else { display("\t " + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate() + "\n"); } } if (isConverged(lsr)) break; initialStepSize = INITIAL_STEP_SIZE; } if (l1Cost > 0 && l2Cost > 0) { double[] x = lsr.getNextPoint(); for (int i = 0; i < dimension; i++) { x[i] = StrictMath.sqrt(1 + l2Cost) * x[i]; } } long endTime = System.currentTimeMillis(); long duration = endTime - startTime; display("Running time: " + (duration / 1000.) + "s\n"); this.updateInfo = null; System.gc(); double[] parameters = new double[dimension]; System.arraycopy(lsr.getNextPoint(), 0, parameters, 0, dimension); return parameters; } QNMinimizer();  QNMinimizer(double l1Cost, double l2Cost);  QNMinimizer(double l1Cost, double l2Cost, int iterations);  QNMinimizer(double l1Cost, double l2Cost,
      int iterations, int m, int maxFctEval);  QNMinimizer(double l1Cost, double l2Cost, int iterations,
      int m, int maxFctEval, boolean verbose); Evaluator getEvaluator(); void setEvaluator(Evaluator evaluator); double[] minimize(Function function); static final double CONVERGE_TOLERANCE; static final double REL_GRAD_NORM_TOL; static final double INITIAL_STEP_SIZE; static final double MIN_STEP_SIZE; static final double L1COST_DEFAULT; static final double L2COST_DEFAULT; static final int NUM_ITERATIONS_DEFAULT; static final int M_DEFAULT; static final int MAX_FCT_EVAL_DEFAULT; }