Gradient descent: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Use the actual gradient.) |
|||
Line 14: | Line 14: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
⚫ | |||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
||
{{Trans|Go}} modified to use the actual gradient function - |
|||
There appear to be a range of answers produced by the various samples... the results calculated by this sample differ from the Go sample |
|||
⚫ | |||
THe results agree with the Fortran sample and the Julia sample to 6 places. |
|||
<lang algol68>PROC steepest descent = ( REF[]LONG REAL x, LONG REAL alphap, tolerance )VOID: |
<lang algol68>PROC steepest descent = ( REF[]LONG REAL x, LONG REAL alphap, tolerance )VOID: |
||
BEGIN |
BEGIN |
||
LONG REAL alpha := alphap; |
LONG REAL alpha := alphap; |
||
LONG REAL h := tolerance; |
|||
LONG REAL g0 := g( x ); # Initial estimate of result. # |
LONG REAL g0 := g( x ); # Initial estimate of result. # |
||
# Calculate initial gradient. # |
# Calculate initial gradient. # |
||
[ LWB x : UPB x ]LONG REAL fi := grad g( x |
[ LWB x : UPB x ]LONG REAL fi := grad g( x ); |
||
# Calculate initial norm. # |
# Calculate initial norm. # |
||
LONG REAL del g := 0.0; |
LONG REAL del g := 0.0; |
||
Line 38: | Line 37: | ||
x[i] -:= b * fi[i] |
x[i] -:= b * fi[i] |
||
OD; |
OD; |
||
⚫ | |||
# Calculate next gradient. # |
# Calculate next gradient. # |
||
fi := grad g( x |
fi := grad g( x ); |
||
# Calculate next norm. # |
# Calculate next norm. # |
||
del g := 0; |
del g := 0; |
||
Line 47: | Line 45: | ||
OD; |
OD; |
||
del g := long sqrt( del g ); |
del g := long sqrt( del g ); |
||
IF del g > 0 THEN |
|||
b := alpha / del g; |
|||
# Calculate next value. # |
|||
LONG REAL g1 := g( x ); |
|||
# Adjust parameter. # |
|||
IF g1 > g0 THEN |
|||
alpha /:= 2 |
|||
ELSE |
|||
g0 := g1 |
|||
⚫ | |||
FI |
FI |
||
OD |
OD |
||
END # steepest descent # ; |
END # steepest descent # ; |
||
# calculates the gradient of g(p). # |
|||
# Provides a rough calculation of gradient g(x). # |
|||
# The derivitives wrt x and y are (as in the Fortran sample ): # |
|||
⚫ | |||
# g' wrt x = 2( x - 1 )e^( - ( y^2 ) ) - 4xe^( -2( x^2) )y( y + 2 ) # |
|||
# g' wrt y = ( -2( x-1 )^2ye^( - (y^2) )) + e^(-2( x^2 ) )( y + 2 ) + e^( -2( x^2 ) )y # |
|||
⚫ | |||
BEGIN |
BEGIN |
||
[ LWB |
[ LWB p : UPB p ]LONG REAL z; |
||
LONG REAL x = p[ 0 ]; |
|||
LONG REAL |
LONG REAL y = p[ 1 ]; |
||
z[ 0 ] := 2 * ( x - 1 ) * long exp( - ( y * y ) ) |
|||
y |
- 4 * x * long exp( -2 * ( x * x ) ) * y * ( y + 2 ); |
||
z[ 1 ] := ( -2 * ( x - 1 ) * ( x - 1 ) * y ) * long exp( - y * y ) |
|||
+ long exp( -2 * x * x ) * ( y + 2 ) |
|||
OD; |
|||
+ long exp( -2 * x * x ) * y; |
|||
z |
z |
||
END # grad g # ; |
END # grad g # ; |
||
# Function for which minimum is to be found. # |
# Function for which minimum is to be found. # |
||
# g( x, y ) = ( ( x - 1 )^2 )e^( - ( x^2 ) ) + y( y + 2 )e^( - 2(x^2)) # |
|||
PROC g = ( []LONG REAL x )LONG REAL: |
PROC g = ( []LONG REAL x )LONG REAL: |
||
( x[ 0 ] - 1 ) |
( x[ 0 ] - 1 ) |