Skip to content

Commit

Permalink
Add basic Population constructor, Population.speciate tests, fix seve…
Browse files Browse the repository at this point in the history
…ral reference bugs
  • Loading branch information
christianechevarria committed Jan 13, 2020
1 parent caa73b6 commit 5612c53
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
10 changes: 7 additions & 3 deletions src/architecture/population.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _ = require('lodash');
const Network = require('./network');
const Species = require('./species')
const methods = require('../methods/methods');
const config = require('../config');

Expand Down Expand Up @@ -69,22 +70,25 @@ const Population = function(options) {
* @function getPopulation
* @memberof Population
*
* @param {Object} config A configuration object to control function behavior
* @param {number} size The number of networks to place in the returned array
*
* @return {Network[]} Returns an array of networks. Must be assigned to Population.members if replacing current population.
*
* @todo Add template network functionality
* @todo Add option to have identical population networks
*/
self.getPopulation = function create_networks(options={}) {
self.getPopulation = function create_networks(config={}) {
const population = []
for (let i = 0; i < size; i++) {
for (let i = 0; i < config.size; i++) {
population.push(new Network(self.inputs, self.outputs, { connIds: self.connIds, nodeIds: self.nodeIds }));
}

return population
};

// Fill the members array on population construction
self.members = self.getPopulation(self.size);
self.members = self.getPopulation({ size: self.size });

/**
* Applies the mutation-scheme dictated by Neat to the provided network.
Expand Down
16 changes: 7 additions & 9 deletions src/architecture/species.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Network = require('./architecture/network');
const methods = require('./methods/methods');
const config = require('./config');
const _ = require('../util/util')
const Network = require('./network');
const methods = require('../methods/methods');
const config = require('../config');
const _ = require('../util/utils')

/**
* Creates a new Species
Expand All @@ -10,9 +10,7 @@ const _ = require('../util/util')
*
* @param { Network } genome The first network to add to the species' members array
* @param {object} options
* @param {number} options.excessCoeff
* @param {number} options.disjointCoeff
* @param {number} options.weightsCoeff
* @param {number[]} options.weights An array of weights for calculating compatability (distance) with the species. From NEAT paper.
*
* @prop { Network[] } members An array of species member networks / genomes
* @prop { Network } bestGenome The best network, by fitness, in the species
Expand All @@ -34,7 +32,7 @@ const _ = require('../util/util')
*
* console.log(mySpecies.members) // [ myNetwork ]
*/
const Species = function(genome, options) {
const Species = function(genome, options={}) {
const self = this;

// NEAT designated props
Expand All @@ -45,7 +43,7 @@ const Species = function(genome, options) {
self.stagnation = 0; // Generation count without improvement

// Adjustable props - compatibility
self.coefficient = options.coefficient || [1,1,0.4];
self.weights = options.weights || [1,1,0.4];
self.threshold = 3; // Threshold for determining whether or not a genome is compatible with the species

// Species metadata
Expand Down
1 change: 1 addition & 0 deletions src/carrot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Carrot = {
Node: require('./architecture/node'),
Neat: require('./neat'),
Population: require('./architecture/population'),
Species: require('./architecture/species'),
GAN: require('./gan'),
multi: require('./multithreading/multi'),
};
Expand Down
2 changes: 1 addition & 1 deletion test/units/architecture/network.1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const _ = require('lodash');
const chai = require('chai');
const { assert, expect } = chai;
const should = chai.should();
chai.use(require('chai-things')); // support for array tests using should
chai.use(require('chai-things')); // support for array-based assertions
const {
Network,
methods,
Expand Down
27 changes: 23 additions & 4 deletions test/units/architecture/population.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
const { assert, expect } = require('chai');
const should = require('chai').should();
const chai = require('chai');
const { assert, expect } = chai;
const should = chai.should();
chai.use(require('chai-things')); // support for array-based assertions
const chalk = require('chalk');

const { Population, Network, methods } = require('../../../src/carrot');
const { Population, Species, Network, methods } = require('../../../src/carrot');

describe("Population", () => {})
describe("Population", () => {
describe("new Population()", function () {
it("new Population() => {Network}", function () {
const population = new Population();

expect(population).instanceOf(Population);
})
})

describe(".speciate()", function () {
it("(pop.members, pop.species) => {Species[]}", function () {
const population = new Population();

// ".all" checks an assertion against all members of an array
expect(population.speciate(population.members, population.species)).all.instanceOf(Species)
})
})
})

0 comments on commit 5612c53

Please sign in to comment.