Skip to content

Commit

Permalink
Implementation of Depth Limited Search citiususc#157
Browse files Browse the repository at this point in the history
Updated DLS unit tests

Fix for Improve GraphBuilder to generate graphs without edge values citiususc#154

- created function in GraphBuilder called connect(vertex , vertex ) which connects two vertices with an empty valued edge.

Add license agreement header

Remove unnecessary code

Fix test for GraphBuilder edge testing
  • Loading branch information
gabizekany committed Dec 24, 2015
1 parent 185ed17 commit 835e451
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package es.usc.citius.hipster.algorithm;

import es.usc.citius.hipster.model.Node;
import es.usc.citius.hipster.model.function.NodeExpander;
import es.usc.citius.hipster.model.impl.UnweightedNode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
/**
* Copyright 2015 Centro de Investigación en Tecnoloxías da Información (CITIUS),
* University of Santiago de Compostela (USC).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* <p>
* In computer science maximumDepth-limited search is an algorithm to explore the vertices of a graph.
* It is a modification of maximumDepth-first search and is used for example in the iterative deepening
* maximumDepth-first search algorithm.
* </p>
*
* For more information see <a href="http://en.wikipedia.org/wiki/Depth-limited_search">this article of the Wikipedia</a> about DLS.
*
* @author Gabriella Zekany
*/
public class DepthLimitedSearch <A,S,N extends Node<A,S,N>> extends Algorithm<A,S,N> {
protected N initialNode;
protected N finalNode;
protected NodeExpander nodeExpander;
protected int maximumDepth;
protected int currentDepth;
protected ArrayList<S> path;

public DepthLimitedSearch(N initialNode, N finalNode, NodeExpander nodeExpander, int maximumDepth) {
this.initialNode = initialNode;
this.finalNode = finalNode;
this.nodeExpander = nodeExpander;
this.maximumDepth = maximumDepth;
this.currentDepth = 0;
this.path = new ArrayList<>();
}

public int getMaximumDepth() {
return this.maximumDepth;
}

public int getCurrentDepth() {
return this.currentDepth;
}

public ArrayList<S> getPath() {
return path;
}

public void incrementCurrentDepth() {
this.currentDepth ++;
}

public boolean execute() {
Stack<StackNode> nodeStack = new Stack();
StackNode tempStackNode = new StackNode(this.initialNode);
nodeStack.add(tempStackNode);

while(!nodeStack.isEmpty()) {
if(this.currentDepth <= this.maximumDepth) {
StackNode temp = nodeStack.pop();
if(!path.contains(temp.getNode()) && ((UnweightedNode) temp.getNode()).state().equals(((UnweightedNode)this.finalNode).state())){
this.path.add((S) temp.getNode().state());
return true;
} else {
this.path.add((S) temp.getNode().state());
for(StackNode child : temp.getChildren()) {
if(!this.path.contains(child.getNode().state())) {
nodeStack.add(child);
}
}
this.incrementCurrentDepth();
}
} else {
return false;
}
}
return false;
}

private class StackNode {
private N node;
private java.util.Iterator<N> children;

public StackNode(N node) {
this.node = node;
this.children = nodeExpander.expand(node).iterator();
}

public N getNode() {
return node;
}

public void setNode(N node) {
this.node = node;
}

public List<StackNode> getChildren() {
ArrayList<StackNode> result = new ArrayList<>();
while(this.children.hasNext()) {
StackNode temp = new StackNode(this.children.next());
result.add(temp);
}
return result;
}

public void setChildren(java.util.Iterator<N> children) {
this.children = children;
}
}

@Override
public Iterator<N> iterator() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ public static <A,S,N extends Node<A,S,N>> DepthFirstSearch<A,S,N> createDepthFir
return new DepthFirstSearch<A, S, N>(components.getInitialNode(), components.getExpander());
}

/**
* Instantiates Depth Limited Search algorithm for a problem definition.
*
* @param components search problem definition with the components of the algorithm
* @param <A> type of the actions
* @param <S> type of the states
* @param <N> type of the nodes
* @return instance of {@link es.usc.citius.hipster.algorithm.DepthFirstSearch} for the problem definition
*/
public static <A,S,N extends Node<A,S,N>> DepthLimitedSearch<A,S,N> createDepthLimitedSearch(SearchProblem<A,S,N> components, int depth){
return new DepthLimitedSearch<A, S, N>(components.getInitialNode(), components.getFinalNode(), components.getExpander(), depth);
}

/**
* Instantiates a IDA* algorithm given a problem definition.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,48 @@ private Connection(V vertex1, V vertex2, E edge) {
this.vertex2 = vertex2;
this.edge = edge;
}

private Connection(V vertex1, V vertex2) {
this.vertex1 = vertex1;
this.vertex2 = vertex2;
this.edge = (E) new Object();
}

public V getVertex1() {
return vertex1;
}

public void setVertex1(V vertex1) {
this.vertex1 = vertex1;
}

public V getVertex2() {
return vertex2;
}

public void setVertex2(V vertex2) {
this.vertex2 = vertex2;
}

public E getEdge() {
return edge;
}

public void setEdge(E edge) {
this.edge = edge;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Connection that = (Connection) o;

if (!vertex1.equals(that.vertex1)) return false;
return vertex2.equals(that.vertex2);

}
}

private List<Connection> connections = new LinkedList<Connection>();
Expand All @@ -54,11 +96,16 @@ public static <V, E> GraphBuilder<V,E> create() {
return new GraphBuilder<V, E>();
}


public Vertex1 connect(V vertex) {
return new Vertex1(vertex);
}

public GraphBuilder<V, E> connect(V vertex1, V vertex2) {
Vertex1 vertex = new Vertex1(vertex1);
vertex.to(vertex2);
return this;
}

public HipsterDirectedGraph<V,E> createDirectedGraph() {
HashBasedHipsterDirectedGraph<V, E> graph = HashBasedHipsterDirectedGraph.create();
for (Connection c : connections) {
Expand Down Expand Up @@ -114,10 +161,18 @@ public class Vertex2 {

private Vertex2(V vertex) {
this.vertex2 = vertex;
connections.add(new Connection(vertex1, vertex2));
}

public GraphBuilder<V, E> withEdge(E edge) {
connections.add(new Connection(vertex1, vertex2, edge));
Connection connection = new Connection(vertex1, vertex2);
int connectionIndex = connections.indexOf(connection);
if(connectionIndex != -1 ) {
connections.get(connectionIndex).setEdge(edge);
} else {
connection.setEdge(edge);
connections.add(connection);
}
return GraphBuilder.this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public C evaluate(Transition<E, V> transition) {

public SearchProblem<E, V, UnweightedNode<E, V>> build() {
return ProblemBuilder.create()
.initialState(fromVertex)
.initialState(fromVertex, toVertex)
.defineProblemWithExplicitActions()
.useTransitionFunction(tf)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class HashBasedHipsterDirectedGraph<V, E> extends HashBasedHipsterGraph<V


@Override
protected GraphEdge<V, E> buildEdge(V v1, V v2, E value) {
public GraphEdge<V, E> buildEdge(V v1, V v2, E value) {
return new DirectedEdge<V, E>(v1, v2, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public GraphEdge<V,E> connect(V v1, V v2, E value){
return edge;
}

protected GraphEdge<V,E> buildEdge(V v1, V v2, E value){
public GraphEdge<V,E> buildEdge(V v1, V v2, E value){
return new UndirectedEdge<V, E>(v1, v2, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ private Wizard(){}
*/
public static final class ActionState<S> {
private final S initialState;
private final S finalState;

public ActionState(S initialState) {
this.initialState = initialState;
this.finalState = null;
}

public ActionState(S initialState, S finalState) {
this.initialState = initialState;
this.finalState = finalState;
}

/**
Expand Down Expand Up @@ -175,8 +182,9 @@ public UnweightedNode<A, S> makeNode(UnweightedNode<A, S> fromNode, Transition<A
}
};
UnweightedNode<A,S> initialNode = factory.makeNode(null, Transition.<A, S>create(null, null, initialState));
UnweightedNode<A,S> finalNode = factory.makeNode(null, Transition.<A, S>create(null, null, finalState));
NodeExpander<A,S,UnweightedNode<A,S>> nodeExpander = new LazyNodeExpander<A, S, UnweightedNode<A, S>>(tf, factory);
return new SearchProblem<A,S, UnweightedNode<A,S>>(initialNode, nodeExpander);
return new SearchProblem<A,S, UnweightedNode<A,S>>(initialNode, finalNode, nodeExpander);
}

/**
Expand Down Expand Up @@ -255,6 +263,10 @@ public <S> ActionState<S> initialState(S initialState){
return new ActionState<S>(initialState);
}

public <S> ActionState<S> initialState(S initialState, S finalState){
return new ActionState<S>(initialState, finalState);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
*/
public class SearchProblem<A,S,N extends Node<A,S,N>> {
private N initialNode;
private N finalNode;
private NodeExpander<A,S,N> expander;

public SearchProblem(N initialNode, NodeExpander<A, S, N> expander) {
this.initialNode = initialNode;
this.finalNode = null;
this.expander = expander;
}

public SearchProblem(N initialNode, N finalNode, NodeExpander<A, S, N> expander) {
this.initialNode = initialNode;
this.finalNode = finalNode;
this.expander = expander;
}

Expand All @@ -26,4 +34,12 @@ public N getInitialNode() {
public NodeExpander<A, S, N> getExpander() {
return expander;
}

public N getFinalNode() {
return finalNode;
}

public void setFinalNode(N finalNode) {
this.finalNode = finalNode;
}
}
Loading

0 comments on commit 835e451

Please sign in to comment.