Re: Optimizing a function with a list as a parameter
- To: mathgroup at smc.vnet.net
- Subject: [mg103091] Re: [mg103081] Optimizing a function with a list as a parameter
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Tue, 8 Sep 2009 05:55:08 -0400 (EDT)
- References: <200909070636.CAA03978@smc.vnet.net>
Hi Mike,
If I understand correctly what you want, I suggest to write your own
<maximize> function,
for example as follows:
In[1] =
ClearAll[maximize, var];
Protect[var];
maximize[f_, argNum_Integer?Positive, constraintsF_] :=
With[{vars = Array[var, {argNum}]},
Maximize[{f[vars], constraintsF @@ vars}, vars]];
The head <var> is used to index variables in the result. The first argument
is the function
you want to minimize (its name if it is DownValue - based, or entire thing
if it is
a pure function), the second is the length of the list passed to <f>, and
the last is a function
to generate a list of constraints.
Here is an example of a function with several maxima:
In[2] =
ClearAll[f];
f[x_List] := (x - 1)^4 - 5 (x - 2)^4 + (x - 3)^4;
We maximize it for different number of arguments and sets of constraints:
In[3] = maximize[f, 2, {} &]
Out[3] = {14,{var[1]->2-Sqrt[2],var[2]->2-Sqrt[2]}}
In[4] = maximize[f, 4, {#1 + #2 < 3} &]
Out[4] = {14, {var[1] -> 2 - Sqrt[2], var[2] -> 2 - Sqrt[2],
var[3] -> 2 - Sqrt[2], var[4] -> 2 - Sqrt[2]}}
In[5] = maximize[f, 4, {#1 + #2 > 3} &]
Out[5] =
{14,{var[1]->2-Sqrt[2],var[2]->2+Sqrt[2],var[3]->2-Sqrt[2],var[4]->2-Sqrt[2]}}
In[6] = maximize[f, 4, {#1 + #2 > 3, #3 - #4 > 2} &]
Out[6] = {14, {var[1] -> 2 - Sqrt[2], var[2] -> 2 + Sqrt[2],
var[3] -> 2 + Sqrt[2], var[4] -> 2 - Sqrt[2]}}
Hope this helps.
Regards,
Leonid
On Mon, Sep 7, 2009 at 10:36 AM, Mike Miller <michael.g.miller at gmail.com>wrote:
> I have a time-series like function which gives an output based on a
> list of real numbers(i.e. you can run the function on a list of
> 1,2,3,... real numbers). The issue I'm encountering is how to maximize
> the function for a given list length(subject to a set of constraints).
> I could fix the function to a fixed number of real numbers(i.e. f
> [x_Real, y_Real] instead of f[x_List]) and treat x and y as the first
> two elements of the list, and call Maximize on the function, but this
> is not really elegant. I want to be able to easily change the number
> of elements in the list.
>
> What is the best way to optimize a function like I described, which
> takes a list as an argument, for a fixed list length?
>
> Thanks,
> Mike
>
>
- References:
- Optimizing a function with a list as a parameter
- From: Mike Miller <michael.g.miller@gmail.com>
- Optimizing a function with a list as a parameter