Code Revision Flashcards

(20 cards)

1
Q

Graphing and start of CA

A

REFER TO SLIDES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Game of Life Explanation

A
  1. Start the program.
  2. Create a grid of cells, each of which can be either alive or dead.
  3. Initialize the grid with some starting pattern of live and dead cells.
  4. Repeat the following steps for each generation:
    a. Create an empty grid for the next generation.
    b. For each cell in the current grid:
    * Count how many of its eight neighboring cells are alive.
    * Apply the following rules:
    o If the cell is alive and has fewer than two live neighbors, it dies (underpopulation).
    o If the cell is alive and has two or three live neighbors, it stays alive (survival).
    o If the cell is alive and has more than three live neighbors, it dies (overpopulation).
    o If the cell is dead and has exactly three live neighbors, it becomes alive (reproduction).
    c. After all cells have been processed, replace the current grid with the new grid.
  5. Display the new grid.
  6. Repeat the process until the user stops the simulation or a maximum number of generations has been reached.
  7. End the program.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Schellings Model - Model of Segregation Explanation

A
  1. Initialise a grid randomly with 3 possible states ie Type A, Type B, and empty
  2. Determine neighbourhood situation for each cell
  3. Assess whether happy or unhappy by comparing neighbourhood ratio with some defined model parameter
  4. Find unhappy agents
  5. Find empty locations
  6. Select (randomly) an unhappy agent and move it to a randomly selected empty location
  7. Update neighbourhood, agent states, empty locations
  8. Repeat the process
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When quantified as a graph, what does schellings model show?

A

The y-axis (Segregation) shows how clustered the agents are — specifically, the average fraction of each agent’s neighbours who are the same type (or “colour”) as that agent.
- A value of 1.0 means perfect segregation: everyone’s neighbours are the same type.
- A value of 0.5 would mean a completely mixed neighbourhood (roughly half same-type, half other).
- The x-axis (Time steps) shows how the system evolves over time as unhappy agents move.
===
Each line corresponds to a different tolerance threshold (p) — the minimum fraction of same-type neighbours needed for an agent to feel happy.

For example:
- p = 0.3: agents are tolerant; they’re fine even if only 30% of neighbours are the same.
- p = 0.5 or 0.7: agents are less tolerant; they need more same-type neighbours to be happy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you add randomness to the schelling model

A

Besides the initial randomness that comes with model, that being random distribution of housing and empty spaces, we can also have randomly chosing of empty spaces, and agents being updated randomly as well rather than in order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

SugarScape Model Explanation

A

The Sugarscape World (environment):
- Each cell in the 2D grid has a capacity (maximum amount of sugar it can hold).
- There are two high-sugar regions, with capacity 4 (peaks), surrounded by concentric rings with capacities 3, 2, and 1.

The Agents:
- 400 agents are placed at random locations. Each agent has their randomly-chosen attributes:
- Sugar: how much sugar they start with;
- Metabolism: how much sugar they consume per time step;
- Vision: how far they can see (radius of k).

During each time step, agents move one at a time in a random order. Each agent follows these rules:
- The agent surveys k cells in each of the 4 compass directions, where k is the range of the agent’s vision.
- It chooses the unoccupied cell with the most sugar. In case of a tie, it chooses the closer cell; among cells at the same distance, it chooses randomly.
- The agent moves to the selected cell and harvests the sugar, adding the harvest to its accumulated wealth and leaving the cell empty.
- The agent consumes some part of its wealth, depending on its metabolism. If the resulting total is negative, the agent “starves” and is removed.

World (environment) update rules:
- After all agents have executed these steps, the cells grow back some sugar, typically 1 unit, but the total sugar in each cell is bounded by its capacity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When quantified as a graph, what does SugarScape model show?

A

What the graph shows
- Y-axis: Number of agents still alive.
- X-axis: Simulation time steps.
- The curve drops over time, meaning agents are gradually dying off (starving).
===
At the start, 400 agents are placed randomly across the landscape.
- Agents in low-sugar regions or with high metabolism and low vision can’t find enough sugar to survive.
- They consume sugar faster than they collect it, so their sugar store drops below zero → they die.

As time goes on:
- The number of living agents declines until the population stabilizes at a smaller number that the environment can sustain.
- Agents with better vision or who were lucky to start near sugar peaks tend to survive longer.
===
Why the population stabilizes
- Sugar grows back slowly (1 unit per step) and has a maximum capacity in each cell.
- This limits the total available sugar in the environment.
- Once a sustainable balance is reached between sugar production and agent consumption, the population levels off — the remaining agents can gather enough sugar to survive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What other factors could be included in the SugarScape model?

A

Agent’s age can increase at each time step

Agents have finite lifespan

New agent can be added to the environment if an existing agent died, to make the population unchanged.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When quantified as a graph, what does SugarScape model show when we give the agents infinite lifespans?

A

What the graph represents
- X-axis: Wealth (the amount of sugar an agent has).
- Y-axis: Cumulative Distribution Function (CDF) of wealth.
- The CDF shows the proportion of agents whose wealth is less than or equal to a given amount.
===
When agents have finite lifespans (they die after a certain age) and new agents are added, the system reaches a stationary wealth distribution — meaning the overall shape of the wealth distribution becomes stable over time, even though individual agents come and go.
- Some agents accumulate high wealth because they start in good locations (near sugar peaks) or have low metabolism and high vision.
- Most agents have low to moderate wealth, reflected by the steep initial rise in the CDF.
-A smaller number of agents become very rich, causing the tail of the curve to flatten out near the top.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you add/see randomness in the sugarscape model?

A

Random initial placements of agents
Random agent attributes
Random movement tie breaking: when multiple unoccupied cells have the same maximum sugar level, the agent then randomly chooses one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Traffic Jam Model Explanation

A
  • Initialize 1D array with equal distance between cars
  • Determine the speed limit on this highway
  • Determine the next car for each car
  • Setup the initial speed for each car
  • Cars will move in order on this highway
  • For the movement of each car:
    • Determine the distance that each car can move
    • Choose the acceleration rate for each driver
    • Compute the speed of each car (with randomness involved)
    • Update the new location for each car
  • If speed > distance from next car, crash happens, and the car will stop with speed set to zero
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When quantifing driver behaviour as a graph, what does traffic jam model show?

A

What the graph represents
- X-axis: Number of cars on the highway (traffic density).
- Y-axis: Average speed of all cars.
- Lines: Different levels of driver “noise” (eps), which introduces randomness into how each car accelerates or reacts.
===
What the results show
As the number of cars increases, the average speed decreases sharply.
- With few cars, everyone can travel near the speed limit.
- As traffic becomes dense, spacing shrinks, so cars slow down to avoid crashes.

Higher noise values (larger eps) lead to lower overall average speeds.
- Noise represents variability or unpredictability in driver behavior.
- When drivers react inconsistently, stop-and-go waves and mini-jams appear more often, reducing average speed even further.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is randomness showning the traffic jam model

A

Cars are often randomly distributed around the circular track.
Each car may start with a random initial speed within allowed limits.

You can add randomly chosen acceleration rates and slow down rates, and alter update order

REFER TO SLIDES FOR CODE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Boids Model Explanation

A
  • Initialize number of boids with random velocity and random positions.
  • Define boids wihtin range in neighbourhood, that will be allowed to move
  • Boids movement rules:
    • Find center: Finds other boids within range (radius and angle of the field of view) and computes a vector toward their centroid.
    • Avoid collision: Finds objects, including other boids (and carrots), within a given range, and computes a vector that points away from their centroid.
    • Align velocity: Finds other boids within range and computes the average of their headings.
    • Love (find carrot): Computes a vector that points toward the carrot.
    • Compute weighted average of each movement rule, so each rule has a different impact
  • Update final velocity and position to complete movement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is randomness and emergance seen in boids?

A

Random is inherently random in bird placement, that allows for the emergance of flocking

You can add other forms of randomness such as having different weights for the attributes or adding jitter, can also update movement randomly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Basic Machinery for Modelling Evolution

A

How to represent the genotype?
○ A vector of numbers of zeros and ones for example
How to setup the fitness value? How to relate it to different genotypes?
Which agents will die over generations? How to choose them?
How many agents will be reproduced? Which agents will be reproduced?
○ Close proximity
○ Sexual or asexual, etc

17
Q

Evolution Model Explanation - Fitness landscape

A
  • Define a fitness landscape:
    • A genotype is a length-N bit string.
    • Fitness of a genotype is computed from per-locus contributions (e.g., mean of locus values drawn at init).
  • Initialise a population of agents with random bit strings; compute their fitness.
  • For each generation:
    • Selection (survival): map fitness to probability of survival and remove unlucky individuals.
    • Reproduction: pick survivors as parents (weighted by fitness); copy their genome to offspring.
    • Mutation: with small probability per bit, flip bits in offspring.
    • Replace the population with the new offspring.
    • (Optional) Track metrics (mean fitness, diversity, mean Hamming distance).
  • Repeat for the desired number of generations or until metrics stabilise.
18
Q

Evolution of Cooperation - Prisoners Dilemma

A
  • Encode strategies (agents):
    • Each agent has a policy table mapping the opponent’s recent move history (e.g., last 0/1/2 moves) to the next action C or D.
  • Tournament (fitness assignment):
    • Pair agents (e.g., round-robin neighbours).
    • For each pair, play an iterated PD for a fixed number of rounds:
      ○ In each round, both choose C/D based on their policy and observed history.
      ○ Payoffs by standard matrix: (C,C)=3/3, (C,D)=0/5, (D,C)=5/0, (D,D)=1/1.
    • Fitness of each agent = average payoff across its matches.
  • Evolutionary update (per generation):
    • Survival: convert fitness to probability of survival; remove others.
    • Reproduction: select survivors proportional to fitness to create offspring (copy their policy table).
    • Mutation: with small probability, flip entries in the policy table (C↔D).
  • Repeat tournament + update for many generations; record prevalence of strategies (e.g., TFT, All-D, Grim).
19
Q

Percolation (site percolation with wetting) Model

A
  1. Create a grid where each cell is porous with probability q (state 1) or blocked with probability 1−q (state 0).
  2. Wet the top row (mark those porous cells as wet, state 5).
  3. Repeat until no change:
    ○ For every porous cell that has at least one wet neighbour (N/E/S/W), make it wet.
  4. Measure percolation: if any bottom-row cell is wet, a percolating cluster exists.

Notes: Estimate the percolation probability by repeating steps 1–4 many times and counting successes.

20
Q

Sandpile Model

A
  • Create a grid of integer “sand heights”. Optionally start with a uniform level.
  • Repeat:
    • Add one grain to a random cell (increase its height by 1).
    • While any cell exceeds the toppling threshold K (typically 3 for 4-neighbour model):
      • Topple every over-threshold cell simultaneously:
      • Decrease that cell’s height by 4.
      • Increase each of its four von Neumann neighbours by 1 (use chosen boundary rule; usually wrap).
    • Record avalanche stats (number of topples, duration).
  • Stop after a set number of drops, or once you’ve collected enough avalanche data.