|
| 1 | +class Node { |
| 2 | + constructor(value) { |
| 3 | + this.value = value |
| 4 | + this.children = [] |
| 5 | + } |
| 6 | + |
| 7 | + get length() { |
| 8 | + if (typeof this.value === 'function' || this.value instanceof VectorizedFunction) { |
| 9 | + return this.value.length |
| 10 | + } |
| 11 | + return 0 |
| 12 | + } |
| 13 | + |
| 14 | + addChild(child) { |
| 15 | + this.children.push(child) |
| 16 | + } |
| 17 | + |
| 18 | + copy() { |
| 19 | + const node = new Node(this.value) |
| 20 | + node.children = this.children.map(c => c.copy()) |
| 21 | + return node |
| 22 | + } |
| 23 | + |
| 24 | + evaluate(env) { |
| 25 | + if (typeof this.value === 'function') { |
| 26 | + return this.value(...this.children.map(child => child.evaluate(env))) |
| 27 | + } |
| 28 | + if (this.value instanceof VectorizedFunction) { |
| 29 | + return this.value.evaluate(...this.children.map(child => child.evaluate(env))) |
| 30 | + } |
| 31 | + if (Object.hasOwn(env, this.value)) { |
| 32 | + return env[this.value] |
| 33 | + } |
| 34 | + return this.value |
| 35 | + } |
| 36 | + |
| 37 | + toString() { |
| 38 | + if (this.value instanceof VectorizedFunction) { |
| 39 | + return this.value.toString(...this.children.map(child => child.toString())) |
| 40 | + } |
| 41 | + if (typeof this.value === 'function') { |
| 42 | + return `${this.value.name}(${this.children.map(child => child.toString()).join(', ')})` |
| 43 | + } |
| 44 | + return `${this.value}` |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class VectorizedFunction { |
| 49 | + constructor(func, name) { |
| 50 | + this._originalfunc = func |
| 51 | + this._name = name |
| 52 | + this._vectorizedFunc = (...args) => { |
| 53 | + if (args.every(v => !Array.isArray(v))) { |
| 54 | + return this._originalfunc(...args) |
| 55 | + } |
| 56 | + const length = args.reduce((l, v) => (Array.isArray(v) ? Math.max(l, v.length) : l), 1) |
| 57 | + const result = [] |
| 58 | + for (let i = 0; i < length; i++) { |
| 59 | + result[i] = this._originalfunc(...args.map(v => (Array.isArray(v) ? v[i] : v))) |
| 60 | + } |
| 61 | + return result |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + get length() { |
| 66 | + return this._originalfunc.length |
| 67 | + } |
| 68 | + |
| 69 | + get name() { |
| 70 | + return this._originalfunc.name || this._name |
| 71 | + } |
| 72 | + |
| 73 | + evaluate(...args) { |
| 74 | + return this._vectorizedFunc(...args) |
| 75 | + } |
| 76 | + |
| 77 | + toString(...args) { |
| 78 | + return `${this.name}(${args.join(', ')})` |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +class BinaryFunction extends VectorizedFunction { |
| 83 | + toString(...args) { |
| 84 | + return `(${args.join(` ${this.name} `)})` |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class Program { |
| 89 | + constructor(root) { |
| 90 | + this._p = root |
| 91 | + } |
| 92 | + |
| 93 | + static create(funcs, variables, depth = 2) { |
| 94 | + const root = new Node(funcs[Math.floor(Math.random() * funcs.length)]) |
| 95 | + let stack = [root] |
| 96 | + for (let i = 0; i < depth; i++) { |
| 97 | + const newStack = [] |
| 98 | + for (const node of stack) { |
| 99 | + for (let j = 0; j < node.length; j++) { |
| 100 | + const child = new Node(funcs[Math.floor(Math.random() * funcs.length)]) |
| 101 | + node.addChild(child) |
| 102 | + newStack.push(child) |
| 103 | + } |
| 104 | + } |
| 105 | + stack = newStack |
| 106 | + } |
| 107 | + for (const node of stack) { |
| 108 | + for (let j = 0; j < node.length; j++) { |
| 109 | + if (Math.random() < 0.5) { |
| 110 | + node.addChild(new Node(variables[Math.floor(Math.random() * variables.length)])) |
| 111 | + } else { |
| 112 | + const x = Math.random() |
| 113 | + const y = Math.random() |
| 114 | + const X = Math.sqrt(-2 * Math.log(x)) * Math.cos(2 * Math.PI * y) |
| 115 | + node.addChild(new Node(X)) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + const p = new Program(root) |
| 120 | + p.normalize() |
| 121 | + return p |
| 122 | + } |
| 123 | + |
| 124 | + *nodes() { |
| 125 | + const stack = [this._p] |
| 126 | + while (stack.length > 0) { |
| 127 | + const node = stack.shift() |
| 128 | + stack.push(...node.children) |
| 129 | + yield node |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + normalize() { |
| 134 | + const nodes = [...this.nodes()] |
| 135 | + for (let i = nodes.length - 1; i >= 0; i--) { |
| 136 | + const node = nodes[i] |
| 137 | + if (node.children.some(c => typeof c.value !== 'number')) { |
| 138 | + continue |
| 139 | + } |
| 140 | + node.value = node.evaluate({}) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + mix(other) { |
| 145 | + const cp = new Program(this._p.copy()) |
| 146 | + const thisNodes = [...cp.nodes()] |
| 147 | + const otherNodes = [...other.nodes()] |
| 148 | + const thisIdx = Math.floor(Math.random() * thisNodes.length) |
| 149 | + const otherIdx = Math.floor(Math.random() * otherNodes.length) |
| 150 | + thisNodes[thisIdx].value = otherNodes[otherIdx].value |
| 151 | + thisNodes[thisIdx].children = otherNodes[otherIdx].children.map(c => c.copy()) |
| 152 | + cp.normalize() |
| 153 | + return cp |
| 154 | + } |
| 155 | + |
| 156 | + evaluate(env) { |
| 157 | + return this._p.evaluate(env) |
| 158 | + } |
| 159 | + |
| 160 | + toString() { |
| 161 | + return this._p.toString() |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +const functions = { |
| 166 | + '+': new BinaryFunction((a, b) => a + b, '+'), |
| 167 | + '-': new BinaryFunction((a, b) => a - b, '-'), |
| 168 | + '*': new BinaryFunction((a, b) => a * b, '*'), |
| 169 | + '/': new BinaryFunction((a, b) => a / b, '/'), |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Genetic Programming |
| 174 | + */ |
| 175 | +export default class GeneticProgramming { |
| 176 | + // Genetic Programming as a Means for Programming Computers by Natural Selection |
| 177 | + // https://www.genetic-programming.com/jkpdf/scjournallong.pdf |
| 178 | + // https://qiita.com/shinjikato/items/f482637d1976a0ca6b7c |
| 179 | + /** |
| 180 | + * @param {('+' | '-' | '*' | '/' | function (number, number): number)[]} [funcs] Functions to use |
| 181 | + * @param {number} [size] Number of populations per generation |
| 182 | + */ |
| 183 | + constructor(funcs = ['+', '-', '*', '/'], size = 100) { |
| 184 | + this._progs = [] |
| 185 | + this._funcs = funcs.map(func => { |
| 186 | + if (typeof func === 'string') { |
| 187 | + return functions[func] |
| 188 | + } |
| 189 | + return new VectorizedFunction(func) |
| 190 | + }) |
| 191 | + this._variables = [] |
| 192 | + this._size = size |
| 193 | + this._loss = (y, y_pred) => { |
| 194 | + if (Array.isArray(y_pred)) { |
| 195 | + return y.reduce((s, v, i) => s + (v - y_pred[i]) ** 2, 1) / y.length |
| 196 | + } |
| 197 | + return y.reduce((s, v) => s + (v - y_pred) ** 2, 1) / y.length |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * @returns {Program[]} Best programs for each outputs |
| 203 | + */ |
| 204 | + get bestPrograms() { |
| 205 | + return this._progs.map(p => p[0].p) |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Initialize model. |
| 210 | + * @param {Array<Array<number>>} x Training data |
| 211 | + * @param {Array<Array<number>>} y Target values |
| 212 | + */ |
| 213 | + init(x, y) { |
| 214 | + this._x = x |
| 215 | + this._y = y |
| 216 | + this._variables = Array.from(this._x[0], (_, i) => `x[${i}]`) |
| 217 | + this._outDim = y[0].length |
| 218 | + |
| 219 | + this._inputs = {} |
| 220 | + for (let i = 0; i < this._variables.length; i++) { |
| 221 | + this._inputs[this._variables[i]] = this._x.map(xi => xi[i]) |
| 222 | + } |
| 223 | + this._outputs = [] |
| 224 | + for (let i = 0; i < this._outDim; i++) { |
| 225 | + this._outputs[i] = this._y.map(v => v[i]) |
| 226 | + } |
| 227 | + for (let d = 0; d < this._outDim; d++) { |
| 228 | + this._progs[d] = [] |
| 229 | + for (let i = 0; i < this._size * 2; i++) { |
| 230 | + const p = Program.create(this._funcs, this._variables) |
| 231 | + this._progs[d].push({ |
| 232 | + p, |
| 233 | + loss: this._loss(this._outputs[d], p.evaluate(this._inputs)), |
| 234 | + }) |
| 235 | + } |
| 236 | + this._progs[d].sort((a, b) => a.loss - b.loss) |
| 237 | + this._progs[d] = this._progs[d].slice(0, this._size) |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Fit model. |
| 243 | + */ |
| 244 | + fit() { |
| 245 | + for (let d = 0; d < this._outDim; d++) { |
| 246 | + const newProgs = [...this._progs[d]] |
| 247 | + for (let i = 0; i < this._size; i++) { |
| 248 | + const sump = this._progs[d].reduce((s, p) => s + 1 / p.loss, 0) |
| 249 | + let r = Math.random() * sump |
| 250 | + for (let j = 0; j < this._size; j++) { |
| 251 | + r -= 1 / this._progs[d][i].loss |
| 252 | + if (r <= 0) { |
| 253 | + const p = this._progs[d][i].p.mix(this._progs[d][j].p) |
| 254 | + newProgs.push({ |
| 255 | + p, |
| 256 | + loss: this._loss(this._outputs[d], p.evaluate(this._inputs)), |
| 257 | + }) |
| 258 | + break |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + newProgs.sort((a, b) => a.loss - b.loss) |
| 263 | + this._progs[d] = newProgs.slice(0, this._size) |
| 264 | + } |
| 265 | + return this._progs.reduce((s, v) => s + v[0].loss, 0) / this._outDim |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * Returns predicted values. |
| 270 | + * @param {Array<Array<number>>} x Sample data |
| 271 | + * @returns {Array<Array<number>>} Predicted values |
| 272 | + */ |
| 273 | + predict(x) { |
| 274 | + const inputs = {} |
| 275 | + for (let i = 0; i < x[0].length; i++) { |
| 276 | + inputs[`x[${i}]`] = x.map(xi => xi[i]) |
| 277 | + } |
| 278 | + const result = Array.from({ length: x.length }, () => []) |
| 279 | + for (let d = 0; d < this._outDim; d++) { |
| 280 | + const od = this._progs[d][0].p.evaluate(inputs) |
| 281 | + for (let i = 0; i < x.length; i++) { |
| 282 | + result[i][d] = Array.isArray(od) ? od[i] : od |
| 283 | + } |
| 284 | + } |
| 285 | + return result |
| 286 | + } |
| 287 | +} |
0 commit comments