Skip to content

Commit aa7a338

Browse files
Merge pull request #1 from robertleeplummerjr/master
Upgade to typescript
2 parents a671169 + 7dc3567 commit aa7a338

25 files changed

+10759
-4956
lines changed

.eslintrc

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
{
2-
"extends": "airbnb/base",
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": [
5+
"@typescript-eslint"
6+
],
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/eslint-recommended",
10+
"plugin:@typescript-eslint/recommended"
11+
],
12+
"env": {
13+
"node": true
14+
},
315
"rules": {
416
"comma-dangle": 0,
517
"max-len": [2, 100, 2],
618
"prefer-rest-params": 0
7-
}
19+
},
20+
"ignorePatterns": ["out"]
821
}

README.md

+45-43
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ucb
55

66
**An upper confidence bounds algorithm for multi-armed bandit problems**
77

8-
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).
8+
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). Now written in Typescript!
99

1010

1111
## Get started
@@ -33,69 +33,71 @@ This implementation often encounters extended floating point numbers. Arm select
3333
1. Create an optimizer with `3` arms:
3434

3535
```js
36-
const Algorithm = require('ucb');
36+
const { Ucb } = require('ucb');
3737

38-
const algorithm = new Algorithm({
38+
const ucb = new Ucb({
3939
arms: 3
4040
});
4141
```
4242

43-
2. Select an arm (exploits or explores, determined by the algorithm):
43+
2. Select an arm (exploits or explores, determined by ucb):
4444

4545
```js
46-
algorithm.select().then((arm) => {
46+
ucb.select().then((arm) => {
4747
// do something based on the chosen arm
4848
});
49+
// or
50+
const arm = ucb.selectSync();
4951
```
5052

5153
3. Report the reward earned from a chosen arm:
5254

5355
```js
54-
algorithm.reward(arm, value);
56+
ucb.reward(arm, value);
5557
```
5658

5759

5860
## API
5961

60-
### `Algorithm(config)`
62+
### `Ucb(config)`
6163

62-
Creates a new optimization algorithm.
64+
Creates a new Ucb.
6365

6466
#### Arguments
6567

66-
- `config` (`Object`): algorithm instance parameters
68+
- `config` (`Object`): ucb instance parameters
6769

6870
The `config` object supports two optional parameters:
6971

7072
- `arms` (`Number`, Integer): The number of arms over which the optimization will operate; defaults to `2`
7173

72-
Alternatively, the `state` object resolved from [`Algorithm#serialize`](https://github.com/kurttheviking/ucb-js#algorithmserialize) can be passed as `config`.
74+
Alternatively, the `state` object resolved from [`Ucb#serialize`](https://github.com/kurttheviking/ucb-js#algorithmserialize) can be passed as `config`.
7375

7476
#### Returns
7577

76-
An instance of the ucb optimization algorithm.
78+
An instance of Ucb.
7779

7880
#### Example
7981

8082
```js
81-
const Algorithm = require('ucb');
82-
const algorithm = new Algorithm();
83+
const { Ucb } = require('ucb');
84+
const ucb = new Ucb();
8385
84-
assert.equal(algorithm.arms, 2);
86+
assert.equal(ucb.arms, 2);
8587
```
8688

8789
Or, with a passed `config`:
8890

8991
```js
90-
const Algorithm = require('ucb');
91-
const algorithm = new Algorithm({ arms: 4 });
92+
const { Ucb } = require('ucb');
93+
const ucb = new Ucb({ arms: 4 });
9294
93-
assert.equal(algorithm.arms, 4);
95+
assert.equal(ucb.arms, 4);
9496
```
9597

96-
### `Algorithm#select()`
98+
### `Ucb#select()`
9799

98-
Choose an arm to play, according to the optimization algorithm.
100+
Choose an arm to play, according to Ucb.
99101

100102
#### Arguments
101103

@@ -108,53 +110,61 @@ A `Promise` that resolves to a `Number` corresponding to the associated arm inde
108110
#### Example
109111

110112
```js
111-
const Algorithm = require('ucb');
112-
const algorithm = new Algorithm();
113+
const { Ucb } = require('ucb');
114+
const ucb = new Ucb();
113115
114-
algorithm.select().then(arm => console.log(arm));
116+
ucb.select().then(arm => console.log(arm));
117+
// or
118+
const arm = ucb.selectSync();
115119
```
116120

117-
### `Algorithm#reward(arm, reward)`
121+
### `Ucb#reward(arm, reward)`
118122

119-
Inform the algorithm about the payoff from a given arm.
123+
Inform Ucb about the payoff from a given arm.
120124

121125
#### Arguments
122126

123-
- `arm` (`Number`, Integer): the arm index (provided from `Algorithm#select()`)
127+
- `arm` (`Number`, Integer): the arm index (provided from `Ucb#select()`)
124128
- `reward` (`Number`): the observed reward value (which can be 0 to indicate no reward)
125129

126130
#### Returns
127131

128-
A `Promise` that resolves to an updated instance of the algorithm. (The original instance is mutated as well.)
132+
A `Promise` that resolves to an updated instance of ucb. (The original instance is mutated as well.)
129133

130134
#### Example
131135

132136
```js
133-
const Algorithm = require('ucb');
134-
const algorithm = new Algorithm();
137+
const { Ucb } = require('ucb');
138+
const ucb = new Ucb();
135139
136-
algorithm.reward(0, 1).then(updatedAlgorithm => console.log(updatedAlgorithm));
140+
ucb.reward(0, 1).then(updatedUcb => console.log(updatedUcb));
141+
// or
142+
const updatedUcb = ucb.rewardSync(0, 1);
143+
console.log(updatedUcb);
137144
```
138145

139-
### `Algorithm#serialize()`
146+
### `Ucb#serialize()`
140147

141-
Obtain a plain object representing the internal state of the algorithm.
148+
Obtain a plain object representing the internal state of ucb.
142149

143150
#### Arguments
144151

145152
_None_
146153

147154
#### Returns
148155

149-
A `Promise` that resolves to a stringify-able `Object` with parameters needed to reconstruct algorithm state.
156+
A `Promise` that resolves to a stringify-able `Object` with parameters needed to reconstruct ucb state.
150157

151158
#### Example
152159

153160
```js
154-
const Algorithm = require('ucb');
155-
const algorithm = new Algorithm();
161+
const { Ucb } = require('ucb');
162+
const ucb = new Ucb();
156163
157-
algorithm.serialize().then(state => console.log(state));
164+
ucb.serialize().then(state => console.log(state));
165+
// or
166+
const state = ucb.serializeSync();
167+
console.log(state);
158168
```
159169

160170

@@ -170,14 +180,6 @@ PRs are welcome! For bugs, please include a failing test which passes when your
170180
2. Changes should be submitted to master via a [Pull Request](https://github.com/kurttheviking/ucb-js/compare).
171181
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.
172182

173-
To enable a git hook that runs `npm test` prior to pushing, `cd` into the local repo and run:
174-
175-
```sh
176-
touch .git/hooks/pre-push
177-
chmod +x .git/hooks/pre-push
178-
echo "npm test" > .git/hooks/pre-push
179-
```
180-
181183
### Tests
182184

183185
To run the unit test suite:

index.d.ts

-27
This file was deleted.

index.js

-1
This file was deleted.

jest.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
testMatch: ['**/?(*.)+(test).ts?(x)'],
5+
testPathIgnorePatterns: ['/node_modules/', '/out/'],
6+
setupFiles: ['reflect-metadata', 'jest-ts-auto-mock'],
7+
globals: {
8+
'ts-jest': {
9+
compiler: 'ttypescript',
10+
},
11+
},
12+
};

lib/Algorithm.js

-48
This file was deleted.

lib/Algorithm/reward.js

-28
This file was deleted.

lib/Algorithm/serialize.js

-15
This file was deleted.

lib/utils/promisify.js

-9
This file was deleted.

lib/utils/sum.js

-9
This file was deleted.

opt/tonic.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint-disable */
22

3-
const Algorithm = require('ucb');
3+
const { Ucb } = require('ucb');
44

5-
const algorithm = new Algorithm();
5+
const ucb = new Ucb();
66

7-
algorithm.select().then((arm) => {
7+
ucb.select().then((arm) => {
88
console.log('chose arm', arm);
9-
return algorithm.reward(arm, 1);
9+
return ucb.reward(arm, 1);
1010
})
11-
.then(() => algorithm.serialize())
11+
.then(() => ucb.serialize())
1212
.then(state => console.log('new state', JSON.stringify(state, null, 2)));

0 commit comments

Comments
 (0)