Skip to content

Commit a792951

Browse files
Merge pull request #189 from SergeStinckwich/master
Add one example of use of PMHillClimbingOptimizer
2 parents a392ade + 20c9448 commit a792951

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/Math-Numerical/PMHillClimbingOptimizer.class.st

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"
2+
PMHillClimbingOptimizer is a class implementing the Powell's algorithm. Powell's algorithm is one of many hill climbing algorithms.
3+
The idea underlying Powell’s algorithm is that once an optimum has been found in one direction, the chance for the biggest improvement lies in the direction perpendicular to that direction.
4+
5+
It has only one additional instance variable, unidimensionalFinder to hold the one-dimensional
6+
optimizer used to find an optimum of the goal function along a given direction.
7+
"
18
Class {
29
#name : #PMHillClimbingOptimizer,
310
#superclass : #PMFunctionOptimizer,
@@ -7,6 +14,21 @@ Class {
714
#category : #'Math-Numerical-Math-FunctionIterator'
815
}
916

17+
{ #category : #examples }
18+
PMHillClimbingOptimizer class >> exampleFindOptimumOnVectorFunction [
19+
| fBlock educatedGuess hillClimber result |
20+
fBlock := [ :v |
21+
| x y |
22+
x := v at: 1.
23+
y := v at: 2.
24+
((2 - x) * (2 - x)) negated + ((2 - y) * (2 - y)) negated ].
25+
educatedGuess := #(100 100) asPMVector.
26+
hillClimber := self maximizingFunction: fBlock.
27+
hillClimber initialValue: educatedGuess.
28+
result := hillClimber evaluate.
29+
^ result inspect
30+
]
31+
1032
{ #category : #initialization }
1133
PMHillClimbingOptimizer >> computeInitialValues [
1234
unidimensionalFinder := PMOneVariableFunctionOptimizer forOptimizer: self.
@@ -47,7 +69,6 @@ PMHillClimbingOptimizer >> finalizeIterations [
4769

4870
{ #category : #operation }
4971
PMHillClimbingOptimizer >> minimizeDirection: aVectorFunction [
50-
"Private"
5172
^unidimensionalFinder
5273
reset;
5374
setFunction: aVectorFunction;
@@ -59,7 +80,6 @@ PMHillClimbingOptimizer >> minimizeDirection: aVectorFunction [
5980

6081
{ #category : #operation }
6182
PMHillClimbingOptimizer >> minimizeDirection: aVectorFunction from: aVector [
62-
"Private"
6383

6484
^aVectorFunction
6585
origin: aVector;
@@ -68,19 +88,18 @@ PMHillClimbingOptimizer >> minimizeDirection: aVectorFunction from: aVector [
6888

6989
{ #category : #operation }
7090
PMHillClimbingOptimizer >> shiftDirections [
71-
"Private"
7291
| position delta firstDirection |
7392
firstDirection := bestPoints first direction.
74-
bestPoints inject: nil
75-
into: [ :prev :each |
76-
position isNil
77-
ifTrue: [ position := each origin]
78-
ifFalse:[ prev direction: each direction].
79-
each
80-
].
93+
bestPoints
94+
inject: nil
95+
into: [ :prev :each |
96+
position
97+
ifNil: [ position := each origin ]
98+
ifNotNil: [ prev direction: each direction ].
99+
each ].
81100
position := bestPoints last origin - position.
82101
delta := position norm.
83102
delta > desiredPrecision
84-
ifTrue: [ bestPoints last direction: (position scaleBy: (1 / delta))]
85-
ifFalse:[ bestPoints last direction: firstDirection]
103+
ifTrue: [ bestPoints last direction: (position scaleBy: 1 / delta) ]
104+
ifFalse: [ bestPoints last direction: firstDirection ]
86105
]

0 commit comments

Comments
 (0)