0

I am new with Mathematica, so forgive me if I completely misunderstood the nature of the problem...

Problem description

I want to optimize a fairly complex function bypassing explicit derivative computations. So I tried to use FindMinimum with the PrincipalAxis method, giving as parameters the two first points to evaluate (note: mathematica should be able to guess from the parameters that I am asking for PrincipalAxis, but never to careful).

The problem is that the parameters of the function to be optimized are probabilities, so I wrote a lots of assertions across my code checking for the definition domain of the variables. For some reason, FindMinimum breaks these assertions, and from what I could gather from the error messages it actually does symbolic black magic - even if I don't really want it to do so.

Minimal Reproducible Example

A way to reproduce my problem would be the following:

This works:

FindMinimum[
{Total[{x,y,z}],0<x<1&&0<y<1&&0<z<1 },
{{x,0.5,0.6},{y,0.5,0.6},{z,0.5,0.6}},
Method->"PrincipalAxis"]

This does not:

blackbox[x_,y_,z_] :=
Module[{total},
Assert[0<x<1 &&0<y<1&&0<z<1];
total = Total[{x,y,z}];
total]

FindMinimum[ {blackbox[x,y,z],0<x<1&&0<y<1&&0<z<1 }, {{x,0.5,0.6},{y,0.5,0.6},{z,0.5,0.6}}, Method->"PrincipalAxis"]

Error Message: Assert :Assertion test 0 < x < 1 && 0 < y < 1 && 0 < z < 1 evaluated to 0 < x < 1 && 0 < y < 1 && 0 < z < 1 that is neither True nor False.

Anyway to circumvent this problem without disabling all the Asserts that I had to put in the first place?

Edit 1 accounting for ?NumericQ

After following the advice given in comment, I used ?NumericQ and also changed the optimization method to avoid "PrincipalAxis can only be used for unconstrained problems". It "works", but the Asserts still throw, because (supposedly) FindMinimum is trying to explore values outside of these constraints.

ClearAll[blackbox]
blackbox[x_?NumericQ,y_?NumericQ,z_?NumericQ] :=
Module[{total},
Assert[0<x<1 &&0<y<1&&0<z<1];
total = Total[{x,y,z}];
total]

blackbox[0.1,0.2,0.3]

FindMinimum[ {blackbox[x,y,z],0<x<1&&0<y<1&&0<z<1 }, {{x,0.5},{y,0.5},{z,0.5}}]

To which the output is:

Assert::asrtf: Assertion 0<1.85432*10^-6<1&&0<-4.20114*10^-6<1&&0<1.85432*10^-6<1 failed.
{2.42736*10^-7,{x->8.09122*10^-8,y->8.09122*10^-8,z->8.09122*10^-8}} 
```
WaterFox
  • 185
  • 6
  • 1
    Are there some brackets missing in blackbox? Otherwise Total[...] is not executed. – Daniel Huber Feb 23 '23 at 20:35
  • oops sorry you're right, I edited the function - I wanted to shorten the code but my mathematica syntax is still too shaky lol. The problem persists though. – WaterFox Feb 23 '23 at 20:40
  • 1
  • I was not able to get it work with NumericQ :/ – WaterFox Feb 23 '23 at 21:44
  • 1
    What version are you using? In V13.1, I get a result from FindMinimum. (Note the docs say, "Currently, the only method available for constrained optimization is the interior point algorithm.". http://reference.wolfram.com/language/tutorial/ConstrainedOptimizationLocalNumerical.html) – Michael E2 Feb 24 '23 at 03:45

0 Answers0