-2

I need to speed up this built in loops how can I do please ?

for(M_P in 0:9)
{
for(M_D in 0:(9-M_P))
{
for(M_A in 0:(9-M_P-M_D))
{
  for(M_CC in 0:(9-M_P-M_D-M_A))
  {
    for(M_CD in (9-M_P-M_D-M_A-M_CC))
    {


      for(G_D in 0:9)
      {
        for(G_A in 0:(9-G_D))
        {
          for(G_CC in 0:(9-G_D-G_A))
          {
            for(G_CD in (9-G_D-G_A-G_CC))
            {


              for(S_D in 0:9)
              {
                for(S_A in 0:(9-S_D))
                {
                  for(S_CC in 0:(9-S_D-S_A))
                  {
                    for(S_CD in (9-S_D-S_A-S_CC))
                    {


                      for(Q_P in 0:3)
                      {
                        for(Q_D in 0:(3-Q_P))
                        {
                          for(Q_A in 0:(3-Q_P-Q_D))
                          {
                            for(Q_CC in 0:(3-Q_P-Q_D-Q_A))
                            {
                              for(Q_CD in (3-Q_P-Q_D-Q_A-Q_CC))
                              {

It's taking forever to compute how can I do please ? I'm only a beginner but I heard that there's maybe something with the apply thing or also with multi cores i have 4cores, i'm using Rstudio please help me. Thank mens !

PS : tell me if you need the rest of code but it's a simple calculation with * and + using all the M_P, M_D etc. If i leave it overnight does it have a chance to end ? I would like to add few loops in that so I would have to do all of this 3 or 10 more times :s

Wicelo
  • 2,358
  • 2
  • 28
  • 44
  • 2
    Have you considered using `expand.grid` to put all your combinations into a data.frame? – sebastian-c Jun 03 '13 at 05:13
  • sorry my algorithm was incomplete i posted too fast ! I added the core element which are the "-" in loops – Wicelo Jun 03 '13 at 05:40
  • 1
    One of the lessons that I learnt when I started with R is to vectorize problems instead of loops. The code runs a lot faster that way. Some of it is decribed in the following question and answer: http://stackoverflow.com/questions/13475039/how-to-optimize-the-following-code-with-nested-while-loop-multicore-an-option. You might want to take this as a new avenue for your thinking in solving the problem. – Jochem Jun 03 '13 at 06:13
  • 2
    Nearly 20 nested `for` loops? Parallelization will not help you much. It might be possible to do it like that in C, but since you want to use R, you should vectorize the code. I highly recommend reading [this document](http://www.burns-stat.com/pages/Tutor/R_inferno.pdf). – Roland Jun 03 '13 at 07:03
  • is vectorization always the same process ? I know nothing about it but if you can vectorize anything easily and quickly then I can learn it, however if it's a pain the ass everytime maybe it's better not to use R for some kind of problem ? Is it easy to vectorize 20 nested loops in your opinion ? – Wicelo Jun 03 '13 at 07:18
  • It depends on what the loops are doing. However, I strongly suspect that it will be quite simple for this problem if you have some basic understanding of the language. The `expand.grid` function has already been pointed out to you as one possible path to vectorization. – Roland Jun 03 '13 at 07:29
  • 1
    I would suggest you expand you question to include a working example, and a description of what you want to accomplish. It is not easy to say if vectorization is going to be straightforward, but having 20 nested loops is always a bad idea. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for details on how to write a good example. – Paul Hiemstra Jun 03 '13 at 07:32
  • possible duplicate of [How vectorize 14 nested loops in R?](http://stackoverflow.com/questions/16901346/how-vectorize-14-nested-loops-in-r) – Chase Jun 03 '13 at 19:19

1 Answers1

0

The variables M_xx, G_xx, S_xx and Q_xx are independent. Also, there is a lot of coincident values generated. I've split these 4 variables in separate loops, then I've generated all combinations considering only unique values. See the code:

M <- NULL

for(M_P in 0:9) for(M_D in 0:(9-M_P)) for(M_A in 0:(9-M_P-M_D)) for(M_CC in 0:(9-M_P-M_D-M_A)) for(M_CD in (9-M_P-M_D-M_A-M_CC))
{
    M[length(M)+1] <- 1.1*M_P+2.1*M_D+3.1*M_A+4.1*M_CC+4*M_CD
}

G <- NULL

for(G_D in 0:9) for(G_A in 0:(9-G_D)) for(G_CC in 0:(9-G_D-G_A)) for(G_CD in (9-G_D-G_A-G_CC))
{
    G[length(G)+1] <- 2*G_D+5*G_A+1.5*G_CC+3*G_CD
}

S <- NULL

for(S_D in 0:9) for(S_A in 0:(9-S_D)) for(S_CC in 0:(9-S_D-S_A)) for(S_CD in (9-S_D-S_A-S_CC))
{
    S[length(S)+1] <- 5*S_D+4*S_A+3*S_CC+6*S_CD
}

Q <- NULL

for(Q_P in 0:3) for(Q_D in 0:(3-Q_P)) for(Q_A in 0:(3-Q_P-Q_D)) for(Q_CC in 0:(3-Q_P-Q_D-Q_A)) for(Q_CD in (3-Q_P-Q_D-Q_A-Q_CC))
{
    Q[length(Q)+1] <- 2*Q_P+3*Q_D+2.2*Q_A+3*Q_CC+4*Q_CD
}

max(apply(expand.grid(unique(M), unique(G), unique(S), unique(Q)), 1, sum))
Ferdinand.kraft
  • 12,579
  • 10
  • 47
  • 69
  • well in fact my variables are not independant in the real scenario and i'm too bad at programming to find a solution starting with yours that's too bad, but i validate your answer since your answered it the way I asked it thank's ! – Wicelo Jun 06 '13 at 00:32