# Symbolically Minimizing Expression with Multiple Piecewise Functions

Following up my previous question about ArgMin, I now face a situation where the solution to an ArgMin call, where the output is rewritten / partly solved.

The code leading to the problem is very long and messy and I hope that someone has encountered this before and can tell me under what circumstances this happens in general without needing the specific example?

Update 1 As requested, some sample code. I do not know of a shorter, more synthesized version of the problem, unfortunately.

Remove["Global*"]
$Assumptions = _Symbol \[Element] Reals; f[x_, u_] := x + u v[x_, w_] := (x - w)^2 u[x_, b1_, b2_, f_, v_] := Module[{x1}, ArgMin[(x1 = f[x, uMin]; v[x1, b1] + v[x1, b2]), uMin, Reals]] uConstrained[x_, b1_, b2_, f_, v_] := Module[{x1}, ArgMin[{(x1 = f[x, uMin]; v[x1, b1] + v[x1, b2]), -1 <= uMin <= 1}, uMin, Reals]] p[x_, b1_, b2_, f_, u_, v_] := v[f[x, u[x, b1, b2, f, v]], b2] c[b11_, b12_, f_, u_, v_, p_, up_] := Module[{u0, x1, u1, x2}, ( u0 = FullSimplify[u[x0, b11, b21, f, v]]; x1 = FullSimplify[f[x0, u0]]; u1 = FullSimplify[u[x1, b12, b22, f, v]]; x2 = FullSimplify[f[x1, u1]]; FullSimplify[v[x1, w1]] + FullSimplify[p[x0, b11, b21, f, up, v]] + FullSimplify[v[x2, w2]] + FullSimplify[p[x1, b12, b22, f, up, v]])] ArgMin[FullySimplify[c[b11, b12, f, uConstrained, v, p, u]], {b11, b12}, Reals]  • Please provide a concrete, preferably short example of the problem at hand. – kirma Apr 6 '18 at 18:14 • @kirma see update – Xaser Apr 6 '18 at 18:53 • So apparently if Mathematica outputs the input, it means that it doesn't know what to do or cannot solve it. However I'm having trouble to extract the properties of the above example, that make the problem unsolvable for mathematica – Xaser Apr 6 '18 at 22:39 • In your definition of c[...] local variables x0, b21, b22, w1 are undefined – ulvi Apr 6 '18 at 23:07 • @Xaser Yes. Unevaluated input usually means that with supplied input and definitions of variables (and, for instance, $Assumptions) in the session don't allow Mathematica to find the result. Often an unevaluated result can be still useful later if further information is provided later on and the returned, unevaluated result is re-evaluated with it. – kirma Apr 7 '18 at 4:05

Edit, taking advantage of improvements to the code in the question and using PiecewiseExpand in the answer.

The quantity to be minimized, c[b1, b2, f, uConstrained, v, p, u], contains two distinct Piecewise functions, one with three regions in {x0, b21, b22, b1, b2} space, and the other with five regions. ({w1, w2} do not appear in the range definitions.) It can be converted into a single Piecewise function of ten expressions and regions, to each of which ArgMin can be applied in turn. Then, finding the minimum of the ten minimums would, in principle, give the desired result.

onepw = PiecewiseExpand[c[b1, b2, f, uConstrained, v, p, u]] // FullSimplify

(* Piecewise[
{{1/2 (b1^2 + b2^2 + b21^2 + b22^2) - (b1 + b21) w1 + w1^2 - (b2 + b22) w2 + w2^2,
-2 < b1 - b2 + b21 - b22 <= 2 && -2 < b1 + b21 - 2 x0 <= 2},
{1/4 ((b1 - b21)^2 + (b2 - b22)^2 + (b1 + b21 - 2 w1)^2 + (b1 + b21 - 2(1 + w2))^2),
-2 < b1 + b21 - 2 x0 <= 2 && ((2 + b1 + b21 > b2 + b22 && 4 + b2 + b22 < 2 x0) ||
b1 + b21 > 2 + b2 + b22)},
{1/4 ((b1 - b21)^2 + (b2 - b22)^2 + (b1 + b21 - 2 w1)^2 + (2 + b1 + b21 - 2 w2)^2),
2 + b1 + b21 <= b2 + b22 && -2 < b1 + b21 - 2 x0 <= 2},
{1/4 ((b1 - b21)^2 + (b2 - b22)^2 + (b2 + b22 - 2 w2)^2 + 4 (1 - w1 + x0)^2),
b1 + b21 > 2 + 2 x0 && 0 < b2 + b22 - 2 x0 <= 4},
{1/4 (b1 - b21)^2 + 1/4 (b2 - b22)^2 + (w2 - x0)^2 + (1 - w1 + x0)^2,
b1 + b21 > 2 + 2 x0 && b2 + b22 <= 2 x0},
{1/4 (b1 - b21)^2 + 1/4 (b2 - b22)^2 + (1 - w1 + x0)^2 + (2 - w2 + x0)^2,
b1 + b21 > 2 + 2 x0 && b2 + b22 > 4 + 2 x0},
{1/4 ((b1 - b21)^2 + (b2 - b22)^2 + (b2 + b22 - 2 w2)^2 + 4 (1 + w1 - x0)^2),
(-4 < b2 + b22 - 2 x0 <= 0 && 2 + b1 + b21 <= 2 x0) || (0 < b2 + b22 - 2 x0 <= 4 &&
b1 + b21 > 2 + 2 x0) || (-2 < b1 + b21 - 2 x0 <= 2 &&
-2 < b1 - b2 + b21 - b22 <= 2)},
{1/4 (b1 - b21)^2 + 1/4 (b2 - b22)^2 + (1 + w1 - x0)^2 + (w2 - x0)^2,
(-2 < b1 + b21 - 2 x0 <= 2 && 2 + b1 + b21 <= b2 + b22) || (b1 + b21 > 2 + 2 x0 &&
b2 + b22 <= 2 x0) || (2 + b1 + b21 <= 2 x0 && b2 + b22 > 2 x0)},
{1/4 (b1 - b21)^2 + 1/4 (b2 - b22)^2 + (1 + w1 - x0)^2 + (2 + w2 - x0)^2,
(-2 < b1 + b21 - 2 x0 <= 2 && ((2 + b1 + b21 > b2 + b22 && 4 + b2 + b22 < 2 x0) ||
b1 + b21 > 2 + b2 + b22)) || (2 + b1 + b21 <= 2 x0 && 4 + b2 + b22 <= 2 x0) ||
(b1 + b21 > 2 + 2 x0 && b2 + b22 <= 2 x0)}},
1/4 ((b1 - b21)^2 + (b2 - b22)^2 + (b2 + b22 - 2 (2 + w2))^2 + 4 (1 + w1 - x0)^2)] *)


Unfortunately, minimizing the first of these ten expressions,

ArgMin[onepw[[1, 1]], {b1, b2}]


produces a Piecewise answer with a LeafCount of 830699 (in about 15 minutes), and Simplify runs for hours (twenty-one before I terminated that computation) without returning a result. Presumably, minimizing the other nine expressions produces similarly enormous results. The reason for the enormous results returned by ArgMin is that the regions over which any of the ten expressions is valid are complex shapes in the five-dimensional space of parameters and solutions, and those shapes change with the values of the parameters. So, there are an enormous number of case to be considered.

A more practical approach is to define the function,

sol[x0t_, w1t_, w2t_, b21t_, b22t_] :=
ArgMin[Simplify[c[b1, b2, f, uConstrained, v, p, u],
x0 == x0t && w1 == w1t && w2 == w2t && b21 == b21t && b22 == b22t],
{b1, b2}, Reals]


which usually can produce the answer for a given set of parameters in several seconds. For instance,

sol[5/2, E, Pi, -7, 1.11]
(* {-7., 3.14159} *)


or

sol[19, 70, -71, 86, 19]
(* {86, 19} *)


Interestingly,

Count[Table[param = RandomInteger[{-300, 300}, 5];
param[[4 ;; 5]] == sol @@ param, 100], True]


suggests that {b1, b2} == {b21, b22} is the answer roughly 90% of the time.

• wow, that's some dedication. Thank you! also made me smile a bit there. I'm not entirely sure why the c function generates such an enormous leafcount or why there are so many cases but I'll evaluate that again tomorrow on a piece of paper. I'm not not familiar with the following functions. Unevaluated, Union, Flatten so I will have to invest some time to understand what you did there. I also started to implement a numerical version in MATLAB in parallel, so I'll see how that turns out. I guess the answer to my question then is: the problem is too complicated to Mathematica to solve. – Xaser Apr 7 '18 at 21:32
• btw, LeafCount[FullSimplify[c[b1, b2, f, uConstrained, v, p, u]]] outputs 329 for me, after adding the universal assumption "everything real numbers" – Xaser Apr 7 '18 at 21:38
• @Xaser Unevaluated[Piecewise[z1_, z2_]  tells Mathematica not to try to evaluate Piecewise but only do the substitution. Otherwise, Mathematica spits out a string of error messages and only then gives the right answer. Flatten eliminates extra pairs of curly brackets, and Union eliminates duplicates from a List, among other things. I may not be familiar with the universal assumption "everything real numbers". Where is its documentation? The additional simplification might be important in getting a better answer for your interesting problem. – bbgodfrey Apr 7 '18 at 22:00
• It's just something I found on google while trying to get rid of specifying it for all Simplify calls separately. simply add the line \$Assumptions = _Symbol \[Element] Reals; – Xaser Apr 7 '18 at 22:29
• @Xaser Not necessarily. Whether the result from minimizing eq` is even relevant depends on the values of the parameters, – bbgodfrey Apr 8 '18 at 17:37