summaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/math/optimization/direct/NelderMead.java
blob: 8b8b20591dab8fd74e29e306453c5ba4590dded1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.math.optimization.direct;

import java.util.Comparator;

import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.RealPointValuePair;

/**
 * This class implements the Nelder-Mead direct search method.
 *
 * @version $Revision: 1070725 $ $Date: 2011-02-15 02:31:12 +0100 (mar. 15 févr. 2011) $
 * @see MultiDirectional
 * @since 1.2
 */
public class NelderMead extends DirectSearchOptimizer {

    /** Reflection coefficient. */
    private final double rho;

    /** Expansion coefficient. */
    private final double khi;

    /** Contraction coefficient. */
    private final double gamma;

    /** Shrinkage coefficient. */
    private final double sigma;

    /** Build a Nelder-Mead optimizer with default coefficients.
     * <p>The default coefficients are 1.0 for rho, 2.0 for khi and 0.5
     * for both gamma and sigma.</p>
     */
    public NelderMead() {
        this.rho   = 1.0;
        this.khi   = 2.0;
        this.gamma = 0.5;
        this.sigma = 0.5;
    }

    /** Build a Nelder-Mead optimizer with specified coefficients.
     * @param rho reflection coefficient
     * @param khi expansion coefficient
     * @param gamma contraction coefficient
     * @param sigma shrinkage coefficient
     */
    public NelderMead(final double rho, final double khi,
                      final double gamma, final double sigma) {
        this.rho   = rho;
        this.khi   = khi;
        this.gamma = gamma;
        this.sigma = sigma;
    }

    /** {@inheritDoc} */
    @Override
    protected void iterateSimplex(final Comparator<RealPointValuePair> comparator)
        throws FunctionEvaluationException, OptimizationException {

        incrementIterationsCounter();

        // the simplex has n+1 point if dimension is n
        final int n = simplex.length - 1;

        // interesting values
        final RealPointValuePair best       = simplex[0];
        final RealPointValuePair secondBest = simplex[n-1];
        final RealPointValuePair worst      = simplex[n];
        final double[] xWorst = worst.getPointRef();

        // compute the centroid of the best vertices
        // (dismissing the worst point at index n)
        final double[] centroid = new double[n];
        for (int i = 0; i < n; ++i) {
            final double[] x = simplex[i].getPointRef();
            for (int j = 0; j < n; ++j) {
                centroid[j] += x[j];
            }
        }
        final double scaling = 1.0 / n;
        for (int j = 0; j < n; ++j) {
            centroid[j] *= scaling;
        }

        // compute the reflection point
        final double[] xR = new double[n];
        for (int j = 0; j < n; ++j) {
            xR[j] = centroid[j] + rho * (centroid[j] - xWorst[j]);
        }
        final RealPointValuePair reflected = new RealPointValuePair(xR, evaluate(xR), false);

        if ((comparator.compare(best, reflected) <= 0) &&
            (comparator.compare(reflected, secondBest) < 0)) {

            // accept the reflected point
            replaceWorstPoint(reflected, comparator);

        } else if (comparator.compare(reflected, best) < 0) {

            // compute the expansion point
            final double[] xE = new double[n];
            for (int j = 0; j < n; ++j) {
                xE[j] = centroid[j] + khi * (xR[j] - centroid[j]);
            }
            final RealPointValuePair expanded = new RealPointValuePair(xE, evaluate(xE), false);

            if (comparator.compare(expanded, reflected) < 0) {
                // accept the expansion point
                replaceWorstPoint(expanded, comparator);
            } else {
                // accept the reflected point
                replaceWorstPoint(reflected, comparator);
            }

        } else {

            if (comparator.compare(reflected, worst) < 0) {

                // perform an outside contraction
                final double[] xC = new double[n];
                for (int j = 0; j < n; ++j) {
                    xC[j] = centroid[j] + gamma * (xR[j] - centroid[j]);
                }
                final RealPointValuePair outContracted = new RealPointValuePair(xC, evaluate(xC), false);

                if (comparator.compare(outContracted, reflected) <= 0) {
                    // accept the contraction point
                    replaceWorstPoint(outContracted, comparator);
                    return;
                }

            } else {

                // perform an inside contraction
                final double[] xC = new double[n];
                for (int j = 0; j < n; ++j) {
                    xC[j] = centroid[j] - gamma * (centroid[j] - xWorst[j]);
                }
                final RealPointValuePair inContracted = new RealPointValuePair(xC, evaluate(xC), false);

                if (comparator.compare(inContracted, worst) < 0) {
                    // accept the contraction point
                    replaceWorstPoint(inContracted, comparator);
                    return;
                }

            }

            // perform a shrink
            final double[] xSmallest = simplex[0].getPointRef();
            for (int i = 1; i < simplex.length; ++i) {
                final double[] x = simplex[i].getPoint();
                for (int j = 0; j < n; ++j) {
                    x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]);
                }
                simplex[i] = new RealPointValuePair(x, Double.NaN, false);
            }
            evaluateSimplex(comparator);

        }

    }

}