-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
187 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Creates an experience object | ||
* | ||
* @param state the current state | ||
* @param action the current action | ||
* @param reward the reward for the current action in the current state | ||
* @param nextState the state following by the current action in the current state | ||
* @param isFinalState Does the game ends at this state? | ||
* @constructor | ||
*/ | ||
function Experience(state, action, reward, nextState, isFinalState) { | ||
this.state = state; | ||
this.action = action; | ||
this.reward = reward; | ||
this.nextState = nextState; | ||
this.isFinalState = isFinalState; | ||
this.loss = 0; | ||
} | ||
|
||
module.exports = Experience; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const Experience = require('./experience'); | ||
|
||
/** | ||
* Creates a replay buffer with a maximum size of experience entries. | ||
* | ||
* @param maxSize maximum number of experiences | ||
* @constructor | ||
*/ | ||
function ReplayBuffer(maxSize) { | ||
this.buffer = []; | ||
this.maxSize = maxSize; | ||
} | ||
|
||
ReplayBuffer.prototype = { | ||
/** | ||
* Adds an experience entry to the buffer. | ||
* | ||
* @param {Experience} experience the experience to add | ||
*/ | ||
add: function(experience) { | ||
if (this.buffer.length >= this.maxSize) { | ||
this.buffer.shift(); | ||
} | ||
this.buffer.push(experience); | ||
}, | ||
|
||
/** | ||
* Get a random mini batch of given size. | ||
* | ||
* @param {number} size the size of the minibatch. | ||
* | ||
* @returns {Experience[]} a batch of Experiences to train from. | ||
*/ | ||
getRandomMiniBatch: function(size) { | ||
//Size can't be bigger than this.buffer.length | ||
size = Math.min(size, this.buffer.length); | ||
if (size === this.buffer.length) { | ||
return this.buffer; | ||
} | ||
|
||
let bufferCopy = [...this.buffer]; | ||
let batch = []; | ||
|
||
for (let i = 0; i < size; i++) { | ||
//Add an random experience to the batch and remove it from the bufferCopy | ||
batch.push(...bufferCopy.splice(Math.floor(Math.random() * bufferCopy.length), 1)); | ||
} | ||
return batch; | ||
}, | ||
}; | ||
|
||
module.exports = ReplayBuffer; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.