Raj
Raj

Reputation: 1071

Netlogo model is extremely slow and has a delay between each tick

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

Upvotes: 1

Views: 328

Answers (1)

JenB
JenB

Reputation: 17678

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.

Upvotes: 2

Related Questions