lambda-optimize

1.1.0 • Public • Published

lambda-optimize

  1. Install from npm:
npm install lambda-optimize --save
  1. Require:
const optimize = require('lambda-optimize');
  1. Optimize:
/* In this trivial example we want 'a' to equal 50
   and we want 'b' to equal 100. */
const optimized = optimize({
  iterations: 10000,
  initialState: () => {
    const state = {
      a: 10,
      b: 20
    };
    return state;
  },
  mutateState: (state) => {
    const mutantState = Object.assign({}, state);
    mutantState.a += (Math.random() * 2) - 1;
    mutantState.b += (Math.random() * 2) - 1;
    return mutantState;
  },
  fitness: (state) => {
    const aTarget = 50;
    const bTarget = 100;
    let fitness = 0;
    fitness += Math.abs(state.a - aTarget);
    fitness += Math.abs(state.b - bTarget);
    return fitness;
  }
});
const optimizedState = optimized.state; // { a: 50.00927231281881, b: 100.00184294400155 }
const optimizedFitness = optimized.fitness; // 0.011115256820360742
  1. Caveats
    1. Optimization will only be as performant as the quality of the fitness function, and mutateState function provided to it.
    2. If you perform mutateState in an immutable way (either manually or with a library like immutable.js) all internal operations of the library will be immutable. In the event no better state is found than the state object provided in initialState, the state object itself provided in initialState is returned.
    3. The internal process to optimize is a simple hill climbing algorithm (ie. mutate and replace is better), there is no artificial genetics or other algorithms taking place.

Readme

Keywords

none

Package Sidebar

Install

npm i lambda-optimize

Weekly Downloads

2

Version

1.1.0

License

ISC

Last publish

Collaborators

  • adrian_seeley