Skip to content

Commit

Permalink
Try removing population/survivors...but it doesnt really work
Browse files Browse the repository at this point in the history
  • Loading branch information
nicfv committed Mar 19, 2024
1 parent 3e6821f commit 2672097
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 26 deletions.
27 changes: 9 additions & 18 deletions datafit/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { Config, Datum, Fit, VariableType, fx } from './types';
* Defines the default configuration options for `fit`
*/
const defaultConfig: Config = {
generations: 100,
population: 100,
survivors: 10,
initialDeviation: 10,
generations: 1000,
initialDeviation: 100,
finalDeviation: 1,
};

Expand Down Expand Up @@ -51,26 +49,19 @@ export function fit<T extends VariableType>(f: fx<T>, data: Array<Datum<T>>, par
// Clean up a potentially incomplete config object by filling it in with default options
const conf: Config = {
generations: config.generations ?? defaultConfig.generations,
population: config.population ?? defaultConfig.population,
survivors: config.survivors ?? defaultConfig.survivors,
initialDeviation: config.initialDeviation ?? defaultConfig.initialDeviation,
finalDeviation: config.finalDeviation ?? defaultConfig.finalDeviation,
};
const census: Array<Fit> = [];
let best: Fit = { params: params_initial, err: err(f, params_initial, data) };
for (let generation = 0; generation < conf.generations; generation++) {
for (let i = 0; i < conf.population; i++) {
// Mutate a random parent from the prior generation of survivors
const params: Array<number> = mutate(
census[randInt(0, conf.survivors)]?.params ?? params_initial,
SMath.translate(generation, 0, conf.generations, conf.initialDeviation, conf.finalDeviation)
);
census.push({ params: params, err: err(f, params, data) });
// Mutate a random parent from the prior generation of survivors
const params: Array<number> = mutate(best.params, SMath.translate(generation, 0, conf.generations, conf.initialDeviation, conf.finalDeviation)),
error: number = err(f, params, data);
if (error < best.err) {
best = { params: params, err: error };
}
// Sort by increasing error and only keep the survivors
census.sort((x, y) => x.err - y.err);
census.splice(conf.survivors);
}
return census[0];
return best;
}
/**
* Calculate the sum of squared errors for a set of function parameters.
Expand Down
8 changes: 0 additions & 8 deletions datafit/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ export interface Config {
* Determines the number of generations, or iterations.
*/
readonly generations: number;
/**
* Determines the number of parameters sets to generate.
*/
readonly population: number;
/**
* Determines how many survivors remain after every generation.
*/
readonly survivors: number;
/**
* Determines the percentage of how much a set of parameters can mutate on the first generation.
*/
Expand Down

0 comments on commit 2672097

Please sign in to comment.