New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

chromosome-js

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chromosome-js - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0

56

dist/chromosome-js.cjs.development.js

@@ -74,2 +74,6 @@ 'use strict';

var pmxCrossover = function pmxCrossover(genome1, genome2) {
if (!(Array.isArray(genome1) && Array.isArray(genome2))) {
throw TypeError("You cant use PMX with object genomes");
}
var mapOffspring1 = {};

@@ -82,6 +86,2 @@ var mapOffspring2 = {};

if (!(Array.isArray(genome1) && Array.isArray(genome2))) {
throw TypeError("You cant use PMX with object genomes");
}
var offspring = [Array.from(genome1), Array.from(genome2)];

@@ -119,3 +119,50 @@

};
/* Cycle crossover operator */
var cycleCrossover = function cycleCrossover(genome1, genome2) {
if (!(Array.isArray(genome1) && Array.isArray(genome2))) {
throw TypeError("You cant use Cycle Crossover with object genomes");
}
var cycleSet = new Set();
var cycles = [];
var i = 0; // Find cycles
while (cycleSet.size !== genome1.length) {
if (!cycleSet.has(genome1[i])) {
(function () {
var pos = i;
var val = null;
var cycle1 = {};
var cycle2 = {};
while (genome1[i] != val) {
cycleSet.add(genome1[pos]);
cycle1[pos] = genome1[pos];
cycle2[pos] = genome2[pos];
val = genome2[pos];
pos = genome1.findIndex(function (value) {
return value === val;
});
}
cycles.push([cycle1, cycle2]);
})();
}
i++;
} // Mix cycles
var offspring = cycles.reduce(function (_ref, cycle, index) {
var first = _ref[0],
second = _ref[1];
return (// (index + 0) % 2 and (index + 1) % 2 is a way of alternating values between 0 and 1
// where for index 0: 0, 1; 1: 1, 0; 2: 0, 1...
[Object.assign(first, cycle[index % 2]), Object.assign(second, cycle[(index + 1) % 2])]
);
}, [{}, {}]);
return offspring.map(Object.values);
};
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {

@@ -1194,2 +1241,3 @@ try {

exports.byFitness = byFitness;
exports.cycleCrossover = cycleCrossover;
exports.flipMutation = flipMutation;

@@ -1196,0 +1244,0 @@ exports.generateIndividual = generateIndividual;

2

dist/chromosome-js.cjs.production.min.js

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(t,r){if(!r&&0!==r)return Math.round(Math.random()*t);var n=r-t;return Math.round(Math.random()*n)+t},r=function(t){return function(r){var n=r.map(function(t){return function(r){return[r.slice(0,t),r.slice(t)]}}(t));return[[].concat(n[0][0],n[1][1]),[].concat(n[1][0],n[0][1])]}},n=function(r,n){if(r<1)throw new RangeError("The number of fix points should be at least 1");for(var e=[t(r-1)],o=1;o<n;o++)e.push(t(e[o-1]+1,r-1));return e},e=function(t){return function(e,o){var i=[e,o];Array.isArray(e)||(i=i.map(Object.entries));for(var a=n(i[0].length,t),u=0;u<a.length;u++)i=r(a[u])(i);return Array.isArray(e)?i:i.map(Object.fromEntries)}},o=e(2),i=e(1);function a(t,r,n,e,o,i,a){try{var u=t[i](a),s=u.value}catch(t){return void n(t)}u.done?r(s):Promise.resolve(s).then(e,o)}function u(t){return function(){var r=this,n=arguments;return new Promise((function(e,o){var i=t.apply(r,n);function u(t){a(i,e,o,u,s,"next",t)}function s(t){a(i,e,o,u,s,"throw",t)}u(void 0)}))}}var s,c=function(t,r){return Array.isArray(r)?r.map(t):Object.keys(r).reduce((function(n,e){return n[e]=t(r[e],e,r),n}),{})},f=function(t,r){if(!t)throw TypeError("Individual needs a genotype");if(!r)throw TypeError("Individual needs fitness function");if("function"==typeof t){var n=t();return{genome:n,fitness:r(n)}}var e=c((function(t){return t()}),t);return{genome:e,fitness:r(e)}},h=function(t){return function(r){return{genome:r,fitness:t(r)}}},l=function(t,r){return t.fitness<r.fitness?1:-1},p=function(t,r,n){for(var e=new Array(n),o=0;o<n;o++)e[o]=f(t,r);return e.sort(l)},v=function(t,r){for(var n=new Array(r),e=0;e<r;e++)n[e]=t();return n.sort(l)},d=function(t,r){if(t>r.length)throw new RangeError("You cannot select more individuals than the population size")},y=function(r,n){return d(r,n),Array.from({length:r},(function(){return n[t(n.length-1)]}))},g=(function(t){var r=function(t){var r=Object.prototype,n=r.hasOwnProperty,e="function"==typeof Symbol?Symbol:{},o=e.iterator||"@@iterator",i=e.asyncIterator||"@@asyncIterator",a=e.toStringTag||"@@toStringTag";function u(t,r,n){return Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,n){return t[r]=n}}function s(t,r,n,e){var o=Object.create((r&&r.prototype instanceof h?r:h).prototype),i=new L(e||[]);return o._invoke=function(t,r,n){var e="suspendedStart";return function(o,i){if("executing"===e)throw new Error("Generator is already running");if("completed"===e){if("throw"===o)throw i;return{value:void 0,done:!0}}for(n.method=o,n.arg=i;;){var a=n.delegate;if(a){var u=x(a,n);if(u){if(u===f)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===e)throw e="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);e="executing";var s=c(t,r,n);if("normal"===s.type){if(e=n.done?"completed":"suspendedYield",s.arg===f)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(e="completed",n.method="throw",n.arg=s.arg)}}}(t,n,i),o}function c(t,r,n){try{return{type:"normal",arg:t.call(r,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var f={};function h(){}function l(){}function p(){}var v={};v[o]=function(){return this};var d=Object.getPrototypeOf,y=d&&d(d(P([])));y&&y!==r&&n.call(y,o)&&(v=y);var g=p.prototype=h.prototype=Object.create(v);function m(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){var e;this._invoke=function(o,i){function a(){return new r((function(e,a){!function e(o,i,a,u){var s=c(t[o],t,i);if("throw"!==s.type){var f=s.arg,h=f.value;return h&&"object"==typeof h&&n.call(h,"__await")?r.resolve(h.__await).then((function(t){e("next",t,a,u)}),(function(t){e("throw",t,a,u)})):r.resolve(h).then((function(t){f.value=t,a(f)}),(function(t){return e("throw",t,a,u)}))}u(s.arg)}(o,i,e,a)}))}return e=e?e.then(a,a):a()}}function x(t,r){var n=t.iterator[r.method];if(void 0===n){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,x(t,r),"throw"===r.method))return f;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var e=c(n,t.iterator,r.arg);if("throw"===e.type)return r.method="throw",r.arg=e.arg,r.delegate=null,f;var o=e.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,f):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,f)}function b(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function E(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function P(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var e=-1,i=function r(){for(;++e<t.length;)if(n.call(t,e))return r.value=t[e],r.done=!1,r;return r.value=void 0,r.done=!0,r};return i.next=i}}return{next:A}}function A(){return{value:void 0,done:!0}}return l.prototype=g.constructor=p,p.constructor=l,l.displayName=u(p,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===l||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,u(t,a,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},m(w.prototype),w.prototype[i]=function(){return this},t.AsyncIterator=w,t.async=function(r,n,e,o,i){void 0===i&&(i=Promise);var a=new w(s(r,n,e,o),i);return t.isGeneratorFunction(n)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},m(g),u(g,a,"Generator"),g[o]=function(){return this},g.toString=function(){return"[object Generator]"},t.keys=function(t){var r=[];for(var n in t)r.push(n);return r.reverse(),function n(){for(;r.length;){var e=r.pop();if(e in t)return n.value=e,n.done=!1,n}return n.done=!0,n}},t.values=P,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function e(n,e){return a.type="throw",a.arg=t,r.next=n,e&&(r.method="next",r.arg=void 0),!!e}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return e("end");if(i.tryLoc<=this.prev){var u=n.call(i,"catchLoc"),s=n.call(i,"finallyLoc");if(u&&s){if(this.prev<i.catchLoc)return e(i.catchLoc,!0);if(this.prev<i.finallyLoc)return e(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return e(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return e(i.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,f):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),f},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),E(n),f}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc===t){var e=n.completion;if("throw"===e.type){var o=e.arg;E(n)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:P(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}}(s={exports:{}}),s.exports),m=function(){function t(t){var r,n=t.seed,e=t.mutation,o=t.fitness,i=t.crossover,a=t.selection,u=t.config,s=t.hooks;this.config={populationSize:100,generations:999,mutationProbability:.2,crossoverProbability:.8,individualValidation:!1,tournamentSize:2},this.generation=1,this.seed=n,this.mutation=e,this.crossover=i,this.selection=a,this.fitness=o,this.config=u,this.hooks=s,this.population=this.config.individualValidation?v(this.seed,this.config.populationSize):p(this.seed,this.fitness,this.config.populationSize),null==(r=this.hooks)||r.onGeneration(this.population,0)}var r=t.prototype;return r.crossoverAndMutation=function(t){var r=this;return t.map((function(t){var n=r.mutation(r.config.mutationProbability,t);return{genome:n,fitness:r.fitness(n)}}))},r.step=function(){for(var t,r=this,n=[this.population[0],this.population[1]];n.length!==this.config.populationSize;){var e=this.selection(2,this.population,{tournamentSize:this.config.tournamentSize}),o=e[0],i=e[1];if(Math.random()<this.config.crossoverProbability){var a=this.crossover(i.genome,o.genome).map(this.config.individualValidation?this.fitness:h(this.fitness));n=n.concat(a)}else n=n.concat([o,i])}if(n.filter((function(t){return r.population[0].fitness===t.fitness})).length>this.config.populationSize/2){var u=this.config.individualValidation?v(this.seed,this.config.populationSize-1):p(this.seed,this.fitness,this.config.populationSize-1);this.population=[this.population[0]].concat(u)}else{var s=n.sort(l);this.population=s}null==(t=this.hooks)||t.onGeneration(this.population,this.generation),this.generation++},t}();exports.GARunner=function(){var t=u(g.mark((function t(r){var n,e;return g.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:for(n=new m(r),e=0;e<r.config.generations;e++)setTimeout((function(){return n.step()}),0);case 2:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),exports.byFitness=l,exports.flipMutation=function(t,r){return r.map((function(r){return Math.random()<t?1^r:r}))},exports.generateIndividual=f,exports.generateIndividualWith=h,exports.generatePopulation=p,exports.generatePopulationWithValidation=v,exports.intInRangeMutation=function(r){return function(n,e){return c((function(e){return Math.random()<n?t.apply(void 0,r):e}),e)}},exports.onePointCrossOver=i,exports.pmxCrossover=function(t,r){var e={},o={},i=n(t.length,2),a=i[0],u=i[1];if(!Array.isArray(t)||!Array.isArray(r))throw TypeError("You cant use PMX with object genomes");for(var s=[Array.from(t),Array.from(r)],c=a;c<u;c++)s[0][c]=r[c],e[r[c]]=t[c],s[1][c]=t[c],o[t[c]]=r[c];for(var f=0;f<a;f++){for(;s[0][f]in e;)s[0][f]=e[s[0][f]];for(;s[1][f]in o;)s[1][f]=o[s[1][f]]}for(var h=u;h<t.length;h++){for(;s[0][h]in e;)s[0][h]=e[s[0][h]];for(;s[1][h]in o;)s[1][h]=o[s[1][h]]}return s},exports.randomInRange=t,exports.selectBest=function(t,r){return d(t,r),r.slice(0,t)},exports.selectByTournament=function(t,r,n){var e=(void 0===n?{tournamentSize:2}:n).tournamentSize;d(t,r);for(var o=[];o.length<t;){var i=y(e,r).sort(l);o.push(i[0])}return o},exports.selectPopulation=function(t,r,n,e){return t(r,n,e)},exports.selectRandom=y,exports.selectRoulette=function(r,n){d(r,n);for(var e=n[0].fitness,o=[],i=0;i<r;i++){for(var a=void 0;a=t(n.length-1),!(Math.random()<=n[a].fitness/e););o=[].concat(o,[n[a]])}return o},exports.selectWorst=function(t,r){return d(t,r),r.slice(-t).reverse()},exports.twoPointCrossOver=o,exports.xPointCrossOver=e;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(t,r){if(!r&&0!==r)return Math.round(Math.random()*t);var n=r-t;return Math.round(Math.random()*n)+t},r=function(t){return function(r){var n=r.map(function(t){return function(r){return[r.slice(0,t),r.slice(t)]}}(t));return[[].concat(n[0][0],n[1][1]),[].concat(n[1][0],n[0][1])]}},n=function(r,n){if(r<1)throw new RangeError("The number of fix points should be at least 1");for(var e=[t(r-1)],o=1;o<n;o++)e.push(t(e[o-1]+1,r-1));return e},e=function(t){return function(e,o){var i=[e,o];Array.isArray(e)||(i=i.map(Object.entries));for(var a=n(i[0].length,t),s=0;s<a.length;s++)i=r(a[s])(i);return Array.isArray(e)?i:i.map(Object.fromEntries)}},o=e(2),i=e(1);function a(t,r,n,e,o,i,a){try{var s=t[i](a),u=s.value}catch(t){return void n(t)}s.done?r(u):Promise.resolve(u).then(e,o)}function s(t){return function(){var r=this,n=arguments;return new Promise((function(e,o){var i=t.apply(r,n);function s(t){a(i,e,o,s,u,"next",t)}function u(t){a(i,e,o,s,u,"throw",t)}s(void 0)}))}}var u,c=function(t,r){return Array.isArray(r)?r.map(t):Object.keys(r).reduce((function(n,e){return n[e]=t(r[e],e,r),n}),{})},f=function(t,r){if(!t)throw TypeError("Individual needs a genotype");if(!r)throw TypeError("Individual needs fitness function");if("function"==typeof t){var n=t();return{genome:n,fitness:r(n)}}var e=c((function(t){return t()}),t);return{genome:e,fitness:r(e)}},h=function(t){return function(r){return{genome:r,fitness:t(r)}}},l=function(t,r){return t.fitness<r.fitness?1:-1},p=function(t,r,n){for(var e=new Array(n),o=0;o<n;o++)e[o]=f(t,r);return e.sort(l)},v=function(t,r){for(var n=new Array(r),e=0;e<r;e++)n[e]=t();return n.sort(l)},d=function(t,r){if(t>r.length)throw new RangeError("You cannot select more individuals than the population size")},y=function(r,n){return d(r,n),Array.from({length:r},(function(){return n[t(n.length-1)]}))},g=(function(t){var r=function(t){var r=Object.prototype,n=r.hasOwnProperty,e="function"==typeof Symbol?Symbol:{},o=e.iterator||"@@iterator",i=e.asyncIterator||"@@asyncIterator",a=e.toStringTag||"@@toStringTag";function s(t,r,n){return Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{s({},"")}catch(t){s=function(t,r,n){return t[r]=n}}function u(t,r,n,e){var o=Object.create((r&&r.prototype instanceof h?r:h).prototype),i=new L(e||[]);return o._invoke=function(t,r,n){var e="suspendedStart";return function(o,i){if("executing"===e)throw new Error("Generator is already running");if("completed"===e){if("throw"===o)throw i;return{value:void 0,done:!0}}for(n.method=o,n.arg=i;;){var a=n.delegate;if(a){var s=x(a,n);if(s){if(s===f)continue;return s}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===e)throw e="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);e="executing";var u=c(t,r,n);if("normal"===u.type){if(e=n.done?"completed":"suspendedYield",u.arg===f)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(e="completed",n.method="throw",n.arg=u.arg)}}}(t,n,i),o}function c(t,r,n){try{return{type:"normal",arg:t.call(r,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var f={};function h(){}function l(){}function p(){}var v={};v[o]=function(){return this};var d=Object.getPrototypeOf,y=d&&d(d(A([])));y&&y!==r&&n.call(y,o)&&(v=y);var g=p.prototype=h.prototype=Object.create(v);function m(t){["next","throw","return"].forEach((function(r){s(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){var e;this._invoke=function(o,i){function a(){return new r((function(e,a){!function e(o,i,a,s){var u=c(t[o],t,i);if("throw"!==u.type){var f=u.arg,h=f.value;return h&&"object"==typeof h&&n.call(h,"__await")?r.resolve(h.__await).then((function(t){e("next",t,a,s)}),(function(t){e("throw",t,a,s)})):r.resolve(h).then((function(t){f.value=t,a(f)}),(function(t){return e("throw",t,a,s)}))}s(u.arg)}(o,i,e,a)}))}return e=e?e.then(a,a):a()}}function x(t,r){var n=t.iterator[r.method];if(void 0===n){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,x(t,r),"throw"===r.method))return f;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var e=c(n,t.iterator,r.arg);if("throw"===e.type)return r.method="throw",r.arg=e.arg,r.delegate=null,f;var o=e.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,f):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,f)}function b(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function E(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function A(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var e=-1,i=function r(){for(;++e<t.length;)if(n.call(t,e))return r.value=t[e],r.done=!1,r;return r.value=void 0,r.done=!0,r};return i.next=i}}return{next:O}}function O(){return{value:void 0,done:!0}}return l.prototype=g.constructor=p,p.constructor=l,l.displayName=s(p,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===l||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,s(t,a,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},m(w.prototype),w.prototype[i]=function(){return this},t.AsyncIterator=w,t.async=function(r,n,e,o,i){void 0===i&&(i=Promise);var a=new w(u(r,n,e,o),i);return t.isGeneratorFunction(n)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},m(g),s(g,a,"Generator"),g[o]=function(){return this},g.toString=function(){return"[object Generator]"},t.keys=function(t){var r=[];for(var n in t)r.push(n);return r.reverse(),function n(){for(;r.length;){var e=r.pop();if(e in t)return n.value=e,n.done=!1,n}return n.done=!0,n}},t.values=A,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function e(n,e){return a.type="throw",a.arg=t,r.next=n,e&&(r.method="next",r.arg=void 0),!!e}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return e("end");if(i.tryLoc<=this.prev){var s=n.call(i,"catchLoc"),u=n.call(i,"finallyLoc");if(s&&u){if(this.prev<i.catchLoc)return e(i.catchLoc,!0);if(this.prev<i.finallyLoc)return e(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return e(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return e(i.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,f):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),f},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),E(n),f}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc===t){var e=n.completion;if("throw"===e.type){var o=e.arg;E(n)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:A(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}}(u={exports:{}}),u.exports),m=function(){function t(t){var r,n=t.seed,e=t.mutation,o=t.fitness,i=t.crossover,a=t.selection,s=t.config,u=t.hooks;this.config={populationSize:100,generations:999,mutationProbability:.2,crossoverProbability:.8,individualValidation:!1,tournamentSize:2},this.generation=1,this.seed=n,this.mutation=e,this.crossover=i,this.selection=a,this.fitness=o,this.config=s,this.hooks=u,this.population=this.config.individualValidation?v(this.seed,this.config.populationSize):p(this.seed,this.fitness,this.config.populationSize),null==(r=this.hooks)||r.onGeneration(this.population,0)}var r=t.prototype;return r.crossoverAndMutation=function(t){var r=this;return t.map((function(t){var n=r.mutation(r.config.mutationProbability,t);return{genome:n,fitness:r.fitness(n)}}))},r.step=function(){for(var t,r=this,n=[this.population[0],this.population[1]];n.length!==this.config.populationSize;){var e=this.selection(2,this.population,{tournamentSize:this.config.tournamentSize}),o=e[0],i=e[1];if(Math.random()<this.config.crossoverProbability){var a=this.crossover(i.genome,o.genome).map(this.config.individualValidation?this.fitness:h(this.fitness));n=n.concat(a)}else n=n.concat([o,i])}if(n.filter((function(t){return r.population[0].fitness===t.fitness})).length>this.config.populationSize/2){var s=this.config.individualValidation?v(this.seed,this.config.populationSize-1):p(this.seed,this.fitness,this.config.populationSize-1);this.population=[this.population[0]].concat(s)}else{var u=n.sort(l);this.population=u}null==(t=this.hooks)||t.onGeneration(this.population,this.generation),this.generation++},t}();exports.GARunner=function(){var t=s(g.mark((function t(r){var n,e;return g.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:for(n=new m(r),e=0;e<r.config.generations;e++)setTimeout((function(){return n.step()}),0);case 2:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),exports.byFitness=l,exports.cycleCrossover=function(t,r){if(!Array.isArray(t)||!Array.isArray(r))throw TypeError("You cant use Cycle Crossover with object genomes");for(var n=new Set,e=[],o=0;n.size!==t.length;)n.has(t[o])||function(){for(var i=o,a=null,s={},u={};t[o]!=a;)n.add(t[i]),s[i]=t[i],u[i]=r[i],a=r[i],i=t.findIndex((function(t){return t===a}));e.push([s,u])}(),o++;return e.reduce((function(t,r,n){var e=t[1];return[Object.assign(t[0],r[n%2]),Object.assign(e,r[(n+1)%2])]}),[{},{}]).map(Object.values)},exports.flipMutation=function(t,r){return r.map((function(r){return Math.random()<t?1^r:r}))},exports.generateIndividual=f,exports.generateIndividualWith=h,exports.generatePopulation=p,exports.generatePopulationWithValidation=v,exports.intInRangeMutation=function(r){return function(n,e){return c((function(e){return Math.random()<n?t.apply(void 0,r):e}),e)}},exports.onePointCrossOver=i,exports.pmxCrossover=function(t,r){if(!Array.isArray(t)||!Array.isArray(r))throw TypeError("You cant use PMX with object genomes");for(var e={},o={},i=n(t.length,2),a=i[0],s=i[1],u=[Array.from(t),Array.from(r)],c=a;c<s;c++)u[0][c]=r[c],e[r[c]]=t[c],u[1][c]=t[c],o[t[c]]=r[c];for(var f=0;f<a;f++){for(;u[0][f]in e;)u[0][f]=e[u[0][f]];for(;u[1][f]in o;)u[1][f]=o[u[1][f]]}for(var h=s;h<t.length;h++){for(;u[0][h]in e;)u[0][h]=e[u[0][h]];for(;u[1][h]in o;)u[1][h]=o[u[1][h]]}return u},exports.randomInRange=t,exports.selectBest=function(t,r){return d(t,r),r.slice(0,t)},exports.selectByTournament=function(t,r,n){var e=(void 0===n?{tournamentSize:2}:n).tournamentSize;d(t,r);for(var o=[];o.length<t;){var i=y(e,r).sort(l);o.push(i[0])}return o},exports.selectPopulation=function(t,r,n,e){return t(r,n,e)},exports.selectRandom=y,exports.selectRoulette=function(r,n){d(r,n);for(var e=n[0].fitness,o=[],i=0;i<r;i++){for(var a=void 0;a=t(n.length-1),!(Math.random()<=n[a].fitness/e););o=[].concat(o,[n[a]])}return o},exports.selectWorst=function(t,r){return d(t,r),r.slice(-t).reverse()},exports.twoPointCrossOver=o,exports.xPointCrossOver=e;
//# sourceMappingURL=chromosome-js.cjs.production.min.js.map

@@ -70,2 +70,6 @@ /**

var pmxCrossover = function pmxCrossover(genome1, genome2) {
if (!(Array.isArray(genome1) && Array.isArray(genome2))) {
throw TypeError("You cant use PMX with object genomes");
}
var mapOffspring1 = {};

@@ -78,6 +82,2 @@ var mapOffspring2 = {};

if (!(Array.isArray(genome1) && Array.isArray(genome2))) {
throw TypeError("You cant use PMX with object genomes");
}
var offspring = [Array.from(genome1), Array.from(genome2)];

@@ -115,3 +115,50 @@

};
/* Cycle crossover operator */
var cycleCrossover = function cycleCrossover(genome1, genome2) {
if (!(Array.isArray(genome1) && Array.isArray(genome2))) {
throw TypeError("You cant use Cycle Crossover with object genomes");
}
var cycleSet = new Set();
var cycles = [];
var i = 0; // Find cycles
while (cycleSet.size !== genome1.length) {
if (!cycleSet.has(genome1[i])) {
(function () {
var pos = i;
var val = null;
var cycle1 = {};
var cycle2 = {};
while (genome1[i] != val) {
cycleSet.add(genome1[pos]);
cycle1[pos] = genome1[pos];
cycle2[pos] = genome2[pos];
val = genome2[pos];
pos = genome1.findIndex(function (value) {
return value === val;
});
}
cycles.push([cycle1, cycle2]);
})();
}
i++;
} // Mix cycles
var offspring = cycles.reduce(function (_ref, cycle, index) {
var first = _ref[0],
second = _ref[1];
return (// (index + 0) % 2 and (index + 1) % 2 is a way of alternating values between 0 and 1
// where for index 0: 0, 1; 1: 1, 0; 2: 0, 1...
[Object.assign(first, cycle[index % 2]), Object.assign(second, cycle[(index + 1) % 2])]
);
}, [{}, {}]);
return offspring.map(Object.values);
};
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {

@@ -1188,3 +1235,3 @@ try {

export { GARunner, byFitness, flipMutation, generateIndividual, generateIndividualWith, generatePopulation, generatePopulationWithValidation, intInRangeMutation, onePointCrossOver, pmxCrossover, randomInRange, selectBest, selectByTournament, selectPopulation, selectRandom, selectRoulette, selectWorst, twoPointCrossOver, xPointCrossOver };
export { GARunner, byFitness, cycleCrossover, flipMutation, generateIndividual, generateIndividualWith, generatePopulation, generatePopulationWithValidation, intInRangeMutation, onePointCrossOver, pmxCrossover, randomInRange, selectBest, selectByTournament, selectPopulation, selectRandom, selectRoulette, selectWorst, twoPointCrossOver, xPointCrossOver };
//# sourceMappingURL=chromosome-js.esm.js.map

@@ -7,1 +7,2 @@ import { Genome } from "../population";

export declare const pmxCrossover: CrossoverFunction;
export declare const cycleCrossover: CrossoverFunction;

@@ -1,2 +0,2 @@

export declare type Genome<T = any> = T[] | {
export declare type Genome = any[] | {
[k: string]: any;

@@ -3,0 +3,0 @@ };

{
"version": "0.4.0",
"version": "0.5.0",
"license": "MIT",

@@ -23,7 +23,2 @@ "main": "dist/index.js",

"peerDependencies": {},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
}
},
"name": "chromosome-js",

@@ -30,0 +25,0 @@ "author": "Angel Paredes Barato",

@@ -1,2 +0,2 @@

![npm bundle size (version)](https://img.shields.io/bundlephobia/minzip/chromosome-js/0.3.0)
![npm bundle size (version)](https://img.shields.io/bundlephobia/minzip/chromosome-js/0.4.0)

@@ -3,0 +3,0 @@ # ChromosomeJS 🐒

@@ -63,2 +63,6 @@ import { randomInRange } from '../../utils/random';

export const pmxCrossover : CrossoverFunction = (genome1, genome2) => {
if (!(Array.isArray(genome1) && Array.isArray(genome2))) {
throw TypeError("You cant use PMX with object genomes")
}
let mapOffspring1 : MapToEqual= {}

@@ -69,6 +73,2 @@ let mapOffspring2 : MapToEqual= {}

if (!(Array.isArray(genome1) && Array.isArray(genome2))) {
throw TypeError("You cant use PMX with object genomes")
}
let offspring = [Array.from(genome1), Array.from(genome2)]

@@ -104,2 +104,42 @@

return offspring as [Genome, Genome]
}
/* Cycle crossover operator */
export const cycleCrossover : CrossoverFunction = (genome1, genome2) => {
if (!(Array.isArray(genome1) && Array.isArray(genome2))) {
throw TypeError("You cant use Cycle Crossover with object genomes")
}
const cycleSet = new Set()
let cycles = []
let i = 0
// Find cycles
while (cycleSet.size !== genome1.length) {
if (!cycleSet.has(genome1[i])) {
let pos = i
let val : number | null = null
let cycle1 = {} as {[k: number]: any}
let cycle2 = {} as {[k: number]: any}
while (genome1[i] != val) {
cycleSet.add(genome1[pos])
cycle1[pos] = genome1[pos]
cycle2[pos] = genome2[pos]
val = genome2[pos]
pos = genome1.findIndex((value) => value === val)
}
cycles.push([cycle1, cycle2])
}
i++
}
// Mix cycles
const offspring = cycles.reduce(([first, second], cycle, index) => (
// (index + 0) % 2 and (index + 1) % 2 is a way of alternating values between 0 and 1
// where for index 0: 0, 1; 1: 1, 0; 2: 0, 1...
[Object.assign(first, cycle[(index%2)]), Object.assign(second, cycle[(index+1)%2])]
), [{}, {}])
return offspring.map(Object.values) as [Genome, Genome]
}
import { mapObjIndexed } from '../../utils/functional';
export type Genome<T = any> = T[] | { [k: string]: any }
export type Genome = any[] | { [k: string]: any }
export interface Individual {

@@ -5,0 +5,0 @@ genome: Genome,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc