|
1
|
|
|
# encoding=utf8 |
|
2
|
|
|
# pylint: disable=mixed-indentation, trailing-whitespace, multiple-statements, attribute-defined-outside-init, logging-not-lazy, arguments-differ, redefined-outer-name, bad-continuation, unused-argument |
|
3
|
|
|
import logging |
|
4
|
|
|
|
|
5
|
|
|
from numpy import random as rand |
|
6
|
|
|
|
|
7
|
|
|
from NiaPy.algorithms.algorithm import Algorithm |
|
8
|
|
|
|
|
9
|
|
|
logging.basicConfig() |
|
10
|
|
|
logger = logging.getLogger('NiaPy.algorithms.other') |
|
11
|
|
|
logger.setLevel('INFO') |
|
12
|
|
|
|
|
13
|
|
|
__all__ = ['HillClimbAlgorithm'] |
|
14
|
|
|
|
|
15
|
|
|
def Neighborhood(x, delta, task, rnd=rand): |
|
16
|
|
|
r"""Get neighbours of point. |
|
17
|
|
|
|
|
18
|
|
|
Args: |
|
19
|
|
|
x numpy.ndarray: Point. |
|
20
|
|
|
delta (float): Standard deviation. |
|
21
|
|
|
task (Task): Optimization task. |
|
22
|
|
|
rnd (Optional[mtrand.RandomState]): Random generator. |
|
23
|
|
|
|
|
24
|
|
|
Returns: |
|
25
|
|
|
Tuple[numpy.ndarray, float]: |
|
26
|
|
|
1. New solution. |
|
27
|
|
|
2. New solutions function/fitness value. |
|
28
|
|
|
""" |
|
29
|
|
|
X = x + rnd.normal(0, delta, task.D) |
|
30
|
|
|
X = task.repair(X, rnd) |
|
31
|
|
|
Xfit = task.eval(X) |
|
32
|
|
|
return X, Xfit |
|
33
|
|
|
|
|
34
|
|
|
class HillClimbAlgorithm(Algorithm): |
|
35
|
|
|
r"""Implementation of iterative hill climbing algorithm. |
|
36
|
|
|
|
|
37
|
|
|
Algorithm: |
|
38
|
|
|
Hill Climbing Algorithm |
|
39
|
|
|
|
|
40
|
|
|
Date: |
|
41
|
|
|
2018 |
|
42
|
|
|
|
|
43
|
|
|
Authors: |
|
44
|
|
|
Jan Popič |
|
45
|
|
|
|
|
46
|
|
|
License: |
|
47
|
|
|
MIT |
|
48
|
|
|
|
|
49
|
|
|
Reference URL: |
|
50
|
|
|
|
|
51
|
|
|
Reference paper: |
|
52
|
|
|
|
|
53
|
|
|
See Also: |
|
54
|
|
|
* :class:`NiaPy.algorithms.Algorithm` |
|
55
|
|
|
|
|
56
|
|
|
Attributes: |
|
57
|
|
|
delta (float): Change for searching in neighborhood. |
|
58
|
|
|
Neighborhood (Callable[numpy.ndarray, float, Task], Tuple[numpy.ndarray, float]]): Function for getting neighbours. |
|
59
|
|
|
""" |
|
60
|
|
|
Name = ['HillClimbAlgorithm', 'BBFA'] |
|
61
|
|
|
|
|
62
|
|
|
@staticmethod |
|
63
|
|
|
def algorithmInfo(): |
|
64
|
|
|
r"""Get basic information of algorithm. |
|
65
|
|
|
|
|
66
|
|
|
Returns: |
|
67
|
|
|
str: Basic information. |
|
68
|
|
|
""" |
|
69
|
|
|
return r"""TODO""" |
|
70
|
|
|
|
|
71
|
|
|
@staticmethod |
|
72
|
|
|
def typeParameters(): |
|
73
|
|
|
r"""TODO. |
|
74
|
|
|
|
|
75
|
|
|
Returns: |
|
76
|
|
|
Dict[str, Callable]: |
|
77
|
|
|
* delta (Callable[[Union[int, float]], bool]): TODO |
|
78
|
|
|
""" |
|
79
|
|
|
return {'delta': lambda x: isinstance(x, (int, float)) and x > 0} |
|
80
|
|
|
|
|
81
|
|
|
def setParameters(self, delta=0.5, Neighborhood=Neighborhood, **ukwargs): |
|
82
|
|
|
r"""Set the algorithm parameters/arguments. |
|
83
|
|
|
|
|
84
|
|
|
Args: |
|
85
|
|
|
* delta (Optional[float]): Change for searching in neighborhood. |
|
86
|
|
|
* Neighborhood (Optional[Callable[numpy.ndarray, float, Task], Tuple[numpy.ndarray, float]]]): Function for getting neighbours. |
|
87
|
|
|
""" |
|
88
|
|
|
Algorithm.setParameters(self, NP=1, **ukwargs) |
|
89
|
|
|
self.delta, self.Neighborhood = delta, Neighborhood |
|
90
|
|
|
if ukwargs: logger.info('Unused arguments: %s' % (ukwargs)) |
|
91
|
|
|
|
|
92
|
|
|
def initPopulation(self, task): |
|
93
|
|
|
r"""Initialize stating point. |
|
94
|
|
|
|
|
95
|
|
|
Args: |
|
96
|
|
|
task (Task): Optimization task. |
|
97
|
|
|
|
|
98
|
|
|
Returns: |
|
99
|
|
|
Tuple[numpy.ndarray, float, Dict[str, Any]]: |
|
100
|
|
|
1. New individual. |
|
101
|
|
|
2. New individual function/fitness value. |
|
102
|
|
|
3. Additional arguments. |
|
103
|
|
|
""" |
|
104
|
|
|
x = task.Lower + self.rand(task.D) * task.bRange |
|
105
|
|
|
return x, task.eval(x), {} |
|
106
|
|
|
|
|
107
|
|
|
def runIteration(self, task, x, fx, xb, fxb, **dparams): |
|
108
|
|
|
r"""Core function of HillClimbAlgorithm algorithm. |
|
109
|
|
|
|
|
110
|
|
|
Args: |
|
111
|
|
|
task (Task): Optimization task. |
|
112
|
|
|
x (numpy.ndarray): Current solution. |
|
113
|
|
|
fx (float): Current solutions fitness/function value. |
|
114
|
|
|
xb (numpy.ndarray): Global best solution. |
|
115
|
|
|
fxb (float): Global best solutions function/fitness value. |
|
116
|
|
|
**dparams (Dict[str, Any]): Additional arguments. |
|
117
|
|
|
|
|
118
|
|
|
Returns: |
|
119
|
|
|
Tuple[numpy.ndarray, float, Dict[str, Any]]: |
|
120
|
|
|
1. New solution. |
|
121
|
|
|
2. New solutions function/fitness value. |
|
122
|
|
|
3. Additional arguments. |
|
123
|
|
|
""" |
|
124
|
|
|
lo, xn = False, task.bcLower() + task.bcRange() * self.rand(task.D) |
|
125
|
|
|
xn_f = task.eval(xn) |
|
126
|
|
|
while not lo: |
|
127
|
|
|
yn, yn_f = self.Neighborhood(x, self.delta, task, rnd=self.Rand) |
|
128
|
|
|
if yn_f < xn_f: xn, xn_f = yn, yn_f |
|
129
|
|
|
else: lo = True or task.stopCond() |
|
130
|
|
|
return xn, xn_f, {} |
|
131
|
|
|
|
|
132
|
|
|
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3 |
|
133
|
|
|
|