Skip to main content

broken code - Why Min cannot be used as constrains of NMinimize?



I met a strange problem when trying to do an optimization.


Initialization code,


goal = 2.;
vector = {1.0, 1.1, 1.2, 1.3, 1.4, 1.5};

f[m1_Real, m2_Real] := Block[{v},
v = Map[# + m1*Sin[Norm[{m1, m2}]] &, vector];
Clip[v, {0, goal}]];

g[m1_Real, m2_Real] := Max[(goal - f[m1, m2])/goal] + Norm[{m1, m2}];


where f is a vector function and g is a scaler function. I want to minimize g[m1,m2] with constrains 0.8 <= Min[f[m1,m2]] <= 1.0 and 0 <= m1 <= 2 && 0 <= m2 <=2. Despite this seems straightforward, a direct implementation is problematic,


counter = 0;
NMinimize[{
g[m1, m2],
And[0.8 <= Min[f[m1, m2]]/goal <= 1.0, 0 <= m1 <= 2, 0 <= m2 <= 2]
}, {m1, m2},
Method -> {"NelderMead", "Tolerance" -> .001},
StepMonitor :> (Print[++counter, ":\t", {m1, m2}])]


If we execute the code, an error message NMinimize::bcons will be shown, which tells us that the constrains are not in the valid format.


After a few tests I found out that the the problem is related to the command Min used in the first constrain 0.8 <= Min[f[m1, m2]]/goal <= 1.0. If we do not use Min then there will be no problem, i.e., 0.8 <= Evaluate[Norm[f[m1, m2]/goal]] <= 1.0 can be evaluated without any problem.


So it seems that Min is not valid for using as constrains with NMizimize (yet we can use Norm, Plus, etc.). But this is really inconvenient because I do need to use Min as part of the constrains. I wonder if we can fix this problem?



Answer



To solve your problem, let's first take a good look at the warning generated by your code:



NMinimize::bcons: The following constraints are not valid:
{0 <= m1, 0 <= m2, 0.8 <= 0.5 f[m1, m2], m1 <= 2, m2 <= 2, 0.5 f[m1, m2] <= 1.}.
Constraints should be equalities, inequalities, or domain
specifications involving the variables. >>


What have you found? Our constraints are not valid, yeah, we know that, but what else? Oh, we see the constraints have been displayed by Mathematica and the form of them have been slightly changed, um, that's understandable, and we can easily find the correspondence between them and their original, 0 <= m1 and 0 <= m2 and m1 <= 2 and m2 <= 2 are for 0 <= m1 <= 2 and 0 <= m2 <= 2, 0.8 <= 0.5 f[m1, m2] and 0.5 f[m1, m2] <= 1. are…… wait, where's the Min? Where did it go? Mathematica don't like it so she deleted it?


Of course not, Min disappeared because it has been calculated before f[m1, m2] become number lists. What happened here is just same as:


Clear[m1, m2]
Min@f[m1, m2]


f[m1, m2]

This can be approved by Trace:



NMinimize[{g[m1, m2], 
And[0.8 <= Min@f[m1, m2]/goal <= 1.0, 0 <= m1 <= 2, 0 <= m2 <= 2]},
{m1, m2},
Method -> {"NelderMead", "Tolerance" -> .001}] // Trace


{{{0.8 <= Min[f[m1, m2]]/goal <= 1. && 0 <= m1 <= 2 && 
0 <= m2 <= 2, {{{Min[f[m1, m2]], f[m1, m2]}…………

To fix your code, you can definite a new function that calculates only if its argument is a list:



min[x_List] := Min[x]

counter = 0;
(* I removed your Method option since it seems to be unnecessary. *)
NMinimize[{g[m1, m2],
And[0.8 <= min@f[m1, m2]/goal <= 1.0, 0 <= m1 <= 2, 0 <= m2 <= 2]},
{m1, m2},
StepMonitor :> (Print[++counter, ":\t", {m1, m2}])]



 {1.02036, {m1 -> 0.820357, m2 -> 2.34313*10^-8}}

Comments

Popular posts from this blog

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...