Skip to content

Commit

Permalink
Test sim upd.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed May 16, 2024
1 parent 8fc010b commit b28f847
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build-only": "vite build",
"type-check": "vue-tsc --noEmit",
"test": "jest",
"tool": "tsx --no-warnings src/scripts/tool.ts"
"tool": "tsx --no-warnings src/scripts/tool.ts",
"genetic": "npm run tool test-simulation-parallel"
},
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.5.1",
Expand Down
15 changes: 15 additions & 0 deletions src/lib/math/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,18 @@ export function weighMatrix(
): number[][] {
return input.map((item) => weighArray((rowModifier ?? ((row) => row))(item), weight));
}

export function averageMatrixColumns(input: number[][]): number[] {
if (input.length === 0) {
return [];
}
const result = [];
for (let i = 0; i < input[0].length; ++i) {
let sum = 0;
for (let j = 0; j < input.length; ++j) {
sum += input[j][i];
}
result[i] = sum / input.length;
}
return result;
}
41 changes: 23 additions & 18 deletions src/scripts/actions/test-simulation-parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import type { TotalSummary } from "@/lib/types/analysis";
import {
convertWeightsToSummaryMatrixRow,
createTransparentWeights,
convertSummaryToSummaryMatrixRow,
getSummaryMatrixGroupIndexes, normalizeSummaryMatrix
getSummaryMatrixGroupIndexes,
normalizeSummaryMatrix,
} from "@/lib/analysis/helpers";
import { normalizeMatrixColumns } from "@/lib/math";

export const simulationTask = async (
[id, worldConfig, typesConfig, steps]: [number, WorldConfig, TypesConfig, number],
[id, worldConfig, typesConfig, steps]: [number, WorldConfig, TypesConfig, number[]],
) => {
console.log(`-> task ${id} started`);
const ts = Date.now();
Expand All @@ -27,6 +26,8 @@ export const simulationTask = async (
const { Simulation } = await import(`${dirName}/lib/simulation`);
const { Runner } = await import(`${dirName}/lib/runner`);
const { CompoundsAnalyzer } = await import(`${dirName}/lib/analysis/compounds`);
const { convertSummaryToSummaryMatrixRow } = await import(`${dirName}/lib/analysis/helpers`);
const { averageMatrixColumns } = await import(`${dirName}/lib/math/operations`);

const sim = new Simulation({
viewMode: '2d',
Expand All @@ -38,18 +39,23 @@ export const simulationTask = async (
});

const runner = new Runner(sim);
runner.runSteps(steps);

const compounds = new CompoundsAnalyzer(sim.exportCompounds(), sim.atoms, typesConfig.FREQUENCIES.length);

const totalSummary: TotalSummary = {
WORLD: sim.summary,
COMPOUNDS: compounds.summary,
};
const summaryMatrix: number[][] = [];

for (const stepsCount of steps) {
runner.runSteps(stepsCount);

const compounds = new CompoundsAnalyzer(sim.exportCompounds(), sim.atoms, typesConfig.FREQUENCIES.length);
const totalSummary: TotalSummary = {
WORLD: sim.summary,
COMPOUNDS: compounds.summary,
};
const rawMatrix = convertSummaryToSummaryMatrixRow(totalSummary);
summaryMatrix.push(rawMatrix);
}

console.log(`<- task ${id} finished in ${Date.now() - ts} ms`);

return totalSummary;
return averageMatrixColumns(summaryMatrix);
}

export const actionTestSimulationParallel = async (...args: string[]) => {
Expand All @@ -60,7 +66,7 @@ export const actionTestSimulationParallel = async (...args: string[]) => {
const typesConfig = createBaseTypesConfig();
const typesCount = typesConfig.FREQUENCIES.length;

const stepsCount = 300;
const stepsCount = [300, 5, 5, 5, 5];
const atomsCount = 500;
const minPosition = [0, 0];
const maxPosition = [1000, 1000];
Expand All @@ -85,17 +91,16 @@ export const actionTestSimulationParallel = async (...args: string[]) => {
console.log('CPUs:', cpuCount);

const pool = new Pool(cpuCount);
const summaries: TotalSummary[] = await pool.map(inputs, simulationTask);
const summaries: number[][] = await pool.map(inputs, simulationTask);
pool.close();

const rawMatrix = summaries.map((summary) => convertSummaryToSummaryMatrixRow(summary));
const normalizedMatrix = normalizeSummaryMatrix(rawMatrix, typesCount);
const normalizedMatrix = normalizeSummaryMatrix(summaries, typesCount);
const indexes = getSummaryMatrixGroupIndexes(typesCount);
const weights = convertWeightsToSummaryMatrixRow(createTransparentWeights(), typesCount);

console.log(normalizedMatrix);

console.log(rawMatrix[0].length);
console.log(summaries[0].length);
console.log(weights.length);
console.log(indexes.flat(1).length);

Expand Down

0 comments on commit b28f847

Please sign in to comment.