-
Notifications
You must be signed in to change notification settings - Fork 1
/
IHS.py
145 lines (122 loc) · 4.68 KB
/
IHS.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
from pprint import pprint
import numpy as np
import copy
from random import uniform
from VariablesParser import *
class IHSAlgorithm:
def __init__(self):
self._HM = []
self._HMS = 4
self._HMCRmax = 0.9
self._HMCRmin = 0.1
self._PARmax = 1
self._PARmin = 0.1
self._BWmax = 10
self._BWmin = 0.0001
self._NumOfIterations = 1000
self._variables = []
self._varUpperBounds = []
self._varLowerBounds = []
self._f = np.empty(self._HMS)
self._generation = 0
self._objective_function = lambda X: sum(X)
self.compute = lambda X: self._objective_function(X)
self._trace = []
self._lastBestSolutionIteration = 0
def initializeHM(self):
def catchZeroDivision(i):
inputVector = {}
for counter, var in enumerate(self._variables):
inputVector.update({var: uniform(self._varLowerBounds[counter], self._varUpperBounds[counter])})
self._HM.append(inputVector)
try:
self._f[i] = self.compute(self, inputVector)
except ZeroDivisionError or RuntimeWarning:
print("You can't '/0' - Not!")
raise
self._f = np.empty(self._HMS)
for i in range(self._HMS):
catchZeroDivision(i)
def improvise(self):
new = {}
for i, variables in enumerate(self._variables):
upperBound = self._varUpperBounds[i]
lowerBound = self._varLowerBounds[i]
if uniform(0, 1) < self._HMCR:
D1 = int(uniform(0, self._HMS))
D2 = self._HM[D1].get(variables)
new.update({variables: D2})
if uniform(0, 1) < self._PAR:
if uniform(0, 1) < 0.5:
D3 = (new.get(variables) -
uniform(0, self._BW)
)
if lowerBound <= D3:
new.update({variables: D3})
else:
D3 = (new.get(variables) +
uniform(0, self._BW)
)
if upperBound >= D3:
new.update({variables: D3})
else:
new.update({variables: uniform(lowerBound,
upperBound )})
return new
def updateHM(self, new):
f = self.compute(self, new)
fMaxValue = np.amax(self._f)
if f < fMaxValue:
for i, value in enumerate(self._f):
if fMaxValue == value:
self._f[i] = f
self._HM[i] = new
break
def _findTrace(self):
index = np.argmin(self._f)
variables = self._HM[index]
if variables not in self._trace:
self._trace.append(variables)
self._lastBestSolutionIteration = self._generation
def doYourTask(self):
def catchZeroDivision():
try:
new = self.improvise()
self.updateHM(new)
except ZeroDivisionError or RuntimeWarning:
print('I caughed ZeroDiv in IHS.updateHM')
catchZeroDivision()
self.initializeHM()
while self._generation < self._NumOfIterations:
self._generation += 1
self._updateHMCR()
self._updatePAR()
self._updateBW()
catchZeroDivision()
self._findTrace()
def _updateHMCR(self):
self._HMCR = (self._HMCRmax - self._generation *
(self._HMCRmax - self._HMCRmin) / self._NumOfIterations)
def _updatePAR(self):
self._PAR = (self._PARmin + self._generation *
(self._PARmax - self._PARmin) / len(self._variables))
def _updateBW(self):
c = log(self._BWmin / self._BWmax)
self._BW = self._BWmax * exp(self._generation * c)
def getOptimalSolution(self):
index = np.argmin(self._f)
functionValue = self._f[index]
variables = self._HM[index]
preparedVariables = []
for key, value in variables.items():
try:
preparedVariables.append(f'{key}:\t{value}')
except TypeError as e:
print(e)
return
return functionValue, preparedVariables
def getTrace(self):
return self._trace
def getLastBestSolutionIteration(self):
return self._lastBestSolutionIteration