Re: Nesting NMaximize

• To: mathgroup at smc.vnet.net
• Subject: [mg60417] Re: Nesting NMaximize
• From: dh <dh at metrohm.ch>
• Date: Fri, 16 Sep 2005 03:48:56 -0400 (EDT)
• References: <dg8lln\$rci\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hello Derek,
You try to do:
NMaximize[ NMaximize[fun[x,y],{x}], {y}]

Now you have to understand how Matematica calculates this expression.
Remember, that if a function does not have the attribute HoldAll or
HoldFirst, arguments are evaluated before delivered to a function.
Therefore, Mathematica first tries to evaluate
NMaximize[fun[x,y],{x}]
This fails, because at this point y has no value.

To Ssolve the problem, you must ensure that NMaximize is not called with
undetermined paramaters. This can be done by an intermediate function
that is only called if the argument is numeric.

f1[y_Real]:=NMaximize[fun[x,y],{x}][[1]];
NMaximize[f1[y],{y}]

In this case f1 can not be evaluated before fed to NMAximize because the
argument is not numeric. Further, f1 changes the output from NMaximize
from a list to a scalar.
From the above you will get the value of ymax. An additional NMaximize
with this value will get you xmax.

sincerely, Daniel

Derek James Gurney wrote:
> Summary :
>   I think I want to nest one NMaximize problem within another, but I can't get Mathematica to do it.
>
> Details, in English and Pseudo-code:
>       I have a problem where one agent is choosing p to maximize f[p,Delta gamma,other parameters], taking Delta gamma as given, and then another agent choses Delta gamma to maximize WA[OptimumP(Delta gamma),Delta gamma, other parameters], where OptimumP(gamma) is the 1st agent's optimal choice of p.
>
> f and g cannot be solved analytically, so I need NMaximize, and when I specify other parameters and Delta gamma, Mathematica will solve NMaximize[f,{p}]
>
> However, when I try the equivalent of the pseudo-code below, Mathematica gives an error:
>
> OptimumP := NMaximize[f,{p}]
> Set parameters except Delta gamma
> NMaximize[WA /. OptimumP,{Delta gamma}]
>
> Mathematica error:
>
>     "The function value  is not a number at "  p = Mathematica's initial value for p
>
>
> I recognize that the problem is that Mathematica is trying to solve OptimumP without a value for Delta gamma before it solves the outer NMaximize problem.  How do I tell Mathematica to do the reverse: try to solve the outer NMaximize first and use the guesses for Delta gamma to solve for OptimumP?
>
>
> I've included the actual code I'm using below.  Thanks for any help.
>
> Derek
>
>
>
>
> \!\(Clear["\<Global`*\>"]\ \ \[IndentingNewLine]
>   \(Qmax\  := \ M;\)\[IndentingNewLine]
>   \(shareA1\  := \ 0;\)\[IndentingNewLine]
>   \(M1\  := \ 0;\)\[IndentingNewLine]
>   \(M2\  := \ M;\)\[IndentingNewLine]
>   \(M3\  := \ M;\)\[IndentingNewLine]
>   \(RA1\  := \ 0;\)\[IndentingNewLine]
>   \(qAj1\  := \ M1\ shareA1;\)\[IndentingNewLine]
>   \(\[CapitalDelta]RA2\  := \ \ \((\[Gamma] - \[CapitalDelta]\[Gamma])\)\ \
> qAj1/Qmax;\)\[IndentingNewLine]
>   \(RA2\  := \ RA1\  + \ \[CapitalDelta]RA2;\)\[IndentingNewLine]
>   \(RU2\  := \ 0;\)\[IndentingNewLine]\[IndentingNewLine]
>   \(shareA2 := \ \ \[ExponentialE]\^\(\(-\[Alpha]\)\ PA2 + \[Beta]\ \((1 - \
> RA2)\) + \[Xi]A2\)\/\(\[ExponentialE]\^\(\(-\[Alpha]\)\ PU2 + \[Beta]\ \((1 - \
> RU2)\) + \[Xi]U2\) + \[ExponentialE]\^\(\(-\[Alpha]\)\ PA2 + \[Beta]\ \((1 - \
> RA2)\) + \[Xi]A2\)\);\)\[IndentingNewLine]\[IndentingNewLine]
>   \(qAj2\  := \ shareA2\ M2;\)\[IndentingNewLine]
>   \(revAj2\  := PA2\ qAj2;\)\[IndentingNewLine]
>   \(varcostAj2\  := \ c\ qAj2;\)\[IndentingNewLine]
>   \(profitAj2\  := revAj2 - varcostAj2\  - oF;\)\[IndentingNewLine]
>   \(profitA2\  := \ n\ profitAj2;\)\[IndentingNewLine]\[IndentingNewLine]
>   \(CSA2 := \
>       M \((2 + \ \(-\[Alpha]\)\ PA2 + \[Beta]\ \((1 -
>                   RA2)\) + \ \[Xi]A2)\);\)\[IndentingNewLine]
>   \(WA2 := \ CSA2 + profitA2;\)\[IndentingNewLine]
>   \(\[CapitalDelta]RA3\  := \ \((\[Gamma] - \[CapitalDelta]\[Gamma])\)\ \
> \((qAj2\  + \ \((n - 1)\) qJ2)\)/Qmax;\)\[IndentingNewLine] (*\
>     have\ to\ adjust\ for\ non - monopoly*) \[IndentingNewLine]
>   \(RA3\  := \ RA2\  + \ \[CapitalDelta]RA3;\)\[IndentingNewLine]
>   \(RB3\  := \ 0;\)\[IndentingNewLine]
>   \(RU3\  := \ 0;\)\[IndentingNewLine]\[IndentingNewLine]
>   \(shareA3 := \ \ \[ExponentialE]\^\(\(-\[Alpha]\)\ PA3 + \[Beta]\ \((1 - \
> RA3)\) + \[Xi]A3\)/\((\[ExponentialE]\^\(\(-\[Alpha]\)\ PU3 + \[Beta]\ \((1 - \
> RU3)\) + \[Xi]U3\) + \[ExponentialE]\^\(\(\(-\[Alpha]\)\ PA3\)\(+\)\(\[Beta] \
> \((1 - \ RA3)\)\)\(+\)\(\[Xi]A3\)\(\ \)\) +
>             x\ \[ExponentialE]\^\(\(\(-\[Alpha]\)\ PB\)\(+\)\(\[Beta]\ \((1 - \
> RB)\)\)\(+\)\(\[Xi]B\)\(\ \)\))\);\)\[IndentingNewLine]
>   \(qAj3\  := \ shareA3\ M3;\)\[IndentingNewLine]
>   \(revAj3\  := PA3\ qAj3;\)\[IndentingNewLine]
>   \(varcostAj3\  := \ c\ qAj3;\)\[IndentingNewLine]
>   \(profitAj3\  := revAj3 - varcostAj3 - oF;\)\[IndentingNewLine]
>   \(profitA3\  := \ n\ profitAj3;\)\[IndentingNewLine]
>   \(CSA3 := \
>       M \((\(-\[Alpha]\)\ PA3 + \[Beta]\ \((1 -
>                   RA3)\) + \ \[Xi]A3)\);\)\[IndentingNewLine]
>   \(WA3 := \ CSA3 + profitA3;\)\[IndentingNewLine]
>   \(WA := \ WA2\  + \ m\[Beta]\ WA3;\)\[IndentingNewLine]
>   \(profitAj\  := \
>       profitAj2\  + \
>         m\[Beta]\ profitAj3;\)\[IndentingNewLine]\[IndentingNewLine]\
> \[IndentingNewLine]\[IndentingNewLine]
>   \(PrevCost\  := c1\ \[CapitalDelta]\[Gamma]^c2;\)\[IndentingNewLine]
>   \(ProfitMaxed\  := \
>       NMaximize[
>         profitAj, {PA2, PA3}];\)\[IndentingNewLine]\[IndentingNewLine]
>   \(mPA2opt\  := \ \(ProfitMaxed[\([2]\)]\)[\([1]\)];\)\[IndentingNewLine]
>   \(mPA3opt\  := \ \(ProfitMaxed[\([2]\)]\)[\([2]\)];\)\[IndentingNewLine]\
> \[IndentingNewLine]\[IndentingNewLine]
>   "\<---------------------------------------------------------\>"\
> \[IndentingNewLine]\[IndentingNewLine] (*\
>     Central\ Case\ *) \[IndentingNewLine]
>   \[Alpha] = 1; b = 2; c = 0.1; oF\  = \ 0.1; R1\  = \ 0; m\[Beta]\  = \ 1;
>   o\[Beta]\  = \ 1; \[Gamma]dflt = 0.5;\[IndentingNewLine]\[IndentingNewLine]
>   \(\[Gamma]\  = \ \[Gamma]dflt;\)\[IndentingNewLine]
>   \(\[CapitalDelta]\[Gamma]\  = 0;\)\[IndentingNewLine]
>   \(n\  = 1;\)\[IndentingNewLine]\[IndentingNewLine]\[IndentingNewLine]
>   \(\[Beta]\  = \ 1;\)\[IndentingNewLine]
>   \(M\  = \ 1000;\)\[IndentingNewLine]
>   \(\[Xi]A2\  = \ 0;\)\[IndentingNewLine]
>   \(\[Xi]A3\  = \ 0;\)\[IndentingNewLine]
>   \(PB\  = \ 10;\)\[IndentingNewLine]
>   \(RB = \ 0;\)\[IndentingNewLine]
>   \(\[Xi]B\  = \ 0;\)\[IndentingNewLine]
>   \(RU = \ 0;\)\[IndentingNewLine]
>   \(PU2\  = \ 100;\)\[IndentingNewLine]
>   \(PU3\  = \ 100;\)\[IndentingNewLine]
>   \(\[Xi]U2\  = \ 0;\)\[IndentingNewLine]
>   \(\[Xi]U3\  = \ 0;\)\[IndentingNewLine]\[IndentingNewLine]
>   \(x = 0;\)\[IndentingNewLine]\[IndentingNewLine]
>   \(c2\  = 2;\)\[IndentingNewLine]
>   \(c1\  =
>       5000/c2;\)\[IndentingNewLine]\[IndentingNewLine]\[IndentingNewLine]
>   \(WA\  /. \ mPA3opt\)\  /. mPA2opt\[IndentingNewLine]
>   mPA3opt\[IndentingNewLine]
>   \(\[CapitalDelta]\[Gamma]\  =. ;\)\[IndentingNewLine]
>   mPA3opt\[IndentingNewLine]
>   \(NMaximize[{Evaluate[\(WA\  /. \ mPA3opt\)\  /. mPA2opt] - \
>           PrevCost, \[CapitalDelta]\[Gamma]\  >=
>           0, \[CapitalDelta]\[Gamma]\  <= \[Gamma]}, \
> {\[CapitalDelta]\[Gamma]}];\)\[IndentingNewLine]
>   \)
>
>
>

```

• Prev by Date: Re: Re: Re: Timed Notebooks for Student Take-home examinations
• Next by Date: Re: solve for a squared variable
• Previous by thread: Re: Nesting NMaximize
• Next by thread: basic issue with do and :=