## Variables in nested opimazation

Hi,

I posted a question about this earlier but it was poorly written so i thought i'd try again and hopefully be more clear.

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.

Maybe this is a simple syntax question, what I want is something like this (just example functions):

start f1(x) global(a,b,y);
return(a*y-b##x);
finish;

start f2(y);
bounds = {0,10000};
return( froot( "f1", bounds) );
finish;

start f3(y) global(a,b);
return(f2(y)*a-y##2);
finish;

y0 = 1;
opt = {1,2};
call nlpnra(rc, result, "f3", y0, opt);

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?

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Variables in nested opimazation

As I indicated last time you wrote, 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<=0). If there are domain issues, you need to add boundary constraints for your objective function.

The following statements plot the objective function. The objective function appears to maximize as x -> 0 from the right, but it is undefined for x <= 0, so that need to be added to the optimization.

``````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 <= y < infinity */
.};
call nlpnra(rc, result, "f3", y0, opt) blc=blc;
``````

Using your notation, I suggest the following:

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.

2. It sounds like you want to optimize over the parameter y where a and b are fixed.

3. As I mentioned last time, I've written about similar examples. Since you didn't like my previous suggestions, try this one: "Optimizing a function that evaluates an integral"

2 REPLIES 2

## Re: Variables in nested opimazation

As I indicated last time you wrote, 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<=0). If there are domain issues, you need to add boundary constraints for your objective function.

The following statements plot the objective function. The objective function appears to maximize as x -> 0 from the right, but it is undefined for x <= 0, so that need to be added to the optimization.

``````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 <= y < infinity */
.};
call nlpnra(rc, result, "f3", y0, opt) blc=blc;
``````

Using your notation, I suggest the following:

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.

2. It sounds like you want to optimize over the parameter y where a and b are fixed.

3. As I mentioned last time, I've written about similar examples. Since you didn't like my previous suggestions, try this one: "Optimizing a function that evaluates an integral"

## Re: Variables in nested opimazation

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.

I don't think the problem lies with the objection function or constraints, bounds etc. Since i'm able to use froot  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.

From The DO Loop