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.7.0 to 2.0.0

lib/Algorithm.js

37

index.js

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

var _ = require('lodash');
var BPromise = require('bluebird');
var debug = require('debug')('egreedy');
var async = BPromise.method;
function Algorithm(options) {
var opts = options || {};
if (!(this instanceof Algorithm)) {
return new Algorithm(opts);
}
debug('init', opts);
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 (this.epsilon < 0) {
throw new TypeError('invalid epsilon: cannot be less than 0');
} else if (this.epsilon > 1) {
throw new TypeError('invalid epsilon: cannot be greater than 1');
}
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);
}
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'));
module.exports = Algorithm;
module.exports = require('./lib/Algorithm');
{
"name": "egreedy",
"description": "An epsilon-greedy multi-armed bandit algorithm",
"version": "0.7.0",
"version": "2.0.0",
"license": "ISC",

@@ -11,7 +11,7 @@ "main": "index.js",

"promises-aplus",
"banditlab"
"banditlab/2.0"
],
"author": {
"name": "Kurt Ericson",
"email": "kurttheviking@outlook.com",
"email": "github@kurttheviking.com",
"url": "http://github.com/kurttheviking"

@@ -22,3 +22,3 @@ },

"name": "Kurt Ericson",
"email": "kurttheviking@outlook.com",
"email": "github@kurttheviking.com",
"url": "https://github.com/kurttheviking"

@@ -29,21 +29,23 @@ }

"type": "git",
"url": "git://github.com/kurttheviking/egreedy.git"
"url": "git://github.com/kurttheviking/egreedy-js.git"
},
"bugs": {
"url": "https://github.com/kurttheviking/egreedy/issues"
"url": "https://github.com/kurttheviking/egreedy-js/issues"
},
"homepage": "https://github.com/kurttheviking/egreedy#readme",
"homepage": "https://github.com/kurttheviking/egreedy-js#readme",
"dependencies": {
"bluebird": "3.3.0",
"bluebird": "3.3.5",
"debug": "2.2.0",
"lodash": "4.3.0"
"lodash": "4.12.0"
},
"devDependencies": {
"chai": "3.5.0",
"eslint": "1.10.3",
"eslint-config-airbnb": "5.0.0",
"mocha-eslint": "1.0.0",
"istanbul": "0.4.2",
"eslint": "2.9.0",
"eslint-config-airbnb": "9.0.1",
"eslint-plugin-import": "1.8.0",
"mocha-eslint": "2.0.2",
"istanbul": "0.4.3",
"mocha": "2.4.5",
"sinon": "1.17.3"
"mockery": "1.7.0",
"sinon": "1.17.4"
},

@@ -53,3 +55,4 @@ "scripts": {

"test": "node node_modules/mocha/bin/mocha ./test --recursive"
}
},
"tonicExampleFilename": "./opt/tonic.js"
}
egreedy
=======
[![Build Status](https://travis-ci.org/kurttheviking/egreedy.svg)](https://travis-ci.org/kurttheviking/egreedy)
[![Build Status](https://travis-ci.org/kurttheviking/egreedy-js.svg?branch=master)](https://travis-ci.org/kurttheviking/egreedy-js)
**An epsilon-greedy algorithm for multi-armed bandit problems**
**An epsilon-greedy multi-armed bandit algorithm**
This implementation is based on [<em>Bandit Algorithms for Website Optimization</em>](http://shop.oreilly.com/product/0636920027393.do) and related empirical research in ["Algorithms for the multi-armed bandit problem"](https://d2w9gswcdc2jtf.cloudfront.net/research/Algorithms+for+the+multi-armed+bandit+problem.pdf).
This implementation is based on [<em>Bandit Algorithms for Website Optimization</em>](http://shop.oreilly.com/product/0636920027393.do) and related empirical research in ["Algorithms for the multi-armed bandit problem"](http://www.cs.mcgill.ca/~vkules/bandits.pdf).

@@ -13,3 +13,3 @@

This module conforms to the [BanditLab/1.0 specification](https://github.com/banditlab/spec-js/blob/master/README.md).
This module conforms to the [BanditLab/2.0 specification](https://github.com/banditlab/spec-js/releases).

@@ -27,3 +27,3 @@

1. Create an optimizer with 3 arms and epsilon 0.25:
1. Create an optimizer with `3` arms and epsilon `0.25`:

@@ -50,5 +50,3 @@ ```js

```js
algorithm.reward(armId, value).then(function (n) {
...
});
algorithm.reward(arm, value);
```

@@ -59,3 +57,3 @@

#### `Algorithm([config])`
#### `Algorithm(config)`

@@ -73,2 +71,4 @@ Create a new optimization algorithm.

Alternatively, the `state` object returned from [`Algorithm#serialize`](https://github.com/kurttheviking/egreedy#algorithmserialize) can be passed as `config`.
**Returns**

@@ -81,6 +81,7 @@

```js
> var Algorithm = require('egreedy');
> var algorithm = new Algorithm();
> assert.equal(algorithm.arms, 3);
> assert.equal(algorithm.epsilon, 0.5);
var Algorithm = require('egreedy');
var algorithm = new Algorithm();
assert.equal(algorithm.arms, 3);
assert.equal(algorithm.epsilon, 0.5);
```

@@ -91,6 +92,7 @@

```js
> var Algorithm = require('egreedy');
> var algorithm = new Algorithm({arms: 4, epsilon: 0.75});
> assert.equal(algorithm.arms, 4);
> assert.equal(algorithm.epsilon, 0.75);
var Algorithm = require('egreedy');
var algorithm = new Algorithm({arms: 4, epsilon: 0.75});
assert.equal(algorithm.arms, 4);
assert.equal(algorithm.epsilon, 0.75);
```

@@ -113,6 +115,9 @@

```js
> var Algorithm = require('egreedy');
> var algorithm = new Algorithm();
> algorithm.select().then(function (arm) { console.log(arm); });
var Algorithm = require('egreedy');
var algorithm = new Algorithm();
algorithm.select().then(function (arm) { console.log(arm); });
```
```js
0

@@ -137,9 +142,17 @@ ```

```js
> var Algorithm = require('egreedy');
> var algorithm = new Algorithm();
> algorithm.reward(0, 1).then(function (n) { console.log(n); });
var Algorithm = require('egreedy');
var algorithm = new Algorithm();
1
algorithm.reward(0, 1).then(function (algorithmUpdated) { console.log(algorithmUpdated) });
```
```js
<Algorithm>{
arms: 2,
epsilon: 0.5,
counts: [ 1, 0 ],
values: [ 1, 0 ]
}
```
#### `Algorithm#serialize()`

@@ -160,6 +173,9 @@

```js
> var Algorithm = require('egreedy');
> var algorithm = new Algorithm();
> algorithm.serialize().then(function (state) { console.log(state); });
var Algorithm = require('egreedy');
var algorithm = new Algorithm();
algorithm.serialize().then(function (state) { console.log(state); });
```
```js
{

@@ -173,26 +189,3 @@ arms: 2,

#### `Algorithm#load(state)`
Restore an instance of an algorithm to a previously serialized state. This method overrides any options parameters passed at instantiation.
**Arguments**
- `state` (Object): a serialized algorithm state (provided from `algorithm.serialize()`)
**Returns**
A promise that resolves to a Number representing the count of observed rounds.
**Example**
```js
> var state = {arms: 2, epsilon: 0.5, counts: [1, 2], values: [1, 0.5]};
> var Algorithm = require('egreedy');
> var algorithm = new Algorithm();
> algorithm.load(state).then(function (n) { console.log(n); });
3
```
## Tests

@@ -224,2 +217,2 @@

While these factors generally do not impede common application, I would consider the implementation suspect in an academic setting.
While these factors generally do not impede common application, I would consider the implementation suspect within academic settings.

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