1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.optimization.direct;
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.util.Comparator;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.FunctionEvaluationException;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.optimization.OptimizationException;
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.optimization.RealConvergenceChecker;
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.optimization.RealPointValuePair;
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This class implements the multi-directional direct search method.
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1070725 $ $Date: 2011-02-15 02:31:12 +0100 (mar. 15 févr. 2011) $
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see NelderMead
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.2
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class MultiDirectional extends DirectSearchOptimizer {
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Expansion coefficient. */
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final double khi;
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Contraction coefficient. */
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final double gamma;
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Build a multi-directional optimizer with default coefficients.
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>The default values are 2.0 for khi and 0.5 for gamma.</p>
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public MultiDirectional() {
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.khi   = 2.0;
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.gamma = 0.5;
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Build a multi-directional optimizer with specified coefficients.
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param khi expansion coefficient
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param gamma contraction coefficient
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public MultiDirectional(final double khi, final double gamma) {
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.khi   = khi;
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.gamma = gamma;
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    protected void iterateSimplex(final Comparator<RealPointValuePair> comparator)
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException {
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final RealConvergenceChecker checker = getConvergenceChecker();
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        while (true) {
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            incrementIterationsCounter();
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // save the original vertex
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final RealPointValuePair[] original = simplex;
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final RealPointValuePair best = original[0];
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // perform a reflection step
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final RealPointValuePair reflected = evaluateNewSimplex(original, 1.0, comparator);
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (comparator.compare(reflected, best) < 0) {
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // compute the expanded simplex
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                final RealPointValuePair[] reflectedSimplex = simplex;
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                final RealPointValuePair expanded = evaluateNewSimplex(original, khi, comparator);
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (comparator.compare(reflected, expanded) <= 0) {
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    // accept the reflected simplex
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    simplex = reflectedSimplex;
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return;
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // compute the contracted simplex
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final RealPointValuePair contracted = evaluateNewSimplex(original, gamma, comparator);
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (comparator.compare(contracted, best) < 0) {
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // accept the contracted simplex
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return;
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // check convergence
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int iter = getIterations();
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            boolean converged = true;
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int i = 0; i < simplex.length; ++i) {
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                converged &= checker.converged(iter, original[i], simplex[i]);
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (converged) {
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return;
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Compute and evaluate a new simplex.
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param original original simplex (to be preserved)
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param coeff linear coefficient
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param comparator comparator to use to sort simplex vertices from best to poorest
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return best point in the transformed simplex
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception FunctionEvaluationException if the function cannot be evaluated at some point
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception OptimizationException if the maximal number of evaluations is exceeded
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private RealPointValuePair evaluateNewSimplex(final RealPointValuePair[] original,
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                              final double coeff,
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                              final Comparator<RealPointValuePair> comparator)
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws FunctionEvaluationException, OptimizationException {
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double[] xSmallest = original[0].getPointRef();
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final int n = xSmallest.length;
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // create the linearly transformed simplex
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        simplex = new RealPointValuePair[n + 1];
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        simplex[0] = original[0];
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 1; i <= n; ++i) {
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double[] xOriginal    = original[i].getPointRef();
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double[] xTransformed = new double[n];
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int j = 0; j < n; ++j) {
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            simplex[i] = new RealPointValuePair(xTransformed, Double.NaN, false);
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // evaluate it
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        evaluateSimplex(comparator);
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return simplex[0];
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
145