Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Math-Core-Process/PMIterativeProcess.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ PMIterativeProcess >> initialize [
super initialize.

desiredPrecision := self class defaultPrecision.
maximumIterations := self class defaultMaximumIterations.
^ self
maximumIterations := self class defaultMaximumIterations
]

{ #category : #operation }
Expand Down
80 changes: 48 additions & 32 deletions src/Math-FunctionFit/PMAnotherChromosomeManager.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Class {
#instVars : [
'hammersley',
'rateOfLC',
'rateOfEir'
'rateOfEir',
'randomGenerator'
],
#classVars : [
'Primes'
Expand All @@ -33,28 +34,29 @@ PMAnotherChromosomeManager class >> integerDigitsFor: anInteger base: aBase [
]

{ #category : #utilities }
PMAnotherChromosomeManager class >> numberOfHamersleyPoints: n dimension: d randomized: aBoolean [
PMAnotherChromosomeManager class >> numberOfHamersleyPoints: n dimension: d randomGenerator: randomGenerator [
"a bit randomized "
| dist randomNumberGenerator |

| dist |
dist := 1.0 / n.
randomNumberGenerator := Random new.

^(1 to: n) collect: [ :number |
(1 to: d) collect: [ :dim |
(dim=1)
ifTrue: [ aBoolean
ifTrue: [ (number/n) - (randomNumberGenerator nextBetween: 0 and: dist) ]
ifFalse: [ 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 ].

aBoolean
ifTrue: [ sum + (randomNumberGenerator nextBetween: 0 and: dist) ]
ifFalse: [ sum ] ] ]].

^ (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' }
Expand Down Expand Up @@ -108,14 +110,15 @@ PMAnotherChromosomeManager >> eirCrossover: aChromosome1 and: aChromosome2 [

{ #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)"

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 }
Expand Down Expand Up @@ -205,16 +208,29 @@ PMAnotherChromosomeManager >> process: aChromosome1 and: aChromosome2 [
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 randomized: true.
population := population collect: [:aChr| aChr *range +origin ]

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 }
Expand Down
13 changes: 7 additions & 6 deletions src/Math-FunctionFit/PMAnotherGeneticOptimizer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ PMAnotherGeneticOptimizer >> computePrecision: whateverData [ "can be changed t

{ #category : #operation }
PMAnotherGeneticOptimizer >> evaluate [
^Cursor wait showWhile: [super evaluate]

^ Cursor wait showWhile: [ super evaluate ]
]

{ #category : #operation }
Expand Down Expand Up @@ -129,12 +130,12 @@ statistics :=false

{ #category : #operation }
PMAnotherGeneticOptimizer >> initializeIterations [
bestPoints size>=chromosomeManager populationSize
ifTrue: [bestPoints := bestPoints copyFrom: 1 to: (bestPoints size // 2 max: 1)].

bestPoints size >= chromosomeManager populationSize ifTrue: [ bestPoints := bestPoints copyFrom: 1 to: (bestPoints size // 2 max: 1) ].
super initializeIterations.
bestValueHistory:=OrderedCollection new.
worstValueHistory:=OrderedCollection new.
whateverHistory:=OrderedCollection new
bestValueHistory := OrderedCollection new.
worstValueHistory := OrderedCollection new.
whateverHistory := OrderedCollection new
]

{ #category : #operation }
Expand Down
5 changes: 3 additions & 2 deletions src/Math-FunctionFit/PMErrorMinimizer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PMErrorMinimizer class >> function: aBlock data: aCollection [

{ #category : #accessing }
PMErrorMinimizer >> maxFunction [
"The number of data partitions used."
^dataHolder size
"The number of data partitions used."

^ dataHolder size
]
7 changes: 6 additions & 1 deletion src/Math-FunctionFit/PMFunctionFit.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ PMFunctionFit >> accumulate: aWeightedPoint [

{ #category : #operation }
PMFunctionFit >> computeChanges [
^[super computeChanges]on: PMSingularMatrixError do:[:signal|signal messageText: 'singular error matrix, set better parameters'.signal pass]

^ [ super computeChanges ]
on: PMSingularMatrixError
do: [ :signal |
signal messageText: 'singular error matrix, set better parameters'.
signal pass ]
]

{ #category : #information }
Expand Down
83 changes: 45 additions & 38 deletions src/Math-FunctionFit/PMGeneralFunctionFit.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Class {
#name : #PMGeneralFunctionFit,
#superclass : #Object,
#instVars : [
'go',
'manager',
'errorFunction',
'result',
'firstResult',
'verbose',
'data',
'dataTruncated'
'dataTruncated',
'geneticOptimizer'
],
#category : #'Math-FunctionFit'
}
Expand Down Expand Up @@ -92,27 +92,31 @@ self resetResult.

{ #category : #operation }
PMGeneralFunctionFit >> evaluate [
|ff|
ff :=PMErrorMinimizer function: errorFunction.
firstResult :=[ff evaluate .ff parameters]
onErrorDo: [ verbose ifTrue: [self inform: 'ErrorMinimizer was not successful']. nil].
firstResult ifNotNil: [go addPointAt: firstResult] .
firstResult := go evaluate .
self errorType =#squared
ifTrue: [ff :=PMFunctionFit function: errorFunction function data: errorFunction data ].
ff parameters: firstResult.
ff desiredPrecision: PMFloatingPointMachine new machinePrecision.
ff maximumIterations: 1000 .
result:=[ff evaluate .ff parameters]
onErrorDo: [
verbose ifTrue: [self inform: 'last FunctionFit was not successful'].
ff result parameters].
((errorFunction value: result) > (errorFunction value: firstResult)) ifTrue:[
ff:=result.
result :=firstResult.
firstResult :=ff.
verbose ifTrue: [self inform: 'first result was better than final result' ] ].
^result

| ff |
ff := PMErrorMinimizer function: errorFunction.
firstResult := [
ff evaluate.
ff parameters ] onErrorDo: [
verbose ifTrue: [ self inform: 'ErrorMinimizer was not successful' ].
nil ].
firstResult ifNotNil: [ geneticOptimizer addPointAt: firstResult ].
firstResult := geneticOptimizer evaluate.
self errorType = #squared ifTrue: [ ff := PMFunctionFit function: errorFunction function data: errorFunction data ].
ff parameters: firstResult.
ff desiredPrecision: PMFloatingPointMachine new machinePrecision.
ff maximumIterations: 1000.
result := [
ff evaluate.
ff parameters ] onErrorDo: [
verbose ifTrue: [ self inform: 'last FunctionFit was not successful' ].
ff result parameters ].
(errorFunction value: result) > (errorFunction value: firstResult) ifTrue: [
ff := result.
result := firstResult.
firstResult := ff.
verbose ifTrue: [ self inform: 'first result was better than final result' ] ].
^ result
]

{ #category : #operation }
Expand Down Expand Up @@ -165,16 +169,19 @@ errorFunction function: aBlock

{ #category : #initialization }
PMGeneralFunctionFit >> initialize [
super initialize .
errorFunction :=PMErrorOfParameterFunction new.
manager :=PMAnotherChromosomeManager new.
manager populationSize: 50.
go :=PMAnotherGeneticOptimizer minimizingFunction: errorFunction .
go chromosomeManager: manager.
go maximumIterations: 170.
self errorType: #squared.
verbose:=false.
dataTruncated :=false

super initialize.
errorFunction := PMErrorOfParameterFunction new.
manager := PMAnotherChromosomeManager new
populationSize: 50;
yourself.
geneticOptimizer := (PMAnotherGeneticOptimizer minimizingFunction: errorFunction)
chromosomeManager: manager;
maximumIterations: 170;
yourself.
self errorType: #squared.
verbose := false.
dataTruncated := false
]

{ #category : #accessing }
Expand All @@ -186,7 +193,7 @@ PMGeneralFunctionFit >> manager [
{ #category : #accessing }
PMGeneralFunctionFit >> maximumIterations: anInteger [
"default is 170."
go maximumIterations: anInteger.
geneticOptimizer maximumIterations: anInteger.
^anInteger
]

Expand All @@ -207,7 +214,7 @@ manager range: (array2 - array1)
{ #category : #accessing }
PMGeneralFunctionFit >> optimizer [
"if you want to change parameters of AnotherGeneticOptimizer not directly available in GeneralFunctionFit"
^go
^geneticOptimizer
]

{ #category : #accessing }
Expand All @@ -221,7 +228,7 @@ manager populationSize: anInteger .
{ #category : #accessing }
PMGeneralFunctionFit >> precision [
"not really useful to look at, you might better look at #error"
^go precision
^geneticOptimizer precision
]

{ #category : #printing }
Expand All @@ -230,7 +237,7 @@ PMGeneralFunctionFit >> printOn: aStream [
nextPutAll: 'a ';
nextPutAll: self class name;
nextPut: $(;
print: go;
print: geneticOptimizer;
nextPutAll: ' with data of size: ';
print: data size .
dataTruncated ifTrue:
Expand Down Expand Up @@ -272,7 +279,7 @@ self data: data
PMGeneralFunctionFit >> resetResult [
result :=nil.
firstResult :=nil.
go resetBestPoints
geneticOptimizer resetBestPoints
]

{ #category : #accessing }
Expand Down
7 changes: 4 additions & 3 deletions src/Math-Numerical/PMFunctionOptimizer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ PMFunctionOptimizer >> initialValue: aVector [

{ #category : #initialization }
PMFunctionOptimizer >> initialize [
"Private"
bestPoints := SortedCollection sortBlock: [ :a :b | a betterThan: b].
^super initialize
"Private"

super initialize.
bestPoints := SortedCollection sortBlock: [ :a :b | a betterThan: b ]
]

{ #category : #initialization }
Expand Down
Loading