2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.math.optimization;
21
* This class implements the Nelder-Mead direct search method.
23
* @version $Revision: 620312 $ $Date: 2008-02-10 12:28:59 -0700 (Sun, 10 Feb 2008) $
24
* @see MultiDirectional
27
public class NelderMead
28
extends DirectSearchOptimizer {
30
/** Build a Nelder-Mead optimizer with default coefficients.
31
* <p>The default coefficients are 1.0 for rho, 2.0 for khi and 0.5
32
* for both gamma and sigma.</p>
42
/** Build a Nelder-Mead optimizer with specified coefficients.
43
* @param rho reflection coefficient
44
* @param khi expansion coefficient
45
* @param gamma contraction coefficient
46
* @param sigma shrinkage coefficient
48
public NelderMead(double rho, double khi, double gamma, double sigma) {
56
/** Compute the next simplex of the algorithm.
57
* @exception CostException if the function cannot be evaluated at
60
protected void iterateSimplex()
61
throws CostException {
63
// the simplex has n+1 point if dimension is n
64
int n = simplex.length - 1;
67
double smallest = simplex[0].getCost();
68
double secondLargest = simplex[n-1].getCost();
69
double largest = simplex[n].getCost();
70
double[] xLargest = simplex[n].getPoint();
72
// compute the centroid of the best vertices
73
// (dismissing the worst point at index n)
74
double[] centroid = new double[n];
75
for (int i = 0; i < n; ++i) {
76
double[] x = simplex[i].getPoint();
77
for (int j = 0; j < n; ++j) {
81
double scaling = 1.0 / n;
82
for (int j = 0; j < n; ++j) {
83
centroid[j] *= scaling;
86
// compute the reflection point
87
double[] xR = new double[n];
88
for (int j = 0; j < n; ++j) {
89
xR[j] = centroid[j] + rho * (centroid[j] - xLargest[j]);
91
double costR = evaluateCost(xR);
93
if ((smallest <= costR) && (costR < secondLargest)) {
95
// accept the reflected point
96
replaceWorstPoint(new PointCostPair(xR, costR));
98
} else if (costR < smallest) {
100
// compute the expansion point
101
double[] xE = new double[n];
102
for (int j = 0; j < n; ++j) {
103
xE[j] = centroid[j] + khi * (xR[j] - centroid[j]);
105
double costE = evaluateCost(xE);
108
// accept the expansion point
109
replaceWorstPoint(new PointCostPair(xE, costE));
111
// accept the reflected point
112
replaceWorstPoint(new PointCostPair(xR, costR));
117
if (costR < largest) {
119
// perform an outside contraction
120
double[] xC = new double[n];
121
for (int j = 0; j < n; ++j) {
122
xC[j] = centroid[j] + gamma * (xR[j] - centroid[j]);
124
double costC = evaluateCost(xC);
126
if (costC <= costR) {
127
// accept the contraction point
128
replaceWorstPoint(new PointCostPair(xC, costC));
134
// perform an inside contraction
135
double[] xC = new double[n];
136
for (int j = 0; j < n; ++j) {
137
xC[j] = centroid[j] - gamma * (centroid[j] - xLargest[j]);
139
double costC = evaluateCost(xC);
141
if (costC < largest) {
142
// accept the contraction point
143
replaceWorstPoint(new PointCostPair(xC, costC));
150
double[] xSmallest = simplex[0].getPoint();
151
for (int i = 1; i < simplex.length; ++i) {
152
double[] x = simplex[i].getPoint();
153
for (int j = 0; j < n; ++j) {
154
x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]);
156
simplex[i] = new PointCostPair(x, Double.NaN);
164
/** Reflection coefficient. */
167
/** Expansion coefficient. */
170
/** Contraction coefficient. */
171
private double gamma;
173
/** Shrinkage coefficient. */
174
private double sigma;