summaryrefslogtreecommitdiff
path: root/src/simulation
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-11-12 05:17:19 -0600
committersanine <sanine.not@pm.me>2023-11-12 05:17:19 -0600
commit444b2b5abfbb70473b0785b38eaba1df4197ae69 (patch)
tree98eeb63082c679f8944c740f466c1e56aef24aad /src/simulation
parente3a042a7a5c041dad63255965576346377e4f823 (diff)
refactor genome to include size information
Diffstat (limited to 'src/simulation')
-rw-r--r--src/simulation/game.js22
-rw-r--r--src/simulation/senses.js3
-rw-r--r--src/simulation/trial.js12
3 files changed, 27 insertions, 10 deletions
diff --git a/src/simulation/game.js b/src/simulation/game.js
index 20c5e4e..f95fbc4 100644
--- a/src/simulation/game.js
+++ b/src/simulation/game.js
@@ -2,10 +2,13 @@
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) {
@@ -89,22 +92,22 @@ export function create_world(size, teams) {
// 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,
+ net: parse_genome(N_INPUT, N_OUTPUT, 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(
@@ -112,6 +115,7 @@ export function create_team(size, genome_size, n_internal) {
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 };
@@ -149,7 +153,7 @@ export function child_team(team, keep=Math.floor(team.size/2)) {
export function create_game(teams, team_indices) {
- const world = create_world(999, team_indices.map(i => teams[i].agents));
+ const world = create_world(100, team_indices.map(i => teams[i].agents));
return { world, team_indices, time: 0 };
}
@@ -157,7 +161,7 @@ export function create_game(teams, team_indices) {
export function step_game(game) {
return {
...game,
- world: world_update(world, postprocess),
+ world: world_update(game.world, postprocess),
time: game.time + 1,
};
}
@@ -202,7 +206,7 @@ function random_indices(teams) {
acc.splice(idx, 1);
return acc;
},
- teams.keys()
+ [...teams.keys()]
);
}
@@ -239,7 +243,7 @@ export function update_epoch(epoch) {
return [...Array(count > 0 ? count : 1)].map(x => team)
})
.flat();
- const new_teams = [...Array(epoch.teams.length]
+ const new_teams = [...Array(epoch.teams.length)]
.reduce((acc) => child_team(random_choice(source_teams)), []);
return create_epoch(new_teams);
}
diff --git a/src/simulation/senses.js b/src/simulation/senses.js
index 23775b6..ca07442 100644
--- a/src/simulation/senses.js
+++ b/src/simulation/senses.js
@@ -157,7 +157,7 @@ const see = {
const [x, y] = vision_idx_to_world_pos(world, agent, idx);
return see_cell(world, x, y);
});
- return world.agents.reduce(
+ const result = world.agents.reduce(
(acc, a) => {
const idx = world_pos_to_vision_idx(world, agent, a.x, a.y);
if (idx < 0 || idx > VIS_WIDTH*VIS_HEIGHT) {
@@ -169,6 +169,7 @@ const see = {
},
vision
);
+ return result;
},
};
diff --git a/src/simulation/trial.js b/src/simulation/trial.js
new file mode 100644
index 0000000..3dedf01
--- /dev/null
+++ b/src/simulation/trial.js
@@ -0,0 +1,12 @@
+'use strict';
+
+import { create_team, create_epoch, update_epoch } from './game.js';
+
+const start_teams = [...Array(50)].map(x => create_team(32, 5, 5));
+
+let epoch = create_epoch(start_teams);
+
+while (epoch.epoch < 1) {
+ console.log('update');
+ epoch = update_epoch(epoch);
+}