Example usage for org.apache.commons.math3.exception TooManyEvaluationsException printStackTrace

List of usage examples for org.apache.commons.math3.exception TooManyEvaluationsException printStackTrace

Introduction

In this page you can find the example usage for org.apache.commons.math3.exception TooManyEvaluationsException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:com.wwidesigner.optimization.ObjectiveFunctionOptimizer.java

/**
 * Use a specified optimizer type to optimize a specified objective
 * function./*from  w w w .j av  a2s. co  m*/
 * 
 * @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:edu.cmu.tetrad.regression.LogisticRegression2.java

public void regress(int[] target, int numValues, double[][] regressors) {
    try {/*from   ww w  . j a  va 2s . c o  m*/
        int numParams = regressors.length + 1;

        double[] coefficients = new double[(numValues - 1) * numParams];

        // Apparently this needs to be fairly loose.
        int tolerance = 250;
        MultivariateOptimizer search = new PowellOptimizer(tolerance, tolerance);

        PointValuePair pair = search.optimize(new InitialGuess(coefficients),
                new ObjectiveFunction(new FittingFunction(target, regressors)), GoalType.MAXIMIZE,
                new MaxEval(1000000));

        this.likelihood = pair.getValue();
    } catch (TooManyEvaluationsException e) {
        e.printStackTrace();
        this.likelihood = Double.NaN;
    }
}