lambda-optimize
v1.1.0
Published
A helpful tool for optimization in AWS Lambda Functions.
Downloads
7
Readme
lambda-optimize
- Install from npm:
npm install lambda-optimize --save
- Require:
const optimize = require('lambda-optimize');
- 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
- Caveats
- Optimization will only be as performant as the quality of the
fitness
function, andmutateState
function provided to it. - 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 ininitialState
, the state object itself provided ininitialState
is returned. - 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.
- Optimization will only be as performant as the quality of the