# NMinimize::nnum: The function value [...] is not a number at [...]

I have been trying to minimize a function, but I believe a syntax error is blocking the evaluation. I tried to add NumericQ to trigger the evaluation of the probability vector p, but the piecewise cost function (trivially defined here) does not evaluate to a number and I don't know why.

This code is reproducible, albite clunky as I wanted to preserve the (now useless) variables defined as closely as possible to my actual syntax.

Reproduce[v_: {{1,1},{1,2},{2,1},{2,2}},targeted_:{0.8,0.2}]:=
Module[{g,m,n,mse,piecewiseMSE, num,objective},
g = Length[First[v]];
m = Length[targeted];
n = Length[v];
mse[p__?NumericQ] :=Total[p];
piecewiseMSE[p__?NumericQ] :=Piecewise[{
{10^9, p \[VectorLessEqual] ConstantArray[0,g]},
{10^9, p \[VectorGreaterEqual] ConstantArray[1,g]}
}, mse[p] ];

objective[p_?NumericQ]:= piecewiseMSE[p];
NMinimize[objective[p],  p \[Element] Vectors[g,Reals]]
]
Reproduce[]


It produces an error NMinimize::nnum: The function value piecewiseMSE\$92320[{0.918621,0.716689}] is not a number at {Subscript[p, 1],Subscript[p, 2]} = {0.918621,0.716689}.

Use VectorQ with the second argument NumericQ:

Reproduce[v_ : {{1, 1}, {1, 2}, {2, 1}, {2, 2}},
targeted_ : {0.8, 0.2}] :=
Module[{g, m, n, mse, piecewiseMSE, num, objective},
g = Length[First[v]];
m = Length[targeted];
n = Length[v];
mse[p_ /; VectorQ[p, NumericQ]] := Total[p];
piecewiseMSE[p_ /; VectorQ[p, NumericQ]] :=
Piecewise[{{10^9,
p \[VectorLessEqual] ConstantArray[0, g]}, {10^9,
p \[VectorGreaterEqual] ConstantArray[1, g]}}, mse[p]];
objective[p_ /; VectorQ[p, NumericQ]] := piecewiseMSE[p];
NMinimize[objective[p], p \[Element] Vectors[g, Reals]]];