Skip to content

Commit 75d1992

Browse files
authored
fix: add prettier write script (#2)
1 parent be920b1 commit 75d1992

File tree

4 files changed

+47
-26
lines changed

4 files changed

+47
-26
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"eslint": "eslint src --ext ts --cache",
1919
"eslint-fix": "npm run eslint -- --fix",
2020
"prepublishOnly": "npm run tsc",
21+
"prettier": "prettier --check src",
22+
"prettier-write": "prettier --write src",
2123
"test": "npm run test-coverage && npm run eslint",
2224
"test-coverage": "npm run test-only -- --coverage",
2325
"test-only": "jest",

src/__tests__/test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ describe('linear sum problem', () => {
1111
const diff = a.map((aElement) => {
1212
return b.map((bElement) => Math.abs(bElement - aElement));
1313
});
14-
const { columnAssignments, rowAssignments } = linearSumAssignment(diff, { maximaze: false});
14+
const { columnAssignments, rowAssignments } = linearSumAssignment(diff, {
15+
maximaze: false,
16+
});
1517
expect(columnAssignments).toMatchCloseTo([2, 0, 1, 3, 4]);
1618
expect(rowAssignments).toMatchCloseTo([1, 2, 0, 3, 4]);
1719
});
@@ -22,7 +24,9 @@ describe('linear sum problem', () => {
2224
const diff = a.map((aElement) => {
2325
return b.map((bElement) => Math.abs(bElement - aElement));
2426
});
25-
const { columnAssignments, rowAssignments } = linearSumAssignment(diff, { maximaze: false});
27+
const { columnAssignments, rowAssignments } = linearSumAssignment(diff, {
28+
maximaze: false,
29+
});
2630
expect(columnAssignments).toMatchCloseTo([2, 0, 1, 3, 4]);
2731
expect(rowAssignments).toMatchCloseTo([1, 2, 0, 3, 4, -1]);
2832
});
@@ -32,7 +36,9 @@ describe('linear sum problem', () => {
3236
const diff = a.map((aElement) => {
3337
return b.map((bElement) => Math.abs(bElement - aElement));
3438
});
35-
const { columnAssignments, rowAssignments } = linearSumAssignment(diff, { maximaze: false});
39+
const { columnAssignments, rowAssignments } = linearSumAssignment(diff, {
40+
maximaze: false,
41+
});
3642
expect(columnAssignments).toMatchCloseTo([1, 2, 0, 3, 4, -1]);
3743
expect(rowAssignments).toMatchCloseTo([2, 0, 1, 3, 4]);
3844
});

src/getShortestPath.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import sequentialFill from 'ml-array-sequential-fill';
33
import type { Matrix } from 'ml-matrix';
44

55
interface GetShortestPathOptions {
6-
currUnAssCol: number,
7-
dualVariableForColumns: DoubleArray,
8-
dualVariableForRows: DoubleArray,
9-
rowAssignments: DoubleArray,
10-
columnAssignments: DoubleArray,
11-
matrix: Matrix,
6+
currUnAssCol: number;
7+
dualVariableForColumns: DoubleArray;
8+
dualVariableForRows: DoubleArray;
9+
rowAssignments: DoubleArray;
10+
columnAssignments: DoubleArray;
11+
matrix: Matrix;
1212
}
1313

1414
export function getShortestPath(options: GetShortestPathOptions) {
@@ -21,7 +21,7 @@ export function getShortestPath(options: GetShortestPathOptions) {
2121
matrix,
2222
} = options;
2323

24-
let nbRows = matrix.rows
24+
let nbRows = matrix.rows;
2525
let nbColumns = matrix.columns;
2626

2727
let pred = new Float64Array(nbColumns);
@@ -43,10 +43,14 @@ export function getShortestPath(options: GetShortestPathOptions) {
4343
for (let curRowScan = 0; curRowScan < numRows2Scan; curRowScan++) {
4444
let curRow = rows2Scan[curRowScan];
4545

46-
let reducedCost = delta + matrix.get(curRow, curColumn) - dualVariableForColumns[curColumn] - dualVariableForRows[curRow];
46+
let reducedCost =
47+
delta +
48+
matrix.get(curRow, curColumn) -
49+
dualVariableForColumns[curColumn] -
50+
dualVariableForRows[curRow];
4751
if (reducedCost < shortestPathCost[curRow]) {
4852
pred[curRow] = curColumn;
49-
shortestPathCost[curRow] = reducedCost
53+
shortestPathCost[curRow] = reducedCost;
5054
}
5155

5256
if (shortestPathCost[curRow] < minVal) {
@@ -74,16 +78,20 @@ export function getShortestPath(options: GetShortestPathOptions) {
7478
for (let sel = 0; sel < nbColumns; sel++) {
7579
if (scannedColumns[sel] === 0) continue;
7680
if (sel === currUnAssCol) continue;
77-
dualVariableForColumns[sel] += delta - shortestPathCost[columnAssignments[sel]];
81+
dualVariableForColumns[sel] +=
82+
delta - shortestPathCost[columnAssignments[sel]];
7883
}
7984
for (let sel = 0; sel < nbRows; sel++) {
8085
if (scannedRows[sel] === 0) continue;
81-
dualVariableForRows[sel] -= (delta - shortestPathCost[sel]);
86+
dualVariableForRows[sel] -= delta - shortestPathCost[sel];
8287
}
8388

8489
return {
85-
sink, pred, dualVariableForColumns, dualVariableForRows
86-
}
90+
sink,
91+
pred,
92+
dualVariableForColumns,
93+
dualVariableForRows,
94+
};
8795
}
8896

8997
function getArrayOfInfinity(nbElements = 1, value = Number.POSITIVE_INFINITY) {
@@ -92,4 +100,4 @@ function getArrayOfInfinity(nbElements = 1, value = Number.POSITIVE_INFINITY) {
92100
array[i] = value;
93101
}
94102
return array;
95-
}
103+
}

src/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import { Matrix } from 'ml-matrix';
44
import { getShortestPath } from './getShortestPath';
55

66
interface Options {
7-
maximaze?: boolean
7+
maximaze?: boolean;
88
}
99

10-
export default function linearSumAssignment(input: DoubleArray[], options: Options = {}) {
10+
export default function linearSumAssignment(
11+
input: DoubleArray[],
12+
options: Options = {},
13+
) {
1114
const { maximaze = true } = options;
1215

1316
let matrix = Matrix.checkMatrix(input);
@@ -25,7 +28,6 @@ export default function linearSumAssignment(input: DoubleArray[], options: Optio
2528
matrix = matrix.subtract(matrixDelta);
2629
if (maximaze) matrix = matrix.mul(-1);
2730

28-
2931
let rowAssignments: DoubleArray = new Float64Array(nbRows).fill(-1);
3032
let columnAssignments: DoubleArray = new Float64Array(nbColumns).fill(-1);
3133
let dualVariableForColumns: DoubleArray = new Float64Array(nbColumns);
@@ -48,7 +50,7 @@ export default function linearSumAssignment(input: DoubleArray[], options: Optio
4850
rowAssignments: [],
4951
columnAssignments: [],
5052
gain: -1,
51-
}
53+
};
5254
}
5355

5456
dualVariableForColumns = currentAugmenting.dualVariableForColumns;
@@ -69,18 +71,21 @@ export default function linearSumAssignment(input: DoubleArray[], options: Optio
6971
gain += matrix.get(columnAssignments[curCol], curCol);
7072
}
7173

72-
gain = ((maximaze ? -1 : 1) * gain) + (matrixDelta * nbColumns);
74+
gain = (maximaze ? -1 : 1) * gain + matrixDelta * nbColumns;
7375

7476
if (didFlip) {
7577
[columnAssignments, rowAssignments] = [rowAssignments, columnAssignments];
76-
[dualVariableForColumns, dualVariableForRows] = [dualVariableForRows, dualVariableForColumns];
78+
[dualVariableForColumns, dualVariableForRows] = [
79+
dualVariableForRows,
80+
dualVariableForColumns,
81+
];
7782
}
7883

7984
return {
8085
rowAssignments,
8186
columnAssignments,
8287
gain,
8388
dualVariableForColumns,
84-
dualVariableForRows
85-
}
86-
}
89+
dualVariableForRows,
90+
};
91+
}

0 commit comments

Comments
 (0)