NelderMeadSimplex.Minimum() throws exception

Hi all,
I’ve got the following code:

// F, I, Precision and MaxEvaluations are defined elsewhere
	var Function = ObjectiveFunction.Value(new Func<Vector<double>, double>(z => F(z.AsArray())));
	var InitialGuess = new DenseVector(I);
	var result = NelderMeadSimplex.Minimum(Function, InitialGuess, Precision, MaxEvaluations);
catch (MaximumIterationsException)

In case the Minimum function throws a MaximumIterationsException, is there any way to retrieve any information about the simplex result or last guess?

I would try to capture and examine the exception, but I suspect that won’t help.

If all else fails, modify your function with a statement to assign the current z value to a local variable, say zLast.

LIke this:

var zLast = Vector.Build.Dense(1);
var Function = ObjectiveFunction.Value(new Func<Vector, double>(z => { zLast = z; return myPoly(z); } ));
var InitialGuess = I;
var result = NelderMeadSimplex.Minimum(Function, InitialGuess, 0.01, 10);
catch (MaximumIterationsException MIE)
Console.WriteLine("Last solution: " + zLast.ToString());
Console.WriteLine("Last value: " + myPoly(zLast).ToString());
Note: “myPoly” is a function of a single variable which implies minor difference from your situation.

1 Like

Thanks, that works.

Having looked at the source code, I don’t understand why the Minimum function needs to throw at all. Why does it not just return a MinimizationResult where reasonForExit is set to the already defined ExitCondition.ExceedIterations. Does anyone know of a good reason?