From 0a48cad1c3a034ad1a48e811649148f4670d8f0c Mon Sep 17 00:00:00 2001 From: pazis Date: Mon, 23 Mar 2020 02:05:05 +0000 Subject: [PATCH] BAEL-3881 --- algorithms-miscellaneous-6/pom.xml | 15 +++++++++ .../gradientdescent/GradientDescent.java | 33 +++++++++++++++++++ .../GradientDescentUnitTest.java | 20 +++++++++++ pom.xml | 2 ++ 4 files changed, 70 insertions(+) create mode 100644 algorithms-miscellaneous-6/pom.xml create mode 100644 algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/gradientdescent/GradientDescent.java create mode 100644 algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/gradientdescent/GradientDescentUnitTest.java diff --git a/algorithms-miscellaneous-6/pom.xml b/algorithms-miscellaneous-6/pom.xml new file mode 100644 index 0000000000..5daa000ea3 --- /dev/null +++ b/algorithms-miscellaneous-6/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + algorithms-miscellaneous-6 + 0.0.1-SNAPSHOT + algorithms-miscellaneous-6 + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + diff --git a/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/gradientdescent/GradientDescent.java b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/gradientdescent/GradientDescent.java new file mode 100644 index 0000000000..c5db3a4a06 --- /dev/null +++ b/algorithms-miscellaneous-6/src/main/java/com/baeldung/algorithms/gradientdescent/GradientDescent.java @@ -0,0 +1,33 @@ +package com.baeldung.algorithms.gradientdescent; + +import java.util.function.Function; + +public class GradientDescent { + + private final double precision = 0.000001; + + public double findLocalMinimum(Function f, double initialX) { + double stepCoefficient = 0.1; + double previousStep = 1.0; + double currentX = initialX; + double previousX = initialX; + double previousY = f.apply(previousX); + int iter = 100; + + currentX += stepCoefficient * previousY; + + while (previousStep > precision && iter > 0) { + iter--; + double currentY = f.apply(currentX); + if (currentY > previousY) { + stepCoefficient = -stepCoefficient / 2; + } + previousX = currentX; + currentX += stepCoefficient * previousY; + previousY = currentY; + previousStep = StrictMath.abs(currentX - previousX); + } + return currentX; + } + +} diff --git a/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/gradientdescent/GradientDescentUnitTest.java b/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/gradientdescent/GradientDescentUnitTest.java new file mode 100644 index 0000000000..34d3e188f2 --- /dev/null +++ b/algorithms-miscellaneous-6/src/test/java/com/baeldung/algorithms/gradientdescent/GradientDescentUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.algorithms.gradientdescent; + +import static org.junit.Assert.assertTrue; + +import java.util.function.Function; + +import org.junit.Test; + +public class GradientDescentUnitTest { + + @Test + public void givenFunction_whenStartingPointIsOne_thenLocalMinimumIsFound() { + Function df = x -> + StrictMath.abs(StrictMath.pow(x, 3)) - (3 * StrictMath.pow(x, 2)) + x; + GradientDescent gd = new GradientDescent(); + double res = gd.findLocalMinimum(df, 1); + assertTrue(res > 1.78); + assertTrue(res < 1.84); + } +} diff --git a/pom.xml b/pom.xml index 15331d8c95..45bdf9747e 100644 --- a/pom.xml +++ b/pom.xml @@ -342,6 +342,7 @@ algorithms-miscellaneous-3 algorithms-miscellaneous-4 algorithms-miscellaneous-5 + algorithms-miscellaneous-6 algorithms-searching algorithms-sorting algorithms-sorting-2 @@ -853,6 +854,7 @@ algorithms-miscellaneous-3 algorithms-miscellaneous-4 algorithms-miscellaneous-5 + algorithms-miscellaneous-6 algorithms-searching algorithms-sorting algorithms-sorting-2