Normalize scripts style
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent 16f15ed · Browse files · 3 files·+117 added−115 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+117 added−115 removed
- tests:+117 added−115 removed
Provenance
- Commit
a8b0d83- Tree
09cd285- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
16f15ed
3 files changed+117 added−115 removed
modified:test/alphabet+3 added-3 removed
1-#!/usr/bin/node --experimental-worker1+#!/usr/bin/node2 3 const WORKERS = 84 5-let spawnSync = require('child_process').spawnSync6-let Worker = require('worker_threads').Worker5+let { spawnSync } = require('child_process')6+let { Worker } = require('worker_threads')7 let chalk = require('chalk')8 let path = require('path')9 let fs = require('fs')10 73 unmodified linesadded:test/alphabet-genetic+114 added
1+#!/usr/bin/node2+3+let geneticAlgorithmConstructor = require('geneticalgorithm')4+let { spawnSync } = require('child_process')5+let chalk = require('chalk')6+let path = require('path')7+let fs = require('fs')8+let gzipSize = require('gzip-size')9+10+if (!process.argv[2]) {11+ process.stderr.write(chalk.red('Pass a file with alphabet as argument\n'))12+ process.exit(1)13+}14+15+let input = path.join(process.cwd(), process.argv[2])16+let output = path.join(__dirname, '..', 'compiled.js')17+18+spawnSync('npx', ['webpack', '--mode', 'production', '--output', output, input])19+let js = fs.readFileSync(output).toString()20+fs.unlinkSync(output)21+22+let match = js.match(/[A-Za-z0-9-_]{30,}/)23+if (!match) {24+ process.stderr.write(chalk.red('Alphabet was not found\n'))25+ process.exit(1)26+}27+let alphabet = match[0]28+29+function mutationFunction (phenotype) {30+ let i = Math.floor(Math.random() * phenotype.alphabet.length)31+ let j = Math.floor(Math.random() * phenotype.alphabet.length)32+ return { alphabet: swapChars(phenotype.alphabet, i, j) }33+}34+35+function swapChars (str, index1, index2) {36+ let l = index1 < index2 ? index1 : index237+ let h = index1 > index2 ? index1 : index238+ return str.substring(0, l) +39+ str[h] +40+ str.substring(l + 1, h) +41+ str[l] +42+ str.substring(h + 1, str.length)43+}44+45+function crossoverFunction (phenotypeA, phenotypeB) {46+ let a = phenotypeA.alphabet47+ let b = phenotypeB.alphabet48+ let indexA = Math.floor(Math.random() * a.length / 2 + a.length / 2)49+ let indexB = Math.floor(Math.random() + a.length / 2)50+ let newStrA = a.substring(indexA, a.length)51+ let newStrB = b.substring(0, indexB)52+ return [53+ { alphabet: addMissingCharacter(newStrA, b) },54+ { alphabet: addMissingCharacter(newStrB, a) }55+ ]56+}57+58+function addMissingCharacter (str, proto) {59+ let newStr = str60+ for (let i = 0; i < proto.length; i++) {61+ if (str.indexOf(proto[i]) === -1) {62+ newStr += proto[i]63+ }64+ }65+ return newStr66+}67+68+function fitnessFunction (phenotype) {69+ let file = js.replace(/[A-Za-z0-9-_]{30,}/, phenotype.alphabet)70+ let size = gzipSize.sync(file)71+ console.log(size, phenotype.alphabet)72+ return -1 * size73+}74+75+function sameAlphabet (a, b) {76+ let ua = a.split('').sort().join('')77+ let ub = b.split('').sort().join('')78+ for (let i = 0; i < ua.length; i++) {79+ if (ua[i] !== ub[i]) {80+ return false81+ }82+ }83+ return true84+}85+86+const firstPhenotype = { alphabet }87+88+const geneticAlgorithm = geneticAlgorithmConstructor({89+ mutationFunction,90+ crossoverFunction,91+ fitnessFunction,92+ population: [firstPhenotype],93+ populationSize: 500094+})95+96+console.log('Starting with:', firstPhenotype.alphabet)97+for (let i = 0; i < 100; i++) {98+ geneticAlgorithm.evolve()99+}100+const best = geneticAlgorithm.best()101+console.log('Finished with:', best.alphabet)102+const isSame = sameAlphabet(firstPhenotype.alphabet, best.alphabet)103+console.log(104+ 'Same set of characters:',105+ isSame ? chalk.green('true') : chalk.red('false')106+)107+console.log(chalk.green(108+ 'Size new string:',109+ gzipSize.sync(js.replace(/[A-Za-z0-9-_]{30,}/, best.alphabet))110+))111+console.log(chalk.yellow(112+ 'Size old string:',113+ gzipSize.sync(js.replace(/[A-Za-z0-9-_]{30,}/, firstPhenotype.alphabet)))114+)deleted:test/alphabet-genetic.js-112 removed
1-let spawnSync = require('child_process').spawnSync2-let chalk = require('chalk')3-let path = require('path')4-let fs = require('fs')5-let geneticAlgorithmConstructor = require('geneticalgorithm')6-let gzipSize = require('gzip-size')7-8-if (!process.argv[2]) {9- process.stderr.write(chalk.red('Pass a file with alphabet as argument\n'))10- process.exit(1)11-}12-13-let input = path.join(process.cwd(), process.argv[2])14-let output = path.join(__dirname, '..', 'compiled.js')15-16-spawnSync('npx', ['webpack', '--mode', 'production', '--output', output, input])17-let js = fs.readFileSync(output).toString()18-fs.unlinkSync(output)19-20-let match = js.match(/[A-Za-z0-9-_]{30,}/)21-if (!match) {22- process.stderr.write(chalk.red('Alphabet was not found\n'))23- process.exit(1)24-}25-let alphabet = match[0]26-27-function mutationFunction (phenotype) {28- let i = Math.floor(Math.random() * phenotype.alphabet.length)29- let j = Math.floor(Math.random() * phenotype.alphabet.length)30-31- return {32- alphabet: swapChars(phenotype.alphabet, i, j)33- }34-}35-36-function swapChars (str, index1, index2) {37- let l = index1 < index2 ? index1 : index238- let h = index1 > index2 ? index1 : index239- return str.substring(0, l) +40- str[h] +41- str.substring(l + 1, h) +42- str[l] +43- str.substring(h + 1, str.length)44-}45-46-function crossoverFunction (phenotypeA, phenotypeB) {47- let alphabetA = phenotypeA.alphabet48- let alphabetB = phenotypeB.alphabet49- let indexA =50- Math.floor(Math.random() * alphabetA.length / 2 + alphabetA.length / 2)51- let indexB =52- Math.floor(Math.random() + alphabetA.length / 2)53- let newStrA = alphabetA.substring(indexA, alphabetA.length)54- let newStrB = alphabetB.substring(0, indexB)55-56- return [57- { alphabet: addMissingCharacter(newStrA, alphabetB) },58- { alphabet: addMissingCharacter(newStrB, alphabetA) }59- ]60-}61-62-function addMissingCharacter (str, proto) {63- let newStr = str64- for (let i = 0; i < proto.length; i++) {65- if (str.indexOf(proto[i]) === -1) {66- newStr += proto[i]67- }68- }69- return newStr70-}71-72-function fitnessFunction (phenotype) {73- let file = js.replace(/[A-Za-z0-9-_]{30,}/, phenotype.alphabet)74- let size = gzipSize.sync(file)75- console.log(size, phenotype.alphabet)76- return -1 * size77-}78-79-function sameAlphabet (a, b) {80- let ua = a.split('').sort().join('')81- let ub = b.split('').sort().join('')82- for (let i = 0; i < ua.length; i++) {83- if (ua[i] !== ub[i]) {84- return false85- }86- }87- return true88-}89-90-const firstPhenotype = { alphabet }91-92-const geneticAlgorithm = geneticAlgorithmConstructor({93- mutationFunction,94- crossoverFunction,95- fitnessFunction,96- population: [firstPhenotype],97- populationSize: 500098-})99-100-console.log('Starting with:', firstPhenotype.alphabet)101-for (let i = 0; i < 100; i++) {102- geneticAlgorithm.evolve()103-}104-const best = geneticAlgorithm.best()105-console.log('Finished with:', best.alphabet)106-const isSame = sameAlphabet(firstPhenotype.alphabet, best.alphabet)107-console.log('Same set of characters:',108- isSame ? chalk.green('true') : chalk.red('false'))109-console.log(chalk.green('Size new string:',110- gzipSize.sync(js.replace(/[A-Za-z0-9-_]{30,}/, best.alphabet))))111-console.log(chalk.yellow('Size old string:',112- gzipSize.sync(js.replace(/[A-Za-z0-9-_]{30,}/, firstPhenotype.alphabet))))