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IACtB,GAAU,IAAN,GAAsB,IAAX,EACb,OAAO,EAAa,KAAK,KAAK,SAAS,IAEzC,MAAM,EAAK,KAAK,KAAK,IAAI,EAAS,GAC5B,EAAO,IAAI,GAAW,IAAM,EAAI,MAAQ,GAAG,EAAG,QACpD,OAAO,EAAO,EAAG,GAAG,EAAK,GAAO,EACpC,S","file":"/npm/@thi.ng/gp@1.0.47/index.js/+esm","sourceRoot":"","sourcesContent":["/**\n * Bundled by jsDelivr using Rollup v2.79.2 and Terser v5.39.0.\n * Original file: /npm/@thi.ng/gp@1.0.47/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { assert } from \"@thi.ng/errors/assert\";\nimport { add } from \"@thi.ng/transducers/add\";\nimport { choices } from \"@thi.ng/transducers/choices\";\nimport { range } from \"@thi.ng/transducers/range\";\nconst terminalNode = (value) => ({\n  type: \"term\",\n  value\n});\nconst opNode = (op, args) => ({\n  type: \"op\",\n  op,\n  args\n});\nconst probabilities = (opts) => {\n  const probabilities2 = opts.ops.map((op) => op.prob);\n  const psum = add(probabilities2);\n  assert(psum < 1, \"total op probabilities MUST be < 1\");\n  return {\n    iter: choices(\n      [...range(probabilities2.length + 1)],\n      [1 - psum, ...probabilities2],\n      opts.rnd\n    ),\n    probTerminal: 1 - psum\n  };\n};\nexport {\n  opNode,\n  probabilities,\n  terminalNode\n};\n","import { assert } from \"@thi.ng/errors/assert\";\nimport { SYSTEM } from \"@thi.ng/random/system\";\nimport { iterate } from \"@thi.ng/transducers/iterate\";\nimport { iterator } from \"@thi.ng/transducers/iterator\";\nimport { repeatedly } from \"@thi.ng/transducers/repeatedly\";\nimport { takeWhile } from \"@thi.ng/transducers/take-while\";\nimport { zipper } from \"@thi.ng/zipper/zipper\";\nimport { opNode, probabilities, terminalNode } from \"./utils.js\";\nclass AST {\n  opts;\n  choices;\n  probTerminal;\n  constructor(opts) {\n    this.opts = { rnd: SYSTEM, ...opts };\n    assert(this.opts.probMutate < 1, \"mutation probability must be < 1.0\");\n    const probs = probabilities(this.opts);\n    this.probTerminal = probs.probTerminal;\n    this.choices = probs.iter;\n  }\n  /**\n   * Returns a random AST with given max tree depth. The max depth\n   * provided in {@link ASTOpts} is used by default.\n   *\n   * @param maxDepth -\n   */\n  randomAST(maxDepth = this.opts.maxDepth) {\n    return this.randomASTNode(0, maxDepth);\n  }\n  /**\n   * Immutably transplants a randomly chosen subtree of `parent2` into\n   * a randomly chosen location in `parent1` and vice versa. Returns 2\n   * new trees.\n   *\n   * @param parent1 -\n   * @param parent2 -\n   */\n  crossoverSingle(parent1, parent2) {\n    return [\n      this.selectRandomNode(parent1).replace(\n        this.selectRandomNode(parent2).node\n      ).root,\n      this.selectRandomNode(parent2).replace(\n        this.selectRandomNode(parent1).node\n      ).root\n    ];\n  }\n  /**\n   * Probilistically replaces randomly chosen tree nodes with a new random AST\n   * of given `maxDepth` (default: 1). Never mutates root.\n   *\n   * @remarks\n   * The AST's pre-configured max tree depth will always be respected, so\n   * depending on the depth of the selected mutation location, the randomly\n   * inserted/replaced subtree might be more shallow than given `maxDepth`.\n   * I.e. if a tree node at max depth is selected for mutation, it will always\n   * result in a randomly chosen terminal node only.\n   *\n   * @param tree -\n   * @param maxDepth -\n   */\n  mutate(tree, maxDepth = 1) {\n    const { rnd, probMutate, maxDepth: limit } = this.opts;\n    let loc = this.asZipper(tree).next;\n    if (!loc) return tree;\n    while (true) {\n      let nextLoc;\n      if (rnd.probability(probMutate)) {\n        loc = loc.replace(\n          this.randomASTNode(0, Math.min(limit - loc.depth, maxDepth))\n        );\n        nextLoc = loc.right;\n        if (!nextLoc) {\n          nextLoc = loc.up;\n          if (nextLoc) {\n            nextLoc = nextLoc.right;\n          }\n        }\n      } else {\n        nextLoc = loc.next;\n      }\n      if (!nextLoc) return loc.root;\n      loc = nextLoc;\n    }\n  }\n  /**\n   * Returns linearized/flat version of given AST as an array of [zipper\n   * locations](https://docs.thi.ng/umbrella/zipper/classes/Location.html).\n   *\n   * @param tree -\n   */\n  linearizedAST(tree) {\n    return [\n      ...iterator(\n        takeWhile((x) => !!x),\n        iterate((x) => x.next, this.asZipper(tree))\n      )\n    ];\n  }\n  /**\n   * Returns random {@link ASTNode} from given tree, using the user provided\n   * PRNG (via `opts`) or\n   * [`SYSTEM`](https://docs.thi.ng/umbrella/random/variables/SYSTEM.html).\n   * The returned value is a [zipper\n   * location](https://docs.thi.ng/umbrella/zipper/classes/Location.html) of\n   * the selected node.\n   *\n   * @remarks\n   * The actual `ASTNode` can be obtained via `.node`.\n   *\n   * Only nodes in the linearized index range `[min..max)` are returned\n   * (default: entire tree range). Since the linear tree length isn't known\n   * beforehand, `max` < 0 (default) is equivalent to the linearized tree end.\n   *\n   * @param tree -\n   * @param min -\n   * @param max -\n   */\n  selectRandomNode(tree, min = 0, max = -1) {\n    const rnd = this.opts.rnd;\n    const linTree = this.linearizedAST(tree);\n    let node;\n    max < 0 && (max = linTree.length);\n    node = linTree[rnd.minmax(min, max) | 0];\n    return node;\n  }\n  randomASTNode(d, maxDepth) {\n    const rnd = this.opts.rnd;\n    const geneID = this.choices.next().value;\n    if (geneID === 0 || d >= maxDepth)\n      return terminalNode(this.opts.terminal(rnd));\n    const op = this.opts.ops[geneID - 1];\n    const children = [\n      ...repeatedly(() => this.randomASTNode(d + 1, maxDepth), op.arity)\n    ];\n    return opNode(op.fn(rnd, children), children);\n  }\n  asZipper(tree) {\n    return zipper(\n      {\n        branch: (x) => x.type === \"op\",\n        children: (x) => x.args,\n        factory: (n, args) => opNode(n.op, args)\n      },\n      tree\n    );\n  }\n}\nexport {\n  AST\n};\n","import { inRange } from \"@thi.ng/math/interval\";\nimport { SYSTEM } from \"@thi.ng/random/system\";\nimport { repeatedly } from \"@thi.ng/transducers/repeatedly\";\nimport { opNode, probabilities, terminalNode } from \"./utils.js\";\nclass MEP {\n  opts;\n  probTerminal;\n  choices;\n  constructor(opts) {\n    this.opts = { rnd: SYSTEM, ...opts };\n    const probs = probabilities(this.opts);\n    this.probTerminal = probs.probTerminal;\n    this.choices = probs.iter;\n  }\n  /**\n   * Returns a new random {@link MEPChromosome} with the configured\n   * probabilities of operator and terminal nodes (constants).\n   *\n   * @remarks\n   * See {@link MEP.decodeChromosome} for conversion to\n   * {@link ASTNode}s.\n   *\n   */\n  randomChromosome() {\n    const res = [];\n    for (let i = 0, n = this.opts.chromoSize; i < n; i++) {\n      res[i] = this.randomGene(i);\n    }\n    return res;\n  }\n  /**\n   * Decodes given chromosome into an array of {@link ASTNode}s and\n   * optionally applies tree depth filter (by default includes all).\n   *\n   * @remarks\n   * A {@link MEPChromosome} encodes multiple solutions (one per gene\n   * slot), therefore a chromosome of length `n` will produce the same\n   * number ASTs (less if min/max tree depth filters are applied).\n   *\n   * @param chromosome -\n   * @param minDepth -\n   * @param maxDepth -\n   */\n  decodeChromosome(chromosome, minDepth = 0, maxDepth = Infinity) {\n    const res = [];\n    const depths = [];\n    for (let i = 0; i < chromosome.length; i++) {\n      const gene = chromosome[i];\n      if (gene.type == \"term\") {\n        res[i] = gene;\n        depths[i] = 1;\n      } else {\n        res[i] = opNode(\n          gene.op,\n          gene.args.map((g) => res[g])\n        );\n        depths[i] = 1 + gene.args.reduce((d, a) => Math.max(d, depths[a]), 0);\n      }\n    }\n    return res.filter((_, i) => inRange(depths[i], minDepth, maxDepth));\n  }\n  crossoverSingle(chromo1, chromo2, cut) {\n    cut = cut !== void 0 ? cut : this.opts.rnd.int() % Math.min(chromo1.length, chromo2.length);\n    return [\n      chromo1.slice(0, cut).concat(chromo2.slice(cut)),\n      chromo2.slice(0, cut).concat(chromo1.slice(cut))\n    ];\n  }\n  crossoverUniform(chromo1, chromo2) {\n    const rnd = this.opts.rnd;\n    const res = [];\n    const minLen = Math.min(chromo1.length, chromo2.length);\n    for (let i = 0; i < minLen; i++) {\n      res[i] = rnd.probability(0.5) ? chromo1[i] : chromo2[i];\n    }\n    return chromo1.length > minLen ? res.concat(chromo1.slice(minLen)) : chromo2.length > minLen ? res.concat(chromo2.slice(minLen)) : res;\n  }\n  mutate(chromo) {\n    const { rnd, probMutate } = this.opts;\n    const res = new Array(chromo.length);\n    for (let i = chromo.length; i-- > 0; ) {\n      res[i] = rnd.probability(probMutate) ? this.randomGene(i) : chromo[i];\n    }\n    return res;\n  }\n  randomGene(i) {\n    const geneID = this.choices.next().value;\n    const rnd = this.opts.rnd;\n    if (i === 0 || geneID === 0) {\n      return terminalNode(this.opts.terminal(rnd));\n    }\n    const op = this.opts.ops[geneID - 1];\n    const args = [...repeatedly(() => rnd.int() % i, op.arity)];\n    return opNode(op.fn(rnd, args), args);\n  }\n}\nexport {\n  MEP\n};\n"]}