forked from PolyMathOrg/PolyMath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPMAnotherChromosomeManager.class.st
More file actions
287 lines (238 loc) · 8.79 KB
/
PMAnotherChromosomeManager.class.st
File metadata and controls
287 lines (238 loc) · 8.79 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"
AnotherChromosomeManager implements more specific operations for Floats.
Is used by AnotherGeneticOptimizer
"
Class {
#name : 'PMAnotherChromosomeManager',
#superclass : 'PMVectorChromosomeManager',
#instVars : [
'hammersley',
'rateOfLC',
'rateOfEir',
'randomGenerator'
],
#classVars : [
'Primes'
],
#category : 'Math-FunctionFit',
#package : 'Math-FunctionFit'
}
{ #category : 'utilities' }
PMAnotherChromosomeManager class >> integerDigitsFor: anInteger base: aBase [
| n integer next result |
"n:=(anInteger ln / aBase ln) floor ." "does not always work because of floating point errors. next 2 lines are better"
"aBase has to be greater than 1. there is no error-checking"
n:=0.
[n:=n+1. (aBase raisedToInteger: n)<=anInteger ]whileTrue.
result := Array new: n.
integer :=anInteger .
n to: 1 by: -1 do: [:i |
next := integer // aBase .
result at: i put: (integer - (next * aBase)).
integer := next].
^result
]
{ #category : 'utilities' }
PMAnotherChromosomeManager class >> numberOfHamersleyPoints: n dimension: d randomGenerator: randomGenerator [
"a bit randomized "
| dist |
dist := 1.0 / n.
^ (1 to: n) collect: [ :number |
(1 to: d) collect: [ :dim |
dim = 1
ifTrue: [
randomGenerator
ifNotNil: [ number / n - (randomGenerator nextBetween: 0 and: dist) ]
ifNil: [ number / n ] ]
ifFalse: [
| sum prime |
sum := 0.
prime := Primes at: dim - 1.
(self integerDigitsFor: number base: prime) reverse withIndexDo: [ :i :index | sum := i / (prime raisedToInteger: index) + sum ].
randomGenerator
ifNotNil: [ sum + (randomGenerator nextBetween: 0 and: dist) ]
ifNil: [ sum ] ] ] ]
]
{ #category : 'instance creation' }
PMAnotherChromosomeManager class >> origin: anArray range: anotherArray [
^self new origin: anArray; range: anotherArray; yourself
]
{ #category : 'operation' }
PMAnotherChromosomeManager >> crossover: aChromosome1 and: aChromosome2 [
"the Discrete Recombination operator
that does not prefer schemata of certain parameters based on their position"
| new1 new2 |
aChromosome1 = aChromosome2 ifTrue:[
^ Array
with: (self mutate: aChromosome2)
with: (self mutate: aChromosome1) ].
new1 := self clone: aChromosome1.
new2 := self clone: aChromosome2.
2 to: new1 size do: [ :i |
(randomNumberGenerator next < 0.5) ifTrue: [
new1 at: i put: (aChromosome2 at: i).
new2 at: i put: (aChromosome1 at: i) ] ].
^ Array with: new1 with: new2
]
{ #category : 'operation' }
PMAnotherChromosomeManager >> eirCrossover: aChromosome1 and: aChromosome2 [
"the Extended Intermediate Recombination 0.5 operator, slightly changed to make it more similar to linecrossover (distribution is more centered around Chromosome1, which is better than C2)"
| randomNumbers new1 new2 dif |
dif := aChromosome2 - aChromosome1.
dif norm = 0 ifTrue: [
^ { self mutate: aChromosome2 . self mutate: aChromosome1 } ].
randomNumbers := (1 to: aChromosome1 size) collect: [ :i |
randomNumberGenerator nextBetween: -0.5 and: 1.5 ].
new1 := aChromosome1 + (randomNumbers * dif).
randomNumbers := (1 to: aChromosome1 size) collect: [ :i |
randomNumberGenerator nextBetween: -0.5 and: 0.5 ].
new2 := aChromosome1 + (randomNumbers * dif).
^ { new1 . new2 }
]
{ #category : 'initialization' }
PMAnotherChromosomeManager >> initialize [
super initialize.
populationSize := 100.
hammersley := true.
rateOfEir := 0.16.
rateOfLC := 0.29.
rateOfMutation := 0.4.
rateOfCrossover := 0.15.
Primes ifNil: [ Primes := Integer primesUpTo: 500 ] "sufficient for up to 95 dimensions (parameters)"
]
{ #category : 'information' }
PMAnotherChromosomeManager >> isFullyPopulated [
population ifNil: [^false].
^super isFullyPopulated
]
{ #category : 'operation' }
PMAnotherChromosomeManager >> lineCrossOver: aChromosome1 and: aChromosome2 [
"BGA Line Recombination; expects C1 to be better than C2, which is not correct at the moment, need to change that!!! mhm i think i did that."
| new1 new2 line norm|
line :=(aChromosome2 - aChromosome1).
norm := line norm.
norm =0 ifTrue:[^Array with: (self mutate: aChromosome1)with: (self mutate: aChromosome1)].
line :=line/norm * (self smallDistribution).
new1 :=aChromosome1 + line.
new2 := aChromosome1 - line.
^Array with: new1 with: new2
]
{ #category : 'operation' }
PMAnotherChromosomeManager >> mutate: aVector [
"BGA mutation"
| isMutated threshold new index |
isMutated := false.
threshold := 1 / aVector size asFloat.
new := aVector copy.
1 to: aVector size do: [ :i |
randomNumberGenerator next < threshold ifTrue: [
isMutated := true.
new at: i put: (new at: i) +
((randomNumberGenerator next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: i)) ] ].
isMutated ifFalse: [
index := randomNumberGenerator nextIntegerBetween: 1 and: aVector size.
new at: index put: (new at: index) +
((randomNumberGenerator next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: index)) ].
^ new
]
{ #category : 'accessing' }
PMAnotherChromosomeManager >> populationSize [
"AnotherGeneticOptimizer needs these data"
^populationSize
]
{ #category : 'printing' }
PMAnotherChromosomeManager >> printOn: aStream [
aStream
nextPutAll: self class name;
nextPutAll: '( popSize: ';print: populationSize ;
nextPutAll: ' origin: ';print: origin ;
nextPutAll: ' range: ';print: range ;
nextPutAll: ' hammersley ';print: hammersley ;
nextPutAll: ' MutRate: ';print: rateOfMutation ;
nextPutAll: ' CORate: ';print: rateOfCrossover ;
nextPutAll: ' LCRate: ';print: rateOfLC ;
nextPutAll: ' EIRRate: ';print: rateOfEir;
nextPut: $)
]
{ #category : 'operation' }
PMAnotherChromosomeManager >> process: aChromosome1 and: aChromosome2 [
| roll |
roll := randomNumberGenerator next.
roll < rateOfCrossover
ifTrue: [population addAll: (self crossover: aChromosome1 and: aChromosome2)]
ifFalse:
[roll < (rateOfCrossover + rateOfMutation)
ifTrue:
[population
add: (self mutate: aChromosome1);
add: (self mutate: aChromosome2)]
ifFalse:
[roll < (rateOfCrossover + rateOfMutation+rateOfEir)
ifTrue:
[population addAll: (self eirCrossover: aChromosome1 and: aChromosome2)]
ifFalse: [roll < (rateOfCrossover + rateOfMutation+rateOfEir+rateOfLC)
ifTrue:
[population addAll: (self lineCrossOver: aChromosome1 and: aChromosome2)]
ifFalse: [population
add: (self clone: aChromosome1);
add: (self clone: aChromosome2)]]]]
]
{ #category : 'accessing' }
PMAnotherChromosomeManager >> randomGenerator [
^ randomGenerator
]
{ #category : 'accessing' }
PMAnotherChromosomeManager >> randomGenerator: anObject [
randomGenerator := anObject
]
{ #category : 'private' }
PMAnotherChromosomeManager >> randomRangeAt: aPosition [
^(range at: aPosition )*(self smallDistribution )
]
{ #category : 'operation' }
PMAnotherChromosomeManager >> randomizePopulation [
hammersley ifFalse: [ ^ super randomizePopulation ].
population := self class numberOfHamersleyPoints: populationSize dimension: origin size randomGenerator: (self randomGenerator ifNil: [ Random new ]).
population := population collect: [ :aChr | aChr * range + origin ]
]
{ #category : 'accessing' }
PMAnotherChromosomeManager >> range [
"AnotherGeneticOptimizer needs these data"
^range
]
{ #category : 'initialization' }
PMAnotherChromosomeManager >> rateOfCrossover: aNumber [
self testRate: aNumber oldRate: rateOfCrossover name: 'rateOfCrossover'.
rateOfCrossover := aNumber
]
{ #category : 'initialization' }
PMAnotherChromosomeManager >> rateOfEir: aNumber [
self testRate: aNumber oldRate: rateOfEir name: 'rateOfEir'.
rateOfEir := aNumber
]
{ #category : 'initialization' }
PMAnotherChromosomeManager >> rateOfLC: aNumber [
self testRate: aNumber oldRate: rateOfLC name: 'rateOfLC'.
rateOfLC := aNumber
]
{ #category : 'initialization' }
PMAnotherChromosomeManager >> rateOfMutation: aNumber [
self testRate: aNumber oldRate: rateOfMutation name: 'rateOfMutation'.
rateOfMutation := aNumber
]
{ #category : 'private' }
PMAnotherChromosomeManager >> smallDistribution [
"an exponential distribution as used by H. Mühlenbein"
^ 2 raisedTo: (16 * randomNumberGenerator next negated)
]
{ #category : 'private' }
PMAnotherChromosomeManager >> testRate:aFloat oldRate: asecondFloat name:aString [
(aFloat between: 0 and: 1) ifFalse: [(DomainError new)from:0;to:1;messageText: 'Value outside [0 , 1]';signalIn: thisContext sender].
(rateOfCrossover + rateOfMutation + rateOfLC + rateOfEir + aFloat - asecondFloat)>1.000000000000001 "for Float inaccuracies"
ifTrue: [Warning signal:'All rates together are higher than 1, if ' , aString, ' is set to ',aFloat asString ]
]
{ #category : 'initialization' }
PMAnotherChromosomeManager >> useHammersley: aBoolean [
"default is true"
hammersley :=aBoolean
]