-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_cli.ts
More file actions
103 lines (84 loc) · 3.38 KB
/
main_cli.ts
File metadata and controls
103 lines (84 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { DEFAULT_CONFIG, Population, EAConfig } from './src/utils/common';
import { initGA, stepGA } from './src/algorithms/ga';
import { initDE, stepDE } from './src/algorithms/de';
import { initES, stepES } from './src/algorithms/es';
import { initPSO, stepPSO } from './src/algorithms/pso';
import { initGP, stepGP } from './src/algorithms/gp';
import { StepLog } from './src/utils/internal-algo-logs';
type AlgoInit = (config: EAConfig) => Population;
type AlgoStep = (pop: Population, config: EAConfig) => { nextPop: Population; logs: StepLog };
interface AlgoRunner {
name: string;
init: AlgoInit;
step: AlgoStep;
}
const formatValue = (val: any): string => {
if (Array.isArray(val)) {
return `[${val.map(v => typeof v === 'number' ? Number(v).toFixed(2) : v).join(', ')}]`;
}
if (typeof val === 'number') {
return Number(val).toFixed(4);
}
if (typeof val === 'object' && val !== null) {
return JSON.stringify(val);
}
return String(val);
};
const printTable = (data: any[]) => {
if (data.length === 0) return;
// format data for console.table
const formattedData = data.map(row => {
const newRow: any = {};
for (const key in row) {
newRow[key] = formatValue(row[key]);
}
return newRow;
});
console.table(formattedData);
};
const runAlgo = (runner: AlgoRunner, config: EAConfig) => {
console.log(`\n========================================`);
console.log(`Running Algorithm: ${runner.name}`);
console.log(`========================================\n`);
let population = runner.init(config);
console.log(`Initial Population (Size: ${population.length}):`);
const initialSummary = population.map(p => ({
id: p.id,
fitness: p.fitness,
genes: p.genes
}));
printTable(initialSummary);
for (let gen = 1; gen <= config.maxGenerations; gen++) {
console.log(`\n--- Generation ${gen} ---`);
const result = runner.step(population, config);
population = result.nextPop;
const logs = result.logs;
console.log(`Logs for Generation ${gen}:`);
printTable(logs);
// Calculate and print stats
const best = population.reduce((prev, current) => (prev.fitness > current.fitness) ? prev : current);
console.log(`Gen ${gen} Best Fitness: ${best.fitness.toFixed(4)}`);
}
console.log(`\n${runner.name} Finished.`);
const finalBest = population.reduce((prev, current) => (prev.fitness > current.fitness) ? prev : current);
console.log(`Final Best Individual: ID ${finalBest.id}, Fitness ${finalBest.fitness.toFixed(4)}`);
console.log(`Genes: ${formatValue(finalBest.genes)}`);
};
const main = () => {
const algorithms: AlgoRunner[] = [
{ name: 'Genetic Algorithm (GA)', init: initGA, step: stepGA },
{ name: 'Differential Evolution (DE)', init: initDE, step: stepDE },
{ name: 'Evolution Strategy (ES)', init: initES, step: stepES },
{ name: 'Particle Swarm Optimization (PSO)', init: initPSO, step: stepPSO },
{ name: 'Genetic Programming (GP)', init: initGP, step: stepGP },
];
const config = { ...DEFAULT_CONFIG };
algorithms.forEach(algo => {
try {
runAlgo(algo, config);
} catch (error) {
console.error(`Error running ${algo.name}:`, error);
}
});
};
main();