-
Notifications
You must be signed in to change notification settings - Fork 2
To increase usability of minimizer it should be possible to set the tolerance and the max number of iteration #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andped10
merged 10 commits into
develop
from
81-to-increase-usability-of-minimizer-it-should-be-possible-to-set-the-tolerance-and-the-max-number-of-iteration
Nov 13, 2024
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3a3738f
Merge pull request #76 from EasyScience/develop
andped10 b449718
max evaluations
andped10 59c4a90
Merge pull request #83 from EasyScience/develop
AndrewSazonov 1f56c0e
tolerance still needs to be done for bumps and dfo
andped10 6a6018b
seems to work for all minimizers
andped10 1168fe3
code cleaning
andped10 9276410
Merge commit '59c4a90292205f9f07ff8d95a52068f80f6e12c0' into 81-to-in…
andped10 b6c23f8
changed to max_iteration and tolerance to property
andped10 3bda6df
expose minimizer enum
andped10 1f4df44
pr response
andped10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -53,9 +53,7 @@ def supported_methods() -> List[str]: | |
|
||
@staticmethod | ||
def all_methods() -> List[str]: | ||
return [ | ||
'leastsq', | ||
] | ||
return ['leastsq'] | ||
|
||
def fit( | ||
self, | ||
|
@@ -65,8 +63,8 @@ def fit( | |
model: Optional[Callable] = None, | ||
parameters: Optional[List[Parameter]] = None, | ||
method: str = None, | ||
xtol: float = 1e-6, | ||
ftol: float = 1e-8, | ||
tolerance: Optional[float] = None, | ||
max_evaluations: Optional[int] = None, | ||
**kwargs, | ||
) -> FitResults: | ||
""" | ||
|
@@ -110,6 +108,8 @@ def fit( | |
stack_status = global_object.stack.enabled | ||
global_object.stack.enabled = False | ||
|
||
kwargs = self._prepare_kwargs(tolerance, max_evaluations, **kwargs) | ||
|
||
try: | ||
model_results = self._dfo_fit(self._cached_pars, model, **kwargs) | ||
self._set_parameter_fit_result(model_results, stack_status) | ||
|
@@ -239,7 +239,11 @@ def _gen_fit_results(self, fit_results, weights, **kwargs) -> FitResults: | |
return results | ||
|
||
@staticmethod | ||
def _dfo_fit(pars: Dict[str, Parameter], model: Callable, **kwargs): | ||
def _dfo_fit( | ||
pars: Dict[str, Parameter], | ||
model: Callable, | ||
**kwargs, | ||
): | ||
""" | ||
Method to convert EasyScience styling to DFO-LS styling (yes, again) | ||
|
||
|
@@ -261,13 +265,23 @@ def _dfo_fit(pars: Dict[str, Parameter], model: Callable, **kwargs): | |
np.array([par.max for par in pars.values()]), | ||
) | ||
# https://numericalalgorithmsgroup.github.io/dfols/build/html/userguide.html | ||
if np.isinf(bounds).any(): | ||
results = dfols.solve(model, pars_values, bounds=bounds, **kwargs) | ||
else: | ||
if not np.isinf(bounds).any(): | ||
# It is only possible to scale (normalize) variables if they are bound (different from inf) | ||
results = dfols.solve(model, pars_values, bounds=bounds, scaling_within_bounds=True, **kwargs) | ||
kwargs['scaling_within_bounds'] = True | ||
|
||
results = dfols.solve(model, pars_values, bounds=bounds, **kwargs) | ||
|
||
if 'Success' not in results.msg: | ||
raise FitError(f'Fit failed with message: {results.msg}') | ||
|
||
return results | ||
|
||
@staticmethod | ||
def _prepare_kwargs(tolerance: Optional[float] = None, max_evaluations: Optional[int] = None, **kwargs) -> dict[str:str]: | ||
if max_evaluations is not None: | ||
kwargs['maxfun'] = max_evaluations # max number of function evaluations | ||
if tolerance is not None: | ||
if 0.1 < tolerance: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is 0.1 chosen as the maximum possible tolerance value? |
||
raise ValueError('Tolerance must be equal or smaller than 0.1') | ||
kwargs['rhoend'] = tolerance # size of the trust region | ||
return kwargs |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is 0.1 chosen as the maximum possible tolerance value?