diff options
| author | sanine-a <sanine.not@pm.me> | 2023-08-10 15:39:56 -0500 | 
|---|---|---|
| committer | sanine-a <sanine.not@pm.me> | 2023-08-10 15:39:56 -0500 | 
| commit | 1ad77cbdadb6e8c389c128b0f95907505e1b22ca (patch) | |
| tree | 5d147fed3c2c7f69801a14017061fd433df89292 /src/genome | |
| parent | 49312fea07e56e2d74179f4160a16cac6f614f97 (diff) | |
refactor: is_valid -> get_size()
Diffstat (limited to 'src/genome')
| -rw-r--r-- | src/genome/genome.js | 17 | ||||
| -rw-r--r-- | src/genome/genome.test.js | 17 | 
2 files changed, 21 insertions, 13 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;
    }
  }
 diff --git a/src/genome/genome.test.js b/src/genome/genome.test.js index f50531b..0ad8d80 100644 --- a/src/genome/genome.test.js +++ b/src/genome/genome.test.js @@ -2,7 +2,7 @@  import { 
    mutation_type, mutate,
 -  is_valid,
 +  get_size,
  } from './genome';
 @@ -23,9 +23,14 @@ test('basic gene mutations', () => {  });
 -test('genome validation', () => {
 -  expect(is_valid(0, 0, [ [ 0, 0, 1.0 ] ])).toBe(true);
 -  expect(is_valid(2, 1, [ [ 0, 2, 1 ] ])).toBe(true);
 -  expect(is_valid(2, 1, [ [ 0, 1, 1 ] ])).toBe(false);
 -  expect(is_valid(2, 1, [ [ 0, 2, 5 ] ])).toBe(false);
 +test('genome validation and size', () => {
 +  expect(get_size(0, 0, [ [ 0, 0, 1.0 ] ])).toBe(1);
 +  expect(get_size(2, 1, [ [ 0, 2, 1 ] ])).toBe(3);
 +  expect(get_size(2, 1, [ [ 0, 1, 1 ] ])).toBe(-1);
 +  expect(get_size(2, 1, [ [ 0, 2, 5 ] ])).toBe(-1);
 +});
 +
 +
 +test('parse a genome into a neural net', () => {
 +  
  });
  | 
