Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ language: node_js
node_js:
- "6"
- "6.1"
- "5.11"
- "iojs"
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
./node_modules/.bin/mocha --reporter spec

.PHONY: test
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Genetic algorithm framework built with JavaScript ES6

[![Build Status](https://travis-ci.org/acupy/genetic-algorithm-es6.svg?branch=master)](https://travis-ci.org/acupy/genetic-algorithm-es6)

[![NPM](https://nodei.co/npm/genetic-algorithm-fw.png?downloads=true)](https://npmjs.org/package/genetic-algorithm-fw)
[![Build Status](https://travis-ci.org/nitin42/genetic-algorithm-es6.svg?branch=master)](https://travis-ci.org/nitin42/genetic-algorithm-es6)
![coverage] (https://img.shields.io/badge/coverage-52.78%25-yellowgreen.svg)

This package provides a framework for building applications where genetic algorithm (GA) is used for solving optimization problems based on a natural selection process that mimics biological evolution.

Expand Down
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"scripts": {
"build": "babel src --presets babel-preset-es2015 --out-dir dist",
"prepublish": "npm run build"
"prepublish": "npm run build",
"test": "mocha --require babel-polyfill --compilers js:babel-register"
},
"keywords": [
"genetic",
Expand All @@ -27,6 +28,18 @@
"readmeFilename": "README.md",
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-preset-es2015": "^6.18.0"
"babel-core": "^6.18.2",
"babel-istanbul": "^0.11.0",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-latest": "^6.16.0",
"babel-register": "^6.18.0",
"chai": "^3.5.0",
"mocha": "^3.2.0",
"should": "^11.1.1"
},
"dependencies": {
"babel-istanbul": "^0.11.0",
"istanbul": "^0.4.5"
}
}
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default class {
module.exports = class GeneticClass {
constructor(
mutationFunction = (phenotype) => phenotype,
crossoverFunction = (a, b) => [a, b],
Expand All @@ -15,6 +15,15 @@ export default class {
this.population = population;
this.populationSize = populationSize;
this.chanceOfMutation = chanceOfMutation;

this.populate = this.populate.bind(this);
this.mutate = this.mutate.bind(this);
this.crossover = this.crossover.bind(this);
this.isABetterThanB = this.isABetterThanB.bind(this);
this.compete = this.compete.bind(this);
this.mixPopulationOrder = this.mixPopulationOrder.bind(this);
this.evolve = this.evolve.bind(this);
this.best = this.best.bind(this);
}

populate () {
Expand Down Expand Up @@ -101,3 +110,4 @@ export default class {
return theBest;
}
}

107 changes: 107 additions & 0 deletions test/src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict';

const chai = require('chai');
const should = require('should');

let GeneticClass = require('../src/index.js');

let ga = new GeneticClass();

const expect = chai.expect;

describe('Testing the GeneticClass', () => {
it('object should be instance of class', () => {
expect(ga).to.be.an.instanceof(GeneticClass);
});

it('Class should be ok', () => {
expect(GeneticClass).to.be.ok;
});

it('instance should be ok', () => {
expect(GeneticClass).to.be.ok;
});

it('class should exists', () => {
expect(GeneticClass).to.exist;
});

it('class should have populate method and not throw Error', () => {
expect(ga.populate).to.exist;
expect(ga.populate).to.not.throw(Error);
});


it('class should have mutate method and not throw Error', () => {
expect(ga.mutate).to.exist;
expect(ga.mutate).to.not.throw(Error);
});

it('class should have crossover method and not throw Error', () => {
expect(ga.crossover).to.exist;
expect(ga.crossover).to.not.throw(Error);
});

it('class should have comparison method and not throw Error', () => {
expect(ga.isABetterThanB).to.exist;
expect(ga.isABetterThanB).to.not.throw(Error);
});

it('class should have compete method and not throw Error', () => {
expect(ga.compete).to.exist;
expect(ga.compete).to.not.throw(Error);
});

it('class should have mixPopulation method and not throw Error', () => {
expect(ga.mixPopulationOrder).to.exist;
expect(ga.mixPopulationOrder).to.not.throw(Error);
});

it('class should have evolve method and not throw Error', () => {
expect(ga.evolve).to.exist;
expect(ga.evolve).to.not.throw(Error);
});

it('class should have best method and not throw Error', () => {
expect(ga.best).to.exist;
expect(ga.best).to.not.throw(Error);
});

it('class should respond to its method', () => {
expect(GeneticClass).to.respondTo('populate');
});

it('class should respond to its method', () => {
expect(GeneticClass).to.respondTo('crossover');
});

it('class should respond to its method', () => {
expect(GeneticClass).to.respondTo('mutate');
});

it('class should respond to its method', () => {
expect(GeneticClass).to.respondTo('isABetterThanB');
});

it('class should respond to its method', () => {
expect(GeneticClass).to.respondTo('compete');
});

it('class should respond to its method', () => {
expect(GeneticClass).to.respondTo('mixPopulationOrder');
});

it('class should respond to its method', () => {
expect(GeneticClass).to.respondTo('evolve');
});

it('class should respond to its method', () => {
expect(GeneticClass).to.respondTo('best');
});

it('some methods should expect arguments', () => {
ga.mutate(expect(arguments).to.be.arguments);
ga.crossover(expect(arguments).to.be.arguments);
ga.isABetterThanB(expect(arguments).to.be.arguments);
});
})