## mathematical optimization – Stopping condition for FindMinimum / FindMaximum

This seems like it should be a simple question — but I am looking to use a “home made” stopping condition with FindMaximum, while evaluating a very complex function.

Printing out the successive changes in the results for my problem (which cannot be boiled down to a quick code sample) I see that the relative differences in about 100 params are on the order of 10^-3 between steps (and often much smaller) which is more than enough for this application — but it just continues to dance around the “right” answer until I hit MaxIterations, and adjusting Precision and Accuracy does not seem to help.

Q: Is there a way to implement a “stopping condition” option for these functions?

## mathematical optimization – FindMaximum works on desktop but not on laptop

I have a simple code to find maximum as follows.

``````myfunc = {-1 + 2/(1 + d), 1/2 (-1 + 1/d), 1 - d, 1 + 1/(-2 + d),
1 - d, 1 - d, -1 + 1/d, -1 + 1/d, 1/(1 + d), 1/(2 d), 1/d, 1/d, d/(
1 - d)};
FindMaximum({##, 0 <= d <= 1}, d) & /@ myfunc
``````

It works well on my desktop and the result is:

``````{{1., {d -> 0.}}, {(Infinity), {d -> 0.}}, {1., {d ->
0.}}, {0.5, {d -> 0.}}, {1., {d -> 0.}}, {1., {d ->
0.}}, {(Infinity), {d -> 0.}}, {(Infinity), {d ->
0.}}, {1., {d -> 0.}}, {(Infinity), {d ->
0.}}, {(Infinity), {d -> 0.}}, {(Infinity), {d ->
0.}}, {(Infinity), {d -> 1.}}}
``````

However, I got error with `\$Failed` when I ran it on my laptop.

``````{{1., {d -> 3.54538*10^-8}}, {-$$Failed, {d -> 0.}}, {1., {d -> 0.}}, {0.5, {d -> 0.}}, {1., {d -> 0.}}, {1., {d -> 0.}}, {-Failed, {d -> 0.}}, {-$$Failed, {d -> 0.}}, {1., {d ->
0.}}, {(Infinity), {d -> Indeterminate}}, {(Infinity), {d ->
0.}}, {(Infinity), {d -> 0.}}, {(Infinity), {d ->
Indeterminate}}}
``````

Why does this happen? How can I solve this?

## mathematical optimization – FindMaximum does not use requested Working Precision

I think that there is a bug in the way FindMaximum handles WorkingPrecision for constrained problems.
Has anyone encountered a similar problem before? Is there a work around?

Here is an example in which the bug occurs:

``````fun(x_?NumberQ) := (Print("x = ", x, "  Precision = ", Precision(x)) ; 1 - x^4 ) ;

FindMaximum( {fun(x),-1<=x<= 1} , {x,0.5`64} , AccuracyGoal->32 , PrecisionGoal->32 , WorkingPrecision -> 64 )
``````

I define a simple function fun that computes the function 1-x^4.
The function fun is only evaluated on numbers. (See below.)
It also prints x and its precision before returning the value.

Then, I use FindMaximum to find the maximum of fun in the interval -1<=x<=1. I request FindMaximum to do it with a working precision of 64.

However, all the evaluations of fun are done with Machine precision,
which should not be the case. Here is the beginning of the output:

x = 0.5 Precision = MachinePrecision

x = 0.499994 Precision = MachinePrecision

x = 0.500006 Precision = MachinePrecision

If I do the same with an unconstrained problem, e.g.,

``````FindMaximum( fun(x) , {x,-0.5`64} , AccuracyGoal -> 32 , PrecisionGoal -> 32 , WorkingPrecision->64 )
``````

Then things work correctly. The beginning of the output is:

x = -0.5000000000000000000000000000000000000000000000000000000000000000 Precision = 64.

x = -0.5000000000000000000000000000000000000000000000000000000000000000 Precision = 64.

x = -0.4999999999999999999999999999999899999999999999624920275479230684 Precision = 64.

This is a stripped down version of a more complicated problem I am
encountering. In the more complicated example fun(x) represents a
function that cannot be differentiated symbolically. Thus,
fun(x_?NumberQ) in the definition is used to prevent symbolic evaluation
and differentiation. I still want FindMaximum to use the specified precision.

I am using version:
12.0.0 for Microsoft Windows (64-bit) (April 6, 2019)

Any ideas?

## solving equations – FindMaximum of a function of 2 variables

I'm trying to figure out how Mathematica find the local maxima of a function of two variables, but I'm a bit lost here … For example, I give the command:

m = FindMaximum [{Sin [x] Sin [2
y], {x, y} [Element] Rectangle [{0,0}, {4,4}]}, {x, y}]

and what i get is this:

{1., {X-> 1.5708, y-> 0.785398}}

which is not bad, but when I plot the function, I see that there are more local maximum points to consider, as the following graph shows:

Display [ContourPlot [Sin [x] Sin [2]
y], {x, 0.4}, {y, 0.4}], Graphics [{Red, PointSize [Large], Point [{x, y} /. Last [m]]}]]

Why doesn't Mathematica provide us with all the local maxima, at least in the bounded domain? Do I have to do something wrong? Thank you.

I have also tried to find the other local maximum points with the command:

Solve [Sin [x] Sin [2y] == 1, {x, y}]

but it doesn't help …

## mathematical optimization – FindMaximum returns a solution that does not respect the constraints

I'm looking at the problem of optimization next

begin {align *} max & 1000 r_1 + frac {1} {2} r_2 + frac {1} {3} r_3 \ text {s.t. } & 1000 ^ 2 r_1 + frac {1} {4} r_2 + frac {1} {9} r_3 = frac {1} {9}, \ & 1000 ^ 2 p_1 + frac {1} {4} p_2 + frac {1} {9} p_3 = frac {1} {9}, \ & r_1 + r_2 + r_3 + r_4 leq 2 \ & p_1 + p_2 + p_3 + p_4 = 2 \ & 0 leq p_i leq 1, quad i = 1, points, 4 \ & r_i ^ 2 leq p_i, quad i = 1, dots, 4 end {align *}
with the following Mathematica code (it is clear that $$r_1 = p_1 = 0$$, $$r_2 = p_2 = 0$$, $$r_3 = p_3 = 1$$, $$r_4 = p_4 = 1$$ is a feasible solution so I chose it as the initial solution)

``````FindMaximum({1000 r1 + 1/2 r2 + 1/3 r3,
{1000^2 r1 + 1/4 r2 + 1/9 r3 == 1/9,
r1 + r2 + r3 + r4 == 2,
r1^2 <= p1, r2^2 <= p2, r3^2 <= p3, r4^2 <= p4,
1000^2 p1 + 1/4 p2 + 1/9 p3 == 1/9,
p1 + p2 + p3 + p4 == 2,
0 <= p1 <= 1, 0 <= p2 <= 1, 0 <= p3 <= 1, 0 <= p4 <= 1}
},
{{r1, 0}, {r2, 0}, {r3, 1}, {r4, 1}, {p1, 0}, {p2, 0}, {p3, 1}, {p4, 1}}, AccuracyGoal -> 10)
``````

``````{0.45521, {r1 -> -6.30073*10^-8, r2 -> 0.268328, r3 -> 0.963328,
r4 -> 0.768345, p1 -> 0., p2 -> 0.0719999, p3 -> 0.928, p4 -> 1.}}
``````

If you look at the constraint $$1000 ^ 2 p_1 + frac {1} {4} p_2 + frac {1} {9} p_3 = frac {1} {9}$$you can see that in the solution, Mathematica returns,
the left side is evaluated to $$0,1211 cdots$$, so the constraint is not satisfied.

Is it a bug or just a digital inaccuracy? Just the value seems too big compared to $$1 / 9$$ – the additive error is not at the scale of $$10 ^ {- 4}$$ or $$10 ^ {- 5}$$.

## maximum – Maximize and FindMaximum drive me crazy

I have to do a digital study.
As part of it, I have to solve:

``````A = 1 * 10 ^ 6; b = 1/4; delta = 0;
K[x_] = 0.9772498680518208` +
x (1.186104449210508` * ^ - 10 +
x (-1.4649246311182838` * ^ - 19 +
x (3.0214683712038235` * ^ - 28 +
x (-2.6422357409090615` * ^ - 37 +
x (-5.8010340178270696` * ^ - 46 +
x (-2.6339819094686574` * ^ - 55 +
x (-3.7463204038553128` * ^ - 65 +
(6.191585769652652` * ^ - 77 + 6,879539744126434` * ^ - 87x) x))))))) //
FullSimplify;

F[x_] = By pieces[{{0-10^10<x[{{0-10^10<x[{{0-10^10=
x> = -2.5 * 10 ^ 9}, {1, .2 * 10 ^ 9 <x}}];
H1[a_, i_, j_] : =
NMaximize[{(A - a p) (p - w) +         (1 -
F[-(A - a p) (w - i)]) (A - a j) ^ 2 / (4 a) +
F[-(A - a p) (w - i)] ((A - a i) ^ 2 / (8 a) - b (A - a p) (p - w)),
i <= w <= A / a, 0 <p <= A / a}, {w, p}]H2[a_, i_, j_] : =
Maximize[{(A - a p) (p - w) +         (1 -
F[-(A - a p) (w - i)]) (A - a j) ^ 2 / (4 a) +
F[-(A - a p) (w - i)] ((A - a i) ^ 2 / (8 a) - b (A - a p) (p - w)),
i <= w <= A / a, 0 <p <= A / a}, {w, p}]H3[a_, i_, j_] : =
FindMaximum[{(A - a p) (p - w) +         (1 -
F[-(A - a p) (w - i)]) (A - a j) ^ 2 / (4 a) +
F[-(A - a p) (w - i)] ((A - a i) ^ 2 / (8 a) - b (A - a p) (p - w)),
i <= w <= A / a, 0 <p <= A / a}, {w, p}]H1[50, 5000, 6500]
H2[50, 5000, 6500]
H3[50, 5000, 6500]

H1[50, 5000, 5000]
H2[50, 5000, 5000]
H3[50, 5000, 5000]
``````

Sry for long functions, but maybe it is important to identify the error.

When I run this code, the first two H1 and H2 find in the first case a maximum less than H3, in the second a higher maximum. I would always like to have the "true", that is to say the highest maximum.

No suggestion?

Best regards
Andreas