1

The model that i have created is very slow. I am a beginner in Netlogo and not sure if my code is inefficient or my system configuration is the problem.

I have a 35 x 35 grid and most of my operations are patch based i.e. each patch has to find the closest and farthest turtle as well as their distance and perform additional operations based on that. In addition at every tick based on an if-else logic where the patches that don't meet the condition turn white with a gradient using the scale-color function while those patches that meet the condition have to take the color of the closest turtle.

I can post the code but don't want to spam, please advise. Thanks.

Following piece of code seems to be problem.

to update-support

  ask patches [
    set closest-party min-one-of parties [distance myself]
    set closest-party-dist [distance myself] of closest-party
    set farthest-party max-one-of parties [distance myself]
    set farthest-party-dist [distance myself] of farthest-party

    set f 0
    set h 0

    set f ( -1 / ( [my-old-size] of closest-party / sum[my-old-size] of parties )) * (closest-party-dist ^ 2)
    set h ( [my-old-size] of farthest-party / sum[my-old-size] of parties ) * (farthest-party-dist ^ 2)

    set b-c (f + h)

    ifelse (b-c <= threshold)
    [ set votes-with-benefit 0 set pcolor scale-color white citizen-share 0 max-citizen-share ]
    [ set votes-with-benefit citizens set pcolor scale-color ([color] of closest-party) citizen-share 0 max-citizen-share]
  ]


   ask parties [ set my-size sum [votes-with-benefit] of patches with [closest-party = myself]
    ;set my-benefit mean[b] of patches with [closest-party = myself]
    set my-benefit-chen mean[b-c] of patches with [closest-party = myself]
  ]


  set largest-party max-one-of parties [my-size]

end

The number of parties is dynamic - it can range between 2 & 10. The update-support module is called both in the set & go. Below is that code:

to setup
  clear-all
  setup-citizens
  setup-parties
  update-support
  setup-plot
  reset-ticks
end

to go
  ask parties [ adapt set my-old-size my-size ]
  update-support
  plot-voter-support
  plot-voter-turnout
tick
end

Regards Yuvaraj

Raj
  • 1,049
  • 3
  • 16
  • 30
  • most likely problem is that you have an `ask ...` inside another `ask` but go ahead and post the code – JenB Feb 22 '16 at 16:49
  • I have included the portion of the code that i think is causing the problem, when i run the model without the script relevant to the "b-c" calculation and conditions the model runs fine without any delay and is fast. – Raj Feb 22 '16 at 16:57
  • how many parties are there? – JenB Feb 22 '16 at 17:15
  • and can we also please see the code that calls this procedure (eg the `go` code) – JenB Feb 22 '16 at 17:22
  • @JenB - Hello JenB, thanks for your comment below. I will include that change to the model. But do you think that's causing the model to run slow, because i had the same part in a previous version of the model which was less complex and it worked fine. It was only after i included the "f", "h" and "b-c" component the model became very heavy and slow. Appreciate your help. thanks – Raj Feb 22 '16 at 18:09
  • no, that wouldn't make a lot of difference, just good coding practice. but see my update. – JenB Feb 22 '16 at 21:57
  • @JenB: Thanks, appreciate your help. Will try the changes you have suggested and let you know if it works. – Raj Feb 22 '16 at 22:39

1 Answers1

2

First simple fix, if you are using a patchset multiple times, create it once and then call it. So:

ask parties [ set my-size sum [votes-with-benefit] of patches with [closest-party = myself]
set my-benefit-chen mean[b-c] of patches with [closest-party = myself]
]

becomes (and easier to read, and ensures consistency if you change the condition later)

ask parties
[ let my-patches patches with [closest-party = myself]
  set my-size sum [votes-with-benefit] of my-patches
  set my-benefit-chen mean[b-c] of my-patches
]

In a similar vein, you have sum[my-old-size] of parties twice in the code (calculating f and h), and you actually calculate it over and over again because you ask each patch to run this code. You should calculate it once and then use it:

to update-support
  let old-total sum [my-old-size] of parties
  ask patches
  [ ...
    set f ( -1 / ( [my-old-size] of closest-party / old-total )) * (closest-party-dist ^ 2)
    set h ( [my-old-size] of farthest-party / old-total ) * (farthest-party-dist ^ 2)
    set b-c (f + h)
    ...
  ]
  ...
end

How much improvement these changes make will depend mostly on the number of parties. Please try them and see if it's solved the problem.

JenB
  • 17,620
  • 2
  • 17
  • 45