License: MIT · Version 2.2.0 · npm install @webwriter/evolution-sim
Explore evolution and natural selection through interactive simulations.
<webwriter-evolution-sim> lets learners watch natural selection and mutation in a small population of creatures, from lower secondary school to introductory university biology. It is a single, self-contained widget: write the scenario, prediction prompts and tasks around it as ordinary text or task widgets, as the Island Evolution snippet does. Each widget runs its own simulation, so several on one page do not affect each other; to compare, for example, two mutation rates, give them the same seed and counts.
These rules and numbers are fixed; only the values in the data block can be configured.
- Creatures (labelled Agents in the widget) and food sources (drawn as berry bushes) stand on the tiles of a rectangular grid. Each tick, a creature eats the food source it stands on or walks one tile toward the nearest food source ready to eat within its vision range, or in a random direction if it sees none. A step costs 1 energy; a creature without energy dies.
- Eating gives up to 15 energy, never more than the creature's energy capacity. The food source then needs 10 ticks to regrow.
- Once a creature has at least 30 energy, it produces one child per tick for 20 energy each until it has less than 20 left (a parent left with 0 dies), so a creature whose energy capacity is below 30 never reproduces. Reproduction is asexual: the child copies its parent's alleles, each of which may change by +1 or -1 (see
mutationRate). Every creature starts with half its energy capacity. - There are two genes, each with exactly one allele, which is the gene's value: Vision Range (1-10 tiles; every initial creature has 4) and Energy Capacity (1-100; every initial creature has 30).
Run and pause the simulation (Start, Pause), advance it by one tick (Step), speed it up (one button that cycles from 0.25x to 5x) and return to its start (Reset); start a new one with New; place or remove food sources and creatures; change the mutation rate during the run unless it is locked; click a creature to see its genes (Genetics tab) and its parent and children (Ancestry tab); and switch to the charts with Simulation / Graphs. The placing and removing tools cannot be locked.
- The run, its history and the charts are not saved. A reload starts again from the configuration in the data block.
- No chart shows mean gene values; the
historysignal and the.csvexport have them.
Each widget is a custom element with a script and a stylesheet. From a CDN:
<link href="https://cdn.jsdelivr.net/npm/@webwriter/evolution-sim/dist/widgets/webwriter-evolution-sim.css" rel="stylesheet">
<script type="module" src="https://cdn.jsdelivr.net/npm/@webwriter/evolution-sim/dist/widgets/webwriter-evolution-sim.js"></script>
<webwriter-evolution-sim></webwriter-evolution-sim>With a bundler, run npm install @webwriter/evolution-sim and import @webwriter/evolution-sim/widgets/webwriter-evolution-sim.js.
Agent-based simulation of natural selection: creatures on a grid search for food, reproduce asexually and pass two mutating genes (vision range, energy capacity) to their children. Learners run, step and reset it, place or remove creatures and food, inspect a creature's genes and ancestry, and chart the population, food and allele shares. It has no attributes and no authored content: configure it only through its data block, and put the scenario and tasks around it as text and task widgets.
Data block: The starting world, the mutation rate, the values learners may change, and the charts.
JSON Schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://cdn.jsdelivr.net/npm/@webwriter/evolution-sim@2/schemas/configuration.schema.json",
"title": "Evolution simulation configuration",
"description": "The configuration of a <webwriter-evolution-sim>: the starting world, the mutation rate, which values learners may change, and which charts exist. The .json import and export use the same format. Write configuration with initOptions and graphs: without one of them the widget ignores the block and uses its defaults (seed \"\", 50 × 25 tiles, 5 creatures, 20 food sources, mutation rate 0.3, nothing locked, all charts on); a missing initOptions entry takes its default and is unlocked. The widget does not range-check the block (a count above the number of tiles stops the simulation from starting); the .json import rejects a file that breaks this schema. The configuration dialog in editing mode and the learners' New dialog write the block.",
"type": "object",
"required": [
"configuration"
],
"properties": {
"configuration": {
"description": "The simulation settings.",
"type": "object",
"required": [
"initOptions",
"graphs"
],
"properties": {
"initOptions": {
"description": "The values a new simulation starts from. The starting world (where creatures and food stand) depends only on seed, worldSize, initialAgentsAmount and initialFoodSourcesAmount: every learner, reload and widget with the same four values starts from the same world, and Reset returns to it and, without learner input, replays the same run. To give a class one shared world, lock these four.",
"type": "object",
"required": [
"seed",
"worldSize",
"initialAgentsAmount",
"initialFoodSourcesAmount"
],
"properties": {
"seed": {
"description": "Seed of the random numbers for the starting world and the run.",
"type": "object",
"required": [
"locked",
"value"
],
"properties": {
"locked": {
"$ref": "#/$defs/locked"
},
"value": {
"description": "Any string; \"\" is a fixed seed too.",
"type": "string",
"examples": [
"island"
]
}
}
},
"worldSize": {
"description": "Size of the grid in tiles.",
"type": "object",
"required": [
"locked",
"value"
],
"properties": {
"locked": {
"$ref": "#/$defs/locked"
},
"value": {
"description": "Width and height in tiles.",
"type": "object",
"required": [
"x",
"y"
],
"properties": {
"x": {
"description": "Width in tiles.",
"type": "integer",
"minimum": 1
},
"y": {
"description": "Height in tiles.",
"type": "integer",
"minimum": 1
}
}
}
}
},
"initialAgentsAmount": {
"description": "Creatures at the start, each on its own random tile.",
"type": "object",
"required": [
"locked",
"value"
],
"properties": {
"locked": {
"$ref": "#/$defs/locked"
},
"value": {
"description": "From 0 to the number of tiles, worldSize x × y.",
"type": "integer",
"minimum": 0
}
}
},
"initialFoodSourcesAmount": {
"description": "Food sources at the start, each on its own random tile (a creature may share it).",
"type": "object",
"required": [
"locked",
"value"
],
"properties": {
"locked": {
"$ref": "#/$defs/locked"
},
"value": {
"description": "From 0 to the number of tiles, worldSize x × y.",
"type": "integer",
"minimum": 0
}
}
},
"mutationRate": {
"description": "Optional; 0.3 when left out. Unless locked, learners can change it during the run without a reset; Reset restores this value.",
"type": "object",
"required": [
"locked",
"value"
],
"properties": {
"locked": {
"$ref": "#/$defs/locked"
},
"value": {
"description": "Probability from 0 to 1 that each gene of a child changes by +1 or -1 against its parent's allele: 0 makes every child an exact copy.",
"type": "number",
"minimum": 0,
"maximum": 1
}
}
}
}
},
"graphs": {
"description": "The Graphs view and its charts. Write all five keys: a missing one counts as false (the .json import rejects it).",
"type": "object",
"required": [
"enabled",
"showPopulationGraph",
"showFoodAvailabilityGraph",
"showAlleleShareGraph",
"showAlleleHistoricShareGraph"
],
"properties": {
"enabled": {
"description": "Whether there is a Graphs view at all, reached with the Simulation / Graphs switch. When false, the other keys have no effect.",
"type": "boolean"
},
"showPopulationGraph": {
"description": "The chart \"Population development\": number of creatures per tick.",
"type": "boolean"
},
"showFoodAvailabilityGraph": {
"description": "The chart \"Food availability\": share of food sources ready to eat per tick.",
"type": "boolean"
},
"showAlleleHistoricShareGraph": {
"description": "The chart \"Gene distribution over time\": the share (0 to 1) of living creatures with each allele of one gene, as lines over the ticks. Learners pick the gene in the chart's Gene menu (Vision Range first, or Energy Capacity); it never shows both at once.",
"type": "boolean"
},
"showAlleleShareGraph": {
"description": "The chart \"Current gene distribution\": the same shares for the current tick, as bars, with its own Gene menu.",
"type": "boolean"
}
}
}
}
}
},
"$defs": {
"locked": {
"description": "Each value has its own lock. true disables this value in the learners' New dialog, where it stays visible; for mutationRate it also disables the mutation-rate control during the run. Locks do not affect Reset or the tools that place and remove creatures and food.",
"type": "boolean"
}
}
}Signals:
population(number): Number of creatures alive in the current tick; 0 before the simulation has started. Not stored in the document.history(table): The run so far: one row for the start, each tick, and each creature or food source that a learner places or removes (with the same tick), in the columnstick,population,food_sources,available_food_sources(ready to eat),mean_vision_rangeandmean_energy_capacity(null while no creature is alive). It starts again with every new or reset simulation and is not stored in the document; thetext/csvexport holds the same table.
Properties:
signals: The published signals, asstatic signalsdeclares them.
Events:
configuration-change: Fired when the configuration is changed in the configuration dialog or when a new simulation is created with New.detailholds the new configuration.
Methods:
configureSimulation(): Opens the configuration dialog, in editing mode only (the editor's "Configure simulation" action).importConfiguration(file): Reads a.jsonfile in the format of the data block ({"configuration": {...}}) and returns a function that stores it in the data block. Throws if the file is not a complete configuration with values in range.exportConfiguration(): Returns the current configuration as the fileevolution-simulation.json({"configuration": {...}}).exportHistoryCsv(): Returns the history table of the current simulation as the fileevolution-history.csv.
Editing config: importFormats: .json · exportFormats: text/csv, .json · label: Evolution Sim
Markup from the package's snippets: the most complete example of each widget variant, and one from each snippet. … marks a data URL shortened to its media type. Copy the structure and attribute encoding, then replace the content.
Island Evolution: <webwriter-evolution-sim>
<webwriter-evolution-sim id="ww-8f3c2a91-5d4e-4b7a-9c61-2e0f7b3d4a15"><script type="application/json" slot="data">{"configuration":{"initOptions":{"seed":{"locked":false,"value":"island"},"initialAgentsAmount":{"locked":true,"value":5},"initialFoodSourcesAmount":{"locked":false,"value":25},"mutationRate":{"locked":false,"value":0.3},"worldSize":{"locked":true,"value":{"x":50,"y":25}}},"graphs":{"enabled":true,"showPopulationGraph":true,"showFoodAvailabilityGraph":true,"showAlleleShareGraph":true,"showAlleleHistoricShareGraph":true}}}</script></webwriter-evolution-sim>Examples and templates that WebWriter inserts as they are: Island Evolution (@webwriter/evolution-sim/snippets/island-evolution.html).
Attributes configure a widget in HTML; properties, methods and events are its JavaScript API. Signals are values a widget publishes; a widget with an id publishes them as #id[signal], which another widget's effect attribute reads. Editing config describes how WebWriter treats the widget.
Generated with @webwriter/build@1.16.0