Programming Mathematical Models ... with NetLogo
This post has the goal to serve as guide for the session "Programming Mathematical Models ... with NetLogo" from the 5th International Summer School of Mathematics in Seville, between July 15th and 31st 2016.
For this reason, this post is more like a set of notes to focus our achievements that something to read from outside... sorry for those coming from outside the Summer School, as it can be of little interest for them.
The main objective of this session is showing how to use mathematical computational models (that is, programming in a adequate environment) as a tool for experimenting and improving knowledge in math. For that, we will take a BIG problem, that cannot be totally explained with current math developments, and will provide some models for playing, getting experience, formulating hypotheses and testing ideas on the math object behind.
Actually, this can (and must) be done with ALL the mathematics we find, but we will take some Complex System related content, as it is not so usual for the beginners. Specifically, we will play around Cellular Automata as representatives of mathematical objects with a simple definition but with complex evolution in time.
You can find the slides used in the session here. Please, remember that it is lesson oriented, so the slides are incomplete since some of the main aspects have to be discovered by the students while discussing about them.
After the theoretical presentation we will build a series of models using NetLogo as programming and experimentation framework (modeling and simulation) that will take us from a very simple (and wrong) model to a complete model for 2D Cellular Automata. After that, we will optimize our model to work with the popular and fascinating Game of Life case (from John H. Conway).
The promenade through this landscape is the following (the title links will download the netlogo files, Right Click on them and "Save..."):
We show here a very basic implementation of a first 2D CA. The initial program is given, so we will not start from scratch.
Cells are Patches and we store the information (state) of the cells in the color:
white -> alive black -> dead
NetLogo concepts: ca, ask, one-of, count, neighbors, with, use of [...] blocks, if, ifelse, lists
- base-colors is the list of basic colors in NetLogo. Give a procedure to, randomly assign a color to every patch in the world.
- Create a procedure that randomly selects a patch and colors its neighbors with the same color of it. Assign this procedure to a forever button.
- Create a procedure that changes all black patches to white, and the others to green.
Compute num-neigh out of the alive/dead checking.
Use the concept of list to store rules and reduce the transition computation.
NetLogo Concepts: item
Create state patch property to store the state of every cell, and change its value to 0/1.
Assign color by using a ifelse structure.
Assign color by using lists adequately.
Create recolor patch procedure. Explain patches procedures and contexts.
Create global variables for alive-rule and dead-rule.
Create a procedure to create rules independently (they will be prefixed in this point).
Don’t forget to remove ca.
Emphasize differences between let and set.
Create a patch predicate (0/1 report) alive?
Reports: Emphasize differences between to and to-report.
NetLogo concepts: true/false, comparisons, report
- Create a report to calculate that receives two numbers and report the biggest one.
- Create a patch report to return the number of neighbors with the same color to it.
Parallelism problem: create patch property new-state and adapt the program by doing a 2-stages process in step procedure.
Create random rules.
Add monitors for the rules.
Play with random rules and try to find interesting behaviors.
NetLogo Concepts: n-values
- Use n-values to return the following lists: \([0\ 1 \ \dots\ n]\), \([0\ 2\ \dots\ 2n]\), \([0\ 1\ 4\ 9\ \dots\ n^2]\), \([0\ \dots\ 0]\)
- Create a report that receives 2 numbers, m and n, and returns the list of length n with all its elements as m: \([ m\ m\ \dots\ m]\)
Statistical problems in the random construction of rules:
- How is the mean rule we can build?
- How can we solve it?
One possible way:
Represent rules in decimal way, use the random in this new representation, and then convert to binary (be careful with the :size: of the resulting list).
- How many rules can we obtain?
NetLogo concepts: recursion, sentence, mod, random, length, lput, int
- Create a report that receives a number n, ad returns a list of n random numbers between 0 and 99.
- Create a report to return the list of divisors of a number.
- Create a report to say if a number is prime or not.
- Create a report to convert from binary to decimal.
Add new types of starting worlds and options to draw/erase by hand.
Create procedure for Game of Life.
NetLogo concepts: mouse instructions, in-radius, sliders, notes
Convert execution mode from continuous to ticks.
Add tables to manage objects.
Optimize to manage only neighbors of living cells (only valid for GoL). Change name to GoL v10.
NetLogo Concepts: comments, choosers, startup, extensions, ticks, reset-ticks, patch-set, patch x y, display, table:make, table:put, table:get, patch-at, first, last