Skip to content

Commit

Permalink
chore!: update dependencies and do not export default
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Oct 22, 2024
1 parent df5f767 commit a9f84dd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 47 deletions.
6 changes: 6 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cheminfo from 'eslint-config-cheminfo';
import globals from 'globals';

export default [
...cheminfo,
]
2 changes: 1 addition & 1 deletion ml-direct.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare module 'ml-direct' {
initialState?: any;
}

export default function direct(
export function direct(
objectiveFunction: (parameters: number[]) => number,
lowerBoundaries: number[],
upperBoundaries: number[],
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"prettier": "prettier --check src",
"prettier-write": "prettier --write src",
"test": "npm run test-only && npm run eslint && npm run prettier",
"test-only": "jest --coverage"
"test-only": "vitest run --coverage"
},
"repository": {
"type": "git",
Expand All @@ -32,18 +32,19 @@
},
"homepage": "https://github.com/mljs/direct#readme",
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.23.3",
"@babel/plugin-transform-modules-commonjs": "^7.25.7",
"@vitest/coverage-v8": "^2.1.3",
"cheminfo-build": "^1.2.0",
"eslint": "^8.57.0",
"eslint-config-cheminfo": "^9.2.0",
"eslint": "^9.13.0",
"eslint-config-cheminfo": "^12.0.1",
"esm": "^3.2.25",
"jest": "^29.7.0",
"jest-matcher-deep-close-to": "^3.0.2",
"prettier": "^3.2.5",
"rollup": "^4.12.1"
"prettier": "^3.3.3",
"rollup": "^4.24.0",
"vitest": "^2.1.3"
},
"dependencies": {
"ml-matrix": "^6.11.0",
"ml-spectra-processing": "^14.2.0"
"ml-matrix": "^6.12.0",
"ml-spectra-processing": "^14.6.0"
}
}
}
5 changes: 3 additions & 2 deletions src/__tests__/initialState.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import direct from '../index';
// Test functions from https://www.sfu.ca/~ssurjano/optimization.html
import { describe, expect, it } from 'vitest';

import { direct } from '../index';
// Test functions from https://www.sfu.ca/~ssurjano/optimization.html
describe('Initial state option', () => {
it('The result after two runs of 50 iterations should be equal to one of 100 iterations', () => {
const lowerBoundaries = [-5, -2];
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/optimization.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import direct from '../index';
// Test functions from https://www.sfu.ca/~ssurjano/optimization.html
import { describe, expect, it } from 'vitest';

import { direct } from '../index';
// Test functions from https://www.sfu.ca/~ssurjano/optimization.html
describe('test Direct method', () => {
it('Evaluating griewank test function in 3D', () => {
const options = {
Expand Down
48 changes: 20 additions & 28 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { xNorm, xMaxValue, xMinValue } from 'ml-spectra-processing';

import antiLowerConvexHull from './util/antiLowerConvexHull';
import { antiLowerConvexHull } from './util/antiLowerConvexHull';

/**
* Performs a global optimization of required parameters
Expand All @@ -9,19 +9,19 @@ import antiLowerConvexHull from './util/antiLowerConvexHull';
* - `optima`: Array of Array of values for all the variables where the function reach its minimum value
* - `iterations`: Number of iterations performed in the process
* - `finalState`: Internal state allowing to continue optimization (initialState)
* @param {function} objectiveFunction Function to evaluate. It should accept an array of variables
* @param {Array} lowerBoundaries Array containing for each variable the lower boundary
* @param {Array} upperBoundaries Array containing for each variable the higher boundary
* @param {Object} [options={}]
* @param {Function} objectiveFunction - Function to evaluate. It should accept an array of variables
* @param {Array} lowerBoundaries - Array containing for each variable the lower boundary
* @param {Array} upperBoundaries - Array containing for each variable the higher boundary
* @param {object} [options={}]

Check warning on line 15 in src/index.js

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @param "options" description
* @param {number} [options.iterations] - Number of iterations.
* @param {number} [options.epsilon] - Tolerance to choose best current value.
* @param {number} [options.tolerance] - Minimum tollerance of the function.
* @param {number} [options.tolerance2] - Minimum tollerance of the function.
* @param {Object} [options.initialState={}}] - finalState of previous optimization.
* @return {Object} {finalState, iterations, minFunctionValue}
* */
* @param {object} [options.initialState={}}] - finalState of previous optimization.
* @returns {object} {finalState, iterations, minFunctionValue}
*/

export default function direct(
export function direct(
objectiveFunction,
lowerBoundaries,
upperBoundaries,
Expand Down Expand Up @@ -83,10 +83,7 @@ export default function direct(
initialState.originalCoordinates.length > 0
) {
bestCurrentValue = xMinValue(functionValues);
choiceLimit =
epsilon * Math.abs(bestCurrentValue) > 1e-8
? epsilon * Math.abs(bestCurrentValue)
: 1e-8;
choiceLimit = Math.max(epsilon * Math.abs(bestCurrentValue), 1e-8);

smallerDistance = getMinIndex(
functionValues,
Expand Down Expand Up @@ -115,10 +112,7 @@ export default function direct(
//----------------------------------------------------------------------

let S1 = [];
let idx = differentDistances.findIndex(
// eslint-disable-next-line no-loop-func
(e) => e === diagonalDistances[smallerDistance],
);
let idx = differentDistances.indexOf(diagonalDistances[smallerDistance]);
let counter = 0;
for (let i = idx; i < differentDistances.length; i++) {
for (let f = 0; f < functionValues.length; f++) {
Expand All @@ -135,7 +129,7 @@ export default function direct(
if (differentDistances.length - idx > 1) {
let a1 = diagonalDistances[smallerDistance];
let b1 = functionValues[smallerDistance];
let a2 = differentDistances[differentDistances.length - 1];
let a2 = differentDistances.at(-1);
let b2 = smallerValuesByDistance[differentDistances.length - 1];
let slope = (b2 - b1) / (a2 - a1);
let constant = b1 - slope * a1;
Expand Down Expand Up @@ -230,10 +224,7 @@ export default function direct(

bestCurrentValue = xMinValue(functionValues);

choiceLimit =
epsilon * Math.abs(bestCurrentValue) > 1e-8
? epsilon * Math.abs(bestCurrentValue)
: 1e-8;
choiceLimit = Math.max(epsilon * Math.abs(bestCurrentValue), 1e-8);

smallerDistance = getMinIndex(
functionValues,
Expand All @@ -251,11 +242,12 @@ export default function direct(
let minIndex;
let minValue = Number.POSITIVE_INFINITY;
for (let k = 0; k < diagonalDistances.length; k++) {
if (diagonalDistances[k] === differentDistances[i]) {
if (functionValues[k] < minValue) {
minValue = functionValues[k];
minIndex = k;
}
if (
diagonalDistances[k] === differentDistances[i] &&
functionValues[k] < minValue
) {
minValue = functionValues[k];
minIndex = k;
}
}
smallerValuesByDistance.push(functionValues[minIndex]);
Expand Down Expand Up @@ -330,6 +322,6 @@ function getMinIndex(
diagonalDistances[i];
}
const min = xMinValue(item);
let result = item.findIndex((x) => x === min);
let result = item.indexOf(min);
return result;
}
4 changes: 3 additions & 1 deletion src/util/__tests__/antiLowerConvexHull.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import antiLowerConvexHull from '../antiLowerConvexHull';
import { describe, expect, it } from 'vitest';

import { antiLowerConvexHull } from '../antiLowerConvexHull';

describe('testing lower convexhull function', () => {
it('Get anti clockwise lower convex hull', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/util/antiLowerConvexHull.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Preparata, F. P., & Shamos, M. I. (2012). Computational geometry: an introduction. Springer Science & Business Media.
* @param {Array} x - The array with x coordinates of the points.
* @param {Array} y - The array with y coordinates of the points.
* @return {Array} The indices of the points of anticlockwise lower convex hull
* @returns {Array} The indices of the points of anticlockwise lower convex hull
* @private
*/
export default function antiLowerConvexHull(x, y) {
export function antiLowerConvexHull(x, y) {
if (x.length !== y.length) {
throw new RangeError('X and Y vectors has different dimensions');
}
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function antiLowerConvexHull(x, y) {
* @param {number} currentPoint - The index of the current point to make the move
* @param {number} nbPoints - The total number of points in the array
* @param {Array} vector - The array with the points
* @return {number} the index of the point after the move
* @returns {number} the index of the point after the move
* @private
*/

Expand Down

0 comments on commit a9f84dd

Please sign in to comment.