Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class SimpleHillClimbing {
- private Problem problem;
- private SimpleHillClimbing(Problem problem) {
- this.problem = problem;
- }
- //Simple transformation
- private ArrayList<Double> findNewNeighbours(ArrayList<Double> parameters, double stepSize) {
- ArrayList<Double> newNeighbours = new ArrayList<>();
- for (int i = 0; i < parameters.size(); i++) {
- int randomInt = (int) (2 * Math.random());
- if (randomInt == 1)
- newNeighbours.add(parameters.get(i) + stepSize);
- else
- newNeighbours.add(parameters.get(i) - stepSize);
- if (newNeighbours.get(i) > problem.getMaxValues().get(i))
- newNeighbours.set(i, problem.getMaxValues().get(i));
- else if (newNeighbours.get(i) < problem.getMinValues().get(i))
- newNeighbours.set(i, problem.getMinValues().get(i));
- }
- return newNeighbours;
- }
- private ArrayList<Double> findOptima(int iterations, int neighbours, double stepSize) {
- ArrayList<Double> bestGlobal = new ArrayList<>();
- for (int i = 0; i < iterations; i++) {
- ArrayList<Double> bestPoint = createInitPoints();
- double bestPointEval = problem.Eval(bestPoint);
- boolean shouldContinue = true;
- while (shouldContinue) {
- ArrayList<Double> newPoint = findNewNeighbours(bestPoint, stepSize);
- double newPointEval = problem.Eval(newPoint);
- for (int nb = 0; nb < neighbours - 1; nb++) {
- ArrayList<Double> newNeighbour = findNewNeighbours(bestPoint, stepSize);
- if (problem.Eval(newNeighbour) > newPointEval)
- newPoint = newNeighbour;
- }
- if (bestPointEval < newPointEval) {
- bestPointEval = newPointEval;
- bestPoint = newPoint;
- } else {
- shouldContinue = false;
- }
- }
- // System.out.println("-------------Global");
- // System.out.println(bestGlobal);
- // if(bestGlobal.size() != 0)
- // System.out.println(problem.Eval(bestGlobal));
- // System.out.println("-------------Iteration");
- // System.out.println(bestPoint);
- // System.out.println(bestPointEval);
- if (bestGlobal.size() == 0 || problem.Eval(bestGlobal) < bestPointEval) {
- bestGlobal = bestPoint;
- }
- //System.out.println(problem.Eval(randomInitPoints));
- }
- System.out.print("(best eval: " + problem.Eval(bestGlobal) + "");
- return bestGlobal;
- }
- private ArrayList<Double> createInitPoints() {
- ArrayList<Double> initPoint = new ArrayList<>();
- for (int dim = 0; dim < problem.getDimensions(); dim++) {
- initPoint.add(problem.getMinValues().get(dim) + Math.random() * (problem.getMaxValues().get(dim) - problem.getMinValues().get(dim)));
- }
- return initPoint;
- }
- public static void main(String[] args) {
- //Initial setup
- SimpleHillClimbing climber = new SimpleHillClimbing(new P1());
- int iterations = 10;
- int neighbours = 1;
- double stepsize = 0.01;
- //----------------------------------P2------------------------------------------------------//
- System.out.println("--> P1");
- System.out.println("-> Iteration Increase");
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- iterations = 100;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- System.out.println();
- iterations = 1;
- System.out.println("-> Neighbour Increase");
- neighbours = 10;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- neighbours = 100;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- System.out.println();
- neighbours = 1;
- System.out.println("-> Iteration Increase");
- stepsize = 0.1;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- stepsize = 0.01;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- System.out.println();
- //----------------------------------P2------------------------------------------------------//
- climber = new SimpleHillClimbing(new P2());
- System.out.println("--> P2");
- System.out.println("-> Iteration Increase");
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- iterations = 100;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- System.out.println();
- iterations = 1;
- System.out.println("-> Neighbour Increase");
- neighbours = 10;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- neighbours = 100;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- System.out.println();
- neighbours = 1;
- System.out.println("-> Iteration Increase");
- stepsize = 0.1;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- stepsize = 0.01;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- System.out.println();
- //----------------------------------P2------------------------------------------------------//
- climber = new SimpleHillClimbing(new RevAckley());
- System.out.println("--> RevAckley");
- System.out.println("-> Iteration Increase");
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- iterations = 100;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- iterations = 200;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- System.out.println();
- iterations = 1;
- System.out.println("-> Neighbour Increase");
- neighbours = 10;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- neighbours = 100;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- neighbours = 200;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- System.out.println();
- neighbours = 1;
- System.out.println("-> Iteration Increase");
- stepsize = 0.1;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- stepsize = 0.01;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- stepsize = 0.001;
- System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement