UNPKG

maths.ts

Version:

Math utilities library for TypeScript, JavaScript and Node.js

80 lines (79 loc) 2.81 kB
"use strict"; /** * @author Hector J. Vasquez <ipi.vasquez@gmail.com> * * @licence * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const _debug = require("debug"); const debug = _debug('mh::ts'); /** * * @param problem The problem which must have a solution generator, a * calculator for the solution and a neighbor generator for each solution. * @param nNeighbors The number of neighbors for the solution generated each * cycle. * @param kDiffer The percentage change of each neighbor from the solution. * @param tlSize The taboo list size. * @param iterations The number of iterations executing the algorithm. * @returns The solution found by this algorithm. */ function tabooSearch(problem, nNeighbors = 5, kDiffer = 2, tlSize = 30, iterations = 100) { // Generates an initial solution const sol = problem.generateSolution(); const tabooList = []; let bestS = sol; debug('Initial sol: ' + bestS); let bestVal = problem.solutionValue(bestS); for (let i = 0; i < iterations; i++) { const neighbors = problem.generateNeighbors(sol, kDiffer, nNeighbors); const candidates = []; neighbors.forEach(n => { debug('%s=%d', '' + n, problem.solutionValue(n)); if (!isIn(n, tabooList)) { candidates.push(n); } }); let bestC = candidates[0]; let bestCVal = 0; candidates.forEach(c => { const cVal = problem.solutionValue(c); if (cVal > bestCVal) { bestCVal = cVal; bestC = c; } }); debug('Picked: %s=%d', '' + bestC, bestCVal); if (bestCVal > bestVal) { debug('Solution updated to ' + bestC); bestS = bestC; bestVal = bestCVal; tabooList.unshift(bestC); if (tabooList.length >= tlSize) { tabooList.pop(); } } } debug(tabooList); return bestS; function isIn(e, array) { for (let i = 0; i < array.length; i++) { if (problem.compareSolutions(array[i], e)) { return true; } } return false; } } exports.tabooSearch = tabooSearch;