# Inner parameters are not updated in the nested optimization

I have a problem with nested optimization. In my program, I select one from the list minipotentX that can maximize the value of the function pieRestMini. Then, I use this selected one as one of the inputs of another function piPlatMini. Parameters \[Theta] and m are the inputs for the function optMiniX and the piPlatMini. The code is as follows:

fOne[q_] :=
Piecewise[{{q*1/15, 0 \[LessSlantEqual] q < 3}, {1/5 - (q - 3)*1/35,
3 \[LessSlantEqual] q \[LessSlantEqual] 10}}, 0];

lambdaTwo[x_] :=
Piecewise[{{(4*(1 -
4/45*Integrate[q*fOne[q], {q, x, 10}, Assumptions -> x \[Element] Reals && 0 \[LessSlantEqual] x \[LessSlantEqual] 10]) - 4/45*Integrate[q^2*fOne[q], {q, x, 10},
Assumptions -> x \[Element] Reals && 0 \[LessSlantEqual] x \[LessSlantEqual] 10])*6/295,
0 \[LessSlantEqual] x < 4.18}, {6/235, 4.18 \[LessSlantEqual] x \[LessSlantEqual]
6.83}, {(2*1*(1 - 4/45*Integrate[q*fOne[q], {q, x, 10}, Assumptions -> x \[Element] Reals &&
0 \[LessSlantEqual] x \[LessSlantEqual] 10]) - 4/45*Integrate[q^2*fOne[q], {q, x, 10},
Assumptions -> x \[Element] Reals && 0 \[LessSlantEqual] x \[LessSlantEqual] 10])*6/235,
6.83 < x \[LessSlantEqual] 10}}, 0];

Remove[optMiniX]
optMiniX[\[Theta]_, m_] :=
Module[{minipotentX, xFive, xSix, optconsX, pieRestMini},
pieRestMini[x_] := lambdaTwo[x]*5 + 4/45*Integrate[(q - Max[\[Theta]*q, m])*fOne[q], {q, x, 10},
Assumptions -> x \[Element] Reals && 0 \[LessSlantEqual] x \[LessSlantEqual] 10];
xFive = Values[Solve[x^2 - x*35/6 + 4*m + m*35/6 == 0, x][[1]][[1]]];
xSix = Values[Solve[x^2 - x*35/6 + 2*m + m*35/6 == 0, x][[1]][[1]]];
minipotentX = Select[{xFive, 4.18, xSix, 6.83, 10, m}, # \[Element] Reals &];
optconsX = minipotentX[[Ordering[Table[pieRestMini[i], {i, minipotentX}], -1][[1]]]];
optconsX
];

piPlatMini[y_, \[Theta]_ /; NumberQ[\[Theta]], m_ /; NumberQ[m]] :=
4/45*Integrate[(Max[\[Theta]*q, m] + 1/5)*fOne[q], {q, y, 10},
Assumptions -> y \[Element] Reals && 0 \[LessSlantEqual] y \[LessSlantEqual] 10 &&
m \[Element] Reals &&  0 \[LessSlantEqual] m \[LessSlantEqual] 10 && 0 <= \[Theta] <= 1 && \[Theta] \[Element] Reals];


The problem occurs in the maximization process. When I directly do the nested maximization,

re=NMaximize[{piPlatMini[optMiniX[\[Theta], m], \[Theta], m], \[Theta] \[Element] Reals && 0 <= \[Theta] < 1 && m \[Element] Reals && 0 <= m < 10}, {\[Theta], m}]


the result is as follows:

{0.130157, {\[Theta] -> 0.999933, m -> 10.}}


When I directly put the optimal solution into the function optMiniX:

optMiniX[\[Theta], m]/. re[[2]]


the result is different

Root[78825 - 3760 #^2 - 376 #^3 + 47 #^4& , 1, 0]


But the analytical result should be 10!

I think Mathematica only updates \[Theta] and m in the outer function and doesn't update \[Theta] and m in the inner function. The folloing evidence is got when I use Maximize function:

In

Maximize[{piPlatMini[optMiniX[\[Theta], m], \[Theta],
m], \[Theta] \[Element] Reals && 0 <= \[Theta] <= 1 &&
m \[Element] Reals && 0 <= m <= 10}, {\[Theta], m}]


Out

Maximize[{piPlatMini[Root[
78825 - 3760 #^2 - 376 #^3 + 47 #^4& , 1, 0], \[Theta],
m], \[Theta] \[Element] Reals && 0 <= \[Theta] <= 1 &&
m \[Element] Reals && 0 <= m <= 10}, {\[Theta], m}]


I wonder how to solve this problem. Thanks in advance!

• The output that is printed on the screen is abbreviated. You must use all digits of the answer. E,g.:re = NMaximize[{piPlatMini[optMiniX[θ, m], θ, m], θ ∈ Reals && 0 <= θ < 1 && m ∈ Reals && 0 <= m < 10}, {θ, m}]; piPlatMini[optMiniX[θ, m], θ, m] /. re[[2]] then you get the correct result. – Daniel Huber Oct 21 '20 at 14:43
• Thanks for your reply. I have revised the code according to your advice. But I still find that the result is not correct. The value of optMiniX under the solution is not correct. And when I use Maximize function, the value of theta and m in the function is fixed. – Haobo Yu Oct 22 '20 at 8:34