summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/simulation/game.js69
1 files changed, 61 insertions, 8 deletions
diff --git a/src/simulation/game.js b/src/simulation/game.js
index f0c4815..ac6b2fd 100644
--- a/src/simulation/game.js
+++ b/src/simulation/game.js
@@ -85,14 +85,67 @@ export function create_world(size, teams) {
// {
// agents
// genome
+// score
// }
-//export function create_team(size, genome_size, n_internal) {
-// const n_input = senses.reduce((acc, sense) => acc + sense.size, 0);
-// const n_output = senses.reduce((acc, sense) => acc + sense.size, 0);
-// const genome = apply(
-// s => mut_genome_insert(
-// ),
-// );
-//}
+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) {
+}