diff --git a/packages/gp/src/api.ts b/packages/gp/src/api.ts index f62f56683f..5331a64e95 100644 --- a/packages/gp/src/api.ts +++ b/packages/gp/src/api.ts @@ -1,10 +1,7 @@ import type { Fn, Fn2 } from "@thi.ng/api"; import type { IRandom } from "@thi.ng/random"; -export enum GeneType { - TERMINAL, - OP, -} +export type GeneType = "term" | "op"; /** * Type alias for S-expression based AST @@ -66,12 +63,12 @@ export interface MEPOpts extends GPOpts { } export interface TerminalGene { - type: GeneType.TERMINAL; + type: "term"; value: T; } export interface OpGene { - type: GeneType.OP; + type: "op"; op: OP; args: A[]; } diff --git a/packages/gp/src/ast.ts b/packages/gp/src/ast.ts index b79cbbcc54..cf43553116 100644 --- a/packages/gp/src/ast.ts +++ b/packages/gp/src/ast.ts @@ -2,7 +2,7 @@ import { assert } from "@thi.ng/api"; import { SYSTEM } from "@thi.ng/random"; import { iterate, iterator, repeatedly, takeWhile } from "@thi.ng/transducers"; import { Location, zipper } from "@thi.ng/zipper"; -import { ASTNode, ASTOpts, GeneType, OpGene } from "./api"; +import type { ASTNode, ASTOpts, OpGene } from "./api"; import { opNode, probabilities, terminalNode } from "./utils"; export class AST { @@ -136,7 +136,7 @@ export class AST { protected asZipper(tree: ASTNode) { return zipper>( { - branch: (x) => x.type === GeneType.OP, + branch: (x) => x.type === "op", children: (x) => (>>x).args, factory: (n, args) => opNode((>>n).op, args), diff --git a/packages/gp/src/mep.ts b/packages/gp/src/mep.ts index e33246b8c1..0dcf07a720 100644 --- a/packages/gp/src/mep.ts +++ b/packages/gp/src/mep.ts @@ -1,7 +1,7 @@ import { inRange } from "@thi.ng/math"; import { SYSTEM } from "@thi.ng/random"; import { repeatedly } from "@thi.ng/transducers"; -import { ASTNode, GeneType, MEPChromosome, MEPGene, MEPOpts } from "./api"; +import type { ASTNode, MEPChromosome, MEPGene, MEPOpts } from "./api"; import { opNode, probabilities, terminalNode } from "./utils"; export class MEP { @@ -55,7 +55,7 @@ export class MEP { const depths: number[] = []; for (let i = 0; i < chromosome.length; i++) { const gene = chromosome[i]; - if (gene.type == GeneType.TERMINAL) { + if (gene.type == "term") { res[i] = gene; depths[i] = 1; } else { diff --git a/packages/gp/src/utils.ts b/packages/gp/src/utils.ts index 7357937984..fba40a2aba 100644 --- a/packages/gp/src/utils.ts +++ b/packages/gp/src/utils.ts @@ -1,14 +1,14 @@ import { assert } from "@thi.ng/api"; import { add, choices, range } from "@thi.ng/transducers"; -import { GeneType, GPOpts, OpGene, TerminalGene } from "./api"; +import type { GPOpts, OpGene, TerminalGene } from "./api"; export const terminalNode = (value: T): TerminalGene => ({ - type: GeneType.TERMINAL, + type: "term", value, }); export const opNode = (op: OP, args: A[]): OpGene => ({ - type: GeneType.OP, + type: "op", op, args, }); diff --git a/packages/gp/test/ast.ts b/packages/gp/test/ast.ts index 001dd5b2af..021e29df8d 100644 --- a/packages/gp/test/ast.ts +++ b/packages/gp/test/ast.ts @@ -23,33 +23,33 @@ describe("gp (ast)", () => { it("generate", () => { assert.deepStrictEqual(ast.randomAST(), { - type: 1, + type: "op", op: "+", args: [ { - type: 1, + type: "op", op: "-", args: [ { - type: 0, + type: "term", value: 5, }, { - type: 0, + type: "term", value: 1, }, ], }, { - type: 1, + type: "op", op: "*", args: [ { - type: 0, + type: "term", value: 8, }, { - type: 0, + type: "term", value: 3, }, ], diff --git a/packages/gp/test/mep.ts b/packages/gp/test/mep.ts index 1f963abb91..ffc2030372 100644 --- a/packages/gp/test/mep.ts +++ b/packages/gp/test/mep.ts @@ -23,121 +23,121 @@ describe("gp (mep)", () => { it("generate", () => { assert.deepStrictEqual(ast.randomChromosome(), [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, - { type: 1, op: "*", args: [1, 1] }, - { type: 1, op: "-", args: [1, 2] }, - { type: 1, op: "-", args: [0, 0] }, - { type: 1, op: "-", args: [3, 4] }, - { type: 1, op: "*", args: [4, 0] }, - { type: 1, op: "-", args: [2, 3] }, - { type: 1, op: "/", args: [1, 4] }, - { type: 1, op: "-", args: [5, 0] }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, + { type: "op", op: "*", args: [1, 1] }, + { type: "op", op: "-", args: [1, 2] }, + { type: "op", op: "-", args: [0, 0] }, + { type: "op", op: "-", args: [3, 4] }, + { type: "op", op: "*", args: [4, 0] }, + { type: "op", op: "-", args: [2, 3] }, + { type: "op", op: "/", args: [1, 4] }, + { type: "op", op: "-", args: [5, 0] }, ]); }); it("decode", () => { assert.deepStrictEqual(ast.decodeChromosome(ast.randomChromosome()), [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, { - type: 1, + type: "op", op: "-", args: [ { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "*", args: [ { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, ], }, { - type: 1, + type: "op", op: "-", args: [ { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], @@ -145,54 +145,54 @@ describe("gp (mep)", () => { ], }, { - type: 1, + type: "op", op: "/", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "-", args: [ { - type: 1, + type: "op", op: "-", args: [ { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, ], }, ]); @@ -203,87 +203,87 @@ describe("gp (mep)", () => { ast.decodeChromosome(ast.randomChromosome(), 3), [ { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "-", args: [ { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "*", args: [ { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, ], }, { - type: 1, + type: "op", op: "-", args: [ { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], @@ -291,54 +291,54 @@ describe("gp (mep)", () => { ], }, { - type: 1, + type: "op", op: "/", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "-", args: [ { - type: 1, + type: "op", op: "-", args: [ { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, + { type: "term", value: 5 }, { - type: 1, + type: "op", op: "*", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, { - type: 1, + type: "op", op: "-", args: [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, ], }, ], }, - { type: 0, value: 5 }, + { type: "term", value: 5 }, ], }, ] @@ -347,16 +347,16 @@ describe("gp (mep)", () => { it("mutate", () => { assert.deepStrictEqual(ast.mutate(ast.randomChromosome()), [ - { type: 0, value: 0 }, - { type: 0, value: 5 }, - { type: 1, op: "*", args: [1, 0] }, - { type: 1, op: "+", args: [2, 1] }, - { type: 1, op: "-", args: [0, 0] }, - { type: 1, op: "+", args: [4, 2] }, - { type: 1, op: "/", args: [3, 1] }, - { type: 1, op: "/", args: [4, 3] }, - { type: 0, value: 5 }, - { type: 1, op: "/", args: [6, 4] }, + { type: "term", value: 0 }, + { type: "term", value: 5 }, + { type: "op", op: "*", args: [1, 0] }, + { type: "op", op: "+", args: [2, 1] }, + { type: "op", op: "-", args: [0, 0] }, + { type: "op", op: "+", args: [4, 2] }, + { type: "op", op: "/", args: [3, 1] }, + { type: "op", op: "/", args: [4, 3] }, + { type: "term", value: 5 }, + { type: "op", op: "/", args: [6, 4] }, ]); }); @@ -364,43 +364,43 @@ describe("gp (mep)", () => { const a = ast.randomChromosome(); const b = ast.randomChromosome(); assert.deepStrictEqual(b, [ - { type: 0, value: 5 }, - { type: 1, op: "*", args: [0, 0] }, - { type: 0, value: 5 }, - { type: 1, op: "-", args: [0, 0] }, - { type: 1, op: "/", args: [1, 0] }, - { type: 1, op: "*", args: [2, 1] }, - { type: 1, op: "-", args: [3, 2] }, - { type: 1, op: "-", args: [2, 1] }, - { type: 0, value: 1 }, - { type: 0, value: 6 }, + { type: "term", value: 5 }, + { type: "op", op: "*", args: [0, 0] }, + { type: "term", value: 5 }, + { type: "op", op: "-", args: [0, 0] }, + { type: "op", op: "/", args: [1, 0] }, + { type: "op", op: "*", args: [2, 1] }, + { type: "op", op: "-", args: [3, 2] }, + { type: "op", op: "-", args: [2, 1] }, + { type: "term", value: 1 }, + { type: "term", value: 6 }, ]); assert.deepStrictEqual(ast.crossoverSingle(a, b, 5), [ [ - { type: 0, value: 5 }, - { type: 0, value: 5 }, - { type: 1, op: "*", args: [1, 1] }, - { type: 1, op: "-", args: [1, 2] }, - { type: 1, op: "-", args: [0, 0] }, + { type: "term", value: 5 }, + { type: "term", value: 5 }, + { type: "op", op: "*", args: [1, 1] }, + { type: "op", op: "-", args: [1, 2] }, + { type: "op", op: "-", args: [0, 0] }, // cut - { type: 1, op: "*", args: [2, 1] }, - { type: 1, op: "-", args: [3, 2] }, - { type: 1, op: "-", args: [2, 1] }, - { type: 0, value: 1 }, - { type: 0, value: 6 }, + { type: "op", op: "*", args: [2, 1] }, + { type: "op", op: "-", args: [3, 2] }, + { type: "op", op: "-", args: [2, 1] }, + { type: "term", value: 1 }, + { type: "term", value: 6 }, ], [ - { type: 0, value: 5 }, - { type: 1, op: "*", args: [0, 0] }, - { type: 0, value: 5 }, - { type: 1, op: "-", args: [0, 0] }, - { type: 1, op: "/", args: [1, 0] }, + { type: "term", value: 5 }, + { type: "op", op: "*", args: [0, 0] }, + { type: "term", value: 5 }, + { type: "op", op: "-", args: [0, 0] }, + { type: "op", op: "/", args: [1, 0] }, // cut - { type: 1, op: "-", args: [3, 4] }, - { type: 1, op: "*", args: [4, 0] }, - { type: 1, op: "-", args: [2, 3] }, - { type: 1, op: "/", args: [1, 4] }, - { type: 1, op: "-", args: [5, 0] }, + { type: "op", op: "-", args: [3, 4] }, + { type: "op", op: "*", args: [4, 0] }, + { type: "op", op: "-", args: [2, 3] }, + { type: "op", op: "/", args: [1, 4] }, + { type: "op", op: "-", args: [5, 0] }, ], ]); }); @@ -409,16 +409,16 @@ describe("gp (mep)", () => { const a = ast.randomChromosome(); const b = ast.randomChromosome(); assert.deepStrictEqual(ast.crossoverUniform(a, b), [ - { type: 0, value: 5 }, - { type: 1, op: "*", args: [0, 0] }, - { type: 0, value: 5 }, - { type: 1, op: "-", args: [1, 2] }, - { type: 1, op: "-", args: [0, 0] }, - { type: 1, op: "-", args: [3, 4] }, - { type: 1, op: "-", args: [3, 2] }, - { type: 1, op: "-", args: [2, 1] }, - { type: 0, value: 1 }, - { type: 1, op: "-", args: [5, 0] }, + { type: "term", value: 5 }, + { type: "op", op: "*", args: [0, 0] }, + { type: "term", value: 5 }, + { type: "op", op: "-", args: [1, 2] }, + { type: "op", op: "-", args: [0, 0] }, + { type: "op", op: "-", args: [3, 4] }, + { type: "op", op: "-", args: [3, 2] }, + { type: "op", op: "-", args: [2, 1] }, + { type: "term", value: 1 }, + { type: "op", op: "-", args: [5, 0] }, ]); }); });