Created
March 15, 2015 05:49
-
-
Save jlas/e6ec4b6ea85603044230 to your computer and use it in GitHub Desktop.
random restart in opt.RandomizedHillClimbing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package opt; | |
import shared.Instance; | |
/** | |
* A randomized hill climbing algorithm | |
* @author Andrew Guillory gtg008g@mail.gatech.edu | |
* @version 1.0 | |
*/ | |
public class RandomizedHillClimbing extends OptimizationAlgorithm { | |
/** | |
* The current optimization data | |
*/ | |
private Instance cur; | |
/** | |
* The current value of the data | |
*/ | |
private double curVal; | |
// Track the best instance / value seen | |
private Instance best; | |
private double bestVal; | |
// Max number of iterations to go without finding a better max | |
private int SINCE_NEW_MAX = 10; | |
// Keep track of iterations since we found a new max | |
private int sinceNewCount = 0; | |
/** | |
* Make a new randomized hill climbing | |
*/ | |
public RandomizedHillClimbing(HillClimbingProblem hcp) { | |
super(hcp); | |
cur = hcp.random(); | |
curVal = hcp.value(cur); | |
best = cur; | |
bestVal = curVal; | |
} | |
/** | |
* @see shared.Trainer#train() | |
*/ | |
public double train() { | |
HillClimbingProblem hcp = (HillClimbingProblem) getOptimizationProblem(); | |
if (sinceNewCount > SINCE_NEW_MAX) { | |
cur = hcp.random(); | |
curVal = hcp.value(cur); | |
sinceNewCount = 0; | |
} | |
Instance neigh = hcp.neighbor(cur); | |
double neighVal = hcp.value(neigh); | |
if (neighVal > curVal) { | |
curVal = neighVal; | |
cur = neigh; | |
sinceNewCount = 0; | |
} else { | |
sinceNewCount += 1; | |
} | |
if (curVal > bestVal) { | |
bestVal = curVal; | |
best = cur; | |
} | |
return bestVal; | |
} | |
/** | |
* @see opt.OptimizationAlgorithm#getOptimalData() | |
*/ | |
public Instance getOptimal() { | |
return cur; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment