1/27/2024 0 Comments Netlogo letThis seminar is designed to introduce students to basic tools of theory building and data analysis in systems science and to apply those tools to better understand the obesity epidemic in human populations. However, it is just beginning to impact public health. NetLogo is one of my favourite simulation tools, as I have said in a previous tutorial.Systems science has been instrumental in breaking new scientific ground in diverse fields such as meteorology, engineering and decision analysis. It is so easy to use that children can program in it, and very sophisticated functionality can be delivered with surprisingly little code. However, this ease of use has attracted scorn from some quarters, with one blogger even suggesting that “ using NetLogo signals that you don’t know how to do real programming.” A colleague of mine once expressed a similar sentiment. ![]() Nothing could be further from the truth – such comments are like suggesting that The Lord of the Rings is unsuitable for adults simply because children enjoy it. In fact, NetLogo incorporates much of the power of Lisp. In this tutorial, I will highlight the power of the list operators foreach, map, and filter, through two classic list-processing case studies: the Eight Queens puzzle (illustrated above) and the Sieve of Eratosthenes. NetLogo also includes a reduce operator, which allows non-parallel versions of the MapReduce style of programming. The Eight Queens puzzle requires placing eight queens on a chessboard so that none threaten each other – i.e. so that none are in the same row, column, or diagonal. Following a relatively standard approach, we will place exactly one queen in each column, representing a solution as a list of row positions, indexed from 1. The picture above shows the solution, for example. Report (j = n) or (i + j = m + n) or (i - j = m - n) To test the (column,row) pairs ( i, j) and ( m, n) for mutual threats, we check for queens in the same row or diagonal (our placement strategy will guarantee exactly one queen per column): to-report add-queen We can add a new queen to such a list with the lput operator ( fput and lput are equally efficient in NetLogo 5.0, so there is no longer any need to prefer fput). We can test a new row position n for compatibility with an existing partial solution lst by calculating the column the new queen will be in: ((length lst) + 1). We then use a foreach loop to iterate through the existing solution. The command block inside this loop refers to the current list element as ?. The combination is safe if we find no threats: to-report safe Simultaneously, we use i to iterate through the column positions in the existing solution. The main reporter takes two arguments: the number of queens to place ( n) and the number of rows (usually 8). ![]() It returns a list of all possible solutions: a list of lists. If n = 0, there is one way of placing zero queens (the sole solution is represented by the empty list). [ let lst (queen-solutions (n - 1) num-rows ) All solutions that pass the safety check are accumulated in res, which is the value returned: to-report queen-solutions Otherwise, we recursively place n - 1 queens (giving lst), and for each solution in that list try all possible rows in the next column (using a while loop). ![]() We also need a global variable to store a list of solutions: to-report all-queen-solutions If (safe ? i) Īt the starting level, we pass in the same number (usually 8) in both arguments.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |