[R] How to speed up nested for loop computations

Max Manfrin mmanfrin at ulb.ac.be
Fri Aug 11 19:25:01 CEST 2006


On 11 Aug 2006, at 12:40, jim holtman wrote:

> In your split, you used the dataframe.  What you want to do is to  
> split the row numbers to give the list of indices.  Once you have  
> dont this, you can 'lapply' this list of indices to a function.

Once again I have to thank you, because I was able to finally code  
something that works (except for an error that I'll explain below)  
and is very fast.

The new R code looks like this:

--- BEGIN CODE ---

optimal_values<-read.table("optimal_values_100.txt",header=TRUE)
resPIR2OPT<-read.table("parallel_independent_2- 
opt_100_100.txt",header=TRUE)
resSEQ2OPT<-read.table("sequential_2-opt_100_800.txt",header=TRUE)
resSEQ22OPT<-read.table("sequential2_2-opt_100_100.txt",header=TRUE)
resFC1x102OPT<-read.table("fc.1.x.10_2-opt_100_100.txt",header=TRUE)
resFC26102OPT<-read.table("fc.2.6.10_2-opt_100_100.txt",header=TRUE)
resFC27102OPT<-read.table("fc.2.7.10_2-opt_100_100.txt",header=TRUE)
resFC28102OPT<-read.table("fc.2.8.10_2-opt_100_100.txt",header=TRUE)
resFC29102OPT<-read.table("fc.2.9.10_2-opt_100_100.txt",header=TRUE)
resFC36102OPT<-read.table("fc.3.6.10_2-opt_100_100.txt",header=TRUE)
resFC37102OPT<-read.table("fc.3.7.10_2-opt_100_100.txt",header=TRUE)
resFC38102OPT<-read.table("fc.3.8.10_2-opt_100_100.txt",header=TRUE)
resFC39102OPT<-read.table("fc.3.9.10_2-opt_100_100.txt",header=TRUE)
resHC1x102OPT<-read.table("hc.1.x.10_2-opt_100_100.txt",header=TRUE)
resHC26102OPT<-read.table("hc.2.6.10_2-opt_100_100.txt",header=TRUE)
resHC27102OPT<-read.table("hc.2.7.10_2-opt_100_100.txt",header=TRUE)
resHC28102OPT<-read.table("hc.2.8.10_2-opt_100_100.txt",header=TRUE)
resHC29102OPT<-read.table("hc.2.9.10_2-opt_100_100.txt",header=TRUE)
resHC36102OPT<-read.table("hc.3.6.10_2-opt_100_100.txt",header=TRUE)
resHC37102OPT<-read.table("hc.3.7.10_2-opt_100_100.txt",header=TRUE)
resHC38102OPT<-read.table("hc.3.8.10_2-opt_100_100.txt",header=TRUE)
resHC39102OPT<-read.table("hc.3.9.10_2-opt_100_100.txt",header=TRUE)
resRW1x102OPT<-read.table("rw.1.x.10_2-opt_100_100.txt",header=TRUE)
resRW26102OPT<-read.table("rw.2.6.10_2-opt_100_100.txt",header=TRUE)
resRW27102OPT<-read.table("rw.2.7.10_2-opt_100_100.txt",header=TRUE)
resRW28102OPT<-read.table("rw.2.8.10_2-opt_100_100.txt",header=TRUE)
resRW29102OPT<-read.table("rw.2.9.10_2-opt_100_100.txt",header=TRUE)
resRW36102OPT<-read.table("rw.3.6.10_2-opt_100_100.txt",header=TRUE)
resRW37102OPT<-read.table("rw.3.7.10_2-opt_100_100.txt",header=TRUE)
resRW38102OPT<-read.table("rw.3.8.10_2-opt_100_100.txt",header=TRUE)
resRW39102OPT<-read.table("rw.3.9.10_2-opt_100_100.txt",header=TRUE)
resUR1x102OPT<-read.table("ur.1.x.10_2-opt_100_100.txt",header=TRUE)
resUR26102OPT<-read.table("ur.2.6.10_2-opt_100_100.txt",header=TRUE)
resUR27102OPT<-read.table("ur.2.7.10_2-opt_100_100.txt",header=TRUE)
resUR28102OPT<-read.table("ur.2.8.10_2-opt_100_100.txt",header=TRUE)
resUR29102OPT<-read.table("ur.2.9.10_2-opt_100_100.txt",header=TRUE)
resUR36102OPT<-read.table("ur.3.6.10_2-opt_100_100.txt",header=TRUE)
resUR37102OPT<-read.table("ur.3.7.10_2-opt_100_100.txt",header=TRUE)
resUR38102OPT<-read.table("ur.3.8.10_2-opt_100_100.txt",header=TRUE)
resUR39102OPT<-read.table("ur.3.9.10_2-opt_100_100.txt",header=TRUE)

res<-rbind 
(resFC1x102OPT,resFC26102OPT,resFC27102OPT,resFC28102OPT,resFC29102OPT,r 
esFC36102OPT,resFC37102OPT,resFC38102OPT,resFC39102OPT,resRW1x102OPT,res 
RW26102OPT,resRW27102OPT,resRW28102OPT,resRW29102OPT,resRW36102OPT,resRW 
37102OPT,resRW38102OPT,resRW39102OPT,resHC1x102OPT,resHC26102OPT,resHC27 
102OPT,resHC28102OPT,resHC29102OPT,resHC36102OPT,resHC37102OPT,resHC3810 
2OPT,resHC39102OPT,resUR1x102OPT,resUR26102OPT,resUR27102OPT,resUR28102O 
PT,resUR29102OPT,resUR36102OPT,resUR37102OPT,resUR38102OPT,resUR39102OPT 
,resPIR2OPT,resSEQ2OPT,resSEQ22OPT)

linstance<-levels(res$instance)

res.split<-split(1:nrow(res), list(res$instance, res$try, res 
$idalgo), drop=TRUE)

min.list <- lapply(res.split, function(x){
         x[match(min(res$best[x]), res$best[x])]
         })

# matches return the first among all the values with min best!!!
# so is not the one with minimal time

min.vector <- unlist(min.list)

bestalgo<-res[min.vector,]

bestalgo.split <- split(1:nrow(bestalgo), bestalgo$instance, drop=TRUE)

for (i in (1:length(bestalgo.split)))
{
         bestalgo.vector <- unlist(bestalgo.split[i])
         bestalgo.temp <- bestalgo[bestalgo.vector,]
         l<-split(bestalgo.temp$best,bestalgo.temp$idalgo)

         epsfile=paste(linstance[i],"_100_nolim.eps",sep="")
         postscript(file=epsfile,onefile=TRUE,horizontal=TRUE)
         par(mar=c(5,5,5,3),cex.axis=0.7,las=2,mgp=c(4, 1, 0))
         title_plot=paste("100 iterations - instance ",linstance 
[i],sep="")
         boxplot(l,xlab="",ylab="solution value",names=c(levels 
(bestalgo$idalgo)),main=title_plot,yaxt="n",ylim=c(optimal_values 
[optimal_values$instance==linstance[i],]$optimum,max(bestalgo.temp 
$best)))
         axis(2, seq(from=optimal_values[optimal_values 
$instance==linstance[i],]$optimum,to=max(bestalgo.temp 
$best),length.out=10))
         abline(h=optimal_values[optimal_values$instance==linstance 
[i],]$optimum)
         grid(nx=0, ny=55,col="gray5")
         dev.off()
}

--- END CODE ---

res contains the results of the experiments on 4 problem instances.  
The code produces the boxplots for the first 3 of them and give an  
errore on instance number 4 when it should execute the boxplot  
command. The error is the following:

 > boxplot(l,xlab="",ylab="solution value",names=c(levels(bestalgo 
$idalgo)),main="title_plot",ylim=c(optimal_values[optimal_values 
$instance==linstance[4],]$optimum,max(bestalgo.temp$best)))
Error in if (any(out[nna])) stats[c(1, 5)] <- range(x[!out], na.rm =  
TRUE) :
	missing value where TRUE/FALSE needed
In addition: Warning message:
NAs produced by integer overflow in: x[floor(d)] + x[ceiling(d)]



I removed the ylim to see if something changed, but the error is  
still there...



 > boxplot(l,xlab="",ylab="solution value",names=c(levels(bestalgo 
$idalgo)),main="title_plot")
Error in if (any(out[nna])) stats[c(1, 5)] <- range(x[!out], na.rm =  
TRUE) :
	missing value where TRUE/FALSE needed
In addition: Warning message:
NAs produced by integer overflow in: x[floor(d)] + x[ceiling(d)]

 > str(l)
List of 39
$ FC.1.x.10-2opt: int [1:30] 1185996137 1186007112 1186410641  
1187064761 1186282173 1186282173 1185996137 1187148446 1188080133  
1186282173 ...
$ FC.2.6.10-2opt: int [1:30] 1186282173 1185996137 1187179912  
1186282173 1186282173 1186369404 1185996137 1185996137 1186369404  
1186282173 ...
$ FC.2.7.10-2opt: int [1:30] 1186282173 1185996137 1185996137  
1187864667 1187717114 1186282173 1185996137 1186369404 1186336586  
1185996137 ...
$ FC.2.8.10-2opt: int [1:30] 1185996137 1186282173 1185996137  
1186282173 1185996137 1185996137 1186282173 1185996137 1187713774  
1186007112 ...
$ FC.2.9.10-2opt: int [1:30] 1186282173 1187071224 1186369404  
1186007112 1187252903 1186282173 1186282173 1185996137 1186007112  
1186412178 ...
$ FC.3.6.10-2opt: int [1:30] 1187064761 1186007112 1186812662  
1186231612 1186369404 1185996137 1186441298 1186886951 1186207793  
1185996137 ...
$ FC.3.7.10-2opt: int [1:30] 1186462444 1186027320 1186526792  
1186007112 1186455716 1186336586 1186123369 1185996137 1186733089  
1187033739 ...
$ FC.3.8.10-2opt: int [1:30] 1186052259 1187302984 1186202613  
1186282173 1186052259 1186648860 1186579169 1187252903 1186819027  
1186459107 ...
$ FC.3.9.10-2opt: int [1:30] 1186420877 1186475071 1186808113  
1187187542 1186007112 1186733089 1186373891 1186007112 1186007112  
1186305438 ...
$ RW.1.x.10-2opt: int [1:30] 1186369404 1186007112 1185996137  
1185996137 1185996137 1185996137 1186282173 1186055449 1187148446  
1186007112 ...
$ RW.2.6.10-2opt: int [1:30] 1186353513 1185996137 1187252903  
1186369404 1185996137 1186282173 1186282173 1186373891 1186282173  
1187695581 ...
$ RW.2.7.10-2opt: int [1:30] 1185996137 1185996137 1186282173  
1186007112 1186336586 1185996137 1186282173 1185996137 1187066018  
1186282173 ...
$ RW.2.8.10-2opt: int [1:30] 1186336586 1185996137 1186369404  
1185996137 1186129090 1186378509 1186266654 1186336586 1185996137  
1187189825 ...
$ RW.2.9.10-2opt: int [1:30] 1186420877 1185996137 1186007112  
1186113036 1186336586 1185996137 1185996137 1186407072 1186052259  
1186407072 ...
$ RW.3.6.10-2opt: int [1:30] 1186571192 1186415725 1186291434  
1185996137 1186007112 1186474741 1187084030 1186202995 1186341458  
1186687874 ...
$ RW.3.7.10-2opt: int [1:30] 1186318813 1186007112 1186282173  
1186282173 1185996137 1186398695 1187252982 1186377917 1186407550  
1186570096 ...
$ RW.3.8.10-2opt: int [1:30] 1186207793 1186007112 1186282173  
1186535035 1186205881 1186007112 1186572299 1187314416 1186373891  
1186957225 ...
$ RW.3.9.10-2opt: int [1:30] 1186347814 1186548317 1185996137  
1186369404 1187009587 1187479550 1186730064 1186282173 1185996137  
1186007112 ...
$ HC.1.x.10-2opt: int [1:30] 1185996137 1185996137 1186282173  
1185996137 1186282173 1186369404 1186282173 1185996137 1186369404  
1186336586 ...
$ HC.2.6.10-2opt: int [1:30] 1185996137 1187552951 1185996137  
1185996137 1185996137 1185996137 1187385192 1186369404 1185996137  
1187021722 ...
$ HC.2.7.10-2opt: int [1:30] 1186282173 1187183908 1185996137  
1186373891 1185996137 1186720189 1185996137 1185996137 1187702896  
1186027320 ...
$ HC.2.8.10-2opt: int [1:30] 1186027320 1186282173 1186282173  
1186369404 1186282173 1186282173 1186373891 1187148446 1185996137  
1186336586 ...
$ HC.2.9.10-2opt: int [1:30] 1185996137 1186007112 1186369404  
1186726691 1186282173 1185996137 1186369404 1186369404 1185996137  
1186282173 ...
$ HC.3.6.10-2opt: int [1:30] 1186007112 1186701767 1186007112  
1186757911 1186639768 1185996137 1186380214 1186007112 1187003657  
1186556661 ...
$ HC.3.7.10-2opt: int [1:30] 1185996137 1186007112 1186027320  
1186373891 1186369404 1186702245 1186373891 1185996137 1186402320  
1185996137 ...
$ HC.3.8.10-2opt: int [1:30] 1186386501 1186282173 1185996137  
1185996137 1186096431 1186007112 1186336586 1186213980 1185996137  
1186369404 ...
$ HC.3.9.10-2opt: int [1:30] 1187080500 1186052259 1186420877  
1186658757 1187268873 1186503010 1186052259 1186417092 1187252982  
1186336586 ...
$ UR.1.x.10-2opt: int [1:30] 1185996137 1186121052 1185996137  
1186282173 1185996137 1186282173 1186282173 1185996137 1186282173  
1186078911 ...
$ UR.2.6.10-2opt: int [1:30] 1187693812 1186282173 1186282173  
1185996137 1185996137 1185996137 1186048686 1185996137 1187686384  
1187064761 ...
$ UR.2.7.10-2opt: int [1:30] 1186007112 1186407072 1185996137  
1186282173 1186291836 1186282173 1185996137 1186282173 1186997336  
1186282173 ...
$ UR.2.8.10-2opt: int [1:30] 1186378509 1186903795 1186336586  
1188401386 1186282173 1186007112 1187385192 1185996137 1186282173  
1185996137 ...
$ UR.2.9.10-2opt: int [1:30] 1186007112 1185996137 1186336586  
1186498687 1185996137 1186336586 1186007112 1186202995 1186144595  
1186410641 ...
$ UR.3.6.10-2opt: int [1:30] 1186750319 1185996137 1186736815  
1186336586 1185996137 1186243800 1186346937 1185996137 1187506525  
1187056082 ...
$ UR.3.7.10-2opt: int [1:30] 1186410205 1186007112 1186762512  
1186638875 1186718248 1186235407 1186052259 1185996137 1186407072  
1185996137 ...
$ UR.3.8.10-2opt: int [1:30] 1186336586 1186007112 1186613948  
1185996137 1186282173 1186452282 1186535035 1186380560 1186061771  
1186373891 ...
$ UR.3.9.10-2opt: int [1:30] 1186558839 1186007112 1187209137  
1186923983 1186953819 1186774611 1186963867 1186642445 1186007112  
1186407072 ...
$ PIR-2opt      : int [1:30] 1186007112 1185996137 1186608743  
1185996137 1186585807 1186606396 1188127153 1186407072 1186373891  
1186007112 ...
$ SEQ-2opt      : int [1:30] 1186129090 1186201301 1185996137  
1185996137 1187064761 1186369404 1185996137 1186007112 1185996137  
1187252903 ...
$ SEQ2-2opt     : int [1:30] 1185996137 1187142110 1188212339  
1186919119 1187252903 1188978682 1187581451 1187127822 1188706301  
1188649642 ...


l contains data that "looks" ok: each of the 39 algorithms has his  
own 30 points for the boxplot.

I don't understand the error message. Could anybody help me  
understand what's going on?

----
Max MANFRIN
http://iridia.ulb.ac.be/~mmanfrin/


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20060811/c6456a31/attachment.bin 


More information about the R-help mailing list