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.
Optimize a function with Discrete inputs
8 messages · Eik Vettorazzi, Sarah Goslee, Peter Dalgaard +2 more
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:
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.
______________________________________________ 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.
Eik Vettorazzi Department of Medical Biometry and Epidemiology University Medical Center Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
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:
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:
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 ?
Sarah Goslee http://www.functionaldiversity.org
On Aug 1, 2012, at 16:34 , Sarah Goslee wrote:
combn() gives ordered combinations, while expand.grid() gives all combinations.
...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.
I'd give worked code but this hints at homework to me.
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.
Sarah On Wed, Aug 1, 2012 at 10:23 AM, Eik Vettorazzi <E.Vettorazzi at uke.de> wrote:
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:
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 ?
-- Sarah Goslee http://www.functionaldiversity.org
______________________________________________ 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.
Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
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:
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.
______________________________________________ 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.
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:
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.