genetic-search
Version:
Multiprocessing genetic algorithm implementation library
172 lines • 6.02 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchedulerConditionException = exports.Scheduler = void 0;
exports.checkSchedulerCondition = checkSchedulerCondition;
var utils_1 = require("./utils");
/**
* A scheduler for a genetic search algorithm.
*
* The scheduler is responsible for executing scheduled tasks or operations
* in the genetic search algorithm.
*
* @template TGenome The type of genome objects in the population.
* @template TConfig The type of configuration object of macro parameters,
* which the scheduler will be able to manipulate.
*
* @category Scheduler
*/
var Scheduler = /** @class */ (function () {
/**
* Constructor of the Scheduler class.
*
* @param params - The parameters to initialize the scheduler.
*/
function Scheduler(params) {
var _this = this;
/**
* An array of log messages generated by the scheduler.
*/
this.logs = [];
/**
* The history of population summaries.
*/
this.history = [];
this.runner = params.runner;
this.config = params.config;
this.actions = params.actions;
this.maxHistoryLength = params.maxHistoryLength;
this.logger = function (message) {
_this.logs.push(message);
};
}
/**
* Executes a single step or iteration in the scheduler.
*/
Scheduler.prototype.step = function (evaluatedPopulation) {
var e_1, _a;
this.clearLogs();
this.handleHistory();
try {
for (var _b = __values(this.actions), _c = _b.next(); !_c.done; _c = _b.next()) {
var rule = _c.value;
try {
rule(this.getRuleInput(evaluatedPopulation));
}
catch (e) {
if (e.name === 'SchedulerConditionException') {
continue;
}
throw e;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
/**
* Handles the history of population summaries.
*
* Adds the current population summary to the history. If the history
* exceeds the maximum allowed length, it trims the oldest entries.
*/
Scheduler.prototype.handleHistory = function () {
this.history.push(this.runner.getPopulationSummary());
if (this.history.length >= this.maxHistoryLength) {
this.history = this.history.slice(this.history.length - this.maxHistoryLength);
}
};
/**
* Constructs the input data for a scheduler rule.
*
* This input is used by both the `condition` and `action` functions
* of a scheduler rule.
*
* @returns An object containing the runner, history, config, and logger.
*/
Scheduler.prototype.getRuleInput = function (evaluatedPopulation) {
var evaluatedPopulationManager = new utils_1.ArrayManager(evaluatedPopulation);
return {
runner: this.runner,
evaluatedPopulation: evaluatedPopulation,
evaluatedPopulationManager: evaluatedPopulationManager,
history: this.history,
config: this.config,
logger: this.logger,
};
};
/**
* Clears all the logs stored in the scheduler.
*/
Scheduler.prototype.clearLogs = function () {
this.logs.length = 0;
};
return Scheduler;
}());
exports.Scheduler = Scheduler;
/**
* An exception thrown when a scheduler condition is not satisfied.
*
* This exception is thrown when a scheduler rule's condition function
* returns false.
*
* @category Exceptions
* @category Scheduler
*/
var SchedulerConditionException = /** @class */ (function (_super) {
__extends(SchedulerConditionException, _super);
function SchedulerConditionException() {
var _this = _super.call(this) || this;
_this.name = 'SchedulerConditionException';
return _this;
}
return SchedulerConditionException;
}(Error));
exports.SchedulerConditionException = SchedulerConditionException;
/**
* Checks if a scheduler condition is satisfied.
*
* If the condition is not satisfied, this function throws a {@link SchedulerConditionException}`.
*
* @see SchedulerConditionException
*
* @param condition - The result of the scheduler condition check.
*
* @category Scheduler
*/
function checkSchedulerCondition(condition) {
if (!condition) {
throw new SchedulerConditionException();
}
}
//# sourceMappingURL=scheduler.js.map