使用Mathematica进行优化:在目标函数中使用BinCounts

Mic*_*erf 5 wolfram-mathematica mathematical-optimization

使用Mathematica,我需要优化一个定义的函数BinCounts; 我希望最大化的参数定义bin分割点.

我认为问题在于Mathematica在给出数值之前根据参数扩展了目标函数,因此BinCounts抱怨bin规范不是"包含实数值,无限和-Infinity的列表".

我认为以下是我正在尝试做的事情以及正在发生的事情的最小例子.我非常感谢有关如何解决这个问题的建议.

In[1]:= data = RandomReal[1, 30]; (* Make some test data. *)

In[2]:= f[a_, b_, c_] := BinCounts[data, {{0, a, b, c, 1}}] (* Shorthand to use below… *)

In[12]:= g[a_, b_, c_] := Max[f[a, b, c]] - Min[f[a, b, c]] (* Objective function. *)

In[13]:= NMaximize[{g[a, b, c], 0 < a < b < c < 1}, {a, b, c}] (* Try to oprimize. *)

During evaluation of In[13]:= BinCounts::cvals: The bin specification {{0,a,b,c,1}} is not a list containing real values, Infinity, and -Infinity. >>

During evaluation of In[13]:= BinCounts::cvals: The bin specification {{0,a,b,c,1}} is not a list containing real values, Infinity, and -Infinity. >>

During evaluation of In[13]:= BinCounts::cvals: The bin specification {{0,a,b,c,1}} is not a list containing real values, Infinity, and -Infinity. >>

During evaluation of In[13]:= General::stop: Further output of BinCounts::cvals will be suppressed during this calculation. >>

Out[13]= {0., {a -> 0., b -> 0., c -> 1.}}
Run Code Online (Sandbox Code Playgroud)

Mic*_*erf 3

解决方案只是指定目标函数仅根据数值参数定义,如下所示:

g[a_?NumericQ, b_?NumericQ, c_?NumericQ] := Max[f[a, b, c]] - Min[f[a, b, c]]
Run Code Online (Sandbox Code Playgroud)