1
|
|
|
"""Hill climbing optimizer from gfo.""" |
2
|
|
|
|
3
|
|
|
# copyright: hyperactive developers, MIT License (see LICENSE file) |
4
|
|
|
|
5
|
|
|
from hyperactive.opt._adapters._gfo import _BaseGFOadapter |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class HillClimbing(_BaseGFOadapter): |
9
|
|
|
"""Hill climbing optimizer. |
10
|
|
|
|
11
|
|
|
Parameters |
12
|
|
|
---------- |
13
|
|
|
search_space : dict[str, list] |
14
|
|
|
The search space to explore. A dictionary with parameter |
15
|
|
|
names as keys and a numpy array as values. |
16
|
|
|
Optional, can be passed later via ``set_params``. |
17
|
|
|
initialize : dict[str, int], default={"grid": 4, "random": 2, "vertices": 4} |
18
|
|
|
The method to generate initial positions. A dictionary with |
19
|
|
|
the following key literals and the corresponding value type: |
20
|
|
|
{"grid": int, "vertices": int, "random": int, "warm_start": list[dict]} |
21
|
|
|
constraints : list[callable], default=[] |
22
|
|
|
A list of constraints, where each constraint is a callable. |
23
|
|
|
The callable returns `True` or `False` dependend on the input parameters. |
24
|
|
|
random_state : None, int, default=None |
25
|
|
|
If None, create a new random state. If int, create a new random state |
26
|
|
|
seeded with the value. |
27
|
|
|
rand_rest_p : float, default=0.1 |
28
|
|
|
The probability of a random iteration during the the search process. |
29
|
|
|
epsilon : float, default=0.01 |
30
|
|
|
The step-size for the climbing. |
31
|
|
|
distribution : str, default="normal" |
32
|
|
|
The type of distribution to sample from. |
33
|
|
|
n_neighbours : int, default=10 |
34
|
|
|
The number of neighbours to sample and evaluate before moving to the best |
35
|
|
|
of those neighbours. |
36
|
|
|
n_iter : int, default=100 |
37
|
|
|
The number of iterations to run the optimizer. |
38
|
|
|
verbose : bool, default=False |
39
|
|
|
If True, print the progress of the optimization process. |
40
|
|
|
experiment : BaseExperiment, optional |
41
|
|
|
The experiment to optimize parameters for. |
42
|
|
|
Optional, can be passed later via ``set_params``. |
43
|
|
|
|
44
|
|
|
Examples |
45
|
|
|
-------- |
46
|
|
|
Hill climbing applied to scikit-learn parameter tuning: |
47
|
|
|
|
48
|
|
|
1. defining the experiment to optimize: |
49
|
|
|
>>> from hyperactive.experiment.integrations import SklearnCvExperiment |
50
|
|
|
>>> from sklearn.datasets import load_iris |
51
|
|
|
>>> from sklearn.svm import SVC |
52
|
|
|
>>> |
53
|
|
|
>>> X, y = load_iris(return_X_y=True) |
54
|
|
|
>>> |
55
|
|
|
>>> sklearn_exp = SklearnCvExperiment( |
56
|
|
|
... estimator=SVC(), |
57
|
|
|
... X=X, |
58
|
|
|
... y=y, |
59
|
|
|
... ) |
60
|
|
|
|
61
|
|
|
2. setting up the hill climbing optimizer: |
62
|
|
|
>>> from hyperactive.opt import HillClimbing |
63
|
|
|
>>> import numpy as np |
64
|
|
|
>>> |
65
|
|
|
>>> config = { |
66
|
|
|
... "search_space": { |
67
|
|
|
... "C": [0.01, 0.1, 1, 10], |
68
|
|
|
... "gamma": [0.0001, 0.01, 0.1, 1, 10], |
69
|
|
|
... }, |
70
|
|
|
... "n_iter": 100, |
71
|
|
|
... } |
72
|
|
|
>>> hillclimbing = HillClimbing(experiment=sklearn_exp, **config) |
73
|
|
|
|
74
|
|
|
3. running the hill climbing search: |
75
|
|
|
>>> best_params = hillclimbing.solve() |
76
|
|
|
|
77
|
|
|
Best parameters can also be accessed via the attributes: |
78
|
|
|
>>> best_params = hillclimbing.best_params_ |
79
|
|
|
""" |
80
|
|
|
|
81
|
|
|
_tags = { |
82
|
|
|
"info:name": "Hill Climbing", |
83
|
|
|
"info:local_vs_global": "local", # "local", "mixed", "global" |
84
|
|
|
"info:explore_vs_exploit": "exploit", # "explore", "exploit", "mixed" |
85
|
|
|
"info:compute": "low", # "low", "middle", "high" |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
def __init__( |
89
|
|
|
self, |
90
|
|
|
search_space=None, |
91
|
|
|
initialize=None, |
92
|
|
|
constraints=None, |
93
|
|
|
random_state=None, |
94
|
|
|
rand_rest_p=0.1, |
95
|
|
|
epsilon=0.01, |
96
|
|
|
distribution="normal", |
97
|
|
|
n_neighbours=10, |
98
|
|
|
n_iter=100, |
99
|
|
|
verbose=False, |
100
|
|
|
experiment=None, |
101
|
|
|
): |
102
|
|
|
self.random_state = random_state |
103
|
|
|
self.rand_rest_p = rand_rest_p |
104
|
|
|
self.epsilon = epsilon |
105
|
|
|
self.distribution = distribution |
106
|
|
|
self.n_neighbours = n_neighbours |
107
|
|
|
self.search_space = search_space |
108
|
|
|
self.initialize = initialize |
109
|
|
|
self.constraints = constraints |
110
|
|
|
self.n_iter = n_iter |
111
|
|
|
self.experiment = experiment |
112
|
|
|
self.verbose = verbose |
113
|
|
|
|
114
|
|
|
super().__init__() |
115
|
|
|
|
116
|
|
|
def _get_gfo_class(self): |
117
|
|
|
"""Get the GFO class to use. |
118
|
|
|
|
119
|
|
|
Returns |
120
|
|
|
------- |
121
|
|
|
class |
122
|
|
|
The GFO class to use. One of the concrete GFO classes |
123
|
|
|
""" |
124
|
|
|
from gradient_free_optimizers import HillClimbingOptimizer |
125
|
|
|
|
126
|
|
|
return HillClimbingOptimizer |
127
|
|
|
|