Skip to content

Commit

Permalink
Fixes, normalizing summary matrix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed May 15, 2024
1 parent a56520b commit 8fc010b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
18 changes: 15 additions & 3 deletions src/lib/analysis/helpers.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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),
Expand All @@ -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);
Expand All @@ -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;
}
8 changes: 6 additions & 2 deletions src/lib/math/normalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/scripts/actions/test-simulation-parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8fc010b

Please sign in to comment.