File tree Expand file tree Collapse file tree
algorithms-miscellaneous-6
main/java/com/baeldung/algorithms/gradientdescent
test/java/com/baeldung/algorithms/gradientdescent Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
4+ <modelVersion >4.0.0</modelVersion >
5+ <artifactId >algorithms-miscellaneous-6</artifactId >
6+ <version >0.0.1-SNAPSHOT</version >
7+ <name >algorithms-miscellaneous-6</name >
8+
9+ <parent >
10+ <groupId >com.baeldung</groupId >
11+ <artifactId >parent-modules</artifactId >
12+ <version >1.0.0-SNAPSHOT</version >
13+ </parent >
14+
15+ </project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .algorithms .gradientdescent ;
2+
3+ import java .util .function .Function ;
4+
5+ public class GradientDescent {
6+
7+ private final double precision = 0.000001 ;
8+
9+ public double findLocalMinimum (Function <Double , Double > f , double initialX ) {
10+ double stepCoefficient = 0.1 ;
11+ double previousStep = 1.0 ;
12+ double currentX = initialX ;
13+ double previousX = initialX ;
14+ double previousY = f .apply (previousX );
15+ int iter = 100 ;
16+
17+ currentX += stepCoefficient * previousY ;
18+
19+ while (previousStep > precision && iter > 0 ) {
20+ iter --;
21+ double currentY = f .apply (currentX );
22+ if (currentY > previousY ) {
23+ stepCoefficient = -stepCoefficient / 2 ;
24+ }
25+ previousX = currentX ;
26+ currentX += stepCoefficient * previousY ;
27+ previousY = currentY ;
28+ previousStep = StrictMath .abs (currentX - previousX );
29+ }
30+ return currentX ;
31+ }
32+
33+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .algorithms .gradientdescent ;
2+
3+ import static org .junit .Assert .assertTrue ;
4+
5+ import java .util .function .Function ;
6+
7+ import org .junit .Test ;
8+
9+ public class GradientDescentUnitTest {
10+
11+ @ Test
12+ public void givenFunction_whenStartingPointIsOne_thenLocalMinimumIsFound () {
13+ Function <Double , Double > df = x ->
14+ StrictMath .abs (StrictMath .pow (x , 3 )) - (3 * StrictMath .pow (x , 2 )) + x ;
15+ GradientDescent gd = new GradientDescent ();
16+ double res = gd .findLocalMinimum (df , 1 );
17+ assertTrue (res > 1.78 );
18+ assertTrue (res < 1.84 );
19+ }
20+ }
Original file line number Diff line number Diff line change 342342 <module >algorithms-miscellaneous-3</module >
343343 <module >algorithms-miscellaneous-4</module >
344344 <module >algorithms-miscellaneous-5</module >
345+ <module >algorithms-miscellaneous-6</module >
345346 <module >algorithms-searching</module >
346347 <module >algorithms-sorting</module >
347348 <module >algorithms-sorting-2</module >
853854 <module >algorithms-miscellaneous-3</module >
854855 <module >algorithms-miscellaneous-4</module >
855856 <module >algorithms-miscellaneous-5</module >
857+ <module >algorithms-miscellaneous-6</module >
856858 <module >algorithms-searching</module >
857859 <module >algorithms-sorting</module >
858860 <module >algorithms-sorting-2</module >
You can’t perform that action at this time.
0 commit comments