Skip to content

Commit

Permalink
Metrics of local search algorithms extended (with value of curr node).
Browse files Browse the repository at this point in the history
  • Loading branch information
ruediger.lunde@gmail.com committed May 14, 2016
1 parent 505b493 commit 9a82667
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
Expand Up @@ -44,6 +44,7 @@ public enum SearchOutcome {
}; };


public static final String METRIC_NODES_EXPANDED = "nodesExpanded"; public static final String METRIC_NODES_EXPANDED = "nodesExpanded";
public static final String METRIC_NODE_VALUE = "nodeValue";


private HeuristicFunction hf = null; private HeuristicFunction hf = null;
private SearchOutcome outcome = SearchOutcome.FAILURE; private SearchOutcome outcome = SearchOutcome.FAILURE;
Expand Down Expand Up @@ -77,23 +78,22 @@ public HillClimbingSearch(HeuristicFunction hf) {
public List<Action> search(Problem p) { public List<Action> search(Problem p) {
clearInstrumentation(); clearInstrumentation();
outcome = SearchOutcome.FAILURE; outcome = SearchOutcome.FAILURE;
lastState = null;
// current <- MAKE-NODE(problem.INITIAL-STATE) // current <- MAKE-NODE(problem.INITIAL-STATE)
Node current = new Node(p.getInitialState()); Node current = new Node(p.getInitialState());
Node neighbor = null; Node neighbor = null;
// loop do // loop do
while (!CancelableThread.currIsCanceled()) { while (!CancelableThread.currIsCanceled()) {
lastState = current.getState(); lastState = current.getState();
metrics.set(METRIC_NODE_VALUE, getValue(current));
metrics.incrementInt(METRIC_NODES_EXPANDED); metrics.incrementInt(METRIC_NODES_EXPANDED);
List<Node> children = SearchUtils.expandNode(current, p); List<Node> children = SearchUtils.expandNode(current, p);
// neighbor <- a highest-valued successor of current // neighbor <- a highest-valued successor of current
neighbor = getHighestValuedNodeFrom(children, p); neighbor = getHighestValuedNodeFrom(children, p);


// if neighbor.VALUE <= current.VALUE then return current.STATE // if neighbor.VALUE <= current.VALUE then return current.STATE
if ((neighbor == null) || (getValue(neighbor) <= getValue(current))) { if ((neighbor == null) || (getValue(neighbor) <= getValue(current))) {
if (SearchUtils.isGoalState(p, current)) { if (SearchUtils.isGoalState(p, current))
outcome = SearchOutcome.SOLUTION_FOUND; outcome = SearchOutcome.SOLUTION_FOUND;
}
return SearchUtils.getSequenceOfActions(current); return SearchUtils.getSequenceOfActions(current);
} }
// current <- neighbor // current <- neighbor
Expand Down Expand Up @@ -136,6 +136,7 @@ public Metrics getMetrics() {
*/ */
public void clearInstrumentation() { public void clearInstrumentation() {
metrics.set(METRIC_NODES_EXPANDED, 0); metrics.set(METRIC_NODES_EXPANDED, 0);
metrics.set(METRIC_NODE_VALUE, 0);
} }


// //
Expand Down
Expand Up @@ -49,7 +49,8 @@ public enum SearchOutcome {
}; };


public static final String METRIC_NODES_EXPANDED = "nodesExpanded"; public static final String METRIC_NODES_EXPANDED = "nodesExpanded";
public static final String TEMPERATURE = "temp"; public static final String METRIC_TEMPERATURE = "temp";
public static final String METRIC_NODE_VALUE = "nodeValue";


private final HeuristicFunction hf; private final HeuristicFunction hf;
private final Scheduler scheduler; private final Scheduler scheduler;
Expand Down Expand Up @@ -92,7 +93,7 @@ public List<Action> search(Problem p) {
// current <- MAKE-NODE(problem.INITIAL-STATE) // current <- MAKE-NODE(problem.INITIAL-STATE)
Node current = new Node(p.getInitialState()); Node current = new Node(p.getInitialState());
Node next = null; Node next = null;
List<Action> ret = new ArrayList<Action>(); List<Action> result = new ArrayList<Action>();
// for t = 1 to INFINITY do // for t = 1 to INFINITY do
int timeStep = 0; int timeStep = 0;
while (!CancelableThread.currIsCanceled()) { while (!CancelableThread.currIsCanceled()) {
Expand All @@ -104,24 +105,24 @@ public List<Action> search(Problem p) {
if (temperature == 0.0) { if (temperature == 0.0) {
if (SearchUtils.isGoalState(p, current)) if (SearchUtils.isGoalState(p, current))
outcome = SearchOutcome.SOLUTION_FOUND; outcome = SearchOutcome.SOLUTION_FOUND;
ret = SearchUtils.getSequenceOfActions(current); result = SearchUtils.getSequenceOfActions(current);
break; break;
} }


updateMetrics(temperature); updateMetrics(temperature, getValue(current));
List<Node> children = SearchUtils.expandNode(current, p); List<Node> children = SearchUtils.expandNode(current, p);
if (children.size() > 0) { if (children.size() > 0) {
// next <- a randomly selected successor of current // next <- a randomly selected successor of current
next = Util.selectRandomlyFromList(children); next = Util.selectRandomlyFromList(children);
// /\E <- next.VALUE - current.value // /\E <- next.VALUE - current.value
double deltaE = getValue(p, next) - getValue(p, current); double deltaE = getValue(next) - getValue(current);


if (shouldAccept(temperature, deltaE)) { if (shouldAccept(temperature, deltaE)) {
current = next; current = next;
} }
} }
} }
return ret; return result;
} }


/** /**
Expand Down Expand Up @@ -160,17 +161,19 @@ public Metrics getMetrics() {
return metrics; return metrics;
} }


private void updateMetrics(double temperature) { private void updateMetrics(double temperature, double value) {
metrics.incrementInt(METRIC_NODES_EXPANDED); metrics.incrementInt(METRIC_NODES_EXPANDED);
metrics.set(TEMPERATURE, temperature); metrics.set(METRIC_TEMPERATURE, temperature);
metrics.set(METRIC_NODE_VALUE, value);
} }


/** /**
* Sets all metrics to zero. * Sets all metrics to zero.
*/ */
public void clearInstrumentation() { public void clearInstrumentation() {
metrics.set(METRIC_NODES_EXPANDED, 0); metrics.set(METRIC_NODES_EXPANDED, 0);
metrics.set(TEMPERATURE, 0); metrics.set(METRIC_TEMPERATURE, 0);
metrics.set(METRIC_NODE_VALUE, 0);
} }


// //
Expand All @@ -185,7 +188,7 @@ private boolean shouldAccept(double temperature, double deltaE) {
temperature, deltaE)); temperature, deltaE));
} }


private double getValue(Problem p, Node n) { private double getValue(Node n) {
// assumption greater heuristic value => // assumption greater heuristic value =>
// HIGHER on hill; 0 == goal state; // HIGHER on hill; 0 == goal state;
// SA deals with gardient DESCENT // SA deals with gardient DESCENT
Expand Down

0 comments on commit 9a82667

Please sign in to comment.