Java Code Examples for org.apache.commons.math.optimization.RealPointValuePair#getValue()

The following examples show how to use org.apache.commons.math.optimization.RealPointValuePair#getValue() . 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: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxIterations(100);
    multiDirectional.setMaxEvaluations(1000);

    final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);

    RealPointValuePair estimate = multiDirectional.optimize(function,
                                  GoalType.MAXIMIZE, function.getMaximumPosition());

    final double EPSILON = 1e-5;

    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );

}
 
Example 2
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxIterations(100);
    multiDirectional.setMaxEvaluations(1000);

    final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);

    RealPointValuePair estimate = multiDirectional.optimize(function,
                                  GoalType.MAXIMIZE, function.getMaximumPosition());

    final double EPSILON = 1e-5;

    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );

}
 
Example 3
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxEvaluations(1000);

    final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);

    RealPointValuePair estimate = multiDirectional.optimize(function,
                                  GoalType.MAXIMIZE, function.getMaximumPosition());

    final double EPSILON = 1e-5;

    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );

}
 
Example 4
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxIterations(100);
    multiDirectional.setMaxEvaluations(1000);

    final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);

    RealPointValuePair estimate = multiDirectional.optimize(function,
                                  GoalType.MAXIMIZE, function.getMaximumPosition());

    final double EPSILON = 1e-5;

    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );
    
}
 
Example 5
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxIterations(100);
    multiDirectional.setMaxEvaluations(1000);

    final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);

    RealPointValuePair estimate = multiDirectional.optimize(function,
                                  GoalType.MAXIMIZE, function.getMaximumPosition());

    final double EPSILON = 1e-5;

    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );

}
 
Example 6
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxIterations(100);
    multiDirectional.setMaxEvaluations(1000);

    final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);

    RealPointValuePair estimate = multiDirectional.optimize(function,
                                  GoalType.MAXIMIZE, function.getMaximumPosition());

    final double EPSILON = 1e-5;

    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );

}
 
Example 7
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxIterations(100);
    multiDirectional.setMaxEvaluations(1000);

    final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);

    RealPointValuePair estimate = multiDirectional.optimize(function,
                                  GoalType.MAXIMIZE, function.getMaximumPosition());

    final double EPSILON = 1e-5;

    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );

}
 
Example 8
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283()
    throws FunctionEvaluationException, OptimizationException {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    MultiDirectional multiDirectional = new MultiDirectional();
    multiDirectional.setMaxIterations(100);
    multiDirectional.setMaxEvaluations(1000);

    final Gaussian2D function = new Gaussian2D(0.0, 0.0, 1.0);

    RealPointValuePair estimate = multiDirectional.optimize(function,
                                  GoalType.MAXIMIZE, function.getMaximumPosition());

    final double EPSILON = 1e-5;

    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );

}
 
Example 9
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 10
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 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 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 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 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 17
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 18
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 19
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 20
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;
    }
}