genetic-search
Version:
Multiprocessing genetic algorithm implementation library
124 lines • 3.64 kB
JavaScript
import { ArrayManager } from "./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
*/
export class Scheduler {
/**
* Constructor of the Scheduler class.
*
* @param params - The parameters to initialize the scheduler.
*/
constructor(params) {
/**
* 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 = (message) => {
this.logs.push(message);
};
}
/**
* Executes a single step or iteration in the scheduler.
*/
step(evaluatedPopulation) {
this.clearLogs();
this.handleHistory();
for (const rule of this.actions) {
try {
rule(this.getRuleInput(evaluatedPopulation));
}
catch (e) {
if (e.name === 'SchedulerConditionException') {
continue;
}
throw e;
}
}
}
/**
* 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.
*/
handleHistory() {
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.
*/
getRuleInput(evaluatedPopulation) {
const evaluatedPopulationManager = new ArrayManager(evaluatedPopulation);
return {
runner: this.runner,
evaluatedPopulation,
evaluatedPopulationManager,
history: this.history,
config: this.config,
logger: this.logger,
};
}
/**
* Clears all the logs stored in the scheduler.
*/
clearLogs() {
this.logs.length = 0;
}
}
/**
* 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
*/
export class SchedulerConditionException extends Error {
constructor() {
super();
this.name = '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
*/
export function checkSchedulerCondition(condition) {
if (!condition) {
throw new SchedulerConditionException();
}
}
//# sourceMappingURL=scheduler.js.map