Courses/CS 461/Winter 2006/Martin Olsen/Week 5

From CSWiki

Jump to: navigation, search

[edit] Week 5

Last week ( Week 4) I developed a netlogo model basically with the following rules, recall:

  • The model consists of a number of worms.
  • If a worm hits another worm it will turn either right or left.
  • If the worm does not hit another worm it goes straight ahead.
  • The worms are set up in a random order.
  • The worms can only move east, south, west, north, no in-the-middle degrees, only 90, 180, 270, 360.


Basically I have gone on with the idea and added some rules:

  • The worms can have more colors.
  • The worm only turns if it hits another worm with a different color.


Following these extended rules, the worms have a lower threshold for developing a non-turning pattern, and larger populations of worms seems to work as well:
Image:Worms2.jpg

Note: I find that programming in a low end environment (slow) such as netlogo forces the programmer to think a lot more and make more efficient choices when it comes to performance. Just working on this worm model I have realized some stuff I could have done better when I program in other languages. Perhaps this is the reason why a lot of people like programming in NetLogo.
I am specifically referring to the drawing of the worms and how to decide which patch is the last patch in the worms tail. Initially I would redraw the worm each time it would move, and add a local variable on each worm, lastpatch?. If it was the last patch it would be the one to be erased next time. However, this may seem like a good way to do it, but it is too much for netlogo. I came on to the solution of giving each patch a number between 0 and the length of the worm. Then the last patch will have number=length -1. Each time the worm moves, you delete the last patch, give it number -1, and increment all the number of your worms patches by 1, Of course, you also add a patch in the beginning.

Source code