From 8fc010b7ee86e303cd0436749c4538aaaf925f12 Mon Sep 17 00:00:00 2001 From: Smoren Date: Wed, 15 May 2024 23:13:12 +0300 Subject: [PATCH] Fixes, normalizing summary matrix. --- src/lib/analysis/helpers.ts | 18 +++++++++++++++--- src/lib/math/normalization.ts | 8 ++++++-- .../actions/test-simulation-parallel.ts | 17 +++++++++-------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/lib/analysis/helpers.ts b/src/lib/analysis/helpers.ts index 8011229..8edfa6f 100644 --- a/src/lib/analysis/helpers.ts +++ b/src/lib/analysis/helpers.ts @@ -1,5 +1,6 @@ import type { TotalSummary, TotalSummaryWeights } from '../types/analysis'; -import { createFilledArray, repeatArrayValues } from '../math'; +import { createFilledArray, normalizeMatrixColumnsUnion, repeatArrayValues } from '../math'; +import { fullCopyObject } from "@/lib/utils/functions"; export function createTransparentWeights(): TotalSummaryWeights { return { @@ -53,7 +54,7 @@ export function getSummaryMatrixGroupIndexes(typesCount: number): number[][] { return groupIndexes; } -export function convertWeightsToMatrixRow(weights: TotalSummaryWeights, typesCount: number): number[] { +export function convertWeightsToSummaryMatrixRow(weights: TotalSummaryWeights, typesCount: number): number[] { return [ weights.ATOMS_MEAN_SPEED, createFilledArray(typesCount, weights.ATOMS_TYPE_MEAN_SPEED), @@ -69,7 +70,7 @@ export function convertWeightsToMatrixRow(weights: TotalSummaryWeights, typesCou ].flat(Infinity) as number[]; } -export function convertSummaryToMatrix(summary: TotalSummary): number[] { +export function convertSummaryToSummaryMatrixRow(summary: TotalSummary): number[] { const compoundsPerAtom = summary.COMPOUNDS.size / summary.WORLD.ATOMS_COUNT[0]; const compoundsPerAtomByTypes = summary.COMPOUNDS.sizeByTypes.map((x) => x / summary.WORLD.ATOMS_COUNT[0]); const compoundLengthSummary = Object.values(summary.COMPOUNDS.itemLengthSummary).slice(1); @@ -91,3 +92,14 @@ export function convertSummaryToMatrix(summary: TotalSummary): number[] { compoundLengthByTypesSummary, ].flat(Infinity) as number[]; } + +export function normalizeSummaryMatrix(matrix: number[][], typesCount: number): number[][] { + const result = fullCopyObject(matrix); + const groupIndexes = getSummaryMatrixGroupIndexes(typesCount); + + for (const group of groupIndexes) { + normalizeMatrixColumnsUnion(result, group, true); + } + + return result; +} diff --git a/src/lib/math/normalization.ts b/src/lib/math/normalization.ts index 04bee2e..8388617 100644 --- a/src/lib/math/normalization.ts +++ b/src/lib/math/normalization.ts @@ -12,7 +12,7 @@ export function normalizeArray(input: number[], inplace: boolean = false): numbe const max = Math.max(...result); if (isEqual(min, max)) { - return isEqual(min, 0) ? result.map(() => 0) : result.map(() => 1); + return result.map(() => 0.5); } return result.map((x) => (x - min) / (max - min)); @@ -60,7 +60,11 @@ export function normalizeMatrixColumnsUnion(matrix: number[][], columns: number[ for (const column of columns) { for (let i = 0; i < result.length; i++) { - result[i][column] = (result[i][column] - min) / (max - min); + if (isEqual(min, max)) { + result[i][column] = 0.5; + } else { + result[i][column] = (result[i][column] - min) / (max - min); + } } } diff --git a/src/scripts/actions/test-simulation-parallel.ts b/src/scripts/actions/test-simulation-parallel.ts index b710923..3414359 100644 --- a/src/scripts/actions/test-simulation-parallel.ts +++ b/src/scripts/actions/test-simulation-parallel.ts @@ -5,10 +5,10 @@ import { createBaseWorldConfig } from '@/lib/config/world'; import { createBaseTypesConfig } from '@/lib/config/types'; import type { TotalSummary } from "@/lib/types/analysis"; import { - convertWeightsToMatrixRow, + convertWeightsToSummaryMatrixRow, createTransparentWeights, - convertSummaryToMatrix, - getSummaryMatrixGroupIndexes + convertSummaryToSummaryMatrixRow, + getSummaryMatrixGroupIndexes, normalizeSummaryMatrix } from "@/lib/analysis/helpers"; import { normalizeMatrixColumns } from "@/lib/math"; @@ -58,6 +58,7 @@ export const actionTestSimulationParallel = async (...args: string[]) => { const worldConfig = createBaseWorldConfig(); const typesConfig = createBaseTypesConfig(); + const typesCount = typesConfig.FREQUENCIES.length; const stepsCount = 300; const atomsCount = 500; @@ -87,12 +88,12 @@ export const actionTestSimulationParallel = async (...args: string[]) => { const summaries: TotalSummary[] = await pool.map(inputs, simulationTask); pool.close(); - const rawMatrix = summaries.map((summary) => convertSummaryToMatrix(summary)); - const normalizedMatrix = normalizeMatrixColumns(rawMatrix); - const indexes = getSummaryMatrixGroupIndexes(typesConfig.FREQUENCIES.length); - const weights = convertWeightsToMatrixRow(createTransparentWeights(), typesConfig.FREQUENCIES.length); + const rawMatrix = summaries.map((summary) => convertSummaryToSummaryMatrixRow(summary)); + const normalizedMatrix = normalizeSummaryMatrix(rawMatrix, typesCount); + const indexes = getSummaryMatrixGroupIndexes(typesCount); + const weights = convertWeightsToSummaryMatrixRow(createTransparentWeights(), typesCount); - console.log(indexes); + console.log(normalizedMatrix); console.log(rawMatrix[0].length); console.log(weights.length);