Skip to content

Optimize a function with Discrete inputs

8 messages · Eik Vettorazzi, Sarah Goslee, Peter Dalgaard +2 more

#
Hi

I need to optimize the below function:

a=function(x){
     A=x[1]
     B=x[2]
     C=B-A
return(C)
}

I need to optimize the above function such that x can be any combination of
these number (0,3,5,8) of vector length 2
(i.e) x can be (3,0), (5,0), (8,0), (3,5), (3,8), (5,8), ...... etc

can someone please help me solve this problem ?




--
View this message in context: http://r.789695.n4.nabble.com/Optimize-a-function-with-Discrete-inputs-tp4638644.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi,
not sure if that is what you are looking for, but have a look at

cmb<-t(combn(c(0,3,5,8),2))   #get all pairs of combinations
cbind(cmb,apply(cmb,1,diff))  #for each pair, get the difference

cheers


Am 01.08.2012 12:29, schrieb loyolite270:

  
    
#
combn() gives ordered combinations, while expand.grid() gives all combinations.

I'd give worked code but this hints at homework to me.

Sarah
On Wed, Aug 1, 2012 at 10:23 AM, Eik Vettorazzi <E.Vettorazzi at uke.de> wrote:

  
    
#
On Aug 1, 2012, at 16:34 , Sarah Goslee wrote:

            
...and there's one more function that is designed to tabulate a function of two variables over a grid. And yet another one to find which value is max in an array and return the array indices.
Yes... And since I would expect that most can solve the stated problem in their head, I wonder what the question is, and how it involves R.

  
    
#
Thanks for the reply, but the example i have given above is a sample
function. I would be using a function with input vector of size more than
100 and the function will compute some score based on the 100 vector
combinations.

Since i don't want to do this computation sequentially for all these
combination (which is very time consuming). May some optimization tech could
help me solve the problem.



--
View this message in context: http://r.789695.n4.nabble.com/Optimize-a-function-with-Discrete-inputs-tp4638644p4638715.html
Sent from the R help mailing list archive at Nabble.com.
#
You'll have to give a more realistic description to get detailed help:
otherwise, look up "combinatorial optimization" and "parallelization"
for some generic pointers.

Best,
Michael
On Wed, Aug 1, 2012 at 11:34 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
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)
 }

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.
#
On Thu, Aug 2, 2012 at 5:58 AM, loyolite270 <loyolite270 at gmail.com> wrote:
Odd name for something that's not a dataFrame.... (i.e., data.frame != matrix)
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