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}}==
{{Trans|Go}}
{{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
{{Trans|FORTRAN}}
but agree with the Fortran sample and the Julia sample to 6 places.
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, h );
[ 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;
h /:= 2;
# Calculate next gradient. #
# Calculate next gradient. #
fi := grad g( x, h );
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 );
b := alpha / del g;
IF del g > 0 THEN
# Calculate next value. #
b := alpha / del g;
LONG REAL g1 := g( x );
# Calculate next value. #
# Adjust parameter. #
LONG REAL g1 := g( x );
IF g1 > g0 THEN
# Adjust parameter. #
alpha /:= 2
IF g1 > g0 THEN
ELSE
alpha /:= 2
g0 := g1
ELSE
g0 := g1
FI
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 ): #
PROC grad g = ( []LONG REAL x, LONG REAL h )[]LONG REAL:
# 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 #
PROC grad g = ( []LONG REAL p )[]LONG REAL:
BEGIN
BEGIN
[ LWB x : UPB x ]LONG REAL z;
[ LWB p : UPB p ]LONG REAL z;
[ LWB x : UPB x ]LONG REAL y := x;
LONG REAL x = p[ 0 ];
LONG REAL g0 := g( x );
LONG REAL y = p[ 1 ];
FOR i FROM LWB x TO UPB x DO
z[ 0 ] := 2 * ( x - 1 ) * long exp( - ( y * y ) )
y[ i ] +:= h;
- 4 * x * long exp( -2 * ( x * x ) ) * y * ( y + 2 );
z[ i ] := ( g( y ) - g0 ) / h
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 )