[R] Optimize a function with Discrete inputs
R. Michael Weylandt
michael.weylandt at gmail.com
Thu Aug 2 19:28:04 CEST 2012
On Thu, Aug 2, 2012 at 5:58 AM, loyolite270 <loyolite270 at gmail.com> wrote:
> oh sorry ..
>
> The detailed description of the problem is given below
>
> dataFrame is matrix of dim 100X100 with some values
Odd name for something that's not a dataFrame.... (i.e., data.frame != matrix)
> a is a vector of length 1
> x is a vector of any length between 1 to 99
>
> funcScore<-function(a,x){
>
> sc<-0
> sc1<-0
>
> for(j in 1:(length(x))){
> sc<-sc+abs(dataFrame[a,x[j]])
> }
>
> if(length(x)==1){
> sc1<-0
> }
>
> else{
> if(length(x)>1){
> for(i in 1:(length(x)-1)){
> for(j in (i+1):(length(x))){
> sc1 <- sc1+abs(dataFrame[(x[i]-1),x[j]])
> }
> }
> }
> }
> score <- sc-sc1
> return(score)
> }
Surely you can write this more efficiently:
score <- function(a, x, datfrm){
sc <- sum(abs(datfrm[a, x]))
# Something similar for sc1
sc - sc1
}
Depending on how fast you get it (and I expect massive speed ups upon
rewrite if you vectorize properly), an exhaustive search via combn()
or expand.grid() might work -- otherwise, see combinatorial
optimization pointers, as I noted earlier.
Best,
Michael
>
> Given the value of "a", i would like to use some optimization function (
> value of x) to maximize the score, such that x can be any combination ?
>
> Thanks
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Optimize-a-function-with-Discrete-inputs-tp4638644p4638851.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list