'use strict'; import { random_choice, apply } from '../util.js'; import { senses } from './senses.js'; import { actions } from './actions.js'; import { lattice_rules } from './lattice_rules.js'; import { validity } from './validity.js'; function is_wall(size, x, y) { return ( x === 0 || x === size-1 || y === 0 || y === size-1 ); } function is_corner(size, x, y) { const subsize = Math.floor(size/3); return ( (x < subsize || x >= 2*subsize) && (y < subsize || y >= 2*subsize) ); } function get_team(size, x, y) { const subsize = Math.floor(size/3); if (y < subsize) { return 0; } else if (x >= 2*subsize) { return 1; } else if (y >= 2*subsize) { return 2; } else if (x < subsize) { return 3; } else { return undefined; } } export function setup_board(size) { const lattice = [...Array(size)] .map(() => [...Array(size)]) .map((row, y) => row.map((_, x) => { if (is_wall(size, x, y)) { return { type: 'immutable', flags: {} }; } else if (is_corner(size, x, y)) { return { type: 'immutable', flags: {} }; } else { const team = get_team(size, x, y); return { type: 'empty', flags: { team } }; } })); return lattice; } export function create_world(size, teams) { const lattice = setup_board(size); const agents = teams.reduce( (agents, team, team_num) => { const team_cells = lattice.map((row, y) => row.map((cell, x) => [x, y, cell])).flat() // only check cells with the right team .filter(([x, y, cell]) => cell.type === 'empty' && cell.flags.team === team_num) return agents.concat(team.reduce( (acc, agent) => { const available_cells = team_cells.filter(([x, y, cell]) => acc.reduce( (occupied, a) => occupied && ((a.x !== x) || (a.y !== y)), true )) const [x, y, ..._] = random_choice(available_cells); const orientation = random_choice([ 'n', 'e', 's', 'w' ]); return [...acc, {...agent, x, y, flags: { ...agent.flags, orientation } }]; }, [] )); }, [] ).flat(); return { lattice, lattice_rules, agents, actions, senses, validity }; }; // team structure: // { // agents // genome // score // } let agent_id = 0; export function create_agent(genome, n_internal) { return { id: agent_id++, // !!!! side effect !!!! net: parse_genome, state: [...Array(n_internal)].map(_ => (2*Math.random()) - 1), } } const N_INPUT = senses.reduce((acc, sense) => acc + sense.size, 0); const N_OUTPUT = actions.reduce((acc, action) => acc + action.size, 0); const MAX_MUTATIONS = 15; export function create_team(size, genome_size, n_internal) { const [..._, genome] = apply( s => mut_genome_insert(s, 4, Math.random(), Math.random(), Math.random()), genome_size, [N_INPUT, n_internal, N_OUTPUT, []], ); const agents = [...Array(size)].map(_ => create_agent(genome, n_internal)); return { agents, genome, score: 0 }; } export function child_team(team, keep=Math.floor(team.size/2)) { const n_internal = get_size(team.genome) - N_INPUT - N_OUTPUT; const genome = apply( s => mutate_genome(s, 4), Math.floor(MAX_MUTATIONS * Math.random()), [N_INPUT, n_internal, N_OUTPUT, team.genome] ); const old_agents = [...Array(team.agents.length - keep)].reduce( (acc, _) => { const idx = Math.floor(Math.random() * acc.length); acc.splice(idx, 1); return acc; }, ); const new_agents = [...Array(team.agents.length - keep)].map(_ => create_agent(genome, n_internal)); const agents = [old_agents, new_agents].flat(); return { agents, genome, score: 0 }; } // game structure: // { // world: world // team_indices: number[] // time: number // } export function create_game(teams, team_indices) { }