10 70 Vestervoldgade 20 .public abstract class AbstractNode implements Node, Comparable {protected

download 10 70 Vestervoldgade 20 .public abstract class AbstractNode implements Node, Comparable {protected

of 13

  • date post

    16-Sep-2018
  • Category

    Documents

  • view

    216
  • download

    0

Embed Size (px)

Transcript of 10 70 Vestervoldgade 20 .public abstract class AbstractNode implements Node, Comparable {protected

  • 10 70 Vestervoldgade 20 50

  • () = () + ()

    = (2 1)2 + (2 1)2

  • Here is the route using the A* algorithm from node [35,80] to [45,70]:

    35,80 SktPedersStraede 50,90 [F: 38,64 | G: 18,03 | H: 20,62]

    50,90 LarslejStraede 35,120 [F: 102,56 | G: 51,57 | H: 50,99]

    35,120 Noerrevoldgade 25,100 [F: 109,98 | G: 73,93 | H: 36,06]

    25,100 Noerrevoldgade 10,70 [F: 142,47 | G: 107,47 | H: 35,00]

    10,70 Vestervoldgade 20,50 [F: 161,85 | G: 129,83 | H: 32,02]

    20,50 Studiestraede 45,70 [F: 161,85 | G: 161,85 | H: 0,00 ]

    Total distance of route = 161,85

  • a,~b,~c

    b,~b

    b,~c,~d

    c

    d

  • The resolution steps are:

    Resolution: ~a && (~b || ~c || a) resolves to (~b || ~c)

    F score: 0.0 | G score: 0.0 | H score: 0.0

    Resolution: (~b || ~c) && (~b || b) resolves to ~c

    F score: 3.0 | G score: 1.0 | H score: 2.0

    Resolution: ~c && c resolves to []

    F score: 4.0 | G score: 3.0 | H score: 1.0

  • public abstract class AbstractNode implements Node, Comparable

    {

    protected static int nextNumber = 1;

    protected int nodeNumber;

    protected double g_score; //Actual distance from START to this node

    protected double h_score; //Heuristic, Euclidian distance from this node

    to GOAL-node

    protected double f_score = g_score + h_score;

    protected AbstractNode cameFrom;

    protected ArrayList outgoingEdges = new

    ArrayList();

    protected boolean visited = false;

    public AbstractNode() {

    this.nodeNumber = nextNumber++;

    }

    public class AbstractEgde implements Comparable {

    private AbstractNode endNode;

    private String edgeName = "";

    public AbstractEgde(AbstractNode endNode, String name) {

    this.endNode = endNode;

    this.edgeName = name;

    }

    //Inspired from WiKipedias pseudocode algorithm

    public LinkedList aStar(AbstractNode startNode,

    AbstractNode goalNode) {

    // The set of tentative nodes to be evalueted, initially containing

    the start node

    LinkedList openSet = new LinkedList();

    openSet.add(startNode);

    double tentative_g_score = 0;

    AbstractNode currentNode;

    while (!openSet.isEmpty()) {

    // Sort openSet

    Collections.sort(openSet);

    currentNode = openSet.pop();

    currentNode.setVisited(true);

    if (currentNode.equals(goalNode)) {

    return reconstruct_path(startNode, currentNode);

    }

    ArrayList outgoingEdges =

    currentNode.getOutgoingEdges();

    Collections.sort(outgoingEdges);

  • for (AbstractEgde e : outgoingEdges) {

    AbstractNode neighbour = e.getEndNode();

    if (!neighbour.isVisited()) {

    tentative_g_score =

    currentNode.getG_score() + currentNode.estimateHeuristic(neighbour);

    boolean tentativeBool;

    if (!openSet.contains(neighbour)) {

    openSet.push(neighbour);

    neighbour.setH_score(neighbour.estimateHeuristic(goalNode));

    tentativeBool = true;

    } else if (tentative_g_score < neighbour.getG_score()) {

    tentativeBool = true;

    } else {

    tentativeBool = false;

    }

    if (tentativeBool) {

    neighbour.setCameFrom(currentNode);

    neighbour.setG_score(tentative_g_score);

    }

    }

    }

    }

    return null;

    }

    private LinkedList reconstruct_path(AbstractNode fromNode,

    AbstractNode toNode) {

    path.push(toNode);

    if (toNode.equals(fromNode)) {

    return path;

    } else {

    return reconstruct_path(fromNode, toNode.getCameFrom());

    }

    }

    }

    public double estimateHeuristic(Node other) {

    double pointX = point.getX();

    double pointY = point.getY();

    double otherX = ((MapNode) other).getPoint().getX();

    double otherY = ((MapNode) other).getPoint().getY();

    // dist = sqrt((x1-x2)^2 + (y1-y2)^2)

    return Math.sqrt((otherX - pointX)* (otherX - pointX) +

    (otherY - pointY)* (otherY - pointY));

    }

  • public void runIE() {

    InferenceEngine k = new InferenceEngine();

    k.buidKB(k.readFile());

    System.out.println(this);

    Clause start = new Clause(KB.clauses);

    start.setG_score(0);

    start.addLiteral(new Literal("~a"));

    System.out.println("Start: " + start);

    Clause goal = new Clause(new ArrayList());

    System.out.println("Goal: " + goal);

    System.out.println("#######################################");

    Search s = new Search();

    LinkedList path = s.aStar(start, goal);

    System.out.println("#######################################");

    System.out.println("\nThe resolution steps are:");

    System.out.println("___________________________________________________");

    for (int h = 0; h < path.size() - 1; h++) {

    Clause node = (Clause) path.get(h);

    Clause nextNode = (Clause) path.get(h + 1);

    AbstractEgde arc = null;

    for (AbstractEgde a : node.getOutgoingEdges()) {

    if (a.getEndNode().equals(nextNode)) {

    arc = a;

    System.out.println("F score: " +node.getF_score());

    }

    }

    String s2 = "Resolution: " + node + " && " + arc.getName()

    + " resolves to " + nextNode;

    System.out.println(s2);

    }

    System.out.println("___________________________________________________\n");

    }

  • public class Clause extends AbstractNode implements Node {

    private HashSet litts = new HashSet();

    ArrayList outGoingEdges;

    private ArrayList orgKB;

    private ArrayList childKB = new ArrayList();

    public Clause(ArrayList localKB) {

    super();

    this.orgKB = localKB;

    childKB.addAll(localKB);

    childKB.add(this);

    }

    public void addLiteral(Literal l) {

    litts.add(l);

    }

    public void setLocalKB(ArrayList localKB) {

    this.orgKB = localKB;

    }

    @Override

    public double estimateHeuristic(Node other) {

    return litts.size();

    }

    @Override

    public ArrayList getOutgoingEdges() {

    if (outGoingEdges == null) {

    outGoingEdges = new ArrayList();

    for (Clause other : orgKB) {

    Clause result = proResolution(other);

    if (result != null) {

    AbstractEgde a = new AbstractEgde(result,

    other.toString());

    outGoingEdges.add(a);

    }

    }

    }

    return outGoingEdges;

    }

  • public Clause proResolution(Clause other) {

    HashSet tempLiterals = new HashSet();

    HashSet finalLiterals = new HashSet();

    tempLiterals.addAll(this.litts);

    tempLiterals.addAll(other.litts);

    for (Literal l : tempLiterals) {

    Literal l2 = new Literal(l.getName());

    l2.setNegated(!l.isNegated());

    if (!tempLiterals.contains(l2)) {

    finalLiterals.add(l);

    }

    }

    Clause result = new Clause(childKB);

    for (Clause c : childKB) {

    if (c.equals(result)) {

    return null;

    }

    }

    result.litts = finalLiterals;

    System.out.println("Resolution: " + this + " && " + other + " => "

    + result + " | F: " +this.f_score +" | G: " +this.g_score

    +" | H: " +this.h_score);

    return result;

    }

    }