-
Notifications
You must be signed in to change notification settings - Fork 23
/
2-D_continuous_seed.py
executable file
·123 lines (92 loc) · 4.77 KB
/
2-D_continuous_seed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
"""
Copyright (c) 2013, Triad National Security, LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Triad National Security, LLC nor the names of its contributors may be used to endorse or
promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
from pyharmonysearch import ObjectiveFunctionInterface, harmony_search
from math import pow
import random
class ObjectiveFunction(ObjectiveFunctionInterface):
"""
This is a toy objective function that contains only continuous variables. A random seed is used, so the same result
will be generated every time.
Goal:
maximize -(x^2 + (y+1)^2) + 4
The maximum is 4 at (0, -1).
Note that since all variables are continuous, we don't actually need to implement get_index() and get_num_discrete_values().
Warning: Stochastically solving a linear system is dumb. This is just a toy example.
"""
def __init__(self):
self._lower_bounds = [-1000, -1000]
self._upper_bounds = [1000, 1000]
self._variable = [True, True]
# define all input parameters
self._maximize = True # do we maximize or minimize?
self._max_imp = 50000 # maximum number of improvisations
self._hms = 100 # harmony memory size
self._hmcr = 0.75 # harmony memory considering rate
self._par = 0.5 # pitch adjusting rate
self._mpap = 0.25 # maximum pitch adjustment proportion (new parameter defined in pitch_adjustment()) - used for continuous variables only
self._mpai = 2 # maximum pitch adjustment index (also defined in pitch_adjustment()) - used for discrete variables only
self._random_seed = 8675309 # optional random seed for reproducible results
def get_fitness(self, vector):
"""
maximize -(x^2 + (y+1)^2) + 4
The maximum is 4 at (0, -1).
"""
return -(pow(vector[0], 2) + pow(vector[1] + 1, 2)) + 4
def get_value(self, i, index=None):
"""
Values are returned uniformly at random in their entire range. Since both parameters are continuous, index can be ignored.
"""
return random.uniform(self._lower_bounds[i], self._upper_bounds[i])
def get_lower_bound(self, i):
return self._lower_bounds[i]
def get_upper_bound(self, i):
return self._upper_bounds[i]
def is_variable(self, i):
return self._variable[i]
def is_discrete(self, i):
# all variables are continuous
return False
def get_num_parameters(self):
return len(self._lower_bounds)
def use_random_seed(self):
return hasattr(self, '_random_seed') and self._random_seed
def get_random_seed(self):
return self._random_seed
def get_max_imp(self):
return self._max_imp
def get_hmcr(self):
return self._hmcr
def get_par(self):
return self._par
def get_hms(self):
return self._hms
def get_mpai(self):
return self._mpai
def get_mpap(self):
return self._mpap
def maximize(self):
return self._maximize
if __name__ == '__main__':
obj_fun = ObjectiveFunction()
num_processes = 1
num_iterations = 1 # because random_seed is defined, there's no point in running this multiple times
results = harmony_search(obj_fun, num_processes, num_iterations)
print('Elapsed time: {}\nBest harmony: {}\nBest fitness: {}'.format(results.elapsed_time, results.best_harmony, results.best_fitness))