# Bilevel optimization

I found a problem here

How do we go about solving this in Mathematica? I did something like this:

NMinimize[(x^2 + y^2 - 1)^2, {x, y}]


this gives {3.08149*10^-33, {x -> 0.865404, y -> 0.501074}}

And used this y -> 0.501074 and did this:

NMinimize[
x^2 + 0.1*Cos[4 Pi x] + y^2 + 0.1*Sin[4 Pi y] /.
y -> 0.5010739136683062, x]


which gave

{0.207832, {x -> -0.221351}}

Edit: Actually, I had tried this method and meant to post it today but got late:

F[x_, y_] := x^2 + 0.1*Cos[4 Pi x] + y^2 + 0.1*Sin[4 Pi y];
g[x_, y_] := (x^2 + y^2 - 1)^2;

NMinimize[{F[x, g[x, y]], Derivative[0, 1][g][x, y] == 0}, {x, y}]


I got {0.390615, {x -> -0.627901, y -> -0.778293}}

If plot this then I get: This doesn't make sense

• Does this answer your question? Combined numerical minimization and maximization Commented Feb 16, 2022 at 16:51
• (1) This has come up several times on MSE. See this or this other for examples. Also the one referenced in my "close as duplicate" vote, if this gets closed. Commented Feb 16, 2022 at 16:53
• (2) Don't get me wrong, this is a reasonable type of inquiry. But it has several answers already. Commented Feb 16, 2022 at 16:54
• Thanks @DanielLichtblau for all the links. Actually, I had tried one of the methods that you mentioned and I just updated my post with an edit. I didn't get a sensible answer. May I am understanding the problem incorrectly.
– sra
Commented Feb 16, 2022 at 20:47

I don't think this is a first, but I rarely answer a question I voted to close. In this case I found the wording of the problem to be convoluted (at least for me) so I opted to take a stab at it.

We need a function to do the inner optimization.

innerMin[x_?NumericQ] :=
First[FindArgMin[{(x^2 + z^2 - 1)^2, 0 <= z <= 1}, {z, 1/2}]]


The actual objective function uses the inner optimization.

objFunc[x_] :=
With[{y = innerMin[x]}, x^2 + 0.1*Cos[4 Pi x] + y^2 + 0.1*Sin[4 Pi y]]


Let's see what we get.

NMinimize[{objFunc[x], 0 <= x <= 1}, x]

(* Out[6]= {0.858145, {x -> 0.270149}} *)

F[x_, y_] = x^2 + 1/10*Cos[4 Pi x] + y^2 + 1/10*Sin[4 Pi y]

g[x_, y_] = (x^2 + y^2 - 1)

nmin = NMinimize[{F[x, y], g[x, y] == 0, 0 <= x <= 1,
0 <= y <= 1}, {x, y}, Method -> "DifferentialEvolution"]

Show[Plot3D[F[x, y], {x, 0, 1}, {y, 0, 1}, PlotRange -> All,
RegionFunction -> Function[{x, y}, -.01 < g[x, y] < .01],
PlotPoints -> 100],
Graphics3D[{Red,
Sphere[{x, y, F[x, y]} /. nmin[[2]], .03]}]]

sol = Flatten@
Solve[{g[x, y] == 0, 0 <= x <= 1, 0 <= y <= 1}, {x, y}, Reals]

(min = Minimize[{F[x, Sqrt[1 - x^2]], 0 <= x <= 1}, x, Reals])

Plot[F[x, Sqrt[1 - x^2]], {x, 0, 1},
GridLines -> {{x /. min[[2]]}, None}]

• Isn't the definition of g[x_,y_] incorrect?
– sra
Commented Feb 18, 2022 at 0:43
• y == ArgMin[{(x^2 + z^2 - 1)^2, 0 < z < 1 && 0 < x < 1}, z, Reals] // FullSimplify[#, 0 < x < 1] &  yields Sqrt[1 - x^2] == y  and sol = Flatten@ Solve[{g[x, y] == 0, 0 <= x <= 1, 0 <= y <= 1}, {x, y}, Reals]  yields {y -> ConditionalExpression[Sqrt[1 - x^2], 0 <= x <= 1]} ` Commented Feb 18, 2022 at 6:49