Skip to content

Commit 622fd2c

Browse files
committed
ensmallen lbfgs improve
1 parent de1acd0 commit 622fd2c

2 files changed

Lines changed: 39 additions & 21 deletions

File tree

src/optimagic/algorithms.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ class BoundedGradientBasedLocalScalarAlgorithms(AlgoSelection):
294294
nlopt_slsqp: Type[NloptSLSQP] = NloptSLSQP
295295
nlopt_tnewton: Type[NloptTNewton] = NloptTNewton
296296
nlopt_var: Type[NloptVAR] = NloptVAR
297-
ensmallen_lbfgs: Type[EnsmallenLBFGS] = EnsmallenLBFGS
298297
scipy_lbfgsb: Type[ScipyLBFGSB] = ScipyLBFGSB
299298
scipy_slsqp: Type[ScipySLSQP] = ScipySLSQP
300299
scipy_truncated_newton: Type[ScipyTruncatedNewton] = ScipyTruncatedNewton
@@ -849,7 +848,6 @@ class BoundedGradientBasedLocalAlgorithms(AlgoSelection):
849848
nlopt_slsqp: Type[NloptSLSQP] = NloptSLSQP
850849
nlopt_tnewton: Type[NloptTNewton] = NloptTNewton
851850
nlopt_var: Type[NloptVAR] = NloptVAR
852-
ensmallen_lbfgs: Type[EnsmallenLBFGS] = EnsmallenLBFGS
853851
scipy_lbfgsb: Type[ScipyLBFGSB] = ScipyLBFGSB
854852
scipy_ls_dogbox: Type[ScipyLSDogbox] = ScipyLSDogbox
855853
scipy_ls_trf: Type[ScipyLSTRF] = ScipyLSTRF
@@ -967,7 +965,6 @@ class BoundedGradientBasedScalarAlgorithms(AlgoSelection):
967965
nlopt_slsqp: Type[NloptSLSQP] = NloptSLSQP
968966
nlopt_tnewton: Type[NloptTNewton] = NloptTNewton
969967
nlopt_var: Type[NloptVAR] = NloptVAR
970-
ensmallen_lbfgs: Type[EnsmallenLBFGS] = EnsmallenLBFGS
971968
scipy_basinhopping: Type[ScipyBasinhopping] = ScipyBasinhopping
972969
scipy_dual_annealing: Type[ScipyDualAnnealing] = ScipyDualAnnealing
973970
scipy_lbfgsb: Type[ScipyLBFGSB] = ScipyLBFGSB
@@ -1692,7 +1689,6 @@ class BoundedLocalScalarAlgorithms(AlgoSelection):
16921689
nlopt_sbplx: Type[NloptSbplx] = NloptSbplx
16931690
nlopt_tnewton: Type[NloptTNewton] = NloptTNewton
16941691
nlopt_var: Type[NloptVAR] = NloptVAR
1695-
ensmallen_lbfgs: Type[EnsmallenLBFGS] = EnsmallenLBFGS
16961692
scipy_lbfgsb: Type[ScipyLBFGSB] = ScipyLBFGSB
16971693
scipy_neldermead: Type[ScipyNelderMead] = ScipyNelderMead
16981694
scipy_powell: Type[ScipyPowell] = ScipyPowell
@@ -1999,7 +1995,6 @@ class BoundedGradientBasedAlgorithms(AlgoSelection):
19991995
nlopt_slsqp: Type[NloptSLSQP] = NloptSLSQP
20001996
nlopt_tnewton: Type[NloptTNewton] = NloptTNewton
20011997
nlopt_var: Type[NloptVAR] = NloptVAR
2002-
ensmallen_lbfgs: Type[EnsmallenLBFGS] = EnsmallenLBFGS
20031998
scipy_basinhopping: Type[ScipyBasinhopping] = ScipyBasinhopping
20041999
scipy_dual_annealing: Type[ScipyDualAnnealing] = ScipyDualAnnealing
20052000
scipy_lbfgsb: Type[ScipyLBFGSB] = ScipyLBFGSB
@@ -2601,7 +2596,6 @@ class BoundedLocalAlgorithms(AlgoSelection):
26012596
nlopt_tnewton: Type[NloptTNewton] = NloptTNewton
26022597
nlopt_var: Type[NloptVAR] = NloptVAR
26032598
pounders: Type[Pounders] = Pounders
2604-
ensmallen_lbfgs: Type[EnsmallenLBFGS] = EnsmallenLBFGS
26052599
scipy_lbfgsb: Type[ScipyLBFGSB] = ScipyLBFGSB
26062600
scipy_ls_dogbox: Type[ScipyLSDogbox] = ScipyLSDogbox
26072601
scipy_ls_trf: Type[ScipyLSTRF] = ScipyLSTRF
@@ -2837,7 +2831,6 @@ class BoundedScalarAlgorithms(AlgoSelection):
28372831
nlopt_sbplx: Type[NloptSbplx] = NloptSbplx
28382832
nlopt_tnewton: Type[NloptTNewton] = NloptTNewton
28392833
nlopt_var: Type[NloptVAR] = NloptVAR
2840-
ensmallen_lbfgs: Type[EnsmallenLBFGS] = EnsmallenLBFGS
28412834
pygmo_bee_colony: Type[PygmoBeeColony] = PygmoBeeColony
28422835
pygmo_cmaes: Type[PygmoCmaes] = PygmoCmaes
28432836
pygmo_compass_search: Type[PygmoCompassSearch] = PygmoCompassSearch
@@ -3349,7 +3342,6 @@ class BoundedAlgorithms(AlgoSelection):
33493342
nlopt_tnewton: Type[NloptTNewton] = NloptTNewton
33503343
nlopt_var: Type[NloptVAR] = NloptVAR
33513344
pounders: Type[Pounders] = Pounders
3352-
ensmallen_lbfgs: Type[EnsmallenLBFGS] = EnsmallenLBFGS
33533345
pygmo_bee_colony: Type[PygmoBeeColony] = PygmoBeeColony
33543346
pygmo_cmaes: Type[PygmoCmaes] = PygmoCmaes
33553347
pygmo_compass_search: Type[PygmoCompassSearch] = PygmoCompassSearch

src/optimagic/optimizers/pyensmallen_optimizers.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,28 @@
88

99
from optimagic import mark
1010
from optimagic.optimization.algo_options import (
11+
CONVERGENCE_FTOL_REL,
12+
CONVERGENCE_GTOL_ABS,
1113
MAX_LINE_SEARCH_STEPS,
1214
STOPPING_MAXITER,
1315
)
1416
from optimagic.optimization.algorithm import Algorithm, InternalOptimizeResult
1517
from optimagic.optimization.internal_optimization_problem import (
1618
InternalOptimizationProblem,
1719
)
18-
from optimagic.typing import (
19-
AggregationLevel,
20-
)
20+
from optimagic.typing import AggregationLevel, NonNegativeFloat, PositiveInt
21+
22+
LIMITED_MEMORY_MAX_HISTORY = 10
23+
"""Number of memory points to be stored (default 10)."""
24+
MIN_LINE_SEARCH_STEPS = 1e-20
25+
"""The minimum step of the line search."""
26+
MAX_LINE_SEARCH_TRIALS = 50
27+
"""The maximum number of trials for the line search (before giving up)."""
28+
ARMIJO_CONSTANT = 1e-4
29+
"""Controls the accuracy of the line search routine for determining the Armijo
30+
condition."""
31+
WOLFE_CONDITION = 0.9
32+
"""Parameter for detecting the Wolfe condition."""
2133

2234

2335
@mark.minimizer(
@@ -28,35 +40,49 @@
2840
needs_jac=True,
2941
needs_hess=False,
3042
supports_parallelism=False,
31-
supports_bounds=True,
43+
supports_bounds=False,
3244
supports_linear_constraints=False,
3345
supports_nonlinear_constraints=False,
3446
disable_history=False,
3547
)
3648
@dataclass(frozen=True)
3749
class EnsmallenLBFGS(Algorithm):
38-
stopping_maxiter = STOPPING_MAXITER
39-
max_step_for_line_search = MAX_LINE_SEARCH_STEPS
40-
# min_step_for_line_search = MIN_LINE_SEARCH_STEPS
50+
limited_memory_max_history: PositiveInt = LIMITED_MEMORY_MAX_HISTORY
51+
stopping_maxiter: PositiveInt = STOPPING_MAXITER
52+
armijo_constant: NonNegativeFloat = ARMIJO_CONSTANT # needs review
53+
wolfe_condition: NonNegativeFloat = WOLFE_CONDITION # needs review
54+
convergence_gtol_abs: NonNegativeFloat = CONVERGENCE_GTOL_ABS
55+
convergence_ftol_rel: NonNegativeFloat = CONVERGENCE_FTOL_REL
56+
max_line_search_trials: PositiveInt = MAX_LINE_SEARCH_TRIALS
57+
min_step_for_line_search: NonNegativeFloat = MIN_LINE_SEARCH_STEPS
58+
max_step_for_line_search: NonNegativeFloat = MAX_LINE_SEARCH_STEPS
4159

4260
def _solve_internal_problem(
4361
self, problem: InternalOptimizationProblem, x0: NDArray[np.float64]
4462
) -> InternalOptimizeResult:
45-
optimizer = pye.L_BFGS()
46-
47-
print(type(problem.fun(x0)))
63+
optimizer = pye.L_BFGS(
64+
numBasis=self.limited_memory_max_history,
65+
maxIterations=self.stopping_maxiter,
66+
armijoConstant=self.armijo_constant,
67+
wolfe=self.wolfe_condition,
68+
minGradientNorm=self.convergence_gtol_abs,
69+
factr=self.convergence_ftol_rel,
70+
maxLineSearchTrials=self.max_line_search_trials,
71+
minStep=self.min_step_for_line_search,
72+
maxStep=self.max_step_for_line_search,
73+
)
4874

4975
def objective_function(
5076
x: NDArray[np.float64], grad: NDArray[np.float64]
5177
) -> np.float64:
5278
grad[:] = problem.jac(x)
5379
return np.float64(problem.fun(x))
5480

55-
raw_res = optimizer.optimize(objective_function, x0)
81+
raw = optimizer.optimize(objective_function, x0)
5682

5783
res = InternalOptimizeResult(
58-
x=raw_res,
59-
fun=problem.fun(raw_res),
84+
x=raw, # only best x is available
85+
fun=problem.fun(raw), # best f(x) value is not available
6086
)
6187

6288
return res

0 commit comments

Comments
 (0)