# NMaximize is not converging to a solution

I am trying to use NMaximize to find the maximum value of a variable that satisfies the given constraints. Since the constraints aren't straightforward, I am using the function.

I can see the constraints are such that the value is bounded but I get the below warning messages:

NMaximize::cvmit: Failed to converge to the requested accuracy or precision within 100000 iterations.

NMaximize::cvdiv: Failed to converge to a solution. The function may be unbounded.

The constraint and the way I am using the function is as below:

    constraint = (x | y) \[Element]
Integers && ((x == 0 && 1. <= y <= 12720.) || (1. <= x <= 10712. &&
0 <= y <
2.08565*10^-36 (3.04959*10^39 + 2.24751*10^34 x) +
2.8484*10^-43 Sqrt[
4.98614*10^92 + 4.65469*10^88 x -
3.63201*10^84 x^2]) || (10713. <= x <= 19762. &&
2.08565*10^-36 (3.04959*10^39 + 2.24751*10^34 x) -
2.8484*10^-43 Sqrt[
4.98614*10^92 + 4.65469*10^88 x - 3.63201*10^84 x^2] < y <
2.08565*10^-36 (3.04959*10^39 + 2.24751*10^34 x) +
2.8484*10^-43 Sqrt[
4.98614*10^92 + 4.65469*10^88 x - 3.63201*10^84 x^2]))

maxX =
NMaximize[{x, constraint}, {x, y}, MaxIterations -> 100000]


I have increased the MaxIterations from 100 to 100000 but it doesn't seem to converge. I am not sure if increasing the MaxIterations is the solution. Can you please guide me with this?

• Could try maximizing over individual regions of the piecewise set-up. But the machine precision values will make validation of inequalities kind of iffy. – Daniel Lichtblau Apr 5 '19 at 17:44
• I'm not seeing what $y$ has to do with this. Wouldn't the maximum value of $x$ be 19762? constraint /. x -> 19762 results in y \[Element] Integers && 7229.16 < y < 7344.29 and constraint /. x -> 19763 results in False. – JimB Apr 5 '19 at 17:51
• @JimB, I think for x, y isn't needed. Thanks for pointing this out. But if I am trying to maximize y, I need to maximize over both the variables since y is an expression of x, right? – gaganso Apr 5 '19 at 18:06
• Yes, if that's what you want. The general solution appears to be $x = 19762$ and $7230\leq y \leq 7344$. So to maximize $y$ you'd choose $7344$. – JimB Apr 5 '19 at 18:49
• OK. I was assuming that you were conditioning on the maximum value of $x$. – JimB Apr 5 '19 at 18:57

## 2 Answers

Rationalize the constraint:

constraint2 = ((x == 0 && 1. <= y <= 12720.) || (1. <= x <= 10712. &&
0 <= y < 2.08565*10^-36 (3.04959*10^39 + 2.24751*10^34 x) +
2.8484*10^-1 Sqrt[
4.98614*10^8 + 4.65469*10^4 x - 3.63201 x^2]) || (10713. <= x <=
19762. &&
2.08565*10^-36 (3.04959*10^39 + 2.24751*10^34 x) -
2.8484*10^-1 Sqrt[4.98614*10^8 + 4.65469*10^4 x - 3.63201 x^2] < y <
2.08565*10^-36 (3.04959*10^39 + 2.24751*10^34 x) +
2.8484*10^-1 Sqrt[4.98614*10^8 + 4.65469*10^4 x - 3.63201 x^2])) //
Rationalize[#, 0] & // Simplify;


With the Rationalized constraint you can use Maximize:

maxX = Maximize[{x, constraint2}, {x, y}]

(* {19762, {x -> 19762, y -> 7287}} *)

constraint2 /. maxX[]

(* True *)


EDIT: To find maximum y

(maxY = Maximize[{y, constraint2}, {x, y}]) // N To plot the region defined by the constraint:

reg = ImplicitRegion[constraint2, {x, y}];

Region[reg,
Frame -> True,
FrameLabel -> (Style[#, 12, Bold] & /@ {x, y}),
Epilog -> {Red,
AbsolutePointSize,
Point[{x, y} /. maxX[]],
Point[{x, y} /. maxY[]]}] You have numbers spread a wide range of magnitudes for no good reason. This range is probably too wide for machine precision arithmetic. Also telling NMinimize explicitly that this an integer optimization problem seems to help. Try this:

constraint2 = ((x == 0 && 1. <= y <= 12720.) || (1. <= x <= 10712. &&
0 <= y <
2.08565*10^-36 (3.04959*10^39 + 2.24751*10^34 x) +
2.8484*10^-1 Sqrt[
4.98614*10^8 + 4.65469*10^4 x - 3.63201 x^2]) || (10713. <=
x <= 19762. &&
2.08565*10^-36 (3.04959*10^39 + 2.24751*10^34 x) -
2.8484*10^-1 Sqrt[
4.98614*10^8 + 4.65469*10^4 x - 3.63201 x^2] < y <
2.08565*10^-36 (3.04959*10^39 + 2.24751*10^34 x) +
2.8484*10^-1 Sqrt[
4.98614*10^8 + 4.65469*10^4 x - 3.63201 x^2])) // Expand

maxX = NMaximize[{x, constraint2}, {x, y}, Integers,
MaxIterations -> 10000]


{19762., {x -> 19762, y -> 7311}}

And with your definition of constraint:

constraint /. maxX[]


True

• But constraint /. x -> 19762 /. y -> 8647 results in False? – JimB Apr 5 '19 at 17:57
• @JimB D'oh. Yeah, I did the simplification wrong. -.- Thanks for pointing that out. – Henrik Schumacher Apr 5 '19 at 18:02
• @HenrikSchumacher, thank you for this. This works for x but when I try to find the maximum y similarly, I still get the same message - NMaximize[{y, res}, {x, y}, Integers, MaxIterations -> 100000]. Output: NMaximize::cvdiv: Failed to converge to a solution. The function may be unbounded. – gaganso Apr 5 '19 at 18:12