| none |
kiln.agents.Population |
Agents kept as numpy arrays, one row per agent: pos, vel, a stable id, a kind and your own fields. Population(n, dim=2, kinds=("agent",), intent=(), **fields). |
| none |
kiln.agents.Population.pos |
(n, dim) positions. |
| none |
kiln.agents.Population.vel |
(n, dim) velocities, in world units per second. |
| none |
kiln.agents.Population.id |
(n,) stable ids, read-only: an agent keeps its id until it dies, and an id is never reused. |
| none |
kiln.agents.Population.kind |
(n,) index of each agent’s kind in kinds. |
| none |
kiln.agents.Population.kinds |
The kind names, a tuple. |
| none |
kiln.agents.Population.intent |
The names of the intent fields: the ones set from outside the simulation with write. |
| none |
kiln.agents.Population.dim |
The number of dimensions. |
| none |
kiln.agents.Population.senses |
What the agents perceived at the last sense; None before the first and after a spawn or kill. |
| none |
kiln.agents.Population.sense |
pop.sense(radius, *, fov=None) finds every agent’s neighbours once per simulation step and fills pop.senses; fov (radians) limits them to a view cone around each heading. |
| none |
kiln.agents.Population.spawn |
pop.spawn(k, kind=None, **values) adds k agents at the end, with new ids; returns their indices. |
| none |
kiln.agents.Population.kill |
pop.kill(mask) removes agents (a boolean mask or indices) and closes the gaps, keeping the survivors in order. |
| none |
kiln.agents.Population.index_of |
Turns stable ids into current indices; -1 for an agent that has died. |
| none |
kiln.agents.Population.write |
pop.write(ids, **intent) sets intent fields from any thread, through kiln.call_soon; they land at the start of the next frame. |
| none |
kiln.agents.Population.intent_age |
pop.intent_age(field) is the seconds, on Kiln’s clock, since each agent’s last write of an intent field; inf if never. |
| none |
kiln.agents.Population.add_field |
pop.add_field(name, value) adds a field after construction, taking the array as it is (no copy); spawn and kill keep it in step from then on. |
| none |
kiln.agents.Population.has_field |
pop.has_field(name) is whether name is a field (pos, vel, kind or the sketch’s own), not a plain attribute. |
| none |
kiln.agents.Population.step |
pop.step(dt, max_speed) caps each speed (one limit, or one per agent) and moves every agent: pos += vel * dt. |
| none |
kiln.agents.Senses |
What every agent perceives within a radius: neighbour count, nearest neighbour, nearest of each kind. |
| none |
kiln.agents.Senses.radius |
The radius the neighbours were sensed within. |
| none |
kiln.agents.Senses.fov |
The width of the view cone in radians, or None for all around. |
| none |
kiln.agents.Senses.count |
(n,) how many neighbours each agent has. |
| none |
kiln.agents.Senses.nearest |
(n,) index of each agent’s nearest neighbour; -1 for none. |
| none |
kiln.agents.Senses.nearest_dist |
(n,) distance to the nearest neighbour; inf for none. |
| none |
kiln.agents.Senses.nearest_of |
nearest_of(kind) returns (index, distance) of each agent’s nearest neighbour of one kind. |
| none |
kiln.agents.seek |
Steering force toward a target at full speed. |
| none |
kiln.agents.arrive |
Steering force toward a target that slows down to stop on it. |
| none |
kiln.agents.flee |
Steering force straight away from a threat. |
| none |
kiln.agents.wander |
A random sideways push, so agents meander; takes a generator, so a seed repeats. |
| none |
kiln.agents.separate |
Pushes neighbours apart, harder the closer they are. |
| none |
kiln.agents.align |
Steers toward the mean velocity of the neighbours. |
| none |
kiln.agents.cohere |
Steers toward the centre of the neighbours. |
| none |
kiln.agents.follow |
Steering force along a FlowField, at max_speed where the flow is 1 long or more and slower where it is weaker. |
| none |
kiln.agents.pursue |
Steering force toward where a moving target will be: pursue(pop, target, target_vel, max_speed, *, max_lead=None). |
| none |
kiln.agents.evade |
Steering force away from where a moving threat will be, within a radius: evade(pop, threat, threat_vel, max_speed, radius=inf, *, max_lead=None). |
| none |
kiln.agents.follow_path |
Steering force along a path of points, keeping within radius of it; closed=True loops it. |
| none |
kiln.agents.contain |
Steering force that turns agents back from the walls of a box, harder the nearer the wall, within a margin. |
| none |
kiln.agents.avoid |
Steering force round the round obstacles ahead, looking look_ahead seconds along the velocity: avoid(pop, centers, radii, *, look_ahead=1.0). |
| none |
kiln.agents.Field |
Amounts on a grid over the world (food, pheromone, heat) that agents deposit, sample and take; wraps around its edges or is walled in. Field(shape, extent, *, diffusion=0, decay=0, regrowth=0, cap=None, edge=FieldEdge.WRAP). |
| none |
kiln.agents.Field.values |
(nx, ny) amount in each cell; edit it in place. |
| none |
kiln.agents.Field.shape |
(nx, ny), the number of cells across and down. |
| none |
kiln.agents.Field.extent |
(width, height) of the world the grid covers. |
| none |
kiln.agents.Field.cell |
The width and height of one cell, in world units. |
| none |
kiln.agents.Field.edge |
What happens at the edges of the world, a FieldEdge. |
| none |
kiln.agents.Field.diffusion |
How fast it spreads, in world units squared per second. |
| none |
kiln.agents.Field.decay |
How fast it fades, per second. |
| none |
kiln.agents.Field.regrowth |
How fast it grows back toward cap, per second. |
| none |
kiln.agents.Field.cap |
The most a cell holds: one number, an (nx, ny) array with one per cell (a fertility map; barren cells may be 0), or None. |
| none |
kiln.agents.Field.deposit |
field.deposit(pos, amount) adds amounts, split bilinearly between the four nearest cells. |
| none |
kiln.agents.Field.sample |
field.sample(pos) is the field at each position, interpolated bilinearly. |
| none |
kiln.agents.Field.gradient |
field.gradient(pos) is the (n, 2) slope at each position: which way it rises. |
| none |
kiln.agents.Field.take |
field.take(pos, amount) removes up to amount per agent from its cell; never more than is there. |
| none |
kiln.agents.Field.step |
field.step(dt) diffuses, decays and regrows the field over dt seconds. |
| none |
kiln.agents.Field.draw |
field.draw(x, y, w, h, *, scale=None) draws it as a picture in the current colour, as opaque as each cell is full. |
| none |
kiln.agents.FieldEdge |
What a Field or FlowField does at the edges of its world. |
| none |
kiln.agents.FieldEdge.WRAP |
The edges wrap round like a torus, and so do positions outside the world. The default. |
| none |
kiln.agents.FieldEdge.CLOSED |
The edges are walls: nothing diffuses through them, and positions outside the world move to the nearest point inside. |
| none |
kiln.agents.FlowField |
A direction on a grid over the world, for follow. FlowField(shape, extent, *, edge=FieldEdge.WRAP). |
| none |
kiln.agents.FlowField.vectors |
(nx, ny, 2) flow in each cell; edit it in place. |
| none |
kiln.agents.FlowField.shape |
(nx, ny), the number of cells across and down. |
| none |
kiln.agents.FlowField.extent |
(width, height) of the world the grid covers. |
| none |
kiln.agents.FlowField.cell |
The width and height of one cell, in world units. |
| none |
kiln.agents.FlowField.edge |
What happens at the edges of the world, a FieldEdge. |
| none |
kiln.agents.FlowField.fill_noise |
flow.fill_noise(scale, t=0) fills it, in place, with unit vectors turning smoothly with signed_noise. |
| none |
kiln.agents.FlowField.sample |
flow.sample(pos) is the flow at each position, interpolated bilinearly. |
| none |
kiln.agents.Genome |
Genes in [0, 1] for every agent, a genome field of the population that follows spawn and kill, mapped to traits through ranges. Genome(pop, genes, *, weight_count=0), with genes={"size": (2, 9), ...}; genome["speed"] is one gene, a writable view of its column, in [0, 1]. |
| none |
kiln.agents.Genome.pop |
The population whose agents carry the genes. |
| none |
kiln.agents.Genome.names |
The named genes, in column order. |
| none |
kiln.agents.Genome.ranges |
Each named gene’s trait range, {name: (lo, hi)}. |
| none |
kiln.agents.Genome.gene_count |
Genes per agent: the named ones plus the weights. |
| none |
kiln.agents.Genome.weight_count |
How many genes follow the named ones, kept for a brain’s weights. |
| none |
kiln.agents.Genome.weights |
(n, weight_count) view of the genes after the named ones, for a brain. |
| none |
kiln.agents.Genome.trait |
genome.trait(name) is every agent’s trait, lo + gene * (hi - lo). |
| none |
kiln.agents.Genome.randomize |
genome.randomize(rng) gives every agent fresh uniform genes. |
| none |
kiln.agents.Crossover |
How reproduce mixes a child’s genes from its parent and a mate. |
| none |
kiln.agents.Crossover.UNIFORM |
Each gene from one parent or the other, at even odds. |
| none |
kiln.agents.metabolize |
metabolize(pop, dt, cost) takes the cost of living out of every agent’s energy: energy -= cost * dt. |
| none |
kiln.agents.reproduce |
reproduce(pop, mask, *, rng, share=0.5, mutation=0.05, sigma=0.1, crossover=None, mates=None, spread=0) gives each picked agent a child with a share of its energy and its genes, mutated (and crossed with a mate’s: its nearest of its kind at the last sense, or one per agent given in mates); energy is conserved. Returns the children’s indices. |
| none |
kiln.agents.die |
die(pop, mask, *, leave=None, amount=1.0) kills agents and deposits their bodies in a field; returns where they died. |
| none |
kiln.agents.Brains |
The shape of a small neural network every agent runs, its weights per agent from genes in [0, 1] (2 g - 1, so in [-1, 1]). Brains(layers=(16, 16, 4), activation=Activation.TANH, *, output=Activation.TANH). |
| none |
kiln.agents.Brains.layers |
The layer sizes, inputs first and outputs last. |
| none |
kiln.agents.Brains.activation |
The hidden layers’ Activation. |
| none |
kiln.agents.Brains.output |
The output layer’s Activation. |
| none |
kiln.agents.Brains.weight_count |
Genes each agent’s network needs: its weights and biases. Pass it to Genome(..., weight_count=). |
| none |
kiln.agents.Brains.forward |
brains.forward(weights, inputs, *, dtype=np.float64) runs every agent’s network at once, one batched matrix product per layer: (n, weight_count) genes and (n, layers[0]) inputs give (n, layers[-1]) outputs. dtype=np.float32 is about twice as fast, to within 1e-5; cast the genes to float32 once a generation. |
| none |
kiln.agents.Activation |
What a layer of a Brains does to its sums. |
| none |
kiln.agents.Activation.TANH |
tanh, in (-1, 1). The default. |
| none |
kiln.agents.Activation.RELU |
max(0, x). |
| none |
kiln.agents.Activation.SIGMOID |
1 / (1 + exp(-x)), in (0, 1). |
| none |
kiln.agents.Activation.LINEAR |
The sums as they are. |
| none |
kiln.agents.rays |
rays(pop, k, fov, reach, *, kinds=(), fields=(), radii=1.0, who=None) casts k rays per agent across fov radians about its heading: per kind, the closeness of the first body hit, 1 - distance / reach (0 for none, 1 touching); per field, the value at each tip, which follows the field’s edge. who= is a boolean mask of the agents that cast rays. Returns (n, k * (len(kinds) + len(fields))), one row per agent cast from. |
| none |
kiln.agents.ray_directions |
ray_directions(pop, k, fov, *, who=None) gives the (n, k, 2) unit directions rays casts along, to draw the rays an agent sensed with. |
| none |
kiln.agents.drive |
drive(pop, outputs, dt, *, max_speed, max_turn) turns two outputs per agent in [-1, 1] into velocities: the first turns the heading, the second sets the speed. The heading lives in the velocity, so zeroing vel resets it to +x. |
| none |
kiln.agents.next_generation |
next_generation(genomes, fitness, rng, *, elite=0.05, selection=Selection.TOURNAMENT, crossover=Crossover.UNIFORM, mutation=0.03, sigma=0.15, tournament_size=3) breeds the next generation’s genes from this one’s scores: the elite kept, the rest children of selected parents, crossed and mutated. |
| none |
kiln.agents.Selection |
How next_generation picks each parent. |
| none |
kiln.agents.Selection.TOURNAMENT |
The fittest of tournament_size agents drawn at random. The default. |
| none |
kiln.agents.Selection.ROULETTE |
An agent drawn with odds in proportion to its fitness above the lowest. |
| none |
kiln.agents.Status |
FAILURE, SUCCESS and RUNNING: what a tree node returns for an agent. |
| none |
kiln.agents.Sequence |
A tree node that runs its children in order while they succeed. memory=True resumes at the running child. |
| none |
kiln.agents.Selector |
A tree node that tries its children in priority order until one succeeds or runs. |
| none |
kiln.agents.leaf |
Decorator: makes a tree leaf from fn(pop, idx), which acts on the agents idx and returns a Status per agent. halt= runs when a higher branch cuts the leaf off. |
| none |
kiln.agents.condition |
Decorator: makes a tree leaf from a test fn(pop, idx) that returns booleans: SUCCESS or FAILURE. |
| none |
kiln.agents.is_kind |
A condition that succeeds for the agents of one kind: branches a tree by species. |
| none |
kiln.agents.Tree |
A behaviour tree that every agent of a population runs, ticked all at once; follows the population’s ids through kill and spawn. |
| none |
kiln.agents.Tree.pop |
The population the tree ticks. |
| none |
kiln.agents.Tree.root |
The root node. |
| none |
kiln.agents.Tree.tick |
Ticks the tree once for every agent; returns the root’s status per agent. |
| none |
kiln.agents.Tree.nodes |
The nodes in pre-order as (depth, name) pairs. |
| none |
kiln.agents.Tree.counts |
(len(nodes), 3) how many agents each node returned FAILURE, SUCCESS and RUNNING for at the last tick. |
| none |
kiln.agents.Tree.draw |
tree.draw(x, y) draws the tree on screen with its live counts: a panel in the look of Gui. |