Hill Climbing

* We start at a given x position.
* We keep comparing the adjacent elements and try to find the maximum among them.
* Ultimately we might halt at a local minimum/maximum.
* In order to find the global optimum we should start the algorithm again and again several times (Repeated local search)

Codes of the Java files included in the program are given below:

HillClimbing.java

In [ ]:
package hillclimbing;

/**
 *
 * @author ANIRUDDHA
 */
public class HillClimbing {

    // The function whose max/min is to be found
    public static double func(double x){
        return -(x-1)*(x-1)+2;
    }
    
    public static void main(String[] args) {
        
        double dx = 0.05; //Intervals
        //double dx = 0.01;
        double actualPointX = -2;
        double max = func(actualPointX);
        
        // Keep iterating or climbing up the slope while slope is +ve ie we find the local max
        while(func(actualPointX + dx) > max){
            max = func(actualPointX + dx); //Update the max
            System.out.println("x: "+actualPointX + " y: "+func(actualPointX)); //Print out the points
            actualPointX += dx;
        }
        
        System.out.println("Local Maximum is at "+max);
        
    }
    
}

Output of the program is :

x: -2.0 y: -7.0
x: -1.95 y: -6.702500000000001
x: -1.9 y: -6.41
x: -1.8499999999999999 y: -6.122499999999999
x: -1.7999999999999998 y: -5.839999999999999
x: -1.7499999999999998 y: -5.5625
x: -1.6999999999999997 y: -5.289999999999998
x: -1.6499999999999997 y: -5.022499999999997
x: -1.5999999999999996 y: -4.759999999999998
x: -1.5499999999999996 y: -4.5024999999999995
x: -1.4999999999999996 y: -4.249999999999998
x: -1.4499999999999995 y: -4.002499999999997
x: -1.3999999999999995 y: -3.759999999999997
x: -1.3499999999999994 y: -3.522499999999998
x: -1.2999999999999994 y: -3.2899999999999974
x: -1.2499999999999993 y: -3.0624999999999964
x: -1.1999999999999993 y: -2.839999999999997
x: -1.1499999999999992 y: -2.622499999999998
x: -1.0999999999999992 y: -2.4099999999999966
x: -1.0499999999999992 y: -2.2024999999999952
x: -0.9999999999999991 y: -1.9999999999999964
x: -0.9499999999999991 y: -1.8024999999999962
x: -0.899999999999999 y: -1.6099999999999963
x: -0.849999999999999 y: -1.4224999999999963
x: -0.7999999999999989 y: -1.2399999999999962
x: -0.7499999999999989 y: -1.062499999999996
x: -0.6999999999999988 y: -0.8899999999999961
x: -0.6499999999999988 y: -0.7224999999999961
x: -0.5999999999999988 y: -0.5599999999999961
x: -0.5499999999999987 y: -0.40249999999999586
x: -0.4999999999999987 y: -0.249999999999996
x: -0.44999999999999873 y: -0.10249999999999648
x: -0.39999999999999875 y: 0.040000000000003366
x: -0.34999999999999876 y: 0.17750000000000332
x: -0.29999999999999877 y: 0.3100000000000034
x: -0.24999999999999878 y: 0.43750000000000333
x: -0.1999999999999988 y: 0.5600000000000027
x: -0.1499999999999988 y: 0.6775000000000027
x: -0.0999999999999988 y: 0.7900000000000027
x: -0.049999999999998795 y: 0.8975000000000026
x: 1.2073675392798577E-15 y: 1.0000000000000024
x: 0.05000000000000121 y: 1.0975000000000021
x: 0.10000000000000121 y: 1.1900000000000022
x: 0.15000000000000122 y: 1.277500000000002
x: 0.20000000000000123 y: 1.360000000000002
x: 0.2500000000000012 y: 1.4375000000000018
x: 0.3000000000000012 y: 1.5100000000000016
x: 0.3500000000000012 y: 1.5775000000000015
x: 0.4000000000000012 y: 1.6400000000000015
x: 0.4500000000000012 y: 1.6975000000000013
x: 0.5000000000000012 y: 1.7500000000000013
x: 0.5500000000000013 y: 1.7975000000000012
x: 0.6000000000000013 y: 1.840000000000001
x: 0.6500000000000014 y: 1.8775000000000008
x: 0.7000000000000014 y: 1.9100000000000008
x: 0.7500000000000014 y: 1.9375000000000007
x: 0.8000000000000015 y: 1.9600000000000006
x: 0.8500000000000015 y: 1.9775000000000005
x: 0.9000000000000016 y: 1.9900000000000002
x: 0.9500000000000016 y: 1.9975
Local Maximum is at 2.0

You can download the complete source code here : Source Code