Comparing version 0.2.1 to 0.3.0
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
21498
17
400
1
1
0
3
6
2
+ Addeddebug@2.2.0
+ Addedbluebird@3.0.5(transitive)
+ Addeddebug@2.2.0(transitive)
+ Addedlodash@3.10.1(transitive)
+ Addedms@0.7.1(transitive)
- Removedbluebird@2.9.13(transitive)
- Removedlodash@3.5.0(transitive)
Updatedbluebird@3.0.5
Updatedlodash@3.10.1