UNPKG

@thi.ng/gp

Version:

Genetic programming helpers & strategies (tree based & multi-expression programming)

32 lines (31 loc) 734 B
import { assert } from "@thi.ng/errors/assert"; import { add } from "@thi.ng/transducers/add"; import { choices } from "@thi.ng/transducers/choices"; import { range } from "@thi.ng/transducers/range"; const terminalNode = (value) => ({ type: "term", value }); const opNode = (op, args) => ({ type: "op", op, args }); const probabilities = (opts) => { const probabilities2 = opts.ops.map((op) => op.prob); const psum = add(probabilities2); assert(psum < 1, "total op probabilities MUST be < 1"); return { iter: choices( [...range(probabilities2.length + 1)], [1 - psum, ...probabilities2], opts.rnd ), probTerminal: 1 - psum }; }; export { opNode, probabilities, terminalNode };