Socket
Socket
Sign inDemoInstall

egreedy

Package Overview
Dependencies
5
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.10 to 2.0.11

lib/utils/promisify.js

33

lib/Algorithm.js

@@ -1,9 +0,12 @@

var _ = require('lodash');
var BPromise = require('bluebird');
var debug = require('debug')('egreedy');
const debug = require('debug')('ucb');
const has = require('has');
var async = BPromise.method;
const promisify = require('./utils/promisify');
const reward = require('./Algorithm/reward');
const select = require('./Algorithm/select');
const serialize = require('./Algorithm/serialize');
function Algorithm(opts) {
var options = opts || {};
const options = opts || {};

@@ -16,4 +19,4 @@ if (!(this instanceof Algorithm)) {

this.arms = _.isUndefined(options.arms) ? 2 : parseInt(options.arms, 10);
this.epsilon = _.isUndefined(options.epsilon) ? 0.5 : parseFloat(options.epsilon);
this.arms = options.arms === undefined ? 2 : parseInt(options.arms, 10);
this.epsilon = options.epsilon === undefined ? 0.5 : parseFloat(options.epsilon);

@@ -28,6 +31,6 @@ if (this.arms < 1) {

if (_.has(options, 'counts') && _.has(options, 'values')) {
if (!_.isArray(options.counts)) {
if (has(options, 'counts') && has(options, 'values')) {
if (!Array.isArray(options.counts)) {
throw new TypeError('counts must be an array');
} else if (!_.isArray(options.values)) {
} else if (!Array.isArray(options.values)) {
throw new TypeError('values must be an array');

@@ -43,11 +46,11 @@ } else if (options.counts.length !== this.arms) {

} else {
this.counts = _.times(this.arms, _.constant(0));
this.values = this.counts.slice(0);
this.counts = new Array(this.arms).fill(0);
this.values = new Array(this.arms).fill(0);
}
}
Algorithm.prototype.reward = async(require('./Algorithm/reward'));
Algorithm.prototype.select = async(require('./Algorithm/select'));
Algorithm.prototype.serialize = async(require('./Algorithm/serialize'));
Algorithm.prototype.reward = promisify(reward);
Algorithm.prototype.select = promisify(select);
Algorithm.prototype.serialize = promisify(serialize);
module.exports = Algorithm;

@@ -1,24 +0,22 @@

var _ = require('lodash');
var debug = require('debug')('egreedy:reward');
const debug = require('debug')('egreedy:reward');
function reward(arm, val) {
var ct;
var prior;
function reward(arm, value) {
debug('reward arm %s with %s', arm, value);
debug('reward arm %s with %s', arm, val);
if (!_.isNumber(arm)) {
if (typeof arm !== 'number') {
throw new TypeError('missing or invalid required parameter: arm');
} else if (arm >= this.arms || arm < 0) {
throw new TypeError('arm index out of bounds');
} else if (!_.isNumber(val)) {
} else if (typeof value !== 'number') {
throw new TypeError('missing or invalid required parameter: reward');
}
ct = ++this.counts[arm];
prior = this.values[arm];
const count = this.counts[arm] + 1;
const prior = this.values[arm];
this.counts[arm] = count;
debug('prior value: %s', prior);
this.values[arm] = ((ct - 1) / ct) * prior + (1 / ct) * val;
this.values[arm] = (((count - 1) / count) * prior) + ((1 / count) * value);

@@ -25,0 +23,0 @@ debug('posterior value: %s', this.values[arm]);

@@ -1,19 +0,20 @@

var _ = require('lodash');
var debug = require('debug')('egreedy:select');
const debug = require('debug')('egreedy:select');
const Random = require('random-js');
const random = new Random(Random.engines.mt19937().autoSeed());
const sum = require('../utils/sum');
function select() {
var arm;
var n = _.sum(this.counts);
const r = random.real(0, 1, true);
const n = sum(this.counts);
if (this.epsilon > _.random(0, 1, true) || n === 0) {
arm = _.random(0, this.arms - 1);
debug('explore arm: %s', arm);
} else {
arm = this.values.indexOf(Math.max.apply(null, this.values));
debug('exploit arm: %s', arm);
debug('threshold (e=%s vs r=%s)', this.epsilon, r);
if (this.epsilon > r || n === 0) {
return random.integer(0, this.arms - 1);
}
return arm;
return this.values.indexOf(Math.max.apply(null, this.values));
}
module.exports = select;

@@ -1,5 +0,5 @@

var debug = require('debug')('egreedy:serialize');
const debug = require('debug')('egreedy:serialize');
function serialize() {
var out = {
const out = {
arms: this.arms,

@@ -11,3 +11,4 @@ epsilon: this.epsilon,

debug('serializing', out);
debug('state', out);
return out;

@@ -14,0 +15,0 @@ }

@@ -1,14 +0,12 @@

var Algorithm = require('egreedy');
/* eslint-disable */
var algorithm = new Algorithm();
const Algorithm = require('egreedy');
algorithm.select().then(function updateArm(arm) {
const algorithm = new Algorithm();
algorithm.select().then((arm) => {
console.log('chose arm', arm);
return algorithm.reward(arm, 1);
})
.then(function serializeAlgorithm() {
return algorithm.serialize();
})
.then(function displayState(state) {
console.log('new state', JSON.stringify(state, null, 2));
});
.then(() => algorithm.serialize())
.then(state => console.log('new state', JSON.stringify(state, null, 2)));
{
"name": "egreedy",
"description": "An epsilon-greedy multi-armed bandit algorithm",
"version": "2.0.10",
"version": "2.0.11",
"license": "ISC",

@@ -34,16 +34,16 @@ "main": "index.js",

"dependencies": {
"bluebird": "3.3.5",
"debug": "2.2.0",
"lodash": "4.12.0"
"debug": "2.6.8",
"has": "1.0.1",
"random-js": "1.0.8"
},
"devDependencies": {
"chai": "3.5.0",
"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",
"mockery": "1.7.0",
"sinon": "1.17.4"
"chai": "4.0.2",
"eslint": "3.19.0",
"eslint-config-airbnb": "15.0.1",
"eslint-plugin-import": "2.3.0",
"mocha-eslint": "3.0.1",
"istanbul": "0.4.5",
"mocha": "3.4.2",
"mockery": "2.0.0",
"sinon": "2.3.5"
},

@@ -50,0 +50,0 @@ "scripts": {

@@ -6,15 +6,17 @@ egreedy

**An epsilon-greedy multi-armed bandit algorithm**
**An epsilon-greedy algorithm for multi-armed bandit problems**
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).
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). In addition, this module conforms to the [BanditLab/2.0 specification](https://github.com/kurttheviking/banditlab-spec/releases).
## Specification
## Get started
This module conforms to the [BanditLab/2.0 specification](https://github.com/kurttheviking/banditlab-spec/releases).
### Prerequisites
- Node.js 4.x+ ([LTS track](https://github.com/nodejs/LTS#lts-schedule1))
- npm
## Quick start
### Installing
First, install this module in your project:
Install with `npm` (or `yarn`):

@@ -25,10 +27,15 @@ ```sh

Then, use the algorithm:
### Caveat emptor
This implementation often encounters extended floating point numbers. Arm selection is therefore subject to JavaScript's floating point precision limitations. For general information about floating point issues see the [floating point guide](http://floating-point-gui.de).
## Usage
1. Create an optimizer with `3` arms and epsilon `0.25`:
```js
var Algorithm = require('egreedy');
const Algorithm = require('egreedy');
var algorithm = new Algorithm({
const algorithm = new Algorithm({
arms: 3,

@@ -42,3 +49,3 @@ epsilon: 0.25

```js
algorithm.select().then(function (arm) {
algorithm.select().then((arm) => {
// do something based on the chosen arm

@@ -57,26 +64,26 @@ });

#### `Algorithm(config)`
### `Algorithm(config)`
Create a new optimization algorithm.
**Arguments**
#### Arguments
- `config` (Object): algorithm instance parameters
- `config` (`Object`): algorithm instance parameters
The `config` object supports two parameters:
The `config` object supports two optional parameters:
- `arms`: (Number:Integer, Optional), default=2, the number of arms over which the optimization will operate
- `epsilon`: (Number:Float, Optional), default=0.5, from 0 (never explore/always exploit) to 1 (always explore/never exploit)
- `arms` (`Number`, Integer): The number of arms over which the optimization will operate; defaults to `2`
- `epsilon` (`Number`, Float, `0` to `1`): lower leads to more exploration (and less exploitation); defaults to `0.5`
Alternatively, the `state` object returned from [`Algorithm#serialize`](https://github.com/kurttheviking/egreedy-js#algorithmserialize) can be passed as `config`.
Alternatively, the `state` object resolved from [`Algorithm#serialize`](https://github.com/kurttheviking/egreedy-js#algorithmserialize) can be passed as `config`.
**Returns**
#### Returns
An instance of the egreedy optimization algorithm.
**Example**
#### Example
```js
var Algorithm = require('egreedy');
var algorithm = new Algorithm();
const Algorithm = require('egreedy');
const algorithm = new Algorithm();

@@ -90,4 +97,4 @@ assert.equal(algorithm.arms, 2);

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

@@ -98,94 +105,92 @@ assert.equal(algorithm.arms, 4);

#### `Algorithm#select()`
### `Algorithm#select()`
Choose an arm to play, according to the specified bandit algorithm.
**Arguments**
#### Arguments
_None_
**Returns**
#### Returns
A promise that resolves to a Number corresponding to the associated arm index.
A `Promise` that resolves to a `Number` corresponding to the associated arm index.
**Example**
#### Example
```js
var Algorithm = require('egreedy');
var algorithm = new Algorithm();
const Algorithm = require('egreedy');
const algorithm = new Algorithm();
algorithm.select().then(function (arm) { console.log(arm); });
algorithm.select().then(arm => console.log(arm));
```
```js
0
```
### `Algorithm#reward(arm, reward)`
#### `Algorithm#reward(arm, reward)`
Inform the algorithm about the payoff earned from a given arm.
**Arguments**
#### Arguments
- `arm` (Integer): the arm index (provided from `algorithm.select()`)
- `reward` (Number): the observed reward value (which can be 0, to indicate no reward)
- `arm` (`Number`, Integer): the arm index (provided from `Algorithm#select()`)
- `reward` (`Number`): the observed reward value (which can be 0 to indicate no reward)
**Returns**
#### Returns
A promise that resolves to an updated instance of the algorithm.
A `Promise` that resolves to an updated instance of the algorithm. (The original instance is mutated as well.)
**Example**
#### Example
```js
var Algorithm = require('egreedy');
var algorithm = new Algorithm();
const Algorithm = require('egreedy');
const algorithm = new Algorithm();
algorithm.reward(0, 1).then(function (algorithmUpdated) { console.log(algorithmUpdated) });
algorithm.reward(0, 1).then(updatedAlgorithm => console.log(updatedAlgorithm));
```
```js
<Algorithm>{
arms: 2,
epsilon: 0.5,
counts: [ 1, 0 ],
values: [ 1, 0 ]
}
```
### `Algorithm#serialize()`
#### `Algorithm#serialize()`
Obtain a plain object representing the internal state of the algorithm.
**Arguments**
#### Arguments
_None_
**Returns**
#### Returns
A promise that resolves to an Object representing parameters required to reconstruct algorithm state.
A `Promise` that resolves to a stringify-able `Object` with parameters needed to reconstruct algorithm state.
**Example**
#### Example
```js
var Algorithm = require('egreedy');
var algorithm = new Algorithm();
const Algorithm = require('egreedy');
const algorithm = new Algorithm();
algorithm.serialize().then(function (state) { console.log(state); });
algorithm.serialize().then(state => console.log(state));
```
```js
{
arms: 2,
epsilon: 0.5,
counts: [0, 0],
values: [0, 0]
}
## Development
### Contribute
PRs are welcome! For bugs, please include a failing test which passes when your PR is applied. [Travis CI](https://travis-ci.org/kurttheviking/egreedy-js) provides on-demand testing for commits and pull requests.
### Workflow
1. Feature development and bug fixing should occur on a non-master branch.
2. Changes should be submitted to master via a [Pull Request](https://github.com/kurttheviking/egreedy-js/compare).
3. Pull Requests should be merged via a merge commit. Local "in-process" commits may be squashed prior to pushing to the remote feature branch.
To enable a git hook that runs `npm test` prior to pushing, `cd` into the local repo and run:
```sh
touch .git/hooks/pre-push
chmod +x .git/hooks/pre-push
echo "npm test" > .git/hooks/pre-push
```
### Tests
## Tests
To run the unit test suite:
```
```sh
npm test

@@ -200,14 +205,2 @@ ```

**Note:** tests against stochastic methods (e.g. `algorithm.select()`) are inherently tricky to test with deterministic assertions. The approach here is to iterate across a semi-random set of conditions to verify that each run produces valid output. So, strictly speaking, each call to `npm test` is executing a slightly different test suite. At some point, the test suite may be expanded to include a more robust test of the distribution's properties &ndash; though because of the number of runs required, would be triggered with an optional flag.
## Contribute
PRs are welcome! For bugs, please include a failing test which passes when your PR is applied. [Travis CI](https://travis-ci.org/kurttheviking/egreedy-js) provides on-demand testing for commits and pull requests.
## Caveat emptor
This implementation relies on the [native Math.random()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) which uses a seeded "random" number generator. In addition, the underlying calculations often encounter extended floating point numbers. Arm selection is therefore subject to JavaScript's floating point precision limitations. For general information about floating point issues see the [floating point guide](http://floating-point-gui.de).
While these factors generally do not impede common application, I would consider the implementation suspect within academic settings.
**Note:** Tests against stochastic methods (e.g. `Algorithm#select`) are inherently tricky to test with deterministic assertions. The approach here is to iterate across a semi-random set of conditions to verify that each run produces valid output. As a result, each test suite run encounters slightly different execution state. In the future, the test suite should be expanded to include a more robust test of the distribution's properties &ndash; though because of the number of runs required, should be triggered with an optional flag.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc