summaryrefslogtreecommitdiff
path: root/src/genome/genome.js
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-08-10 15:39:56 -0500
committersanine-a <sanine.not@pm.me>2023-08-10 15:39:56 -0500
commit1ad77cbdadb6e8c389c128b0f95907505e1b22ca (patch)
tree5d147fed3c2c7f69801a14017061fd433df89292 /src/genome/genome.js
parent49312fea07e56e2d74179f4160a16cac6f614f97 (diff)
refactor: is_valid -> get_size()
Diffstat (limited to 'src/genome/genome.js')
-rw-r--r--src/genome/genome.js17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/genome/genome.js b/src/genome/genome.js
index 3445fe9..71e18c1 100644
--- a/src/genome/genome.js
+++ b/src/genome/genome.js
@@ -9,7 +9,8 @@ export const mutation_type = Object.freeze({
});
-function positive(x) {
+// clamp a number in the range [0, infinity)
+function nonneg(x) {
if (x < 0) {
return 0;
} else {
@@ -18,6 +19,7 @@ function positive(x) {
}
+// mutate a gene
export function mutate(gene, type, value) {
const [ source, sink, weight ] = gene;
@@ -27,14 +29,14 @@ export function mutate(gene, type, value) {
case mutation_type.source:
if (value <= 0.5) {
- return [ positive(source-1), sink, weight ];
+ return [ nonneg(source-1), sink, weight ];
} else {
return [ source+1, sink, weight ];
}
case mutation_type.sink:
if (value <= 0.5) {
- return [ source, positive(sink-1), weight ];
+ return [ source, nonneg(sink-1), weight ];
} else {
return [ source, sink+1, weight ];
}
@@ -49,7 +51,8 @@ export function mutate(gene, type, value) {
}
-export function is_valid(num_input, num_output, genome) {
+// check if a given genome is valid and compute its size
+export function get_size(num_input, num_output, genome) {
const [ max_index, max_weight ] = genome.reduce(
([max_index, max_weight ], [ source, sink, weight]) => [
Math.max(max_index, source, sink),
@@ -59,12 +62,12 @@ export function is_valid(num_input, num_output, genome) {
);
if (max_index < num_input + num_output - 1) {
- return false;
+ return -1;
}
else if (max_weight > 4.0) {
- return false;
+ return -1;
}
else {
- return true;
+ return max_index + 1;
}
}