11Class {
22 #name : #PMOneVariableFunctionOptimizer ,
33 #superclass : #PMFunctionOptimizer ,
4+ #instVars : [
5+ ' randomGenerator'
6+ ],
47 #classVars : [
58 ' GoldenSection'
69 ],
@@ -24,69 +27,86 @@ PMOneVariableFunctionOptimizer class >> goldenSection [
2427{ #category : #operation }
2528PMOneVariableFunctionOptimizer >> computeInitialValues [
2629
27- [ bestPoints size > 3 ] whileTrue: [ bestPoints removeLast].
28- bestPoints size = 3
29- ifTrue: [ self hasBracketingPoints
30- ifFalse: [ bestPoints removeLast].
31- ].
32- bestPoints size < 3
33- ifTrue: [ ( PMOptimizingBracketFinder forOptimizer: self ) evaluate]
30+ | finder |
31+ [ bestPoints size > 3 ] whileTrue: [ bestPoints removeLast ].
32+
33+ bestPoints size = 3 ifTrue: [ self hasBracketingPoints ifFalse: [ bestPoints removeLast ] ].
34+
35+ bestPoints size < 3 ifFalse: [ ^ self ].
36+
37+ finder := PMOptimizingBracketFinder forOptimizer: self .
38+ self randomGenerator ifNotNil: [ :generator | finder randomGenerator: generator ].
39+ finder evaluate
3440]
3541
3642{ #category : #information }
3743PMOneVariableFunctionOptimizer >> computePrecision [
38- ^ self precisionOf: ( ( bestPoints at: 2 ) position - ( bestPoints at: 3 ) position) abs
39- relativeTo: ( bestPoints at: 1 ) position abs
44+
45+ ^ self precisionOf: ((bestPoints at: 2 ) position - (bestPoints at: 3 ) position) abs relativeTo: ( bestPoints at: 1 ) position abs
4046]
4147
4248{ #category : #operation }
4349PMOneVariableFunctionOptimizer >> evaluateIteration [
50+
4451 self addPointAt: self nextXValue.
4552 bestPoints removeAt: self indexOfOuterPoint.
46- ^ self computePrecision
53+ ^ self computePrecision
4754]
4855
4956{ #category : #information }
5057PMOneVariableFunctionOptimizer >> hasBracketingPoints [
51- " Private"
58+ " Private"
59+
5260 | x1 |
53- x1 := ( bestPoints at: 1 ) position.
54- ^ ( ( bestPoints at: 2 ) position - x1) * (( bestPoints at: 3 ) position - x1) < 0
61+ x1 := (bestPoints at: 1 ) position.
62+ ^ ( bestPoints at: 2 ) position - x1 * ((bestPoints at: 3 ) position - x1) < 0
5563]
5664
5765{ #category : #information }
5866PMOneVariableFunctionOptimizer >> indexOfOuterPoint [
59- " Private"
67+ " Private"
68+
6069 | inferior superior x |
6170 inferior := false .
6271 superior := false .
6372 x := bestPoints first position.
64- 2 to: 4 do:
65- [ :n |
66- ( bestPoints at: n) position < x
67- ifTrue: [ inferior
68- ifTrue: [ ^ n].
69- inferior := true .
70- ]
71- ifFalse: [ superior
72- ifTrue: [ ^ n].
73- superior := true .
74- ].
75- ]
73+ 2 to: 4 do: [ :n |
74+ (bestPoints at: n) position < x
75+ ifTrue: [
76+ inferior ifTrue: [ ^ n ].
77+ inferior := true ]
78+ ifFalse: [
79+ superior ifTrue: [ ^ n ].
80+ superior := true ] ]
7681]
7782
7883{ #category : #information }
7984PMOneVariableFunctionOptimizer >> nextXValue [
8085 " Private"
86+
8187 | d3 d2 x1 |
82- x1 := ( bestPoints at: 1 ) position.
83- d2 := ( bestPoints at: 2 ) position - x1.
84- d3 := ( bestPoints at: 3 ) position - x1.
85- ^ ( d3 abs > d2 abs ifTrue: [ d3]
86- ifFalse: [ d2]) * self class goldenSection + x1
88+ x1 := (bestPoints at: 1 ) position.
89+ d2 := (bestPoints at: 2 ) position - x1.
90+ d3 := (bestPoints at: 3 ) position - x1.
91+ ^ (d3 abs > d2 abs
92+ ifTrue: [ d3 ]
93+ ifFalse: [ d2 ]) * self class goldenSection + x1
94+ ]
95+
96+ { #category : #accessing }
97+ PMOneVariableFunctionOptimizer >> randomGenerator [
98+
99+ ^ randomGenerator
100+ ]
101+
102+ { #category : #accessing }
103+ PMOneVariableFunctionOptimizer >> randomGenerator: anObject [
104+
105+ randomGenerator := anObject
87106]
88107
89108{ #category : #transformation }
90109PMOneVariableFunctionOptimizer >> reset [
91- [ bestPoints isEmpty] whileFalse: [ bestPoints removeLast]
110+
111+ bestPoints removeAll
92112]
0 commit comments