-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: global optimization by direct algorithm (#103)
- Loading branch information
Showing
6 changed files
with
163 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { DataXY } from 'cheminfo-types'; | ||
import { toBeDeepCloseTo, toMatchCloseTo } from 'jest-matcher-deep-close-to'; | ||
import { generateSpectrum } from 'spectrum-generator'; | ||
|
||
import { optimize } from '../index'; | ||
|
||
expect.extend({ toBeDeepCloseTo, toMatchCloseTo }); | ||
|
||
describe('Optimize sum of Gaussians', () => { | ||
const peaks = [ | ||
{ x: -0.5, y: 1, shape: { kind: 'gaussian' as const, fwhm: 0.05 } }, | ||
{ x: 0.5, y: 1, shape: { kind: 'gaussian' as const, fwhm: 0.05 } }, | ||
]; | ||
|
||
const data: DataXY = generateSpectrum(peaks, { | ||
generator: { | ||
from: -1, | ||
to: 1, | ||
nbPoints: 1024, | ||
shape: { kind: 'gaussian' }, | ||
}, | ||
}); | ||
|
||
let result = optimize( | ||
data, | ||
[ | ||
{ | ||
x: -0.55, | ||
y: 0.9, | ||
shape: { kind: 'gaussian' as const, fwhm: 0.08 }, | ||
parameters: { | ||
x: { min: -0.49, max: -0.512 }, | ||
y: { min: 0.9, max: 1.2 }, | ||
fwhm: { min: 0.04, max: 0.07 }, | ||
}, | ||
}, | ||
{ | ||
x: 0.55, | ||
y: 0.9, | ||
shape: { kind: 'gaussian' as const, fwhm: 0.08 }, | ||
parameters: { | ||
x: { min: 0.49, max: 0.512 }, | ||
y: { min: 0.9, max: 1.2 }, | ||
fwhm: { min: 0.04, max: 0.07 }, | ||
}, | ||
}, | ||
], | ||
{ | ||
optimization: { | ||
kind: 'direct', | ||
options: { | ||
maxIterations: 20, | ||
}, | ||
}, | ||
}, | ||
); | ||
for (let i = 0; i < 2; i++) { | ||
const peak = peaks[i]; | ||
for (const key in peak) { | ||
//@ts-expect-error | ||
const value = peak[key]; | ||
it(`peak at ${peak.x} key: ${key}`, () => { | ||
//@ts-expect-error | ||
expect(result.peaks[i][key]).toMatchCloseTo(value, 2); | ||
}); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { DataXY } from 'cheminfo-types'; | ||
import direct from 'ml-direct'; | ||
|
||
export function directOptimization( | ||
data: DataXY, | ||
sumOfShapes: (parameters: number[]) => (x: number) => number, | ||
options: any, | ||
) { | ||
const { | ||
minValues, | ||
maxValues, | ||
maxIterations, | ||
epsilon, | ||
tolerance, | ||
tolerance2, | ||
initialState, | ||
} = options; | ||
const objectiveFunction = getObjectiveFunction(data, sumOfShapes); | ||
const result = direct(objectiveFunction, minValues, maxValues, { | ||
iterations: maxIterations, | ||
epsilon, | ||
tolerance, | ||
tolerance2, | ||
initialState, | ||
}); | ||
|
||
const { optima } = result; | ||
|
||
return { | ||
parameterError: result.minFunctionValue, | ||
iterations: result.iterations, | ||
parameterValues: optima[0], | ||
}; | ||
} | ||
|
||
function getObjectiveFunction( | ||
data: DataXY, | ||
sumOfShapes: (parameters: number[]) => (x: number) => number, | ||
) { | ||
const { x, y } = data; | ||
const nbPoints = x.length; | ||
return (parameters: number[]) => { | ||
const fct = sumOfShapes(parameters); | ||
let error = 0; | ||
for (let i = 0; i < nbPoints; i++) { | ||
error += Math.pow(y[i] - fct(x[i]), 2); | ||
} | ||
return error; | ||
}; | ||
} |