Socket
Socket
Sign inDemoInstall

egreedy

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egreedy - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

.eslintrc

106

index.js

@@ -1,102 +0,36 @@

/* jslint node: true */
'use strict';
var _ = require('lodash');
var BPromise = require('bluebird');
var debug = require('debug')('egreedy');
var async = BPromise.method;
var Algorithm = function (options) {
options = options || {};
var self = this;
function Algorithm(options) {
var opts = options || {};
if (!(self instanceof Algorithm)) {
return new Algorithm(options);
if (!(this instanceof Algorithm)) {
return new Algorithm(opts);
}
var arms = _.isUndefined(options.arms) ? 2 : parseInt(options.arms, 10);
var epsilon = _.isUndefined(options.epsilon) ? 0.5 : parseFloat(options.epsilon);
var counts = [];
var values = [];
debug('init', opts);
if (arms < 1) {
this.arms = _.isUndefined(opts.arms) ? 2 : parseInt(opts.arms, 10);
this.epsilon = _.isUndefined(opts.epsilon) ? 0.5 : parseFloat(opts.epsilon);
if (this.arms < 1) {
throw new TypeError('invalid arms: cannot be less than 1');
}
else if (epsilon < 0) {
} else if (this.epsilon < 0) {
throw new TypeError('invalid epsilon: cannot be less than 0');
}
else if (epsilon > 1) {
} else if (this.epsilon > 1) {
throw new TypeError('invalid epsilon: cannot be greater than 1');
}
for (var i=0; i<arms; i++) {
counts.push(0);
values.push(0);
}
this.counts = Array.apply(null, Array(this.arms)).map(Number.prototype.valueOf, 0);
this.values = Array.apply(null, Array(this.arms)).map(Number.prototype.valueOf, 0);
}
var api = {};
Algorithm.prototype.load = async(require('./lib/load'));
Algorithm.prototype.reward = async(require('./lib/reward'));
Algorithm.prototype.select = async(require('./lib/select'));
Algorithm.prototype.serialize = async(require('./lib/serialize'));
api.n = 0;
api.load = function (config) {
arms = config.arms;
epsilon = config.epsilon;
counts = config.counts;
values = config.values;
return BPromise.resolve(values);
};
api.reward = function (arm, reward) {
return new BPromise(function (resolve, reject) {
if (!_.isNumber(arm)) {
return reject(new TypeError('missing or invalid required parameter: arm'));
}
else if (!_.isNumber(reward)) {
return reject(new TypeError('missing or invalid required parameter: reward'));
}
else if (arm >= arms || arm < 0) {
return reject(new TypeError('invalid arm: ' + arm + ' not in valid range (0-' + arms.length + ')'));
}
var ct = ++counts[arm];
var pre = values[arm];
var post = ((ct-1) / ct) * pre + (1/ct) * reward;
values[arm] = post;
api.n = _.reduce(counts, function (sum, ct) {
return sum + ct;
});
resolve(values);
});
};
api.select = function () {
return new BPromise(function (resolve) {
var arm;
if (epsilon > _.random(0, 1, true) || api.n === 0) {
arm = _.random(0, arms-1);
} else {
arm = values.indexOf(Math.max.apply(null, values));
}
resolve(arm);
});
};
api.serialize = function () {
return BPromise.resolve({
arms: arms,
epsilon: epsilon,
counts: counts.slice(0),
values: values.slice(0)
});
};
return api;
};
module.exports = Algorithm;
{
"name": "egreedy",
"description": "A Promises/A+ epsilon-greedy multi-armed bandit",
"version": "0.2.1",
"author": "banditdb",
"contributors": [
"kurttheviking"
],
"description": "An epsilon-greedy multi-armed bandit algorithm",
"version": "0.3.0",
"license": "ISC",
"main": "index.js",
"keywords": [
"multi-armed bandit",
"epsiolon greedy algorithm",
"promise",
"promises-a",
"promises-aplus"
"promises-aplus",
"banditlab"
],
"main": "index.js",
"scripts": {
"test": "node node_modules/mocha/bin/mocha ./test/index"
"author": {
"name": "Kurt Ericson",
"email": "kurttheviking@outlook.com",
"url": "http://github.com/kurttheviking"
},
"repository": "git@github.com:banditdb/egreedy.git",
"contributors": [
{
"name": "Kurt Ericson",
"email": "kurttheviking@outlook.com",
"url": "https://github.com/kurttheviking"
}
],
"repository": {
"type": "git",
"url": "git://github.com/kurttheviking/egreedy.git"
},
"bugs": {
"url": "https://github.com/kurttheviking/egreedy/issues"
},
"homepage": "https://github.com/kurttheviking/egreedy#readme",
"dependencies": {
"bluebird": "2.9.13",
"lodash": "3.5.0"
"bluebird": "3.0.5",
"debug": "2.2.0",
"lodash": "3.10.1"
},
"devDependencies": {
"chai": "2.1.1",
"mocha": "2.2.1",
"sinon": "1.13.0",
"sinon-chai": "2.7.0"
"chai": "3.4.0",
"eslint": "1.9.0",
"eslint-config-airbnb": "1.0.0",
"istanbul": "0.4.0",
"mocha": "2.3.3",
"sinon": "1.17.2"
},
"readmeFilename": "README.md",
"license": "ISC"
"scripts": {
"coverage": "./node_modules/istanbul/lib/cli.js cover --report=json-summary --report=html _mocha ./test -- --recursive",
"test": "node node_modules/mocha/bin/mocha ./test --recursive"
}
}

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