10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract...

13

Transcript of 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract...

Page 1: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static
Page 2: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

10 70 Vestervoldgade 20 50

Page 3: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

𝑓(𝑥) = 𝑔(𝑥) + ℎ(𝑥)

𝑑 = √(𝑥2 − 𝑥1)2 + (𝑦2 − 𝑦1)2

Page 4: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

∧ ¬ α ├

├ α

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

Page 5: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

a,~b,~c

b,~b

b,~c,~d

c

d

Page 6: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

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

Page 7: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static
Page 8: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

public abstract class AbstractNode implements Node, Comparable<AbstractNode>

{

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<AbstractEgde> outgoingEdges = new

ArrayList<AbstractEgde>();

protected boolean visited = false;

public AbstractNode() {

this.nodeNumber = nextNumber++;

}

public class AbstractEgde implements Comparable<AbstractEgde> {

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<AbstractNode> aStar(AbstractNode startNode,

AbstractNode goalNode) {

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

the start node

LinkedList<AbstractNode> openSet = new LinkedList<AbstractNode>();

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<AbstractEgde> outgoingEdges =

currentNode.getOutgoingEdges();

Collections.sort(outgoingEdges);

Page 9: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

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<AbstractNode> 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));

}

Page 10: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

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<Clause>());

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

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

Search s = new Search();

LinkedList<AbstractNode> 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");

}

Page 11: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

public class Clause extends AbstractNode implements Node {

private HashSet<Literal> litts = new HashSet<Literal>();

ArrayList<AbstractEgde> outGoingEdges;

private ArrayList<Clause> orgKB;

private ArrayList<Clause> childKB = new ArrayList<Clause>();

public Clause(ArrayList<Clause> localKB) {

super();

this.orgKB = localKB;

childKB.addAll(localKB);

childKB.add(this);

}

public void addLiteral(Literal l) {

litts.add(l);

}

public void setLocalKB(ArrayList<Clause> localKB) {

this.orgKB = localKB;

}

@Override

public double estimateHeuristic(Node other) {

return litts.size();

}

@Override

public ArrayList<AbstractEgde> getOutgoingEdges() {

if (outGoingEdges == null) {

outGoingEdges = new ArrayList<AbstractEgde>();

for (Clause other : orgKB) {

Clause result = proResolution(other);

if (result != null) {

AbstractEgde a = new AbstractEgde(result,

other.toString());

outGoingEdges.add(a);

}

}

}

return outGoingEdges;

}

Page 12: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static

public Clause proResolution(Clause other) {

HashSet<Literal> tempLiterals = new HashSet<Literal>();

HashSet<Literal> finalLiterals = new HashSet<Literal>();

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;

}

}

Page 13: 10 70 Vestervoldgade 20 50uniguld.dk/wp-content/guld/DTU/Kunstig//HEUREKA.pdf · public abstract class AbstractNode implements Node, Comparable {protected static