0

Do you know how to vectorize 14 nested loops ? (18 but the last of each block of for is predetermined by the others in the same block. I use for because it's easier to read)

max2=0
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))
                              {
                                    max1=1.1*M_P+2.1*M_D+3.1*M_A+4.1*M_CC+4*M_CD+2*G_D+5*G_A+1.5*G_CC+3*G_CD+5*S_D+4*S_A+3*S_CC+6*S_CD+2*Q_P+3*Q_D+2.2*Q_A+3*Q_CC+4*Q_CD
                                if(max1>max2)
                                {
                                  max2=max1
                                }}}}}}}}}}}}}}}}}}}
print(max2)

I need to do this little optimization work but i have no idea how to vetorize it since I'm new in both R and programmation ! Basically I have 3 set of 9 slots and 1 set of 3 slots. In slots I can put some kind of badges that have certain value and those nested loops search for the best combination of badges possible. Of course in the present example you just put badges with higher values but in the real case the max1 calculation is bit more complicated and non trivial (I can give you the whole code if you need it).

TooTone
  • 7,129
  • 5
  • 34
  • 60
Wicelo
  • 2,358
  • 2
  • 28
  • 44
  • 2
    Yes, the full code would indeed be helpful, along with the data. The answer is going to depend on the exact context. – Peyton Jun 03 '13 at 16:25
  • Why don't you fully state your problem. Tell us what you're starting with and what result you want to obtain (i.e., give us actual data that you would like to see as inputs and what that would look like as outputs). I'm not really clear from your code what you're trying to obtain...a single value? – Thomas Jun 03 '13 at 16:30
  • 3
    Very often, when you have an excessive number of nested loops, the problem can be reformulated in a recursive way: in your case, you would probably end up writing a function whose arguments are the number of unallocated slots, the list of remaining badges to assign, and returning the best solution. Your problem vaguely looks like a [knapsack problem](https://en.wikipedia.org/wiki/Knapsack_problem). – Vincent Zoonekynd Jun 03 '13 at 16:33
  • 1
    I think `expand.grid` is your friend. `expand.grid(0:9, 0:9, 0:9)` will give you the result of your first 3 loops... – Justin Jun 03 '13 at 16:34
  • Looks to me like you're trying to do constrained optimization, why not try the `linprog` module: http://cran.r-project.org/web/packages/linprog/index.html – robbrit Jun 03 '13 at 16:34
  • 2
    Re-asking the same question twice is not really constructive, especially when you have not done provided a reproducible example. http://stackoverflow.com/questions/16890250/how-to-use-multicore-with-loops-in-r – Paul Hiemstra Jun 03 '13 at 17:01
  • how is that not reproducible when I copy paste this in R it works for me – Wicelo Jun 03 '13 at 17:35
  • 2
    Reproducible means it works for us if we paste it into R. Give us some data. – Roland Jun 03 '13 at 18:24

0 Answers0