[R] Trouble with Optimization in "Alabama" Package

mcgrete mcgrete.drs at gmail.com
Mon Jun 20 23:55:48 CEST 2011


Hello all,

I was in direct email contact with Ravi, who was very kind to offer his
assistance.  Inbetween correspondence with Ravi, I arrived at a solution. 
In case others have the same issue, I am sharing the solution.   Thanks Ravi
for your willingness to help.

Regards,
Tim

******************************************************************************
Solution: or should I say lack of known-how to pass additional arguments to
functions fn,gr,heq,hin, etc. through the auglag function in the alabama
package.
******************************************************************************

Well, I believe I have solved the problem.  My version of supporting
documentation on alabama must have been an older one.  In more recent
documentation, an example code that I found this morning led me to the
solution. 

Here is what I needed to do.

1.  I created a list 'var2' to hold my parameters that I wished to pass
along to the function fn,gr,hin,heq,etc...  (These parameters are constant,
and will and cannot change during the optimization solution.  I am
attempting to find the best set of the variable 'f' that optimize my cost
function.)  I then modified the declaration of each function (e.g.
fn,fr,hin,heq,etc) to be of the form below.  Not sure a list is required,
but it was nice and compact for my needs.
foo1 <- function(f,var,...){
    x<-var$x
    p<-var$p
    wo<-var$wo
    s<-var$s
    r<-var$r
   # Other code
   return(return_object_name)
}  # end of function


2.  Then, I created a function 'foobar' from which I wished to call auglag
from, passing arguments into foobar.  I ran foobar two different ways: 1)
commenting out the assignment of different values of fo,x,p,wo,s,r (which
are variables in my list 'var2') so as to utilize the values sent by the
arguments in the calling with foobar(...), and 2) uncommented assignments of
variables that form the list 'var2'.  This permitted me to ensure which set
of data were being used by auglag when called from within the function.

foobar<-function(fo,x,p,wo,s,r){
    # Comment out code for setting x,fo,p,wo,s,r to use incoming code,
uncomment to set in function
    # I was successful at demonstrating and using var2 with variables from
arguments supplied to foobar
    # vs using variables for var2 that were specified from within foobar.
    #x<-c(0.25,0.25,0.5)
    #fo<-0.1
    #p<-c(0.4,0.2,0.4)
    #wo<-100
    #s<-c(0.1,0.5,0.4)*1e6
    #r<-0.18
    var2<-list(x=x,p=p,wo=wo,s=s,r=r)
    ans2 <- auglag(par=fo,fn=MVK_cost_fcn, gr=MVK_grad_cost_fcn,
hin=MVK_w_inequality,var=var2)
}

Then, I formulated the call to auglag from the main environment (not from
within any function):
auglag(par=fo,fn=MVK_cost_fcn, gr=MVK_grad_cost_fcn,
hin=MVK_w_inequality,var=var2) # to verify that using additional arguments
in auglag will work.  The key: I had not tried to add the additional
argument(s) in this location before.
Then, I utilized
ans5<-foobar(fo,x,p,wo,s,r) # to ensure that I can execute auglag from a
function, and use variable 'system parameters' (x,p,,wo,s,r) as a list in my
other functions, i.e. cost function, gradient, etc.

I was successful, and was able to verify proper usage of different local and
global variables accordingly.

** NOTE: I realized that I was attempting to send 'var' without assigning a
name to it, such as 'var=var'.  Doing so incorrectly as follows:
ans <- auglag(par=fo,var,fn=MVK_cost_fcn, gr=MVK_grad_cost_fcn,
hin=MVK_w_inequality)

,  I obtained the error: "Error in hin(par, ...) : argument "var" is
missing, with no default
Calls: auglag -> auglag2 -> hin -> hin"  Silly mistake...

Using 'var=var' (or inside my function provided I used 'var2' to hold the
new list of variables) in the call of auglag:
ans <- auglag(par=fo,fn=MVK_cost_fcn, gr=MVK_grad_cost_fcn,
hin=MVK_w_inequality,var=var), or
ans <- auglag(par=fo,var=var,fn=MVK_cost_fcn, gr=MVK_grad_cost_fcn,
hin=MVK_w_inequality)
both work!


Regards,
Tim

--
View this message in context: http://r.789695.n4.nabble.com/Trouble-with-Optimization-in-Alabama-Package-tp2548801p3612614.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list