Hello, This is my first post to the help request list, so I'm going to err on the side of giving too much information. I'm working on writing a simulation in which agents will make repeated production and exchange decisions with randomly chosen partners. The idea is, all agents can produce two goods which they want to consume, they choose a value t in [0,10] which sets their production time allocated to each good. Then they are matched with another individual with whom they will trade according to various bargaining algorithms. I want to write a general purpose optimization that takes their initial allocations (what they produced) and computes, for example, the solution which maximizes the product of their utilities from the final allocation, subject to the constraint that their utilities must be at least as high as if they didn't trade at all. Since constrOptim.nl finds the maximizing set of parameters based on the initial par argument fed to it, I had to include an additional argument to the functions so I could bring the parameters of the utility functions and the initial production into the optimization. I've written the following code but I keep getting strange errors: library(alabama) # y is a vector of initial allocations and CobbDouglas preference parameters #y=c(r1,r2,b1,b2,alpha1,alpha2) y=c(5,50,60,5,.3,.7) # p1=c(r1bar,r1bar,b1bar,b1bar) a feasible allocation p1=c(15,40,50,15) fn=function(x,...){ return(-1*(((x[1]^y[5])*(x[3]^(1-y[5])))*(((x[2]^y[6])*(x[4]^(1-y[6])))))) } hin=function(x,...){ h=rep(NA,2) h[1]=((x[1]^y[5])*(x[3]^(1-y[5])))-((y[1]^y[5])*(y[3]^(1-y[5]))) h[2]=((x[2]^y[6])*(x[4]^(1-y[6])))-((y[2]^y[6])*(y[4]^(1-y[6]))) return(h) } heq=function(x,...){ h=rep(NA,2) h[1]=x[1]+x[2]-y[1]-y[2] h[2]=x[3]+x[4]-y[3]-y[4] return(h) } ans2=constrOptim.nl(par=p1,fn=fn,hin=hin,heq=heq,control.outer=list(itmax1000,mu0=.00001),list(y,y,y)) Any advice or explanation of my errors would be greatly appreciated! -- Erik O. Kimbrough Department of Economics (AE1) School of Business and Economics Maastricht University [[alternative HTML version deleted]]
I will take a look and get back to you. Ravi. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Erik Kimbrough Sent: Tuesday, September 21, 2010 11:01 AM To: R-help at r-project.org Subject: [R] Trouble with Optimization in "Alabama" Package Hello, This is my first post to the help request list, so I'm going to err on the side of giving too much information. I'm working on writing a simulation in which agents will make repeated production and exchange decisions with randomly chosen partners. The idea is, all agents can produce two goods which they want to consume, they choose a value t in [0,10] which sets their production time allocated to each good. Then they are matched with another individual with whom they will trade according to various bargaining algorithms. I want to write a general purpose optimization that takes their initial allocations (what they produced) and computes, for example, the solution which maximizes the product of their utilities from the final allocation, subject to the constraint that their utilities must be at least as high as if they didn't trade at all. Since constrOptim.nl finds the maximizing set of parameters based on the initial par argument fed to it, I had to include an additional argument to the functions so I could bring the parameters of the utility functions and the initial production into the optimization. I've written the following code but I keep getting strange errors: library(alabama) # y is a vector of initial allocations and CobbDouglas preference parameters #y=c(r1,r2,b1,b2,alpha1,alpha2) y=c(5,50,60,5,.3,.7) # p1=c(r1bar,r1bar,b1bar,b1bar) a feasible allocation p1=c(15,40,50,15) fn=function(x,...){ return(-1*(((x[1]^y[5])*(x[3]^(1-y[5])))*(((x[2]^y[6])*(x[4]^(1-y[6])))))) } hin=function(x,...){ h=rep(NA,2) h[1]=((x[1]^y[5])*(x[3]^(1-y[5])))-((y[1]^y[5])*(y[3]^(1-y[5]))) h[2]=((x[2]^y[6])*(x[4]^(1-y[6])))-((y[2]^y[6])*(y[4]^(1-y[6]))) return(h) } heq=function(x,...){ h=rep(NA,2) h[1]=x[1]+x[2]-y[1]-y[2] h[2]=x[3]+x[4]-y[3]-y[4] return(h) } ans2=constrOptim.nl(par=p1,fn=fn,hin=hin,heq=heq,control.outer=list(itmax1000,mu0=.00001),list(y,y,y)) Any advice or explanation of my errors would be greatly appreciated! -- Erik O. Kimbrough Department of Economics (AE1) School of Business and Economics Maastricht University [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hello Erik and Ravi, I am curious if Erick or Ravi have a solution to Erick's question. I have a similar problem. I wish to use auglag to solve a nonlinear optimization problem. The cost function, grad function, and constraints are all function of some parameters that are dependent upon the system I wish to analyze. For any given system, these parameters are fixed, e.g. x,y,z are parameters that are problem or system dependent. The variable f is a vector that I wish to find such that my cost function is maximized or minimized. I have used auglag without any issues as follows. At first, I called auglag without trying to provide arguments for x,y,z, but rather only the initial parameter fo. My functions (fn, gr, hin, heq, etc) were all defined in the format of: foo<-function(f){ # code... } The parameters x,z,z were obtained from what I understand to be the global environment. I was NOT calling 'auglag' from a function, but simply from the command line in 'Rkward'. In other words, parameters x,y,z were calculated outside of any functions but were utilized in the function fn,gr,hin,heq,etc. Now, I have constructed a function, foobar, that utilizes inputs a,b,c that are used to calculate parameters x,y,z. And within foobar, I attempt to call 'auglag' to find an optimum solution. For example: foobar<-function(a,b,c){ # code to calculate x,y,z as function of a,b,c ans<-auglag(par=fo,fn=foo1,gr=foo2,hin=foo3,heq=foo4) return(ans) } However, auglag appears to utilize x,y,z from the global environment rather than my local variables x,y,z inside foobar. I have attempted to construct functions fn,gr,hin,heq,... by several ways without success. For example foo<-function(f,x,y,z){code...}, ans<-auglag(par=fo,fn=foo1,gr=foo2,hin=foo3,heq=foo4), or ans<-auglag(par=c(fo,x,y,z),fn=foo1(,gr=foo2,hin=foo3,heq=foo4) and I tried foo<-function(f,...){code...}, ans<-auglag(par=fo,fn=foo1,gr=foo2,hin=foo3,heq=foo4), or foo<-function(f,...){code...}, ans<-auglag(par=c(fo,x,y,z),fn=foo1(,gr=foo2,hin=foo3,heq=foo4) All failed. Is it simply not feasible to use 'auglag' in this manner? Or, have I missed how to pass arguments to auglag, which in turn can pass these arguments to fn,gr,hin,heq,... Any help would be greatly appreciated. Regards, Tim -- View this message in context: http://r.789695.n4.nabble.com/Trouble-with-Optimization-in-Alabama-Package-tp2548801p3610402.html Sent from the R help mailing list archive at Nabble.com.