UNPKG

ml-q-learning

Version:

Library implementing the q-learning algorithm and several exploration algorithms.

93 lines (92 loc) 3.36 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); class MapInMemory { constructor() { this.map = new Map(); } size() { return __awaiter(this, void 0, void 0, function* () { return this.map.size; }); } setState(stateSerialized, actionsStats) { return __awaiter(this, void 0, void 0, function* () { this.map.set(stateSerialized, actionsStats); }); } setStateBulk(states) { return __awaiter(this, void 0, void 0, function* () { states.forEach(([stateSerialized, actionsStats]) => this.setState(stateSerialized, actionsStats)); }); } hasState(stateSerialized) { return __awaiter(this, void 0, void 0, function* () { return this.map.has(stateSerialized); }); } getState(stateSerialized) { return __awaiter(this, void 0, void 0, function* () { const value = this.map.get(stateSerialized); if (!value) { throw new Error(`Missing state ${stateSerialized}`); } return value; }); } eachState(callback) { return __awaiter(this, void 0, void 0, function* () { this.map.forEach((value, key) => callback(key, value)); }); } setInfo(info) { return __awaiter(this, void 0, void 0, function* () { this.trainingInfo = info; }); } hasInfo() { return __awaiter(this, void 0, void 0, function* () { return this.trainingInfo !== undefined; }); } getInfo() { return __awaiter(this, void 0, void 0, function* () { if (!this.trainingInfo) { throw new Error('Trainig info not initialized.'); } return this.trainingInfo; }); } restore(content) { return __awaiter(this, void 0, void 0, function* () { for (let index = 0; index < content.Q.length; index++) { const [stateSerialized, actionsStats] = content.Q[index]; yield this.setState(stateSerialized, actionsStats); } if (content.trainingInfo) { yield this.setInfo(content.trainingInfo); } }); } backup() { return __awaiter(this, void 0, void 0, function* () { const info = yield this.getInfo(); const context = { Q: [], trainingInfo: info }; yield this.eachState((stateSerialized, actionsStats) => { context.Q.push([stateSerialized, actionsStats]); }); return context; }); } } exports.MapInMemory = MapInMemory;