1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
'use strict';
import { random_choice, apply, shuffle } from '../util.js';
import { mut_genome_insert, parse_genome } from '../genome/genome.js';
import { world_update } from '../world/world.js';
import { senses } from './senses.js';
import { actions } from './actions.js';
import { lattice_rules } from './lattice_rules.js';
import { validity } from './validity.js';
import { postprocess } from './postprocess.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)
);
}
export 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
// games
// }
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;
let agent_id = 0;
export function create_agent(genome, n_internal) {
return {
id: agent_id++, // !!!! side effect !!!!
net: parse_genome(N_INPUT, N_OUTPUT, genome),
state: [...Array(n_internal)].map(_ => (2*Math.random()) - 1),
}
}
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, []],
).slice(-1)[0];
console.log(N_INPUT, N_OUTPUT, genome);
const agents = [...Array(size)].map(_ => create_agent(genome, n_internal));
return { agents, genome, score: 0, games: 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, games: 0 };
}
// game structure:
// {
// world: world
// team_indices: number[]
// time: number
// }
export function create_game(teams, team_indices) {
const world = create_world(100, team_indices.map(i => teams[i].agents));
return { world, team_indices, time: 0 };
}
export function step_game(game) {
return {
...game,
world: world_update(game.world, postprocess),
time: game.time + 1,
};
}
function score(lattice, team_num) {
const size = lattice.length;
// count number of flags in the team's area
return lattice
.map((row, y) => row.map((cell, x) => [x, y, cell]))
.flat()
.filter(([x, y, cell]) => get_team(size, x, y) === team_num && cell.type === 'flag')
.length;
}
export function finish_game(teams, game) {
const scores = [0, 1, 2, 3].map(t => score(game.world.lattice, t));
return game.team_indices.reduce(
(acc, idx, i) => {
const team = teams[idx];
acc.splice(idx, 1, {...team, score: team.score + scores[i], games: team.games+1});
return acc;
},
teams,
);
}
// epoch structure
// {
// game
// time
// teams
// }
function random_indices(teams) {
return [...Array(teams.length - 4)].reduce(
(acc) => {
const idx = Math.floor(Math.random() * acc.length);
acc.splice(idx, 1);
return acc;
},
[...teams.keys()]
);
}
let epoch_num = 0;
export function create_epoch(teams) {
return {
game: create_game(teams, random_indices(teams)),
time: 0,
epoch: epoch_num++, // !!!! side effects !!!!
teams,
}
}
const GAME_STEPS = 5000
const EPOCH_STEPS = 200
export function update_epoch(epoch) {
if (epoch.game.time < GAME_STEPS) {
return { ...epoch, game: step_game(epoch.game) };
} else if (epoch.time < EPOCH_STEPS) {
return {
...epoch,
teams: finish_game(epoch.teams, epoch.game),
game: create_game(epoch.teams, random_indices(epoch.teams)),
time: epoch.time+1
};
} else {
// epoch complete!!
const source_teams = epoch.teams
.map(team => {
const normalized_score = team.score/team.games;
const count = Math.ceil(16*normalized_score);
return [...Array(count > 0 ? count : 1)].map(x => team)
})
.flat();
const new_teams = [...Array(epoch.teams.length)]
.reduce((acc) => child_team(random_choice(source_teams)), []);
return create_epoch(new_teams);
}
}
|