Java Code Examples for org.apache.commons.math.optimization.GoalType#MINIMIZE

The following examples show how to use org.apache.commons.math.optimization.GoalType#MINIMIZE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Math_38_BOBYQAOptimizer_s.java    From coming with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() {
    final double[] lowerBound = getLowerBound();
    final double[] upperBound = getUpperBound();

    // Validity checks.
    setup(lowerBound, upperBound);

    isMinimize = (getGoalType() == GoalType.MINIMIZE);
    currentBest = new ArrayRealVector(getStartPoint());

    final double value = bobyqa(lowerBound, upperBound);

    return new RealPointValuePair(currentBest.getDataRef(),
                                  isMinimize ? value : -value);
}
 
Example 2
Source File: Math_38_BOBYQAOptimizer_t.java    From coming with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() {
    final double[] lowerBound = getLowerBound();
    final double[] upperBound = getUpperBound();

    // Validity checks.
    setup(lowerBound, upperBound);

    isMinimize = (getGoalType() == GoalType.MINIMIZE);
    currentBest = new ArrayRealVector(getStartPoint());

    final double value = bobyqa(lowerBound, upperBound);

    return new RealPointValuePair(currentBest.getDataRef(),
                                  isMinimize ? value : -value);
}
 
Example 3
Source File: BOBYQAOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() {
    // Validity checks.
    setup();

    isMinimize = (getGoalType() == GoalType.MINIMIZE);
    currentBest = new ArrayRealVector(getStartPoint());

    final double value = bobyqa();

    return new RealPointValuePair(currentBest.getDataRef(),
                                  isMinimize ? value : -value);
}
 
Example 4
Source File: BOBYQAOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() {
    // Validity checks.
    setup();

    isMinimize = (getGoalType() == GoalType.MINIMIZE);
    currentBest = new ArrayRealVector(getStartPoint());

    final double value = bobyqa();

    return new RealPointValuePair(currentBest.getDataRef(),
                                  isMinimize ? value : -value);
}
 
Example 5
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction function,
                                   final GoalType goalType,
                                   final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = function;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 6
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction function,
                                   final GoalType goalType,
                                   final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = function;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 7
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction f,
                                     final GoalType goalType,
                                     final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = f;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 8
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction f,
                                     final GoalType goalType,
                                     final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = f;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 9
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction f,
                                     final GoalType goalType,
                                     final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = f;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 10
Source File: SimplexOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() throws MathUserException {
    if (simplex == null) {
        throw new NullArgumentException();
    }

    // Indirect call to "computeObjectiveValue" in order to update the
    // evaluations counter.
    final MultivariateRealFunction evalFunc
        = new MultivariateRealFunction() {
            public double value(double[] point) throws MathUserException {
                return computeObjectiveValue(point);
            }
        };

    final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
    final Comparator<RealPointValuePair> comparator
        = new Comparator<RealPointValuePair>() {
        public int compare(final RealPointValuePair o1,
                           final RealPointValuePair o2) {
            final double v1 = o1.getValue();
            final double v2 = o2.getValue();
            return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1);
        }
    };

    // Initialize search.
    simplex.build(getStartPoint());
    simplex.evaluate(evalFunc, comparator);

    RealPointValuePair[] previous = null;
    int iteration = 0;
    final ConvergenceChecker<RealPointValuePair> checker = getConvergenceChecker();
    while (true) {
        if (iteration > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.getSize(); i++) {
                RealPointValuePair prev = previous[i];
                converged &= checker.converged(iteration, prev, simplex.getPoint(i));
            }
            if (converged) {
                // We have found an optimum.
                return simplex.getPoint(0);
            }
        }

        // We still need to search.
        previous = simplex.getPoints();
        simplex.iterate(evalFunc, comparator);
        ++iteration;
    }
}
 
Example 11
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction function,
                                   final GoalType goalType,
                                   final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = function;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 12
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction function,
                                   final GoalType goalType,
                                   final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = function;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 13
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction function,
                                   final GoalType goalType,
                                   final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = function;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 14
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction function,
                                   final GoalType goalType,
                                   final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = function;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 15
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction function,
                                   final GoalType goalType,
                                   final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = function;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 16
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction function,
                                   final GoalType goalType,
                                   final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = function;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}
 
Example 17
Source File: SimplexOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() throws MathUserException {
    if (simplex == null) {
        throw new NullArgumentException();
    }

    // Indirect call to "computeObjectiveValue" in order to update the
    // evaluations counter.
    final MultivariateRealFunction evalFunc
        = new MultivariateRealFunction() {
            public double value(double[] point) throws MathUserException {
                return computeObjectiveValue(point);
            }
        };

    final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
    final Comparator<RealPointValuePair> comparator
        = new Comparator<RealPointValuePair>() {
        public int compare(final RealPointValuePair o1,
                           final RealPointValuePair o2) {
            final double v1 = o1.getValue();
            final double v2 = o2.getValue();
            return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1);
        }
    };

    // Initialize search.
    simplex.build(getStartPoint());
    simplex.evaluate(evalFunc, comparator);

    RealPointValuePair[] previous = null;
    int iteration = 0;
    final ConvergenceChecker<RealPointValuePair> checker = getConvergenceChecker();
    while (true) {
        if (iteration > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.getSize(); i++) {
                RealPointValuePair prev = previous[i];
                converged &= checker.converged(iteration, prev, simplex.getPoint(i));
            }
            if (converged) {
                // We have found an optimum.
                return simplex.getPoint(0);
            }
        }

        // We still need to search.
        previous = simplex.getPoints();
        simplex.iterate(evalFunc, comparator);
        ++iteration;
    }
}
 
Example 18
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
protected RealPointValuePair doOptimize()
    throws FunctionEvaluationException {

    final double[] startPoint = getStartPoint();
    if ((startConfiguration == null) ||
        (startConfiguration.length != startPoint.length)) {
        // No initial configuration has been set up for simplex
        // build a default one from a unit hypercube.
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }
    
    final boolean isMinim = (getGoalType() == GoalType.MINIMIZE);
    final Comparator<RealPointValuePair> comparator
        = new Comparator<RealPointValuePair>() {
        public int compare(final RealPointValuePair o1,
                           final RealPointValuePair o2) {
            final double v1 = o1.getValue();
            final double v2 = o2.getValue();
            return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1);
        }
    };

    // Initialize search.
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    int iteration = 0;
    final ConvergenceChecker<RealPointValuePair> checker = getConvergenceChecker();
    while (true) {
        if (iteration > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iteration, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // We still need to search.
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);
        ++iteration;
    }
}
 
Example 19
Source File: SimplexOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() throws MathUserException {
    if (simplex == null) {
        throw new NullArgumentException();
    }

    // Indirect call to "computeObjectiveValue" in order to update the
    // evaluations counter.
    final MultivariateRealFunction evalFunc
        = new MultivariateRealFunction() {
            public double value(double[] point) throws MathUserException {
                return computeObjectiveValue(point);
            }
        };

    final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
    final Comparator<RealPointValuePair> comparator
        = new Comparator<RealPointValuePair>() {
        public int compare(final RealPointValuePair o1,
                           final RealPointValuePair o2) {
            final double v1 = o1.getValue();
            final double v2 = o2.getValue();
            return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1);
        }
    };

    // Initialize search.
    simplex.build(getStartPoint());
    simplex.evaluate(evalFunc, comparator);

    RealPointValuePair[] previous = null;
    int iteration = 0;
    final ConvergenceChecker<RealPointValuePair> checker = getConvergenceChecker();
    while (true) {
        if (iteration > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.getSize(); i++) {
                @SuppressWarnings("null") // Cannot be null when iteration > 0
                RealPointValuePair prev = previous[i];
                converged &= checker.converged(iteration, prev, simplex.getPoint(i));
            }
            if (converged) {
                // We have found an optimum.
                return simplex.getPoint(0);
            }
        }

        // We still need to search.
        previous = simplex.getPoints();
        simplex.iterate(evalFunc, comparator);
        ++iteration;
    }
}
 
Example 20
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public RealPointValuePair optimize(final MultivariateRealFunction function,
                                   final GoalType goalType,
                                   final double[] startPoint)
    throws FunctionEvaluationException, OptimizationException,
    IllegalArgumentException {

    if (startConfiguration == null) {
        // no initial configuration has been set up for simplex
        // build a default one from a unit hypercube
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }

    this.f = function;
    final Comparator<RealPointValuePair> comparator =
        new Comparator<RealPointValuePair>() {
            public int compare(final RealPointValuePair o1,
                               final RealPointValuePair o2) {
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goalType == GoalType.MINIMIZE) ?
                        Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        };

    // initialize search
    iterations  = 0;
    evaluations = 0;
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    while (true) {

        if (iterations > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iterations, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // we still need to search
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);

    }

}