agentscape
Version:
Agentscape is a library for creating agent-based simulations. It provides a simple API for defining agents and their behavior, and for defining the environment in which the agents interact. Agentscape is designed to be flexible and extensible, allowing
88 lines (69 loc) • 2.36 kB
text/typescript
import { test as base, expect } from 'vitest'
import { Agent, Cell } from '.'
import { CellGrid } from '../structures'
class TestAgent extends Agent {
constructor(opts) {
super(opts)
}
public act() {
return
}
}
interface TestFixture {
agent: Agent
grid: CellGrid<Cell>
}
const test = base.extend<TestFixture>({
agent: new TestAgent({
initialPosition: [0, 0],
randomSeed: 0,
}),
grid: CellGrid.default(10)
})
test('It should be initialized with default values', ({ agent }) => {
expect(agent.energy).toBeCloseTo(1)
expect(agent.rotation.asRadians()).toBeCloseTo(0)
expect(agent.sightRange).toBe(10)
expect(agent.fov.asDegrees()).toBe(90)
expect(agent.traitMutationRate).toBe(0.01)
expect(agent.traitCrossoverRate).toBe(0.5)
expect(agent.color.toHex()).toBe('#00ff')
})
test('It should move to a neighboring cell', ({ agent, grid }) => {
expect(agent.previousPosition.components).toEqual([0, 0])
agent.moveTo(grid, [0, 1])
expect(agent.previousPosition.components).toEqual([0, 0])
expect(agent.position.components).toEqual([0, 1])
expect(agent.energy).toBeCloseTo(1)
expect(agent.rotation.asDegrees()).toBeCloseTo(90)
})
test('It should move a specified distance', ({ agent, grid }) => {
agent.move(grid, 1)
expect(agent.previousPosition.components).toEqual([0, 1])
expect(agent.position.components[0]).toBeCloseTo(0)
expect(agent.position.components[1]).toBeCloseTo(2)
expect(agent.energy).toBeCloseTo(1)
expect(agent.rotation.asDegrees()).toBeCloseTo(90)
})
test('It should rotate to face a given cell', ({ agent, grid }) => {
expect(agent.rotation.asDegrees()).toBeCloseTo(90)
const cell = grid.getCell([1,1])!
agent.faceCell(cell)
expect(agent.rotation.asDegrees()).toBeCloseTo(-45)
})
test('It should get neighboring cells', ({ agent, grid}) => {
agent.moveTo(grid, [5, 5])
const neighbors = agent.getNeighborCells(grid)
const expectedNeighbors = [
[4,5],
[6,5],
[5,4],
[5,6],
]
expect(neighbors.map(cell => cell.location)).toEqual(expectedNeighbors)
})
// test('It should eat to regain energy', ({ agent }) => {
// expect(agent.energy).toBeCloseTo(1)
// agent.eat()
// expect(agent.energy).toBeCloseTo(2)
// })