<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Variables in nested opimazation in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Variables-in-nested-opimazation/m-p/478585#M4288</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I posted a question about this earlier but it was poorly written so i thought i'd try again and hopefully be more clear.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to solve a nested optimization problem where I first find the optimal x based on y and then y is "updated" based on this x. I'm using the froot function which as i understand it can take in parameters and global variables. Since this y is constantly changing i'm not sure it should be a global variable but it shouldn't be changed when finding x either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe this is a simple syntax question, what I want is something like this (just example functions):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;start f1(x) global(a,b,y);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;return(a*y-b##x);&lt;BR /&gt;finish;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;start f2(y);&lt;BR /&gt;bounds = {0,10000};&lt;BR /&gt;&amp;nbsp; &amp;nbsp;return( froot( "f1", bounds) );&lt;BR /&gt;finish;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;start f3(y) global(a,b);&lt;BR /&gt;return(f2(y)*a-y##2);&lt;BR /&gt;finish;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;y0 = 1;&lt;BR /&gt;opt = {1,2};&lt;BR /&gt;call nlpnra(rc, result, "f3", y0, opt);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in function f1 the current value of y needs to be known but is should not be changed (so its not a parameter in froot?). Should f2 somehow declare y as a global variable, if so how is that done?&lt;/P&gt;</description>
    <pubDate>Tue, 17 Jul 2018 08:41:32 GMT</pubDate>
    <dc:creator>jan_t_lagen</dc:creator>
    <dc:date>2018-07-17T08:41:32Z</dc:date>
    <item>
      <title>Variables in nested opimazation</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Variables-in-nested-opimazation/m-p/478585#M4288</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I posted a question about this earlier but it was poorly written so i thought i'd try again and hopefully be more clear.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to solve a nested optimization problem where I first find the optimal x based on y and then y is "updated" based on this x. I'm using the froot function which as i understand it can take in parameters and global variables. Since this y is constantly changing i'm not sure it should be a global variable but it shouldn't be changed when finding x either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe this is a simple syntax question, what I want is something like this (just example functions):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;start f1(x) global(a,b,y);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;return(a*y-b##x);&lt;BR /&gt;finish;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;start f2(y);&lt;BR /&gt;bounds = {0,10000};&lt;BR /&gt;&amp;nbsp; &amp;nbsp;return( froot( "f1", bounds) );&lt;BR /&gt;finish;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;start f3(y) global(a,b);&lt;BR /&gt;return(f2(y)*a-y##2);&lt;BR /&gt;finish;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;y0 = 1;&lt;BR /&gt;opt = {1,2};&lt;BR /&gt;call nlpnra(rc, result, "f3", y0, opt);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in function f1 the current value of y needs to be known but is should not be changed (so its not a parameter in froot?). Should f2 somehow declare y as a global variable, if so how is that done?&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jul 2018 08:41:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Variables-in-nested-opimazation/m-p/478585#M4288</guid>
      <dc:creator>jan_t_lagen</dc:creator>
      <dc:date>2018-07-17T08:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in nested opimazation</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Variables-in-nested-opimazation/m-p/478625#M4289</link>
      <description>&lt;P&gt;As I indicated &lt;A href="https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Global-variables-in-nested-optimization-problem/m-p/477499#M4285" target="_self"&gt;last time you wrote&lt;/A&gt;, the best way to unravel these situations is to evaluate the objective function on a grid to make sure it is doing what you expect. You should also ensure that the various functions correctly handle out-of-domain errors (such as log(x) when x&amp;lt;=0). If there are domain&amp;nbsp;issues, you need to add boundary constraints for your objective function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following statements plot the objective function. The objective function appears to&amp;nbsp;maximize&amp;nbsp;as x -&amp;gt; 0 from the right, but it is undefined for x &amp;lt;= 0, so that need to be added to the optimization.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
start f1(x) global(a,b,y);
   return(a*y-b##x);
finish;

start f2(x);
   bounds = {0,1000};
   return( froot( "f1", bounds) );
finish;

start f3(y) global(a,b);
   z = f2(y);
   return(z*a-y##2);
finish;

a=2; b=0.5;
y = do(0.05, 2, 0.05);
f = j(1, ncol(y), .);
do i = 1 to ncol(y);
   f[i] = f3(y[i]);
end;
print (f[1:5]);
call series(y, f);

y0 = 1;
opt = {1,2};
blc = {1e-6,    /* 1e-6 &amp;lt;= y &amp;lt; infinity */
       .};        
call nlpnra(rc, result, "f3", y0, opt) blc=blc;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using your notation, I suggest the following:&lt;/P&gt;
&lt;P&gt;1. You've set f1 to be a function of x, with a, b, y as parameters. That means that f2 is also a function of x, with a,b, and y as parameters.&lt;/P&gt;
&lt;P&gt;2. It sounds like you want to optimize over the parameter y&amp;nbsp;where a and b are fixed.&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp;As I mentioned last time,&amp;nbsp;I've written about similar examples. Since you didn't like my previous suggestions, try this one: &lt;A href="https://blogs.sas.com/content/iml/2014/03/05/optimizing-a-function-that-evaluates-an-integral.html" target="_self"&gt;"Optimizing a function that evaluates an integral"&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jul 2018 12:32:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Variables-in-nested-opimazation/m-p/478625#M4289</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-07-17T12:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: Variables in nested opimazation</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Variables-in-nested-opimazation/m-p/478638#M4290</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the fast replay as always! I'll try your suggestion and if I can't get it to work look though the material you linked.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't think the problem lies with the objection function or constraints, bounds etc. Since i'm able to use froot&amp;nbsp; successfully when it is not in a function. I.e. I'm able to do the initial step of optimization if i'm not using nested functions like in the example. Perhaps it was even more confusing to use "example" functions. What I was trying to convey was that I'm 99% sure that the problem lies with my understanding of the functions and not the underlying math.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jul 2018 13:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Variables-in-nested-opimazation/m-p/478638#M4290</guid>
      <dc:creator>jan_t_lagen</dc:creator>
      <dc:date>2018-07-17T13:21:18Z</dc:date>
    </item>
  </channel>
</rss>

