Skip to content

Commit d4d6b5d

Browse files
committed
Removed last references to random
1 parent 34ac92b commit d4d6b5d

File tree

4 files changed

+79
-49
lines changed

4 files changed

+79
-49
lines changed

src/Math-Chromosome/PMChromosomeManager.class.st

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Class {
55
'population',
66
'populationSize',
77
'rateOfMutation',
8-
'rateOfCrossover'
8+
'rateOfCrossover',
9+
'randomNumberGenerator'
910
],
1011
#category : #'Math-Chromosome'
1112
}
@@ -28,6 +29,12 @@ PMChromosomeManager >> crossover: aChromosome1 and: aChromosome2 [
2829
^self subclassResponsibility
2930
]
3031

32+
{ #category : #initialization }
33+
PMChromosomeManager >> initialize [
34+
super initialize.
35+
randomNumberGenerator := Random new.
36+
]
37+
3138
{ #category : #information }
3239
PMChromosomeManager >> isFullyPopulated [
3340

@@ -55,9 +62,8 @@ PMChromosomeManager >> populationSize: anInteger [
5562
{ #category : #operation }
5663
PMChromosomeManager >> process: aChromosome1 and: aChromosome2 [
5764

58-
| random roll |
59-
random := Random new.
60-
roll := random next.
65+
| roll |
66+
roll := randomNumberGenerator next.
6167
roll < rateOfCrossover
6268
ifTrue: [population addAll: (self crossover: aChromosome1 and: aChromosome2)]
6369
ifFalse:

src/Math-Chromosome/PMVectorChromosomeManager.class.st

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,33 @@ Class {
55
'origin',
66
'range'
77
],
8-
#category : 'Math-Chromosome'
8+
#category : #'Math-Chromosome'
99
}
1010

1111
{ #category : #operation }
1212
PMVectorChromosomeManager >> crossover: aChromosome1 and: aChromosome2 [
1313

14-
| index new1 new2|
15-
index := ( aChromosome1 size - 1) random + 2.
14+
| index new1 new2 |
15+
16+
index := randomNumberGenerator nextIntegerBetween: 2 and: aChromosome1 size.
17+
1618
new1 := self clone: aChromosome1.
1719
new1 replaceFrom: index to: new1 size with: aChromosome2 startingAt: index.
20+
1821
new2 := self clone: aChromosome2.
1922
new2 replaceFrom: index to: new2 size with: aChromosome1 startingAt: index.
20-
^Array with: new1 with: new2
23+
24+
^ Array with: new1 with: new2
2125
]
2226

2327
{ #category : #operation }
2428
PMVectorChromosomeManager >> mutate: aVector [
2529

2630
| index |
27-
index := aVector size random + 1.
31+
index := randomNumberGenerator nextIntegerBetween: 1 and: aVector size.
32+
2833
^( aVector copy)
29-
at: index put: ( self randomComponent: index);
34+
at: index put: (self randomComponent: index);
3035
yourself
3136
]
3237

@@ -39,13 +44,13 @@ PMVectorChromosomeManager >> origin: aVector [
3944
{ #category : #creation }
4045
PMVectorChromosomeManager >> randomChromosome [
4146

42-
^( ( 1 to: origin size) collect: [ :n | self randomComponent: n]) asPMVector
47+
^ ((1 to: origin size) collect: [ :n | self randomComponent: n ]) asPMVector
4348
]
4449

4550
{ #category : #information }
4651
PMVectorChromosomeManager >> randomComponent: anInteger [
4752

48-
^( range at: anInteger) asFloat random + ( origin at: anInteger)
53+
^ (randomNumberGenerator nextBetween: 0 and: (range at: anInteger)) + (origin at: anInteger)
4954
]
5055

5156
{ #category : #initialization }

src/Math-FunctionFit/PMAnotherChromosomeManager.class.st

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,27 @@ PMAnotherChromosomeManager class >> integerDigitsFor: anInteger base: aBase [
3434

3535
{ #category : #utils }
3636
PMAnotherChromosomeManager class >> numberOfHamersleyPoints: n dimension: d randomized: aBoolean [
37-
"a bit randomized "
38-
|dist|
39-
dist :=1.0 / n.
40-
^(1 to: n) collect: [:number| (1 to: d)collect: [:dim|
41-
( dim=1)
42-
ifTrue:[aBoolean ifTrue: [(number/n)- dist random] ifFalse: [(number/n)]]
43-
ifFalse: [|sum prime|
44-
sum :=0.
45-
prime :=Primes at: dim-1.
46-
(self integerDigitsFor: number base: prime)
47-
reverse withIndexDo: [:i :index|sum:= i/(prime raisedToInteger: index)+sum.].
48-
aBoolean ifTrue: [sum + dist random] ifFalse: [sum] ] ]].
37+
"a bit randomized "
38+
| dist randomNumberGenerator |
39+
dist := 1.0 / n.
40+
randomNumberGenerator := Random new.
41+
42+
^(1 to: n) collect: [ :number |
43+
(1 to: d) collect: [ :dim |
44+
(dim=1)
45+
ifTrue: [ aBoolean
46+
ifTrue: [ (number/n) - (randomNumberGenerator nextBetween: 0 and: dist) ]
47+
ifFalse: [ number/n ] ]
48+
ifFalse: [ |sum prime|
49+
sum := 0.
50+
prime := Primes at: dim - 1.
51+
52+
(self integerDigitsFor: number base: prime) reverse withIndexDo: [ :i :index |
53+
sum := i / (prime raisedToInteger: index) + sum ].
54+
55+
aBoolean
56+
ifTrue: [ sum + (randomNumberGenerator nextBetween: 0 and: dist) ]
57+
ifFalse: [ sum ] ] ]].
4958
]
5059

5160
{ #category : #'instance creation' }
@@ -57,7 +66,7 @@ PMAnotherChromosomeManager class >> origin: anArray range: anotherArray [
5766
PMAnotherChromosomeManager >> crossover: aChromosome1 and: aChromosome2 [
5867
"the Discrete Recombination operator
5968
that does not prefer schemata of certain parameters based on their position"
60-
| random new1 new2 |
69+
| new1 new2 |
6170

6271
aChromosome1 = aChromosome2 ifTrue:[
6372
^ Array
@@ -67,10 +76,8 @@ that does not prefer schemata of certain parameters based on their position"
6776
new1 := self clone: aChromosome1.
6877
new2 := self clone: aChromosome2.
6978

70-
random := Random new.
71-
7279
2 to: new1 size do: [ :i |
73-
(random next < 0.5) ifTrue: [
80+
(randomNumberGenerator next < 0.5) ifTrue: [
7481
new1 at: i put: (aChromosome2 at: i).
7582
new2 at: i put: (aChromosome1 at: i) ] ].
7683

@@ -79,15 +86,24 @@ that does not prefer schemata of certain parameters based on their position"
7986

8087
{ #category : #operation }
8188
PMAnotherChromosomeManager >> eirCrossover: aChromosome1 and: aChromosome2 [
82-
"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)"
83-
| r new1 new2 dif|
84-
dif:=aChromosome2 - aChromosome1.
85-
dif norm=0 ifTrue:[^Array with:( self mutate: aChromosome2) with: (self mutate: aChromosome1) ].
86-
r:=(1 to: aChromosome1 size) collect:[:i| 2.0 random -0.5] .
87-
new1 := aChromosome1+ (r * dif).
88-
r:=(1 to: aChromosome1 size) collect:[:i| 1.0 random -0.5] .
89-
new2 := aChromosome1+ (r * dif).
90-
^Array with: new1 with: new2
89+
"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)"
90+
| randomNumbers new1 new2 dif |
91+
dif := aChromosome2 - aChromosome1.
92+
93+
dif norm = 0 ifTrue: [
94+
^ { self mutate: aChromosome2 . self mutate: aChromosome1 } ].
95+
96+
randomNumbers := (1 to: aChromosome1 size) collect: [ :i |
97+
randomNumberGenerator nextBetween: -0.5 and: 1.5 ].
98+
99+
new1 := aChromosome1 + (randomNumbers * dif).
100+
101+
randomNumbers := (1 to: aChromosome1 size) collect: [ :i |
102+
randomNumberGenerator nextBetween: -0.5 and: 0.5 ].
103+
104+
new2 := aChromosome1 + (randomNumbers * dif).
105+
106+
^ { new1 . new2 }
91107

92108
]
93109

@@ -127,23 +143,22 @@ PMAnotherChromosomeManager >> lineCrossOver: aChromosome1 and: aChromosome2 [
127143
{ #category : #operation }
128144
PMAnotherChromosomeManager >> mutate: aVector [
129145
"BGA mutation"
130-
| isMutated threshold new random index|
146+
| isMutated threshold new index |
131147

132148
isMutated := false.
133149
threshold := 1 / aVector size asFloat.
134150
new := aVector copy.
135-
random := Random new.
136151

137152
1 to: aVector size do: [ :i |
138-
random next < threshold ifTrue: [
153+
randomNumberGenerator next < threshold ifTrue: [
139154
isMutated := true.
140155
new at: i put: (new at: i) +
141-
((random next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: i)) ] ].
156+
((randomNumberGenerator next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: i)) ] ].
142157

143158
isMutated ifFalse: [
144-
index := aVector size random + 1.
159+
index := randomNumberGenerator nextIntegerBetween: 1 and: aVector size.
145160
new at: index put: (new at: index) +
146-
((random next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: index)) ].
161+
((randomNumberGenerator next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: index)) ].
147162

148163
^ new
149164
]
@@ -171,9 +186,8 @@ PMAnotherChromosomeManager >> printOn: aStream [
171186

172187
{ #category : #operation }
173188
PMAnotherChromosomeManager >> process: aChromosome1 and: aChromosome2 [
174-
| random roll |
175-
random := Random new.
176-
roll := random next.
189+
| roll |
190+
roll := randomNumberGenerator next.
177191
roll < rateOfCrossover
178192
ifTrue: [population addAll: (self crossover: aChromosome1 and: aChromosome2)]
179193
ifFalse:
@@ -242,9 +256,7 @@ PMAnotherChromosomeManager >> rateOfMutation: aNumber [
242256
{ #category : #private }
243257
PMAnotherChromosomeManager >> smallDistribution [
244258
"an exponential distribution as used by H. Mühlenbein"
245-
| random |
246-
random := Random new.
247-
^ 2 raisedTo: (16 * random next negated)
259+
^ 2 raisedTo: (16 * randomNumberGenerator next negated)
248260
]
249261

250262
{ #category : #private }

src/Math-Tests-FunctionFit/PMAnotherChromosomeManagerTest.class.st

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ self assert: (s includesSubstring: '#(2 3)').
132132

133133
]
134134

135+
{ #category : #test }
136+
PMAnotherChromosomeManagerTest >> testProcessAnd [
137+
138+
self flag: #toImplement.
139+
self assert: false
140+
]
141+
135142
{ #category : #tests }
136143
PMAnotherChromosomeManagerTest >> testProcessand [
137144
cm reset.

0 commit comments

Comments
 (0)