UserPreferences

MicroprojectsPage


1. Microproject 5, due by 1:00 pm, Thurs., Feb. 27

Write two cellular-automaton models in Netlogo, or one CA model in Python. Choose from the following:

Note: some of these CAs have Netlogo code readily available on the web and in other places. Please don't consult already-written Netlogo models that have exactly the same purpose as the one you're working on. That is, I don't mind if you look up something in a termites-carrying-woodchips model if you're working on the forest fire or the snowflake, but please do not look up a spatial Prisoner's Dilemma model if you're working on the SpatialEvolutionaryPrisonersDilemmaCellularAutomaton.

2. Microproject 4, due by 1:00 pm, Thurs., Feb. 20

This week's microproject is relatively open-ended. Build a set of simulations (in Python or Netlogo, but Netlogo will be easier) to explore how a diffusion process is affected by spatial heterogeneity.

For example, a population of racoons might grow in its spatial extent by a process accurately modeled as diffusion with exponential growth. But what if the space through which the population is growing consists of patches that differ in hospitability to racoons? Does a racoon population spread as quickly or evenly through a checkerboard region of good and bad habitat as it does through a region that is uniformly intermediate in habitat quality? And what effect to barriers have? And corridors through poor habitat? These sorts of questions can all be addressed through diffusion simulations.

General strategy:

  1. Imagine the desired user experience and design the interface. Make the space biggish with smallish cells. (I don't care about the actual numbers, as long as the model's behavior is clear.

  2. Decide how many types of habitat (i.e., cells with different properties) you want and how good or bad each one is.

    1. Maybe you want two types, and you want them to be opposites: passable? is true or false, say.

    2. Maybe you want subtler differences: passability as some number, say.

    3. Maybe you want an infinite gradation of quality values, so you can make it a function of location.

    4. Maybe you want the value to be dynamic: turtles or time can change the value.

  3. Create a space with the desired mix and configuration of habitat types.

  4. Design the turtle (representing the racoon -- sorry for the terminological menagerie).

    1. Design the turtle's appearance. (That is, the color and shape. you can change the shape if you want -- look up how at Netlogo's documentation.)

    2. Do you need any custom variables or breeds? Probably not.

  5. Write the dynamic.

    1. Every time step, have each turtle change direction randomly and move some number of steps.

      1. Maybe you want the number of steps moved to be dependent on habitat type.

      2. Maybe you don't want this, but probably you do.

    2. Also every time step, have each turtle reproduce with some probability.

  6. Define a time variable so you can measure speeds, etc.

  7. Position the original population in the space.

  8. Run and describe the simulation's behavior.

Write three Netlogo simulations and save them under different names. Use them to answer three of these vague questions about diffusion through heterogeneous spaces:

  1. Does diffusion through a checkerboard space (2 habitat types, 1 good 1 bad) happen as quickly as through a uniform space in which every cell is halfway between the two types of checkerboard cells in habitat quality?

  2. What about a random space in which the average habitat quality is the same as the quality of every cell in the uniform space?

  3. What about a gradient, in which quality varies monotonically, possibly smoothly, in one direction?

  4. How is diffusion affected by an object through which diffusion can't happen, so the diffusion has to go around? Imagine a wall that extends partway across the space.

  5. If there's an impassable region (consisting of cells through which diffusion is impossible), how is speed of diffusion dependent on the width and/or habitat quality of a corridor of passable cells through the impassable region?

  6. What is the effect of spatial scale on the impact of obstacles? Spatial scale might be changed, for example, by changing how many cells a turtle moves in each time step.

Upload your three Netlogo source files, and under the "Information" tab of each please tell me what answer the simulation gives to one of these questions.

How do you describe the behavior of turtles in the simulation? You'll have to be creative. What you'll probably end up doing is stepping through the time steps one step at a time, and observing the spatial extent or shape of the population at each step. How many time steps before the population's diameter grows to six screen inches, for example.

How do you do specific experiments in the simulation? Again, you'll have to be creative. One thing you might try doing is placing your entire population in a particular location. The main piece of information you might be missing (but probably not, if you've looked at the [WWW]Netlogo documentation site) is how to set a turtle's spatial coordinates, or refer to a patch's coordinates. That is, how are cells' locations represented? Here it is: the center patch has pxcor = 0 and pycor = 0. Any turtle on that patch has xcor = 0 and ycor = 0. the rightmost patches have pxcor equal to screen-edge-x, and the leftmost patches have pxcor equal to -1 * screen-edge-x. Similarly for the uppermost and lowermost cells, but this time with pycor equal to screen-edge-y or its negative. (By the way, that means that the space is 1 + screen-edge-x * 2) wide, and 1 + screen-edge-y * 2 tall.) Note that screen-edge-x and screen-edge-y are set in the interface by editing the black rectangle. So, how do you place all the turtles someplace? Easy: ask turtles [set xcor screen-edge-x set ycor 0], for example.

How do you make a particular configuration of patch types? Good question. The main way is to describe the coordinates of the cells you want to change. For example, the cells in the 21 middle locations that are 3 cells below the center line would be changed like this:

ask patches [ifelse abs pxcor < 10 and pycor < -3 [set passable? true][set passable? false]]

How do you set random patches? Easy: use the random primitive to do a sort of "probability coin-flip." You could set the probability in the procedures, but it might be nice to have the user choose a probability using a slider.

How do you make a one-dimensional gradient? I won't tell you straight out, but i'll just suggest this: make some patch variable's value some linear (or other) function of (pxcor - screen-edge-x. Why do you have to add screen-edge-x? It's just a way of dealing with the fact that Netlogo calls the very center of the space (0,0), instead of calling some corner (0,0).

And how do you use color? the netlogo documentation treats Netlogo's color functions much more clearly than I can. Netlogo does color very well, and it gives you a lot of power in visualizing the behavior of your simulation.

If you want it, here is code for funky spaces:

For graphpaper space (squares of red separated by rows and columns of black):

ask patches [ifelse (pxcor mod 2 = 0 and pycor mod 2 = 0) [set passable true][set passable false]]

For checkerboard space:

ask patches [ifelse ((screen-edge-x + pxcor) mod 2 = (screen-edge-y + pycor) mod 2) [set passable true][set passable false]]

For anisotropic passability:

ask patches [ifelse (pycor mod 2 = 0 or pxcor mod 10 = 0) [set passable? true][set passable? false]]

3. Microproject 3, due by 1:00 pm, Thurs., Feb. 13

Modify the coalescence program in Microproject 2 so that it does the following:

  1. Asks the user how many lengths to test.

  2. Reads from a file what the lengths are. For example, the user says to test 10 lengths. The program opens a file and which has ten different lengths, each on a separate line.

  3. For each length, it runs the simulation 100 times and finds the average coalescence time for each starting sequence length, the average of the 100 runs.

  4. Reports the average lengths for each starting sequence length. The report is an output file, and on each line (separated by \n) there are two numbers: the length, then a tab (represented by \t, then the avg coalescence time.

4. Microproject 2, due by 1:00 pm, Thurs., Feb. 13

4.1. General description

The opposite of branching is coalescence. Rivers, raindrop trails on windowpanes, and cracks in solid materials exhibit coalescence, and some branching processes -- notably the evolutionary diversification of populations -- are analyzed in reverse by modeling them as backward coalescent projects. (For example, the geographic origin of the "mitochondrial Eve" was estimated using a coalescent model to analyze genetic differences among populations of modern humans.)

You will build a very simple abstract model of the coalescence of random numbers. The objective is to see what relationship there is (if any) between the lengths of the sequences of numbers and their times to coalescence.

You'll start with one sequence of random integers. The length of the sequence is inputted by the user. The model will run through many time steps. In each time step, every number will (a) increase by one, (b) decrease by one, or (c) not change at all. The three possibilities occur with equal probability. When two adjacent numbers are identical, those two numbers will coalesce and behave as one number, thus decreasing the length of the sequence by one. When the sequence becomes just one number long, the whole thing has coalesced. The number of time steps is the time to coalescence.

I'll tell you how to generate random numbers, and I'll suggest a coalescence algorithm and a few other implementation details.

4.2. Detailed instructions

  1. Represent the sequence as a list.

  2. To generate random integers, do this:

    1. At the top of your program, include this line: import random

    2. When you want a random number, use random.random()

    3. For example, x = random.random()

    4. random.random() returns a random floating-point number between 0.0 and 1.0, not including the endpoints.

    5. To make the number an integer on the interval [a,b], use floor(a+random.random()*(b-a+1))

      1. a+random.random()*(b-a+1) gives floating-point numbers between a and (b + .999)

      2. int(num) gives the largest integer no greater than num.

      3. It probably makes sense to define some function ran_int(a,b) that does all that.

    6. To decide whether or not to do something based on some probability prob, use something like if ran_float(0,1) < prob:, where ran_float(a,b) returns a random floating-point number between a and b. (I think something like answer=a+random.random()*(b-a), but verify it for yourself.

  3. Define a variable called time or something like that. Set it initially to zero and increment it (increase it by one) every time step.

  4. The coalescence algorithm: in every time step, do this:

    1. Is the sequence just one number long?

      1. Yes: output the coalescence time and end program.

      2. No: repeat the following until yes.

        1. For every number in the sequence,

          1. With probability 0.33, nothing happens to the number.

          2. If the number doesn't change, then with probability of .5,

            1. Increase the number by 1

            2. Otherwise, decrease it by 1

          3. Note that if you apply the 50% probability only to numbers that do change (with 67% probability), then each possibility has 33% probability. The probability of going up by one is 67% * 50% = 33%. Same for the probability of decreasing by one.

          4. If the number is smaller than 0, make it 0. If larger than 10, make it 10.

        2. After the numbers are all changed, have each number (except the last one) compare itself to the next one in the sequence. If they're identical, remove one.

When the program finishes, have it output the sequence length and the coalescence time.

Optional features (in order of increasing difficulty):

4.3. Advice

This assignment is much more of a challenge than last week's was. You have the necessary knowledge, but you might not yet be used to thinking like a programmer. That's okay: getting used to that kind of thinking is exactly the point of this exercise.

The best programmers approach something like this by breaking it into logical pieces. Write the program as lots of little functions, and test each function thoroughly by first putting it into some little test program, just to make sure all the outputs come out as expected.

This assignment will probably take a long time, so start early. Comment liberally, because you may well put it aside for a day, come back, and have to re-figure out what you were thinking.

And if you're having lots of trouble, please try to see me before Wednesday afternoon. This program is complicated enough that (a) seeing me so late might not give you enough time to finish the assignment, and (b) I won't be able to help everyone all in one afternoon.

Good luck, and enjoy!

5. Microproject 1, due by 1:00 pm, Thurs., Jan. 30

Write a Python program that does the following:

A. Ask the user whether she wants option 1 or option 2.

B. Execute the appropriate option:

  1. Option 1

    1. Ask the user to enter a positive integer.

    2. If the user enters a negative integer, make it positive.

    3. Calculate the factorial of the integer and output it in a sentence.

  2. Option 2

    1. Ask the user to enter a positive integer.

    2. If the user enters a negative integer, make it positive.

    3. Calculate 2 to the power of the inputted integer.

    4. If the integer is 0, the answer should be 1.

    5. Output the answer in a sentence.

Hint: this assignment is all about flow control. It might help you to draw out a diagram of the tasks involved -- a flow chart. Also, if you need help and don't have a reference book handy, there are lots of easy-to-use Python resources at www.python.org.