From 46f025098aed959b4d6d93916534c2a886f7257e Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Tue, 18 Oct 2016 18:43:51 -0600 Subject: [PATCH 001/191] Added Clause --- Wumpus/src/Clause.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Wumpus/src/Clause.java diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java new file mode 100644 index 0000000..5982d69 --- /dev/null +++ b/Wumpus/src/Clause.java @@ -0,0 +1,13 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/** + * + * @author Wilson + */ +public class Clause { + +} From 869476552a541aa787e8aaa53c531dff373331bc Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Tue, 18 Oct 2016 20:08:27 -0600 Subject: [PATCH 002/191] nothing really --- Wumpus/src/Clause.java | 5 ++++- Wumpus/src/InferenceEngine.java | 4 ++++ Wumpus/src/KnowledgeBase.java | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 5982d69..2842d46 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -1,3 +1,6 @@ + +import java.util.ArrayList; + /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates @@ -9,5 +12,5 @@ * @author Wilson */ public class Clause { - + ArrayList facts = new ArrayList<>(); } diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index d42b1b7..9f2cadb 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -5,6 +5,10 @@ public class InferenceEngine { KnowledgeBase kb; + public boolean follows(Clause clause){ + return true;//follows(clause, ) + } + public ArrayList convertToCNF(Rule rule) { ArrayList cnfRules = new ArrayList<>(); ArrayList toConvertRules = new ArrayList<>(); diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index ab9f733..c11687c 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -8,6 +8,8 @@ public void initializeRules() { //Vx,y,a,b Adjacent(x,y,a,b) iff [a,b] in {(x-1,y),(x+1,y),(x,y-1),(x,y+1)} //Vd,t Facing(d,t) => Facing(d,t+1) OR Action(Turnleft,t) OR Action(TurnRight,t) //Vt,v Facing(d,t) AND Action(Turnleft,t)=>Facing((d-1)%4, t+1) + + //Vt,v Facing(d,t) AND Action(TurnRight,t)=>Facing((d+1)%4,t+1) //Vt,x Arrows(x,t) => Arrows(x,t+1) OR Action(SHOOT,t) //Vt,x HasArrow(t) iff Arrows(x,t) && x>0 From 3a251a57c0b3b4d487df39fcb92f4f47fe25e073 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Tue, 18 Oct 2016 20:32:08 -0600 Subject: [PATCH 003/191] Small things --- Wumpus/src/InferenceEngine.java | 2 ++ Wumpus/src/KnowledgeBase.java | 9 --------- Wumpus/src/Substitute.java | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 Wumpus/src/Substitute.java diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 9f2cadb..3957caa 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -9,6 +9,8 @@ public boolean follows(Clause clause){ return true;//follows(clause, ) } + //So for follows we do a proof by contradiction. + public ArrayList convertToCNF(Rule rule) { ArrayList cnfRules = new ArrayList<>(); ArrayList toConvertRules = new ArrayList<>(); diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index c11687c..1e0c7a1 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -24,7 +24,6 @@ public void initializeRules() { //Wumpus(x,y,t) => Wumpus(x,y,t+1) XOR WumpusDead(x,y,t+1) p XOR q is (pVq)&&(!(p&&q)) //WumpusDead(x,y,t)=>WumpusDead(x,y,t+1) //Stench(x,y)=>Wumpus(x-1,y)ORWumpus(x+1,y)ORWumpus(x,y-1)ORWumpus(x,y+1) - initializeStenchRule(); //Breeze mimics this //Glitter(x,y,t)=>Gold(x,y,t) @@ -41,12 +40,4 @@ public boolean ask(String placeholder) { public void tell(String placeholder) { } - - private void initializeStenchRule() { - //Stench(x,y)=>Wumpus(x-1,y)OR(Wumpus(x+1,y)OR(Wumpus(x,y-1) OR Wumpus(x,y+1))) - Quantifier x = new Quantifier(); - - //ClauseFormConvertion from here... - } - } diff --git a/Wumpus/src/Substitute.java b/Wumpus/src/Substitute.java new file mode 100644 index 0000000..f0790f0 --- /dev/null +++ b/Wumpus/src/Substitute.java @@ -0,0 +1,15 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/** + * + * @author Wilson + */ +public class Substitute { + int varIdToSubstitute; + int valToSubstituteWith; + +} From 9b2f5fc610292730221445f7bae703f007135c9e Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Tue, 18 Oct 2016 21:12:12 -0600 Subject: [PATCH 004/191] Problem generator now adds gold. Start zone changed to be variable. --- Wumpus/PerceptBoard.txt | 6 ++++++ Wumpus/src/Driver.java | 10 +++++----- Wumpus/src/Space.java | 8 ++++++++ Wumpus/src/Unifier.java | 2 +- Wumpus/src/WumpusGame.java | 21 +++++++++++++++++++++ Wumpus/world.txt | 6 ++++++ world.txt | 6 ------ 7 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 Wumpus/PerceptBoard.txt create mode 100644 Wumpus/world.txt delete mode 100644 world.txt diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt new file mode 100644 index 0000000..ff18297 --- /dev/null +++ b/Wumpus/PerceptBoard.txt @@ -0,0 +1,6 @@ +5 +1 34 17 2 0 +0 5 2 0 0 +0 0 4 4 0 +0 0 0 8 0 +0 0 0 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index c53c48a..5ec78a1 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -6,13 +6,13 @@ public class Driver { public static void main(String[] args) throws IOException { - Driver driver = new Driver(); - Tester tester = new Tester(); - tester.testInferenceEngine(); - //driver.makeGame(); +// Driver driver = new Driver(); +// Tester tester = new Tester(); +// tester.testInferenceEngine(); + Driver.makeGame(); } - public void makeGame() throws IOException { + public static void makeGame() throws IOException { BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); int[] prob = new int[3]; diff --git a/Wumpus/src/Space.java b/Wumpus/src/Space.java index a0c1402..1859d78 100644 --- a/Wumpus/src/Space.java +++ b/Wumpus/src/Space.java @@ -5,6 +5,7 @@ public class Space { private boolean hasHole = false; private boolean hasObstacle = false; private boolean hasGold = false; + private boolean start; private boolean filled = false; public Space() { @@ -60,4 +61,11 @@ public void setFilled(boolean filled) { this.filled = filled; } + public boolean isStart() { + return start; + } + + public void setStart(boolean start) { + this.start = start; + } } diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 81db585..5d26026 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -39,7 +39,7 @@ private static Map unify(Sentence x, Sentence y, } private static Map unifyVariables(Variable x, Variable y, Map theta) { - + return null; } private static Map unifyOperators(String x, String y, Map theta) { diff --git a/Wumpus/src/WumpusGame.java b/Wumpus/src/WumpusGame.java index 6bb463f..50afcc5 100644 --- a/Wumpus/src/WumpusGame.java +++ b/Wumpus/src/WumpusGame.java @@ -52,6 +52,19 @@ public void initializeBoard() { chooseState(i, j); } } + int x = random.nextInt(boardSize); + int y = random.nextInt(boardSize); + while (board[x][y].isFilled()) { + x = random.nextInt(boardSize); + y = random.nextInt(boardSize); + } + placeGold(x, y); + + while (board[x][y].isFilled()) { + x = random.nextInt(boardSize); + y = random.nextInt(boardSize); + } + placeStart(x, y); } public void chooseState(int x, int y) { @@ -107,6 +120,10 @@ public void placeWumpus(int x, int y) { wumpus++; } + public void placeStart(int x, int y) { + board[x][y].setStart(true); + } + public void checkBlockedStart() { } @@ -137,6 +154,10 @@ public void printBoard() { System.out.print("H "); } else if (board[i][j].isHasObstacle()) { System.out.print("I "); + } else if (board[i][j].isHasGold()) { + System.out.print("G "); + } else if (board[i][j].isStart()) { + System.out.print("S "); } else { System.out.print("0 "); } diff --git a/Wumpus/world.txt b/Wumpus/world.txt new file mode 100644 index 0000000..4dee392 --- /dev/null +++ b/Wumpus/world.txt @@ -0,0 +1,6 @@ +5 +0 H W 0 0 +0 I 0 0 0 +0 0 I I 0 +0 S 0 G 0 +0 0 0 0 0 diff --git a/world.txt b/world.txt deleted file mode 100644 index edd36fd..0000000 --- a/world.txt +++ /dev/null @@ -1,6 +0,0 @@ -5 -I 0 0 H 0 -0 0 0 0 0 -0 0 0 0 0 -0 I 0 0 0 -0 I 0 0 0 From dbe11579f29eda788637bf9f657b5e5245d6e951 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Tue, 18 Oct 2016 21:25:07 -0600 Subject: [PATCH 005/191] So painful.. --- Wumpus/src/AtomicTerm.java | 4 ---- Wumpus/src/Clause.java | 2 +- Wumpus/src/KnowledgeBase.java | 9 ++++++++- Wumpus/src/Predicate.java | 13 +++++++++++++ Wumpus/src/Sentence.java | 2 +- Wumpus/src/SubstitutionString.java | 4 ++++ Wumpus/src/Unifier.java | 23 +++++++++++------------ 7 files changed, 38 insertions(+), 19 deletions(-) delete mode 100644 Wumpus/src/AtomicTerm.java create mode 100644 Wumpus/src/Predicate.java create mode 100644 Wumpus/src/SubstitutionString.java diff --git a/Wumpus/src/AtomicTerm.java b/Wumpus/src/AtomicTerm.java deleted file mode 100644 index 783db97..0000000 --- a/Wumpus/src/AtomicTerm.java +++ /dev/null @@ -1,4 +0,0 @@ - -public interface AtomicTerm { - -} diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 2842d46..e335ff0 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -11,6 +11,6 @@ * * @author Wilson */ -public class Clause { +public interface Clause { ArrayList facts = new ArrayList<>(); } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index c11687c..dc75bb4 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -1,5 +1,7 @@ public class KnowledgeBase { + + private ComplexSentence knowledge; public void initializeRules() { //Special predicate: Evaluate @@ -39,7 +41,12 @@ public boolean ask(String placeholder) { return true; } - public void tell(String placeholder) { + public void tell(Sentence sentence) { + + //unify + ComplexSentence unifiedknowldge = Unifier.unify(this.knowledge, sentence); + //resolve + //add to database } private void initializeStenchRule() { diff --git a/Wumpus/src/Predicate.java b/Wumpus/src/Predicate.java new file mode 100644 index 0000000..19de678 --- /dev/null +++ b/Wumpus/src/Predicate.java @@ -0,0 +1,13 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/** + * + * @author Kektus + */ +public class Predicate { + +} diff --git a/Wumpus/src/Sentence.java b/Wumpus/src/Sentence.java index 230f9b2..5dbbfef 100644 --- a/Wumpus/src/Sentence.java +++ b/Wumpus/src/Sentence.java @@ -1,5 +1,5 @@ -public interface Sentence { +public interface Sentence extends Clause { @Override boolean equals(Object object); diff --git a/Wumpus/src/SubstitutionString.java b/Wumpus/src/SubstitutionString.java new file mode 100644 index 0000000..570bcdd --- /dev/null +++ b/Wumpus/src/SubstitutionString.java @@ -0,0 +1,4 @@ + +public class SubstitutionString { + +} diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 81db585..8597a29 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -15,12 +15,11 @@ public static String Unify(String p, String q) { return substitution; } - public static Map unify(Sentence x, Sentence y) { - return (Map) unify(x, y, new HashMap<>()); + public static SubstitutionString unify(Sentence x, Sentence y) { + return unify(x, y, new SubstitutionString()); } - private static Map unify(Sentence x, Sentence y, - Map theta) { + private static SubstitutionString unify(Sentence x, Sentence y,SubstitutionString theta) { if (theta == null) { //return null in case of failure to find substitution return null; } else if (x.equals(y)) { //if sentences are equal theta contains the complete substitution @@ -31,19 +30,19 @@ private static Map unify(Sentence x, Sentence y, } else if (y instanceof Variable) { // else if VARIABLE?(y) then return UNIFY-VAR(y, x, theta) return unifyVariables((Variable) y, (Variable) x, theta); - // } else if () { //if x and y are compound - // return unify(x.getArguments(), y.getArguments(), unifyOperators(getOperator(x), y.getValue(), theta)); + // } else if () { //if x and y are compound + // return unify(x.getArguments(), y.getArguments(), unifyOperators(getOperator(x), y.getValue(), theta)); } else { return null; } } - - private static Map unifyVariables(Variable x, Variable y, Map theta) { - + + private static SubstitutionString unifyVariables(Variable x, Variable y, SubstitutionString theta) { + } - - private static Map unifyOperators(String x, String y, Map theta) { - + + private static SubstitutionString unifyOperators(String x, String y, SubstitutionString theta) { + if (theta == null) { return theta; } else if (x.equals(y)) { From c4d39312c2a90b16c4fd9081f1c2820b5b32658a Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Tue, 18 Oct 2016 21:41:38 -0600 Subject: [PATCH 006/191] some explorerlogic --- Wumpus/src/Clause.java | 4 ++++ Wumpus/src/Fact.java | 11 ++++++++++- Wumpus/src/InferenceEngine.java | 6 ------ Wumpus/src/KnowledgeBase.java | 2 +- Wumpus/src/LogicExplorer.java | 25 ++++++++++++++++++++++--- Wumpus/src/Variable.java | 4 ++++ 6 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 2842d46..38dfbf4 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -12,5 +12,9 @@ * @author Wilson */ public class Clause { + public Clause(){} + public Clause(Fact fact){ + facts.add(fact); + } ArrayList facts = new ArrayList<>(); } diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index 8dbbe7c..4009805 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -7,7 +7,16 @@ public class Fact { String predicate; boolean isEvaluation; int operator; - + boolean not; + + public Fact(){} + public Fact(String predicate, int var1Val, int var2Val, int var3Val, boolean not){ + Variable var1 = new Variable(var1Val); + Variable var2 = new Variable(var2Val); + Variable var3 = new Variable(var3Val); + this.predicate = predicate; + this.not = not; + } public void printFact() { System.out.print(predicate + "("); for (Variable var : variables) { diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 3957caa..d42b1b7 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -5,12 +5,6 @@ public class InferenceEngine { KnowledgeBase kb; - public boolean follows(Clause clause){ - return true;//follows(clause, ) - } - - //So for follows we do a proof by contradiction. - public ArrayList convertToCNF(Rule rule) { ArrayList cnfRules = new ArrayList<>(); ArrayList toConvertRules = new ArrayList<>(); diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 1e0c7a1..3f88db3 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -38,6 +38,6 @@ public boolean ask(String placeholder) { return true; } - public void tell(String placeholder) { + public void tell(Clause clause) { } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index c09620a..fe880ab 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -4,9 +4,12 @@ public class LogicExplorer { private final World world; private final KnowledgeBase kb; private int arrowCount; + private int t = 0; + private int curPosX; + private int curPosY; private final byte BREEZE = 0b00000001; - private final byte STENTCH = 0b0000010; + private final byte STENCH = 0b0000010; private final byte BUMP = 0b00000100; private final byte GLITTER = 0b00001000; private final byte DEATH_BY_WUMPUS = 0b00010000; @@ -24,7 +27,7 @@ private void move(int action) { switch (action) { case 2: - kb.tell(encodePercepts(world.action(action))); + //kb.tell(encodePercepts(world.action(action))); break; case 5: arrowCount--; @@ -45,7 +48,7 @@ private String encodePercepts(int percepts) { } public void decideAction(byte percepts) { - + updateKB(percepts); if ((percepts & GLITTER) != 0) {//maybe just kb.ask("Holding(Gold,Result(Grab,CurrentPosition))"): is better, no percept based logic within agent. move(1); //grab gold and end game } else if (kb.ask("Safe(Result(Move,CurrentPosition))&&!Explored(Result(Move,CurrentPosition))")) { @@ -72,6 +75,22 @@ public void decideAction(byte percepts) { } } } + + private void updateKB(byte percepts){ + if((percepts & STENCH) != 0){ + Clause clause = new Clause(); + kb.tell(new Clause(new Fact("Stench", curPosX,curPosY,t,true))); + } + else{ + kb.tell(new Clause(new Fact("Stench", curPosX, curPosY,t,false))); + } + if((percepts & BREEZE) != 0){ + kb.tell(new Clause(new Fact("Breeze", curPosX,curPosY,t,true))); + } + else{ + kb.tell(new Clause(new Fact("Breeze", curPosX, curPosY,t,false))); + } + } private void RHWTraversal(String stopCondition) { diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index d085bc8..261bae1 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -8,6 +8,10 @@ public class Variable { int modifier; boolean isSkolemConstant;//Skolem function would just be the function, a skolem constant will have this and it's varId making a unique identifier + public Variable(){} + public Variable(int value){ + this.value = value; + } public void printVariable() { if (isVariable) { From 72ed6a2e8832ce5ddb9febd29c210c47cd8e804f Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Tue, 18 Oct 2016 21:56:11 -0600 Subject: [PATCH 007/191] Well fk me, im real lost in unification --- Wumpus/src/Clause.java | 8 +++- Wumpus/src/SubstitutionString.java | 21 +++++++++ Wumpus/src/Unifier.java | 76 +++++++++++++++--------------- Wumpus/src/Variable.java | 10 +++- 4 files changed, 76 insertions(+), 39 deletions(-) diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 3d227bb..ed824c0 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -1,11 +1,17 @@ import java.util.ArrayList; +import java.util.Iterator; /* A clause contains a list of predicates */ -public interface Clause { +public class Clause implements Iterable { ArrayList facts = new ArrayList<>(); + + @Override + public Iterator iterator() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } } diff --git a/Wumpus/src/SubstitutionString.java b/Wumpus/src/SubstitutionString.java index 570bcdd..f3a0755 100644 --- a/Wumpus/src/SubstitutionString.java +++ b/Wumpus/src/SubstitutionString.java @@ -1,4 +1,25 @@ +import java.util.ArrayList; + + public class SubstitutionString { + private ArrayList string = new ArrayList<>(); + + public boolean contains(Variable var) { + //returns true if the string contains the variable + return true; + } + + public Variable get(Variable var) { + + return string.get(string.indexOf(var)); + } + + public void set(Variable var, Variable replacement) { + + int index = string.indexOf(var); + string.remove(var); + string.add(index, replacement); + } } diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 397dcce..e9cd498 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -4,12 +4,13 @@ public class Unifier { private static Stack trail = new Stack(); + public Unifier() { } - public static SubstitutionString Unify(Variable x, Variable y, SubstitutionString theta) { - + public static SubstitutionString unify(Variable x, Variable y, SubstitutionString theta) { + if (theta == null) { return null; } else if (x.equals(y)) { @@ -18,43 +19,44 @@ public static SubstitutionString Unify(Variable x, Variable y, SubstitutionStrin return unifyVariable(x, y, theta); } else if (y instanceof Variable) { return unifyVariable(y, x, theta); - } else if (x) + } else if (x) { + + } } - - + private static SubstitutionString unifyVariable(Variable x, Variable y, SubstitutionString theta) { - - if - + + if (theta.contains(x)) { + return unify(theta.get(x), y, theta); + } else if (occurCheck(x, y)) { + return null; + } else { + return extend(theta, x, y); + } } + + private static boolean occurCheck(Variable x, Variable y) { + + if (x.equals(y)) { + return true; + } else if (x instanceof Sentence) { + return x.getOp() == y.getOp() || occurCheck(x, y.getArgs()); + } else if (!(x instanceof Clause)) { + for ( x1 : x) { + + } + } else { + return false; + } + } - - - - - - - - - - - - - - - - - - - - - - - - - - + private static SubstitutionString extend(SubstitutionString theta, Variable x, Variable y) { + + SubstitutionString temp = theta; + temp.set(x, y); + return temp; + } public static String Unify(String p, String q) { String substitution = ""; @@ -87,14 +89,14 @@ private static Object deref(Object object) { } return object; } - + private static void setBinding(Variable var, Object val) { var.binding = val; trail.push(var); } - + private static void undoBindings(Object token) { - + Object var; while ((var = trail.pop()) != token) { if (var instanceof Variable) { diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index d085bc8..25902dd 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -1,5 +1,8 @@ -public class Variable { +import java.util.Iterator; + + +public class Variable implements Iterable { boolean isVariable; int value; @@ -18,4 +21,9 @@ public void printVariable() { public int getValue() { return value; } + + @Override + public Iterator iterator() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } } From 10ed1fb83b8d0473ffa1ea2a3ef89212dd3feecf Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Tue, 18 Oct 2016 22:22:45 -0600 Subject: [PATCH 008/191] Map now imported into the World --- Wumpus/PerceptBoard.txt | 10 +++---- Wumpus/src/Driver.java | 1 + Wumpus/src/World.java | 58 +++++++++++++++++++++++++++++++++----- Wumpus/src/WumpusGame.java | 2 ++ Wumpus/world.txt | 8 +++--- 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index ff18297..1db1f24 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ 5 -1 34 17 2 0 -0 5 2 0 0 -0 0 4 4 0 -0 0 0 8 0 -0 0 0 0 0 +1 0 0 1 32 +32 1 0 0 1 +1 0 0 4 0 +0 0 0 1 0 +0 0 1 32 9 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 5ec78a1..9e479c8 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -27,6 +27,7 @@ public static void makeGame() throws IOException { System.out.println(""); WumpusGame game = new WumpusGame(5, prob); + World world = new World("PerceptBoard.txt"); } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index d214fcd..ce9b671 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -1,4 +1,7 @@ +import java.io.BufferedReader; +import java.io.FileReader; + public class World { protected int arrowCount; @@ -30,9 +33,41 @@ public class World { public World(String fileName) { x = 0; y = 0; + importMap(fileName); //read in file } + public void importMap(String fileName) { + try { + FileReader in = new FileReader(fileName); + BufferedReader reader = new BufferedReader(in); + String next; + int size = Integer.parseInt(reader.readLine()); + perceptMap = new int[size][size]; + int i = 0; + while ((next = reader.readLine()) != null) {//((Integer) reader.read()).toString()).equals("-1")) { + int j = 0; + while (next.contains(" ") && !next.equals(" ")) { + perceptMap[i][j] = Integer.parseInt(next.substring(0, next.indexOf(" "))); + + next = next.substring(next.indexOf(" ") + 1, next.length()); + j++; + } + i++; + } + System.out.println(""); + for (int k = 0; k < perceptMap.length; k++) { + for (int j = 0; j < perceptMap[k].length; j++) { + System.out.print(perceptMap[k][j] + " "); + } + System.out.println(""); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + public int[] getLocation() { int[] location = {x, y}; return location; @@ -45,30 +80,39 @@ public int getPercepts() { public int action(int action) { switch (action) { case GRAB: - if ((perceptMap[x][y] & GLITTER) != 0) + if ((perceptMap[x][y] & GLITTER) != 0) { perceptMap[x][y] -= GLITTER; + } break; case MOVE: if (direction == NORTH) { if (y + 1 < size) { y = y + 1; return perceptMap[x][y]; - } else return BUMP; + } else { + return BUMP; + } } else if (direction == EAST) { if (x + 1 < size) { x = x + 1; return perceptMap[x][y]; - } else return BUMP; + } else { + return BUMP; + } } else if (direction == SOUTH) { if (y - 1 > 0) { y -= 1; return perceptMap[x][y]; - } else return BUMP; + } else { + return BUMP; + } } else if (direction == WEST) { if (x - 1 > 0) { x -= 1; return perceptMap[x][y]; - } else return BUMP; + } else { + return BUMP; + } } break; case TURN_LEFT: @@ -129,10 +173,10 @@ public int action(int action) { } case END: - //needed? + //needed? } System.out.println("Error shouldn't ever get here"); return -1; } -} \ No newline at end of file +} diff --git a/Wumpus/src/WumpusGame.java b/Wumpus/src/WumpusGame.java index 50afcc5..47a6c68 100644 --- a/Wumpus/src/WumpusGame.java +++ b/Wumpus/src/WumpusGame.java @@ -1,5 +1,6 @@ import java.io.File; +import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -36,6 +37,7 @@ public WumpusGame(int boardSize, int[] prob) throws FileNotFoundException { PrintStream out = new PrintStream(new FileOutputStream("world.txt")); System.setOut(out); printBoards(); + System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); } public void setBoard() { diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 4dee392..782a411 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -0 H W 0 0 -0 I 0 0 0 -0 0 I I 0 -0 S 0 G 0 +0 0 0 0 H +H 0 S 0 0 +0 0 0 I 0 0 0 0 0 0 +0 0 0 H G From b70d706d81fbeeb0ca053bdb1b1d535f17ff210b Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Tue, 18 Oct 2016 22:25:56 -0600 Subject: [PATCH 009/191] fix --- Wumpus/src/Clause.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index ed824c0..c215cbc 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -8,7 +8,11 @@ public class Clause implements Iterable { ArrayList facts = new ArrayList<>(); - + + public Clause(){} + public Clause(Fact fact){ + facts.add(fact); + } @Override public Iterator iterator() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. From 2e20541d924fc5257330fc60ec44ce6099b51de5 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 08:53:28 -0600 Subject: [PATCH 010/191] Explorers now both extend an agent class --- Wumpus/src/Agent.java | 14 ++++++++++++++ Wumpus/src/Driver.java | 2 ++ Wumpus/src/LogicExplorer.java | 4 ++-- Wumpus/src/ReactiveExplorer.java | 14 +++++++------- 4 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 Wumpus/src/Agent.java diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java new file mode 100644 index 0000000..c0a573a --- /dev/null +++ b/Wumpus/src/Agent.java @@ -0,0 +1,14 @@ + +public abstract class Agent { + + + private void move(int action) { + + } + + public void decideNextAction() { + + } + + +} diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 9e479c8..f79e037 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -28,6 +28,8 @@ public static void makeGame() throws IOException { WumpusGame game = new WumpusGame(5, prob); World world = new World("PerceptBoard.txt"); + + Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index fe880ab..01aa18f 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -1,5 +1,5 @@ -public class LogicExplorer { +public class LogicExplorer extends Agent { private final World world; private final KnowledgeBase kb; @@ -47,7 +47,7 @@ private String encodePercepts(int percepts) { return sentence; } - public void decideAction(byte percepts) { + public void decideNextAction(byte percepts) { updateKB(percepts); if ((percepts & GLITTER) != 0) {//maybe just kb.ask("Holding(Gold,Result(Grab,CurrentPosition))"): is better, no percept based logic within agent. move(1); //grab gold and end game diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 94ef334..c632cb1 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -1,7 +1,7 @@ import java.util.Random; -public class ReactiveExplorer { +public class ReactiveExplorer extends Agent { private World world; private int arrowCount; @@ -20,13 +20,13 @@ public class ReactiveExplorer { private final byte DEATH_BY_PIT = 0b00100000; private final byte SCREAM = 0b01000000; - public ReactiveExplorer(World world) { + public ReactiveExplorer(World world, int[] location, int direction, int percepts, int arrowCount) { this.world = world; - this.arrowCount = world.arrowCount; - this.previousSpace = world.getLocation(); + this.arrowCount = arrowCount; + this.previousSpace = location; this.currentSpace = previousSpace; - this.percepts = world.getPercepts(); - this.direction = world.direction; + this.percepts = percepts; + this.direction = direction; if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { previousSafe = true; currentSafe = true; @@ -53,7 +53,7 @@ private void move(int action) { } } - public void decideAction() { + public void decideNextAction(byte percepts) { //select safe neighboring cell else select unsafe neighboring cell if (((percepts & STENTCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe From 06208ff86fe5caeb188c45952b7e321ebd57334a Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 08:54:49 -0600 Subject: [PATCH 011/191] Small progress --- Wumpus/src/LogicExplorer.java | 40 ++++++++++++++++++++++++++++------- Wumpus/src/World.java | 12 +++++------ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index fe880ab..3e2105e 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -1,12 +1,19 @@ +import java.util.ArrayList; + + public class LogicExplorer { private final World world; private final KnowledgeBase kb; private int arrowCount; private int t = 0; - private int curPosX; - private int curPosY; + private Position currentPos; + private int currentDirection; + private ArrayList frontier = new ArrayList<>(); + boolean[][] searchedPositions; + int worldSize; + private final byte BREEZE = 0b00000001; private final byte STENCH = 0b0000010; @@ -21,6 +28,8 @@ public LogicExplorer(World world) { kb = new KnowledgeBase(); kb.initializeRules(); this.arrowCount = world.arrowCount; + this.searchedPositions = new boolean[world.size][world.size]; + this.worldSize = world.size; } private void move(int action) { @@ -47,10 +56,11 @@ private String encodePercepts(int percepts) { return sentence; } - public void decideAction(byte percepts) { + public int decideAction(byte percepts) { + processPosition(percepts); updateKB(percepts); if ((percepts & GLITTER) != 0) {//maybe just kb.ask("Holding(Gold,Result(Grab,CurrentPosition))"): is better, no percept based logic within agent. - move(1); //grab gold and end game + return World.GRAB; //grab gold and end game } else if (kb.ask("Safe(Result(Move,CurrentPosition))&&!Explored(Result(Move,CurrentPosition))")) { move(2); //move forward } else if (kb.ask("Safe(Result(Move,Result(TurnLeft,CurrentPosition))&&!Explored(Result(Move,Result(TurnLeft,CurrentPosition))")) { @@ -74,21 +84,30 @@ public void decideAction(byte percepts) { } } } + + return -1; + } + + private void processPosition(byte percepts){ + } private void updateKB(byte percepts){ if((percepts & STENCH) != 0){ Clause clause = new Clause(); - kb.tell(new Clause(new Fact("Stench", curPosX,curPosY,t,true))); + kb.tell(new Clause(new Fact("Stench", currentPos.x,currentPos.y,t,true)));//Stench(x,y,t) } else{ - kb.tell(new Clause(new Fact("Stench", curPosX, curPosY,t,false))); + kb.tell(new Clause(new Fact("Stench", currentPos.x, currentPos.y,t,false)));//!Stench(x,y,t) } if((percepts & BREEZE) != 0){ - kb.tell(new Clause(new Fact("Breeze", curPosX,curPosY,t,true))); + kb.tell(new Clause(new Fact("Breeze", currentPos.x,currentPos.y,t,true))); } else{ - kb.tell(new Clause(new Fact("Breeze", curPosX, curPosY,t,false))); + kb.tell(new Clause(new Fact("Breeze", currentPos.x, currentPos.y,t,false))); + } + if((percepts & SCREAM) != 0){ + //need to deal with this } } @@ -115,4 +134,9 @@ private void RHWTraversal(String stopCondition) { } move(2); } + + private class Position{ + int x; + int y; + } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index ce9b671..f4cc023 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -5,7 +5,7 @@ public class World { protected int arrowCount; - private int size; + public int size; private int x; private int y; protected int direction = 0; @@ -23,11 +23,11 @@ public class World { private final int DEATH_BY_PIT = 32; private final int SCREAM = 64; - final int GRAB = 1; - final int MOVE = 2; - final int TURN_LEFT = 3; - final int TURN_RIGHT = 4; - final int SHOOT = 5; + static final int GRAB = 1; + static final int MOVE = 2; + static final int TURN_LEFT = 3; + static final int TURN_RIGHT = 4; + static final int SHOOT = 5; final int END = 6;//needed? public World(String fileName) { From 0c7fee632b01048bfa10360faf2de88fb32e903a Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 08:56:53 -0600 Subject: [PATCH 012/191] did zachs extend thing --- Wumpus/src/LogicExplorer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 3e2105e..18dee24 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -2,7 +2,7 @@ import java.util.ArrayList; -public class LogicExplorer { +public class LogicExplorer extends Agent{ private final World world; private final KnowledgeBase kb; From 4338e34268e82ac144fdd927ae20daf412469305 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 08:58:48 -0600 Subject: [PATCH 013/191] Moved position to Agent class, int arrays are silly --- Wumpus/src/Agent.java | 5 ++++- Wumpus/src/LogicExplorer.java | 5 ----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index c0a573a..3d9a39a 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -10,5 +10,8 @@ public void decideNextAction() { } - + class Position{ + int x; + int y; + } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 18dee24..ac9f669 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -134,9 +134,4 @@ private void RHWTraversal(String stopCondition) { } move(2); } - - private class Position{ - int x; - int y; - } } From 9744e7bc76a1cabc43e116af4a73c2da76908bbc Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 09:13:29 -0600 Subject: [PATCH 014/191] Position changes --- Wumpus/src/Agent.java | 20 ++++++++++++++++++++ Wumpus/src/LogicExplorer.java | 10 +++++++++- Wumpus/src/World.java | 8 ++++---- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 3d9a39a..372b3c1 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -1,4 +1,7 @@ +import java.util.ArrayList; + + public abstract class Agent { @@ -13,5 +16,22 @@ public void decideNextAction() { class Position{ int x; int y; + int direction; + + public void moveDidMove(){ + switch(direction){ + case World.NORTH: + y+=1; + break; + case World.EAST: + x-=1; + break; + case World.SOUTH: + y--; + break; + case World.WEST: + x++; + } + } } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index ac9f669..59937bf 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -12,6 +12,7 @@ public class LogicExplorer extends Agent{ private int currentDirection; private ArrayList frontier = new ArrayList<>(); boolean[][] searchedPositions; + boolean currentlyNavigatingToSafeSquare; int worldSize; @@ -61,7 +62,14 @@ public int decideAction(byte percepts) { updateKB(percepts); if ((percepts & GLITTER) != 0) {//maybe just kb.ask("Holding(Gold,Result(Grab,CurrentPosition))"): is better, no percept based logic within agent. return World.GRAB; //grab gold and end game - } else if (kb.ask("Safe(Result(Move,CurrentPosition))&&!Explored(Result(Move,CurrentPosition))")) { + } + else if(currentlyNavigatingToSafeSquare){ + //continueNavigation + } + // else if(){ + + //} + else if (kb.ask("Safe(Result(Move,CurrentPosition))&&!Explored(Result(Move,CurrentPosition))")) { move(2); //move forward } else if (kb.ask("Safe(Result(Move,Result(TurnLeft,CurrentPosition))&&!Explored(Result(Move,Result(TurnLeft,CurrentPosition))")) { move(3); //turn left diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index f4cc023..3edc50a 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -11,10 +11,10 @@ public class World { protected int direction = 0; private int[][] perceptMap; - final int NORTH = 1; - final int EAST = 2; - final int SOUTH = 3; - final int WEST = 4; + public static final int NORTH = 1; + public static final int EAST = 2; + public static final int SOUTH = 3; + public static final int WEST = 4; private final int BREEZE = 1; private final int STENTCH = 2; private final int BUMP = 4; From 8529038fe16d054e17b39d7421d69a2e49b5df16 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 09:13:40 -0600 Subject: [PATCH 015/191] WIP respawning --- Wumpus/src/Agent.java | 5 +++++ Wumpus/src/ReactiveExplorer.java | 35 ++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index c0a573a..e203398 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -10,5 +10,10 @@ public void decideNextAction() { } + private static Agent death(Agent agent) { + agent.getClass(); + return new agent.getClass(); + } + } diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index c632cb1..c8a3d99 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -4,13 +4,9 @@ public class ReactiveExplorer extends Agent { private World world; - private int arrowCount; - private int[] previousSpace; - private boolean previousSafe; - private int percepts; - private int[] currentSpace; - private boolean currentSafe; - private int direction; + private int[] currentLocation, previousLocation; + private boolean currentSafe, previousSafe; + private int percepts, direction, arrowCount; private final byte BREEZE = 0b00000001; private final byte STENTCH = 0b0000010; @@ -23,8 +19,8 @@ public class ReactiveExplorer extends Agent { public ReactiveExplorer(World world, int[] location, int direction, int percepts, int arrowCount) { this.world = world; this.arrowCount = arrowCount; - this.previousSpace = location; - this.currentSpace = previousSpace; + this.previousLocation = location; + this.currentLocation = location; this.percepts = percepts; this.direction = direction; if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { @@ -33,20 +29,26 @@ public ReactiveExplorer(World world, int[] location, int direction, int percepts } } + private ReactiveExplorer() { + + } + private void move(int action) { if (action == 1) { percepts = world.action(action); if ((percepts & GLITTER) == GLITTER) { //found gold - } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT || (percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - //got dicked + } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { + death(DEATH_BY_PIT); + } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + death(DEATH_BY_WUMPUS); } if ((percepts & BUMP) != BUMP) { previousSafe = currentSafe; - previousSpace = currentSpace; + previousLocation = currentLocation; } - currentSpace = world.getLocation(); + currentLocation = world.getLocation(); currentSafe = ((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE); } else { world.action(action); @@ -99,4 +101,11 @@ public void decideNextAction(byte percepts) { } } } + + private Agent death(byte killer) { + + currentLocation = previousLocation; + currentSafe = previousSafe; + + } } From 677c9776bd16969be3dd2dc16fe2ec4f4308148f Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Wed, 19 Oct 2016 09:40:46 -0600 Subject: [PATCH 016/191] Started work on addRule method --- Wumpus/src/Tester.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index 62a0e64..7d8c7df 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -1,9 +1,18 @@ public class Tester { + public void addRule(String ruleString) { + ruleString = "Vd,t Facing(d,t) => Facing(d,t+1) OR Action(Turnleft,t) OR Action(TurnRight,t)"; + Rule rule = new Rule(); + if(ruleString.contains("V")){ + String quant = ruleString.substring(ruleString.indexOf("V"), ruleString.indexOf(" ")); + System.out.println(quant); + } + } + public void testInferenceEngine() { InferenceEngine engine = new InferenceEngine(); - + //FORALL x, y, Commutative(x,y) IFF Commutative(y,x) Rule rule = new Rule(); Quantifier x = new Quantifier(); @@ -37,7 +46,14 @@ public void testInferenceEngine() { rule.connector = Rule.IFF; rule.printRule(); - //engine.convertToCNF(null); +// //Vd,t Facing(d,t) => Facing(d,t+1) OR Action(Turnleft,t) OR Action(TurnRight,t) +// Rule rule2 = new Rule(); +// x.variableId = 0; +// y.variableId = 1; +// rule2.quantifiers.add(x); +// rule2.quantifiers.add(y); +// leftFact.predicate = "Facing"; + //engine.convertToCNF(null); } } From eeff2ee554427cfcc13f0ea7e129dafe712dcf55 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 09:47:33 -0600 Subject: [PATCH 017/191] Updates to ReactiveExplorer logic --- Wumpus/src/ReactiveExplorer.java | 65 ++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index c8a3d99..a06a61c 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -3,10 +3,10 @@ public class ReactiveExplorer extends Agent { - private World world; - private int[] currentLocation, previousLocation; + private final World world; + private Position currentPosition, previousPosition; private boolean currentSafe, previousSafe; - private int percepts, direction, arrowCount; + private int percepts, arrowCount; private final byte BREEZE = 0b00000001; private final byte STENTCH = 0b0000010; @@ -16,42 +16,52 @@ public class ReactiveExplorer extends Agent { private final byte DEATH_BY_PIT = 0b00100000; private final byte SCREAM = 0b01000000; - public ReactiveExplorer(World world, int[] location, int direction, int percepts, int arrowCount) { + public ReactiveExplorer(World world, Position startPosition, int percepts, int arrowCount) { this.world = world; this.arrowCount = arrowCount; - this.previousLocation = location; - this.currentLocation = location; + this.previousPosition = startPosition; + this.currentPosition = startPosition; this.percepts = percepts; - this.direction = direction; if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { previousSafe = true; currentSafe = true; } } - private ReactiveExplorer() { - - } - private void move(int action) { - if (action == 1) { + if (action == 1) { //grab gold, game ends + world.action(action); + } else if (action == 2) { //move forward percepts = world.action(action); - if ((percepts & GLITTER) == GLITTER) { //found gold - - } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { - death(DEATH_BY_PIT); - } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - death(DEATH_BY_WUMPUS); - } - if ((percepts & BUMP) != BUMP) { + if ((percepts & BUMP) != BUMP) { //did not bump into anything + previousPosition = currentPosition; previousSafe = currentSafe; - previousLocation = currentLocation; + //update location + int direction = currentPosition.direction; + if (direction == World.NORTH) { //go north + currentPosition.y++; + } else if (direction == World.EAST) { //go east + currentPosition.x++; + } else if (direction == World.SOUTH) { //go south + currentPosition.y--; + } else { //go west + currentPosition.x--; + } } - currentLocation = world.getLocation(); - currentSafe = ((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE); - } else { + } else if (action == 3 || action == 4) { //turn world.action(action); + previousPosition = currentPosition; //there might be a conflict with recording previous states here.. + if (action == 3) { + currentPosition.direction = ++currentPosition.direction % 4; //turn left + } else { + currentPosition.direction = --currentPosition.direction % 4; //turn right + } + } else if (action == 5) { //shoot arrow + world.action(action); + arrowCount--; + } else { //should never reach here + System.out.println("Invalid action: " + action); } } @@ -102,10 +112,9 @@ public void decideNextAction(byte percepts) { } } - private Agent death(byte killer) { + private void death(byte killer) { //there needs to be some record of what killed the agent in this space so it doesnt go back again.. + + currentPosition = previousPosition; - currentLocation = previousLocation; - currentSafe = previousSafe; - } } From 1a3066d3de5868b6f3d53ad8184e8eaf76909da5 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 09:27:46 -0600 Subject: [PATCH 018/191] puncy change --- Wumpus/src/LogicExplorer.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 59937bf..2c97e7c 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -64,7 +64,7 @@ public int decideAction(byte percepts) { return World.GRAB; //grab gold and end game } else if(currentlyNavigatingToSafeSquare){ - //continueNavigation + return continueNavigatingToSafeSquare(); } // else if(){ @@ -96,6 +96,11 @@ else if (kb.ask("Safe(Result(Move,CurrentPosition))&&!Explored(Result(Move,Curre return -1; } + private int continueNavigatingToSafeSquare(){ + //this should basically be RHW Traversal until adjacent to to goal state, then turn to face it + return -1; + } + private void processPosition(byte percepts){ } From 82bd1407782630f7d94d3c762185451745027adf Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Wed, 19 Oct 2016 10:35:26 -0600 Subject: [PATCH 019/191] addRule method works for one rule, continuing generalization --- Wumpus/src/Driver.java | 6 +- Wumpus/src/Quantifier.java | 1 + Wumpus/src/ReactiveExplorer.java | 2 +- Wumpus/src/Sentence.java | 2 +- Wumpus/src/Tester.java | 131 +++++++++++++++++++++++++++++-- Wumpus/src/Unifier.java | 52 ++++++------ 6 files changed, 158 insertions(+), 36 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index f79e037..08a47f4 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -7,9 +7,9 @@ public class Driver { public static void main(String[] args) throws IOException { // Driver driver = new Driver(); -// Tester tester = new Tester(); -// tester.testInferenceEngine(); - Driver.makeGame(); + Tester tester = new Tester(); + tester.testInferenceEngine(); +// Driver.makeGame(); } public static void makeGame() throws IOException { diff --git a/Wumpus/src/Quantifier.java b/Wumpus/src/Quantifier.java index 668c436..88dfba0 100644 --- a/Wumpus/src/Quantifier.java +++ b/Wumpus/src/Quantifier.java @@ -2,6 +2,7 @@ public class Quantifier { int variableId; + char variableRep; boolean isExistential; //if false, assumed universal... boolean not; diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index c8a3d99..bc92825 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -106,6 +106,6 @@ private Agent death(byte killer) { currentLocation = previousLocation; currentSafe = previousSafe; - + return null; } } diff --git a/Wumpus/src/Sentence.java b/Wumpus/src/Sentence.java index 5dbbfef..d1a0377 100644 --- a/Wumpus/src/Sentence.java +++ b/Wumpus/src/Sentence.java @@ -1,5 +1,5 @@ -public interface Sentence extends Clause { +public interface Sentence {//extends Clause { @Override boolean equals(Object object); diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index 7d8c7df..c3c9f73 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -1,18 +1,134 @@ +import java.util.ArrayList; + public class Tester { + + public int[] initialParse(String ruleString){ + int[] initials = new int[3]; + if(ruleString.startsWith("V") || ruleString.startsWith("E")){ + //Has quantifiers + } + return initials; + } - public void addRule(String ruleString) { - ruleString = "Vd,t Facing(d,t) => Facing(d,t+1) OR Action(Turnleft,t) OR Action(TurnRight,t)"; + public Rule addRule(String ruleString) { + ruleString = "Vx,y Commutative(x,y) IFF Commutative(y,x)"; Rule rule = new Rule(); - if(ruleString.contains("V")){ - String quant = ruleString.substring(ruleString.indexOf("V"), ruleString.indexOf(" ")); - System.out.println(quant); + ArrayList quants = new ArrayList(); + if (ruleString.contains("V")) { + String quant = ruleString.substring(ruleString.indexOf("V") + 1, ruleString.indexOf(" ")); + int i = 0; + for (char c : quant.toCharArray()) { + if (c > 64) { + Quantifier q = new Quantifier(); + q.variableRep = c; + q.variableId = i; + quants.add(q); + rule.quantifiers.add(q); + i++; + } + } + ruleString = ruleString.substring(ruleString.indexOf(" ") + 1); + System.out.println(ruleString); + } + + Rule leftRule = new Rule(); + Fact leftFact = new Fact(); + leftFact.predicate = ruleString.substring(0, ruleString.indexOf("(")); + ruleString = ruleString.substring(ruleString.indexOf("(")); + System.out.println(ruleString); + String vars = ruleString.substring(1, ruleString.indexOf(")")); + System.out.println(vars); + int i = 0; + for (char c : vars.toCharArray()) { + if (c > 64) { + Variable v = new Variable(); + if (i + 1 < vars.length() && vars.toCharArray()[i + 1] == 44) { + v.isVariable = true; + v.variableId = checkQuantifiers(quants, c).variableId; + leftFact.variables.add(v); + System.out.println(v.variableId); + } else if (i + 1 == vars.length() && i - 1 > -1 && vars.toCharArray()[i - 1] == 44) { + v.isVariable = true; + v.variableId = checkQuantifiers(quants, c).variableId; + leftFact.variables.add(v); + System.out.println(v.variableId); + } + } + i++; } + leftRule.justFact = true; + leftRule.fact = leftFact; + ruleString = ruleString.substring(ruleString.indexOf(" ") + 1); + System.out.println(ruleString); + + rule.connector = checkConnector(ruleString.substring(0, ruleString.indexOf(" "))); + System.out.println(rule.connector); + + ruleString = ruleString.substring(ruleString.indexOf(" ") + 1); + System.out.println(ruleString); + + Rule rightRule = new Rule(); + Fact rightFact = new Fact(); + leftFact.predicate = ruleString.substring(0, ruleString.indexOf("(")); + ruleString = ruleString.substring(ruleString.indexOf("(")); + System.out.println(ruleString); + vars = ruleString.substring(1, ruleString.indexOf(")")); + System.out.println(vars); + i = 0; + for (char c : vars.toCharArray()) { + if (c > 64) { + Variable v = new Variable(); + if (i + 1 < vars.length() && vars.toCharArray()[i + 1] == 44) { + v.isVariable = true; + v.variableId = checkQuantifiers(quants, c).variableId; + rightFact.variables.add(v); + System.out.println(v.variableId); + } else if (i + 1 == vars.length() && i - 1 > -1 && vars.toCharArray()[i - 1] == 44) { + v.isVariable = true; + v.variableId = checkQuantifiers(quants, c).variableId; + rightFact.variables.add(v); + System.out.println(v.variableId); + } + } + i++; + } + rightRule.justFact = true; + rightRule.fact = leftFact; + System.out.println(ruleString); + + rule.leftRule = leftRule; + rule.rightRule = rightRule; + + return rule; + } + + public Quantifier checkQuantifiers(ArrayList quant, char c) { + for (Quantifier quan : quant) { + if (quan.variableRep == c) { + return quan; + } + } + return null; + } + + public int checkConnector(String connector){ + switch (connector) { + case "AND": + return 1; + case "OR": + return 2; + case "IMPLIES": + return 3; + case "IFF": + return 4; + } + return 0; } public void testInferenceEngine() { InferenceEngine engine = new InferenceEngine(); - + //FORALL x, y, Commutative(x,y) IFF Commutative(y,x) Rule rule = new Rule(); Quantifier x = new Quantifier(); @@ -46,6 +162,9 @@ public void testInferenceEngine() { rule.connector = Rule.IFF; rule.printRule(); + Rule newRule = new Rule(); + newRule = addRule(""); + newRule.printRule(); // //Vd,t Facing(d,t) => Facing(d,t+1) OR Action(Turnleft,t) OR Action(TurnRight,t) // Rule rule2 = new Rule(); diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index e9cd498..9ef6748 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -19,9 +19,10 @@ public static SubstitutionString unify(Variable x, Variable y, SubstitutionStrin return unifyVariable(x, y, theta); } else if (y instanceof Variable) { return unifyVariable(y, x, theta); - } else if (x) { + } //else if (x) { - } + //} + return null; } private static SubstitutionString unifyVariable(Variable x, Variable y, SubstitutionString theta) { @@ -38,17 +39,18 @@ private static SubstitutionString unifyVariable(Variable x, Variable y, Substitu private static boolean occurCheck(Variable x, Variable y) { - if (x.equals(y)) { - return true; - } else if (x instanceof Sentence) { - return x.getOp() == y.getOp() || occurCheck(x, y.getArgs()); - } else if (!(x instanceof Clause)) { - for ( x1 : x) { - - } - } else { - return false; - } +// if (x.equals(y)) { +// return true; +// } else if (x instanceof Sentence) { +// return x.getOp() == y.getOp() || occurCheck(x, y.getArgs()); +//// } else if (!(x instanceof Clause)) { +//// for ( x1 : x) { +//// +//// } +// } else { +// return false; +// } + return true; } private static SubstitutionString extend(SubstitutionString theta, Variable x, Variable y) { @@ -84,25 +86,25 @@ public static boolean unify(Object x, Object y) { private static Object deref(Object object) { - while (object instanceof Variable && ((Variable) object).binding != null) { - object = ((Variable) object).binding; - } +// while (object instanceof Variable && ((Variable) object).binding != null) { +// object = ((Variable) object).binding; +// } return object; } private static void setBinding(Variable var, Object val) { - var.binding = val; - trail.push(var); +// var.binding = val; +// trail.push(var); } private static void undoBindings(Object token) { - Object var; - while ((var = trail.pop()) != token) { - if (var instanceof Variable) { - ((Variable) v).binding = null; - } - } +// Object var; +// while ((var = trail.pop()) != token) { +// if (var instanceof Variable) { +// ((Variable) v).binding = null; +// } +// } } public static SubstitutionString unify(Sentence x, Sentence y) { @@ -128,7 +130,7 @@ private static SubstitutionString unify(Sentence x, Sentence y, SubstitutionStri } private static SubstitutionString unifyVariables(Variable x, Variable y, SubstitutionString theta) { - + return null; } private static SubstitutionString unifyOperators(String x, String y, SubstitutionString theta) { From 1e128b033d68c1a8cb06503ccf52be76a8c80459 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Wed, 19 Oct 2016 10:38:46 -0600 Subject: [PATCH 020/191] Fixed stuff --- Wumpus/src/ReactiveExplorer_BACKUP_7452.java | 126 ------------------- Wumpus/src/ReactiveExplorer_BASE_7452.java | 111 ---------------- Wumpus/src/ReactiveExplorer_LOCAL_7452.java | 111 ---------------- Wumpus/src/ReactiveExplorer_REMOTE_7452.java | 120 ------------------ 4 files changed, 468 deletions(-) delete mode 100644 Wumpus/src/ReactiveExplorer_BACKUP_7452.java delete mode 100644 Wumpus/src/ReactiveExplorer_BASE_7452.java delete mode 100644 Wumpus/src/ReactiveExplorer_LOCAL_7452.java delete mode 100644 Wumpus/src/ReactiveExplorer_REMOTE_7452.java diff --git a/Wumpus/src/ReactiveExplorer_BACKUP_7452.java b/Wumpus/src/ReactiveExplorer_BACKUP_7452.java deleted file mode 100644 index 22db04e..0000000 --- a/Wumpus/src/ReactiveExplorer_BACKUP_7452.java +++ /dev/null @@ -1,126 +0,0 @@ - -import java.util.Random; - -public class ReactiveExplorer extends Agent { - - private final World world; - private Position currentPosition, previousPosition; - private boolean currentSafe, previousSafe; - private int percepts, arrowCount; - - private final byte BREEZE = 0b00000001; - private final byte STENTCH = 0b0000010; - private final byte BUMP = 0b00000100; - private final byte GLITTER = 0b00001000; - private final byte DEATH_BY_WUMPUS = 0b00010000; - private final byte DEATH_BY_PIT = 0b00100000; - private final byte SCREAM = 0b01000000; - - public ReactiveExplorer(World world, Position startPosition, int percepts, int arrowCount) { - this.world = world; - this.arrowCount = arrowCount; - this.previousPosition = startPosition; - this.currentPosition = startPosition; - this.percepts = percepts; - if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { - previousSafe = true; - currentSafe = true; - } - } - - private void move(int action) { - - if (action == 1) { //grab gold, game ends - world.action(action); - } else if (action == 2) { //move forward - percepts = world.action(action); - if ((percepts & BUMP) != BUMP) { //did not bump into anything - previousPosition = currentPosition; - previousSafe = currentSafe; - //update location - int direction = currentPosition.direction; - if (direction == World.NORTH) { //go north - currentPosition.y++; - } else if (direction == World.EAST) { //go east - currentPosition.x++; - } else if (direction == World.SOUTH) { //go south - currentPosition.y--; - } else { //go west - currentPosition.x--; - } - } - } else if (action == 3 || action == 4) { //turn - world.action(action); - previousPosition = currentPosition; //there might be a conflict with recording previous states here.. - if (action == 3) { - currentPosition.direction = ++currentPosition.direction % 4; //turn left - } else { - currentPosition.direction = --currentPosition.direction % 4; //turn right - } - } else if (action == 5) { //shoot arrow - world.action(action); - arrowCount--; - } else { //should never reach here - System.out.println("Invalid action: " + action); - } - } - - public void decideNextAction(byte percepts) { - - //select safe neighboring cell else select unsafe neighboring cell - if (((percepts & STENTCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe - Random random = new Random(); - switch (random.nextInt(3)) { - case 1: //go forward - move(1); - break; - case 2: //turn left and go forward - move(2); - move(1); - break; - case 3: //turn right and go forward - move(3); - move(1); - break; - default: - System.out.println("Should not reach this line."); - } - } else { //neighboring cells may not be safe - //if previous cell was safe, return and pick new direction - if (previousSafe) { - move(3); - move(3); - move(1); - } else { //pick random move - Random random = new Random(); - switch (random.nextInt(3)) { - case 1: //go forward - move(1); - break; - case 2: //turn left and go forward - move(2); - move(1); - break; - case 3: //turn right and go forward - move(3); - move(1); - break; - default: - System.out.println("Should not reach this line."); - } - } - } - } - - private void death(byte killer) { //there needs to be some record of what killed the agent in this space so it doesnt go back again.. - - currentPosition = previousPosition; - -<<<<<<< HEAD - currentLocation = previousLocation; - currentSafe = previousSafe; - return null; -======= ->>>>>>> 1a3066d3de5868b6f3d53ad8184e8eaf76909da5 - } -} diff --git a/Wumpus/src/ReactiveExplorer_BASE_7452.java b/Wumpus/src/ReactiveExplorer_BASE_7452.java deleted file mode 100644 index c8a3d99..0000000 --- a/Wumpus/src/ReactiveExplorer_BASE_7452.java +++ /dev/null @@ -1,111 +0,0 @@ - -import java.util.Random; - -public class ReactiveExplorer extends Agent { - - private World world; - private int[] currentLocation, previousLocation; - private boolean currentSafe, previousSafe; - private int percepts, direction, arrowCount; - - private final byte BREEZE = 0b00000001; - private final byte STENTCH = 0b0000010; - private final byte BUMP = 0b00000100; - private final byte GLITTER = 0b00001000; - private final byte DEATH_BY_WUMPUS = 0b00010000; - private final byte DEATH_BY_PIT = 0b00100000; - private final byte SCREAM = 0b01000000; - - public ReactiveExplorer(World world, int[] location, int direction, int percepts, int arrowCount) { - this.world = world; - this.arrowCount = arrowCount; - this.previousLocation = location; - this.currentLocation = location; - this.percepts = percepts; - this.direction = direction; - if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { - previousSafe = true; - currentSafe = true; - } - } - - private ReactiveExplorer() { - - } - - private void move(int action) { - - if (action == 1) { - percepts = world.action(action); - if ((percepts & GLITTER) == GLITTER) { //found gold - - } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { - death(DEATH_BY_PIT); - } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - death(DEATH_BY_WUMPUS); - } - if ((percepts & BUMP) != BUMP) { - previousSafe = currentSafe; - previousLocation = currentLocation; - } - currentLocation = world.getLocation(); - currentSafe = ((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE); - } else { - world.action(action); - } - } - - public void decideNextAction(byte percepts) { - - //select safe neighboring cell else select unsafe neighboring cell - if (((percepts & STENTCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe - Random random = new Random(); - switch (random.nextInt(3)) { - case 1: //go forward - move(1); - break; - case 2: //turn left and go forward - move(2); - move(1); - break; - case 3: //turn right and go forward - move(3); - move(1); - break; - default: - System.out.println("Should not reach this line."); - } - } else { //neighboring cells may not be safe - //if previous cell was safe, return and pick new direction - if (previousSafe) { - move(3); - move(3); - move(1); - } else { //pick random move - Random random = new Random(); - switch (random.nextInt(3)) { - case 1: //go forward - move(1); - break; - case 2: //turn left and go forward - move(2); - move(1); - break; - case 3: //turn right and go forward - move(3); - move(1); - break; - default: - System.out.println("Should not reach this line."); - } - } - } - } - - private Agent death(byte killer) { - - currentLocation = previousLocation; - currentSafe = previousSafe; - - } -} diff --git a/Wumpus/src/ReactiveExplorer_LOCAL_7452.java b/Wumpus/src/ReactiveExplorer_LOCAL_7452.java deleted file mode 100644 index bc92825..0000000 --- a/Wumpus/src/ReactiveExplorer_LOCAL_7452.java +++ /dev/null @@ -1,111 +0,0 @@ - -import java.util.Random; - -public class ReactiveExplorer extends Agent { - - private World world; - private int[] currentLocation, previousLocation; - private boolean currentSafe, previousSafe; - private int percepts, direction, arrowCount; - - private final byte BREEZE = 0b00000001; - private final byte STENTCH = 0b0000010; - private final byte BUMP = 0b00000100; - private final byte GLITTER = 0b00001000; - private final byte DEATH_BY_WUMPUS = 0b00010000; - private final byte DEATH_BY_PIT = 0b00100000; - private final byte SCREAM = 0b01000000; - - public ReactiveExplorer(World world, int[] location, int direction, int percepts, int arrowCount) { - this.world = world; - this.arrowCount = arrowCount; - this.previousLocation = location; - this.currentLocation = location; - this.percepts = percepts; - this.direction = direction; - if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { - previousSafe = true; - currentSafe = true; - } - } - - private ReactiveExplorer() { - - } - - private void move(int action) { - - if (action == 1) { - percepts = world.action(action); - if ((percepts & GLITTER) == GLITTER) { //found gold - - } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { - death(DEATH_BY_PIT); - } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - death(DEATH_BY_WUMPUS); - } - if ((percepts & BUMP) != BUMP) { - previousSafe = currentSafe; - previousLocation = currentLocation; - } - currentLocation = world.getLocation(); - currentSafe = ((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE); - } else { - world.action(action); - } - } - - public void decideNextAction(byte percepts) { - - //select safe neighboring cell else select unsafe neighboring cell - if (((percepts & STENTCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe - Random random = new Random(); - switch (random.nextInt(3)) { - case 1: //go forward - move(1); - break; - case 2: //turn left and go forward - move(2); - move(1); - break; - case 3: //turn right and go forward - move(3); - move(1); - break; - default: - System.out.println("Should not reach this line."); - } - } else { //neighboring cells may not be safe - //if previous cell was safe, return and pick new direction - if (previousSafe) { - move(3); - move(3); - move(1); - } else { //pick random move - Random random = new Random(); - switch (random.nextInt(3)) { - case 1: //go forward - move(1); - break; - case 2: //turn left and go forward - move(2); - move(1); - break; - case 3: //turn right and go forward - move(3); - move(1); - break; - default: - System.out.println("Should not reach this line."); - } - } - } - } - - private Agent death(byte killer) { - - currentLocation = previousLocation; - currentSafe = previousSafe; - return null; - } -} diff --git a/Wumpus/src/ReactiveExplorer_REMOTE_7452.java b/Wumpus/src/ReactiveExplorer_REMOTE_7452.java deleted file mode 100644 index a06a61c..0000000 --- a/Wumpus/src/ReactiveExplorer_REMOTE_7452.java +++ /dev/null @@ -1,120 +0,0 @@ - -import java.util.Random; - -public class ReactiveExplorer extends Agent { - - private final World world; - private Position currentPosition, previousPosition; - private boolean currentSafe, previousSafe; - private int percepts, arrowCount; - - private final byte BREEZE = 0b00000001; - private final byte STENTCH = 0b0000010; - private final byte BUMP = 0b00000100; - private final byte GLITTER = 0b00001000; - private final byte DEATH_BY_WUMPUS = 0b00010000; - private final byte DEATH_BY_PIT = 0b00100000; - private final byte SCREAM = 0b01000000; - - public ReactiveExplorer(World world, Position startPosition, int percepts, int arrowCount) { - this.world = world; - this.arrowCount = arrowCount; - this.previousPosition = startPosition; - this.currentPosition = startPosition; - this.percepts = percepts; - if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { - previousSafe = true; - currentSafe = true; - } - } - - private void move(int action) { - - if (action == 1) { //grab gold, game ends - world.action(action); - } else if (action == 2) { //move forward - percepts = world.action(action); - if ((percepts & BUMP) != BUMP) { //did not bump into anything - previousPosition = currentPosition; - previousSafe = currentSafe; - //update location - int direction = currentPosition.direction; - if (direction == World.NORTH) { //go north - currentPosition.y++; - } else if (direction == World.EAST) { //go east - currentPosition.x++; - } else if (direction == World.SOUTH) { //go south - currentPosition.y--; - } else { //go west - currentPosition.x--; - } - } - } else if (action == 3 || action == 4) { //turn - world.action(action); - previousPosition = currentPosition; //there might be a conflict with recording previous states here.. - if (action == 3) { - currentPosition.direction = ++currentPosition.direction % 4; //turn left - } else { - currentPosition.direction = --currentPosition.direction % 4; //turn right - } - } else if (action == 5) { //shoot arrow - world.action(action); - arrowCount--; - } else { //should never reach here - System.out.println("Invalid action: " + action); - } - } - - public void decideNextAction(byte percepts) { - - //select safe neighboring cell else select unsafe neighboring cell - if (((percepts & STENTCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe - Random random = new Random(); - switch (random.nextInt(3)) { - case 1: //go forward - move(1); - break; - case 2: //turn left and go forward - move(2); - move(1); - break; - case 3: //turn right and go forward - move(3); - move(1); - break; - default: - System.out.println("Should not reach this line."); - } - } else { //neighboring cells may not be safe - //if previous cell was safe, return and pick new direction - if (previousSafe) { - move(3); - move(3); - move(1); - } else { //pick random move - Random random = new Random(); - switch (random.nextInt(3)) { - case 1: //go forward - move(1); - break; - case 2: //turn left and go forward - move(2); - move(1); - break; - case 3: //turn right and go forward - move(3); - move(1); - break; - default: - System.out.println("Should not reach this line."); - } - } - } - } - - private void death(byte killer) { //there needs to be some record of what killed the agent in this space so it doesnt go back again.. - - currentPosition = previousPosition; - - } -} From fd46503c02ac8874e9de5f7c03d7b4dc6b96b0ec Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 10:38:31 -0600 Subject: [PATCH 021/191] small thing --- Wumpus/src/Agent.java | 10 +++++++--- Wumpus/src/LogicExplorer.java | 5 +++-- Wumpus/src/World.java | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 372b3c1..999a570 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -21,16 +21,20 @@ class Position{ public void moveDidMove(){ switch(direction){ case World.NORTH: + if(y < World.size-1) y+=1; break; case World.EAST: - x-=1; + if(x > 0) + x-=1; break; case World.SOUTH: - y--; + if(y > 0) + y--; break; case World.WEST: - x++; + if(x < World.size-1) + x++; } } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 2c97e7c..fe55b6e 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -14,6 +14,7 @@ public class LogicExplorer extends Agent{ boolean[][] searchedPositions; boolean currentlyNavigatingToSafeSquare; int worldSize; + private Position goalPosition; private final byte BREEZE = 0b00000001; @@ -66,9 +67,9 @@ public int decideAction(byte percepts) { else if(currentlyNavigatingToSafeSquare){ return continueNavigatingToSafeSquare(); } - // else if(){ + else if(kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obsticle(forwardSpot)")){ - //} + } else if (kb.ask("Safe(Result(Move,CurrentPosition))&&!Explored(Result(Move,CurrentPosition))")) { move(2); //move forward } else if (kb.ask("Safe(Result(Move,Result(TurnLeft,CurrentPosition))&&!Explored(Result(Move,Result(TurnLeft,CurrentPosition))")) { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 3edc50a..8f72817 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -5,7 +5,7 @@ public class World { protected int arrowCount; - public int size; + public static int size; private int x; private int y; protected int direction = 0; From 7d9328b95702895497efbcc6288c35da263e5e3a Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 10:49:20 -0600 Subject: [PATCH 022/191] more small changes --- Wumpus/src/LogicExplorer.java | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index fe55b6e..9512376 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -67,31 +67,17 @@ public int decideAction(byte percepts) { else if(currentlyNavigatingToSafeSquare){ return continueNavigatingToSafeSquare(); } - else if(kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obsticle(forwardSpot)")){ + else if(kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")){ } - else if (kb.ask("Safe(Result(Move,CurrentPosition))&&!Explored(Result(Move,CurrentPosition))")) { - move(2); //move forward - } else if (kb.ask("Safe(Result(Move,Result(TurnLeft,CurrentPosition))&&!Explored(Result(Move,Result(TurnLeft,CurrentPosition))")) { - move(3); //turn left - move(2); //move forward - } else if (kb.ask("Safe(Result(Move,Result(TurnRight,CurrentPosition))&&!Explored(Result(Move,Result(TurnRight,CurrentPosition))")) { - move(4); //turn right - move(2); //move forward - } else if (kb.ask("EXIST(s), Safe(s) && !Explored(s)")) { - RHWTraversal("I am adjacent to a space that is unexplored and safe"); - move(2); - } else { - if (arrowCount > 0) { - if (kb.ask("EXIST(s), WUMPUS(s)")) { - RHWTraversal("Facing Wumpus"); - move(5); //shoot arrow - move(2); //move forward - } else { - //explore potentially unsafe space - //we should never reach this step since were only shooting at a Wumpus who's location we know? - } - } + else if("safeSpotInFrontier?" == ""){ + return continueNavigatingToSafeSquare(); + } + else if("KnownWumpusSpotInFrontier" == ""){ + //kill wumpus + } + else{ + //go to random spot in frontier that is not definite death } return -1; From 878ad1ebc2dd635b39b944480b3930452d6c887f Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 11:33:40 -0600 Subject: [PATCH 023/191] Some more changes to ReactiveExplorer, it should be close to functional at this point. No testing has been done yet. --- Wumpus/src/Agent.java | 43 ++++++++------- Wumpus/src/Driver.java | 2 +- Wumpus/src/ReactiveExplorer.java | 92 +++++++++++++++++--------------- 3 files changed, 74 insertions(+), 63 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 999a570..9781dc4 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -1,40 +1,47 @@ -import java.util.ArrayList; +public abstract class Agent { + Position curPos; -public abstract class Agent { - - private void move(int action) { - } - + public void decideNextAction() { - } - - class Position{ + + class Position { + int x; int y; int direction; - - public void moveDidMove(){ - switch(direction){ + + public Position(int x, int y, int direction) { + this.x = x; + this.y = y; + this.direction = direction; + } + + public void moveDidMove() { + switch (direction) { case World.NORTH: - if(y < World.size-1) - y+=1; + if (y < World.size - 1) { + y += 1; + } break; case World.EAST: - if(x > 0) - x-=1; + if (x > 0) { + x -= 1; + } break; case World.SOUTH: - if(y > 0) + if (y > 0) { y--; + } break; case World.WEST: - if(x < World.size-1) + if (x < World.size - 1) { x++; + } } } } diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 08a47f4..979dee0 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -29,7 +29,7 @@ public static void makeGame() throws IOException { WumpusGame game = new WumpusGame(5, prob); World world = new World("PerceptBoard.txt"); - Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); + Agent explorer = new ReactiveExplorer(world); } } diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 22db04e..7708324 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -4,8 +4,8 @@ public class ReactiveExplorer extends Agent { private final World world; - private Position currentPosition, previousPosition; - private boolean currentSafe, previousSafe; + private Position prevPos; + private State curState, prevState; private int percepts, arrowCount; private final byte BREEZE = 0b00000001; @@ -15,16 +15,30 @@ public class ReactiveExplorer extends Agent { private final byte DEATH_BY_WUMPUS = 0b00010000; private final byte DEATH_BY_PIT = 0b00100000; private final byte SCREAM = 0b01000000; + + enum State { + SAFE, + UNSAFE, + EXPLORED; + } - public ReactiveExplorer(World world, Position startPosition, int percepts, int arrowCount) { + public ReactiveExplorer(World world) { this.world = world; - this.arrowCount = arrowCount; - this.previousPosition = startPosition; - this.currentPosition = startPosition; - this.percepts = percepts; + arrowCount = world.arrowCount; + curPos = new Position(world.getLocation()[0], world.getLocation()[1], world.direction); + prevPos = curPos; + percepts = world.getPercepts(); if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { - previousSafe = true; - currentSafe = true; + curState = State.SAFE; + prevState = State.SAFE; + } + run(); + } + + private void run() { + + while(true) { + decideNextAction((byte) percepts); } } @@ -35,27 +49,21 @@ private void move(int action) { } else if (action == 2) { //move forward percepts = world.action(action); if ((percepts & BUMP) != BUMP) { //did not bump into anything - previousPosition = currentPosition; - previousSafe = currentSafe; - //update location - int direction = currentPosition.direction; - if (direction == World.NORTH) { //go north - currentPosition.y++; - } else if (direction == World.EAST) { //go east - currentPosition.x++; - } else if (direction == World.SOUTH) { //go south - currentPosition.y--; - } else { //go west - currentPosition.x--; - } + prevPos = this.curPos; + prevState = curState; + curPos.moveDidMove(); + } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { //killed by a wumpus + death(DEATH_BY_PIT); + } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { //killed by a pit + death(DEATH_BY_PIT); } } else if (action == 3 || action == 4) { //turn world.action(action); - previousPosition = currentPosition; //there might be a conflict with recording previous states here.. + prevPos = curPos; if (action == 3) { - currentPosition.direction = ++currentPosition.direction % 4; //turn left + curPos.direction = ++curPos.direction % 4; //turn left } else { - currentPosition.direction = --currentPosition.direction % 4; //turn right + curPos.direction = --curPos.direction % 4; //turn right } } else if (action == 5) { //shoot arrow world.action(action); @@ -65,43 +73,42 @@ private void move(int action) { } } - public void decideNextAction(byte percepts) { + public void decideNextAction(byte percepts) { //select safe neighboring cell else select unsafe neighboring cell - //select safe neighboring cell else select unsafe neighboring cell if (((percepts & STENTCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe Random random = new Random(); switch (random.nextInt(3)) { - case 1: //go forward + case 1: //go forward move(1); break; - case 2: //turn left and go forward + case 2: //turn left and go forward move(2); move(1); break; - case 3: //turn right and go forward + case 3: //turn right and go forward move(3); move(1); break; default: System.out.println("Should not reach this line."); } - } else { //neighboring cells may not be safe + } else { //neighboring cells may not be safe //if previous cell was safe, return and pick new direction - if (previousSafe) { + if (prevState == State.SAFE) { //there might be a situation where the agent move back and forth between 3 safe safe spaces here we might need to account for move(3); move(3); move(1); - } else { //pick random move + } else { //pick random move Random random = new Random(); switch (random.nextInt(3)) { - case 1: //go forward + case 1: //go forward move(1); break; - case 2: //turn left and go forward + case 2: //turn left and go forward move(2); move(1); break; - case 3: //turn right and go forward + case 3: //turn right and go forward move(3); move(1); break; @@ -114,13 +121,10 @@ public void decideNextAction(byte percepts) { private void death(byte killer) { //there needs to be some record of what killed the agent in this space so it doesnt go back again.. - currentPosition = previousPosition; - -<<<<<<< HEAD - currentLocation = previousLocation; - currentSafe = previousSafe; - return null; -======= ->>>>>>> 1a3066d3de5868b6f3d53ad8184e8eaf76909da5 + Position temp = curPos; + curPos = prevPos; + prevPos = temp; + prevState = State.UNSAFE; + curState = State.EXPLORED; } } From a566f18ed90d1e4aa2680bca4133df0def349067 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 11:11:05 -0600 Subject: [PATCH 024/191] starting follows --- Wumpus/src/InferenceEngine.java | 23 +++++++++++++++++++++++ Wumpus/src/KnowledgeBase.java | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index d42b1b7..d278f75 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -5,6 +5,29 @@ public class InferenceEngine { KnowledgeBase kb; + public void follows(Fact fact){ + Clause clause = new Clause(); + //Step 1: negate input fact + fact.not = !fact.not; + clause.facts.add(fact); + //Step 2: Run negated facts against all known facts + for(Clause kbClause:kb.clauses){ + for(Fact kbFact:kbClause.facts){ + + for(Fact followFact:clause.facts){ + if(kbFact.predicate.equals(followFact.predicate) && kbFact.not == !followFact.not){ + //extend clause with everything in kbClause, remove kbFact and followFact, start over + //before starting over check if clause is empty... + if(clause.facts.isEmpty()) + return;//true + } + } + } + } + //Step 3: If all facts become empty, return true, else if all facts are exhausted, return false + + + } public ArrayList convertToCNF(Rule rule) { ArrayList cnfRules = new ArrayList<>(); ArrayList toConvertRules = new ArrayList<>(); diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 3f88db3..8e53bb2 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -1,6 +1,10 @@ +import java.util.ArrayList; + + public class KnowledgeBase { + ArrayList clauses; public void initializeRules() { //Special predicate: Evaluate From 1385f824c9bcb66681f5958030846e9ba6357088 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 11:28:42 -0600 Subject: [PATCH 025/191] follows done hopefully, at least for a single fact --- Wumpus/src/InferenceEngine.java | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index d278f75..d348f22 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -5,29 +5,47 @@ public class InferenceEngine { KnowledgeBase kb; - public void follows(Fact fact){ + public boolean follows(Fact fact){ Clause clause = new Clause(); //Step 1: negate input fact fact.not = !fact.not; clause.facts.add(fact); + ArrayList kbClausesClone = new ArrayList<>(kb.clauses); + boolean keepGoing = true; //Step 2: Run negated facts against all known facts - for(Clause kbClause:kb.clauses){ + while(!kbClausesClone.isEmpty()){ + keepGoing = true; + for(Clause kbClause:kbClausesClone){ for(Fact kbFact:kbClause.facts){ for(Fact followFact:clause.facts){ if(kbFact.predicate.equals(followFact.predicate) && kbFact.not == !followFact.not){ //extend clause with everything in kbClause, remove kbFact and followFact, start over + kbClause.facts.remove(kbFact); + clause.facts.remove(followFact); + clause.facts.addAll(kbClause.facts); + //before starting over check if clause is empty... if(clause.facts.isEmpty()) - return;//true + return true; + keepGoing = false; } + if(!keepGoing) + break; } + if(!keepGoing) + break; } + if(!keepGoing) + break; } + if(keepGoing)//if we didn't do any changes, keepGoing will be true, if we hit here, then we exhausted every clause comparison with no results, terminate + return false; + } + return false; //Step 3: If all facts become empty, return true, else if all facts are exhausted, return false - - } + public ArrayList convertToCNF(Rule rule) { ArrayList cnfRules = new ArrayList<>(); ArrayList toConvertRules = new ArrayList<>(); From bfad6a8507171f73dc120887d2a807ad545e8962 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 11:44:49 -0600 Subject: [PATCH 026/191] Death is now handled internally, also when ReactiveExplorer gets killed by the wumpus his next action is shoot, and then move to where wumpus was. --- Wumpus/src/ReactiveExplorer.java | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 7708324..a05550c 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -15,8 +15,9 @@ public class ReactiveExplorer extends Agent { private final byte DEATH_BY_WUMPUS = 0b00010000; private final byte DEATH_BY_PIT = 0b00100000; private final byte SCREAM = 0b01000000; - + enum State { + SAFE, UNSAFE, EXPLORED; @@ -34,10 +35,10 @@ public ReactiveExplorer(World world) { } run(); } - + private void run() { - - while(true) { + + while (true) { decideNextAction((byte) percepts); } } @@ -52,10 +53,15 @@ private void move(int action) { prevPos = this.curPos; prevState = curState; curPos.moveDidMove(); - } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { //killed by a wumpus - death(DEATH_BY_PIT); + } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge + move(5); + move(2); } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { //killed by a pit - death(DEATH_BY_PIT); + Position temp = curPos; + curPos = prevPos; + prevPos = temp; + prevState = State.UNSAFE; + curState = State.EXPLORED; } } else if (action == 3 || action == 4) { //turn world.action(action); @@ -118,13 +124,4 @@ public void decideNextAction(byte percepts) { //select safe neighboring ce } } } - - private void death(byte killer) { //there needs to be some record of what killed the agent in this space so it doesnt go back again.. - - Position temp = curPos; - curPos = prevPos; - prevPos = temp; - prevState = State.UNSAFE; - curState = State.EXPLORED; - } } From 1c31e52c986b5d7b4c3ab11ac8f31846db841314 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 11:49:24 -0600 Subject: [PATCH 027/191] World only returns the death percept if agent is killed and does not update the agents position since they will be reseting to the previous state. --- Wumpus/src/World.java | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 8f72817..8f46aa8 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -49,7 +49,7 @@ public void importMap(String fileName) { int j = 0; while (next.contains(" ") && !next.equals(" ")) { perceptMap[i][j] = Integer.parseInt(next.substring(0, next.indexOf(" "))); - + next = next.substring(next.indexOf(" ") + 1, next.length()); j++; } @@ -87,12 +87,28 @@ public int action(int action) { case MOVE: if (direction == NORTH) { if (y + 1 < size) { + if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + //subtract 1000 from score + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + //subtract 1000 from score + return DEATH_BY_PIT; + } y = y + 1; return perceptMap[x][y]; } else { return BUMP; } } else if (direction == EAST) { + if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + //subtract 1000 from score + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + //subtract 1000 from score + return DEATH_BY_PIT; + } if (x + 1 < size) { x = x + 1; return perceptMap[x][y]; @@ -100,6 +116,14 @@ public int action(int action) { return BUMP; } } else if (direction == SOUTH) { + if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + //subtract 1000 from score + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + //subtract 1000 from score + return DEATH_BY_PIT; + } if (y - 1 > 0) { y -= 1; return perceptMap[x][y]; @@ -107,6 +131,14 @@ public int action(int action) { return BUMP; } } else if (direction == WEST) { + if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + //subtract 1000 from score + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + //subtract 1000 from score + return DEATH_BY_PIT; + } if (x - 1 > 0) { x -= 1; return perceptMap[x][y]; From 1729f630ebfb394c6f062b87ce522f3a54f4af95 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 11:55:35 -0600 Subject: [PATCH 028/191] WIP: remove wumpus from board --- Wumpus/src/World.java | 63 ++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 8f46aa8..6b27a4e 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -1,8 +1,9 @@ import java.io.BufferedReader; import java.io.FileReader; +import java.io.IOException; -public class World { +public final class World { protected int arrowCount; public static int size; @@ -56,15 +57,15 @@ public void importMap(String fileName) { i++; } System.out.println(""); - for (int k = 0; k < perceptMap.length; k++) { - for (int j = 0; j < perceptMap[k].length; j++) { - System.out.print(perceptMap[k][j] + " "); + for (int[] row : perceptMap) { + for (int j = 0; j < row.length; j++) { + System.out.print(row[j] + " "); } System.out.println(""); } - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException | NumberFormatException e) { + System.out.println("Exception caught: " + e); } } @@ -102,13 +103,13 @@ public int action(int action) { } } else if (direction == EAST) { if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - //subtract 1000 from score - return DEATH_BY_WUMPUS; - } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - //subtract 1000 from score - return DEATH_BY_PIT; - } + //subtract 1000 from score + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + //subtract 1000 from score + return DEATH_BY_PIT; + } if (x + 1 < size) { x = x + 1; return perceptMap[x][y]; @@ -117,13 +118,13 @@ public int action(int action) { } } else if (direction == SOUTH) { if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - //subtract 1000 from score - return DEATH_BY_WUMPUS; - } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - //subtract 1000 from score - return DEATH_BY_PIT; - } + //subtract 1000 from score + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + //subtract 1000 from score + return DEATH_BY_PIT; + } if (y - 1 > 0) { y -= 1; return perceptMap[x][y]; @@ -132,13 +133,13 @@ public int action(int action) { } } else if (direction == WEST) { if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - //subtract 1000 from score - return DEATH_BY_WUMPUS; - } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - //subtract 1000 from score - return DEATH_BY_PIT; - } + //subtract 1000 from score + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + //subtract 1000 from score + return DEATH_BY_PIT; + } if (x - 1 > 0) { x -= 1; return perceptMap[x][y]; @@ -163,6 +164,7 @@ public int action(int action) { case 1: //shoot north for (int i = y; i < perceptMap.length; i++) { if (perceptMap[x][i] == 16) { //hits Wumpus + removeWumpus(x, i); return SCREAM; } else if (perceptMap[x][i] == 4) { //hits Obstacle return perceptMap[x][y]; @@ -173,6 +175,7 @@ public int action(int action) { case 2: //shoot east for (int i = y; i < perceptMap.length; i++) { if (perceptMap[i][y] == 16) { //hits Wumpus + removeWumpus(i, y); return SCREAM; } else if (perceptMap[i][y] == 4) { //hits Obstacle return perceptMap[x][y]; @@ -183,6 +186,7 @@ public int action(int action) { case 3: //shoot south for (int i = y; i > 0; i--) { if (perceptMap[x][i] == 16) { //hits Wumpus + removeWumpus(x, i); return SCREAM; } else if (perceptMap[x][i] == 4) { //hits Obstacle return perceptMap[x][y]; @@ -193,6 +197,7 @@ public int action(int action) { case 4: //shoot west for (int i = y; i > 0; i--) { if (perceptMap[i][y] == 16) { //hits Wumpus + removeWumpus(i, y); return SCREAM; } else if (perceptMap[i][y] == 4) { //hits Obstacle return perceptMap[x][y]; @@ -211,4 +216,8 @@ public int action(int action) { System.out.println("Error shouldn't ever get here"); return -1; } + + private static void removeWumpus(int x, int y) { + //remove stench percepts form spaces adjacent to wumpus + } } From 700c88b9f31ffdc8e8aa74a1fa5bb25618a653a6 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Wed, 19 Oct 2016 11:57:45 -0600 Subject: [PATCH 029/191] Commit for pull --- Wumpus/src/Driver.java | 2 +- Wumpus/src/ReactiveExplorer.java | 5 ----- Wumpus/src/Tester.java | 5 +++++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 08a47f4..fed7816 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -29,7 +29,7 @@ public static void makeGame() throws IOException { WumpusGame game = new WumpusGame(5, prob); World world = new World("PerceptBoard.txt"); - Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); + //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); } } diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 22db04e..6137d83 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -116,11 +116,6 @@ private void death(byte killer) { //there needs to be some record of what currentPosition = previousPosition; -<<<<<<< HEAD - currentLocation = previousLocation; currentSafe = previousSafe; - return null; -======= ->>>>>>> 1a3066d3de5868b6f3d53ad8184e8eaf76909da5 } } diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index c3c9f73..ad1077b 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -7,7 +7,12 @@ public int[] initialParse(String ruleString){ int[] initials = new int[3]; if(ruleString.startsWith("V") || ruleString.startsWith("E")){ //Has quantifiers + initials[0] = 1; } + else{ + initials[0] = 0; + } + return initials; } From 22b9e62eefe5d39faefeb656b387d17f8c2f9710 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 13:00:48 -0600 Subject: [PATCH 030/191] sync up --- Wumpus/src/LogicExplorer.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 9512376..221a7ab 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -8,6 +8,7 @@ public class LogicExplorer extends Agent{ private final KnowledgeBase kb; private int arrowCount; private int t = 0; + private int previousAction; private Position currentPos; private int currentDirection; private ArrayList frontier = new ArrayList<>(); @@ -89,7 +90,11 @@ private int continueNavigatingToSafeSquare(){ } private void processPosition(byte percepts){ - + if((percepts & BUMP) == 0){//did not bump + if(previousAction == World.MOVE){ + currentPos.moveDidMove(); + } + } } private void updateKB(byte percepts){ From 64ff482ae6ec8c515af9a688c1a3994093b2d67f Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Wed, 19 Oct 2016 13:21:33 -0600 Subject: [PATCH 031/191] Back to unification, with a plan this time! --- Wumpus/src/Clause.java | 8 +++- Wumpus/src/ComplexSentence.java | 49 -------------------- Wumpus/src/Driver.java | 5 +- Wumpus/src/Fact.java | 18 +++++++- Wumpus/src/Sentence.java | 10 ---- Wumpus/src/Unifier.java | 81 ++++++++++++++++----------------- Wumpus/src/Variable.java | 16 +++---- 7 files changed, 75 insertions(+), 112 deletions(-) delete mode 100644 Wumpus/src/ComplexSentence.java delete mode 100644 Wumpus/src/Sentence.java diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index c215cbc..dba7a6f 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -4,7 +4,7 @@ /* A clause contains a list of predicates */ -public class Clause implements Iterable { +public class Clause extends Fact implements Iterable { ArrayList facts = new ArrayList<>(); @@ -13,6 +13,12 @@ public Clause(){} public Clause(Fact fact){ facts.add(fact); } + + @Override + public boolean isUnary() { + return false; + } + @Override public Iterator iterator() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. diff --git a/Wumpus/src/ComplexSentence.java b/Wumpus/src/ComplexSentence.java deleted file mode 100644 index cad08d4..0000000 --- a/Wumpus/src/ComplexSentence.java +++ /dev/null @@ -1,49 +0,0 @@ - -public class ComplexSentence implements Sentence { - - private final Connective connective; - private final Sentence[] sentences; - - public ComplexSentence(Connective connective, Sentence... sentences) { - - this.connective = connective; - this.sentences = new Sentence[sentences.length]; - System.arraycopy(sentences, 0, this.sentences, 0, sentences.length); - } - - public Connective getConnective() { - return this.connective; - } - - public Sentence getSentence(int index) { - return this.sentences[index]; - } - - @Override - public boolean equals(Object object) { - if (this == object) { - return true; - } else if ((object == null) || this.getClass() != object.getClass()) { - return false; - } else { - ComplexSentence sentence = (ComplexSentence) object; - if ((this.connective == sentence.connective)) { - for (int i = 0; i < sentences.length; i ++) { - if (!this.sentences[i].equals(sentence.sentences[i])) { - return false; - } - } - } - } - return true; - } - - @Override - public String toString() { - - //requires implementation - String stringSentence = ""; - - return stringSentence; - } -} diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 979dee0..9369d0c 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -27,9 +27,12 @@ public static void makeGame() throws IOException { System.out.println(""); WumpusGame game = new WumpusGame(5, prob); - World world = new World("PerceptBoard.txt"); + World world = new World("PerceptBoard.txt"); Agent explorer = new ReactiveExplorer(world); + + world = new World("PerceptBoard.txt"); + explorer = new LogicExplorer(world); } } diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index 4009805..6392835 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -1,7 +1,8 @@ import java.util.ArrayList; +import java.util.Iterator; -public class Fact { +public class Fact extends Variable implements Iterable { ArrayList variables = new ArrayList<>(); String predicate; @@ -25,4 +26,19 @@ public void printFact() { } System.out.print(") "); } + + @Override + public boolean isUnary() { + return false; + } + + @Override + public int getOp() { + return operator; + } + + @Override + public Iterator iterator() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } } diff --git a/Wumpus/src/Sentence.java b/Wumpus/src/Sentence.java deleted file mode 100644 index d1a0377..0000000 --- a/Wumpus/src/Sentence.java +++ /dev/null @@ -1,10 +0,0 @@ - -public interface Sentence {//extends Clause { - - @Override - boolean equals(Object object); - - @Override - String toString(); - -} diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 9ef6748..51377e7 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -1,10 +1,6 @@ -import java.util.Stack; - public class Unifier { - private static Stack trail = new Stack(); - public Unifier() { } @@ -106,41 +102,42 @@ private static void undoBindings(Object token) { // } // } } - - public static SubstitutionString unify(Sentence x, Sentence y) { - return unify(x, y, new SubstitutionString()); - } - - private static SubstitutionString unify(Sentence x, Sentence y, SubstitutionString theta) { - if (theta == null) { //return null in case of failure to find substitution - return null; - } else if (x.equals(y)) { //if sentences are equal theta contains the complete substitution - return theta; - } else if (x instanceof Variable) { - // else if VARIABLE?(x) then return UNIVY-VAR(x, y, theta) - return unifyVariables((Variable) x, (Variable) y, theta); - } else if (y instanceof Variable) { - // else if VARIABLE?(y) then return UNIFY-VAR(y, x, theta) - return unifyVariables((Variable) y, (Variable) x, theta); - // } else if () { //if x and y are compound - // return unify(x.getArguments(), y.getArguments(), unifyOperators(getOperator(x), y.getValue(), theta)); - } else { - return null; - } - } - - private static SubstitutionString unifyVariables(Variable x, Variable y, SubstitutionString theta) { - return null; - } - - private static SubstitutionString unifyOperators(String x, String y, SubstitutionString theta) { - - if (theta == null) { - return theta; - } else if (x.equals(y)) { - return theta; - } else { - return null; - } - } -} +// +// public static SubstitutionString unify(Variable x, Variable y) { +// return unify(x, y, new SubstitutionString()); +// } +// +// private static SubstitutionString unify(Variable x, Variable y, SubstitutionString theta) { +// if (theta == null) { //return null in case of failure to find substitution +// return null; +// } else if (x.equals(y)) { //if sentences are equal theta contains the complete substitution +// return theta; +// } else if (x instanceof Variable) { +// // else if VARIABLE?(x) then return UNIVY-VAR(x, y, theta) +// return unifyVariables((Variable) x, (Variable) y, theta); +// } else if (y instanceof Variable) { +// // else if VARIABLE?(y) then return UNIFY-VAR(y, x, theta) +// return unifyVariables((Variable) y, (Variable) x, theta); +// // } else if () { //if x and y are compound +// // return unify(x.getArguments(), y.getArguments(), unifyOperators(getOperator(x), y.getValue(), theta)); +// } else { +// return null; +// } +// } +// +// private static SubstitutionString unifyVariables(Variable x, Variable y, SubstitutionString theta) { +// return null; +// } +// +// private static SubstitutionString unifyOperators(String x, String y, SubstitutionString theta) { +// +// if (theta == null) { +// return theta; +// } else if (x.equals(y)) { +// return theta; +// } else { +// return null; +// } +// } +//} +} \ No newline at end of file diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index 7c5e730..ad6439e 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -1,8 +1,5 @@ -import java.util.Iterator; - - -public class Variable implements Iterable { +public class Variable { boolean isVariable; int value; @@ -25,9 +22,12 @@ public void printVariable() { public int getValue() { return value; } - - @Override - public Iterator iterator() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + + public boolean isUnary() { + return true; + } + + public int getOp() { + return modifier; } } From df52b916e7cc60c8017fca2de301b0978aa05ac4 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Wed, 19 Oct 2016 13:56:04 -0600 Subject: [PATCH 032/191] More unification things --- Wumpus/src/Clause.java | 5 ++ Wumpus/src/Fact.java | 5 ++ Wumpus/src/SubstitutionString.java | 8 ++- Wumpus/src/Unifier.java | 83 ++++++++++++++++++++++++++++++ Wumpus/src/Variable.java | 7 +++ 5 files changed, 107 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index dba7a6f..2347cf3 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -19,6 +19,11 @@ public boolean isUnary() { return false; } + @Override + public ArrayList getArgs() { + return facts; + } + @Override public Iterator iterator() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index 6392835..de22233 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -36,6 +36,11 @@ public boolean isUnary() { public int getOp() { return operator; } + + @Override + public ArrayList getArgs() { + return variables; + } @Override public Iterator iterator() { diff --git a/Wumpus/src/SubstitutionString.java b/Wumpus/src/SubstitutionString.java index f3a0755..fd57d16 100644 --- a/Wumpus/src/SubstitutionString.java +++ b/Wumpus/src/SubstitutionString.java @@ -1,11 +1,17 @@ import java.util.ArrayList; +import java.util.LinkedHashMap; -public class SubstitutionString { +public class SubstitutionString extends LinkedHashMap { private ArrayList string = new ArrayList<>(); + public SubstitutionString() { + + } + + public boolean contains(Variable var) { //returns true if the string contains the variable return true; diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 51377e7..e44e4c3 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -1,10 +1,93 @@ +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.Map; + + public class Unifier { public Unifier() { } + + public SubstitutionString unify(Variable x, Variable y) { + return unify(x, y, (SubstitutionString) new LinkedHashMap<>()); + } + private Map unify(Variable x, Variable y, Map theta) { + + if (theta == null) { + return null; + } else if (x.equals(y)) { + return theta; + } else if (x instanceof Clause) { + return unifyClause((Clause) x, y, theta); + } else if (y instanceof Clause) { + return unifyClause((Clause) y, x, theta); + } else if (!x.isUnary() && !y.isUnary()) { + return unify(x.getArgs(), y.getArgs(), UnifyOps(x.getOp(), y.getOp(), theta)); + } else { + return null; + } + } + + private Map unifyClause(Clause clause, Variable var, Map theta) { + + if (!Fact.class.isInstance(var)) { + return null; + } else if (theta.keySet().contains(clause)) { + return unify(theta.get(clause), var, theta); + } else if (theta.keySet().contains(var)) { + return unify(clause, theta.get(var), theta); + } else if (occurCheck(theta, clause, var)) { + return null; + } else { + cascadeSubstittution(theta, clause, (Fact) var); + return theta; + } + } + + private Map unifyOps(String x, String y, Map theta) { + + if (theta == null) { + return null; + } else if (x.equals(y)) { + return theta; + } else { + return null; + } + } + + private ArrayList args(Variable x) { + + } + + private boolean occurCheck(Map theta, Clause clause, Variable var) { + + if (clause.equals(var)) { + return true; + } else if (!theta.containsKey(var)) { + if (!var.isVariable) { //is function + //recursivly perform occursCheck on each of functions arguments? + if (true) { //tem + return true; + } + } + } else { + return occurCheck(theta, clause, theta.get(var)); + } + return false; + } + + + + + + + + + + public static SubstitutionString unify(Variable x, Variable y, SubstitutionString theta) { if (theta == null) { diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index ad6439e..ae3c52f 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -1,4 +1,7 @@ +import java.util.ArrayList; + + public class Variable { boolean isVariable; @@ -30,4 +33,8 @@ public boolean isUnary() { public int getOp() { return modifier; } + + public ArrayList getArgs() { + return null; + } } From 5f15646a84e46fe889991a1e75f72faabd1b9e72 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Wed, 19 Oct 2016 14:01:16 -0600 Subject: [PATCH 033/191] Unification shit.. --- Wumpus/src/Fact.java | 4 +- Wumpus/src/Unifier.java | 193 +++++---------------------------------- Wumpus/src/Variable.java | 4 +- 3 files changed, 28 insertions(+), 173 deletions(-) diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index de22233..f17d5f1 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -33,8 +33,8 @@ public boolean isUnary() { } @Override - public int getOp() { - return operator; + public String getOp() { + return "" + operator; } @Override diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index e44e4c3..d95ef1d 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -3,19 +3,18 @@ import java.util.LinkedHashMap; import java.util.Map; - public class Unifier { public Unifier() { } - + public SubstitutionString unify(Variable x, Variable y) { return unify(x, y, (SubstitutionString) new LinkedHashMap<>()); } private Map unify(Variable x, Variable y, Map theta) { - + if (theta == null) { return null; } else if (x.equals(y)) { @@ -25,30 +24,35 @@ private Map unify(Variable x, Variable y, Map theta) } else if (y instanceof Clause) { return unifyClause((Clause) y, x, theta); } else if (!x.isUnary() && !y.isUnary()) { - return unify(x.getArgs(), y.getArgs(), UnifyOps(x.getOp(), y.getOp(), theta)); + return unify(x.getArgs(), y.getArgs(), unifyOps(x.getOp(), y.getOp(), theta)); } else { return null; } } - - private Map unifyClause(Clause clause, Variable var, Map theta) { - - if (!Fact.class.isInstance(var)) { + + private Map unifyClause(Clause c, Variable v, Map theta) { + + if (!Fact.class.isInstance(v)) { return null; - } else if (theta.keySet().contains(clause)) { - return unify(theta.get(clause), var, theta); - } else if (theta.keySet().contains(var)) { - return unify(clause, theta.get(var), theta); - } else if (occurCheck(theta, clause, var)) { + } else if (theta.keySet().contains(c)) { + return unify(theta.get(c), v, theta); + } else if (theta.keySet().contains(v)) { + return unify(c, theta.get(v), theta); + } else if (occurCheck(theta, c, v)) { return null; } else { - cascadeSubstittution(theta, clause, (Fact) var); + cascadeSubstitution(theta, c, (Fact) v); return theta; - } + } } - - private Map unifyOps(String x, String y, Map theta) { + + private Map cascadeSubstitution(Map theta, Clause c, Fact f) { + + } + + private Map unifyOps(String x, String y, Map theta) { + if (theta == null) { return null; } else if (x.equals(y)) { @@ -57,13 +61,9 @@ private Map unifyOps(String x, String y, Map theta) return null; } } - - private ArrayList args(Variable x) { - - } - + private boolean occurCheck(Map theta, Clause clause, Variable var) { - + if (clause.equals(var)) { return true; } else if (!theta.containsKey(var)) { @@ -78,149 +78,4 @@ private boolean occurCheck(Map theta, Clause clause, Variable var) } return false; } - - - - - - - - - - - public static SubstitutionString unify(Variable x, Variable y, SubstitutionString theta) { - - if (theta == null) { - return null; - } else if (x.equals(y)) { - return theta; - } else if (x instanceof Variable) { - return unifyVariable(x, y, theta); - } else if (y instanceof Variable) { - return unifyVariable(y, x, theta); - } //else if (x) { - - //} - return null; - } - - private static SubstitutionString unifyVariable(Variable x, Variable y, SubstitutionString theta) { - - if (theta.contains(x)) { - return unify(theta.get(x), y, theta); - } else if (occurCheck(x, y)) { - return null; - } else { - return extend(theta, x, y); - } - } - - - private static boolean occurCheck(Variable x, Variable y) { - -// if (x.equals(y)) { -// return true; -// } else if (x instanceof Sentence) { -// return x.getOp() == y.getOp() || occurCheck(x, y.getArgs()); -//// } else if (!(x instanceof Clause)) { -//// for ( x1 : x) { -//// -//// } -// } else { -// return false; -// } - return true; - } - - private static SubstitutionString extend(SubstitutionString theta, Variable x, Variable y) { - - SubstitutionString temp = theta; - temp.set(x, y); - return temp; - } - public static String Unify(String p, String q) { - - String substitution = ""; - - return substitution; - } - - public static boolean unify(Object x, Object y) { - - x = deref(x); - y = deref(y); - - if (x.equals(y)) { - return true; - } else if (x instanceof Variable) { - setBinding((Variable) x, y); - return true; - } else if (y instanceof Variable) { - setBinding((Variable) y, x); - return true; - } else { - return false; - } - } - - private static Object deref(Object object) { - -// while (object instanceof Variable && ((Variable) object).binding != null) { -// object = ((Variable) object).binding; -// } - return object; - } - - private static void setBinding(Variable var, Object val) { -// var.binding = val; -// trail.push(var); - } - - private static void undoBindings(Object token) { - -// Object var; -// while ((var = trail.pop()) != token) { -// if (var instanceof Variable) { -// ((Variable) v).binding = null; -// } -// } - } -// -// public static SubstitutionString unify(Variable x, Variable y) { -// return unify(x, y, new SubstitutionString()); -// } -// -// private static SubstitutionString unify(Variable x, Variable y, SubstitutionString theta) { -// if (theta == null) { //return null in case of failure to find substitution -// return null; -// } else if (x.equals(y)) { //if sentences are equal theta contains the complete substitution -// return theta; -// } else if (x instanceof Variable) { -// // else if VARIABLE?(x) then return UNIVY-VAR(x, y, theta) -// return unifyVariables((Variable) x, (Variable) y, theta); -// } else if (y instanceof Variable) { -// // else if VARIABLE?(y) then return UNIFY-VAR(y, x, theta) -// return unifyVariables((Variable) y, (Variable) x, theta); -// // } else if () { //if x and y are compound -// // return unify(x.getArguments(), y.getArguments(), unifyOperators(getOperator(x), y.getValue(), theta)); -// } else { -// return null; -// } -// } -// -// private static SubstitutionString unifyVariables(Variable x, Variable y, SubstitutionString theta) { -// return null; -// } -// -// private static SubstitutionString unifyOperators(String x, String y, SubstitutionString theta) { -// -// if (theta == null) { -// return theta; -// } else if (x.equals(y)) { -// return theta; -// } else { -// return null; -// } -// } -//} -} \ No newline at end of file +} diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index ae3c52f..2a48236 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -30,8 +30,8 @@ public boolean isUnary() { return true; } - public int getOp() { - return modifier; + public String getOp() { + return "" + modifier; } public ArrayList getArgs() { From f7ee19e09a724c5a81e36b1c9e33a0ebae211881 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Wed, 19 Oct 2016 16:27:14 -0600 Subject: [PATCH 034/191] Zach gives up on unification for now, left some notes and resources if anyone wants to try, but hes going to focus on other problems and leave this one for later. RIP --- Wumpus/src/Clause.java | 8 +- Wumpus/src/Fact.java | 7 +- Wumpus/src/Unifier.java | 174 +++++++++++++++++++++++++-------------- Wumpus/src/Variable.java | 4 +- 4 files changed, 127 insertions(+), 66 deletions(-) diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 2347cf3..2c920df 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -15,7 +15,13 @@ public Clause(Fact fact){ } @Override - public boolean isUnary() { + public boolean contains(Variable var) { + + for (Fact f : facts) { + if (f.contains(var) || f.equals(var)) { + return true; + } + } return false; } diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index f17d5f1..c6de3a4 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -28,7 +28,12 @@ public void printFact() { } @Override - public boolean isUnary() { + public boolean contains(Variable var) { + for (Variable v : variables) { + if (v.equals(var)) { + return true; + } + } return false; } diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index d95ef1d..eb717e7 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -1,6 +1,7 @@ import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; public class Unifier { @@ -9,73 +10,122 @@ public Unifier() { } - public SubstitutionString unify(Variable x, Variable y) { - return unify(x, y, (SubstitutionString) new LinkedHashMap<>()); - } - - private Map unify(Variable x, Variable y, Map theta) { - - if (theta == null) { - return null; - } else if (x.equals(y)) { + /* + So...Unification sucks, ive tried a ton of different things and followed numerous textbook's psedocodes. + I'm going to leave some notes and resources ive used here and work on something else for the time being. + First off, in a nutshell the unify method needs to take in two sentences the kb sends it. These sentences can be + atomic variables, lists of variables (facts), or lists of facts (clauses). The function needs to return a set of substitutions + that make the two arguments the same, else it returns failure in some fassion (throw an exception maybe). I've tried using HashMaps to store these sets + of substitution bindings, which is what most people do online, but never got it to quite work. + + here are some links that may or may not be useful: + http://web.cecs.pdx.edu/~york/cs441/Luger_Idioms.pdf (part 4, pages ~300 - 330 or so) + https://github.com/aimacode/aima-java this is a repo provided by our textbook that has code implementations for everything. They go about it in a very complicated way though + https://en.wikipedia.org/wiki/Unification_(computer_science) the wiki is super technical, but I did learn a fair amount from it + https://baylor-ir.tdl.org/baylor-ir/bitstream/handle/2104/2887/AshishArteThesis.pdf?sequence=4 this is dense af, but has some good information and explaination of pseduo code + + + */ + + //this is the general outline of how to go about unification... + public static SubstitutionString unify(Variable p, Variable q, SubstitutionString theta) { + + if (p.equals(q)) { //success return theta; - } else if (x instanceof Clause) { - return unifyClause((Clause) x, y, theta); - } else if (y instanceof Clause) { - return unifyClause((Clause) y, x, theta); - } else if (!x.isUnary() && !y.isUnary()) { - return unify(x.getArgs(), y.getArgs(), unifyOps(x.getOp(), y.getOp(), theta)); - } else { - return null; - } - } - - private Map unifyClause(Clause c, Variable v, Map theta) { - - if (!Fact.class.isInstance(v)) { - return null; - } else if (theta.keySet().contains(c)) { - return unify(theta.get(c), v, theta); - } else if (theta.keySet().contains(v)) { - return unify(c, theta.get(v), theta); - } else if (occurCheck(theta, c, v)) { - return null; - } else { - cascadeSubstitution(theta, c, (Fact) v); + } else if (!p.contains(q)) { + //bind p to q and insert (p/q) into theta return theta; - } - } - - private Map cascadeSubstitution(Map theta, Clause c, Fact f) { - - - } - - private Map unifyOps(String x, String y, Map theta) { - - if (theta == null) { - return null; - } else if (x.equals(y)) { + } else if (!q.contains(p)) { + //bind q to p and insert (q/p) into theta return theta; + } else if (true) { //if r is a variable? Occurs checks need to be done here i think + //theta = the union of (theta, {r/s}) + //unify(substitutionOf(theta, p), substitutionOf(theta, q), theta) + } else if (true) { //if s is a variable? Occurs checks need to be done here i think + //theta = the union of (theta, {s/r}) + //unify(substitutionOf(theta, p), substitutionOf(theta, q), theta) } else { - return null; + //failure } + return null; } - - private boolean occurCheck(Map theta, Clause clause, Variable var) { - - if (clause.equals(var)) { - return true; - } else if (!theta.containsKey(var)) { - if (!var.isVariable) { //is function - //recursivly perform occursCheck on each of functions arguments? - if (true) { //tem - return true; - } - } - } else { - return occurCheck(theta, clause, theta.get(var)); - } - return false; + + private static boolean occursCheck(Variable var, Variable term) { + //determins if term is in var + return true; } } + + + +// public SubstitutionString unify(Variable x, Variable y) { +// return (SubstitutionString) unify(x, y, (SubstitutionString) new LinkedHashMap<>()); +// } +// +// private Map unify(Variable x, Variable y, Map theta) { +// +// if (theta == null) { +// return null; +// } else if (x.equals(y)) { +// return theta; +// } else if (x instanceof Clause) { +// return unifyClause((Clause) x, y, theta); +// } else if (y instanceof Clause) { +// return unifyClause((Clause) y, x, theta); +// } else if (!x.isUnary() && !y.isUnary()) { +// // return unify(x.getArgs(), y.getArgs(), unifyOps(x.getOp(), y.getOp(), theta)); +// return theta; +// } else { +// return null; +// } +// } +// +// private Map unifyClause(Clause c, Variable v, Map theta) { +// +// if (!Fact.class.isInstance(v)) { +// return null; +// } else if (theta.keySet().contains(c)) { +// return unify(theta.get(c), v, theta); +// } else if (theta.keySet().contains(v)) { +// return unify(c, theta.get(v), theta); +// } else if (occurCheck(theta, c, v)) { +// return null; +// } else { +// cascadeSubstitution(theta, c, (Fact) v); +// return theta; +// } +// } +// +// private Map cascadeSubstitution(Map theta, Clause c, Fact f) { +// +// return theta; +// } +// +// private Map unifyOps(String x, String y, Map theta) { +// +// if (theta == null) { +// return null; +// } else if (x.equals(y)) { +// return theta; +// } else { +// return null; +// } +// } +// +// private boolean occurCheck(Map theta, Clause clause, Variable var) { +// +// if (clause.equals(var)) { +// return true; +// } else if (!theta.containsKey(var)) { +// if (!var.isVariable) { //is function +// //recursivly perform occursCheck on each of functions arguments? +// if (true) { //tem +// return true; +// } +// } +// } else { +// return occurCheck(theta, clause, theta.get(var)); +// } +// return false; +// } +//} diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index 2a48236..7174a69 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -26,8 +26,8 @@ public int getValue() { return value; } - public boolean isUnary() { - return true; + public boolean contains(Variable var) { + return false; } public String getOp() { From 5be441c330b7d704bfb6e9b542e5ed3001b24100 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 17:12:58 -0600 Subject: [PATCH 035/191] Stuff --- Wumpus/src/KnowledgeBase.java | 6 ++- Wumpus/src/LogicExplorer.java | 88 ++++++++++++++++---------------- Wumpus/src/ReactiveExplorer.java | 6 --- 3 files changed, 48 insertions(+), 52 deletions(-) diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 8e53bb2..be316bd 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -38,7 +38,11 @@ public void initializeRules() { //!Wumpus(x,size)&&!Wumpus(size,y)&&!PIT(z,size)&&!Pit(size,a) } - public boolean ask(String placeholder) { + public boolean ask(String question) { + + //if question follows from known facts ==> return true + //else return false + return true; } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 221a7ab..06c2eeb 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -1,22 +1,19 @@ import java.util.ArrayList; - -public class LogicExplorer extends Agent{ +public class LogicExplorer extends Agent { private final World world; private final KnowledgeBase kb; private int arrowCount; - private int t = 0; + private int t = 0; //time variable private int previousAction; - private Position currentPos; - private int currentDirection; + private Position curPos; private ArrayList frontier = new ArrayList<>(); - boolean[][] searchedPositions; - boolean currentlyNavigatingToSafeSquare; - int worldSize; + private boolean[][] searchedPositions; + private boolean currentlyNavigatingToSafeSquare; + private int worldSize; private Position goalPosition; - private final byte BREEZE = 0b00000001; private final byte STENCH = 0b0000010; @@ -33,6 +30,14 @@ public LogicExplorer(World world) { this.arrowCount = world.arrowCount; this.searchedPositions = new boolean[world.size][world.size]; this.worldSize = world.size; + run(); + } + + private void run() { + + while (true) { + decideNextAction((byte) world.getPercepts()); + } } private void move(int action) { @@ -59,59 +64,55 @@ private String encodePercepts(int percepts) { return sentence; } - public int decideAction(byte percepts) { + private int decideNextAction(byte percepts) { + processPosition(percepts); updateKB(percepts); if ((percepts & GLITTER) != 0) {//maybe just kb.ask("Holding(Gold,Result(Grab,CurrentPosition))"): is better, no percept based logic within agent. return World.GRAB; //grab gold and end game - } - else if(currentlyNavigatingToSafeSquare){ + } else if (currentlyNavigatingToSafeSquare) { // im pretty sure this is all handled by RHW method, so thers no need to call decide next actions while its traversing return continueNavigatingToSafeSquare(); - } - else if(kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")){ - - } - else if("safeSpotInFrontier?" == ""){ + } else if (kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")) { + + } else if ("safeSpotInFrontier?" == "") { return continueNavigatingToSafeSquare(); - } - else if("KnownWumpusSpotInFrontier" == ""){ + } else if ("KnownWumpusSpotInFrontier" == "") { //kill wumpus - } - else{ + } else { //go to random spot in frontier that is not definite death } - + return -1; } - - private int continueNavigatingToSafeSquare(){ + + //i dont think we need this, RHW takes to the space and then turns to face already...its all done in a look so new percepts arent being processed + //since the agent has already been to all of the spaces in will be traveling through + private int continueNavigatingToSafeSquare() { //this should basically be RHW Traversal until adjacent to to goal state, then turn to face it return -1; } - - private void processPosition(byte percepts){ - if((percepts & BUMP) == 0){//did not bump - if(previousAction == World.MOVE){ - currentPos.moveDidMove(); + + private void processPosition(byte percepts) { + if ((percepts & BUMP) == 0) {//did not bump + if (previousAction == World.MOVE) { + curPos.moveDidMove(); } } } - - private void updateKB(byte percepts){ - if((percepts & STENCH) != 0){ + + private void updateKB(byte percepts) { + if ((percepts & STENCH) != 0) { Clause clause = new Clause(); - kb.tell(new Clause(new Fact("Stench", currentPos.x,currentPos.y,t,true)));//Stench(x,y,t) - } - else{ - kb.tell(new Clause(new Fact("Stench", currentPos.x, currentPos.y,t,false)));//!Stench(x,y,t) - } - if((percepts & BREEZE) != 0){ - kb.tell(new Clause(new Fact("Breeze", currentPos.x,currentPos.y,t,true))); + kb.tell(new Clause(new Fact("Stench", curPos.x, curPos.y, t, true)));//Stench(x,y,t) + } else { + kb.tell(new Clause(new Fact("Stench", curPos.x, curPos.y, t, false)));//!Stench(x,y,t) } - else{ - kb.tell(new Clause(new Fact("Breeze", currentPos.x, currentPos.y,t,false))); + if ((percepts & BREEZE) != 0) { + kb.tell(new Clause(new Fact("Breeze", curPos.x, curPos.y, t, true))); + } else { + kb.tell(new Clause(new Fact("Breeze", curPos.x, curPos.y, t, false))); } - if((percepts & SCREAM) != 0){ + if ((percepts & SCREAM) != 0) { //need to deal with this } } @@ -128,9 +129,6 @@ private void RHWTraversal(String stopCondition) { move(3); //turn left } } - //if space to the right is safe --> turn right - //if space to right is unsafe && forward is safe --> go forward - //if space to right is unsafe && forward is unsafe --> turn left } while (!kb.ask(stopCondition)); //face stop condition diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 021ba0a..e87811a 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -17,7 +17,6 @@ public class ReactiveExplorer extends Agent { private final byte SCREAM = 0b01000000; enum State { - SAFE, UNSAFE, EXPLORED; @@ -124,9 +123,4 @@ public void decideNextAction(byte percepts) { //select safe neighboring ce } } } - - private void death(byte killer) { //there needs to be some record of what killed the agent in this space so it doesnt go back again.. - - - } } From 122ff535c8b4a8d1db9efecc2bfcd87014bed82f Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 17:27:25 -0600 Subject: [PATCH 036/191] git gud ryan.. --- Wumpus/src/LogicExplorer.java | 6 +++++- Wumpus/src/ReactiveExplorer.java | 4 +++- Wumpus/src/World.java | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 06c2eeb..faa2e71 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -14,6 +14,7 @@ public class LogicExplorer extends Agent { private boolean currentlyNavigatingToSafeSquare; private int worldSize; private Position goalPosition; + private boolean gameOver = false; private final byte BREEZE = 0b00000001; private final byte STENCH = 0b0000010; @@ -35,7 +36,7 @@ public LogicExplorer(World world) { private void run() { - while (true) { + while (!gameOver) { decideNextAction((byte) world.getPercepts()); } } @@ -43,6 +44,9 @@ private void run() { private void move(int action) { switch (action) { + case 1: + world.action(action); + gameOver = true; case 2: //kb.tell(encodePercepts(world.action(action))); break; diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index e87811a..c842863 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -7,6 +7,7 @@ public class ReactiveExplorer extends Agent { private Position prevPos; private State curState, prevState; private int percepts, arrowCount; + private boolean gameOver = false; private final byte BREEZE = 0b00000001; private final byte STENTCH = 0b0000010; @@ -37,7 +38,7 @@ public ReactiveExplorer(World world) { private void run() { - while (true) { + while (!gameOver) { decideNextAction((byte) percepts); } } @@ -46,6 +47,7 @@ private void move(int action) { if (action == 1) { //grab gold, game ends world.action(action); + gameOver = true; } else if (action == 2) { //move forward percepts = world.action(action); if ((percepts & BUMP) != BUMP) { //did not bump into anything diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 6b27a4e..53f7f72 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -11,6 +11,7 @@ public final class World { private int y; protected int direction = 0; private int[][] perceptMap; + protected int score = 0; public static final int NORTH = 1; public static final int EAST = 2; @@ -84,8 +85,10 @@ public int action(int action) { if ((perceptMap[x][y] & GLITTER) != 0) { perceptMap[x][y] -= GLITTER; } + score += 1000; break; case MOVE: + score--; if (direction == NORTH) { if (y + 1 < size) { if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { From de024dd35a0b6973f65fcfae03d816cc91ecfdac Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 17:35:35 -0600 Subject: [PATCH 037/191] THINGS --- Wumpus/src/LogicExplorer.java | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index faa2e71..518efce 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -48,11 +48,11 @@ private void move(int action) { world.action(action); gameOver = true; case 2: - //kb.tell(encodePercepts(world.action(action))); + kb.tell(encodePercepts(world.action(action)));//kb.tell(encodePercepts(world.action(action))); break; case 5: arrowCount--; - world.action(action); + kb.tell(encodePercepts(world.action(action))); break; default: world.action(action); @@ -60,33 +60,34 @@ private void move(int action) { } } - private String encodePercepts(int percepts) { + private Clause encodePercepts(int percepts) { - String sentence = ""; + Clause clause = new Clause(); //conversion logic goes here - return sentence; + return clause; } - private int decideNextAction(byte percepts) { - + private void decideNextAction(byte percepts) { + processPosition(percepts); updateKB(percepts); if ((percepts & GLITTER) != 0) {//maybe just kb.ask("Holding(Gold,Result(Grab,CurrentPosition))"): is better, no percept based logic within agent. - return World.GRAB; //grab gold and end game + //return World.GRAB; //grab gold and end game + move(1); } else if (currentlyNavigatingToSafeSquare) { // im pretty sure this is all handled by RHW method, so thers no need to call decide next actions while its traversing - return continueNavigatingToSafeSquare(); + //return continueNavigatingToSafeSquare(); } else if (kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")) { - + move(2); } else if ("safeSpotInFrontier?" == "") { - return continueNavigatingToSafeSquare(); + RHWTraversal("!Wumpus(adjacent)AND!Pit(adjacent)"); + //return continueNavigatingToSafeSquare(); } else if ("KnownWumpusSpotInFrontier" == "") { //kill wumpus + RHWTraversal("Wumpus(adjacent)"); } else { //go to random spot in frontier that is not definite death } - - return -1; } //i dont think we need this, RHW takes to the space and then turns to face already...its all done in a look so new percepts arent being processed From 47c90c8d85da53f740f6066b6d451089adb280f3 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 18:44:17 -0600 Subject: [PATCH 038/191] Things --- Wumpus/src/Connective.java | 20 ----------- Wumpus/src/ReactiveExplorer.java | 2 +- Wumpus/src/World.java | 60 ++++++++------------------------ 3 files changed, 16 insertions(+), 66 deletions(-) delete mode 100644 Wumpus/src/Connective.java diff --git a/Wumpus/src/Connective.java b/Wumpus/src/Connective.java deleted file mode 100644 index a99c1e6..0000000 --- a/Wumpus/src/Connective.java +++ /dev/null @@ -1,20 +0,0 @@ - -public enum Connective { - - NOT("~"), - AND("&"), - OR("|"), - IMPLICATION("=>"), - BICONDITIONAL("<=>"); - - private final String symbol; - - @Override - public String toString() { - return symbol; - } - - private Connective(String symbol) { - this.symbol = symbol; - } -} diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index c842863..9bf3dab 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -26,7 +26,7 @@ enum State { public ReactiveExplorer(World world) { this.world = world; arrowCount = world.arrowCount; - curPos = new Position(world.getLocation()[0], world.getLocation()[1], world.direction); + curPos = new Position(world.x, world.y, world.direction); prevPos = curPos; percepts = world.getPercepts(); if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 53f7f72..d2f1c3c 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -5,36 +5,15 @@ public final class World { - protected int arrowCount; + public static final int NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4; + public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5; + public static final int BREEZE = 1, STENTCH = 2, BUMP = 4, GLITTER = 8, DEATH_BY_WUMPUS = 16, DEATH_BY_PIT = 32, SCREAM = 64; + + protected int arrowCount, x, y, direction = 0, score = 0; public static int size; - private int x; - private int y; - protected int direction = 0; private int[][] perceptMap; - protected int score = 0; - - public static final int NORTH = 1; - public static final int EAST = 2; - public static final int SOUTH = 3; - public static final int WEST = 4; - private final int BREEZE = 1; - private final int STENTCH = 2; - private final int BUMP = 4; - private final int GLITTER = 8; - private final int DEATH_BY_WUMPUS = 16; - private final int DEATH_BY_PIT = 32; - private final int SCREAM = 64; - - static final int GRAB = 1; - static final int MOVE = 2; - static final int TURN_LEFT = 3; - static final int TURN_RIGHT = 4; - static final int SHOOT = 5; - final int END = 6;//needed? public World(String fileName) { - x = 0; - y = 0; importMap(fileName); //read in file } @@ -44,7 +23,7 @@ public void importMap(String fileName) { FileReader in = new FileReader(fileName); BufferedReader reader = new BufferedReader(in); String next; - int size = Integer.parseInt(reader.readLine()); + size = Integer.parseInt(reader.readLine()); perceptMap = new int[size][size]; int i = 0; while ((next = reader.readLine()) != null) {//((Integer) reader.read()).toString()).equals("-1")) { @@ -64,17 +43,11 @@ public void importMap(String fileName) { } System.out.println(""); } - } catch (IOException | NumberFormatException e) { System.out.println("Exception caught: " + e); } } - public int[] getLocation() { - int[] location = {x, y}; - return location; - } - public int getPercepts() { return perceptMap[x][y]; } @@ -92,11 +65,11 @@ public int action(int action) { if (direction == NORTH) { if (y + 1 < size) { if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - //subtract 1000 from score + score -= 1000; return DEATH_BY_WUMPUS; } if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - //subtract 1000 from score + score -= 1000; return DEATH_BY_PIT; } y = y + 1; @@ -106,11 +79,11 @@ public int action(int action) { } } else if (direction == EAST) { if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - //subtract 1000 from score + score -= 1000; return DEATH_BY_WUMPUS; } if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - //subtract 1000 from score + score -= 1000; return DEATH_BY_PIT; } if (x + 1 < size) { @@ -121,11 +94,11 @@ public int action(int action) { } } else if (direction == SOUTH) { if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - //subtract 1000 from score + score -= 1000; return DEATH_BY_WUMPUS; } if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - //subtract 1000 from score + score -= 1000; return DEATH_BY_PIT; } if (y - 1 > 0) { @@ -136,11 +109,11 @@ public int action(int action) { } } else if (direction == WEST) { if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - //subtract 1000 from score + score -= 1000; return DEATH_BY_WUMPUS; } if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - //subtract 1000 from score + score -= 1000; return DEATH_BY_PIT; } if (x - 1 > 0) { @@ -211,16 +184,13 @@ public int action(int action) { default: System.out.println("Error in shooting logic."); } - - case END: - //needed? } - System.out.println("Error shouldn't ever get here"); return -1; } private static void removeWumpus(int x, int y) { //remove stench percepts form spaces adjacent to wumpus + } } From fee2a9a191ca17edf82491f6f7b3325b7cbf55fc Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 19:10:59 -0600 Subject: [PATCH 039/191] Removed Skolem (fk that guy), world now updates percept map when wumpus is killed. Added score. --- Wumpus/src/MinusFunction.java | 1 - Wumpus/src/PlusFunction.java | 1 - Wumpus/src/SkolemFunction.java | 20 -------------------- Wumpus/src/Substitute.java | 9 --------- Wumpus/src/Variable.java | 1 - Wumpus/src/World.java | 23 +++++++++++++++++++++-- 6 files changed, 21 insertions(+), 34 deletions(-) delete mode 100644 Wumpus/src/SkolemFunction.java diff --git a/Wumpus/src/MinusFunction.java b/Wumpus/src/MinusFunction.java index c6e3469..fcd1176 100644 --- a/Wumpus/src/MinusFunction.java +++ b/Wumpus/src/MinusFunction.java @@ -10,7 +10,6 @@ public int process(int value) { public Variable processVariable(Variable variable) { Variable processedVariable = new Variable(); processedVariable.function = variable.function; - processedVariable.isSkolemConstant = variable.isSkolemConstant; processedVariable.isVariable = variable.isVariable; processedVariable.modifier = variable.modifier - 1;//This is the important one processedVariable.value = variable.value; diff --git a/Wumpus/src/PlusFunction.java b/Wumpus/src/PlusFunction.java index 849f640..07806d6 100644 --- a/Wumpus/src/PlusFunction.java +++ b/Wumpus/src/PlusFunction.java @@ -10,7 +10,6 @@ public int process(int value) { public Variable processVariable(Variable variable) { Variable processedVariable = new Variable(); processedVariable.function = variable.function; - processedVariable.isSkolemConstant = variable.isSkolemConstant; processedVariable.isVariable = variable.isVariable; processedVariable.modifier = variable.modifier + 1;//This is the important one processedVariable.value = variable.value; diff --git a/Wumpus/src/SkolemFunction.java b/Wumpus/src/SkolemFunction.java deleted file mode 100644 index ec64ab3..0000000 --- a/Wumpus/src/SkolemFunction.java +++ /dev/null @@ -1,20 +0,0 @@ - -public class SkolemFunction implements IFunction { - - Variable var; - - public SkolemFunction(Variable variable) { - var = variable; - } - - @Override - public int process(int value) { - return value;//idk yet - } - - @Override - public Variable processVariable(Variable variable) { - return var; - - } -} diff --git a/Wumpus/src/Substitute.java b/Wumpus/src/Substitute.java index f0790f0..2d450c9 100644 --- a/Wumpus/src/Substitute.java +++ b/Wumpus/src/Substitute.java @@ -1,13 +1,4 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -/** - * - * @author Wilson - */ public class Substitute { int varIdToSubstitute; int valToSubstituteWith; diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index 7174a69..e5261a7 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -9,7 +9,6 @@ public class Variable { IFunction function; int variableId; int modifier; - boolean isSkolemConstant;//Skolem function would just be the function, a skolem constant will have this and it's varId making a unique identifier public Variable(){} public Variable(int value){ diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index d2f1c3c..4e0d3c9 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -189,8 +189,27 @@ public int action(int action) { return -1; } - private static void removeWumpus(int x, int y) { + private void removeWumpus(int x, int y) { + + //remove death_by_wumpus percepts from x, y + perceptMap[x][y] = perceptMap[x][y] & ~DEATH_BY_WUMPUS; + //remove stench percepts form spaces adjacent to wumpus - + if (x > 0) { + //remove stentch to left + perceptMap[x - 1][y] = perceptMap[x - 1][y] & ~STENTCH; + if (x < size - 1) { + //remove stentch to right + perceptMap[x + 1][y] = perceptMap[x + 1][y] & ~STENTCH; + } + } + if (y > 0) { + //remove stentch below + perceptMap[x][y - 1] = perceptMap[x][y - 1] & ~STENTCH; + if (y < size - 1) { + //remove stentch above + perceptMap[x][y + 1] = perceptMap[x][y + 1] & ~STENTCH; + } + } } } From b0b743ee355957d1366043e27ab8ef46bbe6d546 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 18:50:08 -0600 Subject: [PATCH 040/191] rules commented --- Wumpus/src/Fact.java | 7 +++-- Wumpus/src/KnowledgeBase.java | 50 +++++++++++++++-------------------- Wumpus/src/LogicExplorer.java | 11 ++++---- Wumpus/src/Variable.java | 3 ++- 4 files changed, 31 insertions(+), 40 deletions(-) diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index c6de3a4..16da54c 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -11,10 +11,9 @@ public class Fact extends Variable implements Iterable { boolean not; public Fact(){} - public Fact(String predicate, int var1Val, int var2Val, int var3Val, boolean not){ - Variable var1 = new Variable(var1Val); - Variable var2 = new Variable(var2Val); - Variable var3 = new Variable(var3Val); + public Fact(String predicate, int var1Val, int var2Val, boolean not, IFunction var1Function, IFunction var2Function){ + Variable var1 = new Variable(var1Val, var1Function); + Variable var2 = new Variable(var2Val, var2Function); this.predicate = predicate; this.not = not; } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index be316bd..27b2b16 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -7,35 +7,25 @@ public class KnowledgeBase { ArrayList clauses; public void initializeRules() { //Special predicate: Evaluate - - //Percepts come in as Glitter(t) Stench(t) Breeze(t) Bump(t) - //Vx,y,a,b Adjacent(x,y,a,b) iff [a,b] in {(x-1,y),(x+1,y),(x,y-1),(x,y+1)} - //Vd,t Facing(d,t) => Facing(d,t+1) OR Action(Turnleft,t) OR Action(TurnRight,t) - //Vt,v Facing(d,t) AND Action(Turnleft,t)=>Facing((d-1)%4, t+1) - - - //Vt,v Facing(d,t) AND Action(TurnRight,t)=>Facing((d+1)%4,t+1) - //Vt,x Arrows(x,t) => Arrows(x,t+1) OR Action(SHOOT,t) - //Vt,x HasArrow(t) iff Arrows(x,t) && x>0 - //Vt,x Action(SHOOT,t) AND Arrows(x,t)AND HasArrow(t) => Arrows(x-1,t+1) - //Vt At(Agent,x,y,t) && Facing(NORTH,t)&&Action(SHOOT,t)=>(Exists(a) st Scream(t+1) AND WumpusDead(x+a,y - //Vt,x Action(x,t) => !(Action(y,t) AND True(EVALUATE(x,y,NOTEQUALS)) - //True(1) - //!True(0) - //percept rules: - //At(Agent, x, y, t) && Stench(x,y,t) => At(Stink,x,y,t) - //Stink(x,y,t) iff Exists(a,b) st Wumpus(a,b,t) ^ Adjacent(x,y,a,b) - //Wumpus(x,y,t) => Wumpus(x,y,t+1) XOR WumpusDead(x,y,t+1) p XOR q is (pVq)&&(!(p&&q)) - //WumpusDead(x,y,t)=>WumpusDead(x,y,t+1) - //Stench(x,y)=>Wumpus(x-1,y)ORWumpus(x+1,y)ORWumpus(x,y-1)ORWumpus(x,y+1) - - //Breeze mimics this - //Glitter(x,y,t)=>Gold(x,y,t) - //Bump(x,y)=>Obsticle(x,y) - //action(Move - //(!Wumpus(x,y)&&!Pit(x,y))=>Safe(x,y) - //!Wumpus(-1,y)&&!Wumpus(x,-1)&&!Pit(-1,z)&&!Pit(-1,a)//no boarder things - //!Wumpus(x,size)&&!Wumpus(size,y)&&!PIT(z,size)&&!Pit(size,a) + + //Stench(x,y)=>(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) + //!Stench(x,y) v Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) + Fact fact = new Fact(); + fact.predicate = "Stench"; + fact.not = true; + + //Breeze(x,y)=>(Pit(x-1,y) v Pit(x-1,y) v Pit(x,y-1) v Pit(x, y+1) + //!Breeze(x,y) v Pit(x-1,y) v Pit(x-1,y) v Pit(x,y-1) v Pit(x, y+1) + + //!Wumpus(-1,y) ^ !Wumpus(x,-1) ^ !Wumpus(x,World.size)^ !Wumpus(World.size,y) + //!Wumpus(-1,y), !Wumpus(x,-1), !Wumpus(x,World.size), !Wumpus(World.size,y) + + //!Pit(-1,y), !Pit(x,-1), !Pit(x,World.size), !Wumpus(World.size,y) + + //Wumpus(x,y)<=>!Pit(x,y) + //(!Wumpus(x,y) v !Pit(x,y)) ^ (Pit(x,y) v Wumpus(x,y)) + + } public boolean ask(String question) { @@ -47,5 +37,7 @@ public boolean ask(String question) { } public void tell(Clause clause) { + //If told DeadWumpus delete the wumpus entry at that position and add !Wumpus(x,y) + //and check if there is a stench at any adjacent position, remove those facts too } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 518efce..f788831 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -20,8 +20,7 @@ public class LogicExplorer extends Agent { private final byte STENCH = 0b0000010; private final byte BUMP = 0b00000100; private final byte GLITTER = 0b00001000; - private final byte DEATH_BY_WUMPUS = 0b00010000; - private final byte DEATH_BY_PIT = 0b00100000; + private final byte DEATH = 0b00010000; private final byte SCREAM = 0b01000000; public LogicExplorer(World world) { @@ -108,14 +107,14 @@ private void processPosition(byte percepts) { private void updateKB(byte percepts) { if ((percepts & STENCH) != 0) { Clause clause = new Clause(); - kb.tell(new Clause(new Fact("Stench", curPos.x, curPos.y, t, true)));//Stench(x,y,t) + kb.tell(new Clause(new Fact("Stench", curPos.x, curPos.y, true, null, null)));//Stench(x,y,t) } else { - kb.tell(new Clause(new Fact("Stench", curPos.x, curPos.y, t, false)));//!Stench(x,y,t) + kb.tell(new Clause(new Fact("Stench", curPos.x, curPos.y, false, null, null)));//!Stench(x,y,t) } if ((percepts & BREEZE) != 0) { - kb.tell(new Clause(new Fact("Breeze", curPos.x, curPos.y, t, true))); + kb.tell(new Clause(new Fact("Breeze", curPos.x, curPos.y, true, null, null))); } else { - kb.tell(new Clause(new Fact("Breeze", curPos.x, curPos.y, t, false))); + kb.tell(new Clause(new Fact("Breeze", curPos.x, curPos.y, false, null, null))); } if ((percepts & SCREAM) != 0) { //need to deal with this diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index e5261a7..6928745 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -11,8 +11,9 @@ public class Variable { int modifier; public Variable(){} - public Variable(int value){ + public Variable(int value, IFunction function){ this.value = value; + this.function = function; } public void printVariable() { From a61d3fe9dec84dece80a075bb7507680cc2391ba Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 19:14:28 -0600 Subject: [PATCH 041/191] Rules encoded --- Wumpus/src/Fact.java | 6 ++--- Wumpus/src/KnowledgeBase.java | 50 +++++++++++++++++++++++++++++------ Wumpus/src/LogicExplorer.java | 8 +++--- Wumpus/src/Variable.java | 8 ++++-- 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index 16da54c..ba6c6e6 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -11,9 +11,9 @@ public class Fact extends Variable implements Iterable { boolean not; public Fact(){} - public Fact(String predicate, int var1Val, int var2Val, boolean not, IFunction var1Function, IFunction var2Function){ - Variable var1 = new Variable(var1Val, var1Function); - Variable var2 = new Variable(var2Val, var2Function); + public Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function){ + Variable var1 = new Variable(var1Val, var1Var, var1Function); + Variable var2 = new Variable(var2Val, var2Var, var2Function); this.predicate = predicate; this.not = not; } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 27b2b16..4a5aea0 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -6,26 +6,60 @@ public class KnowledgeBase { ArrayList clauses; public void initializeRules() { + //Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function) //Special predicate: Evaluate //Stench(x,y)=>(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) //!Stench(x,y) v Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) - Fact fact = new Fact(); - fact.predicate = "Stench"; - fact.not = true; + Clause stench = new Clause(); + Fact stenchXY = new Fact("Stench",0,true,1,true,true,null,null); + Fact wumpusxminy = new Fact("Wumpus",0,true,1,true,false,new MinusFunction(),null); + Fact wumpusxplusy = new Fact("Wumpus",0,true,1,true,false,new PlusFunction(),null); + Fact wumpusxymin = new Fact("Wumpus",0,true,1,true,false,null,new MinusFunction()); + Fact wumpusxyplus = new Fact("Wumpus",0,true,1,true,false,null,new PlusFunction()); + stench.facts.add(stenchXY); + stench.facts.add(wumpusxminy); + stench.facts.add(wumpusxplusy); + stench.facts.add(wumpusxymin); + stench.facts.add(wumpusxyplus); + clauses.add(stench); + //Breeze(x,y)=>(Pit(x-1,y) v Pit(x-1,y) v Pit(x,y-1) v Pit(x, y+1) //!Breeze(x,y) v Pit(x-1,y) v Pit(x-1,y) v Pit(x,y-1) v Pit(x, y+1) + Clause breeze = new Clause(); + Fact breezehXY = new Fact("Breeze",0,true,1,true,true,null,null); + Fact pitxminy = new Fact("Pit",0,true,1,true,false,new MinusFunction(),null); + Fact pitxplusy = new Fact("Pit",0,true,1,true,false,new PlusFunction(),null); + Fact pitxymin = new Fact("Pit",0,true,1,true,false,null,new MinusFunction()); + Fact pitxyplus = new Fact("Pit",0,true,1,true,false,null,new PlusFunction()); + breeze.facts.add(stenchXY); + breeze.facts.add(wumpusxminy); + breeze.facts.add(wumpusxplusy); + breeze.facts.add(wumpusxymin); + breeze.facts.add(wumpusxyplus); + clauses.add(breeze); //!Wumpus(-1,y) ^ !Wumpus(x,-1) ^ !Wumpus(x,World.size)^ !Wumpus(World.size,y) //!Wumpus(-1,y), !Wumpus(x,-1), !Wumpus(x,World.size), !Wumpus(World.size,y) - + clauses.add(new Clause(new Fact("Wumpus",-1,false,1,true,true,null,null))); + clauses.add(new Clause(new Fact("Wumpus",0,true,-1,false,true,null,null))); + clauses.add(new Clause(new Fact("Wumpus",0,true,World.size,false,true,null,null))); + clauses.add(new Clause(new Fact("Wumpus",World.size,false,1,true,true,null,null))); //!Pit(-1,y), !Pit(x,-1), !Pit(x,World.size), !Wumpus(World.size,y) + clauses.add(new Clause(new Fact("Pit",-1,false,1,true,true,null,null))); + clauses.add(new Clause(new Fact("Pit",0,true,-1,false,true,null,null))); + clauses.add(new Clause(new Fact("Pit",0,true,World.size,false,true,null,null))); + clauses.add(new Clause(new Fact("Pit",World.size,false,1,true,true,null,null))); - //Wumpus(x,y)<=>!Pit(x,y) - //(!Wumpus(x,y) v !Pit(x,y)) ^ (Pit(x,y) v Wumpus(x,y)) - - + //Wumpus(x,y)=>!Pit(x,y) + //!Wumpus(x,y) v !Pit(x,y) + Clause notWumpusOrNotPit = new Clause(); + Fact notWumpus = new Fact("Wumpus",0,true,1,true,true,null,null); + Fact notPit = new Fact("Pit",0,true,1,true,true,null,null); + notWumpusOrNotPit.facts.add(notWumpus); + notWumpusOrNotPit.facts.add(notPit); + clauses.add(notWumpusOrNotPit); } public boolean ask(String question) { diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index f788831..faca6bf 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -107,14 +107,14 @@ private void processPosition(byte percepts) { private void updateKB(byte percepts) { if ((percepts & STENCH) != 0) { Clause clause = new Clause(); - kb.tell(new Clause(new Fact("Stench", curPos.x, curPos.y, true, null, null)));//Stench(x,y,t) + kb.tell(new Clause(new Fact("Stench", curPos.x, false, curPos.y,false, true, null, null)));//Stench(x,y,t) } else { - kb.tell(new Clause(new Fact("Stench", curPos.x, curPos.y, false, null, null)));//!Stench(x,y,t) + kb.tell(new Clause(new Fact("Stench", curPos.x, false, curPos.y,false, false, null, null)));//!Stench(x,y,t) } if ((percepts & BREEZE) != 0) { - kb.tell(new Clause(new Fact("Breeze", curPos.x, curPos.y, true, null, null))); + kb.tell(new Clause(new Fact("Breeze", curPos.x, false, curPos.y,false, true, null, null))); } else { - kb.tell(new Clause(new Fact("Breeze", curPos.x, curPos.y, false, null, null))); + kb.tell(new Clause(new Fact("Breeze", curPos.x, false, curPos.y,false, false, null, null))); } if ((percepts & SCREAM) != 0) { //need to deal with this diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index 6928745..0388fdd 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -11,8 +11,12 @@ public class Variable { int modifier; public Variable(){} - public Variable(int value, IFunction function){ - this.value = value; + public Variable(int value, boolean isVariable, IFunction function){ + this.isVariable = isVariable; + if(!isVariable) + this.value = value; + else + this.variableId = value; this.function = function; } public void printVariable() { From 4f7aef99aaaed9207ec28fc0955a523340d2ed92 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 19:44:01 -0600 Subject: [PATCH 042/191] Idk what this is, i changed some things and then forgot. Im useful! --- Wumpus/src/LogicExplorer.java | 13 +++++--- Wumpus/src/ReactiveExplorer.java | 56 ++++++++++++++++---------------- Wumpus/src/World.java | 10 ++++-- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index faca6bf..13c2d22 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -12,7 +12,7 @@ public class LogicExplorer extends Agent { private ArrayList frontier = new ArrayList<>(); private boolean[][] searchedPositions; private boolean currentlyNavigatingToSafeSquare; - private int worldSize; + private final int worldSize; private Position goalPosition; private boolean gameOver = false; @@ -28,8 +28,8 @@ public LogicExplorer(World world) { kb = new KnowledgeBase(); kb.initializeRules(); this.arrowCount = world.arrowCount; - this.searchedPositions = new boolean[world.size][world.size]; - this.worldSize = world.size; + this.searchedPositions = new boolean[World.size][World.size]; + this.worldSize = World.size; run(); } @@ -47,7 +47,7 @@ private void move(int action) { world.action(action); gameOver = true; case 2: - kb.tell(encodePercepts(world.action(action)));//kb.tell(encodePercepts(world.action(action))); + kb.tell(encodePercepts(world.action(action))); break; case 5: arrowCount--; @@ -69,6 +69,9 @@ private Clause encodePercepts(int percepts) { private void decideNextAction(byte percepts) { + if (frontier.isEmpty()) { + move(World.QUIT); + } processPosition(percepts); updateKB(percepts); if ((percepts & GLITTER) != 0) {//maybe just kb.ask("Holding(Gold,Result(Grab,CurrentPosition))"): is better, no percept based logic within agent. @@ -77,7 +80,7 @@ private void decideNextAction(byte percepts) { } else if (currentlyNavigatingToSafeSquare) { // im pretty sure this is all handled by RHW method, so thers no need to call decide next actions while its traversing //return continueNavigatingToSafeSquare(); } else if (kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")) { - move(2); + move(World.MOVE); } else if ("safeSpotInFrontier?" == "") { RHWTraversal("!Wumpus(adjacent)AND!Pit(adjacent)"); //return continueNavigatingToSafeSquare(); diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 9bf3dab..bef0fb9 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -38,44 +38,44 @@ public ReactiveExplorer(World world) { private void run() { - while (!gameOver) { - decideNextAction((byte) percepts); + while (true) { + decideNextAction(); } } private void move(int action) { - if (action == 1) { //grab gold, game ends + if (action == World.GRAB) { //grab gold, game ends world.action(action); gameOver = true; - } else if (action == 2) { //move forward + } else if (action == World.MOVE) { //move forward percepts = world.action(action); if ((percepts & BUMP) != BUMP) { //did not bump into anything prevPos = this.curPos; prevState = curState; curPos.moveDidMove(); - } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge - move(5); - move(2); - } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { //killed by a pit + } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge + move(World.SHOOT); + move(World.MOVE); + } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { //killed by a pit Position temp = curPos; curPos = prevPos; prevPos = temp; prevState = State.UNSAFE; curState = State.EXPLORED; } - } else if (action == 3 || action == 4) { //turn + } else if (action == World.TURN_LEFT || action == World.TURN_RIGHT) { //turn world.action(action); prevPos = curPos; - if (action == 3) { - curPos.direction = ++curPos.direction % 4; //turn left + if (action == World.SOUTH) { + curPos.direction = ++curPos.direction % 4; //turn left } else { - curPos.direction = --curPos.direction % 4; //turn right + curPos.direction = --curPos.direction % 4; //turn right } - } else if (action == 5) { //shoot arrow + } else if (action == World.SHOOT) { //shoot arrow world.action(action); arrowCount--; - } else { //should never reach here + } else if (action == World.QUIT) { //should never reach here System.out.println("Invalid action: " + action); } } @@ -86,38 +86,38 @@ public void decideNextAction(byte percepts) { //select safe neighboring ce Random random = new Random(); switch (random.nextInt(3)) { case 1: //go forward - move(1); + move(World.MOVE); break; case 2: //turn left and go forward - move(2); - move(1); + move(World.TURN_LEFT); + move(World.MOVE); break; case 3: //turn right and go forward - move(3); - move(1); + move(World.TURN_RIGHT); + move(World.MOVE); break; default: System.out.println("Should not reach this line."); } } else { //neighboring cells may not be safe //if previous cell was safe, return and pick new direction - if (prevState == State.SAFE) { //there might be a situation where the agent move back and forth between 3 safe safe spaces here we might need to account for - move(3); - move(3); - move(1); + if (prevState == State.SAFE) { //there might be a situation where the agent move back and forth between 3 safe safe spaces here we might need to account for + move(World.TURN_LEFT); + move(World.TURN_LEFT); + move(World.MOVE); } else { //pick random move Random random = new Random(); switch (random.nextInt(3)) { case 1: //go forward - move(1); + move(World.MOVE); break; case 2: //turn left and go forward - move(2); - move(1); + move(World.TURN_LEFT); + move(World.MOVE); break; case 3: //turn right and go forward - move(3); - move(1); + move(World.TURN_RIGHT); + move(World.MOVE); break; default: System.out.println("Should not reach this line."); diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 4e0d3c9..ac4edcb 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -6,7 +6,7 @@ public final class World { public static final int NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4; - public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5; + public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; public static final int BREEZE = 1, STENTCH = 2, BUMP = 4, GLITTER = 8, DEATH_BY_WUMPUS = 16, DEATH_BY_PIT = 32, SCREAM = 64; protected int arrowCount, x, y, direction = 0, score = 0; @@ -15,7 +15,6 @@ public final class World { public World(String fileName) { importMap(fileName); - //read in file } public void importMap(String fileName) { @@ -59,6 +58,8 @@ public int action(int action) { perceptMap[x][y] -= GLITTER; } score += 1000; + System.out.println("Gold found!\nScore: " + score); + System.exit(0); break; case MOVE: score--; @@ -184,12 +185,17 @@ public int action(int action) { default: System.out.println("Error in shooting logic."); } + case QUIT: + System.out.println("Agent elected to end game."); + System.exit(0); } System.out.println("Error shouldn't ever get here"); return -1; } private void removeWumpus(int x, int y) { + + System.out.println("Wumpus slain at " + x + ", " + y + "!!!"); //remove death_by_wumpus percepts from x, y perceptMap[x][y] = perceptMap[x][y] & ~DEATH_BY_WUMPUS; From 20944b7e874f4380dd6254105fafcecdba0554d9 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 19:58:43 -0600 Subject: [PATCH 043/191] Wilson is amazing --- Wumpus/src/Clause.java | 5 + Wumpus/src/Fact.java | 13 ++- Wumpus/src/InferenceEngine.java | 178 ++++---------------------------- Wumpus/src/KnowledgeBase.java | 45 +++++--- 4 files changed, 62 insertions(+), 179 deletions(-) diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 2c920df..c58ef24 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -10,6 +10,11 @@ public class Clause extends Fact implements Iterable { ArrayList facts = new ArrayList<>(); public Clause(){} + public Clause(Clause clause){ + for(Fact tempFact : clause.facts){ + facts.add(new Fact(tempFact)); + } + } public Clause(Fact fact){ facts.add(fact); } diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index ba6c6e6..5d280c1 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -6,10 +6,13 @@ public class Fact extends Variable implements Iterable { ArrayList variables = new ArrayList<>(); String predicate; - boolean isEvaluation; - int operator; boolean not; - + public Fact(Fact fact){ + not = fact.not; + variables = fact.variables; + predicate = fact.predicate; + + } public Fact(){} public Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function){ Variable var1 = new Variable(var1Val, var1Var, var1Function); @@ -36,10 +39,6 @@ public boolean contains(Variable var) { return false; } - @Override - public String getOp() { - return "" + operator; - } @Override public ArrayList getArgs() { diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index d348f22..9d91054 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -4,13 +4,20 @@ public class InferenceEngine { KnowledgeBase kb; + public InferenceEngine(KnowledgeBase kb){ + this.kb = kb; + } public boolean follows(Fact fact){ Clause clause = new Clause(); //Step 1: negate input fact fact.not = !fact.not; clause.facts.add(fact); - ArrayList kbClausesClone = new ArrayList<>(kb.clauses); + ArrayList tempClone = new ArrayList<>(kb.getClauses()); + ArrayList kbClausesClone = new ArrayList<>(); + for (Clause tempClause : tempClone) { + kbClausesClone.add(new Clause(tempClause)); + } boolean keepGoing = true; //Step 2: Run negated facts against all known facts while(!kbClausesClone.isEmpty()){ @@ -46,163 +53,18 @@ public boolean follows(Fact fact){ //Step 3: If all facts become empty, return true, else if all facts are exhausted, return false } - public ArrayList convertToCNF(Rule rule) { - ArrayList cnfRules = new ArrayList<>(); - ArrayList toConvertRules = new ArrayList<>(); - toConvertRules.add(rule); - //Step 1: break iff into two implication cases - convertToCNFStepOne(toConvertRules); - - return cnfRules; - } - - public void convertToCNFStepOne(ArrayList rules) { - //Convert every iff to two implies statements - while (!rules.isEmpty()) { - ArrayList allRules = getAllRules(rules.get(0)); - for (Rule rule : allRules) { - if (rule.connector == Rule.IFF) { //for each a iff b we change it to ((a=>b)&&(b=>a)) - Rule newRule1 = new Rule(); - newRule1.leftRule = rule.leftRule; - newRule1.rightRule = rule.rightRule; - newRule1.connector = Rule.IMPLIES; - Rule newRule2 = new Rule(); - newRule2.leftRule = rule.rightRule; - newRule2.rightRule = rule.leftRule; - newRule2.connector = Rule.IMPLIES; - rule.leftRule = newRule1; - rule.rightRule = newRule2; - rule.connector = Rule.AND; - } - } - } - } - - public void convertToCNFStepTwo(ArrayList rules) { - //Convert every a=>b to !a V b - while (!rules.isEmpty()) { - ArrayList allRules = getAllRules(rules.get(0)); - for (Rule rule : allRules) { - if (rule.connector == Rule.IMPLIES) { - rule.leftRule.negated = !rule.leftRule.negated; - rule.connector = Rule.OR; - } - } - } - } - - public void convertToCNFStepThree(ArrayList rules) { - //Deal with negated quantifiers, ie !(EXISTS x, p) to (Vx, !p) or !(Vx, p) to (Exists x, !p) - while (!rules.isEmpty()) { - ArrayList allRules = getAllRules(rules.get(0)); - for (Rule rule : allRules) { - for (Quantifier quantifier : rule.quantifiers) { - if (quantifier.not) { //we have a negated quantifier, move inwards - quantifier.not = false; - quantifier.isExistential = !quantifier.isExistential; - rule.negated = !rule.negated; - } - } - - } - } - } - - public void convertToCNFStepFour(ArrayList rules) { - //Move negation inwards, ie !(A&&B) to (!A V !B) also !(A V B) to (!A && !B) - while (!rules.isEmpty()) { - ArrayList allRules = getAllRules(rules.get(0)); - for (Rule rule : allRules) {//Maybe we have to order outter terms before inner terms for this to work?? - if (rule.negated) { //We assume only connectors left are OR and AND - rule.negated = false; - rule.leftRule.negated = !rule.leftRule.negated; - rule.rightRule.negated = !rule.rightRule.negated; - if (rule.connector == Rule.OR) { - rule.connector = Rule.AND; - } else { - rule.connector = Rule.OR; - } - } - } - } - } - - public void convertToCNFStepFive(ArrayList rules) { - //Pair every quantifier to a unique variable - int uniqueVariableId = 0;//We will increment this for each varId used. - while (!rules.isEmpty()) { - ArrayList allRules = getAllRules(rules.get(0)); - - for (int i = allRules.size() - 1; i >= 0; i--) {//We want to work with most inner rules prior to the ones containing them - Rule rule = allRules.get(i); - for (Quantifier quantifier : rule.quantifiers) { - ArrayList subRules = getAllRules(rule); - for (Rule subRule : subRules) { - if (subRule.justFact) { - for (Variable var : subRule.fact.variables) { - if (var.variableId == quantifier.variableId) { - var.variableId = uniqueVariableId; - } - } - } - } - uniqueVariableId++; - } - } - } - } - - public void convertToCNFStepSix(ArrayList rules) { - //Move all quantifiers to 'top' rule - while (!rules.isEmpty()) { - ArrayList allRules = getAllRules(rules.get(0));//assume allRules.get(0) is topMost rule. - ArrayList allQuantifiers = new ArrayList<>(); - for (Rule rule : allRules) { - allQuantifiers.addAll(rule.quantifiers); - rule.quantifiers = new ArrayList<>(); - } - allRules.get(0).quantifiers = allQuantifiers; - } - } - - public void convertToCNFStepSeven(ArrayList rules) { - //Skolemize existentials... this will be a bitch... - while (!rules.isEmpty()) { - ArrayList allRules = getAllRules(rules.get(0)); - for (Rule rule : allRules) { - - } - } + public void infer(Fact fact){ + //look at each fact in each clause in kb.rules, if predicates match, and negations are opposite + //unify the two rules + //apply the unification substitution on a copy of the kb.rules fact thing + //remove the matching fact from that rule + //add the rule copy with the substitution to kb.clauses } - - public void convertToCNFStepEight(ArrayList rules) { - while (!rules.isEmpty()) { - ArrayList allRules = getAllRules(rules.get(0)); - for (Rule rule : allRules) { - - } - } - } - - public void convertToCNFStepNine(ArrayList rules) { - while (!rules.isEmpty()) { - ArrayList allRules = getAllRules(rules.get(0)); - for (Rule rule : allRules) { - - } - } - } - - public ArrayList getAllRules(Rule rule) { - ArrayList allRules = new ArrayList<>(); - if (rule.leftRule != null) { - allRules.addAll(getAllRules(rule.leftRule)); - } - if (rule.rightRule != null) { - allRules.addAll(getAllRules(rule.rightRule)); - } - allRules.add(rule); - - return allRules; + public void infer(Clause clause){ + //look in kb.rules, only use rules with a single fact + //for each such fact, go through the clause and look for a matching predicate + //if negations are opposite, apply unification, if unification exists.. + //with the substitution, apply to copies of both + //strip fact from input clause, make sure it's not a copy } } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 4a5aea0..7fd414e 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -4,7 +4,24 @@ public class KnowledgeBase { - ArrayList clauses; + InferenceEngine inferenceEngine = new InferenceEngine(this); + private ArrayList clauses = new ArrayList<>(); + ArrayList rules = new ArrayList<>(); + public ArrayList getClauses(){ + return clauses; + } + public void addToClauses(Clause clause){ + if(clause.facts.size() == 0) + return; + else if(clause.facts.size() == 1) + inferenceEngine.infer(clause.facts.get(0)); + else + inferenceEngine.infer(clause); + clauses.add(clause); + + + } + public void initializeRules() { //Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function) //Special predicate: Evaluate @@ -22,11 +39,11 @@ public void initializeRules() { stench.facts.add(wumpusxplusy); stench.facts.add(wumpusxymin); stench.facts.add(wumpusxyplus); - clauses.add(stench); + rules.add(stench); - //Breeze(x,y)=>(Pit(x-1,y) v Pit(x-1,y) v Pit(x,y-1) v Pit(x, y+1) - //!Breeze(x,y) v Pit(x-1,y) v Pit(x-1,y) v Pit(x,y-1) v Pit(x, y+1) + //Breeze(x,y)=>(Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) + //!Breeze(x,y) v Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) Clause breeze = new Clause(); Fact breezehXY = new Fact("Breeze",0,true,1,true,true,null,null); Fact pitxminy = new Fact("Pit",0,true,1,true,false,new MinusFunction(),null); @@ -38,19 +55,19 @@ public void initializeRules() { breeze.facts.add(wumpusxplusy); breeze.facts.add(wumpusxymin); breeze.facts.add(wumpusxyplus); - clauses.add(breeze); + rules.add(breeze); //!Wumpus(-1,y) ^ !Wumpus(x,-1) ^ !Wumpus(x,World.size)^ !Wumpus(World.size,y) //!Wumpus(-1,y), !Wumpus(x,-1), !Wumpus(x,World.size), !Wumpus(World.size,y) - clauses.add(new Clause(new Fact("Wumpus",-1,false,1,true,true,null,null))); - clauses.add(new Clause(new Fact("Wumpus",0,true,-1,false,true,null,null))); - clauses.add(new Clause(new Fact("Wumpus",0,true,World.size,false,true,null,null))); - clauses.add(new Clause(new Fact("Wumpus",World.size,false,1,true,true,null,null))); + rules.add(new Clause(new Fact("Wumpus",-1,false,1,true,true,null,null))); + rules.add(new Clause(new Fact("Wumpus",0,true,-1,false,true,null,null))); + rules.add(new Clause(new Fact("Wumpus",0,true,World.size,false,true,null,null))); + rules.add(new Clause(new Fact("Wumpus",World.size,false,1,true,true,null,null))); //!Pit(-1,y), !Pit(x,-1), !Pit(x,World.size), !Wumpus(World.size,y) - clauses.add(new Clause(new Fact("Pit",-1,false,1,true,true,null,null))); - clauses.add(new Clause(new Fact("Pit",0,true,-1,false,true,null,null))); - clauses.add(new Clause(new Fact("Pit",0,true,World.size,false,true,null,null))); - clauses.add(new Clause(new Fact("Pit",World.size,false,1,true,true,null,null))); + rules.add(new Clause(new Fact("Pit",-1,false,1,true,true,null,null))); + rules.add(new Clause(new Fact("Pit",0,true,-1,false,true,null,null))); + rules.add(new Clause(new Fact("Pit",0,true,World.size,false,true,null,null))); + rules.add(new Clause(new Fact("Pit",World.size,false,1,true,true,null,null))); //Wumpus(x,y)=>!Pit(x,y) //!Wumpus(x,y) v !Pit(x,y) @@ -59,7 +76,7 @@ public void initializeRules() { Fact notPit = new Fact("Pit",0,true,1,true,true,null,null); notWumpusOrNotPit.facts.add(notWumpus); notWumpusOrNotPit.facts.add(notPit); - clauses.add(notWumpusOrNotPit); + rules.add(notWumpusOrNotPit); } public boolean ask(String question) { From 7abb3f73f5b6b10b24ea9c05acb49b405b85c1b0 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Wed, 19 Oct 2016 20:04:10 -0600 Subject: [PATCH 044/191] DIRECTION IS NOW AN ENUM, GIT PISSED --- Wumpus/src/Agent.java | 42 +++++++++++++++++++++++++++----- Wumpus/src/LogicExplorer.java | 41 ++++++++++++++++--------------- Wumpus/src/ReactiveExplorer.java | 2 +- 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 9781dc4..be3ecf7 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -3,6 +3,17 @@ public abstract class Agent { Position curPos; + enum Direction { + NORTH, + EAST, + SOUTH, + WEST; + + void next() { + + } + }; + private void move(int action) { } @@ -13,32 +24,51 @@ class Position { int x; int y; - int direction; + Direction direction; - public Position(int x, int y, int direction) { + public Position(int x, int y, Direction direction) { this.x = x; this.y = y; this.direction = direction; } +// public int[] getLeft() { +// switch (direction) { +// +// } +// +// } +// +// public int[] getRight() { +// +// } +// +// public int[] getForward() { +// +// } +// +// public int[] getBehind() { +// +// } + public void moveDidMove() { switch (direction) { - case World.NORTH: + case NORTH: if (y < World.size - 1) { y += 1; } break; - case World.EAST: + case EAST: if (x > 0) { x -= 1; } break; - case World.SOUTH: + case SOUTH: if (y > 0) { y--; } break; - case World.WEST: + case WEST: if (x < World.size - 1) { x++; } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 13c2d22..c25f4e1 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -1,20 +1,17 @@ import java.util.ArrayList; +import java.util.Random; public class LogicExplorer extends Agent { private final World world; private final KnowledgeBase kb; private int arrowCount; - private int t = 0; //time variable private int previousAction; - private Position curPos; private ArrayList frontier = new ArrayList<>(); private boolean[][] searchedPositions; - private boolean currentlyNavigatingToSafeSquare; private final int worldSize; - private Position goalPosition; - private boolean gameOver = false; + private Random random = new Random(); private final byte BREEZE = 0b00000001; private final byte STENCH = 0b0000010; @@ -35,7 +32,7 @@ public LogicExplorer(World world) { private void run() { - while (!gameOver) { + while (true) { decideNextAction((byte) world.getPercepts()); } } @@ -45,7 +42,6 @@ private void move(int action) { switch (action) { case 1: world.action(action); - gameOver = true; case 2: kb.tell(encodePercepts(world.action(action))); break; @@ -72,33 +68,38 @@ private void decideNextAction(byte percepts) { if (frontier.isEmpty()) { move(World.QUIT); } + if ((percepts & GLITTER) != 0) { + move(World.GRAB); + } + processPosition(percepts); updateKB(percepts); - if ((percepts & GLITTER) != 0) {//maybe just kb.ask("Holding(Gold,Result(Grab,CurrentPosition))"): is better, no percept based logic within agent. - //return World.GRAB; //grab gold and end game - move(1); - } else if (currentlyNavigatingToSafeSquare) { // im pretty sure this is all handled by RHW method, so thers no need to call decide next actions while its traversing - //return continueNavigatingToSafeSquare(); + + //check if adjacent to any unexplored and safe spaces + + //check forward + //check left + //check right + if (kb.ask("!Wumpus(forward)AND!Pit(forward)")) + + + if (kb.ask("Is ")) { + } else if (kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")) { move(World.MOVE); } else if ("safeSpotInFrontier?" == "") { RHWTraversal("!Wumpus(adjacent)AND!Pit(adjacent)"); - //return continueNavigatingToSafeSquare(); } else if ("KnownWumpusSpotInFrontier" == "") { //kill wumpus RHWTraversal("Wumpus(adjacent)"); + move(World.SHOOT); } else { //go to random spot in frontier that is not definite death + Position target = frontier.get(random.nextInt(frontier.size())); + RHWTraversal("At(target)"); } } - //i dont think we need this, RHW takes to the space and then turns to face already...its all done in a look so new percepts arent being processed - //since the agent has already been to all of the spaces in will be traveling through - private int continueNavigatingToSafeSquare() { - //this should basically be RHW Traversal until adjacent to to goal state, then turn to face it - return -1; - } - private void processPosition(byte percepts) { if ((percepts & BUMP) == 0) {//did not bump if (previousAction == World.MOVE) { diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index bef0fb9..188022d 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -68,7 +68,7 @@ private void move(int action) { world.action(action); prevPos = curPos; if (action == World.SOUTH) { - curPos.direction = ++curPos.direction % 4; //turn left + curPos.direction = ++curPos.direction; //turn left } else { curPos.direction = --curPos.direction % 4; //turn right } From ae42384087f73565f2e945d6143c8d94844946c3 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Wed, 19 Oct 2016 21:27:38 -0600 Subject: [PATCH 045/191] inference done? --- Wumpus/src/InferenceEngine.java | 68 ++++++++++++++++++++++++++++++++- Wumpus/src/KnowledgeBase.java | 13 +++++-- Wumpus/src/Unifier.java | 4 +- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 9d91054..5d29807 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -55,16 +55,80 @@ public boolean follows(Fact fact){ public void infer(Fact fact){ //look at each fact in each clause in kb.rules, if predicates match, and negations are opposite + ArrayList tempClone = new ArrayList<>(kb.rules); + ArrayList kbClausesClone = new ArrayList<>(); + for (Clause tempClause : tempClone) { + kbClausesClone.add(new Clause(tempClause)); + } + for(Clause clause : kbClausesClone){ + for(Fact ruleFact:clause.facts){ + if(ruleFact.predicate.equals(fact.predicate) && ruleFact.not == !fact.not){ + ArrayList substitutions = Unifier.unify(fact, ruleFact); + if(substitutions.isEmpty()) + continue; + for(Substitute sub:substitutions){ + for(Variable var : fact.variables){ + if(var.isVariable && var.variableId == sub.varIdToSubstitute){ + var.isVariable = false; + var.value = sub.valToSubstituteWith; + } + } + for(Fact tempFact:clause.facts){ + + for(Variable var : tempFact.variables){ + if(var.isVariable && var.variableId == sub.varIdToSubstitute){ + var.isVariable = false; + var.value = sub.valToSubstituteWith; + } + } + } + } + + clause.facts.remove(ruleFact); + kb.addToClauses(clause); + break; + } + } + } //unify the two rules //apply the unification substitution on a copy of the kb.rules fact thing //remove the matching fact from that rule //add the rule copy with the substitution to kb.clauses } - public void infer(Clause clause){ + public void infer(Clause addedClause){ //look in kb.rules, only use rules with a single fact //for each such fact, go through the clause and look for a matching predicate //if negations are opposite, apply unification, if unification exists.. //with the substitution, apply to copies of both - //strip fact from input clause, make sure it's not a copy + //strip fact from input clause + for(Clause ruleClause : kb.rules){ + if(ruleClause.facts.size()==1){ + Fact ruleFact = new Fact(ruleClause.facts.get(0));//we use a copy + Clause clause = new Clause(addedClause);//copy the added clause, redo each time + for(Fact fact : clause.facts){ + if(fact.predicate.equals(ruleFact.predicate) && fact.not != ruleFact.not){ + ArrayList substitutions = Unifier.unify(fact,ruleFact); + for(Substitute sub : substitutions){ + for(Variable var : ruleFact.variables){ + if(var.isVariable && var.variableId==sub.varIdToSubstitute){ + var.isVariable = false; + var.value = sub.valToSubstituteWith; + } + } + for(Fact changeFact : clause.facts){ + for(Variable var : changeFact.variables){ + if(var.isVariable && var.variableId==sub.varIdToSubstitute){ + var.isVariable = false; + var.value = sub.valToSubstituteWith; + } + } + } + } + clause.facts.remove(fact); + kb.addToClauses(clause); + } + } + } + } } } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 7fd414e..0874c11 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -13,11 +13,18 @@ public ArrayList getClauses(){ public void addToClauses(Clause clause){ if(clause.facts.size() == 0) return; - else if(clause.facts.size() == 1) + else if(clause.facts.size() == 1){ inferenceEngine.infer(clause.facts.get(0)); - else + clauses.add(clause); + } + else{ + int beforeSize = clauses.size(); inferenceEngine.infer(clause); - clauses.add(clause); + if(clauses.size() == beforeSize) + clauses.add(clause);//We only need add the clause itself if we didn't infer anything, otherwise the inferred shortened clause which is more valuable got added + + } + //clauses.add(clause); } diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index eb717e7..e06e4b0 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -26,7 +26,9 @@ that make the two arguments the same, else it returns failure in some fassion (t */ - + public static ArrayList unify(Fact fact1, Fact fact2){ + return new ArrayList(); + } //this is the general outline of how to go about unification... public static SubstitutionString unify(Variable p, Variable q, SubstitutionString theta) { From 47e68253d009d980e3fcf67f506e27ef66907749 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 08:21:15 -0600 Subject: [PATCH 046/191] Changes to location and direction handling, now with some fancy enum stuff --- Wumpus/src/Agent.java | 62 +++++++++-------- Wumpus/src/InferenceEngine.java | 111 +++++++++++++++++-------------- Wumpus/src/LogicExplorer.java | 26 +++----- Wumpus/src/ReactiveExplorer.java | 20 +++--- Wumpus/src/Tester.java | 2 +- 5 files changed, 115 insertions(+), 106 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index be3ecf7..de4fad5 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -1,7 +1,8 @@ public abstract class Agent { - Position curPos; + Location curLoc; + Direction direction; enum Direction { NORTH, @@ -9,8 +10,13 @@ enum Direction { SOUTH, WEST; - void next() { - + private final Direction[] terms = values(); + public Direction left() { + return terms[(this.ordinal()+1) % terms.length]; + } + + public Direction right() { + return terms[(this.ordinal()-1) % terms.length]; } }; @@ -19,38 +25,40 @@ private void move(int action) { public void decideNextAction() { } + + public void moveDidMove() { + switch (direction) { + case NORTH: + if (curLoc.y < World.size - 1) { + curLoc.y += 1; + } + break; + case EAST: + if (curLoc.x > 0) { + curLoc.x -= 1; + } + break; + case SOUTH: + if (curLoc.y > 0) { + curLoc.y--; + } + break; + case WEST: + if (curLoc.x < World.size - 1) { + curLoc.x++; + } + } + } - class Position { + class Location { int x; int y; - Direction direction; - public Position(int x, int y, Direction direction) { + public Location(int x, int y) { this.x = x; this.y = y; - this.direction = direction; } - -// public int[] getLeft() { -// switch (direction) { -// -// } -// -// } -// -// public int[] getRight() { -// -// } -// -// public int[] getForward() { -// -// } -// -// public int[] getBehind() { -// -// } - public void moveDidMove() { switch (direction) { case NORTH: diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 5d29807..b187099 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -4,11 +4,12 @@ public class InferenceEngine { KnowledgeBase kb; - public InferenceEngine(KnowledgeBase kb){ + + public InferenceEngine(KnowledgeBase kb) { this.kb = kb; } - public boolean follows(Fact fact){ + public boolean follows(Fact fact) { Clause clause = new Clause(); //Step 1: negate input fact fact.not = !fact.not; @@ -17,73 +18,80 @@ public boolean follows(Fact fact){ ArrayList kbClausesClone = new ArrayList<>(); for (Clause tempClause : tempClone) { kbClausesClone.add(new Clause(tempClause)); - } + } boolean keepGoing = true; //Step 2: Run negated facts against all known facts - while(!kbClausesClone.isEmpty()){ + while (!kbClausesClone.isEmpty()) { keepGoing = true; - for(Clause kbClause:kbClausesClone){ - for(Fact kbFact:kbClause.facts){ - - for(Fact followFact:clause.facts){ - if(kbFact.predicate.equals(followFact.predicate) && kbFact.not == !followFact.not){ - //extend clause with everything in kbClause, remove kbFact and followFact, start over - kbClause.facts.remove(kbFact); - clause.facts.remove(followFact); - clause.facts.addAll(kbClause.facts); - - //before starting over check if clause is empty... - if(clause.facts.isEmpty()) - return true; - keepGoing = false; + for (Clause kbClause : kbClausesClone) { + for (Fact kbFact : kbClause.facts) { + + for (Fact followFact : clause.facts) { + if (kbFact.predicate.equals(followFact.predicate) && kbFact.not == !followFact.not) { + //extend clause with everything in kbClause, remove kbFact and followFact, start over + kbClause.facts.remove(kbFact); + clause.facts.remove(followFact); + clause.facts.addAll(kbClause.facts); + + //before starting over check if clause is empty... + if (clause.facts.isEmpty()) { + return true; + } + keepGoing = false; + } + if (!keepGoing) { + break; + } } - if(!keepGoing) + if (!keepGoing) { break; + } } - if(!keepGoing) + if (!keepGoing) { break; + } + } + if (keepGoing)//if we didn't do any changes, keepGoing will be true, if we hit here, then we exhausted every clause comparison with no results, terminate + { + return false; } - if(!keepGoing) - break; - } - if(keepGoing)//if we didn't do any changes, keepGoing will be true, if we hit here, then we exhausted every clause comparison with no results, terminate - return false; } return false; //Step 3: If all facts become empty, return true, else if all facts are exhausted, return false } - - public void infer(Fact fact){ + + public void infer(Fact fact) { //look at each fact in each clause in kb.rules, if predicates match, and negations are opposite ArrayList tempClone = new ArrayList<>(kb.rules); ArrayList kbClausesClone = new ArrayList<>(); for (Clause tempClause : tempClone) { kbClausesClone.add(new Clause(tempClause)); } - for(Clause clause : kbClausesClone){ - for(Fact ruleFact:clause.facts){ - if(ruleFact.predicate.equals(fact.predicate) && ruleFact.not == !fact.not){ + for (Clause clause : kbClausesClone) { + for (Fact ruleFact : clause.facts) { + if (ruleFact.predicate.equals(fact.predicate) && ruleFact.not == !fact.not) { ArrayList substitutions = Unifier.unify(fact, ruleFact); - if(substitutions.isEmpty()) + if (substitutions.isEmpty()) { continue; - for(Substitute sub:substitutions){ - for(Variable var : fact.variables){ - if(var.isVariable && var.variableId == sub.varIdToSubstitute){ + } + for (Substitute sub : substitutions) { + for (Variable var : fact.variables) { + if (var.isVariable && var.variableId == sub.varIdToSubstitute) { var.isVariable = false; var.value = sub.valToSubstituteWith; } } - for(Fact tempFact:clause.facts){ - - for(Variable var : tempFact.variables){ - if(var.isVariable && var.variableId == sub.varIdToSubstitute){ + for (Fact tempFact : clause.facts) { + + for (Variable var : tempFact.variables) { + if (var.isVariable && var.variableId == sub.varIdToSubstitute) { var.isVariable = false; var.value = sub.valToSubstituteWith; } } } } - + clause.facts.remove(ruleFact); kb.addToClauses(clause); break; @@ -95,29 +103,30 @@ public void infer(Fact fact){ //remove the matching fact from that rule //add the rule copy with the substitution to kb.clauses } - public void infer(Clause addedClause){ + + public void infer(Clause addedClause) { //look in kb.rules, only use rules with a single fact //for each such fact, go through the clause and look for a matching predicate //if negations are opposite, apply unification, if unification exists.. //with the substitution, apply to copies of both //strip fact from input clause - for(Clause ruleClause : kb.rules){ - if(ruleClause.facts.size()==1){ + for (Clause ruleClause : kb.rules) { + if (ruleClause.facts.size() == 1) { Fact ruleFact = new Fact(ruleClause.facts.get(0));//we use a copy Clause clause = new Clause(addedClause);//copy the added clause, redo each time - for(Fact fact : clause.facts){ - if(fact.predicate.equals(ruleFact.predicate) && fact.not != ruleFact.not){ - ArrayList substitutions = Unifier.unify(fact,ruleFact); - for(Substitute sub : substitutions){ - for(Variable var : ruleFact.variables){ - if(var.isVariable && var.variableId==sub.varIdToSubstitute){ + for (Fact fact : clause.facts) { + if (fact.predicate.equals(ruleFact.predicate) && fact.not != ruleFact.not) { + ArrayList substitutions = Unifier.unify(fact, ruleFact); + for (Substitute sub : substitutions) { + for (Variable var : ruleFact.variables) { + if (var.isVariable && var.variableId == sub.varIdToSubstitute) { var.isVariable = false; var.value = sub.valToSubstituteWith; } } - for(Fact changeFact : clause.facts){ - for(Variable var : changeFact.variables){ - if(var.isVariable && var.variableId==sub.varIdToSubstitute){ + for (Fact changeFact : clause.facts) { + for (Variable var : changeFact.variables) { + if (var.isVariable && var.variableId == sub.varIdToSubstitute) { var.isVariable = false; var.value = sub.valToSubstituteWith; } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index c25f4e1..39d0956 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -6,19 +6,12 @@ public class LogicExplorer extends Agent { private final World world; private final KnowledgeBase kb; - private int arrowCount; - private int previousAction; - private ArrayList frontier = new ArrayList<>(); + private int arrowCount, previousAction; + private ArrayList frontier = new ArrayList<>(); private boolean[][] searchedPositions; - private final int worldSize; private Random random = new Random(); - private final byte BREEZE = 0b00000001; - private final byte STENCH = 0b0000010; - private final byte BUMP = 0b00000100; - private final byte GLITTER = 0b00001000; - private final byte DEATH = 0b00010000; - private final byte SCREAM = 0b01000000; + private final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, SCREAM = 0b01000000; public LogicExplorer(World world) { this.world = world; @@ -26,7 +19,6 @@ public LogicExplorer(World world) { kb.initializeRules(); this.arrowCount = world.arrowCount; this.searchedPositions = new boolean[World.size][World.size]; - this.worldSize = World.size; run(); } @@ -95,7 +87,7 @@ private void decideNextAction(byte percepts) { move(World.SHOOT); } else { //go to random spot in frontier that is not definite death - Position target = frontier.get(random.nextInt(frontier.size())); + Location target = frontier.get(random.nextInt(frontier.size())); RHWTraversal("At(target)"); } } @@ -103,7 +95,7 @@ private void decideNextAction(byte percepts) { private void processPosition(byte percepts) { if ((percepts & BUMP) == 0) {//did not bump if (previousAction == World.MOVE) { - curPos.moveDidMove(); + curLoc.moveDidMove(); } } } @@ -111,14 +103,14 @@ private void processPosition(byte percepts) { private void updateKB(byte percepts) { if ((percepts & STENCH) != 0) { Clause clause = new Clause(); - kb.tell(new Clause(new Fact("Stench", curPos.x, false, curPos.y,false, true, null, null)));//Stench(x,y,t) + kb.tell(new Clause(new Fact("Stench", curLoc.x, false, curLoc.y,false, true, null, null)));//Stench(x,y,t) } else { - kb.tell(new Clause(new Fact("Stench", curPos.x, false, curPos.y,false, false, null, null)));//!Stench(x,y,t) + kb.tell(new Clause(new Fact("Stench", curLoc.x, false, curLoc.y,false, false, null, null)));//!Stench(x,y,t) } if ((percepts & BREEZE) != 0) { - kb.tell(new Clause(new Fact("Breeze", curPos.x, false, curPos.y,false, true, null, null))); + kb.tell(new Clause(new Fact("Breeze", curLoc.x, false, curLoc.y,false, true, null, null))); } else { - kb.tell(new Clause(new Fact("Breeze", curPos.x, false, curPos.y,false, false, null, null))); + kb.tell(new Clause(new Fact("Breeze", curLoc.x, false, curLoc.y,false, false, null, null))); } if ((percepts & SCREAM) != 0) { //need to deal with this diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 188022d..3c0566e 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -4,7 +4,7 @@ public class ReactiveExplorer extends Agent { private final World world; - private Position prevPos; + private Location prevPos; private State curState, prevState; private int percepts, arrowCount; private boolean gameOver = false; @@ -26,8 +26,8 @@ enum State { public ReactiveExplorer(World world) { this.world = world; arrowCount = world.arrowCount; - curPos = new Position(world.x, world.y, world.direction); - prevPos = curPos; + curLoc = new Location(world.x, world.y); + prevPos = curLoc; percepts = world.getPercepts(); if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { curState = State.SAFE; @@ -51,26 +51,26 @@ private void move(int action) { } else if (action == World.MOVE) { //move forward percepts = world.action(action); if ((percepts & BUMP) != BUMP) { //did not bump into anything - prevPos = this.curPos; + prevPos = this.curLoc; prevState = curState; - curPos.moveDidMove(); + curLoc.moveDidMove(); } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge move(World.SHOOT); move(World.MOVE); } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { //killed by a pit - Position temp = curPos; - curPos = prevPos; + Location temp = curLoc; + curLoc = prevPos; prevPos = temp; prevState = State.UNSAFE; curState = State.EXPLORED; } } else if (action == World.TURN_LEFT || action == World.TURN_RIGHT) { //turn world.action(action); - prevPos = curPos; + prevPos = curLoc; if (action == World.SOUTH) { - curPos.direction = ++curPos.direction; //turn left + direction = direction.left(); //turn left } else { - curPos.direction = --curPos.direction % 4; //turn right + direction = direction.right(); //turn right } } else if (action == World.SHOOT) { //shoot arrow world.action(action); diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index 4bdd97c..73587bc 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -115,7 +115,7 @@ public int checkConnector(String connector) { } public void testInferenceEngine() { - InferenceEngine engine = new InferenceEngine(); + InferenceEngine engine = new InferenceEngine(new KnowledgeBase()); //FORALL x, y, Commutative(x,y) IFF Commutative(y,x) Rule rule = new Rule(); From 41bfce6f5c30ddbcb1572ccd46df71150c0f50df Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 08:53:03 -0600 Subject: [PATCH 047/191] Refactoring agent abstract class --- Wumpus/src/Agent.java | 92 +++++++++++---------------- Wumpus/src/LogicExplorer.java | 9 +-- Wumpus/src/ReactiveExplorer.java | 104 ++++++++++++++----------------- 3 files changed, 88 insertions(+), 117 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index de4fad5..ad974e3 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -1,54 +1,61 @@ +import java.util.Random; + public abstract class Agent { Location curLoc; Direction direction; + protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; + protected int percepts, arrowCount; + protected World world; + protected Random random = new Random(); enum Direction { NORTH, EAST, SOUTH, WEST; - + private final Direction[] terms = values(); + public Direction left() { - return terms[(this.ordinal()+1) % terms.length]; + return terms[(this.ordinal() + 1) % terms.length]; } - + public Direction right() { - return terms[(this.ordinal()-1) % terms.length]; + return terms[(this.ordinal() - 1) % terms.length]; } - }; - - private void move(int action) { } - public void decideNextAction() { + enum State { + SAFE, + UNSAFE, + EXPLORED; } - - public void moveDidMove() { - switch (direction) { - case NORTH: - if (curLoc.y < World.size - 1) { - curLoc.y += 1; - } - break; - case EAST: - if (curLoc.x > 0) { - curLoc.x -= 1; - } - break; - case SOUTH: - if (curLoc.y > 0) { - curLoc.y--; - } - break; - case WEST: - if (curLoc.x < World.size - 1) { - curLoc.x++; - } - } + + public void updateLocation() { + switch (direction) { + case NORTH: + if (curLoc.y < World.size - 1) { + curLoc.y += 1; + } + break; + case EAST: + if (curLoc.x > 0) { + curLoc.x -= 1; + } + break; + case SOUTH: + if (curLoc.y > 0) { + curLoc.y--; + } + break; + case WEST: + if (curLoc.x < World.size - 1) { + curLoc.x++; + } } + } class Location { @@ -59,28 +66,5 @@ public Location(int x, int y) { this.x = x; this.y = y; } - public void moveDidMove() { - switch (direction) { - case NORTH: - if (y < World.size - 1) { - y += 1; - } - break; - case EAST: - if (x > 0) { - x -= 1; - } - break; - case SOUTH: - if (y > 0) { - y--; - } - break; - case WEST: - if (x < World.size - 1) { - x++; - } - } - } } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 39d0956..e99564e 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -1,17 +1,12 @@ import java.util.ArrayList; -import java.util.Random; public class LogicExplorer extends Agent { - private final World world; private final KnowledgeBase kb; - private int arrowCount, previousAction; + private int previousAction; private ArrayList frontier = new ArrayList<>(); private boolean[][] searchedPositions; - private Random random = new Random(); - - private final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, SCREAM = 0b01000000; public LogicExplorer(World world) { this.world = world; @@ -95,7 +90,7 @@ private void decideNextAction(byte percepts) { private void processPosition(byte percepts) { if ((percepts & BUMP) == 0) {//did not bump if (previousAction == World.MOVE) { - curLoc.moveDidMove(); + updateLocation(); } } } diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 3c0566e..43fc1e6 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -1,27 +1,8 @@ -import java.util.Random; - public class ReactiveExplorer extends Agent { - private final World world; private Location prevPos; private State curState, prevState; - private int percepts, arrowCount; - private boolean gameOver = false; - - private final byte BREEZE = 0b00000001; - private final byte STENTCH = 0b0000010; - private final byte BUMP = 0b00000100; - private final byte GLITTER = 0b00001000; - private final byte DEATH_BY_WUMPUS = 0b00010000; - private final byte DEATH_BY_PIT = 0b00100000; - private final byte SCREAM = 0b01000000; - - enum State { - SAFE, - UNSAFE, - EXPLORED; - } public ReactiveExplorer(World world) { this.world = world; @@ -29,7 +10,7 @@ public ReactiveExplorer(World world) { curLoc = new Location(world.x, world.y); prevPos = curLoc; percepts = world.getPercepts(); - if (((percepts & STENTCH) != STENTCH) && ((percepts & BREEZE) != BREEZE)) { + if (((percepts & STENCH) != STENCH) && ((percepts & BREEZE) != BREEZE)) { curState = State.SAFE; prevState = State.SAFE; } @@ -45,45 +26,57 @@ private void run() { private void move(int action) { - if (action == World.GRAB) { //grab gold, game ends - world.action(action); - gameOver = true; - } else if (action == World.MOVE) { //move forward - percepts = world.action(action); - if ((percepts & BUMP) != BUMP) { //did not bump into anything - prevPos = this.curLoc; - prevState = curState; - curLoc.moveDidMove(); - } else if ((percepts & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge - move(World.SHOOT); - move(World.MOVE); - } else if ((percepts & DEATH_BY_PIT) == DEATH_BY_PIT) { //killed by a pit - Location temp = curLoc; - curLoc = prevPos; - prevPos = temp; - prevState = State.UNSAFE; - curState = State.EXPLORED; - } - } else if (action == World.TURN_LEFT || action == World.TURN_RIGHT) { //turn - world.action(action); - prevPos = curLoc; - if (action == World.SOUTH) { - direction = direction.left(); //turn left - } else { - direction = direction.right(); //turn right - } - } else if (action == World.SHOOT) { //shoot arrow - world.action(action); - arrowCount--; - } else if (action == World.QUIT) { //should never reach here - System.out.println("Invalid action: " + action); + switch (action) { + case World.GRAB: + //grab gold, game ends + world.action(action); + break; + case World.MOVE: + //move forward + percepts = world.action(action); + if ((percepts & BUMP) != BUMP) { //did not bump into anything + prevPos = this.curLoc; + prevState = curState; + updateLocation(); + } else if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge + move(World.SHOOT); + move(World.MOVE); + } else if ((percepts & DEATH) == DEATH) { //killed by a pit + Location temp = curLoc; + curLoc = prevPos; + prevPos = temp; + prevState = State.UNSAFE; + curState = State.EXPLORED; + } + break; + case World.TURN_LEFT: + case World.TURN_RIGHT: + //turn + world.action(action); + prevPos = curLoc; + if (action == World.SOUTH) { + direction = direction.left(); //turn left + } else { + direction = direction.right(); //turn right + } + break; + case World.SHOOT: + //shoot arrow + world.action(action); + arrowCount--; + break; + case World.QUIT: + //should never reach here + System.out.println("Invalid action: " + action); + break; + default: + break; } } - public void decideNextAction(byte percepts) { //select safe neighboring cell else select unsafe neighboring cell + public void decideNextAction() { //select safe neighboring cell else select unsafe neighboring cell - if (((percepts & STENTCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe - Random random = new Random(); + if (((percepts & STENCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe switch (random.nextInt(3)) { case 1: //go forward move(World.MOVE); @@ -106,7 +99,6 @@ public void decideNextAction(byte percepts) { //select safe neighboring ce move(World.TURN_LEFT); move(World.MOVE); } else { //pick random move - Random random = new Random(); switch (random.nextInt(3)) { case 1: //go forward move(World.MOVE); From 86cc2dd87a8174d20a198d2b707c54f464c1eb22 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 09:12:33 -0600 Subject: [PATCH 048/191] More refactoring wow! --- Wumpus/src/Agent.java | 35 ++++++---- Wumpus/src/LogicExplorer.java | 2 - Wumpus/src/ReactiveExplorer.java | 101 +++++++++++++---------------- Wumpus/src/World.java | 107 ++++++++++++++++--------------- 4 files changed, 122 insertions(+), 123 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index ad974e3..4b13a06 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -3,14 +3,23 @@ public abstract class Agent { - Location curLoc; - Direction direction; + protected Location location; + protected Direction direction; protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; + protected static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected int percepts, arrowCount; protected World world; protected Random random = new Random(); + + public Agent(World world) { + this.world = world; + this.arrowCount = world.arrowCount; + this.location = new Location(world.x, world.y); + this.direction = Direction.NORTH; + this.percepts = world.getPercepts(); + } - enum Direction { + public enum Direction { NORTH, EAST, SOUTH, @@ -27,7 +36,7 @@ public Direction right() { } } - enum State { + public enum State { SAFE, UNSAFE, EXPLORED; @@ -36,28 +45,28 @@ enum State { public void updateLocation() { switch (direction) { case NORTH: - if (curLoc.y < World.size - 1) { - curLoc.y += 1; + if (location.y < World.size - 1) { + location.y += 1; } break; case EAST: - if (curLoc.x > 0) { - curLoc.x -= 1; + if (location.x > 0) { + location.x -= 1; } break; case SOUTH: - if (curLoc.y > 0) { - curLoc.y--; + if (location.y > 0) { + location.y--; } break; case WEST: - if (curLoc.x < World.size - 1) { - curLoc.x++; + if (location.x < World.size - 1) { + location.x++; } } } - class Location { + protected class Location { int x; int y; diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index e99564e..45bc5b3 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -9,10 +9,8 @@ public class LogicExplorer extends Agent { private boolean[][] searchedPositions; public LogicExplorer(World world) { - this.world = world; kb = new KnowledgeBase(); kb.initializeRules(); - this.arrowCount = world.arrowCount; this.searchedPositions = new boolean[World.size][World.size]; run(); } diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 43fc1e6..b013800 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -5,10 +5,8 @@ public class ReactiveExplorer extends Agent { private State curState, prevState; public ReactiveExplorer(World world) { - this.world = world; - arrowCount = world.arrowCount; - curLoc = new Location(world.x, world.y); - prevPos = curLoc; + super(world); + prevPos = location; percepts = world.getPercepts(); if (((percepts & STENCH) != STENCH) && ((percepts & BREEZE) != BREEZE)) { curState = State.SAFE; @@ -27,92 +25,83 @@ private void run() { private void move(int action) { switch (action) { - case World.GRAB: - //grab gold, game ends - world.action(action); + case GRAB: + world.action(GRAB); break; - case World.MOVE: - //move forward - percepts = world.action(action); - if ((percepts & BUMP) != BUMP) { //did not bump into anything - prevPos = this.curLoc; + case MOVE: + percepts = world.action(MOVE); + if ((percepts & BUMP) != BUMP) { //did not bump into anything + prevPos = location; prevState = curState; updateLocation(); - } else if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge - move(World.SHOOT); - move(World.MOVE); - } else if ((percepts & DEATH) == DEATH) { //killed by a pit - Location temp = curLoc; - curLoc = prevPos; + } else if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge + move(SHOOT); + move(MOVE); + } else if ((percepts & DEATH) == DEATH) { //killed by a pit + Location temp = location; + location = prevPos; prevPos = temp; prevState = State.UNSAFE; curState = State.EXPLORED; } break; - case World.TURN_LEFT: - case World.TURN_RIGHT: - //turn - world.action(action); - prevPos = curLoc; - if (action == World.SOUTH) { - direction = direction.left(); //turn left - } else { - direction = direction.right(); //turn right - } + case TURN_LEFT: + world.action(TURN_LEFT); + direction = direction.left(); break; - case World.SHOOT: - //shoot arrow - world.action(action); - arrowCount--; + case TURN_RIGHT: + world.action(TURN_RIGHT); + direction = direction.right(); break; - case World.QUIT: - //should never reach here - System.out.println("Invalid action: " + action); + case SHOOT: + world.action(SHOOT); + arrowCount--; break; - default: + case QUIT: + world.action(QUIT); break; } } - public void decideNextAction() { //select safe neighboring cell else select unsafe neighboring cell + public void decideNextAction() { //select safe neighboring cell else select unsafe neighboring cell if (((percepts & STENCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe switch (random.nextInt(3)) { - case 1: //go forward - move(World.MOVE); + case 1: //go forward + move(MOVE); break; - case 2: //turn left and go forward - move(World.TURN_LEFT); - move(World.MOVE); + case 2: //turn left and go forward + move(TURN_LEFT); + move(MOVE); break; - case 3: //turn right and go forward - move(World.TURN_RIGHT); - move(World.MOVE); + case 3: //turn right and go forward + move(TURN_RIGHT); + move(MOVE); break; default: - System.out.println("Should not reach this line."); + System.out.println("Invalid case: random action, reactive explorer (safe)"); } } else { //neighboring cells may not be safe - //if previous cell was safe, return and pick new direction + if (prevState == State.SAFE) { //there might be a situation where the agent move back and forth between 3 safe safe spaces here we might need to account for - move(World.TURN_LEFT); - move(World.TURN_LEFT); - move(World.MOVE); + move(TURN_LEFT); + move(TURN_LEFT); + move(MOVE); } else { //pick random move switch (random.nextInt(3)) { case 1: //go forward - move(World.MOVE); + move(MOVE); break; case 2: //turn left and go forward - move(World.TURN_LEFT); - move(World.MOVE); + move(TURN_LEFT); + move(MOVE); break; case 3: //turn right and go forward - move(World.TURN_RIGHT); - move(World.MOVE); + move(TURN_RIGHT); + move(MOVE); break; default: - System.out.println("Should not reach this line."); + System.out.println("Invalid case: random action, reactive explorer (unsafe)"); } } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index ac4edcb..1d107bc 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -63,22 +63,9 @@ public int action(int action) { break; case MOVE: score--; - if (direction == NORTH) { - if (y + 1 < size) { - if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - score -= 1000; - return DEATH_BY_WUMPUS; - } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - score -= 1000; - return DEATH_BY_PIT; - } - y = y + 1; - return perceptMap[x][y]; - } else { - return BUMP; - } - } else if (direction == EAST) { + switch (direction) { + case NORTH: + if (y + 1 < size) { if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { score -= 1000; return DEATH_BY_WUMPUS; @@ -87,43 +74,59 @@ public int action(int action) { score -= 1000; return DEATH_BY_PIT; } - if (x + 1 < size) { - x = x + 1; - return perceptMap[x][y]; - } else { - return BUMP; - } - } else if (direction == SOUTH) { - if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - score -= 1000; - return DEATH_BY_WUMPUS; - } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - score -= 1000; - return DEATH_BY_PIT; - } - if (y - 1 > 0) { - y -= 1; - return perceptMap[x][y]; - } else { - return BUMP; - } - } else if (direction == WEST) { - if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { - score -= 1000; - return DEATH_BY_WUMPUS; - } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { - score -= 1000; - return DEATH_BY_PIT; - } - if (x - 1 > 0) { - x -= 1; - return perceptMap[x][y]; - } else { - return BUMP; - } + y = y + 1; + return perceptMap[x][y]; + } else { + return BUMP; + } + case EAST: + if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + score -= 1000; + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + score -= 1000; + return DEATH_BY_PIT; } + if (x + 1 < size) { + x = x + 1; + return perceptMap[x][y]; + } else { + return BUMP; + } + case SOUTH: + if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + score -= 1000; + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + score -= 1000; + return DEATH_BY_PIT; + } + if (y - 1 > 0) { + y -= 1; + return perceptMap[x][y]; + } else { + return BUMP; + } + case WEST: + if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + score -= 1000; + return DEATH_BY_WUMPUS; + } + if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + score -= 1000; + return DEATH_BY_PIT; + } + if (x - 1 > 0) { + x -= 1; + return perceptMap[x][y]; + } else { + return BUMP; + } + default: + break; + } break; case TURN_LEFT: direction = (direction + 3) % 4 + 1; From 39caff2f456bbad11d5b806c0bf11c745d4ca9eb Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 09:29:39 -0600 Subject: [PATCH 049/191] More LogicExplorer stuff... --- Wumpus/src/LogicExplorer.java | 118 ++++++++++++++++------------------ 1 file changed, 57 insertions(+), 61 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 45bc5b3..12cbd93 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -9,6 +9,7 @@ public class LogicExplorer extends Agent { private boolean[][] searchedPositions; public LogicExplorer(World world) { + super(world); kb = new KnowledgeBase(); kb.initializeRules(); this.searchedPositions = new boolean[World.size][World.size]; @@ -18,37 +19,62 @@ public LogicExplorer(World world) { private void run() { while (true) { - decideNextAction((byte) world.getPercepts()); + decideNextAction(); } } private void move(int action) { switch (action) { - case 1: - world.action(action); - case 2: - kb.tell(encodePercepts(world.action(action))); + case GRAB: + world.action(GRAB); + System.out.println("Game failed to end after action(GRAB)."); break; - case 5: + case MOVE: + percepts = (byte) world.action(MOVE); + processPercepts(); + break; + case TURN_LEFT: + world.action(TURN_LEFT); + break; + case TURN_RIGHT: + world.action(TURN_RIGHT); + break; + case SHOOT: arrowCount--; - kb.tell(encodePercepts(world.action(action))); + percepts = (byte) world.action(SHOOT); + processPercepts(); break; - default: - world.action(action); + case QUIT: + world.action(QUIT); + System.out.println("Game failed to end after action(QUIT)."); break; + default: + System.out.println("Error processing movement."); } } - private Clause encodePercepts(int percepts) { - - Clause clause = new Clause(); + private void processPercepts() { - //conversion logic goes here - return clause; + if (((percepts & BUMP) != BUMP) && (percepts & DEATH) != DEATH) { + updateLocation(); + } + if ((percepts & STENCH) != 0) { + kb.tell(new Clause(new Fact("Stench", location.x, false, location.y, false, true, null, null))); + } else { + kb.tell(new Clause(new Fact("Stench", location.x, false, location.y, false, false, null, null))); + } + if ((percepts & BREEZE) != 0) { + kb.tell(new Clause(new Fact("Breeze", location.x, false, location.y, false, true, null, null))); + } else { + kb.tell(new Clause(new Fact("Breeze", location.x, false, location.y, false, false, null, null))); + } + if ((percepts & SCREAM) != 0) { + kb.tell(new Clause(new Fact("Screem", location.x, false, location.y, false, true, null, null))); + } } - private void decideNextAction(byte percepts) { + private void decideNextAction() { if (frontier.isEmpty()) { move(World.QUIT); @@ -56,60 +82,30 @@ private void decideNextAction(byte percepts) { if ((percepts & GLITTER) != 0) { move(World.GRAB); } - - processPosition(percepts); - updateKB(percepts); - + //check if adjacent to any unexplored and safe spaces - //check forward //check left //check right - if (kb.ask("!Wumpus(forward)AND!Pit(forward)")) - - - if (kb.ask("Is ")) { - - } else if (kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")) { - move(World.MOVE); - } else if ("safeSpotInFrontier?" == "") { - RHWTraversal("!Wumpus(adjacent)AND!Pit(adjacent)"); - } else if ("KnownWumpusSpotInFrontier" == "") { - //kill wumpus - RHWTraversal("Wumpus(adjacent)"); - move(World.SHOOT); - } else { - //go to random spot in frontier that is not definite death - Location target = frontier.get(random.nextInt(frontier.size())); - RHWTraversal("At(target)"); - } - } - - private void processPosition(byte percepts) { - if ((percepts & BUMP) == 0) {//did not bump - if (previousAction == World.MOVE) { - updateLocation(); + if (kb.ask("!Wumpus(forward)AND!Pit(forward)")) { + if (kb.ask("Is ")) { + + } else if (kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")) { + move(World.MOVE); + } else if ("safeSpotInFrontier?" == "") { + RHWTraversal("!Wumpus(adjacent)AND!Pit(adjacent)"); + } else if ("KnownWumpusSpotInFrontier" == "") { + //kill wumpus + RHWTraversal("Wumpus(adjacent)"); + move(World.SHOOT); + } else { + //go to random spot in frontier that is not definite death + Location target = frontier.get(random.nextInt(frontier.size())); + RHWTraversal("At(target)"); } } } - private void updateKB(byte percepts) { - if ((percepts & STENCH) != 0) { - Clause clause = new Clause(); - kb.tell(new Clause(new Fact("Stench", curLoc.x, false, curLoc.y,false, true, null, null)));//Stench(x,y,t) - } else { - kb.tell(new Clause(new Fact("Stench", curLoc.x, false, curLoc.y,false, false, null, null)));//!Stench(x,y,t) - } - if ((percepts & BREEZE) != 0) { - kb.tell(new Clause(new Fact("Breeze", curLoc.x, false, curLoc.y,false, true, null, null))); - } else { - kb.tell(new Clause(new Fact("Breeze", curLoc.x, false, curLoc.y,false, false, null, null))); - } - if ((percepts & SCREAM) != 0) { - //need to deal with this - } - } - private void RHWTraversal(String stopCondition) { do { From 332cd14740239d400bedeed02b9595d323d571f1 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 09:35:55 -0600 Subject: [PATCH 050/191] Cleanup, removed iterator implementation from Zach's previous unification attempt. --- Wumpus/src/Agent.java | 2 +- Wumpus/src/Clause.java | 34 +++++++++++++----------------- Wumpus/src/Driver.java | 4 ++-- Wumpus/src/Fact.java | 26 +++++++++++------------ Wumpus/src/ReactiveExplorer.java | 2 +- Wumpus/src/SubstitutionString.java | 31 --------------------------- Wumpus/src/Unifier.java | 2 +- Wumpus/src/WumpusGame.java | 1 - 8 files changed, 32 insertions(+), 70 deletions(-) delete mode 100644 Wumpus/src/SubstitutionString.java diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 4b13a06..b0f484a 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -10,7 +10,7 @@ public abstract class Agent { protected int percepts, arrowCount; protected World world; protected Random random = new Random(); - + public Agent(World world) { this.world = world; this.arrowCount = world.arrowCount; diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index c58ef24..a240ec1 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -1,27 +1,29 @@ import java.util.ArrayList; -import java.util.Iterator; + /* A clause contains a list of predicates -*/ -public class Clause extends Fact implements Iterable { - - + */ +public class Clause extends Fact { + ArrayList facts = new ArrayList<>(); - - public Clause(){} - public Clause(Clause clause){ - for(Fact tempFact : clause.facts){ + + public Clause() { + } + + public Clause(Clause clause) { + for (Fact tempFact : clause.facts) { facts.add(new Fact(tempFact)); } } - public Clause(Fact fact){ + + public Clause(Fact fact) { facts.add(fact); } - + @Override public boolean contains(Variable var) { - + for (Fact f : facts) { if (f.contains(var) || f.equals(var)) { return true; @@ -29,15 +31,9 @@ public boolean contains(Variable var) { } return false; } - + @Override public ArrayList getArgs() { return facts; } - - @Override - public Iterator iterator() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - } diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 8fcf53f..82a603d 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -27,11 +27,11 @@ public static void makeGame() throws IOException { System.out.println(""); WumpusGame game = new WumpusGame(5, prob); - + //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); World world = new World("PerceptBoard.txt"); Agent explorer = new ReactiveExplorer(world); - + world = new World("PerceptBoard.txt"); explorer = new LogicExplorer(world); } diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index 5d280c1..3599327 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -1,25 +1,29 @@ import java.util.ArrayList; -import java.util.Iterator; -public class Fact extends Variable implements Iterable { +public class Fact extends Variable { ArrayList variables = new ArrayList<>(); String predicate; boolean not; - public Fact(Fact fact){ + + public Fact(Fact fact) { not = fact.not; variables = fact.variables; predicate = fact.predicate; - + + } + + public Fact() { } - public Fact(){} - public Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function){ + + public Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function) { Variable var1 = new Variable(var1Val, var1Var, var1Function); Variable var2 = new Variable(var2Val, var2Var, var2Function); this.predicate = predicate; this.not = not; } + public void printFact() { System.out.print(predicate + "("); for (Variable var : variables) { @@ -28,7 +32,7 @@ public void printFact() { } System.out.print(") "); } - + @Override public boolean contains(Variable var) { for (Variable v : variables) { @@ -38,15 +42,9 @@ public boolean contains(Variable var) { } return false; } - - + @Override public ArrayList getArgs() { return variables; } - - @Override - public Iterator iterator() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } } diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index b013800..3b45563 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -82,7 +82,7 @@ public void decideNextAction() { //select safe neighboring cell else select u System.out.println("Invalid case: random action, reactive explorer (safe)"); } } else { //neighboring cells may not be safe - + if (prevState == State.SAFE) { //there might be a situation where the agent move back and forth between 3 safe safe spaces here we might need to account for move(TURN_LEFT); move(TURN_LEFT); diff --git a/Wumpus/src/SubstitutionString.java b/Wumpus/src/SubstitutionString.java deleted file mode 100644 index fd57d16..0000000 --- a/Wumpus/src/SubstitutionString.java +++ /dev/null @@ -1,31 +0,0 @@ - -import java.util.ArrayList; -import java.util.LinkedHashMap; - - -public class SubstitutionString extends LinkedHashMap { - - private ArrayList string = new ArrayList<>(); - - public SubstitutionString() { - - } - - - public boolean contains(Variable var) { - //returns true if the string contains the variable - return true; - } - - public Variable get(Variable var) { - - return string.get(string.indexOf(var)); - } - - public void set(Variable var, Variable replacement) { - - int index = string.indexOf(var); - string.remove(var); - string.add(index, replacement); - } -} diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index e06e4b0..964e914 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -30,7 +30,7 @@ public static ArrayList unify(Fact fact1, Fact fact2){ return new ArrayList(); } //this is the general outline of how to go about unification... - public static SubstitutionString unify(Variable p, Variable q, SubstitutionString theta) { + public static Substitute unify(Variable p, Variable q, Substitute theta) { if (p.equals(q)) { //success return theta; diff --git a/Wumpus/src/WumpusGame.java b/Wumpus/src/WumpusGame.java index 47a6c68..c657bd6 100644 --- a/Wumpus/src/WumpusGame.java +++ b/Wumpus/src/WumpusGame.java @@ -1,7 +1,6 @@ import java.io.File; import java.io.FileDescriptor; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; From f136630b54691612cd1462c0bb0ab3cc3fdb9245 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 09:45:33 -0600 Subject: [PATCH 051/191] byte to int --- Wumpus/src/Agent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index b0f484a..0f614f3 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -5,7 +5,7 @@ public abstract class Agent { protected Location location; protected Direction direction; - protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; + protected final int BREEZE = 1, STENCH = 2, BUMP = 4, GLITTER = 8, DEATH = 16, DEATH_WUMPUS = 32, SCREAM = 64; protected static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected int percepts, arrowCount; protected World world; From 6c00c90a24c331e1c66d78b93157cb247646934e Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 09:46:29 -0600 Subject: [PATCH 052/191] Removed rule methods --- Wumpus/src/Tester.java | 114 ----------------------------------------- 1 file changed, 114 deletions(-) diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index 4bdd97c..200f124 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -1,119 +1,7 @@ -import java.util.ArrayList; public class Tester { - public int[] initialParse(String ruleString) { - int[] initials = new int[3]; - if (ruleString.startsWith("V") || ruleString.startsWith("E")) { - //Has quantifiers - initials[0] = 1; - } else { - initials[0] = 0; - } - - return initials; - } - - public Rule addRule(String ruleString) { - ruleString = "Vx,y Commutative(x,y) IFF Commutative(y,x) "; - Rule rule = new Rule(); - ArrayList quants = new ArrayList(); - ArrayList facts = new ArrayList(); - ArrayList rules = new ArrayList(); - //Gets quantifier for main rule - if (ruleString.contains("V")) { - String quant = ruleString.substring(ruleString.indexOf("V") + 1, ruleString.indexOf(" ")); - int i = 0; - for (char c : quant.toCharArray()) { - if (c > 64) { - Quantifier q = new Quantifier(); - q.variableRep = c; - q.variableId = i; - quants.add(q); - rule.quantifiers.add(q); - i++; - } - } - ruleString = ruleString.substring(ruleString.indexOf(" ") + 1); - } - - int numPredicates = 0; - String tempRule = ruleString; - while (!tempRule.equals("")) { - if (checkConnector(tempRule.substring(0, tempRule.indexOf(" "))) != 0) { - numPredicates++; - } - tempRule = tempRule.substring(tempRule.indexOf(" ") + 1); - } - if (numPredicates != 0) { - numPredicates++; - } - - if (numPredicates == 2) { - int connector = 0; - for (int j = 0; j < numPredicates; j++) { - Rule sideRule = new Rule(); - Fact sideFact = new Fact(); - sideFact.predicate = ruleString.substring(0, ruleString.indexOf("(")); - ruleString = ruleString.substring(ruleString.indexOf("(")); - String vars = ruleString.substring(1, ruleString.indexOf(")")); - int i = 0; - for (char c : vars.toCharArray()) { - if (c > 64) { - Variable v = new Variable(); - if (i + 1 < vars.length() && vars.toCharArray()[i + 1] == 44) { - v.isVariable = true; - v.variableId = checkQuantifiers(quants, c).variableId; - sideFact.variables.add(v); - } else if (i + 1 == vars.length() && i - 1 > -1 && vars.toCharArray()[i - 1] == 44) { - v.isVariable = true; - v.variableId = checkQuantifiers(quants, c).variableId; - sideFact.variables.add(v); - } - } - i++; - } - ruleString = ruleString.substring(ruleString.indexOf(" ") + 1); - if(checkConnector(ruleString.substring(0, ruleString.indexOf(" "))) != 0){ - connector = rule.connector = checkConnector(ruleString.substring(0, ruleString.indexOf(" "))); - ruleString = ruleString.substring(ruleString.indexOf(" ") + 1); - } - sideRule.justFact = true; - sideRule.fact = sideFact; - rules.add(sideRule); - } - rule.connector = connector; - rule.leftRule = rules.get(0); - rule.rightRule = rules.get(1); - } - - return rule; - } - - public Quantifier checkQuantifiers(ArrayList quant, char c) { - for (Quantifier quan : quant) { - if (quan.variableRep == c) { - return quan; - } - } - return null; - } - - public int checkConnector(String connector) { - switch (connector) { - case "AND": - return 1; - case "OR": - return 2; - case "IMPLIES": - return 3; - case "IFF": - return 4; - } - return 0; - } - public void testInferenceEngine() { InferenceEngine engine = new InferenceEngine(); @@ -151,8 +39,6 @@ public void testInferenceEngine() { rule.printRule(); Rule newRule = new Rule(); - newRule = addRule(""); - newRule.printRule(); // //Vd,t Facing(d,t) => Facing(d,t+1) OR Action(Turnleft,t) OR Action(TurnRight,t) // Rule rule2 = new Rule(); From fbeac59ff5a72363b43fe9c2afa850f0aaf4e4df Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 09:58:00 -0600 Subject: [PATCH 053/191] Bytes and ints, trying to fix some errors --- Wumpus/src/Agent.java | 2 +- Wumpus/src/Driver.java | 6 ++--- Wumpus/src/World.java | 52 +++++++++++++++++++++--------------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index b0f484a..e0116ef 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -15,7 +15,7 @@ public Agent(World world) { this.world = world; this.arrowCount = world.arrowCount; this.location = new Location(world.x, world.y); - this.direction = Direction.NORTH; + // this.direction = NORTH; this.percepts = world.getPercepts(); } diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 82a603d..e135899 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -7,9 +7,9 @@ public class Driver { public static void main(String[] args) throws IOException { // Driver driver = new Driver(); - Tester tester = new Tester(); - tester.testInferenceEngine(); -// Driver.makeGame(); + // Tester tester = new Tester(); + // tester.testInferenceEngine(); + Driver.makeGame(); } public static void makeGame() throws IOException { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 1d107bc..1b0b2a6 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -7,11 +7,11 @@ public final class World { public static final int NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4; public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; - public static final int BREEZE = 1, STENTCH = 2, BUMP = 4, GLITTER = 8, DEATH_BY_WUMPUS = 16, DEATH_BY_PIT = 32, SCREAM = 64; + protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; protected int arrowCount, x, y, direction = 0, score = 0; public static int size; - private int[][] perceptMap; + private byte[][] perceptMap; public World(String fileName) { importMap(fileName); @@ -23,12 +23,12 @@ public void importMap(String fileName) { BufferedReader reader = new BufferedReader(in); String next; size = Integer.parseInt(reader.readLine()); - perceptMap = new int[size][size]; + perceptMap = new byte[size][size]; int i = 0; while ((next = reader.readLine()) != null) {//((Integer) reader.read()).toString()).equals("-1")) { int j = 0; while (next.contains(" ") && !next.equals(" ")) { - perceptMap[i][j] = Integer.parseInt(next.substring(0, next.indexOf(" "))); + perceptMap[i][j] = (byte) Integer.parseInt(next.substring(0, next.indexOf(" "))); next = next.substring(next.indexOf(" ") + 1, next.length()); j++; @@ -36,7 +36,7 @@ public void importMap(String fileName) { i++; } System.out.println(""); - for (int[] row : perceptMap) { + for (byte[] row : perceptMap) { for (int j = 0; j < row.length; j++) { System.out.print(row[j] + " "); } @@ -66,13 +66,13 @@ public int action(int action) { switch (direction) { case NORTH: if (y + 1 < size) { - if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; - return DEATH_BY_WUMPUS; + return DEATH_WUMPUS; } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + if ((perceptMap[x][y] & DEATH) == DEATH) { score -= 1000; - return DEATH_BY_PIT; + return DEATH; } y = y + 1; return perceptMap[x][y]; @@ -80,13 +80,13 @@ public int action(int action) { return BUMP; } case EAST: - if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; - return DEATH_BY_WUMPUS; + return DEATH_WUMPUS; } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + if ((perceptMap[x][y] & DEATH) == DEATH) { score -= 1000; - return DEATH_BY_PIT; + return DEATH; } if (x + 1 < size) { x = x + 1; @@ -95,13 +95,13 @@ public int action(int action) { return BUMP; } case SOUTH: - if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; - return DEATH_BY_WUMPUS; + return DEATH_WUMPUS; } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + if ((perceptMap[x][y] & DEATH) == DEATH) { score -= 1000; - return DEATH_BY_PIT; + return DEATH; } if (y - 1 > 0) { y -= 1; @@ -110,13 +110,13 @@ public int action(int action) { return BUMP; } case WEST: - if ((perceptMap[x][y] & DEATH_BY_WUMPUS) == DEATH_BY_WUMPUS) { + if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; - return DEATH_BY_WUMPUS; + return DEATH_WUMPUS; } - if ((perceptMap[x][y] & DEATH_BY_PIT) == DEATH_BY_PIT) { + if ((perceptMap[x][y] & DEATH) == DEATH) { score -= 1000; - return DEATH_BY_PIT; + return DEATH; } if (x - 1 > 0) { x -= 1; @@ -201,23 +201,23 @@ private void removeWumpus(int x, int y) { System.out.println("Wumpus slain at " + x + ", " + y + "!!!"); //remove death_by_wumpus percepts from x, y - perceptMap[x][y] = perceptMap[x][y] & ~DEATH_BY_WUMPUS; + perceptMap[x][y] = (byte) (perceptMap[x][y] & ~DEATH_WUMPUS); //remove stench percepts form spaces adjacent to wumpus if (x > 0) { //remove stentch to left - perceptMap[x - 1][y] = perceptMap[x - 1][y] & ~STENTCH; + perceptMap[x - 1][y] = (byte) (perceptMap[x - 1][y] & ~STENCH); if (x < size - 1) { //remove stentch to right - perceptMap[x + 1][y] = perceptMap[x + 1][y] & ~STENTCH; + perceptMap[x + 1][y] = (byte) (perceptMap[x + 1][y] & ~STENCH); } } if (y > 0) { //remove stentch below - perceptMap[x][y - 1] = perceptMap[x][y - 1] & ~STENTCH; + perceptMap[x][y - 1] = (byte) (perceptMap[x][y - 1] & ~STENCH); if (y < size - 1) { //remove stentch above - perceptMap[x][y + 1] = perceptMap[x][y + 1] & ~STENTCH; + perceptMap[x][y + 1] = (byte) (perceptMap[x][y + 1] & ~STENCH); } } } From 8a6bd9a1d2b75e06e085f1dd2f834d426530a956 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 10:59:00 -0600 Subject: [PATCH 054/191] Wrote up bulk of unification. No testing done --- Wumpus/src/Tester.java | 6 ++++ Wumpus/src/Unifier.java | 68 +++++++++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index 15ac1d7..620c6ce 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -1,6 +1,12 @@ public class Tester { + + public void testUnify(){ + Fact f1 = new Fact(); + f1.isVariable = true; + Variable v1 = new Variable(); + } public void testInferenceEngine() { InferenceEngine engine = new InferenceEngine(new KnowledgeBase()); diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 964e914..d498ff4 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -25,19 +25,35 @@ that make the two arguments the same, else it returns failure in some fassion (t https://baylor-ir.tdl.org/baylor-ir/bitstream/handle/2104/2887/AshishArteThesis.pdf?sequence=4 this is dense af, but has some good information and explaination of pseduo code - */ - public static ArrayList unify(Fact fact1, Fact fact2){ + */ + public static ArrayList unify1(Fact fact1, Fact fact2) { + if (!canUnify(fact1, fact2)) { + return new ArrayList(); + } + return new ArrayList(); } + + public static boolean canUnify(Fact fact1, Fact fact2) { + //Predicates don't match + if (!fact1.predicate.equals(fact2.predicate)) { + return false; + } + return true; + } + //this is the general outline of how to go about unification... - public static Substitute unify(Variable p, Variable q, Substitute theta) { - + public static Substitute unify1(Variable p, Variable q, Substitute theta) { + if (p.equals(q)) { //success + theta.varIdToSubstitute = p.variableId; + theta.valToSubstituteWith = q.variableId; return theta; - } else if (!p.contains(q)) { + } else if (p.variableId != q.variableId) { //bind p to q and insert (p/q) into theta + return theta; - } else if (!q.contains(p)) { + } else if (q.variableId != q.variableId) { //bind q to p and insert (q/p) into theta return theta; } else if (true) { //if r is a variable? Occurs checks need to be done here i think @@ -51,15 +67,49 @@ public static Substitute unify(Variable p, Variable q, Substitute theta) { } return null; } - + + public static ArrayList unify(Fact f1, Fact f2) { + ArrayList vars = new ArrayList<>(); + Fact temp = new Fact(); + temp.variables.add(f1.variables.get(0)); + temp.variables.add(f2.variables.get(0)); + vars.add(temp); + + temp = new Fact(); + temp.variables.add(f1.variables.get(1)); + temp.variables.add(f2.variables.get(1)); + vars.add(temp); + + ArrayList subs = new ArrayList<>(); + + while (!vars.isEmpty()) { + Fact tempFact = vars.remove(0); + if (!tempFact.variables.get(0).equals(tempFact.variables.get(1))) { + if (tempFact.variables.get(0).isVariable) { + Substitute s = new Substitute(); + s.varIdToSubstitute = tempFact.variables.get(0).variableId; + s.varIdToSubstitute = tempFact.variables.get(1).variableId; + subs.add(s); + } else if (tempFact.variables.get(1).isVariable) { + Substitute s = new Substitute(); + s.varIdToSubstitute = tempFact.variables.get(1).variableId; + s.varIdToSubstitute = tempFact.variables.get(0).variableId; + subs.add(s); + } + else{ + return new ArrayList(); + } + } + } + return subs; + } + private static boolean occursCheck(Variable var, Variable term) { //determins if term is in var return true; } } - - // public SubstitutionString unify(Variable x, Variable y) { // return (SubstitutionString) unify(x, y, (SubstitutionString) new LinkedHashMap<>()); // } From 8a3b151ee6c5ab4a276ea6f400b9cc90bc172168 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 11:16:27 -0600 Subject: [PATCH 055/191] Fixed minor issue with unification, added test case --- Wumpus/src/Driver.java | 5 +++-- Wumpus/src/Tester.java | 37 ++++++++++++++++++++++++++++++++++--- Wumpus/src/Unifier.java | 4 ++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index e135899..61abb79 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -7,9 +7,10 @@ public class Driver { public static void main(String[] args) throws IOException { // Driver driver = new Driver(); - // Tester tester = new Tester(); + Tester tester = new Tester(); + tester.testUnify(); // tester.testInferenceEngine(); - Driver.makeGame(); + //Driver.makeGame(); } public static void makeGame() throws IOException { diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index 620c6ce..bbfa1cf 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -1,11 +1,42 @@ +import java.util.ArrayList; + public class Tester { - - public void testUnify(){ + + public void testUnify() { Fact f1 = new Fact(); - f1.isVariable = true; Variable v1 = new Variable(); + + v1.isVariable = true; + v1.variableId = 0; + + f1.variables.add(v1); + + v1 = new Variable(); + v1.isVariable = true; + v1.variableId = 1; + + f1.variables.add(v1); + + Fact f2 = new Fact(); + v1 = new Variable(); + v1.isVariable = true; + v1.variableId = 2; + + f2.variables.add(v1); + + v1 = new Variable(); + v1.isVariable = true; + v1.variableId = 3; + + f2.variables.add(v1); + + ArrayList subs = Unifier.unify(f1, f2); + System.out.println(subs.size()); + for(Substitute sub: subs){ + System.out.println(sub.varIdToSubstitute + "/" + sub.valToSubstituteWith); + } } public void testInferenceEngine() { diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index d498ff4..2e5bfa9 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -88,12 +88,12 @@ public static ArrayList unify(Fact f1, Fact f2) { if (tempFact.variables.get(0).isVariable) { Substitute s = new Substitute(); s.varIdToSubstitute = tempFact.variables.get(0).variableId; - s.varIdToSubstitute = tempFact.variables.get(1).variableId; + s.valToSubstituteWith = tempFact.variables.get(1).variableId; subs.add(s); } else if (tempFact.variables.get(1).isVariable) { Substitute s = new Substitute(); s.varIdToSubstitute = tempFact.variables.get(1).variableId; - s.varIdToSubstitute = tempFact.variables.get(0).variableId; + s.valToSubstituteWith = tempFact.variables.get(0).variableId; subs.add(s); } else{ From f3cf78a1b11608988cc4609f7f61a361a8899254 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 11:03:46 -0600 Subject: [PATCH 056/191] clone goes down all the way --- Wumpus/src/Fact.java | 5 ++++- Wumpus/src/Variable.java | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index 3599327..e27a961 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -9,7 +9,10 @@ public class Fact extends Variable { public Fact(Fact fact) { not = fact.not; - variables = fact.variables; + + for(Variable var : fact.variables){ + variables.add(new Variable(var)); + } predicate = fact.predicate; } diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index 0388fdd..5b596e5 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -9,7 +9,13 @@ public class Variable { IFunction function; int variableId; int modifier; - + public Variable(Variable var){ + isVariable = var.isVariable; + value = var.value; + function = var.function; + variableId = var.variableId; + modifier = var.modifier; + } public Variable(){} public Variable(int value, boolean isVariable, IFunction function){ this.isVariable = isVariable; From fe03b70f31b42f19dafd56f1811532aa9a002541 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 14:14:23 -0600 Subject: [PATCH 057/191] Debugging ReactiveExplorer --- Wumpus/src/Agent.java | 22 ++++++++++++------- Wumpus/src/Driver.java | 36 ++++++++++++++++---------------- Wumpus/src/ReactiveExplorer.java | 1 + Wumpus/src/World.java | 2 +- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index bb94749..8fc0340 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -1,7 +1,7 @@ import java.util.Random; -public abstract class Agent { +public class Agent { protected Location location; protected Direction direction; @@ -15,17 +15,21 @@ public Agent(World world) { this.world = world; this.arrowCount = world.arrowCount; this.location = new Location(world.x, world.y); - // this.direction = NORTH; + this.direction = direction.getDirection(0); this.percepts = world.getPercepts(); } public enum Direction { - NORTH, - EAST, - SOUTH, - WEST; + + NORTH(0), EAST(1), SOUTH(2), WEST(3); - private final Direction[] terms = values(); + private final int id; + + private Direction(int id) { + this.id = id; + } + + Direction[] terms = values(); public Direction left() { return terms[(this.ordinal() + 1) % terms.length]; @@ -34,6 +38,10 @@ public Direction left() { public Direction right() { return terms[(this.ordinal() - 1) % terms.length]; } + + public Direction getDirection(int i) { + return terms[i]; + } } public enum State { diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 61abb79..21ef8d2 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -7,34 +7,34 @@ public class Driver { public static void main(String[] args) throws IOException { // Driver driver = new Driver(); - Tester tester = new Tester(); - tester.testUnify(); + //Tester tester = new Tester(); + //tester.testUnify(); // tester.testInferenceEngine(); - //Driver.makeGame(); + makeGame(); } public static void makeGame() throws IOException { - BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); - int[] prob = new int[3]; - System.out.print("% chance of generating pit: "); - prob[0] = Integer.parseInt(dataIn.readLine()); - System.out.println(); - System.out.print("% chance of generating obstacle: "); - prob[1] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - System.out.print("% chance of generating wumpus: "); - prob[2] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - - WumpusGame game = new WumpusGame(5, prob); +// BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); +// int[] prob = new int[3]; +// System.out.print("% chance of generating pit: "); +// prob[0] = Integer.parseInt(dataIn.readLine()); +// System.out.println(); +// System.out.print("% chance of generating obstacle: "); +// prob[1] = Integer.parseInt(dataIn.readLine()); +// System.out.println(""); +// System.out.print("% chance of generating wumpus: "); +// prob[2] = Integer.parseInt(dataIn.readLine()); +// System.out.println(""); +// +// WumpusGame game = new WumpusGame(5, prob); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); World world = new World("PerceptBoard.txt"); Agent explorer = new ReactiveExplorer(world); - world = new World("PerceptBoard.txt"); - explorer = new LogicExplorer(world); + //world = new World("PerceptBoard.txt"); + // explorer = new LogicExplorer(world); } } diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 3b45563..080bf83 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -47,6 +47,7 @@ private void move(int action) { break; case TURN_LEFT: world.action(TURN_LEFT); + System.out.println("direction: " + direction); direction = direction.left(); break; case TURN_RIGHT: diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 1b0b2a6..b2c614d 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -192,7 +192,7 @@ public int action(int action) { System.out.println("Agent elected to end game."); System.exit(0); } - System.out.println("Error shouldn't ever get here"); + System.out.println("Error with world movement, no case exists: " + action); return -1; } From e670cd48de7223d0d1d71abb8f42b9e193efa50f Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 14:44:12 -0600 Subject: [PATCH 058/191] Ewwww bugs --- Wumpus/src/Agent.java | 72 +++++++++------ Wumpus/src/ReactiveExplorer.java | 12 +-- Wumpus/src/World.java | 147 ++++++++++++++++--------------- 3 files changed, 129 insertions(+), 102 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 8fc0340..7f8d37e 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -4,9 +4,10 @@ public class Agent { protected Location location; - protected Direction direction; - protected final int BREEZE = 1, STENCH = 2, BUMP = 4, GLITTER = 8, DEATH = 16, DEATH_WUMPUS = 32, SCREAM = 64; + protected int direction; + protected static final int BREEZE = 1, STENCH = 2, BUMP = 4, GLITTER = 8, DEATH = 16, DEATH_WUMPUS = 32, SCREAM = 64; protected static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; + protected static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3; protected int percepts, arrowCount; protected World world; protected Random random = new Random(); @@ -15,34 +16,51 @@ public Agent(World world) { this.world = world; this.arrowCount = world.arrowCount; this.location = new Location(world.x, world.y); - this.direction = direction.getDirection(0); + this.direction = world.direction; this.percepts = world.getPercepts(); } - - public enum Direction { - - NORTH(0), EAST(1), SOUTH(2), WEST(3); - - private final int id; - - private Direction(int id) { - this.id = id; - } - - Direction[] terms = values(); - - public Direction left() { - return terms[(this.ordinal() + 1) % terms.length]; - } - - public Direction right() { - return terms[(this.ordinal() - 1) % terms.length]; - } - - public Direction getDirection(int i) { - return terms[i]; - } + + public void turnRight() { + direction = (direction + 1) % 4; } + + public void turnLeft() { + direction = (direction - 1) % 4; + } + +// public enum Direction { +// +// NORTH(0), EAST(1), SOUTH(2), WEST(3); +// +// private static final int id = 0; +// +// Direction(int id) { +// +// } +// +// static Direction[] terms = values(); +// +// public Direction left() { +// return terms[(this.ordinal() + 1) % terms.length]; +// } +// +// public Direction right() { +// return terms[(this.ordinal() - 1) % terms.length]; +// } +// +//// public Direction getDirection(int i) { +//// return terms[i - 1]; +//// } +// +// public static Direction getDirection(int id) { +// for (Direction d : terms) { +// if (Direction.id == id) { +// return d; +// } +// } +// throw new IllegalArgumentException("Invalid direction id: " + id); +// } +// } public enum State { SAFE, diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 080bf83..0792070 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -48,11 +48,11 @@ private void move(int action) { case TURN_LEFT: world.action(TURN_LEFT); System.out.println("direction: " + direction); - direction = direction.left(); + turnLeft(); break; case TURN_RIGHT: world.action(TURN_RIGHT); - direction = direction.right(); + turnRight(); break; case SHOOT: world.action(SHOOT); @@ -67,7 +67,8 @@ private void move(int action) { public void decideNextAction() { //select safe neighboring cell else select unsafe neighboring cell if (((percepts & STENCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe - switch (random.nextInt(3)) { + int rand = random.nextInt(3); + switch (rand) { case 1: //go forward move(MOVE); break; @@ -80,7 +81,7 @@ public void decideNextAction() { //select safe neighboring cell else select u move(MOVE); break; default: - System.out.println("Invalid case: random action, reactive explorer (safe)"); + System.out.println("Invalid case: random action, reactive explorer (safe) rand = " + rand); } } else { //neighboring cells may not be safe @@ -89,6 +90,7 @@ public void decideNextAction() { //select safe neighboring cell else select u move(TURN_LEFT); move(MOVE); } else { //pick random move + int rand = random.nextInt(3); switch (random.nextInt(3)) { case 1: //go forward move(MOVE); @@ -102,7 +104,7 @@ public void decideNextAction() { //select safe neighboring cell else select u move(MOVE); break; default: - System.out.println("Invalid case: random action, reactive explorer (unsafe)"); + System.out.println("Invalid case: random action, reactive explorer (unsafe) rand = " + rand); } } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index b2c614d..e530059 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -9,7 +9,7 @@ public final class World { public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; - protected int arrowCount, x, y, direction = 0, score = 0; + protected int arrowCount, x, y, direction = 0, score = 0, numMoves; public static int size; private byte[][] perceptMap; @@ -52,81 +52,85 @@ public int getPercepts() { } public int action(int action) { + System.out.println("Action: " + action); + numMoves++; switch (action) { case GRAB: if ((perceptMap[x][y] & GLITTER) != 0) { perceptMap[x][y] -= GLITTER; + score += 1000; + System.out.println("Gold found!\nScore: " + score); + System.exit(0); + } else { + System.out.println("gold not found error"); } - score += 1000; - System.out.println("Gold found!\nScore: " + score); - System.exit(0); break; case MOVE: score--; - switch (direction) { - case NORTH: - if (y + 1 < size) { - if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { - score -= 1000; - return DEATH_WUMPUS; - } - if ((perceptMap[x][y] & DEATH) == DEATH) { - score -= 1000; - return DEATH; - } - y = y + 1; - return perceptMap[x][y]; - } else { - return BUMP; - } - case EAST: - if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { - score -= 1000; - return DEATH_WUMPUS; - } - if ((perceptMap[x][y] & DEATH) == DEATH) { - score -= 1000; - return DEATH; - } - if (x + 1 < size) { - x = x + 1; - return perceptMap[x][y]; - } else { - return BUMP; - } - case SOUTH: - if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { - score -= 1000; - return DEATH_WUMPUS; - } - if ((perceptMap[x][y] & DEATH) == DEATH) { - score -= 1000; - return DEATH; - } - if (y - 1 > 0) { - y -= 1; - return perceptMap[x][y]; - } else { - return BUMP; - } - case WEST: - if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { - score -= 1000; - return DEATH_WUMPUS; - } - if ((perceptMap[x][y] & DEATH) == DEATH) { - score -= 1000; - return DEATH; - } - if (x - 1 > 0) { - x -= 1; - return perceptMap[x][y]; - } else { - return BUMP; + switch (direction) { + case NORTH: + if (y + 1 < size) { + if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + score -= 1000; + return DEATH_WUMPUS; + } + if ((perceptMap[x][y] & DEATH) == DEATH) { + score -= 1000; + return DEATH; + } + y = y + 1; + return perceptMap[x][y]; + } else { + return BUMP; + } + case EAST: + if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + score -= 1000; + return DEATH_WUMPUS; + } + if ((perceptMap[x][y] & DEATH) == DEATH) { + score -= 1000; + return DEATH; + } + if (x + 1 < size) { + x = x + 1; + return perceptMap[x][y]; + } else { + return BUMP; + } + case SOUTH: + if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + score -= 1000; + return DEATH_WUMPUS; + } + if ((perceptMap[x][y] & DEATH) == DEATH) { + score -= 1000; + return DEATH; + } + if (y - 1 > 0) { + y -= 1; + return perceptMap[x][y]; + } else { + return BUMP; + } + case WEST: + if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + score -= 1000; + return DEATH_WUMPUS; + } + if ((perceptMap[x][y] & DEATH) == DEATH) { + score -= 1000; + return DEATH; + } + if (x - 1 > 0) { + x -= 1; + return perceptMap[x][y]; + } else { + return BUMP; + } + default: + break; } - default: - break; - } break; case TURN_LEFT: direction = (direction + 3) % 4 + 1; @@ -191,13 +195,16 @@ public int action(int action) { case QUIT: System.out.println("Agent elected to end game."); System.exit(0); + break; + default: + System.out.println("Error with world movement, no case exists: " + action); + return -1; } - System.out.println("Error with world movement, no case exists: " + action); return -1; } private void removeWumpus(int x, int y) { - + System.out.println("Wumpus slain at " + x + ", " + y + "!!!"); //remove death_by_wumpus percepts from x, y @@ -213,7 +220,7 @@ private void removeWumpus(int x, int y) { } } if (y > 0) { - //remove stentch below + //remove stentch below perceptMap[x][y - 1] = (byte) (perceptMap[x][y - 1] & ~STENCH); if (y < size - 1) { //remove stentch above From 3bbf3f9cd34db629d44cb14747100bac47861507 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 14:56:33 -0600 Subject: [PATCH 059/191] ReactiveExplorer is fking retarded...bump into wall, turn, turn, bump into same wall --- Wumpus/src/Agent.java | 40 +++----------------------------- Wumpus/src/ReactiveExplorer.java | 39 +++++++++++++++++++------------ Wumpus/src/World.java | 16 +++++++++++++ 3 files changed, 43 insertions(+), 52 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 7f8d37e..d275d07 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -5,7 +5,7 @@ public class Agent { protected Location location; protected int direction; - protected static final int BREEZE = 1, STENCH = 2, BUMP = 4, GLITTER = 8, DEATH = 16, DEATH_WUMPUS = 32, SCREAM = 64; + protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; protected static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3; protected int percepts, arrowCount; @@ -19,49 +19,15 @@ public Agent(World world) { this.direction = world.direction; this.percepts = world.getPercepts(); } - + public void turnRight() { direction = (direction + 1) % 4; } - + public void turnLeft() { direction = (direction - 1) % 4; } -// public enum Direction { -// -// NORTH(0), EAST(1), SOUTH(2), WEST(3); -// -// private static final int id = 0; -// -// Direction(int id) { -// -// } -// -// static Direction[] terms = values(); -// -// public Direction left() { -// return terms[(this.ordinal() + 1) % terms.length]; -// } -// -// public Direction right() { -// return terms[(this.ordinal() - 1) % terms.length]; -// } -// -//// public Direction getDirection(int i) { -//// return terms[i - 1]; -//// } -// -// public static Direction getDirection(int id) { -// for (Direction d : terms) { -// if (Direction.id == id) { -// return d; -// } -// } -// throw new IllegalArgumentException("Invalid direction id: " + id); -// } -// } - public enum State { SAFE, UNSAFE, diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 0792070..2806f67 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -1,12 +1,12 @@ public class ReactiveExplorer extends Agent { - private Location prevPos; + private Location prevLocation; private State curState, prevState; public ReactiveExplorer(World world) { super(world); - prevPos = location; + prevLocation = location; percepts = world.getPercepts(); if (((percepts & STENCH) != STENCH) && ((percepts & BREEZE) != BREEZE)) { curState = State.SAFE; @@ -17,8 +17,10 @@ public ReactiveExplorer(World world) { private void run() { - while (true) { + int i = 0; + while (i < 10) { decideNextAction(); + i++; } } @@ -31,16 +33,15 @@ private void move(int action) { case MOVE: percepts = world.action(MOVE); if ((percepts & BUMP) != BUMP) { //did not bump into anything - prevPos = location; + prevLocation = location; prevState = curState; updateLocation(); } else if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge - move(SHOOT); - move(MOVE); + killWumpus(); } else if ((percepts & DEATH) == DEATH) { //killed by a pit Location temp = location; - location = prevPos; - prevPos = temp; + location = prevLocation; + prevLocation = temp; prevState = State.UNSAFE; curState = State.EXPLORED; } @@ -69,14 +70,14 @@ public void decideNextAction() { //select safe neighboring cell else select u if (((percepts & STENCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe int rand = random.nextInt(3); switch (rand) { - case 1: //go forward + case 0: //go forward move(MOVE); break; - case 2: //turn left and go forward + case 1: //turn left and go forward move(TURN_LEFT); move(MOVE); break; - case 3: //turn right and go forward + case 2: //turn right and go forward move(TURN_RIGHT); move(MOVE); break; @@ -92,21 +93,29 @@ public void decideNextAction() { //select safe neighboring cell else select u } else { //pick random move int rand = random.nextInt(3); switch (random.nextInt(3)) { - case 1: //go forward + case 0: //go forward move(MOVE); break; - case 2: //turn left and go forward + case 1: //turn left and go forward move(TURN_LEFT); move(MOVE); break; - case 3: //turn right and go forward + case 2: //turn right and go forward move(TURN_RIGHT); move(MOVE); break; default: - System.out.println("Invalid case: random action, reactive explorer (unsafe) rand = " + rand); + System.out.println("Invalid case: random action, reactive explorer (unsafe) rand = " + rand); } } } } + + private void killWumpus() { + System.out.println("kill wumpus"); + location = prevLocation; + percepts = world.getPercepts(); + move(SHOOT); + move(MOVE); + } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index e530059..21bfec2 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -50,9 +50,25 @@ public void importMap(String fileName) { public int getPercepts() { return perceptMap[x][y]; } + + private void printWorld() { + + for (int i = 0; i < perceptMap.length; i++) { + for (int j = 0; j < perceptMap.length; j++) { + if (x == i && y == j) { + System.out.print("A "); + } else { + System.out.print(perceptMap[i][j] +" "); + } + } + System.out.println(""); + } + System.out.println(""); + } public int action(int action) { System.out.println("Action: " + action); + printWorld(); numMoves++; switch (action) { case GRAB: From 76424cfedd7ef0e81c17806a2e3c599dcf97d572 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 15:19:03 -0600 Subject: [PATCH 060/191] AWWWWW --- Wumpus/src/Agent.java | 10 +++++++++ Wumpus/src/ReactiveExplorer.java | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index d275d07..cd507d1 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -22,10 +22,20 @@ public Agent(World world) { public void turnRight() { direction = (direction + 1) % 4; + world.action(TURN_RIGHT); } public void turnLeft() { direction = (direction - 1) % 4; + world.action(TURN_LEFT); + } + + public int getRight() { + return (direction + 1) % 4; + } + + public int getLeft() { + return (direction - 1) % 4; } public enum State { diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 2806f67..1b5e29e 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -3,6 +3,9 @@ public class ReactiveExplorer extends Agent { private Location prevLocation; private State curState, prevState; + private boolean stench = false, breeze = false, bump = false, wumpus = false; + private boolean safeMap[][]; + private static final int FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3; public ReactiveExplorer(World world) { super(world); @@ -12,6 +15,8 @@ public ReactiveExplorer(World world) { curState = State.SAFE; prevState = State.SAFE; } + safeMap = new boolean[world.size][world.size]; + safeMap[location.x][location.y] = true; run(); } @@ -23,6 +28,36 @@ private void run() { i++; } } + + private void move() { + + //getpercepts + world.getPercepts(); + + if (getSafe(FORWARD)) { + //go forward + move(MOVE); + } else if (getSafe(LEFT)) { + //go left + + } else if (getSafe(RIGHT)) { + //go right + } else if (getSafe(BACK)) { //back should always be safe? + //idk this bits weird + } + } + + private boolean getSafe(int direction) { + + switch (direction) { + case FORWARD: + + case LEFT: + case RIGHT: + case BACK: + } + return true; + } private void move(int action) { From 6c1e2c3fd6291190c62122575f8120a170b09d54 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Thu, 20 Oct 2016 16:00:09 -0600 Subject: [PATCH 061/191] the struggle continues.. --- Wumpus/src/Agent.java | 7 ++-- Wumpus/src/ReactiveExplorer.java | 66 +++++++++++++++++++------------- Wumpus/src/World.java | 4 +- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index cd507d1..dd97a75 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -4,11 +4,11 @@ public class Agent { protected Location location; - protected int direction; + protected int direction, arrowCount; protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; protected static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3; - protected int percepts, arrowCount; + protected byte percepts; protected World world; protected Random random = new Random(); @@ -41,7 +41,8 @@ public int getLeft() { public enum State { SAFE, UNSAFE, - EXPLORED; + EXPLORED, + UNEXPLORED; } public void updateLocation() { diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 1b5e29e..acd5781 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -28,43 +28,59 @@ private void run() { i++; } } - + private void move() { - + + if ((percepts & STENCH) != STENCH && (percepts & BREEZE) != BREEZE) { + updateSafe(); + //go in random direction, left + } //getpercepts - world.getPercepts(); - + + //need check boundries if (getSafe(FORWARD)) { //go forward move(MOVE); } else if (getSafe(LEFT)) { //go left - + } else if (getSafe(RIGHT)) { //go right } else if (getSafe(BACK)) { //back should always be safe? //idk this bits weird } } - + private boolean getSafe(int direction) { - - switch (direction) { - case FORWARD: - - case LEFT: - case RIGHT: - case BACK: + + int trueDirection = (this.direction - direction) % 4; + switch (trueDirection) { + case NORTH: + return safeMap[location.x][location.y + 1]; + case SOUTH: + return safeMap[location.x][location.y - 1]; + case EAST: + return safeMap[location.x + 1][location.y]; + case WEST: + return safeMap[location.x - 1][location.y]; } + System.out.println("Invalid direction mapping, getSafe()"); return true; } - private void move(int action) { + private void updateSafe() { + + safeMap[location.x + 1][location.y] = true; + safeMap[location.x - 1][location.y] = true; + safeMap[location.x][location.y + 1] = true; + safeMap[location.x][location.y - 1] = true; + } + + private byte move(int action) { switch (action) { case GRAB: - world.action(GRAB); - break; + return world.action(GRAB); case MOVE: percepts = world.action(MOVE); if ((percepts & BUMP) != BUMP) { //did not bump into anything @@ -80,24 +96,20 @@ private void move(int action) { prevState = State.UNSAFE; curState = State.EXPLORED; } - break; + return percepts; case TURN_LEFT: - world.action(TURN_LEFT); - System.out.println("direction: " + direction); turnLeft(); - break; + return world.action(TURN_LEFT); case TURN_RIGHT: - world.action(TURN_RIGHT); turnRight(); - break; + return world.action(TURN_RIGHT); case SHOOT: - world.action(SHOOT); arrowCount--; - break; + return world.action(SHOOT); case QUIT: - world.action(QUIT); - break; + return world.action(QUIT); } + return 0b00000000; } public void decideNextAction() { //select safe neighboring cell else select unsafe neighboring cell @@ -145,7 +157,7 @@ public void decideNextAction() { //select safe neighboring cell else select u } } } - + private void killWumpus() { System.out.println("kill wumpus"); location = prevLocation; diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 21bfec2..b20f422 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -47,7 +47,7 @@ public void importMap(String fileName) { } } - public int getPercepts() { + public byte getPercepts() { return perceptMap[x][y]; } @@ -66,7 +66,7 @@ private void printWorld() { System.out.println(""); } - public int action(int action) { + public byte action(int action) { System.out.println("Action: " + action); printWorld(); numMoves++; From 965c9127610e2ca50a56251341343882f36eefc8 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 16:18:51 -0600 Subject: [PATCH 062/191] Added predicate check in unifier --- Wumpus/src/Unifier.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 2e5bfa9..546a714 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -69,6 +69,10 @@ public static Substitute unify1(Variable p, Variable q, Substitute theta) { } public static ArrayList unify(Fact f1, Fact f2) { + if(!f1.predicate.equals(f2.predicate)){ + return new ArrayList<>(); + } + ArrayList vars = new ArrayList<>(); Fact temp = new Fact(); temp.variables.add(f1.variables.get(0)); From a5e6cecf44b29e465304bb35d29c078c56d1b228 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 16:31:15 -0600 Subject: [PATCH 063/191] added wilsonifier --- Wumpus/src/Unifier.java | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 546a714..175d11c 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -67,10 +67,44 @@ public static Substitute unify1(Variable p, Variable q, Substitute theta) { } return null; } - + public static ArrayList wilsonUnify(Fact f1, Fact f2){ + ArrayList subs = new ArrayList<>(); + if(!f1.predicate.equals(f2.predicate)) + return subs; + + Fact fact1 = new Fact(f1); + Fact fact2 = new Fact(f2); + //check if fact1 has a variable + for (int i = 0; i < fact1.variables.size(); i++) { + Variable tempVar = fact1.variables.get(i); + if(tempVar.isVariable){ + if(!fact2.variables.get(i).isVariable){ + Substitute sub = new Substitute(); + sub.varIdToSubstitute = tempVar.variableId; + sub.valToSubstituteWith = fact2.variables.get(i).value; + subs.add(sub); + } + } + } + for (int i = 0; i < fact2.variables.size(); i++) { + Variable tempVar = fact2.variables.get(i); + if(tempVar.isVariable){ + if(!fact1.variables.get(i).isVariable){ + Substitute sub = new Substitute(); + sub.varIdToSubstitute = tempVar.variableId; + sub.valToSubstituteWith = fact1.variables.get(i).value; + subs.add(sub); + } + } + } + + return subs; + } public static ArrayList unify(Fact f1, Fact f2) { + + ArrayList subs = new ArrayList<>(); if(!f1.predicate.equals(f2.predicate)){ - return new ArrayList<>(); + return subs; } ArrayList vars = new ArrayList<>(); @@ -84,7 +118,6 @@ public static ArrayList unify(Fact f1, Fact f2) { temp.variables.add(f2.variables.get(1)); vars.add(temp); - ArrayList subs = new ArrayList<>(); while (!vars.isEmpty()) { Fact tempFact = vars.remove(0); From dfcf5c342d8d385e72f229cee481f3c1f0244139 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 16:48:10 -0600 Subject: [PATCH 064/191] kek --- Wumpus/src/ReactiveExplorer.java | 24 +++++++++++++++++++++++- Wumpus/src/World.java | 3 ++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index acd5781..5f1303c 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -33,7 +33,29 @@ private void move() { if ((percepts & STENCH) != STENCH && (percepts & BREEZE) != BREEZE) { updateSafe(); - //go in random direction, left + //go in random direction + int rand = random.nextInt(3); + switch (rand) { + case 0: //try to go forward + percepts = world.action(MOVE); + if ((percepts & BUMP) != BUMP) { + return; + } + case 1: //try to go left + turnLeft(); + percepts = world.action(MOVE); + if ((percepts & BUMP) != BUMP) { + return; + } + case 2: //try go right + turnRight(); + percepts = world.action(MOVE); + if ((percepts & BUMP) != BUMP) { + return; + } + default: + System.out.println("Move error, invalid case: " + rand); + } } //getpercepts diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index b20f422..1838d81 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -7,9 +7,10 @@ public final class World { public static final int NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4; public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; + protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; - protected int arrowCount, x, y, direction = 0, score = 0, numMoves; + public static int size; private byte[][] perceptMap; From 9f49929a982afa23473ab6b7b779fa882f01553e Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 16:49:43 -0600 Subject: [PATCH 065/191] git gud zach hacky solution --- Wumpus/src/Agent.java | 8 ++++---- Wumpus/src/Driver.java | 3 ++- Wumpus/src/LogicExplorer.java | 9 +++++++-- Wumpus/src/ReactiveExplorer.java | 4 ++-- Wumpus/src/World.java | 12 +++++++++++- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index dd97a75..a443d12 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -12,11 +12,11 @@ public class Agent { protected World world; protected Random random = new Random(); - public Agent(World world) { + public Agent(World world, int startingArrowCount, int startingX, int startingY, int startingDirection) { this.world = world; - this.arrowCount = world.arrowCount; - this.location = new Location(world.x, world.y); - this.direction = world.direction; + this.arrowCount = startingArrowCount; + this.location = new Location(startingX, startingY); + this.direction = startingDirection; this.percepts = world.getPercepts(); } diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 21ef8d2..b4f98cf 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -31,7 +31,8 @@ public static void makeGame() throws IOException { //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); World world = new World("PerceptBoard.txt"); - Agent explorer = new ReactiveExplorer(world); + world.startGame("LogicExplorer"); + //Agent explorer = new LogicExplorer(world); //world = new World("PerceptBoard.txt"); // explorer = new LogicExplorer(world); diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 12cbd93..6ba8e2f 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -8,13 +8,18 @@ public class LogicExplorer extends Agent { private ArrayList frontier = new ArrayList<>(); private boolean[][] searchedPositions; - public LogicExplorer(World world) { - super(world); + public LogicExplorer(World world, int startingArrows, int startingX, int startingY, int direction) { + super(world,startingArrows,startingX,startingY,direction); kb = new KnowledgeBase(); kb.initializeRules(); this.searchedPositions = new boolean[World.size][World.size]; + initializeFrontier(); run(); } + + public void initializeFrontier(){ + + } private void run() { diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index acd5781..cdad49d 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -7,8 +7,8 @@ public class ReactiveExplorer extends Agent { private boolean safeMap[][]; private static final int FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3; - public ReactiveExplorer(World world) { - super(world); + public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { + super(world, arrows,x,y,direction); prevLocation = location; percepts = world.getPercepts(); if (((percepts & STENCH) != STENCH) && ((percepts & BREEZE) != BREEZE)) { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index b20f422..1f7d5bf 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -9,13 +9,23 @@ public final class World { public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; - protected int arrowCount, x, y, direction = 0, score = 0, numMoves; + private int arrowCount, x, y, direction = 0, score = 0, numMoves; public static int size; private byte[][] perceptMap; public World(String fileName) { importMap(fileName); } + + public void startGame(String id){ + Agent explorer; + if(id.equals("LogicExplorer")) + explorer = new LogicExplorer(this,arrowCount,x,y,direction); + else if(id.equals("ReactiveExplorer")){ + explorer = new ReactiveExplorer(this,arrowCount,x,y,direction); + } + + } public void importMap(String fileName) { try { From 463d0b80838ebf7450e6e78a76b98e24d612d4af Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 16:53:08 -0600 Subject: [PATCH 066/191] Something useless probably --- Wumpus/src/Unifier.java | 49 ++++------------------------------------- 1 file changed, 4 insertions(+), 45 deletions(-) diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 546a714..32a623f 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -26,47 +26,6 @@ that make the two arguments the same, else it returns failure in some fassion (t */ - public static ArrayList unify1(Fact fact1, Fact fact2) { - if (!canUnify(fact1, fact2)) { - return new ArrayList(); - } - - return new ArrayList(); - } - - public static boolean canUnify(Fact fact1, Fact fact2) { - //Predicates don't match - if (!fact1.predicate.equals(fact2.predicate)) { - return false; - } - return true; - } - - //this is the general outline of how to go about unification... - public static Substitute unify1(Variable p, Variable q, Substitute theta) { - - if (p.equals(q)) { //success - theta.varIdToSubstitute = p.variableId; - theta.valToSubstituteWith = q.variableId; - return theta; - } else if (p.variableId != q.variableId) { - //bind p to q and insert (p/q) into theta - - return theta; - } else if (q.variableId != q.variableId) { - //bind q to p and insert (q/p) into theta - return theta; - } else if (true) { //if r is a variable? Occurs checks need to be done here i think - //theta = the union of (theta, {r/s}) - //unify(substitutionOf(theta, p), substitutionOf(theta, q), theta) - } else if (true) { //if s is a variable? Occurs checks need to be done here i think - //theta = the union of (theta, {s/r}) - //unify(substitutionOf(theta, p), substitutionOf(theta, q), theta) - } else { - //failure - } - return null; - } public static ArrayList unify(Fact f1, Fact f2) { if(!f1.predicate.equals(f2.predicate)){ @@ -91,13 +50,13 @@ public static ArrayList unify(Fact f1, Fact f2) { if (!tempFact.variables.get(0).equals(tempFact.variables.get(1))) { if (tempFact.variables.get(0).isVariable) { Substitute s = new Substitute(); - s.varIdToSubstitute = tempFact.variables.get(0).variableId; - s.valToSubstituteWith = tempFact.variables.get(1).variableId; + s.varIdToSubstitute = tempFact.variables.get(0).value; + s.valToSubstituteWith = tempFact.variables.get(1).value; subs.add(s); } else if (tempFact.variables.get(1).isVariable) { Substitute s = new Substitute(); - s.varIdToSubstitute = tempFact.variables.get(1).variableId; - s.valToSubstituteWith = tempFact.variables.get(0).variableId; + s.varIdToSubstitute = tempFact.variables.get(1).value; + s.valToSubstituteWith = tempFact.variables.get(0).value; subs.add(s); } else{ From bb152ce8a51ded2222e756146aae547a8378874e Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 16:59:14 -0600 Subject: [PATCH 067/191] kek --- Wumpus/src/ReactiveExplorer.java | 33 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 5f1303c..d188578 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -31,9 +31,9 @@ private void run() { private void move() { - if ((percepts & STENCH) != STENCH && (percepts & BREEZE) != BREEZE) { + if ((percepts & STENCH) != STENCH && (percepts & BREEZE) != BREEZE) { //all adjacent spaces are safe updateSafe(); - //go in random direction + //go in random direction int rand = random.nextInt(3); switch (rand) { case 0: //try to go forward @@ -53,23 +53,20 @@ private void move() { if ((percepts & BUMP) != BUMP) { return; } - default: - System.out.println("Move error, invalid case: " + rand); + default: //turn around + turnRight(); + turnRight(); + percepts = world.action(MOVE); } - } - //getpercepts - - //need check boundries - if (getSafe(FORWARD)) { - //go forward - move(MOVE); - } else if (getSafe(LEFT)) { - //go left - - } else if (getSafe(RIGHT)) { - //go right - } else if (getSafe(BACK)) { //back should always be safe? - //idk this bits weird + } else { + //if forward is safe, go forward + + + //if left is safe, go left + + //if right is safe, go right + + } } From 445d78f53f3e2c7128ad3759116139b12a1806ad Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 16:58:46 -0600 Subject: [PATCH 068/191] start frontier --- Wumpus/src/LogicExplorer.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 6ba8e2f..d661172 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -13,12 +13,20 @@ public LogicExplorer(World world, int startingArrows, int startingX, int startin kb = new KnowledgeBase(); kb.initializeRules(); this.searchedPositions = new boolean[World.size][World.size]; + searchedPositions[location.x][location.y] = true; initializeFrontier(); run(); } public void initializeFrontier(){ - + if(location.x>0) + frontier.add(new Location(location.x-1,location.y)); + if(location.x < world.size) + frontier.add(new Location(location.x+1,location.y)); + if(location.y>0) + frontier.add(new Location(location.x,location.y-1)); + if(location.y Date: Thu, 20 Oct 2016 17:05:12 -0600 Subject: [PATCH 069/191] Fixed a knowledge base thing --- Wumpus/src/KnowledgeBase.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 0874c11..9c16735 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -86,12 +86,12 @@ public void initializeRules() { rules.add(notWumpusOrNotPit); } - public boolean ask(String question) { + public boolean ask(Fact fact) { //if question follows from known facts ==> return true //else return false - return true; + return inferenceEngine.follows(fact); } public void tell(Clause clause) { From 1b08ac7ca4d44e71cbe4121f15fcdbcb636e4c63 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 17:05:22 -0600 Subject: [PATCH 070/191] getforward --- Wumpus/src/Agent.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index a443d12..27b04b4 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -19,6 +19,17 @@ public Agent(World world, int startingArrowCount, int startingX, int startingY, this.direction = startingDirection; this.percepts = world.getPercepts(); } + + public Location getForward(){ + if(direction == NORTH) + return new Location(location.x,location.y+1); + else if(direction == EAST) + return new Location(location.x+1,location.y); + else if(direction == SOUTH) + return new Location(location.x,location.y-1); + else + return new Location(location.x-1,location.y); + } public void turnRight() { direction = (direction + 1) % 4; From 969cf2bacdb1c5f8942a43902fc0eee2bcb3ddb8 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 17:10:45 -0600 Subject: [PATCH 071/191] KEK! --- Wumpus/src/ReactiveExplorer.java | 54 +++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index e3cda4b..d503b16 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -1,4 +1,6 @@ +import java.util.ArrayList; + public class ReactiveExplorer extends Agent { private Location prevLocation; @@ -8,7 +10,7 @@ public class ReactiveExplorer extends Agent { private static final int FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3; public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { - super(world, arrows,x,y,direction); + super(world, arrows, x, y, direction); prevLocation = location; percepts = world.getPercepts(); if (((percepts & STENCH) != STENCH) && ((percepts & BREEZE) != BREEZE)) { @@ -47,12 +49,14 @@ private void move() { if ((percepts & BUMP) != BUMP) { return; } + turnRight(); case 2: //try go right turnRight(); percepts = world.action(MOVE); if ((percepts & BUMP) != BUMP) { return; } + turnLeft(); default: //turn around turnRight(); turnRight(); @@ -60,13 +64,47 @@ private void move() { } } else { //if forward is safe, go forward - - - //if left is safe, go left - - //if right is safe, go right - - + ArrayList safeMoves = new ArrayList(); + if (getSafe(FORWARD)) { + safeMoves.add(FORWARD); + } + if (getSafe(LEFT)) { + safeMoves.add(LEFT); + } + if (getSafe(RIGHT)) { + safeMoves.add(RIGHT); + } + if (safeMoves.size() > 0) { + int rand = random.nextInt(safeMoves.size()); + percepts = world.action(safeMoves.get(rand)); + } else { + int rand = random.nextInt(3); + switch (rand) { + case 0: + percepts = world.action(MOVE); + if ((percepts & BUMP) != BUMP) { + return; + } + case 1: + turnLeft(); + percepts = world.action(MOVE); + if ((percepts & BUMP) != BUMP) { + return; + } + turnRight(); + case 2: + turnRight(); + percepts = world.action(MOVE); + if ((percepts & BUMP) != BUMP) { + return; + } + turnLeft(); + default: + turnRight(); + turnRight(); + percepts = world.action(MOVE); + } + } } } From c4d15c06355f09b2b0ae284b9a27d1facfd31163 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 17:42:35 -0600 Subject: [PATCH 072/191] logic progress --- Wumpus/src/LogicExplorer.java | 47 ++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index d661172..504bdc9 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -7,6 +7,8 @@ public class LogicExplorer extends Agent { private int previousAction; private ArrayList frontier = new ArrayList<>(); private boolean[][] searchedPositions; + private boolean navigatingToSafePosition; + private Location safeSpace; public LogicExplorer(World world, int startingArrows, int startingX, int startingY, int direction) { super(world,startingArrows,startingX,startingY,direction); @@ -29,6 +31,14 @@ public void initializeFrontier(){ frontier.add(new Location(location.x,location.y+1)); } + public void updateLocation(){ + super.updateLocation(); + expandFrontier(); + } + + public void expandFrontier(){ + //TODO: write + } private void run() { while (true) { @@ -71,7 +81,12 @@ private void processPercepts() { if (((percepts & BUMP) != BUMP) && (percepts & DEATH) != DEATH) { updateLocation(); + searchedPositions[location.x][location.y] = true; } + if((percepts&BUMP)!=0) + kb.tell(new Clause(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null))); + else + kb.tell(new Clause(new Fact("Obsticle",getForward().x,false,getForward().y,false,true,null,null))); if ((percepts & STENCH) != 0) { kb.tell(new Clause(new Fact("Stench", location.x, false, location.y, false, true, null, null))); } else { @@ -83,7 +98,7 @@ private void processPercepts() { kb.tell(new Clause(new Fact("Breeze", location.x, false, location.y, false, false, null, null))); } if ((percepts & SCREAM) != 0) { - kb.tell(new Clause(new Fact("Screem", location.x, false, location.y, false, true, null, null))); + kb.tell(new Clause(new Fact("Scream", location.x, false, location.y, false, true, null, null))); } } @@ -100,6 +115,18 @@ private void decideNextAction() { //check forward //check left //check right + if(getForward().x >= 0 && getForward().x < world.size && getForward().y >= 0 && getForward().y < world.size){ + if(kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,true,null,null))){ + if(kb.ask(new Fact("Pit",getForward().x,false,getForward().y,false,true,null,null))) + if(kb.ask(new Fact("Obsticle",getForward().x,false,getForward().y,false,true,null,null))) + move(World.MOVE); + } + } + else if(safeSpaceInFrontier()){ + //TODO: rhw traversal to safeSpace; + } + + if (kb.ask("!Wumpus(forward)AND!Pit(forward)")) { if (kb.ask("Is ")) { @@ -118,6 +145,24 @@ private void decideNextAction() { } } } + + private boolean safeSpaceInFrontier(){ + for(int i = frontier.size() - 1; i >= 0; i--){ + Location loc = frontier.get(i); + if(kb.ask(new Fact("Wumpus",loc.x,false,loc.y,false,true,null,null))){ + if(kb.ask(new Fact("Pit",loc.x,false,loc.y,false,true,null,null))) + if(kb.ask(new Fact("Obsticle",loc.x,false,loc.y,false,true,null,null))){ + safeSpace = new Location(loc.x,loc.y); + return true; + } + } + if(kb.ask(new Fact("Wumpus",loc.x,false,loc.y,false,false,null,null)) || kb.ask(new Fact("Pit",loc.x,false,loc.y,false,false,null,null))|| kb.ask(new Fact("Obsticle",loc.x,false,loc.y,false,false,null,null))){ + //there is specifically a wumpus, pit, or obsticle at this position, don't navigate to it. + frontier.remove(i); + } + } + return false; + } private void RHWTraversal(String stopCondition) { From f9faa263aa862bd99cf054d04083ed2d1e788d13 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 17:46:36 -0600 Subject: [PATCH 073/191] Dead wumpussy --- Wumpus/src/KnowledgeBase.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 9c16735..353fe1f 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -94,8 +94,17 @@ public boolean ask(Fact fact) { return inferenceEngine.follows(fact); } - public void tell(Clause clause) { + public void tell(Fact fact) { //If told DeadWumpus delete the wumpus entry at that position and add !Wumpus(x,y) + if(fact.predicate.equals("DeadWumpus")){ + for(int i = 0; i < clauses.size(); i++){ + for(int j = 0; j < clauses.get(i).facts.size(); j++){ + if(clauses.get(i).facts.get(j).variables.get(0).value == fact.variables.get(0).value && clauses.get(i).facts.get(j).variables.get(1).value == fact.variables.get(1).value){ + clauses.get(i).facts.get(j).not = true; + } + } + } + } //and check if there is a stench at any adjacent position, remove those facts too } } From 6a54da9de52bb8e998f4c4df0dc227e13a730ee0 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 17:49:51 -0600 Subject: [PATCH 074/191] i didnt break it, i sware! --- Wumpus/src/Agent.java | 28 ++-- Wumpus/src/Driver.java | 4 +- Wumpus/src/LogicExplorer.java | 130 ++++++++++--------- Wumpus/src/ReactiveExplorer.java | 211 ++++++++++++++++--------------- Wumpus/src/World.java | 60 ++++++--- 5 files changed, 235 insertions(+), 198 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 27b04b4..043058f 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -19,16 +19,17 @@ public Agent(World world, int startingArrowCount, int startingX, int startingY, this.direction = startingDirection; this.percepts = world.getPercepts(); } - - public Location getForward(){ - if(direction == NORTH) - return new Location(location.x,location.y+1); - else if(direction == EAST) - return new Location(location.x+1,location.y); - else if(direction == SOUTH) - return new Location(location.x,location.y-1); - else - return new Location(location.x-1,location.y); + + public Location getForward() { + if (direction == NORTH) { + return new Location(location.x, location.y + 1); + } else if (direction == EAST) { + return new Location(location.x + 1, location.y); + } else if (direction == SOUTH) { + return new Location(location.x, location.y - 1); + } else { + return new Location(location.x - 1, location.y); + } } public void turnRight() { @@ -40,16 +41,17 @@ public void turnLeft() { direction = (direction - 1) % 4; world.action(TURN_LEFT); } - + public int getRight() { return (direction + 1) % 4; } - + public int getLeft() { return (direction - 1) % 4; } - public enum State { + public enum State { //im not sure were going to need this + SAFE, UNSAFE, EXPLORED, diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index b4f98cf..dc9d60f 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -31,11 +31,11 @@ public static void makeGame() throws IOException { //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); World world = new World("PerceptBoard.txt"); - world.startGame("LogicExplorer"); + world.startGame("ReactiveExplorer"); //Agent explorer = new LogicExplorer(world); //world = new World("PerceptBoard.txt"); - // explorer = new LogicExplorer(world); + // Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction, world.getPercepts(), world.arrowCount); } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index d661172..febb891 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -9,7 +9,7 @@ public class LogicExplorer extends Agent { private boolean[][] searchedPositions; public LogicExplorer(World world, int startingArrows, int startingX, int startingY, int direction) { - super(world,startingArrows,startingX,startingY,direction); + super(world, startingArrows, startingX, startingY, direction); kb = new KnowledgeBase(); kb.initializeRules(); this.searchedPositions = new boolean[World.size][World.size]; @@ -17,22 +17,28 @@ public LogicExplorer(World world, int startingArrows, int startingX, int startin initializeFrontier(); run(); } - - public void initializeFrontier(){ - if(location.x>0) - frontier.add(new Location(location.x-1,location.y)); - if(location.x < world.size) - frontier.add(new Location(location.x+1,location.y)); - if(location.y>0) - frontier.add(new Location(location.x,location.y-1)); - if(location.y 0) { + frontier.add(new Location(location.x - 1, location.y)); + } + if (location.x < world.size) { + frontier.add(new Location(location.x + 1, location.y)); + } + if (location.y > 0) { + frontier.add(new Location(location.x, location.y - 1)); + } + if (location.y < world.size) { + frontier.add(new Location(location.x, location.y + 1)); + } } private void run() { - while (true) { + int i = 0; + while (i < 10) { decideNextAction(); + i++; } } @@ -88,55 +94,55 @@ private void processPercepts() { } private void decideNextAction() { - - if (frontier.isEmpty()) { - move(World.QUIT); - } - if ((percepts & GLITTER) != 0) { - move(World.GRAB); - } - - //check if adjacent to any unexplored and safe spaces - //check forward - //check left - //check right - if (kb.ask("!Wumpus(forward)AND!Pit(forward)")) { - if (kb.ask("Is ")) { - - } else if (kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")) { - move(World.MOVE); - } else if ("safeSpotInFrontier?" == "") { - RHWTraversal("!Wumpus(adjacent)AND!Pit(adjacent)"); - } else if ("KnownWumpusSpotInFrontier" == "") { - //kill wumpus - RHWTraversal("Wumpus(adjacent)"); - move(World.SHOOT); - } else { - //go to random spot in frontier that is not definite death - Location target = frontier.get(random.nextInt(frontier.size())); - RHWTraversal("At(target)"); - } - } - } - - private void RHWTraversal(String stopCondition) { - - do { - if (kb.ask("right is safe")) { - move(4); //turn right - } else { - if (kb.ask("forward is safe")) { - move(2); //go forward - } else { - move(3); //turn left - } - } - } while (!kb.ask(stopCondition)); - - //face stop condition - while (!kb.ask("am i facing the stop condition?")) { - move(3); //turn until facing stp condition - } - move(2); +// +// if (frontier.isEmpty()) { +// move(World.QUIT); +// } +// if ((percepts & GLITTER) != 0) { +// move(World.GRAB); +// } +// +// //check if adjacent to any unexplored and safe spaces +// //check forward +// //check left +// //check right +// if (kb.ask("!Wumpus(forward)AND!Pit(forward)")) { +// if (kb.ask("Is ")) { +// +// } else if (kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")) { +// move(World.MOVE); +// } else if ("safeSpotInFrontier?" == "") { +// RHWTraversal("!Wumpus(adjacent)AND!Pit(adjacent)"); +// } else if ("KnownWumpusSpotInFrontier" == "") { +// //kill wumpus +// RHWTraversal("Wumpus(adjacent)"); +// move(World.SHOOT); +// } else { +// //go to random spot in frontier that is not definite death +// Location target = frontier.get(random.nextInt(frontier.size())); +// RHWTraversal("At(target)"); +// } +// } +// } +// +// private void RHWTraversal(String stopCondition) { +// +// do { +// if (kb.ask("right is safe")) { +// move(4); //turn right +// } else { +// if (kb.ask("forward is safe")) { +// move(2); //go forward +// } else { +// move(3); //turn left +// } +// } +// } while (!kb.ask(stopCondition)); +// +// //face stop condition +// while (!kb.ask("am i facing the stop condition?")) { +// move(3); //turn until facing stp condition +// } +// move(2); } } diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index d503b16..d01fa27 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -5,7 +5,6 @@ public class ReactiveExplorer extends Agent { private Location prevLocation; private State curState, prevState; - private boolean stench = false, breeze = false, bump = false, wumpus = false; private boolean safeMap[][]; private static final int FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3; @@ -17,7 +16,7 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { curState = State.SAFE; prevState = State.SAFE; } - safeMap = new boolean[world.size][world.size]; + safeMap = new boolean[World.size][World.size]; safeMap[location.x][location.y] = true; run(); } @@ -26,7 +25,7 @@ private void run() { int i = 0; while (i < 10) { - decideNextAction(); + move(); i++; } } @@ -110,116 +109,126 @@ private void move() { private boolean getSafe(int direction) { - int trueDirection = (this.direction - direction) % 4; - switch (trueDirection) { - case NORTH: - return safeMap[location.x][location.y + 1]; - case SOUTH: - return safeMap[location.x][location.y - 1]; - case EAST: - return safeMap[location.x + 1][location.y]; - case WEST: - return safeMap[location.x - 1][location.y]; + try { + int trueDirection = (this.direction - direction + 4) % 4; + switch (trueDirection) { + case NORTH: + return safeMap[location.x][location.y + 1]; + case SOUTH: + return safeMap[location.x][location.y - 1]; + case EAST: + return safeMap[location.x + 1][location.y]; + case WEST: + return safeMap[location.x - 1][location.y]; + } + System.out.println("Invalid direction mapping, getSafe(): " + direction + " " + trueDirection); + return true; + } catch (ArrayIndexOutOfBoundsException ex) { + return false; } - System.out.println("Invalid direction mapping, getSafe()"); - return true; } private void updateSafe() { - safeMap[location.x + 1][location.y] = true; - safeMap[location.x - 1][location.y] = true; - safeMap[location.x][location.y + 1] = true; - safeMap[location.x][location.y - 1] = true; - } - - private byte move(int action) { - - switch (action) { - case GRAB: - return world.action(GRAB); - case MOVE: - percepts = world.action(MOVE); - if ((percepts & BUMP) != BUMP) { //did not bump into anything - prevLocation = location; - prevState = curState; - updateLocation(); - } else if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge - killWumpus(); - } else if ((percepts & DEATH) == DEATH) { //killed by a pit - Location temp = location; - location = prevLocation; - prevLocation = temp; - prevState = State.UNSAFE; - curState = State.EXPLORED; - } - return percepts; - case TURN_LEFT: - turnLeft(); - return world.action(TURN_LEFT); - case TURN_RIGHT: - turnRight(); - return world.action(TURN_RIGHT); - case SHOOT: - arrowCount--; - return world.action(SHOOT); - case QUIT: - return world.action(QUIT); + if (location.x < World.size) { + safeMap[location.x + 1][location.y] = true; } - return 0b00000000; - } - - public void decideNextAction() { //select safe neighboring cell else select unsafe neighboring cell - - if (((percepts & STENCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe - int rand = random.nextInt(3); - switch (rand) { - case 0: //go forward - move(MOVE); - break; - case 1: //turn left and go forward - move(TURN_LEFT); - move(MOVE); - break; - case 2: //turn right and go forward - move(TURN_RIGHT); - move(MOVE); - break; - default: - System.out.println("Invalid case: random action, reactive explorer (safe) rand = " + rand); - } - } else { //neighboring cells may not be safe - - if (prevState == State.SAFE) { //there might be a situation where the agent move back and forth between 3 safe safe spaces here we might need to account for - move(TURN_LEFT); - move(TURN_LEFT); - move(MOVE); - } else { //pick random move - int rand = random.nextInt(3); - switch (random.nextInt(3)) { - case 0: //go forward - move(MOVE); - break; - case 1: //turn left and go forward - move(TURN_LEFT); - move(MOVE); - break; - case 2: //turn right and go forward - move(TURN_RIGHT); - move(MOVE); - break; - default: - System.out.println("Invalid case: random action, reactive explorer (unsafe) rand = " + rand); - } - } + if (location.x > 0) { + safeMap[location.x - 1][location.y] = true; + } + if (location.y < World.size) { + safeMap[location.x][location.y + 1] = true; + } + if (location.y > 0) { + safeMap[location.x][location.y - 1] = true; } } +// private byte move(int action) { +// +// switch (action) { +// case GRAB: +// return world.action(GRAB); +// case MOVE: +// percepts = world.action(MOVE); +// if ((percepts & BUMP) != BUMP) { //did not bump into anything +// prevLocation = location; +// prevState = curState; +// updateLocation(); +// } else if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge +// killWumpus(); +// } else if ((percepts & DEATH) == DEATH) { //killed by a pit +// Location temp = location; +// location = prevLocation; +// prevLocation = temp; +// prevState = State.UNSAFE; +// curState = State.EXPLORED; +// } +// return percepts; +// case TURN_LEFT: +// turnLeft(); +// return world.action(TURN_LEFT); +// case TURN_RIGHT: +// turnRight(); +// return world.action(TURN_RIGHT); +// case SHOOT: +// arrowCount--; +// return world.action(SHOOT); +// case QUIT: +// return world.action(QUIT); +// } +// return 0b00000000; +// } +// public void decideNextAction() { //select safe neighboring cell else select unsafe neighboring cell +// +// if (((percepts & STENCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe +// int rand = random.nextInt(3); +// switch (rand) { +// case 0: //go forward +// move(MOVE); +// break; +// case 1: //turn left and go forward +// move(TURN_LEFT); +// move(MOVE); +// break; +// case 2: //turn right and go forward +// move(TURN_RIGHT); +// move(MOVE); +// break; +// default: +// System.out.println("Invalid case: random action, reactive explorer (safe) rand = " + rand); +// } +// } else { //neighboring cells may not be safe +// +// if (prevState == State.SAFE) { //there might be a situation where the agent move back and forth between 3 safe safe spaces here we might need to account for +// move(TURN_LEFT); +// move(TURN_LEFT); +// move(MOVE); +// } else { //pick random move +// int rand = random.nextInt(3); +// switch (random.nextInt(3)) { +// case 0: //go forward +// move(MOVE); +// break; +// case 1: //turn left and go forward +// move(TURN_LEFT); +// move(MOVE); +// break; +// case 2: //turn right and go forward +// move(TURN_RIGHT); +// move(MOVE); +// break; +// default: +// System.out.println("Invalid case: random action, reactive explorer (unsafe) rand = " + rand); +// } +// } +// } +// } private void killWumpus() { System.out.println("kill wumpus"); location = prevLocation; percepts = world.getPercepts(); - move(SHOOT); - move(MOVE); + world.action(SHOOT); + world.action(MOVE); } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 1f7d5bf..26acaee 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -9,22 +9,22 @@ public final class World { public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; - private int arrowCount, x, y, direction = 0, score = 0, numMoves; + private int arrowCount, x, y, direction = 0, score = 0, numMoves = 0, pitDeaths = 0, wumpusDeaths = 0; public static int size; private byte[][] perceptMap; public World(String fileName) { importMap(fileName); } - - public void startGame(String id){ + + public void startGame(String id) { Agent explorer; - if(id.equals("LogicExplorer")) - explorer = new LogicExplorer(this,arrowCount,x,y,direction); - else if(id.equals("ReactiveExplorer")){ - explorer = new ReactiveExplorer(this,arrowCount,x,y,direction); + if (id.equals("LogicExplorer")) { + explorer = new LogicExplorer(this, arrowCount, x, y, direction); + } else if (id.equals("ReactiveExplorer")) { + explorer = new ReactiveExplorer(this, arrowCount, x, y, direction); } - + } public void importMap(String fileName) { @@ -45,13 +45,13 @@ public void importMap(String fileName) { } i++; } - System.out.println(""); - for (byte[] row : perceptMap) { - for (int j = 0; j < row.length; j++) { - System.out.print(row[j] + " "); - } - System.out.println(""); - } +// System.out.println(""); +// for (byte[] row : perceptMap) { +// for (int j = 0; j < row.length; j++) { +// System.out.print(row[j] + " "); +// } +// System.out.println(""); +// } } catch (IOException | NumberFormatException e) { System.out.println("Exception caught: " + e); } @@ -60,25 +60,30 @@ public void importMap(String fileName) { public byte getPercepts() { return perceptMap[x][y]; } - + private void printWorld() { - + for (int i = 0; i < perceptMap.length; i++) { for (int j = 0; j < perceptMap.length; j++) { if (x == i && y == j) { System.out.print("A "); } else { - System.out.print(perceptMap[i][j] +" "); + if ((perceptMap[i][j] & DEATH_WUMPUS) != 0) { + System.out.print("W "); + } else { + System.out.print(perceptMap[i][j] + " "); + } } } System.out.println(""); - } + } System.out.println(""); } public byte action(int action) { - System.out.println("Action: " + action); printWorld(); + System.out.println("Action: " + action); + System.out.println(""); numMoves++; switch (action) { case GRAB: @@ -98,10 +103,14 @@ public byte action(int action) { if (y + 1 < size) { if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; + wumpusDeaths++; + System.out.println("Death to wumpus."); return DEATH_WUMPUS; } if ((perceptMap[x][y] & DEATH) == DEATH) { score -= 1000; + pitDeaths++; + System.out.println("Death to pit."); return DEATH; } y = y + 1; @@ -112,10 +121,13 @@ public byte action(int action) { case EAST: if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; + wumpusDeaths++; + System.out.println("Death to wumpus."); return DEATH_WUMPUS; } if ((perceptMap[x][y] & DEATH) == DEATH) { score -= 1000; + pitDeaths++; return DEATH; } if (x + 1 < size) { @@ -127,10 +139,14 @@ public byte action(int action) { case SOUTH: if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; + wumpusDeaths++; + System.out.println("Death to wumpus."); return DEATH_WUMPUS; } if ((perceptMap[x][y] & DEATH) == DEATH) { score -= 1000; + pitDeaths++; + System.out.println("Death to pit."); return DEATH; } if (y - 1 > 0) { @@ -142,10 +158,14 @@ public byte action(int action) { case WEST: if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; + wumpusDeaths++; + System.out.println("Death to wumpus."); return DEATH_WUMPUS; } if ((perceptMap[x][y] & DEATH) == DEATH) { score -= 1000; + pitDeaths++; + System.out.println("Death to pit."); return DEATH; } if (x - 1 > 0) { From 45efa1f2643d65911f96e0a8d54f93441add60bf Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 17:56:36 -0600 Subject: [PATCH 075/191] good changes --- Wumpus/src/LogicExplorer.java | 79 ++++++++++++++++------------------- 1 file changed, 37 insertions(+), 42 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 504bdc9..f033c26 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -76,9 +76,18 @@ private void move(int action) { System.out.println("Error processing movement."); } } + + public void removeFromFrontier(Location locToRemove){ + for(Location loc : frontier){ + if(loc.x == locToRemove.x && loc.y == locToRemove.y) + frontier.remove(loc); + } + } private void processPercepts() { - + if((percepts & DEATH) != 0){ + removeFromFrontier(getForward()); + } if (((percepts & BUMP) != BUMP) && (percepts & DEATH) != DEATH) { updateLocation(); searchedPositions[location.x][location.y] = true; @@ -103,7 +112,6 @@ private void processPercepts() { } private void decideNextAction() { - if (frontier.isEmpty()) { move(World.QUIT); } @@ -123,27 +131,14 @@ private void decideNextAction() { } } else if(safeSpaceInFrontier()){ - //TODO: rhw traversal to safeSpace; - } - - - if (kb.ask("!Wumpus(forward)AND!Pit(forward)")) { - if (kb.ask("Is ")) { - - } else if (kb.ask("!Wumpus(forwardspot)AND!Pit(forwardSpot)&!Obstical(forwardSpot)")) { - move(World.MOVE); - } else if ("safeSpotInFrontier?" == "") { - RHWTraversal("!Wumpus(adjacent)AND!Pit(adjacent)"); - } else if ("KnownWumpusSpotInFrontier" == "") { - //kill wumpus - RHWTraversal("Wumpus(adjacent)"); - move(World.SHOOT); - } else { - //go to random spot in frontier that is not definite death - Location target = frontier.get(random.nextInt(frontier.size())); - RHWTraversal("At(target)"); - } + rhwTraversal(safeSpace); } + else + rhwTraversal(frontier.get(0)); + } + + private void rhwTraversal(Location location){ + //go to location zach } private boolean safeSpaceInFrontier(){ @@ -164,24 +159,24 @@ private boolean safeSpaceInFrontier(){ return false; } - private void RHWTraversal(String stopCondition) { - - do { - if (kb.ask("right is safe")) { - move(4); //turn right - } else { - if (kb.ask("forward is safe")) { - move(2); //go forward - } else { - move(3); //turn left - } - } - } while (!kb.ask(stopCondition)); - - //face stop condition - while (!kb.ask("am i facing the stop condition?")) { - move(3); //turn until facing stp condition - } - move(2); - } +// private void RHWTraversal(String stopCondition) { +// +// do { +// if (kb.ask("right is safe")) { +// move(4); //turn right +// } else { +// if (kb.ask("forward is safe")) { +// move(2); //go forward +// } else { +// move(3); //turn left +// } +// } +// } while (!kb.ask(stopCondition)); +// +// //face stop condition +// while (!kb.ask("am i facing the stop condition?")) { +// move(3); //turn until facing stp condition +// } +// move(2); +// } } From 88c1df263ae1af416071e5f73366181badd05747 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 18:03:36 -0600 Subject: [PATCH 076/191] toPush --- Wumpus/src/Fact.java | 2 ++ Wumpus/src/LogicExplorer.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index e27a961..ba7f3a2 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -23,6 +23,8 @@ public Fact() { public Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function) { Variable var1 = new Variable(var1Val, var1Var, var1Function); Variable var2 = new Variable(var2Val, var2Var, var2Function); + variables.add(var1); + variables.add(var2); this.predicate = predicate; this.not = not; } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index f033c26..518e484 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -42,6 +42,8 @@ public void expandFrontier(){ private void run() { while (true) { + percepts = world.getPercepts(); + processPercepts(); decideNextAction(); } } From 19547c7d5ee870b1127f14dc706b822a336c1e50 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 18:16:10 -0600 Subject: [PATCH 077/191] push --- Wumpus/src/Driver.java | 2 +- Wumpus/src/KnowledgeBase.java | 1 + Wumpus/src/LogicExplorer.java | 14 +++++++------- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index dc9d60f..6c6e454 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -31,7 +31,7 @@ public static void makeGame() throws IOException { //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); World world = new World("PerceptBoard.txt"); - world.startGame("ReactiveExplorer"); + world.startGame("LogicExplorer"); //Agent explorer = new LogicExplorer(world); //world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 353fe1f..0bc3fe2 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -105,6 +105,7 @@ public void tell(Fact fact) { } } } + clauses.add(new Clause(fact)); //and check if there is a stench at any adjacent position, remove those facts too } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 518e484..16332d1 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -95,21 +95,21 @@ private void processPercepts() { searchedPositions[location.x][location.y] = true; } if((percepts&BUMP)!=0) - kb.tell(new Clause(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null))); + kb.tell(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null)); else - kb.tell(new Clause(new Fact("Obsticle",getForward().x,false,getForward().y,false,true,null,null))); + kb.tell(new Fact("Obsticle",getForward().x,false,getForward().y,false,true,null,null)); if ((percepts & STENCH) != 0) { - kb.tell(new Clause(new Fact("Stench", location.x, false, location.y, false, true, null, null))); + kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); } else { - kb.tell(new Clause(new Fact("Stench", location.x, false, location.y, false, false, null, null))); + kb.tell(new Fact("Stench", location.x, false, location.y, false, false, null, null)); } if ((percepts & BREEZE) != 0) { - kb.tell(new Clause(new Fact("Breeze", location.x, false, location.y, false, true, null, null))); + kb.tell(new Fact("Breeze", location.x, false, location.y, false, true, null, null)); } else { - kb.tell(new Clause(new Fact("Breeze", location.x, false, location.y, false, false, null, null))); + kb.tell(new Fact("Breeze", location.x, false, location.y, false, false, null, null)); } if ((percepts & SCREAM) != 0) { - kb.tell(new Clause(new Fact("Scream", location.x, false, location.y, false, true, null, null))); + kb.tell(new Fact("Scream", location.x, false, location.y, false, true, null, null)); } } From 78b0c984f003ce51689ed59feadb81185af2e779 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 18:28:41 -0600 Subject: [PATCH 078/191] idiots now start at correct position --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/ReactiveExplorer.java | 4 ++-- Wumpus/src/World.java | 10 ++++++++-- Wumpus/src/WumpusGame.java | 9 ++++++++- Wumpus/world.txt | 8 ++++---- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 1db1f24..fe71f39 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 -1 0 0 1 32 -32 1 0 0 1 -1 0 0 4 0 -0 0 0 1 0 -0 0 1 32 9 +5 4 2 +0 0 2 0 0 +0 2 16 6 0 +0 8 2 0 0 +0 0 0 0 0 +0 0 0 0 0 diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index d01fa27..9522635 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -130,13 +130,13 @@ private boolean getSafe(int direction) { private void updateSafe() { - if (location.x < World.size) { + if (location.x < World.size - 1) { safeMap[location.x + 1][location.y] = true; } if (location.x > 0) { safeMap[location.x - 1][location.y] = true; } - if (location.y < World.size) { + if (location.y < World.size - 1) { safeMap[location.x][location.y + 1] = true; } if (location.y > 0) { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 26acaee..803de9d 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -31,8 +31,14 @@ public void importMap(String fileName) { try { FileReader in = new FileReader(fileName); BufferedReader reader = new BufferedReader(in); - String next; - size = Integer.parseInt(reader.readLine()); + String next = reader.readLine(); + size = Integer.parseInt(next.substring(0, next.indexOf(" "))); + next = next.substring(next.indexOf(" ") + 1); + x = Integer.parseInt(next.substring(0,next.indexOf(" "))); + next = next.substring(next.indexOf(" ") + 1); + y = Integer.parseInt(next.substring(0, next.indexOf(" "))); + System.out.println(x); + System.out.println(y); perceptMap = new byte[size][size]; int i = 0; while ((next = reader.readLine()) != null) {//((Integer) reader.read()).toString()).equals("-1")) { diff --git a/Wumpus/src/WumpusGame.java b/Wumpus/src/WumpusGame.java index c657bd6..3e36c8b 100644 --- a/Wumpus/src/WumpusGame.java +++ b/Wumpus/src/WumpusGame.java @@ -12,6 +12,8 @@ public class WumpusGame { private int boardSize; private int wumpus; + private int startX; + private int startY; private Space[][] board; private Random random = new Random(); private HashMap probabilityGeneration; @@ -65,6 +67,8 @@ public void initializeBoard() { x = random.nextInt(boardSize); y = random.nextInt(boardSize); } + startX = x; + startY = y; placeStart(x, y); } @@ -135,7 +139,10 @@ public void printBoards() { } public void printPerceptBoard() { - out.println(boardSize); + out.print(boardSize + " "); + out.print(startX + " "); + out.print(startY + " "); + out.println(); for (int i = 0; i < boardSize; i++) { for (int j = 0; j < boardSize; j++) { out.print(perceptBoard[i][j] + " "); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 782a411..c887bde 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -0 0 0 0 H -H 0 S 0 0 -0 0 0 I 0 0 0 0 0 0 -0 0 0 H G +0 0 W I 0 +0 G 0 0 0 +0 0 0 0 0 +0 0 S 0 0 From d5d0db36e42ad672e888260ecd55f92ddf1fe1ab Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 18:48:55 -0600 Subject: [PATCH 079/191] sall --- Wumpus/src/InferenceEngine.java | 5 +++++ Wumpus/src/KnowledgeBase.java | 12 ++++++++++-- Wumpus/src/LogicExplorer.java | 6 +++++- Wumpus/src/Unifier.java | 4 ++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index b187099..fb4bee6 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -79,6 +79,8 @@ public void infer(Fact fact) { if (var.isVariable && var.variableId == sub.varIdToSubstitute) { var.isVariable = false; var.value = sub.valToSubstituteWith; + if(var.function != null) + var.value = var.function.process(var.value); } } for (Fact tempFact : clause.facts) { @@ -87,6 +89,8 @@ public void infer(Fact fact) { if (var.isVariable && var.variableId == sub.varIdToSubstitute) { var.isVariable = false; var.value = sub.valToSubstituteWith; + if(var.function != null) + var.value = var.function.process(var.value); } } } @@ -135,6 +139,7 @@ public void infer(Clause addedClause) { } clause.facts.remove(fact); kb.addToClauses(clause); + break; } } } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 0bc3fe2..a138de0 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -13,7 +13,15 @@ public ArrayList getClauses(){ public void addToClauses(Clause clause){ if(clause.facts.size() == 0) return; - else if(clause.facts.size() == 1){ + else{ + for(Fact fact : clause.facts){ + for(Variable var : fact.variables){ + if(var.isVariable) + return; + } + } + } + if(clause.facts.size() == 1){ inferenceEngine.infer(clause.facts.get(0)); clauses.add(clause); } @@ -105,7 +113,7 @@ public void tell(Fact fact) { } } } - clauses.add(new Clause(fact)); + addToClauses(new Clause(fact)); //and check if there is a stench at any adjacent position, remove those facts too } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 16332d1..e27bca9 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -9,6 +9,7 @@ public class LogicExplorer extends Agent { private boolean[][] searchedPositions; private boolean navigatingToSafePosition; private Location safeSpace; + boolean notFirstMove = false; public LogicExplorer(World world, int startingArrows, int startingX, int startingY, int direction) { super(world,startingArrows,startingX,startingY,direction); @@ -32,7 +33,10 @@ public void initializeFrontier(){ } public void updateLocation(){ - super.updateLocation(); + if(notFirstMove) + super.updateLocation(); + else + notFirstMove = true; expandFrontier(); } diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index a6ece2d..9045cb0 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -67,7 +67,7 @@ public static Substitute unify1(Variable p, Variable q, Substitute theta) { } return null; } - public static ArrayList wilsonUnify(Fact f1, Fact f2){ + public static ArrayList unify(Fact f1, Fact f2){ ArrayList subs = new ArrayList<>(); if(!f1.predicate.equals(f2.predicate)) return subs; @@ -100,7 +100,7 @@ public static ArrayList wilsonUnify(Fact f1, Fact f2){ return subs; } - public static ArrayList unify(Fact f1, Fact f2) { + public static ArrayList unify2(Fact f1, Fact f2) { ArrayList subs = new ArrayList<>(); if(!f1.predicate.equals(f2.predicate)){ From ef16bb6fc78e1f6cb0f4c62c6e8cbb0b8ce241dc Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 18:49:21 -0600 Subject: [PATCH 080/191] fa --- Wumpus/PerceptBoard.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index fe71f39..1db1f24 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 4 2 -0 0 2 0 0 -0 2 16 6 0 -0 8 2 0 0 -0 0 0 0 0 -0 0 0 0 0 +5 +1 0 0 1 32 +32 1 0 0 1 +1 0 0 4 0 +0 0 0 1 0 +0 0 1 32 9 From aa207ce42b72c76c861995899218d62fb2f62679 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 18:51:36 -0600 Subject: [PATCH 081/191] push --- Wumpus/src/InferenceEngine.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index fb4bee6..e833ad5 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -60,8 +60,9 @@ public boolean follows(Fact fact) { //Step 3: If all facts become empty, return true, else if all facts are exhausted, return false } - public void infer(Fact fact) { + public void infer(Fact factStart) { //look at each fact in each clause in kb.rules, if predicates match, and negations are opposite + Fact fact = new Fact(factStart); ArrayList tempClone = new ArrayList<>(kb.rules); ArrayList kbClausesClone = new ArrayList<>(); for (Clause tempClause : tempClone) { @@ -108,12 +109,13 @@ public void infer(Fact fact) { //add the rule copy with the substitution to kb.clauses } - public void infer(Clause addedClause) { + public void infer(Clause clauseToCheck) { //look in kb.rules, only use rules with a single fact //for each such fact, go through the clause and look for a matching predicate //if negations are opposite, apply unification, if unification exists.. //with the substitution, apply to copies of both //strip fact from input clause + Clause addedClause = new Clause(clauseToCheck); for (Clause ruleClause : kb.rules) { if (ruleClause.facts.size() == 1) { Fact ruleFact = new Fact(ruleClause.facts.get(0));//we use a copy From 86dad1808252ab1e95d644dd16f3bef32ef55120 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 19:23:52 -0600 Subject: [PATCH 082/191] stuff --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/InferenceEngine.java | 7 ++++++- Wumpus/src/LogicExplorer.java | 2 -- Wumpus/src/World.java | 2 +- Wumpus/world.txt | 8 ++++---- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 1db1f24..29424d5 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 -1 0 0 1 32 -32 1 0 0 1 -1 0 0 4 0 -0 0 0 1 0 -0 0 1 32 9 +5 3 1 +4 0 0 0 0 +9 0 0 0 0 +32 1 1 2 1 +1 1 34 17 34 +0 0 1 2 1 diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index e833ad5..a8067d1 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -115,8 +115,13 @@ public void infer(Clause clauseToCheck) { //if negations are opposite, apply unification, if unification exists.. //with the substitution, apply to copies of both //strip fact from input clause + ArrayList tempClone = new ArrayList<>(kb.rules); + ArrayList kbClausesClone = new ArrayList<>(); + for (Clause tempClause : tempClone) { + kbClausesClone.add(new Clause(tempClause)); + } Clause addedClause = new Clause(clauseToCheck); - for (Clause ruleClause : kb.rules) { + for (Clause ruleClause : kbClausesClone) { if (ruleClause.facts.size() == 1) { Fact ruleFact = new Fact(ruleClause.facts.get(0));//we use a copy Clause clause = new Clause(addedClause);//copy the added clause, redo each time diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index e27bca9..833b8ff 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -100,8 +100,6 @@ private void processPercepts() { } if((percepts&BUMP)!=0) kb.tell(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null)); - else - kb.tell(new Fact("Obsticle",getForward().x,false,getForward().y,false,true,null,null)); if ((percepts & STENCH) != 0) { kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); } else { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 803de9d..d6a8cea 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -5,7 +5,7 @@ public final class World { - public static final int NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4; + public static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3; public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; diff --git a/Wumpus/world.txt b/Wumpus/world.txt index c887bde..1b40f8e 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 +I 0 0 0 0 +G 0 0 0 0 +H 0 0 0 0 +0 S H W H 0 0 0 0 0 -0 0 W I 0 -0 G 0 0 0 -0 0 0 0 0 -0 0 S 0 0 From 10a9a3d7454e8af9505eae1c9a6122f51a7c94cc Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 19:25:05 -0600 Subject: [PATCH 083/191] failing at fixing reactivexplorer --- Wumpus/src/Driver.java | 2 +- Wumpus/src/ReactiveExplorer.java | 1 - Wumpus/src/World.java | 6 ++---- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 6c6e454..dc9d60f 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -31,7 +31,7 @@ public static void makeGame() throws IOException { //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); World world = new World("PerceptBoard.txt"); - world.startGame("LogicExplorer"); + world.startGame("ReactiveExplorer"); //Agent explorer = new LogicExplorer(world); //world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 9522635..562ea43 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -22,7 +22,6 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { } private void run() { - int i = 0; while (i < 10) { move(); diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 803de9d..be4e4e1 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -5,7 +5,7 @@ public final class World { - public static final int NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4; + public static final int NORTH = 0, EAST = 1, SOUTH = 3, WEST = 4; public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; @@ -37,8 +37,6 @@ public void importMap(String fileName) { x = Integer.parseInt(next.substring(0,next.indexOf(" "))); next = next.substring(next.indexOf(" ") + 1); y = Integer.parseInt(next.substring(0, next.indexOf(" "))); - System.out.println(x); - System.out.println(y); perceptMap = new byte[size][size]; int i = 0; while ((next = reader.readLine()) != null) {//((Integer) reader.read()).toString()).equals("-1")) { @@ -67,7 +65,7 @@ public byte getPercepts() { return perceptMap[x][y]; } - private void printWorld() { + public void printWorld() { for (int i = 0; i < perceptMap.length; i++) { for (int j = 0; j < perceptMap.length; j++) { From 9649cc6c88f9f72acf124c6270919541082d7139 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 19:47:37 -0600 Subject: [PATCH 084/191] well fk me mate --- Wumpus/src/Driver.java | 5 +- Wumpus/src/LogicExplorer.java | 119 +++++++++++++++--------------- Wumpus/src/PathFinder.java | 135 ++++++++++++++++++++++++++++++++++ Wumpus/src/Tester.java | 10 +++ 4 files changed, 206 insertions(+), 63 deletions(-) create mode 100644 Wumpus/src/PathFinder.java diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index dc9d60f..6512a67 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -7,10 +7,11 @@ public class Driver { public static void main(String[] args) throws IOException { // Driver driver = new Driver(); - //Tester tester = new Tester(); + Tester tester = new Tester(); + tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + // makeGame(); } public static void makeGame() throws IOException { diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 518e484..a836d2b 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -1,5 +1,8 @@ import java.util.ArrayList; +import java.util.LinkedList; +import java.util.PriorityQueue; +import java.util.Queue; public class LogicExplorer extends Agent { @@ -11,7 +14,7 @@ public class LogicExplorer extends Agent { private Location safeSpace; public LogicExplorer(World world, int startingArrows, int startingX, int startingY, int direction) { - super(world,startingArrows,startingX,startingY,direction); + super(world, startingArrows, startingX, startingY, direction); kb = new KnowledgeBase(); kb.initializeRules(); this.searchedPositions = new boolean[World.size][World.size]; @@ -19,26 +22,31 @@ public LogicExplorer(World world, int startingArrows, int startingX, int startin initializeFrontier(); run(); } - - public void initializeFrontier(){ - if(location.x>0) - frontier.add(new Location(location.x-1,location.y)); - if(location.x < world.size) - frontier.add(new Location(location.x+1,location.y)); - if(location.y>0) - frontier.add(new Location(location.x,location.y-1)); - if(location.y 0) { + frontier.add(new Location(location.x - 1, location.y)); + } + if (location.x < world.size) { + frontier.add(new Location(location.x + 1, location.y)); + } + if (location.y > 0) { + frontier.add(new Location(location.x, location.y - 1)); + } + if (location.y < world.size) { + frontier.add(new Location(location.x, location.y + 1)); + } } - public void updateLocation(){ + public void updateLocation() { super.updateLocation(); expandFrontier(); } - - public void expandFrontier(){ + + public void expandFrontier() { //TODO: write } + private void run() { while (true) { @@ -78,26 +86,28 @@ private void move(int action) { System.out.println("Error processing movement."); } } - - public void removeFromFrontier(Location locToRemove){ - for(Location loc : frontier){ - if(loc.x == locToRemove.x && loc.y == locToRemove.y) + + public void removeFromFrontier(Location locToRemove) { + for (Location loc : frontier) { + if (loc.x == locToRemove.x && loc.y == locToRemove.y) { frontier.remove(loc); + } } } private void processPercepts() { - if((percepts & DEATH) != 0){ + if ((percepts & DEATH) != 0) { removeFromFrontier(getForward()); } if (((percepts & BUMP) != BUMP) && (percepts & DEATH) != DEATH) { updateLocation(); searchedPositions[location.x][location.y] = true; } - if((percepts&BUMP)!=0) - kb.tell(new Clause(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null))); - else - kb.tell(new Clause(new Fact("Obsticle",getForward().x,false,getForward().y,false,true,null,null))); + if ((percepts & BUMP) != 0) { + kb.tell(new Clause(new Fact("Obsticle", getForward().x, false, getForward().y, false, false, null, null))); + } else { + kb.tell(new Clause(new Fact("Obsticle", getForward().x, false, getForward().y, false, true, null, null))); + } if ((percepts & STENCH) != 0) { kb.tell(new Clause(new Fact("Stench", location.x, false, location.y, false, true, null, null))); } else { @@ -125,35 +135,37 @@ private void decideNextAction() { //check forward //check left //check right - if(getForward().x >= 0 && getForward().x < world.size && getForward().y >= 0 && getForward().y < world.size){ - if(kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,true,null,null))){ - if(kb.ask(new Fact("Pit",getForward().x,false,getForward().y,false,true,null,null))) - if(kb.ask(new Fact("Obsticle",getForward().x,false,getForward().y,false,true,null,null))) + if (getForward().x >= 0 && getForward().x < world.size && getForward().y >= 0 && getForward().y < world.size) { + if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, true, null, null))) { + if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { + if (kb.ask(new Fact("Obsticle", getForward().x, false, getForward().y, false, true, null, null))) { move(World.MOVE); + } + } } - } - else if(safeSpaceInFrontier()){ + } else if (safeSpaceInFrontier()) { rhwTraversal(safeSpace); - } - else + } else { rhwTraversal(frontier.get(0)); + } } - - private void rhwTraversal(Location location){ + + private void rhwTraversal(Location location) { //go to location zach } - - private boolean safeSpaceInFrontier(){ - for(int i = frontier.size() - 1; i >= 0; i--){ + + private boolean safeSpaceInFrontier() { + for (int i = frontier.size() - 1; i >= 0; i--) { Location loc = frontier.get(i); - if(kb.ask(new Fact("Wumpus",loc.x,false,loc.y,false,true,null,null))){ - if(kb.ask(new Fact("Pit",loc.x,false,loc.y,false,true,null,null))) - if(kb.ask(new Fact("Obsticle",loc.x,false,loc.y,false,true,null,null))){ - safeSpace = new Location(loc.x,loc.y); + if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, true, null, null))) { + if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, true, null, null))) { + if (kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, true, null, null))) { + safeSpace = new Location(loc.x, loc.y); return true; } + } } - if(kb.ask(new Fact("Wumpus",loc.x,false,loc.y,false,false,null,null)) || kb.ask(new Fact("Pit",loc.x,false,loc.y,false,false,null,null))|| kb.ask(new Fact("Obsticle",loc.x,false,loc.y,false,false,null,null))){ + if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, false, null, null))) { //there is specifically a wumpus, pit, or obsticle at this position, don't navigate to it. frontier.remove(i); } @@ -161,24 +173,9 @@ private boolean safeSpaceInFrontier(){ return false; } -// private void RHWTraversal(String stopCondition) { -// -// do { -// if (kb.ask("right is safe")) { -// move(4); //turn right -// } else { -// if (kb.ask("forward is safe")) { -// move(2); //go forward -// } else { -// move(3); //turn left -// } -// } -// } while (!kb.ask(stopCondition)); -// -// //face stop condition -// while (!kb.ask("am i facing the stop condition?")) { -// move(3); //turn until facing stp condition -// } -// move(2); -// } + private void RHWTraversal(int x, int y) { + + //ArrayList path = PathFinder.getPath(x, y, location.x, location.y, searchedPositions); + //traverse path + } } diff --git a/Wumpus/src/PathFinder.java b/Wumpus/src/PathFinder.java new file mode 100644 index 0000000..c25847c --- /dev/null +++ b/Wumpus/src/PathFinder.java @@ -0,0 +1,135 @@ + +import java.util.ArrayList; +import java.util.PriorityQueue; + +public class PathFinder { + + //Blocked cells are just null Cell values in grid + Cell[][] map; + PriorityQueue open; + ArrayList path = new ArrayList<>(); + + boolean closed[][]; + int startX, startY, endX, endY; + + public PathFinder(int endX, int endY, int startX, int startY, boolean[][] blocked) { + map = new Cell[blocked.length][blocked.length]; + getPath(endX, endY, startX, startY, blocked); + } + + public void getPath(int endX, int endY, int startX, int startY, boolean[][] blocked) { + + for (int i = 0; i < blocked.length - 1; i++) { + for (int j = 0; j < blocked.length - 1; j++) { + if (blocked[i][j] != true) { + map[i][j] = null; + } + } + } + this.startX = startX; + this.startY = startY; + this.endX = endX; + this.endY = endY; + + AStar(); + while (open.peek() != null) { + path.add(open.poll()); + } + printPath(); + } + + private void printPath() { + + for (Cell c : path) { + System.out.println(c.x + ", " + c.y); + } + } + + void checkAndUpdateCost(Cell current, Cell cell, int cost) { + if (cell == null || closed[cell.x][cell.y]) { + return; + } + int t_final_cost = cost; + + boolean inOpen = open.contains(cell); + if (!inOpen || t_final_cost < cell.finalCost) { + cell.finalCost = t_final_cost; + cell.parent = current; + if (!inOpen) { + open.add(cell); + } + } + } + + public void AStar() { + + //add the start location to open list. + open.add(map[startX][startY]); + Cell current; + + while (true) { + current = open.poll(); + if (current == null) { + break; + } + closed[current.x][current.y] = true; + + if (current.equals(map[endX][endY])) { + return; + } + + Cell cell; + if (current.x - 1 >= 0) { + cell = map[current.x - 1][current.y]; + checkAndUpdateCost(current, cell, current.finalCost++); + + if (current.y - 1 >= 0) { + cell = map[current.x - 1][current.y - 1]; + checkAndUpdateCost(current, cell, current.finalCost++); + } + + if (current.y + 1 < map[0].length) { + cell = map[current.x - 1][current.y + 1]; + checkAndUpdateCost(current, cell, current.finalCost++); + } + } + + if (current.y - 1 >= 0) { + cell = map[current.x][current.y - 1]; + checkAndUpdateCost(current, cell, current.finalCost++); + } + + if (current.y + 1 < map[0].length) { + cell = map[current.x][current.y + 1]; + checkAndUpdateCost(current, cell, current.finalCost++); + } + + if (current.x + 1 < map.length) { + cell = map[current.x + 1][current.y]; + checkAndUpdateCost(current, cell, current.finalCost++); + + if (current.y - 1 >= 0) { + cell = map[current.x + 1][current.y - 1]; + checkAndUpdateCost(current, cell, current.finalCost++); + } + + if (current.y + 1 < map[0].length) { + cell = map[current.x + 1][current.y + 1]; + checkAndUpdateCost(current, cell, current.finalCost++); + } + } + } + } + + class Cell { + + int finalCost = 0; + int x, y; + Cell parent; + + Cell(int x, int y) { + this.x = x; + this.y = y; + } + } +} diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index bbfa1cf..601aff6 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -38,6 +38,16 @@ public void testUnify() { System.out.println(sub.varIdToSubstitute + "/" + sub.valToSubstituteWith); } } + + public void testPathFinder() { + boolean[][] test = new boolean[][]{{true, true, true, true, true}, + {true, true, false, false, true}, + {true, true, false, false, true}, + {true, true, false, true, true}, + {true, true, false, true, true}}; + new PathFinder(1, 3, 3, 1, test); + + } public void testInferenceEngine() { InferenceEngine engine = new InferenceEngine(new KnowledgeBase()); From a40d16b56dff4b406ad63b68685faeb0ccea2f25 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 19:53:38 -0600 Subject: [PATCH 085/191] stuff --- Wumpus/src/InferenceEngine.java | 6 ++++-- Wumpus/src/Unifier.java | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index a8067d1..f3b5441 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -144,8 +144,10 @@ public void infer(Clause clauseToCheck) { } } } - clause.facts.remove(fact); - kb.addToClauses(clause); + if(!substitutions.isEmpty()){ + clause.facts.remove(fact); + kb.addToClauses(clause); + } break; } } diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 9045cb0..de1936f 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -98,8 +98,27 @@ public static ArrayList unify(Fact f1, Fact f2){ } } + substitute(subs, fact1); + substitute(subs, fact2); + for(int i = 0; i < fact1.variables.size(); i++){ + Variable var1 = fact1.variables.get(i); + Variable var2 = fact2.variables.get(i); + if(var1.value != var2.value) + return new ArrayList(); + } return subs; } + + public static void substitute(ArrayList subs, Fact fact){ + for(Substitute sub : subs){ + for(Variable var: fact.variables){ + if(var.isVariable && var.variableId == sub.varIdToSubstitute){ + var.isVariable = false; + var.value = sub.valToSubstituteWith; + } + } + } + } public static ArrayList unify2(Fact f1, Fact f2) { ArrayList subs = new ArrayList<>(); From 8e22b851896aa8e9a9adb9e2ecea978f72992ffc Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 20:32:33 -0600 Subject: [PATCH 086/191] stuff --- Wumpus/src/LogicExplorer.java | 13 ++++++++++++- Wumpus/src/World.java | 4 ++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 6e53e0c..45176d1 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -48,7 +48,18 @@ public void updateLocation(){ } public void expandFrontier() { - //TODO: write + if (location.x > 0 && !searchedPositions[location.x-1][location.y]) { + frontier.add(new Location(location.x - 1, location.y)); + } + if (location.x < world.size && !searchedPositions[location.x+1][location.y]) { + frontier.add(new Location(location.x + 1, location.y)); + } + if (location.y > 0 && !searchedPositions[location.x][location.y-1]) { + frontier.add(new Location(location.x, location.y - 1)); + } + if (location.y < world.size && !searchedPositions[location.x][location.y+1]) { + frontier.add(new Location(location.x, location.y + 1)); + } } private void run() { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index d6a8cea..88ed7f4 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -185,10 +185,10 @@ public byte action(int action) { } break; case TURN_LEFT: - direction = (direction + 3) % 4 + 1; + direction = (direction + 3) % 4; return perceptMap[x][y]; case TURN_RIGHT: - direction = (direction + 1) % 4 + 1; + direction = (direction + 1) % 4; return perceptMap[x][y]; case SHOOT: //shoot logic From 54eba925fcd922e91baa707c768599f6b28e4664 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 20:33:17 -0600 Subject: [PATCH 087/191] Fucking reactiveexplorer --- Wumpus/src/Driver.java | 6 +++--- Wumpus/src/ReactiveExplorer.java | 2 ++ Wumpus/src/World.java | 10 ++++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 6512a67..cf73b22 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -7,11 +7,11 @@ public class Driver { public static void main(String[] args) throws IOException { // Driver driver = new Driver(); - Tester tester = new Tester(); - tester.testPathFinder(); +// Tester tester = new Tester(); +// tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + makeGame(); } public static void makeGame() throws IOException { diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 562ea43..f506dd4 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -35,6 +35,7 @@ private void move() { updateSafe(); //go in random direction int rand = random.nextInt(3); + System.out.println("Safe space, action is : " + rand); switch (rand) { case 0: //try to go forward percepts = world.action(MOVE); @@ -77,6 +78,7 @@ private void move() { percepts = world.action(safeMoves.get(rand)); } else { int rand = random.nextInt(3); + System.out.println("Unsafe space, action is : " + rand); switch (rand) { case 0: percepts = world.action(MOVE); diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index b23ddcf..0825579 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -104,6 +104,7 @@ public byte action(int action) { score--; switch (direction) { case NORTH: + System.out.println("Moved north"); if (y + 1 < size) { if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; @@ -123,6 +124,7 @@ public byte action(int action) { return BUMP; } case EAST: + System.out.println("Moved east"); if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -141,6 +143,7 @@ public byte action(int action) { return BUMP; } case SOUTH: + System.out.println("Moved south"); if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -160,6 +163,7 @@ public byte action(int action) { return BUMP; } case WEST: + System.out.println("Moved west"); if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -179,14 +183,16 @@ public byte action(int action) { return BUMP; } default: + System.out.println("Defaulted"); + System.out.println(direction); break; } break; case TURN_LEFT: - direction = (direction + 3) % 4 + 1; + direction = (direction + 3); return perceptMap[x][y]; case TURN_RIGHT: - direction = (direction + 1) % 4 + 1; + direction = (direction + 1); return perceptMap[x][y]; case SHOOT: //shoot logic From 529963ef113de3c59e11f34fa04ccb412f965b15 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 20:34:36 -0600 Subject: [PATCH 088/191] fuuuuuck --- Wumpus/src/LogicExplorer.java | 2 +- Wumpus/src/PathFinder.java | 129 +--------------------------------- Wumpus/src/Tester.java | 4 +- 3 files changed, 4 insertions(+), 131 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 6e53e0c..24fc7bd 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -152,7 +152,7 @@ private void decideNextAction() { } private void rhwTraversal(Location location) { - //go to location zach + //go to location zach NOOOO! } private boolean safeSpaceInFrontier() { diff --git a/Wumpus/src/PathFinder.java b/Wumpus/src/PathFinder.java index c25847c..d81f28c 100644 --- a/Wumpus/src/PathFinder.java +++ b/Wumpus/src/PathFinder.java @@ -4,132 +4,5 @@ public class PathFinder { - //Blocked cells are just null Cell values in grid - Cell[][] map; - PriorityQueue open; - ArrayList path = new ArrayList<>(); - - boolean closed[][]; - int startX, startY, endX, endY; - - public PathFinder(int endX, int endY, int startX, int startY, boolean[][] blocked) { - map = new Cell[blocked.length][blocked.length]; - getPath(endX, endY, startX, startY, blocked); - } - - public void getPath(int endX, int endY, int startX, int startY, boolean[][] blocked) { - - for (int i = 0; i < blocked.length - 1; i++) { - for (int j = 0; j < blocked.length - 1; j++) { - if (blocked[i][j] != true) { - map[i][j] = null; - } - } - } - this.startX = startX; - this.startY = startY; - this.endX = endX; - this.endY = endY; - - AStar(); - while (open.peek() != null) { - path.add(open.poll()); - } - printPath(); - } - - private void printPath() { - - for (Cell c : path) { - System.out.println(c.x + ", " + c.y); - } - } - - void checkAndUpdateCost(Cell current, Cell cell, int cost) { - if (cell == null || closed[cell.x][cell.y]) { - return; - } - int t_final_cost = cost; - - boolean inOpen = open.contains(cell); - if (!inOpen || t_final_cost < cell.finalCost) { - cell.finalCost = t_final_cost; - cell.parent = current; - if (!inOpen) { - open.add(cell); - } - } - } - - public void AStar() { - - //add the start location to open list. - open.add(map[startX][startY]); - Cell current; - - while (true) { - current = open.poll(); - if (current == null) { - break; - } - closed[current.x][current.y] = true; - - if (current.equals(map[endX][endY])) { - return; - } - - Cell cell; - if (current.x - 1 >= 0) { - cell = map[current.x - 1][current.y]; - checkAndUpdateCost(current, cell, current.finalCost++); - - if (current.y - 1 >= 0) { - cell = map[current.x - 1][current.y - 1]; - checkAndUpdateCost(current, cell, current.finalCost++); - } - - if (current.y + 1 < map[0].length) { - cell = map[current.x - 1][current.y + 1]; - checkAndUpdateCost(current, cell, current.finalCost++); - } - } - - if (current.y - 1 >= 0) { - cell = map[current.x][current.y - 1]; - checkAndUpdateCost(current, cell, current.finalCost++); - } - - if (current.y + 1 < map[0].length) { - cell = map[current.x][current.y + 1]; - checkAndUpdateCost(current, cell, current.finalCost++); - } - - if (current.x + 1 < map.length) { - cell = map[current.x + 1][current.y]; - checkAndUpdateCost(current, cell, current.finalCost++); - - if (current.y - 1 >= 0) { - cell = map[current.x + 1][current.y - 1]; - checkAndUpdateCost(current, cell, current.finalCost++); - } - - if (current.y + 1 < map[0].length) { - cell = map[current.x + 1][current.y + 1]; - checkAndUpdateCost(current, cell, current.finalCost++); - } - } - } - } - - class Cell { - - int finalCost = 0; - int x, y; - Cell parent; - - Cell(int x, int y) { - this.x = x; - this.y = y; - } - } + } diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index 601aff6..5ba0c51 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -45,8 +45,8 @@ public void testPathFinder() { {true, true, false, false, true}, {true, true, false, true, true}, {true, true, false, true, true}}; - new PathFinder(1, 3, 3, 1, test); - + //new PathFinder(1, 3, 3, 1, test); + } public void testInferenceEngine() { From 7d3413d9142ee81312aa8bf9fdd84afca48a1474 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 20:37:28 -0600 Subject: [PATCH 089/191] ting --- Wumpus/src/LogicExplorer.java | 6 ++++++ Wumpus/src/World.java | 1 + 2 files changed, 7 insertions(+) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index d016b3c..6d7c51b 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -75,25 +75,31 @@ private void move(int action) { switch (action) { case GRAB: + System.out.println("Grabbing"); world.action(GRAB); System.out.println("Game failed to end after action(GRAB)."); break; case MOVE: + System.out.println("Moving"); percepts = (byte) world.action(MOVE); processPercepts(); break; case TURN_LEFT: + System.out.println("Turning left"); world.action(TURN_LEFT); break; case TURN_RIGHT: + System.out.println("turning right"); world.action(TURN_RIGHT); break; case SHOOT: + System.out.println("shooting"); arrowCount--; percepts = (byte) world.action(SHOOT); processPercepts(); break; case QUIT: + System.out.println("no possible solution"); world.action(QUIT); System.out.println("Game failed to end after action(QUIT)."); break; diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 88ed7f4..a218531 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -93,6 +93,7 @@ public byte action(int action) { numMoves++; switch (action) { case GRAB: + score--; if ((perceptMap[x][y] & GLITTER) != 0) { perceptMap[x][y] -= GLITTER; score += 1000; From cc025a7933f3cb5dfdb785724c9aa2d11b9e1b18 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 20:43:38 -0600 Subject: [PATCH 090/191] testa --- Wumpus/src/Agent.java | 4 ++-- Wumpus/src/Driver.java | 6 +++--- Wumpus/src/LogicExplorer.java | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 043058f..9213b0c 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -38,7 +38,7 @@ public void turnRight() { } public void turnLeft() { - direction = (direction - 1) % 4; + direction = (direction + 3) % 4; world.action(TURN_LEFT); } @@ -47,7 +47,7 @@ public int getRight() { } public int getLeft() { - return (direction - 1) % 4; + return (direction + 3) % 4; } public enum State { //im not sure were going to need this diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 717141e..7b3f35e 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -7,11 +7,11 @@ public class Driver { public static void main(String[] args) throws IOException { // Driver driver = new Driver(); - Tester tester = new Tester(); - tester.testPathFinder(); + //Tester tester = new Tester(); + //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + makeGame(); } public static void makeGame() throws IOException { diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 6d7c51b..8f3ee55 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -28,13 +28,13 @@ public void initializeFrontier() { if (location.x > 0) { frontier.add(new Location(location.x - 1, location.y)); } - if (location.x < world.size) { + if (location.x < world.size-1) { frontier.add(new Location(location.x + 1, location.y)); } if (location.y > 0) { frontier.add(new Location(location.x, location.y - 1)); } - if (location.y < world.size) { + if (location.y < world.size-1) { frontier.add(new Location(location.x, location.y + 1)); } } @@ -51,13 +51,13 @@ public void expandFrontier() { if (location.x > 0 && !searchedPositions[location.x-1][location.y]) { frontier.add(new Location(location.x - 1, location.y)); } - if (location.x < world.size && !searchedPositions[location.x+1][location.y]) { + if (location.x < world.size -1&& !searchedPositions[location.x+1][location.y]) { frontier.add(new Location(location.x + 1, location.y)); } if (location.y > 0 && !searchedPositions[location.x][location.y-1]) { frontier.add(new Location(location.x, location.y - 1)); } - if (location.y < world.size && !searchedPositions[location.x][location.y+1]) { + if (location.y < world.size -1&& !searchedPositions[location.x][location.y+1]) { frontier.add(new Location(location.x, location.y + 1)); } } From e3a99204e5d4e1ca1529b42955f044c5baa866fb Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 20:44:00 -0600 Subject: [PATCH 091/191] ugggh --- Wumpus/src/LogicExplorer.java | 37 ++++++++++++++++++++++++++++++++++- Wumpus/src/PathFinder.java | 8 -------- Wumpus/src/Tester.java | 10 ---------- 3 files changed, 36 insertions(+), 19 deletions(-) delete mode 100644 Wumpus/src/PathFinder.java diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index d016b3c..4372960 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -1,5 +1,6 @@ import java.util.ArrayList; +import java.util.Collection; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; @@ -163,7 +164,41 @@ private void decideNextAction() { } private void rhwTraversal(Location location) { - //go to location zach NOOOO! + + Location current = this.location; + Location goal = location; + + Queue queue = new LinkedList<>(); + //add adjacent spaces to queue + queue.addAll(getSafeAdjacent(location)); + + } + + private ArrayList getSafeAdjacent(Location location) { + + ArrayList adjacent = new ArrayList<>(); + + if (location.x < World.size - 1) { + if (searchedPositions[location.x+1][location.y]) { + adjacent.add(new Location(location.x+1, location.y)); + } + } + if (location.x > 0) { + if (searchedPositions[location.x-1][location.y]) { + adjacent.add(new Location(location.x-1, location.y)); + } + } + if (location.y < World.size - 1) { + if (searchedPositions[location.x][location.y+1]) { + adjacent.add(new Location(location.x, location.y+1)); + } + } + if (location.y > 0) { + if (searchedPositions[location.x][location.y-1]) { + adjacent.add(new Location(location.x, location.y-1)); + } + } + return adjacent; } private boolean safeSpaceInFrontier() { diff --git a/Wumpus/src/PathFinder.java b/Wumpus/src/PathFinder.java deleted file mode 100644 index d81f28c..0000000 --- a/Wumpus/src/PathFinder.java +++ /dev/null @@ -1,8 +0,0 @@ - -import java.util.ArrayList; -import java.util.PriorityQueue; - -public class PathFinder { - - -} diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index 5ba0c51..bbfa1cf 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -38,16 +38,6 @@ public void testUnify() { System.out.println(sub.varIdToSubstitute + "/" + sub.valToSubstituteWith); } } - - public void testPathFinder() { - boolean[][] test = new boolean[][]{{true, true, true, true, true}, - {true, true, false, false, true}, - {true, true, false, false, true}, - {true, true, false, true, true}, - {true, true, false, true, true}}; - //new PathFinder(1, 3, 3, 1, test); - - } public void testInferenceEngine() { InferenceEngine engine = new InferenceEngine(new KnowledgeBase()); From 8cf458aae015abb3c847efcd6ba10ae106ee99a5 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 21:14:23 -0600 Subject: [PATCH 092/191] kek --- Wumpus/src/LogicExplorer.java | 93 +++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 31 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 934e929..8d65ff7 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -13,7 +13,7 @@ public class LogicExplorer extends Agent { private boolean[][] searchedPositions; private boolean navigatingToSafePosition; private Location safeSpace; - boolean notFirstMove = false; + boolean notFirstMove = false; public LogicExplorer(World world, int startingArrows, int startingX, int startingY, int direction) { super(world, startingArrows, startingX, startingY, direction); @@ -29,36 +29,37 @@ public void initializeFrontier() { if (location.x > 0) { frontier.add(new Location(location.x - 1, location.y)); } - if (location.x < world.size-1) { + if (location.x < world.size - 1) { frontier.add(new Location(location.x + 1, location.y)); } if (location.y > 0) { frontier.add(new Location(location.x, location.y - 1)); } - if (location.y < world.size-1) { + if (location.y < world.size - 1) { frontier.add(new Location(location.x, location.y + 1)); } } - public void updateLocation(){ - if(notFirstMove) + public void updateLocation() { + if (notFirstMove) { super.updateLocation(); - else + } else { notFirstMove = true; + } expandFrontier(); } public void expandFrontier() { - if (location.x > 0 && !searchedPositions[location.x-1][location.y]) { + if (location.x > 0 && !searchedPositions[location.x - 1][location.y]) { frontier.add(new Location(location.x - 1, location.y)); } - if (location.x < world.size -1&& !searchedPositions[location.x+1][location.y]) { + if (location.x < world.size - 1 && !searchedPositions[location.x + 1][location.y]) { frontier.add(new Location(location.x + 1, location.y)); } - if (location.y > 0 && !searchedPositions[location.x][location.y-1]) { + if (location.y > 0 && !searchedPositions[location.x][location.y - 1]) { frontier.add(new Location(location.x, location.y - 1)); } - if (location.y < world.size -1&& !searchedPositions[location.x][location.y+1]) { + if (location.y < world.size - 1 && !searchedPositions[location.x][location.y + 1]) { frontier.add(new Location(location.x, location.y + 1)); } } @@ -125,8 +126,9 @@ private void processPercepts() { updateLocation(); searchedPositions[location.x][location.y] = true; } - if((percepts&BUMP)!=0) - kb.tell(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null)); + if ((percepts & BUMP) != 0) { + kb.tell(new Fact("Obsticle", getForward().x, false, getForward().y, false, false, null, null)); + } if ((percepts & STENCH) != 0) { kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); } else { @@ -170,38 +172,72 @@ private void decideNextAction() { } private void rhwTraversal(Location location) { - + Location current = this.location; Location goal = location; - + Queue queue = new LinkedList<>(); //add adjacent spaces to queue queue.addAll(getSafeAdjacent(location)); - + + } + //searchedpositions + int[][] grid = new int[World.size][World.size]; + + private void search(Location location) { + for (int i = 0; i < World.size; i++) { + for (int j = 0; j < World.size; j++) { + if (searchedPositions[i][j]) { + grid[i][j] = 0; + } else { + grid[i][j] = 1; + } + } + } + grid[location.x][location.y] = 2; + grid[this.location.x][this.location.y] = 3; + search(this.location.x, this.location.y); } - + + private boolean search(int x, int y) { + //0 = safe, 1 = unsafe, 2 = goal, 3 = visited + if (grid[x][y] == 2) { + return true; + } else if (grid[x][y] == 1) { + return false; + } else if (grid[x][y] == 3) { + return false; + } + grid[x][y] = 3; + //explore neighbors clockwise + if ((x < grid.length - 1 && search(x + 1, y)) || (y > 0 && search(x, y - 1)) || (x > 0 && search(x - 1, y)) || (y < grid.length - 1 && search(x, y + 1))) { + return true; + } + return false; + } + private ArrayList getSafeAdjacent(Location location) { - + ArrayList adjacent = new ArrayList<>(); - + if (location.x < World.size - 1) { - if (searchedPositions[location.x+1][location.y]) { - adjacent.add(new Location(location.x+1, location.y)); + if (searchedPositions[location.x + 1][location.y]) { + adjacent.add(new Location(location.x + 1, location.y)); } } if (location.x > 0) { - if (searchedPositions[location.x-1][location.y]) { - adjacent.add(new Location(location.x-1, location.y)); + if (searchedPositions[location.x - 1][location.y]) { + adjacent.add(new Location(location.x - 1, location.y)); } } if (location.y < World.size - 1) { - if (searchedPositions[location.x][location.y+1]) { - adjacent.add(new Location(location.x, location.y+1)); + if (searchedPositions[location.x][location.y + 1]) { + adjacent.add(new Location(location.x, location.y + 1)); } } if (location.y > 0) { - if (searchedPositions[location.x][location.y-1]) { - adjacent.add(new Location(location.x, location.y-1)); + if (searchedPositions[location.x][location.y - 1]) { + adjacent.add(new Location(location.x, location.y - 1)); } } return adjacent; @@ -226,9 +262,4 @@ private boolean safeSpaceInFrontier() { return false; } - private void RHWTraversal(int x, int y) { - - //ArrayList path = PathFinder.getPath(x, y, location.x, location.y, searchedPositions); - //traverse path - } } From 7cbc13df4b95284d09ef48c3b63d6b825a2feec1 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 21:34:11 -0600 Subject: [PATCH 093/191] pfafds --- Wumpus/src/LogicExplorer.java | 106 +++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 8f3ee55..f58e6c9 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -12,6 +12,8 @@ public class LogicExplorer extends Agent { private boolean[][] searchedPositions; private boolean navigatingToSafePosition; private Location safeSpace; + private Location wumpusSpace; + private ArrayList moveHistory = new ArrayList<>(); boolean notFirstMove = false; public LogicExplorer(World world, int startingArrows, int startingX, int startingY, int direction) { @@ -80,16 +82,21 @@ private void move(int action) { System.out.println("Game failed to end after action(GRAB)."); break; case MOVE: + moveHistory.add(MOVE); System.out.println("Moving"); percepts = (byte) world.action(MOVE); processPercepts(); break; case TURN_LEFT: + moveHistory.add(TURN_LEFT); System.out.println("Turning left"); + direction = (direction + 3)%4; world.action(TURN_LEFT); break; case TURN_RIGHT: + moveHistory.add(TURN_RIGHT); System.out.println("turning right"); + direction = (direction+1)%4; world.action(TURN_RIGHT); break; case SHOOT: @@ -163,12 +170,104 @@ private void decideNextAction() { } } else if (safeSpaceInFrontier()) { rhwTraversal(safeSpace); - } else { + } + else if(arrowCount > 0 && wumpusInFrontier()){ + rhwTraversal(neighborSafeSpace(wumpusSpace)); + turnToSpace(wumpusSpace); + move(SHOOT); + move(MOVE); + } + else { rhwTraversal(frontier.get(0)); } } + + public void turnToSpace(Location loc){ + if(location.x > loc.x){ + if(direction == EAST) + return; + else if(direction == NORTH) + move(TURN_LEFT); + else if(direction == SOUTH) + move(TURN_RIGHT); + } + else if(loc.x > location.x){ + if(direction == WEST) + return; + else if(direction == NORTH) + move(TURN_RIGHT); + else if(direction == SOUTH) + move(TURN_LEFT); + } + else if(loc.y < location.y){ + if(direction == SOUTH) + return; + else if(direction == WEST) + move(TURN_LEFT); + else if(direction == EAST) + move(TURN_RIGHT); + } + else if(loc.y > location.y){ + if(direction == NORTH){ + return; + } + else if(direction == WEST){ + move(TURN_RIGHT); + } + else if(direction == EAST) + move(TURN_LEFT); + } + } + + private Location neighborSafeSpace(Location location){ + Location loc = null; + if(location.x > 0){ + if(searchedPositions[location.x-1][location.y]){ + loc = new Location(location.x-1,location.y); + } + } + else if(location.x < World.size -1 && searchedPositions[location.x+1][location.y]) + loc = new Location(location.x+1,location.y); + else if(location.y > 0 && searchedPositions[location.x][location.y-1]) + loc = new Location(location.x,location.y-1); + else if(location.y < World.size -1 && searchedPositions[location.x][location.y+1]) + loc = new Location(location.x,location.y+1); + return loc; + } + + private boolean wumpusInFrontier(){ + for(Location loc : frontier){ + if(kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))){ + wumpusSpace = loc; + return true; + } + } + + return false; + } + private void moveHistoryTraversal(Location loc){ + if(loc.x == location.x && loc.y == location.y){ + return; + } + for(int i = moveHistory.size()-1;i>=0; i--){ + if(loc.x == location.x && loc.y == location.y){ + return; + } + int move = moveHistory.get(i); + if(move == MOVE){ + move(TURN_LEFT); + move(TURN_LEFT); + move(MOVE); + } + else if(move == TURN_LEFT) + move(TURN_RIGHT); + else if(move == TURN_RIGHT) + move(TURN_LEFT); + } + } private void rhwTraversal(Location location) { + moveHistoryTraversal(location); //go to location zach NOOOO! } @@ -183,10 +282,13 @@ private boolean safeSpaceInFrontier() { } } } - if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, false, null, null))) { + if ( kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, false, null, null))) { //there is specifically a wumpus, pit, or obsticle at this position, don't navigate to it. frontier.remove(i); } + else if(arrowCount == 0 && kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))){ + frontier.remove(i); + } } return false; } From f684470226da31ffefa985968c9c38b52743a5b6 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 21:49:53 -0600 Subject: [PATCH 094/191] awwww --- Wumpus/src/LogicExplorer.java | 64 +++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 8d65ff7..56e938e 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -1,8 +1,6 @@ import java.util.ArrayList; -import java.util.Collection; import java.util.LinkedList; -import java.util.PriorityQueue; import java.util.Queue; public class LogicExplorer extends Agent { @@ -172,15 +170,67 @@ private void decideNextAction() { } private void rhwTraversal(Location location) { + for (int i = 0; i < World.size; i++) { + for (int j = 0; j < World.size; j++) { + if (searchedPositions[i][j]) { + grid[i][j] = 0; + } else { + grid[i][j] = 1; + } + } + } + grid[location.x][location.y] = 2; + grid[this.location.x][this.location.y] = 3; + } - Location current = this.location; - Location goal = location; + private ArrayList getPath(Location goal, Location current, ArrayList path) { - Queue queue = new LinkedList<>(); - //add adjacent spaces to queue - queue.addAll(getSafeAdjacent(location)); + solve(goal, current, path); + return path; + } + + private boolean solve(Location goal, Location current, ArrayList path) { + boolean done = false; + if (valid(current.x, current.y)) { + //grid[current.x][current.y] = 3; + path.add(new Location(current.x, current.y)); + if (current.x == goal.x && current.y == goal.y) { + return true; + } else { + solve(goal, new Location(current.x + 1, current.y), path); + done = solve(goal, new Location(current.x + 1, current.y), path); + if (!done) { + done = solve(goal, new Location(current.x, current.y + 1), path); + } + if (!done) { + done = solve(goal, new Location(current.x - 1, current.y), path); + } + if (!done) { + done = solve(goal, new Location(current.x, current.y - 1), path); + } + } + if (done) { + // grid[current][column] = 7; + } + + } else { + return false; + } + return false; } + + private boolean valid(int row, int column) { + boolean result = false; + // check if cell is in the bounds of the matrix + if (row >= 0 && row < grid.length && column >= 0 && column < grid[0].length) { + if (grid[row][column] == 0) { + result = true; + } + } + return result; + } + //searchedpositions int[][] grid = new int[World.size][World.size]; From a0f0ee581a528b30cc5c0e97c2cec75640903b9a Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 22:05:56 -0600 Subject: [PATCH 095/191] good stuff --- Wumpus/src/InferenceEngine.java | 10 ++++++++++ Wumpus/src/LogicExplorer.java | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index f3b5441..a87785e 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -29,6 +29,15 @@ public boolean follows(Fact fact) { for (Fact followFact : clause.facts) { if (kbFact.predicate.equals(followFact.predicate) && kbFact.not == !followFact.not) { //extend clause with everything in kbClause, remove kbFact and followFact, start over + boolean skipOut = false; + for(int i = 0; i < kbFact.variables.size(); i++){ + Variable var1 = kbFact.variables.get(0); + Variable var2 = followFact.variables.get(0); + if(var1.value != var2.value){ + skipOut = true; + } + } + if(!skipOut){ kbClause.facts.remove(kbFact); clause.facts.remove(followFact); clause.facts.addAll(kbClause.facts); @@ -38,6 +47,7 @@ public boolean follows(Fact fact) { return true; } keepGoing = false; + } } if (!keepGoing) { break; diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index f58e6c9..b248056 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -165,11 +165,15 @@ private void decideNextAction() { if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { if (kb.ask(new Fact("Obsticle", getForward().x, false, getForward().y, false, true, null, null))) { move(World.MOVE); + return; } } } - } else if (safeSpaceInFrontier()) { - rhwTraversal(safeSpace); + } + if (safeSpaceInFrontier()) { + rhwTraversal(neighborSafeSpace(safeSpace)); + turnToSpace(safeSpace); + move(MOVE); } else if(arrowCount > 0 && wumpusInFrontier()){ rhwTraversal(neighborSafeSpace(wumpusSpace)); @@ -178,7 +182,9 @@ else if(arrowCount > 0 && wumpusInFrontier()){ move(MOVE); } else { - rhwTraversal(frontier.get(0)); + rhwTraversal(neighborSafeSpace(frontier.get(0))); + turnToSpace(frontier.get(0)); + move(MOVE); } } @@ -221,10 +227,9 @@ else if(direction == EAST) private Location neighborSafeSpace(Location location){ Location loc = null; - if(location.x > 0){ - if(searchedPositions[location.x-1][location.y]){ + if(location.x > 0 && searchedPositions[location.x-1][location.y]){ loc = new Location(location.x-1,location.y); - } + } else if(location.x < World.size -1 && searchedPositions[location.x+1][location.y]) loc = new Location(location.x+1,location.y); @@ -232,6 +237,7 @@ else if(location.y > 0 && searchedPositions[location.x][location.y-1]) loc = new Location(location.x,location.y-1); else if(location.y < World.size -1 && searchedPositions[location.x][location.y+1]) loc = new Location(location.x,location.y+1); + return loc; } From e0a0a73dcac5b71bb0d79545c096c4c1542a9ba4 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 22:14:22 -0600 Subject: [PATCH 096/191] fa --- Wumpus/src/Driver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index c2fd77e..f2c25e7 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -34,7 +34,7 @@ public static void makeGame() throws IOException { //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); World world = new World("PerceptBoard.txt"); - world.startGame("ReactiveExplorer"); + world.startGame("LogicExplorer"); //Agent explorer = new LogicExplorer(world); //world = new World("PerceptBoard.txt"); From 5b409bc01648a44f6e5ea28a531b6a57ce709541 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Thu, 20 Oct 2016 22:16:43 -0600 Subject: [PATCH 097/191] maybe fixed board stuff --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/ReactiveExplorer.java | 4 ++-- Wumpus/src/WumpusGame.java | 4 ++-- Wumpus/world.txt | 8 ++++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 29424d5..b7b8b83 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 3 1 -4 0 0 0 0 -9 0 0 0 0 -32 1 1 2 1 -1 1 34 17 34 -0 0 1 2 1 +5 3 3 +2 0 0 0 0 +18 2 0 0 0 +18 2 0 8 0 +2 0 0 0 0 +0 0 0 0 0 diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index f506dd4..aba5a1c 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -4,8 +4,7 @@ public class ReactiveExplorer extends Agent { private Location prevLocation; - private State curState, prevState; - private boolean safeMap[][]; + private State curState, prevState; private boolean safeMap[][]; private static final int FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3; public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { @@ -46,6 +45,7 @@ private void move() { turnLeft(); percepts = world.action(MOVE); if ((percepts & BUMP) != BUMP) { + System.out.println("Here"); return; } turnRight(); diff --git a/Wumpus/src/WumpusGame.java b/Wumpus/src/WumpusGame.java index 3e36c8b..5761083 100644 --- a/Wumpus/src/WumpusGame.java +++ b/Wumpus/src/WumpusGame.java @@ -50,8 +50,8 @@ public void setBoard() { } public void initializeBoard() { - for (int i = 0; i < board.length; i++) { - for (int j = board[i].length - 1; j >= 0; j--) { + for (int i = board.length - 1; i >= 0; i--) { + for (int j = 0; j Date: Thu, 20 Oct 2016 22:17:10 -0600 Subject: [PATCH 098/191] jlk --- Wumpus/src/LogicExplorer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index b248056..64ab954 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -184,6 +184,7 @@ else if(arrowCount > 0 && wumpusInFrontier()){ else { rhwTraversal(neighborSafeSpace(frontier.get(0))); turnToSpace(frontier.get(0)); + frontier.remove(0); move(MOVE); } } From 7b86dc5551ddfa8f403488150fb26a5c2ee8701a Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 22:25:29 -0600 Subject: [PATCH 099/191] fe --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/Driver.java | 29 +++++++++++++++-------------- Wumpus/src/ReactiveExplorer.java | 2 +- Wumpus/world.txt | 10 +++++----- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index b7b8b83..cee3d53 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 3 3 -2 0 0 0 0 -18 2 0 0 0 -18 2 0 8 0 -2 0 0 0 0 -0 0 0 0 0 +5 3 4 +17 2 5 32 1 +34 1 0 5 0 +5 32 1 0 4 +0 1 0 8 0 +0 0 4 0 4 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index f2c25e7..fb72ffe 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -18,23 +18,24 @@ public static void main(String[] args) throws IOException { public static void makeGame() throws IOException { -// BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); -// int[] prob = new int[3]; -// System.out.print("% chance of generating pit: "); -// prob[0] = Integer.parseInt(dataIn.readLine()); -// System.out.println(); -// System.out.print("% chance of generating obstacle: "); -// prob[1] = Integer.parseInt(dataIn.readLine()); -// System.out.println(""); -// System.out.print("% chance of generating wumpus: "); -// prob[2] = Integer.parseInt(dataIn.readLine()); -// System.out.println(""); -// -// WumpusGame game = new WumpusGame(5, prob); + BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); + int[] prob = new int[3]; + System.out.print("% chance of generating pit: "); + prob[0] = Integer.parseInt(dataIn.readLine()); + System.out.println(); + System.out.print("% chance of generating obstacle: "); + prob[1] = Integer.parseInt(dataIn.readLine()); + System.out.println(""); + System.out.print("% chance of generating wumpus: "); + prob[2] = Integer.parseInt(dataIn.readLine()); + System.out.println(""); + + WumpusGame game = new WumpusGame(5, prob); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); World world = new World("PerceptBoard.txt"); - world.startGame("LogicExplorer"); + //world.startGame("LogicExplorer"); + world.startGame("ReactiveExplorer"); //Agent explorer = new LogicExplorer(world); //world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index aba5a1c..dd0c7bb 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -75,7 +75,7 @@ private void move() { } if (safeMoves.size() > 0) { int rand = random.nextInt(safeMoves.size()); - percepts = world.action(safeMoves.get(rand)); + percepts = world.action(safeMoves.get(rand) + 1); } else { int rand = random.nextInt(3); System.out.println("Unsafe space, action is : " + rand); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 95f4dd4..b181881 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -0 0 0 0 0 -W 0 0 0 0 -W 0 0 G 0 -0 0 0 S 0 -0 0 0 0 0 +W 0 I H 0 +H 0 0 I 0 +I H 0 0 I +0 0 0 G S +0 0 I 0 I From 1d4fba36d0343ea8c5c494a6d2b195beaf4a24ea Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Thu, 20 Oct 2016 22:28:22 -0600 Subject: [PATCH 100/191] stench is not spelt with a second t... get gud --- Wumpus/src/WumpusGame.java | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/Wumpus/src/WumpusGame.java b/Wumpus/src/WumpusGame.java index 5761083..1fb1572 100644 --- a/Wumpus/src/WumpusGame.java +++ b/Wumpus/src/WumpusGame.java @@ -10,22 +10,13 @@ public class WumpusGame { - private int boardSize; - private int wumpus; - private int startX; - private int startY; + private int boardSize, wumpus, startX, startY; private Space[][] board; private Random random = new Random(); private HashMap probabilityGeneration; private int[] prob; private byte[][] perceptBoard; - private final byte BREEZE = 0b00000001; - private final byte STENTCH = 0b0000010; - private final byte BUMP = 0b00000100; - private final byte GLITTER = 0b00001000; - private final byte DEATH_BY_WUMPUS = 0b00010000; - private final byte DEATH_BY_PIT = 0b00100000; - private final byte SCREAM = 0b01000000; + protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; private PrintWriter out = new PrintWriter(new File("PerceptBoard.txt")); public WumpusGame(int boardSize, int[] prob) throws FileNotFoundException { @@ -110,7 +101,7 @@ public void placeObstacle(int x, int y) { public void placePit(int x, int y) { board[x][y].setHasHole(true); placeAdjacentPercept(x, y, BREEZE); - placePercept(x, y, DEATH_BY_PIT); + placePercept(x, y, DEATH); } public void placeGold(int x, int y) { @@ -120,8 +111,8 @@ public void placeGold(int x, int y) { public void placeWumpus(int x, int y) { board[x][y].toggleWumpus(); - placeAdjacentPercept(x, y, STENTCH); - placePercept(x, y, DEATH_BY_WUMPUS); + placeAdjacentPercept(x, y, STENCH); + placePercept(x, y, DEATH_WUMPUS); wumpus++; } From 111cff8ec9b96d4668687898c7424362aa5f7c6b Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Thu, 20 Oct 2016 23:00:34 -0600 Subject: [PATCH 101/191] lkj --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/LogicExplorer.java | 5 ++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index b7b8b83..29424d5 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 3 3 -2 0 0 0 0 -18 2 0 0 0 -18 2 0 8 0 -2 0 0 0 0 -0 0 0 0 0 +5 3 1 +4 0 0 0 0 +9 0 0 0 0 +32 1 1 2 1 +1 1 34 17 34 +0 0 1 2 1 diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 64ab954..b8de68c 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -22,7 +22,7 @@ public LogicExplorer(World world, int startingArrows, int startingX, int startin kb.initializeRules(); this.searchedPositions = new boolean[World.size][World.size]; searchedPositions[location.x][location.y] = true; - initializeFrontier(); + //initializeFrontier(); run(); } @@ -119,6 +119,7 @@ public void removeFromFrontier(Location locToRemove) { for (Location loc : frontier) { if (loc.x == locToRemove.x && loc.y == locToRemove.y) { frontier.remove(loc); + return; } } } @@ -174,12 +175,14 @@ private void decideNextAction() { rhwTraversal(neighborSafeSpace(safeSpace)); turnToSpace(safeSpace); move(MOVE); + removeFromFrontier(safeSpace); } else if(arrowCount > 0 && wumpusInFrontier()){ rhwTraversal(neighborSafeSpace(wumpusSpace)); turnToSpace(wumpusSpace); move(SHOOT); move(MOVE); + removeFromFrontier(wumpusSpace); } else { rhwTraversal(neighborSafeSpace(frontier.get(0))); From c4baa50f036a9a05ba749a3f92acb09106047500 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Fri, 21 Oct 2016 07:43:42 -0600 Subject: [PATCH 102/191] This might work for the path finding, as soon as i finish the traversePath method, have to class dou --- Wumpus/src/LogicExplorer.java | 54 +++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index b8de68c..0ebf294 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -303,9 +303,57 @@ else if(arrowCount == 0 && kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, return false; } - private void RHWTraversal(int x, int y) { + private void goTo(int x, int y) { - //ArrayList path = PathFinder.getPath(x, y, location.x, location.y, searchedPositions); - //traverse path + ArrayList path = new ArrayList<>(); + path = searchForPath(location.x, location.y, x, y, path); + traversePath(path); + } + + private ArrayList searchForPath(int curX, int curY, int goalX, int goalY, ArrayList path) { + + + if (searchNext(curX, curY, goalX, goalY, path)) { + return path; + } else { + System.out.println("Path finding error, no path found."); + return null; + } + } + + private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path) { + + path.add(new Location(curX, curY)); + if (curX == goalX && curY == goalY) { + return true; + } + if (isValid(curX, curY + 1)) { //north is valid + return searchNext(curX, curY + 1, goalX, goalY, path); + } + if (isValid(curX + 1, curY)) { + return searchNext(curX + 1, curY, goalX, goalY, path); //east is valid + } + if (isValid(curX - 1, curY)) { //west is valid + return searchNext(curX + 1, curY, goalX, goalY, path); + } + path.remove(path.size() - 1); + return false; + } + + private boolean isValid(int x, int y) { + + if (x >= 0 && y >= 0 && x < World.size && y < World.size) { + return searchedPositions[x][y]; + } else { + return false; + } + } + + private void traversePath(ArrayList path) { + + while (!path.isEmpty()) { + Location next = path.remove(0); + //go to next + } } } From 1b152df6d1ea1ba4850bcf96b73a4d5214ab5801 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 08:00:31 -0600 Subject: [PATCH 103/191] dying should be done in the space you would move to, without actually moving --- Wumpus/src/Driver.java | 4 ++- Wumpus/src/LogicExplorer.java | 6 ++-- Wumpus/src/World.java | 66 +++++++++++++++++------------------ 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index fb72ffe..543c496 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,9 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + // makeGame(); + World world = new World("PerceptBoard.txt"); + world.startGame("LogicExplorer"); } public static void makeGame() throws IOException { diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 0ebf294..6f40557 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -185,9 +185,9 @@ else if(arrowCount > 0 && wumpusInFrontier()){ removeFromFrontier(wumpusSpace); } else { - rhwTraversal(neighborSafeSpace(frontier.get(0))); - turnToSpace(frontier.get(0)); - frontier.remove(0); + rhwTraversal(neighborSafeSpace(frontier.get(frontier.size()-1))); + turnToSpace(frontier.get(frontier.size()-1)); + frontier.remove(frontier.size()-1); move(MOVE); } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 2296171..3db38d6 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -108,13 +108,13 @@ public byte action(int action) { case NORTH: System.out.println("Moved north"); if (y + 1 < size) { - if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + if ((perceptMap[x][y+1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x][y] & DEATH) == DEATH) { + if ((perceptMap[x][y+1] & DEATH) == DEATH) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); @@ -124,64 +124,64 @@ public byte action(int action) { return perceptMap[x][y]; } else { return BUMP; - } + } case EAST: System.out.println("Moved east"); - if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { - score -= 1000; - wumpusDeaths++; - System.out.println("Death to wumpus."); - return DEATH_WUMPUS; - } - if ((perceptMap[x][y] & DEATH) == DEATH) { - score -= 1000; - pitDeaths++; - return DEATH; - } - if (x + 1 < size) { - x = x + 1; + if(x+1 < size){ + if ((perceptMap[x+1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + score -= 1000; + wumpusDeaths++; + System.out.println("Death to wumpus."); + return DEATH_WUMPUS; + } + if ((perceptMap[x+1][y] & DEATH) == DEATH) { + score -= 1000; + pitDeaths++; + return DEATH; + } + x = x + 1; return perceptMap[x][y]; - } else { + }else { return BUMP; } case SOUTH: System.out.println("Moved south"); - if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + if(y-1 > 0){ + if ((perceptMap[x][y-1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x][y] & DEATH) == DEATH) { + if ((perceptMap[x][y-1] & DEATH) == DEATH) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); return DEATH; } - if (y - 1 > 0) { y -= 1; return perceptMap[x][y]; - } else { + }else { return BUMP; } case WEST: System.out.println("Moved west"); - if ((perceptMap[x][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + if(x-1>0){ + if ((perceptMap[x-1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x][y] & DEATH) == DEATH) { + if ((perceptMap[x-1][y] & DEATH) == DEATH) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); return DEATH; } - if (x - 1 > 0) { x -= 1; return perceptMap[x][y]; - } else { + }else { return BUMP; } default: @@ -205,10 +205,10 @@ public byte action(int action) { switch (direction) { case 1: //shoot north for (int i = y; i < perceptMap.length; i++) { - if (perceptMap[x][i] == 16) { //hits Wumpus + if ((perceptMap[x][i] & DEATH_WUMPUS)!= 0) { //hits Wumpus removeWumpus(x, i); return SCREAM; - } else if (perceptMap[x][i] == 4) { //hits Obstacle + } else if ((perceptMap[x][i] & BUMP)!= 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -216,10 +216,10 @@ public byte action(int action) { case 2: //shoot east for (int i = y; i < perceptMap.length; i++) { - if (perceptMap[i][y] == 16) { //hits Wumpus + if ((perceptMap[i][y] & DEATH_WUMPUS)!= 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; - } else if (perceptMap[i][y] == 4) { //hits Obstacle + } else if ((perceptMap[i][y] & BUMP)!=0) { //hits Obstacle return perceptMap[x][y]; } } @@ -227,10 +227,10 @@ public byte action(int action) { case 3: //shoot south for (int i = y; i > 0; i--) { - if (perceptMap[x][i] == 16) { //hits Wumpus + if ((perceptMap[x][i] & DEATH_WUMPUS)!=0) { //hits Wumpus removeWumpus(x, i); return SCREAM; - } else if (perceptMap[x][i] == 4) { //hits Obstacle + } else if ((perceptMap[x][i] & BUMP)!=0) { //hits Obstacle return perceptMap[x][y]; } } @@ -238,10 +238,10 @@ public byte action(int action) { case 4: //shoot west for (int i = y; i > 0; i--) { - if (perceptMap[i][y] == 16) { //hits Wumpus + if ((perceptMap[i][y] & DEATH_WUMPUS)!=0) { //hits Wumpus removeWumpus(i, y); return SCREAM; - } else if (perceptMap[i][y] == 4) { //hits Obstacle + } else if ((perceptMap[i][y] &BUMP)!=0) { //hits Obstacle return perceptMap[x][y]; } } From e1746969778263fd33c84e0a17d66068ff3395cf Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 08:02:21 -0600 Subject: [PATCH 104/191] okok --- Wumpus/src/LogicExplorer.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 6f40557..0ebf294 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -185,9 +185,9 @@ else if(arrowCount > 0 && wumpusInFrontier()){ removeFromFrontier(wumpusSpace); } else { - rhwTraversal(neighborSafeSpace(frontier.get(frontier.size()-1))); - turnToSpace(frontier.get(frontier.size()-1)); - frontier.remove(frontier.size()-1); + rhwTraversal(neighborSafeSpace(frontier.get(0))); + turnToSpace(frontier.get(0)); + frontier.remove(0); move(MOVE); } } From e65ab50754cb19f8b7766c6de3060bca2a54c327 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 08:03:44 -0600 Subject: [PATCH 105/191] small opt --- Wumpus/src/LogicExplorer.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 0ebf294..6f40557 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -185,9 +185,9 @@ else if(arrowCount > 0 && wumpusInFrontier()){ removeFromFrontier(wumpusSpace); } else { - rhwTraversal(neighborSafeSpace(frontier.get(0))); - turnToSpace(frontier.get(0)); - frontier.remove(0); + rhwTraversal(neighborSafeSpace(frontier.get(frontier.size()-1))); + turnToSpace(frontier.get(frontier.size()-1)); + frontier.remove(frontier.size()-1); move(MOVE); } } From 5dcf89321fcb7f44842404a6d59b2b318dd9c7f6 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 08:20:02 -0600 Subject: [PATCH 106/191] smallish changes --- Wumpus/src/LogicExplorer.java | 6 +++++- Wumpus/src/World.java | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 6f40557..161dc52 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -131,6 +131,10 @@ private void processPercepts() { if (((percepts & BUMP) != BUMP) && (percepts & DEATH) != DEATH) { updateLocation(); searchedPositions[location.x][location.y] = true; + removeFromFrontier(location); + } + if((percepts & DEATH)!= 0){ + removeFromFrontier(new Location(getForward().x,getForward().y));// } if((percepts&BUMP)!=0) kb.tell(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null)); @@ -184,7 +188,7 @@ else if(arrowCount > 0 && wumpusInFrontier()){ move(MOVE); removeFromFrontier(wumpusSpace); } - else { + else if(!frontier.isEmpty()){ rhwTraversal(neighborSafeSpace(frontier.get(frontier.size()-1))); turnToSpace(frontier.get(frontier.size()-1)); frontier.remove(frontier.size()-1); diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 3db38d6..83402ee 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -107,7 +107,7 @@ public byte action(int action) { switch (direction) { case NORTH: System.out.println("Moved north"); - if (y + 1 < size) { + if (y + 1 < size || (perceptMap[x][y+1] & BUMP)!=0) { if ((perceptMap[x][y+1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -127,7 +127,7 @@ public byte action(int action) { } case EAST: System.out.println("Moved east"); - if(x+1 < size){ + if(x+1 < size || (perceptMap[x+1][y] & BUMP)!=0){ if ((perceptMap[x+1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -146,7 +146,7 @@ public byte action(int action) { } case SOUTH: System.out.println("Moved south"); - if(y-1 > 0){ + if(y-1 > 0 || (perceptMap[x][y-1] & BUMP)!=0){ if ((perceptMap[x][y-1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -166,7 +166,7 @@ public byte action(int action) { } case WEST: System.out.println("Moved west"); - if(x-1>0){ + if(x-1>0 || (perceptMap[x-1][y] & BUMP)!=0){ if ((perceptMap[x-1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; From 971da101378b2558fe6903985c61c400fdefb710 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 08:33:01 -0600 Subject: [PATCH 107/191] fixed a break --- Wumpus/src/Clause.java | 7 +++++++ Wumpus/src/KnowledgeBase.java | 10 +++++++++- Wumpus/src/Variable.java | 2 ++ Wumpus/src/World.java | 10 +++++----- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index a240ec1..aadd5f6 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -20,6 +20,13 @@ public Clause(Clause clause) { public Clause(Fact fact) { facts.add(fact); } + + public static void printClause(Clause clause){ + for(Fact fact : clause.facts){ + fact.printFact(); + } + System.out.println(""); + } @Override public boolean contains(Variable var) { diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index a138de0..48fba7a 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -23,14 +23,22 @@ public void addToClauses(Clause clause){ } if(clause.facts.size() == 1){ inferenceEngine.infer(clause.facts.get(0)); + System.out.println("Added to kb Clause: "); + System.out.print("\t"); + Clause.printClause(clause); clauses.add(clause); } else{ int beforeSize = clauses.size(); inferenceEngine.infer(clause); - if(clauses.size() == beforeSize) + if(clauses.size() == beforeSize){ clauses.add(clause);//We only need add the clause itself if we didn't infer anything, otherwise the inferred shortened clause which is more valuable got added + + System.out.println("Added to kb Clause: "); + System.out.print("\t"); + Clause.printClause(clause); + } } //clauses.add(clause); diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index 5b596e5..e74034a 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -30,6 +30,8 @@ public void printVariable() { if (isVariable) { System.out.print((char) (variableId + 97)); } + else + System.out.print(value); } public int getValue() { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 83402ee..2e7ac08 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -107,7 +107,7 @@ public byte action(int action) { switch (direction) { case NORTH: System.out.println("Moved north"); - if (y + 1 < size || (perceptMap[x][y+1] & BUMP)!=0) { + if (y + 1 < size-1 && (perceptMap[x][y+1] & BUMP)!=0) { if ((perceptMap[x][y+1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -127,7 +127,7 @@ public byte action(int action) { } case EAST: System.out.println("Moved east"); - if(x+1 < size || (perceptMap[x+1][y] & BUMP)!=0){ + if(x+1 < size-1 && (perceptMap[x+1][y] & BUMP)!=0){ if ((perceptMap[x+1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -146,7 +146,7 @@ public byte action(int action) { } case SOUTH: System.out.println("Moved south"); - if(y-1 > 0 || (perceptMap[x][y-1] & BUMP)!=0){ + if(y > 0 && (perceptMap[x][y-1] & BUMP)!=0){ if ((perceptMap[x][y-1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -166,7 +166,7 @@ public byte action(int action) { } case WEST: System.out.println("Moved west"); - if(x-1>0 || (perceptMap[x-1][y] & BUMP)!=0){ + if(x>0 && (perceptMap[x-1][y] & BUMP)!=0){ if ((perceptMap[x-1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -198,7 +198,7 @@ public byte action(int action) { return perceptMap[x][y]; case SHOOT: //shoot logic - if (arrowCount < 1) { + if (arrowCount == 0) { return -1; //out of arrows, which shouldn't be possible } arrowCount--; From af70d655b66b06b53a4ddf0517f52d9d5de59ae0 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Fri, 21 Oct 2016 08:41:41 -0600 Subject: [PATCH 108/191] goTo mostly working? its getting called way too often though so weird things are happening...investingating --- Wumpus/src/LogicExplorer.java | 202 ++++++++++++++++++++-------------- 1 file changed, 117 insertions(+), 85 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 6f40557..c735328 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -14,7 +14,7 @@ public class LogicExplorer extends Agent { private Location safeSpace; private Location wumpusSpace; private ArrayList moveHistory = new ArrayList<>(); - boolean notFirstMove = false; + boolean notFirstMove = false; public LogicExplorer(World world, int startingArrows, int startingX, int startingY, int direction) { super(world, startingArrows, startingX, startingY, direction); @@ -30,36 +30,37 @@ public void initializeFrontier() { if (location.x > 0) { frontier.add(new Location(location.x - 1, location.y)); } - if (location.x < world.size-1) { + if (location.x < world.size - 1) { frontier.add(new Location(location.x + 1, location.y)); } if (location.y > 0) { frontier.add(new Location(location.x, location.y - 1)); } - if (location.y < world.size-1) { + if (location.y < world.size - 1) { frontier.add(new Location(location.x, location.y + 1)); } } - public void updateLocation(){ - if(notFirstMove) + public void updateLocation() { + if (notFirstMove) { super.updateLocation(); - else + } else { notFirstMove = true; + } expandFrontier(); } public void expandFrontier() { - if (location.x > 0 && !searchedPositions[location.x-1][location.y]) { + if (location.x > 0 && !searchedPositions[location.x - 1][location.y]) { frontier.add(new Location(location.x - 1, location.y)); } - if (location.x < world.size -1&& !searchedPositions[location.x+1][location.y]) { + if (location.x < world.size - 1 && !searchedPositions[location.x + 1][location.y]) { frontier.add(new Location(location.x + 1, location.y)); } - if (location.y > 0 && !searchedPositions[location.x][location.y-1]) { + if (location.y > 0 && !searchedPositions[location.x][location.y - 1]) { frontier.add(new Location(location.x, location.y - 1)); } - if (location.y < world.size -1&& !searchedPositions[location.x][location.y+1]) { + if (location.y < world.size - 1 && !searchedPositions[location.x][location.y + 1]) { frontier.add(new Location(location.x, location.y + 1)); } } @@ -90,13 +91,13 @@ private void move(int action) { case TURN_LEFT: moveHistory.add(TURN_LEFT); System.out.println("Turning left"); - direction = (direction + 3)%4; + direction = (direction + 3) % 4; world.action(TURN_LEFT); break; case TURN_RIGHT: moveHistory.add(TURN_RIGHT); System.out.println("turning right"); - direction = (direction+1)%4; + direction = (direction + 1) % 4; world.action(TURN_RIGHT); break; case SHOOT: @@ -132,8 +133,9 @@ private void processPercepts() { updateLocation(); searchedPositions[location.x][location.y] = true; } - if((percepts&BUMP)!=0) - kb.tell(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null)); + if ((percepts & BUMP) != 0) { + kb.tell(new Fact("Obsticle", getForward().x, false, getForward().y, false, false, null, null)); + } if ((percepts & STENCH) != 0) { kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); } else { @@ -176,108 +178,110 @@ private void decideNextAction() { turnToSpace(safeSpace); move(MOVE); removeFromFrontier(safeSpace); - } - else if(arrowCount > 0 && wumpusInFrontier()){ + } else if (arrowCount > 0 && wumpusInFrontier()) { rhwTraversal(neighborSafeSpace(wumpusSpace)); turnToSpace(wumpusSpace); move(SHOOT); move(MOVE); removeFromFrontier(wumpusSpace); - } - else { - rhwTraversal(neighborSafeSpace(frontier.get(frontier.size()-1))); - turnToSpace(frontier.get(frontier.size()-1)); - frontier.remove(frontier.size()-1); + } else { + rhwTraversal(neighborSafeSpace(frontier.get(frontier.size() - 1))); + turnToSpace(frontier.get(frontier.size() - 1)); + frontier.remove(frontier.size() - 1); move(MOVE); } } - - public void turnToSpace(Location loc){ - if(location.x > loc.x){ - if(direction == EAST) + + public void turnToSpace(Location loc) { + if (location.x > loc.x) { + if (direction == EAST) { return; - else if(direction == NORTH) + } else if (direction == NORTH) { move(TURN_LEFT); - else if(direction == SOUTH) + } else if (direction == SOUTH) { move(TURN_RIGHT); - } - else if(loc.x > location.x){ - if(direction == WEST) + } + } else if (loc.x > location.x) { + if (direction == WEST) { return; - else if(direction == NORTH) + } else if (direction == NORTH) { move(TURN_RIGHT); - else if(direction == SOUTH) + } else if (direction == SOUTH) { move(TURN_LEFT); - } - else if(loc.y < location.y){ - if(direction == SOUTH) + } + } else if (loc.y < location.y) { + if (direction == SOUTH) { return; - else if(direction == WEST) + } else if (direction == WEST) { move(TURN_LEFT); - else if(direction == EAST) + } else if (direction == EAST) { move(TURN_RIGHT); - } - else if(loc.y > location.y){ - if(direction == NORTH){ - return; } - else if(direction == WEST){ + } else if (loc.y > location.y) { + if (direction == NORTH) { + return; + } else if (direction == WEST) { move(TURN_RIGHT); - } - else if(direction == EAST) + } else if (direction == EAST) { move(TURN_LEFT); + } } } - - private Location neighborSafeSpace(Location location){ + + private Location neighborSafeSpace(Location location) { Location loc = null; - if(location.x > 0 && searchedPositions[location.x-1][location.y]){ - loc = new Location(location.x-1,location.y); - - } - else if(location.x < World.size -1 && searchedPositions[location.x+1][location.y]) - loc = new Location(location.x+1,location.y); - else if(location.y > 0 && searchedPositions[location.x][location.y-1]) - loc = new Location(location.x,location.y-1); - else if(location.y < World.size -1 && searchedPositions[location.x][location.y+1]) - loc = new Location(location.x,location.y+1); - + if (location.x > 0 && searchedPositions[location.x - 1][location.y]) { + loc = new Location(location.x - 1, location.y); + + } else if (location.x < World.size - 1 && searchedPositions[location.x + 1][location.y]) { + loc = new Location(location.x + 1, location.y); + } else if (location.y > 0 && searchedPositions[location.x][location.y - 1]) { + loc = new Location(location.x, location.y - 1); + } else if (location.y < World.size - 1 && searchedPositions[location.x][location.y + 1]) { + loc = new Location(location.x, location.y + 1); + } + return loc; } - - private boolean wumpusInFrontier(){ - for(Location loc : frontier){ - if(kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))){ + + private boolean wumpusInFrontier() { + for (Location loc : frontier) { + if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { wumpusSpace = loc; return true; } } - + return false; } - private void moveHistoryTraversal(Location loc){ - if(loc.x == location.x && loc.y == location.y){ + private void moveHistoryTraversal(Location loc) { + if (loc.x == location.x && loc.y == location.y) { return; } - for(int i = moveHistory.size()-1;i>=0; i--){ - if(loc.x == location.x && loc.y == location.y){ + for (int i = moveHistory.size() - 1; i >= 0; i--) { + if (loc.x == location.x && loc.y == location.y) { return; } int move = moveHistory.get(i); - if(move == MOVE){ + if (move == MOVE) { move(TURN_LEFT); move(TURN_LEFT); move(MOVE); - } - else if(move == TURN_LEFT) + } else if (move == TURN_LEFT) { move(TURN_RIGHT); - else if(move == TURN_RIGHT) + } else if (move == TURN_RIGHT) { move(TURN_LEFT); + } } } + private void rhwTraversal(Location location) { - moveHistoryTraversal(location); + //moveHistoryTraversal(location); + if (this.location != location) { + goTo(location.x, location.y); + } + //go to location zach NOOOO! } @@ -292,11 +296,10 @@ private boolean safeSpaceInFrontier() { } } } - if ( kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, false, null, null))) { + if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, false, null, null))) { //there is specifically a wumpus, pit, or obsticle at this position, don't navigate to it. frontier.remove(i); - } - else if(arrowCount == 0 && kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))){ + } else if (arrowCount == 0 && kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { frontier.remove(i); } } @@ -309,20 +312,19 @@ private void goTo(int x, int y) { path = searchForPath(location.x, location.y, x, y, path); traversePath(path); } - + private ArrayList searchForPath(int curX, int curY, int goalX, int goalY, ArrayList path) { - if (searchNext(curX, curY, goalX, goalY, path)) { return path; } else { System.out.println("Path finding error, no path found."); - return null; + return path; } } - + private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path) { - + path.add(new Location(curX, curY)); if (curX == goalX && curY == goalY) { return true; @@ -339,21 +341,51 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList= 0 && y >= 0 && x < World.size && y < World.size) { return searchedPositions[x][y]; } else { return false; } } - + private void traversePath(ArrayList path) { - - while (!path.isEmpty()) { + + while (path.size() > 0) { Location next = path.remove(0); - //go to next + //turn to face + if (next.x > this.location.x) { + //go east + turnToSpace(next); + move(MOVE); + //world.action(MOVE); + } else if (next.x < this.location.x) { + //go west + turnToSpace(next); + move(MOVE); + //world.action(MOVE); + } else if (next.y > this.location.y) { + //go north + turnToSpace(next); + move(MOVE); + //world.action(MOVE); + } else if (next.y < this.location.y) { + //go south + turnToSpace(next); + move(MOVE); + //world.action(MOVE); + } else { + System.out.println("Error traversing path."); + } + } + } + + private void turnToFace(int direction) { + + while (this.direction != direction) { + turnLeft(); } } } From 88716064f75349b23cbeda966a4c4242da521216 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 08:44:58 -0600 Subject: [PATCH 109/191] Found no solution really really fast --- Wumpus/src/Fact.java | 2 ++ Wumpus/src/KnowledgeBase.java | 19 +++++++++++++++++++ Wumpus/src/LogicExplorer.java | 10 ++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index ba7f3a2..b285dc7 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -30,6 +30,8 @@ public Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean } public void printFact() { + if(not) + System.out.print("!"); System.out.print(predicate + "("); for (Variable var : variables) { var.printVariable(); diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 48fba7a..1e718ad 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -121,7 +121,26 @@ public void tell(Fact fact) { } } } + if(factInClauses(fact)){ + return; + } + addToClauses(new Clause(fact)); //and check if there is a stench at any adjacent position, remove those facts too } + private boolean factInClauses(Fact fact){ + for(Clause clause : clauses){ + if(clause.facts.size() == 1){ + Fact clauseFact = clause.facts.get(0); + if(clauseFact.predicate.equals(fact.predicate)){ + for(int i = 0; i < fact.variables.size(); i++){ + if(clauseFact.variables.get(i).value != fact.variables.get(i).value){ + return true; + } + } + } + } + } + return false; + } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 161dc52..a189320 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -126,18 +126,20 @@ public void removeFromFrontier(Location locToRemove) { private void processPercepts() { if ((percepts & DEATH) != 0) { + System.out.println("Explorer died after moving"); removeFromFrontier(getForward()); + moveHistory.remove(moveHistory.size()-1);//why die again? } if (((percepts & BUMP) != BUMP) && (percepts & DEATH) != DEATH) { updateLocation(); searchedPositions[location.x][location.y] = true; removeFromFrontier(location); } - if((percepts & DEATH)!= 0){ - removeFromFrontier(new Location(getForward().x,getForward().y));// - } - if((percepts&BUMP)!=0) + if((percepts&BUMP)!=0){ + System.out.println("Explorer bumped after moving"); + moveHistory.remove(moveHistory.size()-1);//why bump again? kb.tell(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null)); + } if ((percepts & STENCH) != 0) { kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); } else { From 3f73add633f3a5246367c5acb144c92feb302b01 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Fri, 21 Oct 2016 09:12:35 -0600 Subject: [PATCH 110/191] Theres some problem with him running into walls that dont exist? --- Wumpus/src/Agent.java | 4 ++++ Wumpus/src/LogicExplorer.java | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 9213b0c..0ad92df 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -91,5 +91,9 @@ public Location(int x, int y) { this.x = x; this.y = y; } + + public boolean equals(Location location) { + return (this.x == location.x && this.y == location.y); + } } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index ad34fa7..264adda 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -125,7 +125,7 @@ public void removeFromFrontier(Location locToRemove) { } } - private void processPercepts() { + private void processPercepts() { //there might still be an issue with wumpus death since its a seperate percept if ((percepts & DEATH) != 0) { System.out.println("Explorer died after moving"); removeFromFrontier(getForward()); @@ -284,12 +284,27 @@ private void moveHistoryTraversal(Location loc) { private void rhwTraversal(Location location) { //moveHistoryTraversal(location); - if (this.location != location) { + if (!this.location.equals(location) && !adjacent(location)) { goTo(location.x, location.y); } //go to location zach NOOOO! } + + private boolean adjacent(Location location) { + + if (location.x == this.location.x) { + if (Math.abs(location.x - this.location.x) == 1) { + return true; + } + } + if (location.y == this.location.y) { + if (Math.abs(location.y - this.location.y) == 1) { + return true; + } + } + return false; + } private boolean safeSpaceInFrontier() { for (int i = frontier.size() - 1; i >= 0; i--) { From 255cf0350a5ebb1a22784cb880b938f22f5069fc Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Fri, 21 Oct 2016 09:32:11 -0600 Subject: [PATCH 111/191] Some cleanup and readability stuff, change to death percepts...lets just use both since he never specified we couldn't and it makes things easier. Also tweaked some logic in navigation. The big issue right now is in processPercepts()...weird shit is happening with bumping into walls that don't exist. --- Wumpus/PerceptBoard.txt | 12 +-- Wumpus/src/Agent.java | 2 +- Wumpus/src/Driver.java | 6 +- Wumpus/src/LogicExplorer.java | 154 +++++++++++++++++----------------- Wumpus/src/World.java | 18 ++-- Wumpus/world.txt | 8 +- 6 files changed, 101 insertions(+), 99 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 29424d5..6257dc1 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 3 1 -4 0 0 0 0 -9 0 0 0 0 -32 1 1 2 1 -1 1 34 17 34 -0 0 1 2 1 +5 1 1 +17 1 2 5 0 +17 3 33 18 1 +1 0 10 1 0 +4 0 4 0 0 +0 0 0 0 0 diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 0ad92df..5b620b2 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -5,7 +5,7 @@ public class Agent { protected Location location; protected int direction, arrowCount; - protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; + protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH_PIT = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; protected static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3; protected byte percepts; diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 543c496..bf6f94d 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + // makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } @@ -35,9 +35,9 @@ public static void makeGame() throws IOException { WumpusGame game = new WumpusGame(5, prob); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); - World world = new World("PerceptBoard.txt"); + // World world = new World("PerceptBoard.txt"); //world.startGame("LogicExplorer"); - world.startGame("ReactiveExplorer"); + // world.startGame("ReactiveExplorer"); //Agent explorer = new LogicExplorer(world); //world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 264adda..c34e37a 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -1,8 +1,5 @@ import java.util.ArrayList; -import java.util.LinkedList; -import java.util.PriorityQueue; -import java.util.Queue; public class LogicExplorer extends Agent { @@ -126,20 +123,20 @@ public void removeFromFrontier(Location locToRemove) { } private void processPercepts() { //there might still be an issue with wumpus death since its a seperate percept - if ((percepts & DEATH) != 0) { + if ((percepts & DEATH_PIT) == DEATH_PIT) { System.out.println("Explorer died after moving"); removeFromFrontier(getForward()); - moveHistory.remove(moveHistory.size()-1);//why die again? + moveHistory.remove(moveHistory.size() - 1);//why die again? } - if (((percepts & BUMP) != BUMP) && (percepts & DEATH) != DEATH) { + if ((((percepts & BUMP) != BUMP) && (percepts & DEATH_PIT) != DEATH_PIT) && ((percepts & DEATH_WUMPUS) != DEATH_WUMPUS)) { updateLocation(); searchedPositions[location.x][location.y] = true; removeFromFrontier(location); } - if((percepts&BUMP)!=0){ + if ((percepts & BUMP) == BUMP) { //theres soemthing funky here, hes bumping when there arent obsticales System.out.println("Explorer bumped after moving"); - moveHistory.remove(moveHistory.size()-1);//why bump again? - kb.tell(new Fact("Obsticle",getForward().x,false,getForward().y,false,false,null,null)); + moveHistory.remove(moveHistory.size() - 1);//why bump again? + kb.tell(new Fact("Obsticle", getForward().x, false, getForward().y, false, false, null, null)); } if ((percepts & STENCH) != 0) { kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); @@ -163,12 +160,7 @@ private void decideNextAction() { if ((percepts & GLITTER) != 0) { move(World.GRAB); } - - //check if adjacent to any unexplored and safe spaces - //check forward - //check left - //check right - if (getForward().x >= 0 && getForward().x < world.size && getForward().y >= 0 && getForward().y < world.size) { + if (getForward().x >= 0 && getForward().x < World.size && getForward().y >= 0 && getForward().y < World.size) { if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, true, null, null))) { if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { if (kb.ask(new Fact("Obsticle", getForward().x, false, getForward().y, false, true, null, null))) { @@ -189,47 +181,66 @@ private void decideNextAction() { move(SHOOT); move(MOVE); removeFromFrontier(wumpusSpace); - } - else if(!frontier.isEmpty()){ - rhwTraversal(neighborSafeSpace(frontier.get(frontier.size()-1))); - turnToSpace(frontier.get(frontier.size()-1)); - frontier.remove(frontier.size()-1); + } else if (!frontier.isEmpty()) { + rhwTraversal(neighborSafeSpace(frontier.get(frontier.size() - 1))); + turnToSpace(frontier.get(frontier.size() - 1)); + frontier.remove(frontier.size() - 1); move(MOVE); } } public void turnToSpace(Location loc) { if (location.x > loc.x) { - if (direction == EAST) { - return; - } else if (direction == NORTH) { - move(TURN_LEFT); - } else if (direction == SOUTH) { - move(TURN_RIGHT); + switch (direction) { + case EAST: + break; + case NORTH: + move(TURN_LEFT); + break; + case SOUTH: + move(TURN_RIGHT); + break; + default: + break; } } else if (loc.x > location.x) { - if (direction == WEST) { - return; - } else if (direction == NORTH) { - move(TURN_RIGHT); - } else if (direction == SOUTH) { - move(TURN_LEFT); + switch (direction) { + case WEST: + return; + case NORTH: + move(TURN_RIGHT); + break; + case SOUTH: + move(TURN_LEFT); + break; + default: + break; } } else if (loc.y < location.y) { - if (direction == SOUTH) { - return; - } else if (direction == WEST) { - move(TURN_LEFT); - } else if (direction == EAST) { - move(TURN_RIGHT); + switch (direction) { + case SOUTH: + return; + case WEST: + move(TURN_LEFT); + break; + case EAST: + move(TURN_RIGHT); + break; + default: + break; } } else if (loc.y > location.y) { - if (direction == NORTH) { - return; - } else if (direction == WEST) { - move(TURN_RIGHT); - } else if (direction == EAST) { - move(TURN_LEFT); + switch (direction) { + case NORTH: + return; + case WEST: + move(TURN_RIGHT); + break; + case EAST: + move(TURN_LEFT); + break; + default: + break; } } } @@ -246,7 +257,6 @@ private Location neighborSafeSpace(Location location) { } else if (location.y < World.size - 1 && searchedPositions[location.x][location.y + 1]) { loc = new Location(location.x, location.y + 1); } - return loc; } @@ -257,7 +267,6 @@ private boolean wumpusInFrontier() { return true; } } - return false; } @@ -270,38 +279,43 @@ private void moveHistoryTraversal(Location loc) { return; } int move = moveHistory.get(i); - if (move == MOVE) { - move(TURN_LEFT); - move(TURN_LEFT); - move(MOVE); - } else if (move == TURN_LEFT) { - move(TURN_RIGHT); - } else if (move == TURN_RIGHT) { - move(TURN_LEFT); + switch (move) { + case MOVE: + move(TURN_LEFT); + move(TURN_LEFT); + move(MOVE); + break; + case TURN_LEFT: + move(TURN_RIGHT); + break; + case TURN_RIGHT: + move(TURN_LEFT); + break; + default: + break; } } } private void rhwTraversal(Location location) { - //moveHistoryTraversal(location); + // moveHistoryTraversal(location); if (!this.location.equals(location) && !adjacent(location)) { goTo(location.x, location.y); } - //go to location zach NOOOO! } - + private boolean adjacent(Location location) { - + if (location.x == this.location.x) { if (Math.abs(location.x - this.location.x) == 1) { return true; - } + } } if (location.y == this.location.y) { if (Math.abs(location.y - this.location.y) == 1) { return true; - } + } } return false; } @@ -350,13 +364,13 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path) { while (path.size() > 0) { Location next = path.remove(0); - //turn to face if (next.x > this.location.x) { //go east turnToSpace(next); move(MOVE); - //world.action(MOVE); } else if (next.x < this.location.x) { //go west turnToSpace(next); move(MOVE); - //world.action(MOVE); } else if (next.y > this.location.y) { //go north turnToSpace(next); move(MOVE); - //world.action(MOVE); } else if (next.y < this.location.y) { //go south turnToSpace(next); move(MOVE); - //world.action(MOVE); } else { System.out.println("Error traversing path."); } } } - - private void turnToFace(int direction) { - - while (this.direction != direction) { - turnLeft(); - } - } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 2e7ac08..6951d81 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -7,7 +7,7 @@ public final class World { public static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3; public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; - protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; + protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH_PIT = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; private int arrowCount, x, y, direction = 0, score = 0, numMoves = 0, pitDeaths = 0, wumpusDeaths = 0; public static int size; @@ -114,11 +114,11 @@ public byte action(int action) { System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x][y+1] & DEATH) == DEATH) { + if ((perceptMap[x][y+1] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); - return DEATH; + return DEATH_PIT; } y = y + 1; return perceptMap[x][y]; @@ -134,10 +134,10 @@ public byte action(int action) { System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x+1][y] & DEATH) == DEATH) { + if ((perceptMap[x+1][y] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; - return DEATH; + return DEATH_PIT; } x = x + 1; return perceptMap[x][y]; @@ -153,11 +153,11 @@ public byte action(int action) { System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x][y-1] & DEATH) == DEATH) { + if ((perceptMap[x][y-1] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); - return DEATH; + return DEATH_PIT; } y -= 1; return perceptMap[x][y]; @@ -173,11 +173,11 @@ public byte action(int action) { System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x-1][y] & DEATH) == DEATH) { + if ((perceptMap[x-1][y] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); - return DEATH; + return DEATH_PIT; } x -= 1; return perceptMap[x][y]; diff --git a/Wumpus/world.txt b/Wumpus/world.txt index b181881..d3e1cb3 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -W 0 I H 0 H 0 0 I 0 -I H 0 0 I -0 0 0 G S -0 0 I 0 I +H S W H 0 +0 0 G 0 0 +I 0 I 0 0 +0 0 0 0 0 From b515cc984f04f357c3dff01c94e0a0875e07e01c Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 10:43:21 -0600 Subject: [PATCH 112/191] Holy shit it's working --- Wumpus/src/KnowledgeBase.java | 12 ++++++------ Wumpus/src/LogicExplorer.java | 25 ++++++++++++++----------- Wumpus/src/World.java | 10 +++++----- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 1e718ad..823ecda 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -68,16 +68,16 @@ public void initializeRules() { //Breeze(x,y)=>(Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) //!Breeze(x,y) v Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) Clause breeze = new Clause(); - Fact breezehXY = new Fact("Breeze",0,true,1,true,true,null,null); + Fact breezeXY = new Fact("Breeze",0,true,1,true,true,null,null); Fact pitxminy = new Fact("Pit",0,true,1,true,false,new MinusFunction(),null); Fact pitxplusy = new Fact("Pit",0,true,1,true,false,new PlusFunction(),null); Fact pitxymin = new Fact("Pit",0,true,1,true,false,null,new MinusFunction()); Fact pitxyplus = new Fact("Pit",0,true,1,true,false,null,new PlusFunction()); - breeze.facts.add(stenchXY); - breeze.facts.add(wumpusxminy); - breeze.facts.add(wumpusxplusy); - breeze.facts.add(wumpusxymin); - breeze.facts.add(wumpusxyplus); + breeze.facts.add(breezeXY); + breeze.facts.add(pitxminy); + breeze.facts.add(pitxplusy); + breeze.facts.add(pitxymin); + breeze.facts.add(pitxyplus); rules.add(breeze); //!Wumpus(-1,y) ^ !Wumpus(x,-1) ^ !Wumpus(x,World.size)^ !Wumpus(World.size,y) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index c34e37a..c3e15e4 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -65,8 +65,9 @@ public void expandFrontier() { private void run() { while (true) { + if(!notFirstMove){ percepts = world.getPercepts(); - processPercepts(); + processPercepts();} decideNextAction(); } } @@ -136,21 +137,23 @@ private void processPercepts() { //there might still be an issue with wum if ((percepts & BUMP) == BUMP) { //theres soemthing funky here, hes bumping when there arent obsticales System.out.println("Explorer bumped after moving"); moveHistory.remove(moveHistory.size() - 1);//why bump again? + removeFromFrontier(getForward()); kb.tell(new Fact("Obsticle", getForward().x, false, getForward().y, false, false, null, null)); } + else{//you if you bump than the only inputted percept should've been bump if ((percepts & STENCH) != 0) { - kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); - } else { kb.tell(new Fact("Stench", location.x, false, location.y, false, false, null, null)); + } else { + kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); } if ((percepts & BREEZE) != 0) { - kb.tell(new Fact("Breeze", location.x, false, location.y, false, true, null, null)); - } else { kb.tell(new Fact("Breeze", location.x, false, location.y, false, false, null, null)); + } else { + kb.tell(new Fact("Breeze", location.x, false, location.y, false, true, null, null)); } if ((percepts & SCREAM) != 0) { - kb.tell(new Fact("Scream", location.x, false, location.y, false, true, null, null)); - } + kb.tell(new Fact("Scream", location.x, false, location.y, false, false, null, null)); + }} } private void decideNextAction() { @@ -298,10 +301,10 @@ private void moveHistoryTraversal(Location loc) { } private void rhwTraversal(Location location) { - // moveHistoryTraversal(location); - if (!this.location.equals(location) && !adjacent(location)) { - goTo(location.x, location.y); - } + moveHistoryTraversal(location); +// if (!this.location.equals(location) && !adjacent(location)) { +// goTo(location.x, location.y); +// } //go to location zach NOOOO! } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 6951d81..d466a8d 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -67,7 +67,7 @@ public byte getPercepts() { public void printWorld() { - for (int i = 0; i < perceptMap.length; i++) { + for (int i = perceptMap.length-1; i >= 0; i--) { for (int j = 0; j < perceptMap.length; j++) { if (x == i && y == j) { System.out.print("A "); @@ -107,7 +107,7 @@ public byte action(int action) { switch (direction) { case NORTH: System.out.println("Moved north"); - if (y + 1 < size-1 && (perceptMap[x][y+1] & BUMP)!=0) { + if (y + 1 < size-1 && (perceptMap[x][y+1] & BUMP)==0) { if ((perceptMap[x][y+1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -127,7 +127,7 @@ public byte action(int action) { } case EAST: System.out.println("Moved east"); - if(x+1 < size-1 && (perceptMap[x+1][y] & BUMP)!=0){ + if(x+1 < size-1 && (perceptMap[x+1][y] & BUMP)==0){ if ((perceptMap[x+1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -146,7 +146,7 @@ public byte action(int action) { } case SOUTH: System.out.println("Moved south"); - if(y > 0 && (perceptMap[x][y-1] & BUMP)!=0){ + if(y > 0 && (perceptMap[x][y-1] & BUMP)==0){ if ((perceptMap[x][y-1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -166,7 +166,7 @@ public byte action(int action) { } case WEST: System.out.println("Moved west"); - if(x>0 && (perceptMap[x-1][y] & BUMP)!=0){ + if(x>0 && (perceptMap[x-1][y] & BUMP)==0){ if ((perceptMap[x-1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; From b1a082834d7d31245d886810e6e18806fac6165c Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 11:01:02 -0600 Subject: [PATCH 113/191] working even better now --- Wumpus/src/LogicExplorer.java | 19 +++++++++++++++++-- Wumpus/src/World.java | 6 +++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index c3e15e4..dcf70ed 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -124,7 +124,7 @@ public void removeFromFrontier(Location locToRemove) { } private void processPercepts() { //there might still be an issue with wumpus death since its a seperate percept - if ((percepts & DEATH_PIT) == DEATH_PIT) { + if ((percepts & DEATH_PIT) == DEATH_PIT || (percepts & DEATH_WUMPUS)!= 0) { System.out.println("Explorer died after moving"); removeFromFrontier(getForward()); moveHistory.remove(moveHistory.size() - 1);//why die again? @@ -140,7 +140,7 @@ private void processPercepts() { //there might still be an issue with wum removeFromFrontier(getForward()); kb.tell(new Fact("Obsticle", getForward().x, false, getForward().y, false, false, null, null)); } - else{//you if you bump than the only inputted percept should've been bump + else if((percepts & DEATH_PIT) == 0 && (percepts & DEATH_WUMPUS)==0){//you if you bump than the only inputted percept should've been bump if ((percepts & STENCH) != 0) { kb.tell(new Fact("Stench", location.x, false, location.y, false, false, null, null)); } else { @@ -203,6 +203,10 @@ public void turnToSpace(Location loc) { case SOUTH: move(TURN_RIGHT); break; + case WEST: + move(TURN_RIGHT); + move(TURN_RIGHT); + break; default: break; } @@ -216,6 +220,10 @@ public void turnToSpace(Location loc) { case SOUTH: move(TURN_LEFT); break; + case EAST: + move(TURN_RIGHT); + move(TURN_RIGHT); + break; default: break; } @@ -229,6 +237,10 @@ public void turnToSpace(Location loc) { case EAST: move(TURN_RIGHT); break; + case NORTH: + move(TURN_RIGHT); + move(TURN_RIGHT); + break; default: break; } @@ -242,6 +254,9 @@ public void turnToSpace(Location loc) { case EAST: move(TURN_LEFT); break; + case SOUTH: + move(TURN_RIGHT); + move(TURN_RIGHT); default: break; } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index d466a8d..fccc832 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -69,14 +69,14 @@ public void printWorld() { for (int i = perceptMap.length-1; i >= 0; i--) { for (int j = 0; j < perceptMap.length; j++) { - if (x == i && y == j) { + if (x == j && y == i) { System.out.print("A "); } else { - if ((perceptMap[i][j] & DEATH_WUMPUS) != 0) { + if ((perceptMap[j][i] & DEATH_WUMPUS) != 0) { System.out.print("W "); } else { - System.out.print(perceptMap[i][j] + " "); + System.out.print(perceptMap[j][i] + " "); } } } From 8563bd6e55e10887c3fa3fc34a255b909756441e Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 11:26:53 -0600 Subject: [PATCH 114/191] big important changes --- Wumpus/PerceptBoard.txt | 10 ++--- Wumpus/src/Driver.java | 2 +- Wumpus/src/Fact.java | 2 + Wumpus/src/InferenceEngine.java | 4 ++ Wumpus/src/KnowledgeBase.java | 69 ++++++++++++++++++++++++++++++++- Wumpus/src/LogicExplorer.java | 4 +- Wumpus/world.txt | 8 ++-- 7 files changed, 86 insertions(+), 13 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 6257dc1..a407c63 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 1 1 -17 1 2 5 0 -17 3 33 18 1 -1 0 10 1 0 -4 0 4 0 0 +5 4 4 +0 0 0 0 0 +0 6 0 0 0 +2 32 2 0 0 +0 2 0 4 8 0 0 0 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index bf6f94d..61cdcf1 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index b285dc7..f8804a0 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -14,6 +14,7 @@ public Fact(Fact fact) { variables.add(new Variable(var)); } predicate = fact.predicate; + this.function = fact.function; } @@ -26,6 +27,7 @@ public Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean variables.add(var1); variables.add(var2); this.predicate = predicate; + this.not = not; } diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index a87785e..6aa43e6 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -108,6 +108,8 @@ public void infer(Fact factStart) { } clause.facts.remove(ruleFact); + System.out.println("Inferred:"); + Clause.printClause(clause); kb.addToClauses(clause); break; } @@ -156,6 +158,8 @@ public void infer(Clause clauseToCheck) { } if(!substitutions.isEmpty()){ clause.facts.remove(fact); + System.out.println("Inferred:"); + Clause.printClause(clause); kb.addToClauses(clause); } break; diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 823ecda..5f2ac6e 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -49,6 +49,7 @@ public void initializeRules() { //Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function) //Special predicate: Evaluate + //Stench(x,y)<=>(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) //Stench(x,y)=>(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) //!Stench(x,y) v Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) Clause stench = new Clause(); @@ -63,9 +64,43 @@ public void initializeRules() { stench.facts.add(wumpusxymin); stench.facts.add(wumpusxyplus); rules.add(stench); + //(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1)) =>Stench(x,y) + //!(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1)) v Stench(x,y) + //(!Wumpus(x-1,y) ^ !Wumpus(x+1,y) ^ !Wumpus(x,y-1) ^ !Wumpus(x,y+1)) v Stench(x,y) + //(Stench(x,y) v !Wumpus(x-1,y)), (Stench(x,y) v !Wumpus(x+1,y)), (Stench(x,y) v !Wumpus(x,y-1)), (Stench(x,y) v !Wumpus(x,y+1) + Clause stench1 = new Clause(); + Fact stenchXY1 = new Fact(stenchXY); + Fact wumpusxminy1 = new Fact(wumpusxminy); + wumpusxminy1.not = true; + stench1.facts.add(stenchXY1); + stench1.facts.add(wumpusxminy1); + rules.add(stench1); + Clause stench2 = new Clause(); + Fact stenchXY2 = new Fact(stenchXY); + Fact wumpusxplusy1 = new Fact(wumpusxplusy); + wumpusxplusy1.not = true; + stench2.facts.add(stenchXY2); + stench2.facts.add(wumpusxplusy1); + rules.add(stench2); - //Breeze(x,y)=>(Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) + Clause stench3 = new Clause(); + Fact stenchXY3 = new Fact(stenchXY); + Fact wumpusxymin1 = new Fact(wumpusxymin); + wumpusxymin1.not = true; + stench3.facts.add(stenchXY3); + stench3.facts.add(wumpusxymin1); + rules.add(stench3); + + Clause stench4 = new Clause(); + Fact stenchXY4 = new Fact(stenchXY); + Fact wumpusxyplus1 = new Fact(wumpusxyplus); + wumpusxyplus1.not = true; + stench4.facts.add(stenchXY4); + stench4.facts.add(wumpusxyplus1); + rules.add(stench4); + + //Breeze(x,y)<=>(Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) //!Breeze(x,y) v Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) Clause breeze = new Clause(); Fact breezeXY = new Fact("Breeze",0,true,1,true,true,null,null); @@ -80,6 +115,38 @@ public void initializeRules() { breeze.facts.add(pitxyplus); rules.add(breeze); + Clause breeze1 = new Clause(); + Fact breezeXY1 = new Fact(breezeXY); + Fact pitxminy1 = new Fact(pitxminy); + pitxminy1.not = true; + breeze1.facts.add(breezeXY1); + breeze1.facts.add(pitxminy1); + rules.add(breeze1); + + Clause breeze2 = new Clause(); + Fact breezeXY2 = new Fact(breezeXY); + Fact pitxplusy1 = new Fact(pitxplusy); + pitxplusy1.not = true; + breeze2.facts.add(breezeXY2); + breeze2.facts.add(pitxplusy1); + rules.add(breeze2); + + Clause breeze3 = new Clause(); + Fact breezeXY3 = new Fact(breezeXY); + Fact pitxymin1 = new Fact(pitxymin); + pitxymin1.not = true; + breeze3.facts.add(breezeXY3); + breeze3.facts.add(pitxymin1); + rules.add(breeze3); + + Clause breeze4 = new Clause(); + Fact breezeXY4 = new Fact(breezeXY); + Fact pitxyplus1 = new Fact(pitxyplus); + pitxyplus1.not = true; + breeze4.facts.add(breezeXY4); + breeze4.facts.add(pitxyplus1); + rules.add(breeze4); + //!Wumpus(-1,y) ^ !Wumpus(x,-1) ^ !Wumpus(x,World.size)^ !Wumpus(World.size,y) //!Wumpus(-1,y), !Wumpus(x,-1), !Wumpus(x,World.size), !Wumpus(World.size,y) rules.add(new Clause(new Fact("Wumpus",-1,false,1,true,true,null,null))); diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index dcf70ed..2e1af4e 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -343,10 +343,10 @@ private boolean safeSpaceInFrontier() { Location loc = frontier.get(i); if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, true, null, null))) { if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, true, null, null))) { - if (kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, true, null, null))) { + //hopefully no obsticle is in frontier as it should be removed when found... safeSpace = new Location(loc.x, loc.y); return true; - } + } } if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, false, null, null))) { diff --git a/Wumpus/world.txt b/Wumpus/world.txt index d3e1cb3..477a4b0 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -H 0 0 I 0 -H S W H 0 -0 0 G 0 0 -I 0 I 0 0 0 0 0 0 0 +0 I 0 0 0 +0 W 0 0 0 +0 0 0 I G +0 0 0 0 S From 04d98f0c5c3894e49c54bc79cd32626a36f5f4f8 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 11:45:11 -0600 Subject: [PATCH 115/191] glory to me --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/Clause.java | 2 ++ Wumpus/src/Driver.java | 2 +- Wumpus/src/KnowledgeBase.java | 20 +++++++++++++++++--- Wumpus/world.txt | 8 ++++---- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index a407c63..50b2c83 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 4 4 -0 0 0 0 0 -0 6 0 0 0 -2 32 2 0 0 -0 2 0 4 8 -0 0 0 0 0 +5 2 2 +0 1 0 1 0 +1 16 9 16 1 +1 1 0 1 0 +16 1 0 4 0 +1 0 0 0 0 diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index aadd5f6..8266d31 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -24,6 +24,8 @@ public Clause(Fact fact) { public static void printClause(Clause clause){ for(Fact fact : clause.facts){ fact.printFact(); + if(fact != clause.facts.get(clause.facts.size()-1)) + System.out.print(" v "); } System.out.println(""); } diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 61cdcf1..bf6f94d 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + // makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 5f2ac6e..d1f9346 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -22,11 +22,11 @@ public void addToClauses(Clause clause){ } } if(clause.facts.size() == 1){ - inferenceEngine.infer(clause.facts.get(0)); System.out.println("Added to kb Clause: "); System.out.print("\t"); Clause.printClause(clause); clauses.add(clause); + inferenceEngine.infer(clause.facts.get(0)); } else{ int beforeSize = clauses.size(); @@ -70,6 +70,7 @@ public void initializeRules() { //(Stench(x,y) v !Wumpus(x-1,y)), (Stench(x,y) v !Wumpus(x+1,y)), (Stench(x,y) v !Wumpus(x,y-1)), (Stench(x,y) v !Wumpus(x,y+1) Clause stench1 = new Clause(); Fact stenchXY1 = new Fact(stenchXY); + stenchXY1.not = false; Fact wumpusxminy1 = new Fact(wumpusxminy); wumpusxminy1.not = true; stench1.facts.add(stenchXY1); @@ -78,6 +79,7 @@ public void initializeRules() { Clause stench2 = new Clause(); Fact stenchXY2 = new Fact(stenchXY); + stenchXY2.not = false; Fact wumpusxplusy1 = new Fact(wumpusxplusy); wumpusxplusy1.not = true; stench2.facts.add(stenchXY2); @@ -86,6 +88,7 @@ public void initializeRules() { Clause stench3 = new Clause(); Fact stenchXY3 = new Fact(stenchXY); + stenchXY3.not = false; Fact wumpusxymin1 = new Fact(wumpusxymin); wumpusxymin1.not = true; stench3.facts.add(stenchXY3); @@ -94,6 +97,7 @@ public void initializeRules() { Clause stench4 = new Clause(); Fact stenchXY4 = new Fact(stenchXY); + stenchXY4.not = false; Fact wumpusxyplus1 = new Fact(wumpusxyplus); wumpusxyplus1.not = true; stench4.facts.add(stenchXY4); @@ -117,6 +121,7 @@ public void initializeRules() { Clause breeze1 = new Clause(); Fact breezeXY1 = new Fact(breezeXY); + breezeXY1.not = false; Fact pitxminy1 = new Fact(pitxminy); pitxminy1.not = true; breeze1.facts.add(breezeXY1); @@ -125,6 +130,8 @@ public void initializeRules() { Clause breeze2 = new Clause(); Fact breezeXY2 = new Fact(breezeXY); + + breezeXY2.not = false; Fact pitxplusy1 = new Fact(pitxplusy); pitxplusy1.not = true; breeze2.facts.add(breezeXY2); @@ -133,6 +140,8 @@ public void initializeRules() { Clause breeze3 = new Clause(); Fact breezeXY3 = new Fact(breezeXY); + + breezeXY3.not = false; Fact pitxymin1 = new Fact(pitxymin); pitxymin1.not = true; breeze3.facts.add(breezeXY3); @@ -141,6 +150,7 @@ public void initializeRules() { Clause breeze4 = new Clause(); Fact breezeXY4 = new Fact(breezeXY); + breezeXY4.not = false; Fact pitxyplus1 = new Fact(pitxyplus); pitxyplus1.not = true; breeze4.facts.add(breezeXY4); @@ -201,9 +211,13 @@ private boolean factInClauses(Fact fact){ Fact clauseFact = clause.facts.get(0); if(clauseFact.predicate.equals(fact.predicate)){ for(int i = 0; i < fact.variables.size(); i++){ - if(clauseFact.variables.get(i).value != fact.variables.get(i).value){ - return true; + + if(clauseFact.variables.get(i).value == fact.variables.get(i).value){ + if(i == fact.variables.size()-1) + return true; } + else + break; } } } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 477a4b0..4ca78b3 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 0 0 0 0 0 -0 I 0 0 0 -0 W 0 0 0 -0 0 0 I G -0 0 0 0 S +0 H G H 0 +0 0 S 0 0 +H 0 0 I 0 +0 0 0 0 0 From dbd648aabd4ac55d8096f7227b7423971c4fecb4 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 11:56:15 -0600 Subject: [PATCH 116/191] another fix by Wilson --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/LogicExplorer.java | 8 ++++---- Wumpus/src/World.java | 4 ++-- Wumpus/world.txt | 10 +++++----- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 50b2c83..7614b84 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 2 2 -0 1 0 1 0 -1 16 9 16 1 -1 1 0 1 0 -16 1 0 4 0 -1 0 0 0 0 +5 3 3 +0 2 0 0 4 +2 32 2 0 0 +32 10 0 0 1 +2 0 0 1 16 +0 4 0 0 1 diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 2e1af4e..058bb1b 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -316,10 +316,10 @@ private void moveHistoryTraversal(Location loc) { } private void rhwTraversal(Location location) { - moveHistoryTraversal(location); -// if (!this.location.equals(location) && !adjacent(location)) { -// goTo(location.x, location.y); -// } + //moveHistoryTraversal(location); + if (!this.location.equals(location) && !adjacent(location)) { + goTo(location.x, location.y); + } //go to location zach NOOOO! } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index fccc832..521fe1a 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -107,7 +107,7 @@ public byte action(int action) { switch (direction) { case NORTH: System.out.println("Moved north"); - if (y + 1 < size-1 && (perceptMap[x][y+1] & BUMP)==0) { + if (y + 1 < size && (perceptMap[x][y+1] & BUMP)==0) { if ((perceptMap[x][y+1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; @@ -127,7 +127,7 @@ public byte action(int action) { } case EAST: System.out.println("Moved east"); - if(x+1 < size-1 && (perceptMap[x+1][y] & BUMP)==0){ + if(x+1 < size && (perceptMap[x+1][y] & BUMP)==0){ if ((perceptMap[x+1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 4ca78b3..3af43c1 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -0 0 0 0 0 -0 H G H 0 -0 0 S 0 0 -H 0 0 I 0 -0 0 0 0 0 +0 0 0 0 I +0 W 0 0 0 +W G 0 0 0 +0 0 0 S H +0 I 0 0 0 From d624e696d504179ff8428bfdff8a7f99d91cd38a Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 11:59:04 -0600 Subject: [PATCH 117/191] good fixes --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/Driver.java | 2 +- Wumpus/src/LogicExplorer.java | 8 ++++---- Wumpus/world.txt | 10 +++++----- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 7614b84..60c4815 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 3 3 -0 2 0 0 4 -2 32 2 0 0 -32 10 0 0 1 -2 0 0 1 16 -0 4 0 0 1 +5 1 4 +0 0 0 0 0 +0 1 0 0 2 +1 16 1 2 32 +0 1 4 0 2 +0 0 0 0 8 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index bf6f94d..61cdcf1 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 058bb1b..7299c2f 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -316,10 +316,10 @@ private void moveHistoryTraversal(Location loc) { } private void rhwTraversal(Location location) { - //moveHistoryTraversal(location); - if (!this.location.equals(location) && !adjacent(location)) { - goTo(location.x, location.y); - } + moveHistoryTraversal(location); + //if (!this.location.equals(location) && !adjacent(location)) { + // goTo(location.x, location.y); + //} //go to location zach NOOOO! } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 3af43c1..454280b 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -0 0 0 0 I -0 W 0 0 0 -W G 0 0 0 -0 0 0 S H -0 I 0 0 0 +0 0 0 0 0 +0 0 0 0 S +0 H 0 0 W +0 0 I 0 0 +0 0 0 0 G From 136089724aee04db79d35ad4e1d5e2f94f1fdfc2 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Fri, 21 Oct 2016 12:05:17 -0600 Subject: [PATCH 118/191] something --- Wumpus/src/Driver.java | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index fb72ffe..9f74374 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -18,19 +18,19 @@ public static void main(String[] args) throws IOException { public static void makeGame() throws IOException { - BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); - int[] prob = new int[3]; - System.out.print("% chance of generating pit: "); - prob[0] = Integer.parseInt(dataIn.readLine()); - System.out.println(); - System.out.print("% chance of generating obstacle: "); - prob[1] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - System.out.print("% chance of generating wumpus: "); - prob[2] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - - WumpusGame game = new WumpusGame(5, prob); +// BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); +// int[] prob = new int[3]; +// System.out.print("% chance of generating pit: "); +// prob[0] = Integer.parseInt(dataIn.readLine()); +// System.out.println(); +// System.out.print("% chance of generating obstacle: "); +// prob[1] = Integer.parseInt(dataIn.readLine()); +// System.out.println(""); +// System.out.print("% chance of generating wumpus: "); +// prob[2] = Integer.parseInt(dataIn.readLine()); +// System.out.println(""); +// +// WumpusGame game = new WumpusGame(5, prob); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); World world = new World("PerceptBoard.txt"); From 5e91724e4469db89dc690110d9844c57afcc660e Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 12:12:01 -0600 Subject: [PATCH 119/191] wuuuuu --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/LogicExplorer.java | 10 +++++++++- Wumpus/world.txt | 8 ++++---- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 60c4815..cf0eac8 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 1 4 -0 0 0 0 0 -0 1 0 0 2 -1 16 1 2 32 -0 1 4 0 2 -0 0 0 0 8 +5 2 1 +0 1 16 1 0 +2 0 1 0 10 +32 3 16 3 32 +3 16 1 0 2 +0 1 0 0 0 diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 7299c2f..2813021 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -156,6 +156,13 @@ else if((percepts & DEATH_PIT) == 0 && (percepts & DEATH_WUMPUS)==0){//you if yo }} } + private boolean inFrontier(Location loc){ + for(Location location : frontier){ + if(location.x == loc.x && location.y == loc.y) + return true; + } + return false; + } private void decideNextAction() { if (frontier.isEmpty()) { move(World.QUIT); @@ -166,10 +173,11 @@ private void decideNextAction() { if (getForward().x >= 0 && getForward().x < World.size && getForward().y >= 0 && getForward().y < World.size) { if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, true, null, null))) { if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { - if (kb.ask(new Fact("Obsticle", getForward().x, false, getForward().y, false, true, null, null))) { + if(inFrontier(getForward())){ move(World.MOVE); return; } + } } } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 454280b..935fc5f 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -0 0 0 0 0 -0 0 0 0 S -0 H 0 0 W -0 0 I 0 0 +0 0 H 0 0 0 0 0 0 G +W S H 0 W +0 H 0 0 0 +0 0 0 0 0 From 3104fbc1653214a00e3d9f308199fc306e537af1 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 12:48:21 -0600 Subject: [PATCH 120/191] fixes --- Wumpus/PerceptBoard.txt | 17 +++++++++++------ Wumpus/src/Driver.java | 7 +++++-- Wumpus/src/LogicExplorer.java | 10 +++++----- Wumpus/src/Unifier.java | 8 ++++++++ Wumpus/src/World.java | 19 +++++++++++++++---- Wumpus/world.txt | 17 +++++++++++------ 6 files changed, 55 insertions(+), 23 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index cf0eac8..2581f59 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,11 @@ -5 2 1 -0 1 16 1 0 -2 0 1 0 10 -32 3 16 3 32 -3 16 1 0 2 -0 1 0 0 0 +10 5 2 +4 0 4 6 34 34 2 0 0 0 +2 0 3 34 2 2 1 0 0 1 +34 7 19 35 2 1 16 1 1 16 +34 3 17 3 0 0 1 17 17 1 +2 0 1 0 0 5 16 1 1 0 +0 0 0 0 0 4 3 2 0 0 +0 0 0 0 0 2 34 34 2 0 +4 0 0 0 0 0 10 6 1 0 +4 1 0 0 0 0 0 1 16 1 +1 16 1 0 0 0 0 0 1 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 61cdcf1..e89bcc2 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + //makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } @@ -22,6 +22,9 @@ public static void makeGame() throws IOException { BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); int[] prob = new int[3]; + System.out.println("Size of board: "); + + int size = Integer.parseInt(dataIn.readLine()); System.out.print("% chance of generating pit: "); prob[0] = Integer.parseInt(dataIn.readLine()); System.out.println(); @@ -32,7 +35,7 @@ public static void makeGame() throws IOException { prob[2] = Integer.parseInt(dataIn.readLine()); System.out.println(""); - WumpusGame game = new WumpusGame(5, prob); + WumpusGame game = new WumpusGame(size, prob); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); // World world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 2813021..519dcfc 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -76,30 +76,30 @@ private void move(int action) { switch (action) { case GRAB: - System.out.println("Grabbing"); + System.out.println("Explorer Action:Grabbing"); world.action(GRAB); System.out.println("Game failed to end after action(GRAB)."); break; case MOVE: moveHistory.add(MOVE); - System.out.println("Moving"); + System.out.println("Explorer Action: Moving"); percepts = (byte) world.action(MOVE); processPercepts(); break; case TURN_LEFT: moveHistory.add(TURN_LEFT); - System.out.println("Turning left"); + System.out.println("Explorer Action: Turning left"); direction = (direction + 3) % 4; world.action(TURN_LEFT); break; case TURN_RIGHT: moveHistory.add(TURN_RIGHT); - System.out.println("turning right"); + System.out.println("Explorer Action: turning right"); direction = (direction + 1) % 4; world.action(TURN_RIGHT); break; case SHOOT: - System.out.println("shooting"); + System.out.println("Explorer Action: shooting"); arrowCount--; percepts = (byte) world.action(SHOOT); processPercepts(); diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index de1936f..974e5fc 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -82,6 +82,10 @@ public static ArrayList unify(Fact f1, Fact f2){ Substitute sub = new Substitute(); sub.varIdToSubstitute = tempVar.variableId; sub.valToSubstituteWith = fact2.variables.get(i).value; + if(tempVar.function != null){ + int val = tempVar.function.process(0); + sub.valToSubstituteWith += -1*val; + } subs.add(sub); } } @@ -93,6 +97,10 @@ public static ArrayList unify(Fact f1, Fact f2){ Substitute sub = new Substitute(); sub.varIdToSubstitute = tempVar.variableId; sub.valToSubstituteWith = fact1.variables.get(i).value; + if(tempVar.function != null){ + int val = tempVar.function.process(0); + sub.valToSubstituteWith += -1*val; + } subs.add(sub); } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 521fe1a..dc0a82b 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -12,7 +12,7 @@ public final class World { private int arrowCount, x, y, direction = 0, score = 0, numMoves = 0, pitDeaths = 0, wumpusDeaths = 0; public static int size; private byte[][] perceptMap; - + public World(String fileName) { importMap(fileName); } @@ -65,6 +65,13 @@ public byte getPercepts() { return perceptMap[x][y]; } + public void printStats(){ + System.out.println("Number of Actions: "+numMoves); + System.out.println("Final score: "+score); + System.out.println("Wumpus Deaths: "+wumpusDeaths); + System.out.println("Pit Deaths: "+ pitDeaths); + } + public void printWorld() { for (int i = perceptMap.length-1; i >= 0; i--) { @@ -84,9 +91,12 @@ public void printWorld() { } System.out.println(""); } - - public byte action(int action) { + public byte action(int action){ + byte percepts = action(action, true); printWorld(); + return percepts; + } + public byte action(int action, boolean thing) { System.out.println("Action: " + action); System.out.println(""); numMoves++; @@ -96,7 +106,8 @@ public byte action(int action) { if ((perceptMap[x][y] & GLITTER) != 0) { perceptMap[x][y] -= GLITTER; score += 1000; - System.out.println("Gold found!\nScore: " + score); + System.out.println("Gold found!\n"); + printStats(); System.exit(0); } else { System.out.println("gold not found error"); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 935fc5f..5dfd8ec 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,11 @@ -5 -0 0 H 0 0 -0 0 0 0 G -W S H 0 W -0 H 0 0 0 -0 0 0 0 0 +10 +I 0 I I W W 0 0 0 0 +0 0 0 W 0 0 0 0 0 0 +W I H W 0 0 H 0 0 H +W 0 H 0 0 0 0 H H 0 +0 0 0 0 0 I H 0 0 0 +0 0 S 0 0 I 0 0 0 0 +0 0 0 0 0 0 W W 0 0 +I 0 0 0 0 0 G I 0 0 +I 0 0 0 0 0 0 0 H 0 +0 H 0 0 0 0 0 0 0 0 From 291632bfe5c8dc2745581b833609e5a3c285a865 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Fri, 21 Oct 2016 12:48:38 -0600 Subject: [PATCH 121/191] Reactive explorer can grab gold --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/src/Driver.java | 2 +- Wumpus/src/ReactiveExplorer.java | 8 ++++++-- Wumpus/src/World.java | 1 + Wumpus/world.txt | 10 +++++----- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 60c4815..6921829 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 1 4 -0 0 0 0 0 -0 1 0 0 2 -1 16 1 2 32 -0 1 4 0 2 -0 0 0 0 8 +5 2 1 +0 0 1 18 33 +0 8 0 1 18 +0 0 2 2 33 +0 2 32 2 2 +0 0 2 2 32 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 260832e..9830a0b 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -15,7 +15,7 @@ public static void main(String[] args) throws IOException { // tester.testInferenceEngine(); makeGame(); World world = new World("PerceptBoard.txt"); - world.startGame("LogicExplorer"); + world.startGame("ReactiveExplorer"); } public static void makeGame() throws IOException { diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index dd0c7bb..40bd7cc 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -4,7 +4,8 @@ public class ReactiveExplorer extends Agent { private Location prevLocation; - private State curState, prevState; private boolean safeMap[][]; + private State curState, prevState; + private boolean safeMap[][]; private static final int FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3; public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { @@ -31,6 +32,9 @@ private void run() { private void move() { if ((percepts & STENCH) != STENCH && (percepts & BREEZE) != BREEZE) { //all adjacent spaces are safe + if((percepts & GLITTER) != GLITTER){ + world.action(GRAB); + } updateSafe(); //go in random direction int rand = random.nextInt(3); @@ -45,7 +49,7 @@ private void move() { turnLeft(); percepts = world.action(MOVE); if ((percepts & BUMP) != BUMP) { - System.out.println("Here"); + System.out.println("Not Reseting"); return; } turnRight(); diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 521fe1a..bf32a75 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -194,6 +194,7 @@ public byte action(int action) { direction = (direction + 3) % 4; return perceptMap[x][y]; case TURN_RIGHT: + System.out.println("Turning right"); direction = (direction + 1) % 4; return perceptMap[x][y]; case SHOOT: diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 454280b..1fc1c9b 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -0 0 0 0 0 -0 0 0 0 S -0 H 0 0 W -0 0 I 0 0 -0 0 0 0 G +0 0 0 H W +0 G 0 0 H +0 S 0 0 W +0 0 W 0 0 +0 0 0 0 W From 50aa1159d8d59e377bbb17f05480804ec5d84290 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 12:59:54 -0600 Subject: [PATCH 122/191] thing --- Wumpus/PerceptBoard.txt | 22 +++++++++++----------- Wumpus/src/Driver.java | 2 +- Wumpus/src/InferenceEngine.java | 4 ++-- Wumpus/world.txt | 20 ++++++++++---------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 2581f59..f1e6077 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,11 +1,11 @@ -10 5 2 -4 0 4 6 34 34 2 0 0 0 -2 0 3 34 2 2 1 0 0 1 -34 7 19 35 2 1 16 1 1 16 -34 3 17 3 0 0 1 17 17 1 -2 0 1 0 0 5 16 1 1 0 -0 0 0 0 0 4 3 2 0 0 -0 0 0 0 0 2 34 34 2 0 -4 0 0 0 0 0 10 6 1 0 -4 1 0 0 0 0 0 1 16 1 -1 16 1 0 0 0 0 0 1 0 +10 1 4 +1 16 1 0 0 4 1 16 1 2 +0 1 0 8 2 5 0 1 2 32 +0 0 2 3 33 18 1 0 0 2 +0 2 33 18 3 1 1 0 0 0 +0 1 2 3 1 17 17 1 0 0 +1 16 3 32 2 1 1 0 1 2 +0 3 0 2 0 0 2 5 18 33 +2 32 2 0 0 2 32 6 1 2 +2 2 0 0 1 0 3 0 0 0 +32 2 0 1 16 1 16 5 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index e89bcc2..6f645d8 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - //makeGame(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 6aa43e6..2b4c1ed 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -31,8 +31,8 @@ public boolean follows(Fact fact) { //extend clause with everything in kbClause, remove kbFact and followFact, start over boolean skipOut = false; for(int i = 0; i < kbFact.variables.size(); i++){ - Variable var1 = kbFact.variables.get(0); - Variable var2 = followFact.variables.get(0); + Variable var1 = kbFact.variables.get(i); + Variable var2 = followFact.variables.get(i); if(var1.value != var2.value){ skipOut = true; } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 5dfd8ec..a3e8f2d 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,11 +1,11 @@ 10 -I 0 I I W W 0 0 0 0 -0 0 0 W 0 0 0 0 0 0 -W I H W 0 0 H 0 0 H -W 0 H 0 0 0 0 H H 0 -0 0 0 0 0 I H 0 0 0 -0 0 S 0 0 I 0 0 0 0 -0 0 0 0 0 0 W W 0 0 -I 0 0 0 0 0 G I 0 0 -I 0 0 0 0 0 0 0 H 0 -0 H 0 0 0 0 0 0 0 0 +0 H 0 0 0 I 0 H 0 0 +0 0 0 G S I 0 0 0 W +0 0 0 0 W H 0 0 0 0 +0 0 W H 0 0 0 0 0 0 +0 0 0 0 0 H H 0 0 0 +0 H 0 W 0 0 0 0 0 0 +0 0 0 0 0 0 0 I H W +0 W 0 0 0 0 W I 0 0 +0 0 0 0 0 0 0 0 0 0 +W 0 0 0 H 0 H I 0 0 From 62ca6884372649e39c2800523abf2ad95424b45a Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Fri, 21 Oct 2016 13:02:03 -0600 Subject: [PATCH 123/191] fk u git --- Wumpus/PerceptBoard.txt | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index f1e6077..6257dc1 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,11 +1,6 @@ -10 1 4 -1 16 1 0 0 4 1 16 1 2 -0 1 0 8 2 5 0 1 2 32 -0 0 2 3 33 18 1 0 0 2 -0 2 33 18 3 1 1 0 0 0 -0 1 2 3 1 17 17 1 0 0 -1 16 3 32 2 1 1 0 1 2 -0 3 0 2 0 0 2 5 18 33 -2 32 2 0 0 2 32 6 1 2 -2 2 0 0 1 0 3 0 0 0 -32 2 0 1 16 1 16 5 0 0 +5 1 1 +17 1 2 5 0 +17 3 33 18 1 +1 0 10 1 0 +4 0 4 0 0 +0 0 0 0 0 From ce3374f6f8ea89d814ca1b98864eed850b30bc4a Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Fri, 21 Oct 2016 13:07:24 -0600 Subject: [PATCH 124/191] neater printing --- Wumpus/src/World.java | 134 +++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 68 deletions(-) diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index dc0a82b..c7a5550 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -12,7 +12,7 @@ public final class World { private int arrowCount, x, y, direction = 0, score = 0, numMoves = 0, pitDeaths = 0, wumpusDeaths = 0; public static int size; private byte[][] perceptMap; - + public World(String fileName) { importMap(fileName); } @@ -34,7 +34,7 @@ public void importMap(String fileName) { String next = reader.readLine(); size = Integer.parseInt(next.substring(0, next.indexOf(" "))); next = next.substring(next.indexOf(" ") + 1); - x = Integer.parseInt(next.substring(0,next.indexOf(" "))); + x = Integer.parseInt(next.substring(0, next.indexOf(" "))); next = next.substring(next.indexOf(" ") + 1); y = Integer.parseInt(next.substring(0, next.indexOf(" "))); perceptMap = new byte[size][size]; @@ -49,13 +49,6 @@ public void importMap(String fileName) { } i++; } -// System.out.println(""); -// for (byte[] row : perceptMap) { -// for (int j = 0; j < row.length; j++) { -// System.out.print(row[j] + " "); -// } -// System.out.println(""); -// } } catch (IOException | NumberFormatException e) { System.out.println("Exception caught: " + e); } @@ -65,25 +58,28 @@ public byte getPercepts() { return perceptMap[x][y]; } - public void printStats(){ - System.out.println("Number of Actions: "+numMoves); - System.out.println("Final score: "+score); - System.out.println("Wumpus Deaths: "+wumpusDeaths); - System.out.println("Pit Deaths: "+ pitDeaths); + public void printStats() { + System.out.println("Number of Actions: " + numMoves); + System.out.println("Final score: " + score); + System.out.println("Wumpus Deaths: " + wumpusDeaths); + System.out.println("Pit Deaths: " + pitDeaths); } - + public void printWorld() { - for (int i = perceptMap.length-1; i >= 0; i--) { + for (int i = perceptMap.length - 1; i >= 0; i--) { for (int j = 0; j < perceptMap.length; j++) { if (x == j && y == i) { - System.out.print("A "); + System.out.print("A "); } else { - if ((perceptMap[j][i] & DEATH_WUMPUS) != 0) { - System.out.print("W "); - } - else { + if ((perceptMap[j][i] & DEATH_WUMPUS) == DEATH_WUMPUS) { + System.out.print("W "); + } else if ((perceptMap[j][i] & DEATH_PIT) == DEATH_PIT) { + System.out.print("P "); + } else if (perceptMap[j][i] > 9) { System.out.print(perceptMap[j][i] + " "); + } else { + System.out.print(perceptMap[j][i] + " "); } } } @@ -91,11 +87,13 @@ public void printWorld() { } System.out.println(""); } - public byte action(int action){ + + public byte action(int action) { byte percepts = action(action, true); printWorld(); return percepts; } + public byte action(int action, boolean thing) { System.out.println("Action: " + action); System.out.println(""); @@ -118,14 +116,14 @@ public byte action(int action, boolean thing) { switch (direction) { case NORTH: System.out.println("Moved north"); - if (y + 1 < size && (perceptMap[x][y+1] & BUMP)==0) { - if ((perceptMap[x][y+1] & DEATH_WUMPUS) == DEATH_WUMPUS) { + if (y + 1 < size && (perceptMap[x][y + 1] & BUMP) == 0) { + if ((perceptMap[x][y + 1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x][y+1] & DEATH_PIT) == DEATH_PIT) { + if ((perceptMap[x][y + 1] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); @@ -135,64 +133,64 @@ public byte action(int action, boolean thing) { return perceptMap[x][y]; } else { return BUMP; - } + } case EAST: System.out.println("Moved east"); - if(x+1 < size && (perceptMap[x+1][y] & BUMP)==0){ - if ((perceptMap[x+1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + if (x + 1 < size && (perceptMap[x + 1][y] & BUMP) == 0) { + if ((perceptMap[x + 1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x+1][y] & DEATH_PIT) == DEATH_PIT) { + if ((perceptMap[x + 1][y] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; return DEATH_PIT; - } - x = x + 1; + } + x = x + 1; return perceptMap[x][y]; - }else { + } else { return BUMP; } case SOUTH: System.out.println("Moved south"); - if(y > 0 && (perceptMap[x][y-1] & BUMP)==0){ - if ((perceptMap[x][y-1] & DEATH_WUMPUS) == DEATH_WUMPUS) { - score -= 1000; - wumpusDeaths++; - System.out.println("Death to wumpus."); - return DEATH_WUMPUS; - } - if ((perceptMap[x][y-1] & DEATH_PIT) == DEATH_PIT) { - score -= 1000; - pitDeaths++; - System.out.println("Death to pit."); - return DEATH_PIT; - } + if (y > 0 && (perceptMap[x][y - 1] & BUMP) == 0) { + if ((perceptMap[x][y - 1] & DEATH_WUMPUS) == DEATH_WUMPUS) { + score -= 1000; + wumpusDeaths++; + System.out.println("Death to wumpus."); + return DEATH_WUMPUS; + } + if ((perceptMap[x][y - 1] & DEATH_PIT) == DEATH_PIT) { + score -= 1000; + pitDeaths++; + System.out.println("Death to pit."); + return DEATH_PIT; + } y -= 1; return perceptMap[x][y]; - }else { + } else { return BUMP; } case WEST: System.out.println("Moved west"); - if(x>0 && (perceptMap[x-1][y] & BUMP)==0){ - if ((perceptMap[x-1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { - score -= 1000; - wumpusDeaths++; - System.out.println("Death to wumpus."); - return DEATH_WUMPUS; - } - if ((perceptMap[x-1][y] & DEATH_PIT) == DEATH_PIT) { - score -= 1000; - pitDeaths++; - System.out.println("Death to pit."); - return DEATH_PIT; - } + if (x > 0 && (perceptMap[x - 1][y] & BUMP) == 0) { + if ((perceptMap[x - 1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + score -= 1000; + wumpusDeaths++; + System.out.println("Death to wumpus."); + return DEATH_WUMPUS; + } + if ((perceptMap[x - 1][y] & DEATH_PIT) == DEATH_PIT) { + score -= 1000; + pitDeaths++; + System.out.println("Death to pit."); + return DEATH_PIT; + } x -= 1; return perceptMap[x][y]; - }else { + } else { return BUMP; } default: @@ -216,10 +214,10 @@ public byte action(int action, boolean thing) { switch (direction) { case 1: //shoot north for (int i = y; i < perceptMap.length; i++) { - if ((perceptMap[x][i] & DEATH_WUMPUS)!= 0) { //hits Wumpus + if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); return SCREAM; - } else if ((perceptMap[x][i] & BUMP)!= 0) { //hits Obstacle + } else if ((perceptMap[x][i] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -227,10 +225,10 @@ public byte action(int action, boolean thing) { case 2: //shoot east for (int i = y; i < perceptMap.length; i++) { - if ((perceptMap[i][y] & DEATH_WUMPUS)!= 0) { //hits Wumpus + if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; - } else if ((perceptMap[i][y] & BUMP)!=0) { //hits Obstacle + } else if ((perceptMap[i][y] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -238,10 +236,10 @@ public byte action(int action, boolean thing) { case 3: //shoot south for (int i = y; i > 0; i--) { - if ((perceptMap[x][i] & DEATH_WUMPUS)!=0) { //hits Wumpus + if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); return SCREAM; - } else if ((perceptMap[x][i] & BUMP)!=0) { //hits Obstacle + } else if ((perceptMap[x][i] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -249,10 +247,10 @@ public byte action(int action, boolean thing) { case 4: //shoot west for (int i = y; i > 0; i--) { - if ((perceptMap[i][y] & DEATH_WUMPUS)!=0) { //hits Wumpus + if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; - } else if ((perceptMap[i][y] &BUMP)!=0) { //hits Obstacle + } else if ((perceptMap[i][y] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } From c4ae140481027f164c335015a6a4a4531cf4fb90 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 13:07:55 -0600 Subject: [PATCH 125/191] fix --- Wumpus/PerceptBoard.txt | 17 ++++++----------- Wumpus/src/World.java | 9 ++++++++- Wumpus/world.txt | 17 ++++++----------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index f1e6077..98d06c7 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,11 +1,6 @@ -10 1 4 -1 16 1 0 0 4 1 16 1 2 -0 1 0 8 2 5 0 1 2 32 -0 0 2 3 33 18 1 0 0 2 -0 2 33 18 3 1 1 0 0 0 -0 1 2 3 1 17 17 1 0 0 -1 16 3 32 2 1 1 0 1 2 -0 3 0 2 0 0 2 5 18 33 -2 32 2 0 0 2 32 6 1 2 -2 2 0 0 1 0 3 0 0 0 -32 2 0 1 16 1 16 5 0 0 +5 3 3 +0 0 0 0 0 +0 0 0 0 0 +0 0 0 0 0 +8 0 4 1 0 +0 0 1 16 1 diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index dc0a82b..1bdbe79 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -9,7 +9,7 @@ public final class World { public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH_PIT = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; - private int arrowCount, x, y, direction = 0, score = 0, numMoves = 0, pitDeaths = 0, wumpusDeaths = 0; + private int arrowCount, x, y, direction = 0, score = 0, numMoves = 0, pitDeaths = 0, wumpusDeaths = 0, killedWumpus = 0; public static int size; private byte[][] perceptMap; @@ -70,6 +70,8 @@ public void printStats(){ System.out.println("Final score: "+score); System.out.println("Wumpus Deaths: "+wumpusDeaths); System.out.println("Pit Deaths: "+ pitDeaths); + System.out.println("Killed Wumpus's: "+killedWumpus); + System.out.println("Leftover arrows: "+arrowCount); } public void printWorld() { @@ -202,9 +204,11 @@ public byte action(int action, boolean thing) { } break; case TURN_LEFT: + score--; direction = (direction + 3) % 4; return perceptMap[x][y]; case TURN_RIGHT: + score--; direction = (direction + 1) % 4; return perceptMap[x][y]; case SHOOT: @@ -213,11 +217,14 @@ public byte action(int action, boolean thing) { return -1; //out of arrows, which shouldn't be possible } arrowCount--; + score-=10; switch (direction) { case 1: //shoot north for (int i = y; i < perceptMap.length; i++) { if ((perceptMap[x][i] & DEATH_WUMPUS)!= 0) { //hits Wumpus removeWumpus(x, i); + score+=10; + killedWumpus++; return SCREAM; } else if ((perceptMap[x][i] & BUMP)!= 0) { //hits Obstacle return perceptMap[x][y]; diff --git a/Wumpus/world.txt b/Wumpus/world.txt index a3e8f2d..33a2c8c 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,11 +1,6 @@ -10 -0 H 0 0 0 I 0 H 0 0 -0 0 0 G S I 0 0 0 W -0 0 0 0 W H 0 0 0 0 -0 0 W H 0 0 0 0 0 0 -0 0 0 0 0 H H 0 0 0 -0 H 0 W 0 0 0 0 0 0 -0 0 0 0 0 0 0 I H W -0 W 0 0 0 0 W I 0 0 -0 0 0 0 0 0 0 0 0 0 -W 0 0 0 H 0 H I 0 0 +5 +0 0 0 0 0 +0 0 0 0 0 +0 0 0 0 0 +G 0 I S 0 +0 0 0 H 0 From cf79ef091089c1e74dfc085db53238e22ed723c2 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Fri, 21 Oct 2016 13:19:31 -0600 Subject: [PATCH 126/191] output now much easier to read. --- Wumpus/src/World.java | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 1bdbe79..d51874a 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -49,13 +49,6 @@ public void importMap(String fileName) { } i++; } -// System.out.println(""); -// for (byte[] row : perceptMap) { -// for (int j = 0; j < row.length; j++) { -// System.out.print(row[j] + " "); -// } -// System.out.println(""); -// } } catch (IOException | NumberFormatException e) { System.out.println("Exception caught: " + e); } @@ -79,13 +72,18 @@ public void printWorld() { for (int i = perceptMap.length-1; i >= 0; i--) { for (int j = 0; j < perceptMap.length; j++) { if (x == j && y == i) { - System.out.print("A "); + System.out.print("A "); } else { - if ((perceptMap[j][i] & DEATH_WUMPUS) != 0) { - System.out.print("W "); - } - else { + if ((perceptMap[j][i] & DEATH_WUMPUS) == DEATH_WUMPUS) { + System.out.print("W "); + } else if ((perceptMap[j][i] & DEATH_PIT) == DEATH_PIT) { + System.out.print("P "); + } else if ((perceptMap[j][i] & GLITTER) == GLITTER) { + System.out.print("G "); + } else if (perceptMap[j][i] > 9) { System.out.print(perceptMap[j][i] + " "); + } else { + System.out.print(perceptMap[j][i] + " "); } } } From 9c8ca99385064616a125c031436e11c9415fa505 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 13:26:44 -0600 Subject: [PATCH 127/191] mychanges --- Wumpus/PerceptBoard.txt | 17 +++++++++++------ Wumpus/src/LogicExplorer.java | 7 ++++--- Wumpus/src/World.java | 7 +++++++ Wumpus/world.txt | 17 +++++++++++------ 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 98d06c7..21bd78b 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,11 @@ -5 3 3 -0 0 0 0 0 -0 0 0 0 0 -0 0 0 0 0 -8 0 4 1 0 -0 0 1 16 1 +10 0 5 +1 17 1 1 0 0 4 0 6 0 +1 17 1 16 1 0 2 2 32 2 +0 1 0 5 0 2 32 2 2 2 +4 2 0 4 1 1 2 34 35 34 +2 32 2 1 17 17 1 3 18 3 +0 6 4 1 1 17 3 32 7 0 +2 0 1 17 1 1 16 3 0 0 +32 6 9 17 1 16 1 1 0 0 +3 1 0 1 0 5 1 18 1 0 +17 17 1 16 1 0 2 33 2 0 diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 519dcfc..e5d3b6d 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -164,12 +164,13 @@ private boolean inFrontier(Location loc){ return false; } private void decideNextAction() { - if (frontier.isEmpty()) { - move(World.QUIT); - } if ((percepts & GLITTER) != 0) { move(World.GRAB); } + + if (frontier.isEmpty()) { + move(World.QUIT); + } if (getForward().x >= 0 && getForward().x < World.size && getForward().y >= 0 && getForward().y < World.size) { if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, true, null, null))) { if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index d51874a..fce58fe 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -15,6 +15,13 @@ public final class World { public World(String fileName) { importMap(fileName); + for (int i = 0; i < perceptMap.length; i++) { + for (int j = 0; j < perceptMap.length; j++) { + if((perceptMap[i][j] & DEATH_WUMPUS) != 0) + arrowCount++; + } + + } } public void startGame(String id) { diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 33a2c8c..f41523a 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,11 @@ -5 -0 0 0 0 0 -0 0 0 0 0 -0 0 0 0 0 -G 0 I S 0 -0 0 0 H 0 +10 +0 H 0 0 0 S I 0 I 0 +0 H 0 H 0 0 0 0 W 0 +0 0 0 I 0 0 W 0 0 0 +I 0 0 I 0 0 0 W W W +0 W 0 0 H H 0 0 H 0 +0 I I 0 0 H 0 W I 0 +0 0 0 H 0 0 H 0 0 0 +W I G H 0 H 0 0 0 0 +0 0 0 0 0 I 0 H 0 0 +H H 0 H 0 0 0 W 0 0 From 74691fc7c31e54199158f785615481b8b542ef41 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Fri, 21 Oct 2016 13:40:26 -0600 Subject: [PATCH 128/191] Can now randomly place start and gold on pre-existing map --- Wumpus/PerceptBoard.txt | 26 +++-------- Wumpus/clean.txt | 6 +++ Wumpus/src/Driver.java | 49 ++++++++++---------- Wumpus/src/ReactiveExplorer.java | 2 +- Wumpus/src/World.java | 4 ++ Wumpus/src/WumpusGame.java | 77 +++++++++++++++++++++++++++----- Wumpus/world.txt | 22 ++------- 7 files changed, 113 insertions(+), 73 deletions(-) create mode 100644 Wumpus/clean.txt diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 1aaeaf6..5836c97 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,20 +1,6 @@ -<<<<<<< HEAD -5 2 1 -0 0 1 18 33 -0 8 0 1 18 -0 0 2 2 33 -0 2 32 2 2 -0 0 2 2 32 -======= -10 5 2 -4 0 4 6 34 34 2 0 0 0 -2 0 3 34 2 2 1 0 0 1 -34 7 19 35 2 1 16 1 1 16 -34 3 17 3 0 0 1 17 17 1 -2 0 1 0 0 5 16 1 1 0 -0 0 0 0 0 4 3 2 0 0 -0 0 0 0 0 2 34 34 2 0 -4 0 0 0 0 0 10 6 1 0 -4 1 0 0 0 0 0 1 16 1 -1 16 1 0 0 0 0 0 1 0 ->>>>>>> 3104fbc1653214a00e3d9f308199fc306e537af1 +5 3 1 +0 2 32 2 0 +0 0 2 0 0 +0 0 0 2 8 +0 0 2 32 2 +0 4 0 2 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt new file mode 100644 index 0000000..8bc2909 --- /dev/null +++ b/Wumpus/clean.txt @@ -0,0 +1,6 @@ +5 +0 0 W 0 0 +0 0 0 0 0 +0 0 0 0 0 +0 0 0 W 0 +0 I 0 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 1ad6141..6222456 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -12,39 +12,40 @@ public static void main(String[] args) throws IOException { //Tester tester = new Tester(); //tester.testPathFinder(); //tester.testUnify(); - // tester.testInferenceEngine(); - //makeGame(); - World world = new World("PerceptBoard.txt"); - world.startGame("ReactiveExplorer"); + // tester.testInferenceEngine(); + makeGame(); } public static void makeGame() throws IOException { - BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); - int[] prob = new int[3]; - System.out.println("Size of board: "); - - int size = Integer.parseInt(dataIn.readLine()); - System.out.print("% chance of generating pit: "); - prob[0] = Integer.parseInt(dataIn.readLine()); - System.out.println(); - System.out.print("% chance of generating obstacle: "); - prob[1] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - System.out.print("% chance of generating wumpus: "); - prob[2] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - - WumpusGame game = new WumpusGame(size, prob); +// BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); +// int[] prob = new int[3]; +// System.out.println("Size of board: "); +// +// int size = Integer.parseInt(dataIn.readLine()); +// System.out.print("% chance of generating pit: "); +// prob[0] = Integer.parseInt(dataIn.readLine()); +// System.out.println(); +// System.out.print("% chance of generating obstacle: "); +// prob[1] = Integer.parseInt(dataIn.readLine()); +// System.out.println(""); +// System.out.print("% chance of generating wumpus: "); +// prob[2] = Integer.parseInt(dataIn.readLine()); +// System.out.println(""); + + WumpusGame game = new WumpusGame("clean.txt"); + +// game = new WumpusGame(size, prob, true); + World world = new World("PerceptBoard.txt"); + world.startGame("LogicExplorer"); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); - // World world = new World("PerceptBoard.txt"); + // World world = new World("PerceptBoard.txt"); //world.startGame("LogicExplorer"); - // world.startGame("ReactiveExplorer"); + // world.startGame("ReactiveExplorer"); //Agent explorer = new LogicExplorer(world); - //world = new World("PerceptBoard.txt"); - // Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction, world.getPercepts(), world.arrowCount); + // Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction, world.getPercepts(), world.arrowCount); } } diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 40bd7cc..5f25349 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -23,7 +23,7 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { private void run() { int i = 0; - while (i < 10) { + while (i < 500) { move(); i++; } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 235fbe8..0f1c0c1 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -29,6 +29,10 @@ public void startGame(String id) { public void importMap(String fileName) { try { + + FileReader in1 = new FileReader(fileName); + BufferedReader reader1 = new BufferedReader(in1); + FileReader in = new FileReader(fileName); BufferedReader reader = new BufferedReader(in); String next = reader.readLine(); diff --git a/Wumpus/src/WumpusGame.java b/Wumpus/src/WumpusGame.java index 1fb1572..ae71c1c 100644 --- a/Wumpus/src/WumpusGame.java +++ b/Wumpus/src/WumpusGame.java @@ -1,8 +1,11 @@ +import java.io.BufferedReader; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.util.HashMap; @@ -19,17 +22,34 @@ public class WumpusGame { protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; private PrintWriter out = new PrintWriter(new File("PerceptBoard.txt")); - public WumpusGame(int boardSize, int[] prob) throws FileNotFoundException { + public WumpusGame(String fileName) throws FileNotFoundException { + newStart(fileName); + System.setOut(new PrintStream(new FileOutputStream("world.txt"))); + printBoards(); + System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); + } + + public WumpusGame(int boardSize, int[] prob, boolean newOrNo) throws FileNotFoundException { this.boardSize = boardSize; this.prob = prob; perceptBoard = new byte[boardSize][boardSize]; board = new Space[boardSize][boardSize]; setBoard(); - initializeBoard(); - PrintStream out = new PrintStream(new FileOutputStream("world.txt")); - System.setOut(out); - printBoards(); - System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); + if (!newOrNo) { + initializeBoard(); + PrintStream out = new PrintStream(new FileOutputStream("world.txt")); + System.setOut(out); + printBoards(); + System.setOut(new PrintStream(new FileOutputStream("clean.txt"))); + printBoards(); + System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); + } else { + newStart("clean.txt"); + System.setOut(new PrintStream(new FileOutputStream("world.txt"))); + printBoards(); + System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); + } + } public void setBoard() { @@ -40,12 +60,40 @@ public void setBoard() { } } - public void initializeBoard() { - for (int i = board.length - 1; i >= 0; i--) { - for (int j = 0; j = 0; i--) { + for (int j = 0; j < board[i].length; j++) { + chooseState(i, j); + } + } } public void chooseState(int x, int y) { diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 9768e0d..7781f96 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,20 +1,6 @@ -<<<<<<< HEAD 5 -0 0 0 H W -0 G 0 0 H -0 S 0 0 W 0 0 W 0 0 -0 0 0 0 W -======= -10 -I 0 I I W W 0 0 0 0 -0 0 0 W 0 0 0 0 0 0 -W I H W 0 0 H 0 0 H -W 0 H 0 0 0 0 H H 0 -0 0 0 0 0 I H 0 0 0 -0 0 S 0 0 I 0 0 0 0 -0 0 0 0 0 0 W W 0 0 -I 0 0 0 0 0 G I 0 0 -I 0 0 0 0 0 0 0 H 0 -0 H 0 0 0 0 0 0 0 0 ->>>>>>> 3104fbc1653214a00e3d9f308199fc306e537af1 +0 0 0 0 0 +0 0 0 0 G +0 S 0 W 0 +0 I 0 0 0 From 54c7c099db6902f18571aa6b1bb423c6cc15da08 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Fri, 21 Oct 2016 13:42:19 -0600 Subject: [PATCH 129/191] fk your spaceing --- Wumpus/src/World.java | 138 ++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 65 deletions(-) diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index d51874a..17d7325 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -12,17 +12,20 @@ public final class World { private int arrowCount, x, y, direction = 0, score = 0, numMoves = 0, pitDeaths = 0, wumpusDeaths = 0, killedWumpus = 0; public static int size; private byte[][] perceptMap; - + public World(String fileName) { importMap(fileName); } public void startGame(String id) { Agent explorer; - if (id.equals("LogicExplorer")) { - explorer = new LogicExplorer(this, arrowCount, x, y, direction); - } else if (id.equals("ReactiveExplorer")) { - explorer = new ReactiveExplorer(this, arrowCount, x, y, direction); + switch (id) { + case "LogicExplorer": + explorer = new LogicExplorer(this, arrowCount, x, y, direction); + break; + case "ReactiveExplorer": + explorer = new ReactiveExplorer(this, arrowCount, x, y, direction); + break; } } @@ -34,7 +37,7 @@ public void importMap(String fileName) { String next = reader.readLine(); size = Integer.parseInt(next.substring(0, next.indexOf(" "))); next = next.substring(next.indexOf(" ") + 1); - x = Integer.parseInt(next.substring(0,next.indexOf(" "))); + x = Integer.parseInt(next.substring(0, next.indexOf(" "))); next = next.substring(next.indexOf(" ") + 1); y = Integer.parseInt(next.substring(0, next.indexOf(" "))); perceptMap = new byte[size][size]; @@ -58,18 +61,18 @@ public byte getPercepts() { return perceptMap[x][y]; } - public void printStats(){ - System.out.println("Number of Actions: "+numMoves); - System.out.println("Final score: "+score); - System.out.println("Wumpus Deaths: "+wumpusDeaths); - System.out.println("Pit Deaths: "+ pitDeaths); - System.out.println("Killed Wumpus's: "+killedWumpus); - System.out.println("Leftover arrows: "+arrowCount); + public void printStats() { + System.out.println("Number of Actions: " + numMoves); + System.out.println("Final score: " + score); + System.out.println("Wumpus Deaths: " + wumpusDeaths); + System.out.println("Pit Deaths: " + pitDeaths); + System.out.println("Killed Wumpus's: " + killedWumpus); + System.out.println("Leftover arrows: " + arrowCount); } - + public void printWorld() { - for (int i = perceptMap.length-1; i >= 0; i--) { + for (int i = perceptMap.length - 1; i >= 0; i--) { for (int j = 0; j < perceptMap.length; j++) { if (x == j && y == i) { System.out.print("A "); @@ -80,6 +83,8 @@ public void printWorld() { System.out.print("P "); } else if ((perceptMap[j][i] & GLITTER) == GLITTER) { System.out.print("G "); + } else if ((perceptMap[j][i] & BUMP) == BUMP) { + System.out.print("B "); } else if (perceptMap[j][i] > 9) { System.out.print(perceptMap[j][i] + " "); } else { @@ -91,11 +96,13 @@ public void printWorld() { } System.out.println(""); } - public byte action(int action){ + + public byte action(int action) { byte percepts = action(action, true); printWorld(); return percepts; } + public byte action(int action, boolean thing) { System.out.println("Action: " + action); System.out.println(""); @@ -110,7 +117,7 @@ public byte action(int action, boolean thing) { printStats(); System.exit(0); } else { - System.out.println("gold not found error"); + System.out.println("Error: Gold not found"); } break; case MOVE: @@ -118,14 +125,14 @@ public byte action(int action, boolean thing) { switch (direction) { case NORTH: System.out.println("Moved north"); - if (y + 1 < size && (perceptMap[x][y+1] & BUMP)==0) { - if ((perceptMap[x][y+1] & DEATH_WUMPUS) == DEATH_WUMPUS) { + if (y + 1 < size && (perceptMap[x][y + 1] & BUMP) == 0) { + if ((perceptMap[x][y + 1] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x][y+1] & DEATH_PIT) == DEATH_PIT) { + if ((perceptMap[x][y + 1] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); @@ -135,64 +142,64 @@ public byte action(int action, boolean thing) { return perceptMap[x][y]; } else { return BUMP; - } + } case EAST: System.out.println("Moved east"); - if(x+1 < size && (perceptMap[x+1][y] & BUMP)==0){ - if ((perceptMap[x+1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + if (x + 1 < size && (perceptMap[x + 1][y] & BUMP) == 0) { + if ((perceptMap[x + 1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus."); return DEATH_WUMPUS; } - if ((perceptMap[x+1][y] & DEATH_PIT) == DEATH_PIT) { + if ((perceptMap[x + 1][y] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; return DEATH_PIT; - } - x = x + 1; + } + x = x + 1; return perceptMap[x][y]; - }else { + } else { return BUMP; } case SOUTH: System.out.println("Moved south"); - if(y > 0 && (perceptMap[x][y-1] & BUMP)==0){ - if ((perceptMap[x][y-1] & DEATH_WUMPUS) == DEATH_WUMPUS) { - score -= 1000; - wumpusDeaths++; - System.out.println("Death to wumpus."); - return DEATH_WUMPUS; - } - if ((perceptMap[x][y-1] & DEATH_PIT) == DEATH_PIT) { - score -= 1000; - pitDeaths++; - System.out.println("Death to pit."); - return DEATH_PIT; - } + if (y > 0 && (perceptMap[x][y - 1] & BUMP) == 0) { + if ((perceptMap[x][y - 1] & DEATH_WUMPUS) == DEATH_WUMPUS) { + score -= 1000; + wumpusDeaths++; + System.out.println("Death to wumpus."); + return DEATH_WUMPUS; + } + if ((perceptMap[x][y - 1] & DEATH_PIT) == DEATH_PIT) { + score -= 1000; + pitDeaths++; + System.out.println("Death to pit."); + return DEATH_PIT; + } y -= 1; return perceptMap[x][y]; - }else { + } else { return BUMP; } case WEST: System.out.println("Moved west"); - if(x>0 && (perceptMap[x-1][y] & BUMP)==0){ - if ((perceptMap[x-1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { - score -= 1000; - wumpusDeaths++; - System.out.println("Death to wumpus."); - return DEATH_WUMPUS; - } - if ((perceptMap[x-1][y] & DEATH_PIT) == DEATH_PIT) { - score -= 1000; - pitDeaths++; - System.out.println("Death to pit."); - return DEATH_PIT; - } + if (x > 0 && (perceptMap[x - 1][y] & BUMP) == 0) { + if ((perceptMap[x - 1][y] & DEATH_WUMPUS) == DEATH_WUMPUS) { + score -= 1000; + wumpusDeaths++; + System.out.println("Death to wumpus."); + return DEATH_WUMPUS; + } + if ((perceptMap[x - 1][y] & DEATH_PIT) == DEATH_PIT) { + score -= 1000; + pitDeaths++; + System.out.println("Death to pit."); + return DEATH_PIT; + } x -= 1; return perceptMap[x][y]; - }else { + } else { return BUMP; } default: @@ -212,19 +219,20 @@ public byte action(int action, boolean thing) { case SHOOT: //shoot logic if (arrowCount == 0) { + System.out.println("Error: Out of arrows"); return -1; //out of arrows, which shouldn't be possible } arrowCount--; - score-=10; + score -= 10; switch (direction) { case 1: //shoot north for (int i = y; i < perceptMap.length; i++) { - if ((perceptMap[x][i] & DEATH_WUMPUS)!= 0) { //hits Wumpus + if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); - score+=10; + score += 10; killedWumpus++; return SCREAM; - } else if ((perceptMap[x][i] & BUMP)!= 0) { //hits Obstacle + } else if ((perceptMap[x][i] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -232,10 +240,10 @@ public byte action(int action, boolean thing) { case 2: //shoot east for (int i = y; i < perceptMap.length; i++) { - if ((perceptMap[i][y] & DEATH_WUMPUS)!= 0) { //hits Wumpus + if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; - } else if ((perceptMap[i][y] & BUMP)!=0) { //hits Obstacle + } else if ((perceptMap[i][y] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -243,10 +251,10 @@ public byte action(int action, boolean thing) { case 3: //shoot south for (int i = y; i > 0; i--) { - if ((perceptMap[x][i] & DEATH_WUMPUS)!=0) { //hits Wumpus + if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); return SCREAM; - } else if ((perceptMap[x][i] & BUMP)!=0) { //hits Obstacle + } else if ((perceptMap[x][i] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -254,10 +262,10 @@ public byte action(int action, boolean thing) { case 4: //shoot west for (int i = y; i > 0; i--) { - if ((perceptMap[i][y] & DEATH_WUMPUS)!=0) { //hits Wumpus + if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; - } else if ((perceptMap[i][y] &BUMP)!=0) { //hits Obstacle + } else if ((perceptMap[i][y] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } From fccf113b6bc165303584e4de580998ff7ea91be0 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 13:42:31 -0600 Subject: [PATCH 130/191] things --- Wumpus/PerceptBoard.txt | 17 ++++++----------- Wumpus/src/Driver.java | 2 +- Wumpus/src/LogicExplorer.java | 13 ++++++++----- Wumpus/world.txt | 17 ++++++----------- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 21bd78b..062ba74 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,11 +1,6 @@ -10 0 5 -1 17 1 1 0 0 4 0 6 0 -1 17 1 16 1 0 2 2 32 2 -0 1 0 5 0 2 32 2 2 2 -4 2 0 4 1 1 2 34 35 34 -2 32 2 1 17 17 1 3 18 3 -0 6 4 1 1 17 3 32 7 0 -2 0 1 17 1 1 16 3 0 0 -32 6 9 17 1 16 1 1 0 0 -3 1 0 1 0 5 1 18 1 0 -17 17 1 16 1 0 2 33 2 0 +5 1 3 +0 0 0 0 0 +0 4 0 0 0 +0 8 0 0 0 +0 0 0 2 0 +0 0 2 32 2 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 6f645d8..7fa4995 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + // makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index e5d3b6d..43e3dc0 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -48,16 +48,16 @@ public void updateLocation() { } public void expandFrontier() { - if (location.x > 0 && !searchedPositions[location.x - 1][location.y]) { + if (location.x > 0 && !searchedPositions[location.x - 1][location.y] && !inFrontier(new Location(location.x-1,location.y))) { frontier.add(new Location(location.x - 1, location.y)); } - if (location.x < world.size - 1 && !searchedPositions[location.x + 1][location.y]) { + if (location.x < world.size - 1 && !searchedPositions[location.x + 1][location.y] && !inFrontier(new Location(location.x+1,location.y))) { frontier.add(new Location(location.x + 1, location.y)); } - if (location.y > 0 && !searchedPositions[location.x][location.y - 1]) { + if (location.y > 0 && !searchedPositions[location.x][location.y - 1] && !inFrontier(new Location(location.x,location.y-1))) { frontier.add(new Location(location.x, location.y - 1)); } - if (location.y < world.size - 1 && !searchedPositions[location.x][location.y + 1]) { + if (location.y < world.size - 1 && !searchedPositions[location.x][location.y + 1] && !inFrontier(new Location(location.x,location.y+1))) { frontier.add(new Location(location.x, location.y + 1)); } } @@ -84,7 +84,6 @@ private void move(int action) { moveHistory.add(MOVE); System.out.println("Explorer Action: Moving"); percepts = (byte) world.action(MOVE); - processPercepts(); break; case TURN_LEFT: moveHistory.add(TURN_LEFT); @@ -176,6 +175,7 @@ private void decideNextAction() { if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { if(inFrontier(getForward())){ move(World.MOVE); + processPercepts(); return; } @@ -186,18 +186,21 @@ private void decideNextAction() { rhwTraversal(neighborSafeSpace(safeSpace)); turnToSpace(safeSpace); move(MOVE); + processPercepts(); removeFromFrontier(safeSpace); } else if (arrowCount > 0 && wumpusInFrontier()) { rhwTraversal(neighborSafeSpace(wumpusSpace)); turnToSpace(wumpusSpace); move(SHOOT); move(MOVE); + processPercepts(); removeFromFrontier(wumpusSpace); } else if (!frontier.isEmpty()) { rhwTraversal(neighborSafeSpace(frontier.get(frontier.size() - 1))); turnToSpace(frontier.get(frontier.size() - 1)); frontier.remove(frontier.size() - 1); move(MOVE); + processPercepts(); } } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index f41523a..8ddcdb0 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,11 +1,6 @@ -10 -0 H 0 0 0 S I 0 I 0 -0 H 0 H 0 0 0 0 W 0 -0 0 0 I 0 0 W 0 0 0 -I 0 0 I 0 0 0 W W W -0 W 0 0 H H 0 0 H 0 -0 I I 0 0 H 0 W I 0 -0 0 0 H 0 0 H 0 0 0 -W I G H 0 H 0 0 0 0 -0 0 0 0 0 I 0 H 0 0 -H H 0 H 0 0 0 W 0 0 +5 +0 0 0 0 0 +0 I 0 S 0 +0 G 0 0 0 +0 0 0 0 0 +0 0 0 W 0 From 26c2b195845c4ce94373369d83a5ab9887889cf5 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 13:43:06 -0600 Subject: [PATCH 131/191] commit --- Wumpus/src/LogicExplorer.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 43e3dc0..e5d3b6d 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -48,16 +48,16 @@ public void updateLocation() { } public void expandFrontier() { - if (location.x > 0 && !searchedPositions[location.x - 1][location.y] && !inFrontier(new Location(location.x-1,location.y))) { + if (location.x > 0 && !searchedPositions[location.x - 1][location.y]) { frontier.add(new Location(location.x - 1, location.y)); } - if (location.x < world.size - 1 && !searchedPositions[location.x + 1][location.y] && !inFrontier(new Location(location.x+1,location.y))) { + if (location.x < world.size - 1 && !searchedPositions[location.x + 1][location.y]) { frontier.add(new Location(location.x + 1, location.y)); } - if (location.y > 0 && !searchedPositions[location.x][location.y - 1] && !inFrontier(new Location(location.x,location.y-1))) { + if (location.y > 0 && !searchedPositions[location.x][location.y - 1]) { frontier.add(new Location(location.x, location.y - 1)); } - if (location.y < world.size - 1 && !searchedPositions[location.x][location.y + 1] && !inFrontier(new Location(location.x,location.y+1))) { + if (location.y < world.size - 1 && !searchedPositions[location.x][location.y + 1]) { frontier.add(new Location(location.x, location.y + 1)); } } @@ -84,6 +84,7 @@ private void move(int action) { moveHistory.add(MOVE); System.out.println("Explorer Action: Moving"); percepts = (byte) world.action(MOVE); + processPercepts(); break; case TURN_LEFT: moveHistory.add(TURN_LEFT); @@ -175,7 +176,6 @@ private void decideNextAction() { if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { if(inFrontier(getForward())){ move(World.MOVE); - processPercepts(); return; } @@ -186,21 +186,18 @@ private void decideNextAction() { rhwTraversal(neighborSafeSpace(safeSpace)); turnToSpace(safeSpace); move(MOVE); - processPercepts(); removeFromFrontier(safeSpace); } else if (arrowCount > 0 && wumpusInFrontier()) { rhwTraversal(neighborSafeSpace(wumpusSpace)); turnToSpace(wumpusSpace); move(SHOOT); move(MOVE); - processPercepts(); removeFromFrontier(wumpusSpace); } else if (!frontier.isEmpty()) { rhwTraversal(neighborSafeSpace(frontier.get(frontier.size() - 1))); turnToSpace(frontier.get(frontier.size() - 1)); frontier.remove(frontier.size() - 1); move(MOVE); - processPercepts(); } } From fff70136532b856ad721b3e869b6502e63b3a175 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 17:33:14 -0600 Subject: [PATCH 132/191] did that do it? --- Wumpus/src/Driver.java | 2 +- Wumpus/src/Fact.java | 3 ++- Wumpus/src/LogicExplorer.java | 16 ++++++++-------- Wumpus/src/World.java | 2 ++ 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 7fa4995..9c7c825 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -35,7 +35,7 @@ public static void makeGame() throws IOException { prob[2] = Integer.parseInt(dataIn.readLine()); System.out.println(""); - WumpusGame game = new WumpusGame(size, prob); + WumpusGame game = new WumpusGame(size, prob, false); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); // World world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index f8804a0..b6085ec 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -37,7 +37,8 @@ public void printFact() { System.out.print(predicate + "("); for (Variable var : variables) { var.printVariable(); - System.out.print(", "); + if(var != variables.get(variables.size()-1)) + System.out.print(", "); } System.out.print(") "); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index e5d3b6d..a4de797 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -204,15 +204,15 @@ private void decideNextAction() { public void turnToSpace(Location loc) { if (location.x > loc.x) { switch (direction) { - case EAST: + case WEST: break; case NORTH: - move(TURN_LEFT); + move(TURN_RIGHT); break; case SOUTH: - move(TURN_RIGHT); + move(TURN_LEFT); break; - case WEST: + case EAST: move(TURN_RIGHT); move(TURN_RIGHT); break; @@ -221,15 +221,15 @@ public void turnToSpace(Location loc) { } } else if (loc.x > location.x) { switch (direction) { - case WEST: + case EAST: return; - case NORTH: + case SOUTH: move(TURN_RIGHT); break; - case SOUTH: + case NORTH: move(TURN_LEFT); break; - case EAST: + case WEST: move(TURN_RIGHT); move(TURN_RIGHT); break; diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 604fdfb..aa05486 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -22,6 +22,8 @@ public World(String fileName) { } } + System.out.println("Starting world:"); + printWorld(); } public void startGame(String id) { From d9f7db245b1ed59eff8cb5fee52f003371bd22d0 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 20:11:21 -0600 Subject: [PATCH 133/191] amazing stuff --- Wumpus/PerceptBoard.txt | 10 ++++---- Wumpus/clean.txt | 6 ++--- Wumpus/src/Agent.java | 4 ++-- Wumpus/src/Driver.java | 4 ++-- Wumpus/src/KnowledgeBase.java | 8 +++++++ Wumpus/src/LogicExplorer.java | 44 +++++++++++++++++++++-------------- Wumpus/world.txt | 6 ++--- 7 files changed, 49 insertions(+), 33 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 062ba74..f9d6619 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 1 3 +5 3 0 0 0 0 0 0 -0 4 0 0 0 -0 8 0 0 0 -0 0 0 2 0 -0 0 2 32 2 +2 4 0 8 0 +32 2 1 0 0 +2 1 16 1 4 +0 0 1 0 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 8bc2909..0187b59 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,6 @@ 5 -0 0 W 0 0 0 0 0 0 0 -0 0 0 0 0 -0 0 0 W 0 0 I 0 0 0 +W 0 0 0 0 +0 0 H 0 I +0 0 0 0 0 diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 5b620b2..82a0da2 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -65,7 +65,7 @@ public void updateLocation() { location.y += 1; } break; - case EAST: + case WEST: if (location.x > 0) { location.x -= 1; } @@ -75,7 +75,7 @@ public void updateLocation() { location.y--; } break; - case WEST: + case EAST: if (location.x < World.size - 1) { location.x++; } diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 9c7c825..1c128d5 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + //makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } @@ -35,7 +35,7 @@ public static void makeGame() throws IOException { prob[2] = Integer.parseInt(dataIn.readLine()); System.out.println(""); - WumpusGame game = new WumpusGame(size, prob, false); + WumpusGame game = new WumpusGame(size, prob, true); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); // World world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index d1f9346..6c4f809 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -193,8 +193,16 @@ public void tell(Fact fact) { for(int i = 0; i < clauses.size(); i++){ for(int j = 0; j < clauses.get(i).facts.size(); j++){ if(clauses.get(i).facts.get(j).variables.get(0).value == fact.variables.get(0).value && clauses.get(i).facts.get(j).variables.get(1).value == fact.variables.get(1).value){ + + if(clauses.get(i).facts.get(j).predicate.equals("Wumpus")){ clauses.get(i).facts.get(j).not = true; } + else if(clauses.get(i).facts.get(j).predicate.equals("Stench")){ + clauses.get(i).facts.remove(j); + if(clauses.get(i).facts.size() == 0) + clauses.remove(i); + } + } } } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index a4de797..f0364d5 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -44,21 +44,28 @@ public void updateLocation() { } else { notFirstMove = true; } + searchedPositions[location.x][location.y] = true; expandFrontier(); } + public void addToFrontier(Location loc){ + if(inFrontier(loc)){ + removeFromFrontier(loc); + } + frontier.add(loc); + } public void expandFrontier() { if (location.x > 0 && !searchedPositions[location.x - 1][location.y]) { - frontier.add(new Location(location.x - 1, location.y)); + addToFrontier(new Location(location.x - 1, location.y)); } if (location.x < world.size - 1 && !searchedPositions[location.x + 1][location.y]) { - frontier.add(new Location(location.x + 1, location.y)); + addToFrontier(new Location(location.x + 1, location.y)); } if (location.y > 0 && !searchedPositions[location.x][location.y - 1]) { - frontier.add(new Location(location.x, location.y - 1)); + addToFrontier(new Location(location.x, location.y - 1)); } if (location.y < world.size - 1 && !searchedPositions[location.x][location.y + 1]) { - frontier.add(new Location(location.x, location.y + 1)); + addToFrontier(new Location(location.x, location.y + 1)); } } @@ -131,7 +138,6 @@ private void processPercepts() { //there might still be an issue with wum } if ((((percepts & BUMP) != BUMP) && (percepts & DEATH_PIT) != DEATH_PIT) && ((percepts & DEATH_WUMPUS) != DEATH_WUMPUS)) { updateLocation(); - searchedPositions[location.x][location.y] = true; removeFromFrontier(location); } if ((percepts & BUMP) == BUMP) { //theres soemthing funky here, hes bumping when there arent obsticales @@ -152,7 +158,7 @@ else if((percepts & DEATH_PIT) == 0 && (percepts & DEATH_WUMPUS)==0){//you if yo kb.tell(new Fact("Breeze", location.x, false, location.y, false, true, null, null)); } if ((percepts & SCREAM) != 0) { - kb.tell(new Fact("Scream", location.x, false, location.y, false, false, null, null)); + kb.tell(new Fact("DeadWumpus", getForward().x, false, getForward().y, false, false, null, null)); }} } @@ -183,18 +189,20 @@ private void decideNextAction() { } } if (safeSpaceInFrontier()) { - rhwTraversal(neighborSafeSpace(safeSpace)); + if(!adjacent(safeSpace)) + rhwTraversal(safeSpace); turnToSpace(safeSpace); move(MOVE); removeFromFrontier(safeSpace); } else if (arrowCount > 0 && wumpusInFrontier()) { - rhwTraversal(neighborSafeSpace(wumpusSpace)); + if(!adjacent(safeSpace)) + rhwTraversal(wumpusSpace); turnToSpace(wumpusSpace); move(SHOOT); move(MOVE); removeFromFrontier(wumpusSpace); } else if (!frontier.isEmpty()) { - rhwTraversal(neighborSafeSpace(frontier.get(frontier.size() - 1))); + rhwTraversal(frontier.get(frontier.size() - 1)); turnToSpace(frontier.get(frontier.size() - 1)); frontier.remove(frontier.size() - 1); move(MOVE); @@ -202,15 +210,15 @@ private void decideNextAction() { } public void turnToSpace(Location loc) { - if (location.x > loc.x) { + if (loc.x < location.x) { switch (direction) { case WEST: break; case NORTH: - move(TURN_RIGHT); + move(TURN_LEFT); break; case SOUTH: - move(TURN_LEFT); + move(TURN_RIGHT); break; case EAST: move(TURN_RIGHT); @@ -224,10 +232,10 @@ public void turnToSpace(Location loc) { case EAST: return; case SOUTH: - move(TURN_RIGHT); + move(TURN_LEFT); break; case NORTH: - move(TURN_LEFT); + move(TURN_RIGHT); break; case WEST: move(TURN_RIGHT); @@ -298,11 +306,11 @@ private boolean wumpusInFrontier() { } private void moveHistoryTraversal(Location loc) { - if (loc.x == location.x && loc.y == location.y) { + if (adjacent(loc)) { return; } for (int i = moveHistory.size() - 1; i >= 0; i--) { - if (loc.x == location.x && loc.y == location.y) { + if (adjacent(loc)) { return; } int move = moveHistory.get(i); @@ -335,12 +343,12 @@ private void rhwTraversal(Location location) { private boolean adjacent(Location location) { if (location.x == this.location.x) { - if (Math.abs(location.x - this.location.x) == 1) { + if (Math.abs(location.y - this.location.y) == 1) { return true; } } if (location.y == this.location.y) { - if (Math.abs(location.y - this.location.y) == 1) { + if (Math.abs(location.x - this.location.x) == 1) { return true; } } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 8ddcdb0..487b544 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 0 0 0 0 0 -0 I 0 S 0 -0 G 0 0 0 +0 I 0 G 0 +W 0 0 0 0 +S 0 H 0 I 0 0 0 0 0 -0 0 0 W 0 From 019f1c2de4eaccf7b04d7f155cc0cf74c4c35add Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Fri, 21 Oct 2016 20:50:07 -0600 Subject: [PATCH 134/191] Made driver less retarded --- Wumpus/PerceptBoard.txt | 26 +++++----------------- Wumpus/clean.txt | 6 ++--- Wumpus/src/Driver.java | 45 +++++++++++++++++++++----------------- Wumpus/src/WumpusGame.java | 23 +++++++------------ Wumpus/world.txt | 22 ++++--------------- 5 files changed, 46 insertions(+), 76 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index f4821b9..be8e8ae 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,20 +1,6 @@ -<<<<<<< HEAD -5 3 1 -0 2 32 2 0 -0 0 2 0 0 -0 0 0 2 8 -0 0 2 32 2 -0 4 0 2 0 -======= -10 0 5 -1 17 1 1 0 0 4 0 6 0 -1 17 1 16 1 0 2 2 32 2 -0 1 0 5 0 2 32 2 2 2 -4 2 0 4 1 1 2 34 35 34 -2 32 2 1 17 17 1 3 18 3 -0 6 4 1 1 17 3 32 7 0 -2 0 1 17 1 1 16 3 0 0 -32 6 9 17 1 16 1 1 0 0 -3 1 0 1 0 5 1 18 1 0 -17 17 1 16 1 0 2 33 2 0 ->>>>>>> 9c8ca99385064616a125c031436e11c9415fa505 +5 1 0 +0 0 1 16 1 +0 0 0 1 16 +0 0 0 0 1 +0 0 4 0 0 +0 8 0 0 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 8bc2909..2b684a6 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,6 @@ 5 -0 0 W 0 0 +0 0 0 H 0 +0 0 0 0 H 0 0 0 0 0 +0 0 I 0 0 0 0 0 0 0 -0 0 0 W 0 -0 I 0 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 6222456..5fcc608 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -18,26 +18,31 @@ public static void main(String[] args) throws IOException { public static void makeGame() throws IOException { -// BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); -// int[] prob = new int[3]; -// System.out.println("Size of board: "); -// -// int size = Integer.parseInt(dataIn.readLine()); -// System.out.print("% chance of generating pit: "); -// prob[0] = Integer.parseInt(dataIn.readLine()); -// System.out.println(); -// System.out.print("% chance of generating obstacle: "); -// prob[1] = Integer.parseInt(dataIn.readLine()); -// System.out.println(""); -// System.out.print("% chance of generating wumpus: "); -// prob[2] = Integer.parseInt(dataIn.readLine()); -// System.out.println(""); - - WumpusGame game = new WumpusGame("clean.txt"); - -// game = new WumpusGame(size, prob, true); - World world = new World("PerceptBoard.txt"); - world.startGame("LogicExplorer"); + boolean newBoard = false; + boolean newStart = true; + WumpusGame game; + if (newBoard) { + BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); + int[] prob = new int[3]; + System.out.println("Size of board: "); + + int size = Integer.parseInt(dataIn.readLine()); + System.out.print("% chance of generating pit: "); + prob[0] = Integer.parseInt(dataIn.readLine()); + System.out.println(); + System.out.print("% chance of generating obstacle: "); + prob[1] = Integer.parseInt(dataIn.readLine()); + System.out.println(""); + System.out.print("% chance of generating wumpus: "); + prob[2] = Integer.parseInt(dataIn.readLine()); + System.out.println(""); + game = new WumpusGame(size, prob); + } + if (newStart) { + game = new WumpusGame("clean.txt"); + } + World world = new World("PerceptBoard.txt"); + world.startGame("LogicExplorer"); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); // World world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/WumpusGame.java b/Wumpus/src/WumpusGame.java index ae71c1c..4c0aebc 100644 --- a/Wumpus/src/WumpusGame.java +++ b/Wumpus/src/WumpusGame.java @@ -29,26 +29,19 @@ public WumpusGame(String fileName) throws FileNotFoundException { System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); } - public WumpusGame(int boardSize, int[] prob, boolean newOrNo) throws FileNotFoundException { + public WumpusGame(int boardSize, int[] prob) throws FileNotFoundException { this.boardSize = boardSize; this.prob = prob; perceptBoard = new byte[boardSize][boardSize]; board = new Space[boardSize][boardSize]; setBoard(); - if (!newOrNo) { - initializeBoard(); - PrintStream out = new PrintStream(new FileOutputStream("world.txt")); - System.setOut(out); - printBoards(); - System.setOut(new PrintStream(new FileOutputStream("clean.txt"))); - printBoards(); - System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); - } else { - newStart("clean.txt"); - System.setOut(new PrintStream(new FileOutputStream("world.txt"))); - printBoards(); - System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); - } + initializeBoard(); + PrintStream out = new PrintStream(new FileOutputStream("world.txt")); + System.setOut(out); + printBoards(); + System.setOut(new PrintStream(new FileOutputStream("clean.txt"))); + printBoards(); + System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 0c1a1aa..01fdebf 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,20 +1,6 @@ -<<<<<<< HEAD 5 -0 0 W 0 0 +0 0 0 H 0 +S 0 0 0 H 0 0 0 0 0 -0 0 0 0 G -0 S 0 W 0 -0 I 0 0 0 -======= -10 -0 H 0 0 0 S I 0 I 0 -0 H 0 H 0 0 0 0 W 0 -0 0 0 I 0 0 W 0 0 0 -I 0 0 I 0 0 0 W W W -0 W 0 0 H H 0 0 H 0 -0 I I 0 0 H 0 W I 0 -0 0 0 H 0 0 H 0 0 0 -W I G H 0 H 0 0 0 0 -0 0 0 0 0 I 0 H 0 0 -H H 0 H 0 0 0 W 0 0 ->>>>>>> 9c8ca99385064616a125c031436e11c9415fa505 +0 0 I 0 0 +0 G 0 0 0 From 7295eca93649a606bd6cde632a161639b63116b9 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 21:21:58 -0600 Subject: [PATCH 135/191] can find wumpus now... well kb can --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/clean.txt | 6 +++--- Wumpus/src/Driver.java | 4 ++-- Wumpus/src/InferenceEngine.java | 11 +++++++---- Wumpus/src/LogicExplorer.java | 2 ++ Wumpus/src/Unifier.java | 10 ++++++++-- Wumpus/world.txt | 6 +++--- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index f9d6619..c085bdc 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 3 0 -0 0 0 0 0 -2 4 0 8 0 -32 2 1 0 0 -2 1 16 1 4 -0 0 1 0 0 +5 0 0 +0 2 0 0 0 +2 32 2 0 0 +0 2 0 0 0 +0 0 0 4 0 +0 0 4 0 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 0187b59..22d65ec 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,6 @@ 5 0 0 0 0 0 -0 I 0 0 0 -W 0 0 0 0 -0 0 H 0 I +0 W 0 0 0 0 0 0 0 0 +0 0 0 I 0 +0 0 I 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 1c128d5..9c7c825 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - //makeGame(); + // makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } @@ -35,7 +35,7 @@ public static void makeGame() throws IOException { prob[2] = Integer.parseInt(dataIn.readLine()); System.out.println(""); - WumpusGame game = new WumpusGame(size, prob, true); + WumpusGame game = new WumpusGame(size, prob, false); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); // World world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 2b4c1ed..9926e4b 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -38,7 +38,7 @@ public boolean follows(Fact fact) { } } if(!skipOut){ - kbClause.facts.remove(kbFact); + kbClause.facts.remove(kbFact);//maybe commment out this line clause.facts.remove(followFact); clause.facts.addAll(kbClause.facts); @@ -74,6 +74,7 @@ public void infer(Fact factStart) { //look at each fact in each clause in kb.rules, if predicates match, and negations are opposite Fact fact = new Fact(factStart); ArrayList tempClone = new ArrayList<>(kb.rules); + tempClone.addAll(kb.getClauses()); ArrayList kbClausesClone = new ArrayList<>(); for (Clause tempClause : tempClone) { kbClausesClone.add(new Clause(tempClause)); @@ -82,7 +83,7 @@ public void infer(Fact factStart) { for (Fact ruleFact : clause.facts) { if (ruleFact.predicate.equals(fact.predicate) && ruleFact.not == !fact.not) { ArrayList substitutions = Unifier.unify(fact, ruleFact); - if (substitutions.isEmpty()) { + if (!Unifier.equalWithSubs(fact, ruleFact, substitutions)) { continue; } for (Substitute sub : substitutions) { @@ -128,6 +129,7 @@ public void infer(Clause clauseToCheck) { //with the substitution, apply to copies of both //strip fact from input clause ArrayList tempClone = new ArrayList<>(kb.rules); + tempClone.addAll(kb.getClauses()); ArrayList kbClausesClone = new ArrayList<>(); for (Clause tempClause : tempClone) { kbClausesClone.add(new Clause(tempClause)); @@ -156,13 +158,14 @@ public void infer(Clause clauseToCheck) { } } } - if(!substitutions.isEmpty()){ + if(Unifier.equalWithSubs(fact, ruleFact, substitutions)){ clause.facts.remove(fact); System.out.println("Inferred:"); Clause.printClause(clause); kb.addToClauses(clause); + return;//the smaller clause will be inferred against again, no need to keep trying rules immediately. } - break; + continue; } } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index f0364d5..9267d43 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -137,6 +137,8 @@ private void processPercepts() { //there might still be an issue with wum moveHistory.remove(moveHistory.size() - 1);//why die again? } if ((((percepts & BUMP) != BUMP) && (percepts & DEATH_PIT) != DEATH_PIT) && ((percepts & DEATH_WUMPUS) != DEATH_WUMPUS)) { + kb.tell(new Fact("Wumpus",location.x,false,location.y,false,true,null,null)); + kb.tell(new Fact("Pit",location.x,false,location.y,false,true,null,null)); updateLocation(); removeFromFrontier(location); } diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index 974e5fc..d2c2540 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -106,15 +106,21 @@ public static ArrayList unify(Fact f1, Fact f2){ } } + if(equalWithSubs(fact1,fact2,subs)) + return subs; + else return new ArrayList(); + } + + public static boolean equalWithSubs(Fact fact1, Fact fact2, ArrayList subs){ substitute(subs, fact1); substitute(subs, fact2); for(int i = 0; i < fact1.variables.size(); i++){ Variable var1 = fact1.variables.get(i); Variable var2 = fact2.variables.get(i); if(var1.value != var2.value) - return new ArrayList(); + return false; } - return subs; + return true; } public static void substitute(ArrayList subs, Fact fact){ diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 487b544..22d65ec 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 0 0 0 0 0 -0 I 0 G 0 -W 0 0 0 0 -S 0 H 0 I +0 W 0 0 0 0 0 0 0 0 +0 0 0 I 0 +0 0 I 0 0 From 8d5e7f301a7ab0042835ce868cc5350dca0e5f5d Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 21:35:28 -0600 Subject: [PATCH 136/191] beauty --- Wumpus/src/Driver.java | 2 +- Wumpus/src/LogicExplorer.java | 4 ++-- Wumpus/src/World.java | 16 ++++++++-------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 9c7c825..7fa4995 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -35,7 +35,7 @@ public static void makeGame() throws IOException { prob[2] = Integer.parseInt(dataIn.readLine()); System.out.println(""); - WumpusGame game = new WumpusGame(size, prob, false); + WumpusGame game = new WumpusGame(size, prob); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); // World world = new World("PerceptBoard.txt"); diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 9267d43..3df5ea6 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -137,9 +137,9 @@ private void processPercepts() { //there might still be an issue with wum moveHistory.remove(moveHistory.size() - 1);//why die again? } if ((((percepts & BUMP) != BUMP) && (percepts & DEATH_PIT) != DEATH_PIT) && ((percepts & DEATH_WUMPUS) != DEATH_WUMPUS)) { + updateLocation(); kb.tell(new Fact("Wumpus",location.x,false,location.y,false,true,null,null)); kb.tell(new Fact("Pit",location.x,false,location.y,false,true,null,null)); - updateLocation(); removeFromFrontier(location); } if ((percepts & BUMP) == BUMP) { //theres soemthing funky here, hes bumping when there arent obsticales @@ -197,7 +197,7 @@ private void decideNextAction() { move(MOVE); removeFromFrontier(safeSpace); } else if (arrowCount > 0 && wumpusInFrontier()) { - if(!adjacent(safeSpace)) + if(!adjacent(wumpusSpace)) rhwTraversal(wumpusSpace); turnToSpace(wumpusSpace); move(SHOOT); diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index aa05486..521c2ca 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -238,8 +238,8 @@ public byte action(int action, boolean thing) { arrowCount--; score -= 10; switch (direction) { - case 1: //shoot north - for (int i = y; i < perceptMap.length; i++) { + case NORTH: //shoot north + for (int i = y+1; i < perceptMap.length; i++) { if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); score += 10; @@ -251,8 +251,8 @@ public byte action(int action, boolean thing) { } return perceptMap[x][y]; - case 2: //shoot east - for (int i = y; i < perceptMap.length; i++) { + case EAST: //shoot east + for (int i = x+1; i < perceptMap.length; i++) { if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; @@ -262,8 +262,8 @@ public byte action(int action, boolean thing) { } return perceptMap[x][y]; - case 3: //shoot south - for (int i = y; i > 0; i--) { + case SOUTH: //shoot south + for (int i = y-1; i >= 0; i--) { if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); return SCREAM; @@ -273,8 +273,8 @@ public byte action(int action, boolean thing) { } return perceptMap[x][y]; - case 4: //shoot west - for (int i = y; i > 0; i--) { + case WEST: //shoot west + for (int i = x-1; i > 0; i--) { if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; From ecdc446c29769ea68f0b1206f5d0c2bc12310b92 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 21:40:22 -0600 Subject: [PATCH 137/191] stuff --- Wumpus/PerceptBoard.txt | 17 ++++++++++------ Wumpus/clean.txt | 17 ++++++++++------ Wumpus/src/Driver.java | 44 ++++++++++++++++++++++++----------------- Wumpus/world.txt | 17 ++++++++++------ 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index c085bdc..301fcd1 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,11 @@ -5 0 0 -0 2 0 0 0 -2 32 2 0 0 -0 2 0 0 0 -0 0 0 4 0 -0 0 4 0 0 +10 3 7 +0 0 0 6 32 2 0 0 0 0 +0 4 4 0 2 0 0 0 4 0 +4 1 4 0 0 1 1 4 0 0 +5 16 3 0 1 17 17 1 0 0 +4 7 32 2 0 1 5 0 4 0 +0 5 2 0 4 0 0 4 6 4 +1 16 1 4 0 0 6 2 32 2 +0 5 16 1 1 2 32 6 3 0 +0 0 1 1 16 1 2 5 16 5 +0 0 0 8 1 0 0 4 1 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 22d65ec..8d4c827 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,11 @@ -5 -0 0 0 0 0 -0 W 0 0 0 -0 0 0 0 0 -0 0 0 I 0 -0 0 I 0 0 +10 +0 0 0 I W 0 0 0 0 0 +0 I I 0 0 0 0 0 I 0 +I 0 I 0 0 0 0 I 0 0 +I H 0 0 0 H H 0 0 0 +I I W 0 0 0 I 0 I 0 +0 I 0 0 I 0 0 I I I +0 H 0 I 0 0 I 0 W 0 +0 I H 0 0 0 W I 0 0 +0 0 0 0 H 0 0 I H I +0 0 0 0 0 0 0 I 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 7fa4995..d78d7fd 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,30 +13,38 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } public static void makeGame() throws IOException { - BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); - int[] prob = new int[3]; - System.out.println("Size of board: "); - - int size = Integer.parseInt(dataIn.readLine()); - System.out.print("% chance of generating pit: "); - prob[0] = Integer.parseInt(dataIn.readLine()); - System.out.println(); - System.out.print("% chance of generating obstacle: "); - prob[1] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - System.out.print("% chance of generating wumpus: "); - prob[2] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - - WumpusGame game = new WumpusGame(size, prob); - + boolean newBoard = true; + boolean newStart = true; + WumpusGame game; + if (newBoard) { + BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); + int[] prob = new int[3]; + System.out.println("Size of board: "); + + int size = Integer.parseInt(dataIn.readLine()); + System.out.print("% chance of generating pit: "); + prob[0] = Integer.parseInt(dataIn.readLine()); + System.out.println(); + System.out.print("% chance of generating obstacle: "); + prob[1] = Integer.parseInt(dataIn.readLine()); + System.out.println(""); + System.out.print("% chance of generating wumpus: "); + prob[2] = Integer.parseInt(dataIn.readLine()); + System.out.println(""); + game = new WumpusGame(size, prob); + } + if (newStart) { + game = new WumpusGame("clean.txt"); + } + World world = new World("PerceptBoard.txt"); + world.startGame("LogicExplorer"); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); // World world = new World("PerceptBoard.txt"); //world.startGame("LogicExplorer"); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 22d65ec..21b38c9 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,11 @@ -5 -0 0 0 0 0 -0 W 0 0 0 -0 0 0 0 0 -0 0 0 I 0 -0 0 I 0 0 +10 +0 0 0 I W 0 0 0 0 0 +0 I I 0 0 0 0 0 I 0 +I 0 I 0 0 0 0 I 0 0 +I H 0 0 0 H H S 0 0 +I I W 0 0 0 I 0 I 0 +0 I 0 0 I 0 0 I I I +0 H 0 I 0 0 I 0 W 0 +0 I H 0 0 0 W I 0 0 +0 0 0 0 H 0 0 I H I +0 0 0 G 0 0 0 I 0 0 From f5bed3b4b5bef825b6cd94705ac2fa2e2cc4b263 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 22:15:38 -0600 Subject: [PATCH 138/191] turn more/less at the same time --- Wumpus/PerceptBoard.txt | 17 ++++++----------- Wumpus/clean.txt | 17 ++++++----------- Wumpus/src/Driver.java | 2 +- Wumpus/src/LogicExplorer.java | 13 +++++++++---- Wumpus/world.txt | 17 ++++++----------- 5 files changed, 28 insertions(+), 38 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 301fcd1..95c330f 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,11 +1,6 @@ -10 3 7 -0 0 0 6 32 2 0 0 0 0 -0 4 4 0 2 0 0 0 4 0 -4 1 4 0 0 1 1 4 0 0 -5 16 3 0 1 17 17 1 0 0 -4 7 32 2 0 1 5 0 4 0 -0 5 2 0 4 0 0 4 6 4 -1 16 1 4 0 0 6 2 32 2 -0 5 16 1 1 2 32 6 3 0 -0 0 1 1 16 1 2 5 16 5 -0 0 0 8 1 0 0 4 1 0 +5 1 3 +0 4 4 4 4 +5 4 4 0 4 +16 1 8 0 0 +1 0 4 4 0 +0 0 0 4 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 8d4c827..ea0a733 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,11 +1,6 @@ -10 -0 0 0 I W 0 0 0 0 0 -0 I I 0 0 0 0 0 I 0 -I 0 I 0 0 0 0 I 0 0 -I H 0 0 0 H H 0 0 0 -I I W 0 0 0 I 0 I 0 -0 I 0 0 I 0 0 I I I -0 H 0 I 0 0 I 0 W 0 -0 I H 0 0 0 W I 0 0 -0 0 0 0 H 0 0 I H I -0 0 0 0 0 0 0 I 0 0 +5 +0 I I I I +I I I 0 I +H 0 0 0 0 +0 0 I I 0 +0 0 0 I 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index d78d7fd..a1f6f84 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + //makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 3df5ea6..548d78c 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -49,10 +49,11 @@ public void updateLocation() { } public void addToFrontier(Location loc){ + if(!searchedPositions[loc.x][loc.y]){ if(inFrontier(loc)){ removeFromFrontier(loc); } - frontier.add(loc); + frontier.add(loc);} } public void expandFrontier() { if (location.x > 0 && !searchedPositions[location.x - 1][location.y]) { @@ -91,7 +92,8 @@ private void move(int action) { moveHistory.add(MOVE); System.out.println("Explorer Action: Moving"); percepts = (byte) world.action(MOVE); - processPercepts(); + searchedPositions[getForward().x][getForward().y] = true; + processPercepts(); break; case TURN_LEFT: moveHistory.add(TURN_LEFT); @@ -173,7 +175,7 @@ private boolean inFrontier(Location loc){ } private void decideNextAction() { if ((percepts & GLITTER) != 0) { - move(World.GRAB); + move(GRAB); } if (frontier.isEmpty()) { @@ -183,7 +185,8 @@ private void decideNextAction() { if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, true, null, null))) { if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { if(inFrontier(getForward())){ - move(World.MOVE); + move(MOVE); + removeFromFrontier(getForward()); return; } @@ -321,6 +324,8 @@ private void moveHistoryTraversal(Location loc) { move(TURN_LEFT); move(TURN_LEFT); move(MOVE); + move(TURN_LEFT); + move(TURN_LEFT); break; case TURN_LEFT: move(TURN_RIGHT); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 21b38c9..1bfbc87 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,11 +1,6 @@ -10 -0 0 0 I W 0 0 0 0 0 -0 I I 0 0 0 0 0 I 0 -I 0 I 0 0 0 0 I 0 0 -I H 0 0 0 H H S 0 0 -I I W 0 0 0 I 0 I 0 -0 I 0 0 I 0 0 I I I -0 H 0 I 0 0 I 0 W 0 -0 I H 0 0 0 W I 0 0 -0 0 0 0 H 0 0 I H I -0 0 0 G 0 0 0 I 0 0 +5 +0 I I I I +I I I S I +H 0 G 0 0 +0 0 I I 0 +0 0 0 I 0 From 07af19a559158218bb44429a2d1266ea12faba92 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 22:20:46 -0600 Subject: [PATCH 139/191] fixer upper --- Wumpus/PerceptBoard.txt | 32 ++++++++++++++++++++++++++------ Wumpus/clean.txt | 32 ++++++++++++++++++++++++++------ Wumpus/src/Driver.java | 2 +- Wumpus/world.txt | 32 ++++++++++++++++++++++++++------ 4 files changed, 79 insertions(+), 19 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 95c330f..fe86b39 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,26 @@ -5 1 3 -0 4 4 4 4 -5 4 4 0 4 -16 1 8 0 0 -1 0 4 4 0 -0 0 0 4 0 +25 20 21 +0 4 4 0 4 0 1 16 9 6 32 2 2 32 2 0 0 0 0 0 0 0 0 0 0 +0 4 4 0 0 0 4 1 0 5 6 4 4 2 0 1 4 0 0 1 0 0 0 4 4 +1 4 0 4 0 4 0 4 1 16 1 0 4 0 1 16 7 0 1 16 1 5 0 4 0 +16 1 0 0 0 4 1 0 0 5 0 5 0 0 4 3 32 2 4 5 1 16 1 0 0 +1 0 0 0 4 1 16 1 0 4 1 16 1 0 0 0 6 4 0 0 0 5 1 4 4 +4 4 0 0 4 0 1 2 5 6 0 1 0 4 0 0 0 0 0 4 4 5 16 1 0 +0 4 4 4 0 0 6 33 18 33 2 0 4 0 4 0 4 0 0 0 1 0 1 16 5 +0 0 4 0 0 0 0 2 1 6 4 0 0 0 0 0 4 0 0 1 17 1 0 1 6 +4 4 0 5 4 1 0 0 4 0 4 0 0 0 4 4 0 0 0 1 17 1 0 2 32 +0 0 1 18 1 16 3 0 5 0 2 0 0 4 4 0 0 0 0 4 1 0 4 4 6 +0 4 2 33 19 3 34 3 16 3 32 2 4 0 0 0 0 4 0 2 4 0 4 4 4 +0 4 0 7 17 7 34 6 7 0 2 0 0 0 0 0 0 0 2 32 6 0 0 4 0 +0 1 4 0 5 0 6 6 34 2 0 0 0 0 2 0 4 4 4 3 4 0 4 1 0 +1 16 1 2 0 0 4 2 34 6 0 0 4 6 32 2 0 4 1 16 1 2 1 18 5 +2 1 2 32 2 0 4 0 2 0 5 4 0 0 2 0 0 4 0 1 2 33 2 33 2 +32 2 4 2 0 4 0 0 4 1 16 5 6 4 0 0 4 4 4 0 1 18 5 2 1 +6 0 0 0 4 4 4 0 0 0 5 2 34 2 0 0 0 1 1 0 4 5 0 5 17 +0 0 4 6 0 4 4 0 0 1 16 3 34 2 2 1 1 17 17 1 4 0 4 1 17 +0 0 2 32 2 0 6 0 0 4 1 4 2 6 33 18 1 1 1 4 0 0 4 0 5 +6 0 0 2 0 2 32 6 4 4 0 4 0 0 6 1 0 0 4 0 2 0 0 4 0 +32 2 0 0 0 4 2 4 0 0 0 0 4 0 0 4 0 0 0 2 33 2 0 0 4 +2 0 4 4 0 1 0 0 0 4 0 0 0 0 6 4 4 0 0 1 18 1 0 0 4 +0 4 4 5 1 16 1 0 0 0 4 0 5 6 32 2 0 4 1 16 1 0 4 5 0 +0 0 5 16 1 1 4 4 4 0 0 5 16 1 2 4 4 0 0 1 0 4 5 16 1 +0 0 4 1 4 0 0 0 0 4 0 4 1 0 0 4 0 0 4 0 0 4 4 1 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index ea0a733..7b1a7ca 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,26 @@ -5 -0 I I I I -I I I 0 I -H 0 0 0 0 -0 0 I I 0 -0 0 0 I 0 +25 +0 I I 0 I 0 0 H 0 I W 0 0 W 0 0 0 0 0 0 0 0 0 0 0 +0 I I 0 0 0 I 0 0 I I I I 0 0 0 I 0 0 0 0 0 0 I I +0 I 0 I 0 I 0 I 0 H 0 0 I 0 0 H I 0 0 H 0 I 0 I 0 +H 0 0 0 0 I 0 0 0 I 0 I 0 0 I 0 W 0 I I 0 H 0 0 0 +0 0 0 0 I 0 H 0 0 I 0 H 0 0 0 0 I I 0 0 0 I 0 I I +I I 0 0 I 0 0 0 I I 0 0 0 I 0 0 0 0 0 I I I H 0 0 +0 I I I 0 0 I W H W 0 0 I 0 I 0 I 0 0 0 0 0 0 H I +0 0 I 0 0 0 0 0 0 I I 0 0 0 0 0 I 0 0 0 H 0 0 0 I +I I 0 I I 0 0 0 I 0 I 0 0 0 I I 0 0 0 0 H 0 0 0 W +0 0 0 H 0 H 0 0 I 0 0 0 0 I I 0 0 0 0 I 0 0 I I I +0 I 0 W H 0 W 0 H 0 W 0 I 0 0 0 0 I 0 0 I 0 I I I +0 I 0 I H I W I I 0 0 0 0 0 0 0 0 0 0 W I 0 0 I 0 +0 0 I 0 I 0 I I W 0 0 0 0 0 0 0 I I I 0 I 0 I 0 0 +0 H 0 0 0 0 I 0 W I 0 0 I I W 0 0 I 0 H 0 0 0 H I +0 0 0 W 0 0 I 0 0 0 I I 0 0 0 0 0 I 0 0 0 W 0 W 0 +W 0 I 0 0 I 0 0 I 0 H I I I 0 0 I I I 0 0 H I 0 0 +I 0 0 0 I I I 0 0 0 I 0 W 0 0 0 0 0 0 0 I I 0 I H +0 0 I I 0 I I 0 0 0 H 0 W 0 0 0 0 H H 0 I 0 I 0 H +0 0 0 W 0 0 I 0 0 I 0 I 0 I W H 0 0 0 I 0 0 I 0 I +I 0 0 0 0 0 W I I I 0 I 0 0 I 0 0 0 I 0 0 0 0 I 0 +W 0 0 0 0 I 0 I 0 0 0 0 I 0 0 I 0 0 0 0 W 0 0 0 I +0 0 I I 0 0 0 0 0 I 0 0 0 0 I I I 0 0 0 H 0 0 0 I +0 I I I 0 H 0 0 0 0 I 0 I I W 0 0 I 0 H 0 0 I I 0 +0 0 I H 0 0 I I I 0 0 I H 0 0 I I 0 0 0 0 I I H 0 +0 0 I 0 I 0 0 0 0 I 0 I 0 0 0 I 0 0 I 0 0 I I 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index a1f6f84..d78d7fd 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - //makeGame(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 1bfbc87..b3742da 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,26 @@ -5 -0 I I I I -I I I S I -H 0 G 0 0 -0 0 I I 0 -0 0 0 I 0 +25 +0 I I 0 I 0 0 H G I W 0 0 W 0 0 0 0 0 0 0 0 0 0 0 +0 I I 0 0 0 I 0 0 I I I I 0 0 0 I 0 0 0 0 0 0 I I +0 I 0 I 0 I 0 I 0 H 0 0 I 0 0 H I 0 0 H 0 I 0 I 0 +H 0 0 0 0 I 0 0 0 I 0 I 0 0 I 0 W 0 I I 0 H 0 0 0 +0 0 0 0 I 0 H 0 0 I 0 H 0 0 0 0 I I 0 0 0 I 0 I I +I I 0 0 I 0 0 0 I I 0 0 0 I 0 0 0 0 0 I I I H 0 0 +0 I I I 0 0 I W H W 0 0 I 0 I 0 I 0 0 0 0 0 0 H I +0 0 I 0 0 0 0 0 0 I I 0 0 0 0 0 I 0 0 0 H 0 0 0 I +I I 0 I I 0 0 0 I 0 I 0 0 0 I I 0 0 0 0 H 0 0 0 W +0 0 0 H 0 H 0 0 I 0 0 0 0 I I 0 0 0 0 I 0 0 I I I +0 I 0 W H 0 W 0 H 0 W 0 I 0 0 0 0 I 0 0 I 0 I I I +0 I 0 I H I W I I 0 0 0 0 0 0 0 0 0 0 W I 0 0 I 0 +0 0 I 0 I 0 I I W 0 0 0 0 0 0 0 I I I 0 I 0 I 0 0 +0 H 0 0 0 0 I 0 W I 0 0 I I W 0 0 I 0 H 0 0 0 H I +0 0 0 W 0 0 I 0 0 0 I I 0 0 0 0 0 I 0 0 0 W 0 W 0 +W 0 I 0 0 I 0 0 I 0 H I I I 0 0 I I I 0 0 H I 0 0 +I 0 0 0 I I I 0 0 0 I 0 W 0 0 0 0 0 0 0 I I 0 I H +0 0 I I 0 I I 0 0 0 H 0 W 0 0 0 0 H H 0 I 0 I 0 H +0 0 0 W 0 0 I 0 0 I 0 I 0 I W H 0 0 0 I 0 0 I 0 I +I 0 0 0 0 0 W I I I 0 I 0 0 I 0 0 0 I 0 0 0 0 I 0 +W 0 0 0 0 I 0 I 0 0 0 0 I 0 0 I 0 0 0 0 W S 0 0 I +0 0 I I 0 0 0 0 0 I 0 0 0 0 I I I 0 0 0 H 0 0 0 I +0 I I I 0 H 0 0 0 0 I 0 I I W 0 0 I 0 H 0 0 I I 0 +0 0 I H 0 0 I I I 0 0 I H 0 0 I I 0 0 0 0 I I H 0 +0 0 I 0 I 0 0 0 0 I 0 I 0 0 0 I 0 0 I 0 0 I I 0 0 From d94a64b7bfa7c5d16a76026a2a72b6a936636db9 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 22:29:07 -0600 Subject: [PATCH 140/191] I'm awesome, I know --- Wumpus/PerceptBoard.txt | 52 +++++++++++++++++------------------ Wumpus/clean.txt | 50 ++++++++++++++++----------------- Wumpus/src/Driver.java | 2 +- Wumpus/src/KnowledgeBase.java | 3 ++ Wumpus/world.txt | 50 ++++++++++++++++----------------- 5 files changed, 80 insertions(+), 77 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index fe86b39..8c7de29 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,26 +1,26 @@ -25 20 21 -0 4 4 0 4 0 1 16 9 6 32 2 2 32 2 0 0 0 0 0 0 0 0 0 0 -0 4 4 0 0 0 4 1 0 5 6 4 4 2 0 1 4 0 0 1 0 0 0 4 4 -1 4 0 4 0 4 0 4 1 16 1 0 4 0 1 16 7 0 1 16 1 5 0 4 0 -16 1 0 0 0 4 1 0 0 5 0 5 0 0 4 3 32 2 4 5 1 16 1 0 0 -1 0 0 0 4 1 16 1 0 4 1 16 1 0 0 0 6 4 0 0 0 5 1 4 4 -4 4 0 0 4 0 1 2 5 6 0 1 0 4 0 0 0 0 0 4 4 5 16 1 0 -0 4 4 4 0 0 6 33 18 33 2 0 4 0 4 0 4 0 0 0 1 0 1 16 5 -0 0 4 0 0 0 0 2 1 6 4 0 0 0 0 0 4 0 0 1 17 1 0 1 6 -4 4 0 5 4 1 0 0 4 0 4 0 0 0 4 4 0 0 0 1 17 1 0 2 32 -0 0 1 18 1 16 3 0 5 0 2 0 0 4 4 0 0 0 0 4 1 0 4 4 6 -0 4 2 33 19 3 34 3 16 3 32 2 4 0 0 0 0 4 0 2 4 0 4 4 4 -0 4 0 7 17 7 34 6 7 0 2 0 0 0 0 0 0 0 2 32 6 0 0 4 0 -0 1 4 0 5 0 6 6 34 2 0 0 0 0 2 0 4 4 4 3 4 0 4 1 0 -1 16 1 2 0 0 4 2 34 6 0 0 4 6 32 2 0 4 1 16 1 2 1 18 5 -2 1 2 32 2 0 4 0 2 0 5 4 0 0 2 0 0 4 0 1 2 33 2 33 2 -32 2 4 2 0 4 0 0 4 1 16 5 6 4 0 0 4 4 4 0 1 18 5 2 1 -6 0 0 0 4 4 4 0 0 0 5 2 34 2 0 0 0 1 1 0 4 5 0 5 17 -0 0 4 6 0 4 4 0 0 1 16 3 34 2 2 1 1 17 17 1 4 0 4 1 17 -0 0 2 32 2 0 6 0 0 4 1 4 2 6 33 18 1 1 1 4 0 0 4 0 5 -6 0 0 2 0 2 32 6 4 4 0 4 0 0 6 1 0 0 4 0 2 0 0 4 0 -32 2 0 0 0 4 2 4 0 0 0 0 4 0 0 4 0 0 0 2 33 2 0 0 4 -2 0 4 4 0 1 0 0 0 4 0 0 0 0 6 4 4 0 0 1 18 1 0 0 4 -0 4 4 5 1 16 1 0 0 0 4 0 5 6 32 2 0 4 1 16 1 0 4 5 0 -0 0 5 16 1 1 4 4 4 0 0 5 16 1 2 4 4 0 0 1 0 4 5 16 1 -0 0 4 1 4 0 0 0 0 4 0 4 1 0 0 4 0 0 4 0 0 4 4 1 0 +25 7 14 +0 2 0 2 34 2 0 0 0 0 0 0 0 0 0 0 0 4 4 2 0 0 0 0 1 +2 32 2 2 34 2 0 0 0 0 1 0 0 0 0 0 0 0 2 32 2 0 2 1 16 +0 2 32 2 6 2 0 0 0 1 16 1 0 0 4 0 0 1 1 2 0 2 32 2 1 +0 0 2 0 2 32 2 0 0 4 1 0 1 4 0 0 1 17 17 1 0 0 2 0 0 +0 0 0 0 0 2 0 0 0 0 0 1 16 1 0 0 0 1 1 0 0 0 0 0 0 +0 0 4 0 0 2 0 0 0 0 0 0 1 1 0 0 0 4 0 0 0 0 0 0 0 +0 0 0 0 2 32 2 0 2 0 0 0 1 16 1 0 0 0 0 0 0 4 0 0 0 +0 0 0 0 4 2 0 2 32 2 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 +0 0 2 32 2 2 0 2 32 2 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 +0 0 0 2 2 32 2 0 2 0 2 32 2 0 0 0 4 0 0 0 0 6 32 2 0 +0 0 0 0 0 2 0 0 0 0 4 2 0 0 0 0 0 0 0 0 0 0 2 0 0 +0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 6 0 0 4 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 4 0 0 2 2 32 2 0 0 0 0 0 0 0 4 +0 0 0 0 0 0 0 4 0 0 0 0 2 32 2 2 1 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 2 0 0 0 0 0 2 0 1 16 1 0 0 8 0 4 0 2 +0 0 0 0 0 0 2 32 2 0 0 0 0 0 4 0 1 0 0 0 0 0 2 2 32 +0 0 0 2 0 0 0 2 0 0 0 0 0 0 1 1 0 0 0 0 0 3 32 2 2 +0 0 2 32 2 0 4 0 0 0 0 0 0 1 17 17 1 0 0 0 1 16 3 0 0 +0 0 0 2 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 5 0 0 0 +0 0 0 5 16 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 +0 0 1 0 1 0 0 0 0 1 16 1 0 1 16 1 0 0 0 0 0 0 0 2 0 +0 1 16 1 0 0 0 0 4 1 1 0 0 1 5 0 0 0 0 5 0 0 2 32 2 +0 2 1 0 0 0 0 0 1 17 17 1 1 16 1 0 2 4 1 16 1 0 0 2 0 +2 32 2 0 0 0 0 0 0 1 5 0 0 1 16 3 32 2 0 1 0 0 0 0 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 7b1a7ca..ab621c2 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,26 +1,26 @@ 25 -0 I I 0 I 0 0 H 0 I W 0 0 W 0 0 0 0 0 0 0 0 0 0 0 -0 I I 0 0 0 I 0 0 I I I I 0 0 0 I 0 0 0 0 0 0 I I -0 I 0 I 0 I 0 I 0 H 0 0 I 0 0 H I 0 0 H 0 I 0 I 0 -H 0 0 0 0 I 0 0 0 I 0 I 0 0 I 0 W 0 I I 0 H 0 0 0 -0 0 0 0 I 0 H 0 0 I 0 H 0 0 0 0 I I 0 0 0 I 0 I I -I I 0 0 I 0 0 0 I I 0 0 0 I 0 0 0 0 0 I I I H 0 0 -0 I I I 0 0 I W H W 0 0 I 0 I 0 I 0 0 0 0 0 0 H I -0 0 I 0 0 0 0 0 0 I I 0 0 0 0 0 I 0 0 0 H 0 0 0 I -I I 0 I I 0 0 0 I 0 I 0 0 0 I I 0 0 0 0 H 0 0 0 W -0 0 0 H 0 H 0 0 I 0 0 0 0 I I 0 0 0 0 I 0 0 I I I -0 I 0 W H 0 W 0 H 0 W 0 I 0 0 0 0 I 0 0 I 0 I I I -0 I 0 I H I W I I 0 0 0 0 0 0 0 0 0 0 W I 0 0 I 0 -0 0 I 0 I 0 I I W 0 0 0 0 0 0 0 I I I 0 I 0 I 0 0 -0 H 0 0 0 0 I 0 W I 0 0 I I W 0 0 I 0 H 0 0 0 H I -0 0 0 W 0 0 I 0 0 0 I I 0 0 0 0 0 I 0 0 0 W 0 W 0 -W 0 I 0 0 I 0 0 I 0 H I I I 0 0 I I I 0 0 H I 0 0 -I 0 0 0 I I I 0 0 0 I 0 W 0 0 0 0 0 0 0 I I 0 I H -0 0 I I 0 I I 0 0 0 H 0 W 0 0 0 0 H H 0 I 0 I 0 H -0 0 0 W 0 0 I 0 0 I 0 I 0 I W H 0 0 0 I 0 0 I 0 I -I 0 0 0 0 0 W I I I 0 I 0 0 I 0 0 0 I 0 0 0 0 I 0 -W 0 0 0 0 I 0 I 0 0 0 0 I 0 0 I 0 0 0 0 W 0 0 0 I -0 0 I I 0 0 0 0 0 I 0 0 0 0 I I I 0 0 0 H 0 0 0 I -0 I I I 0 H 0 0 0 0 I 0 I I W 0 0 I 0 H 0 0 I I 0 -0 0 I H 0 0 I I I 0 0 I H 0 0 I I 0 0 0 0 I I H 0 -0 0 I 0 I 0 0 0 0 I 0 I 0 0 0 I 0 0 I 0 0 I I 0 0 +0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 I I 0 0 0 0 0 0 +0 W 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 0 H +0 0 W 0 I 0 0 0 0 0 H 0 0 0 I 0 0 0 0 0 0 0 W 0 0 +0 0 0 0 0 W 0 0 0 I 0 0 0 I 0 0 0 H H 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 +0 0 0 0 0 W 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 I 0 0 0 +0 0 0 0 I 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 +0 0 0 W 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 W 0 0 0 0 0 W 0 0 0 0 I 0 0 0 0 I W 0 0 +0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 I 0 0 I 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 W 0 0 0 0 0 0 0 0 I +0 0 0 0 0 0 0 I 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 I 0 0 +0 0 0 0 0 0 0 W 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 W +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 +0 0 0 W 0 0 I 0 0 0 0 0 0 0 H H 0 0 0 0 0 H 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 +0 0 0 I H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 0 +0 0 H 0 0 0 0 0 I 0 0 0 0 0 I 0 0 0 0 I 0 0 0 W 0 +0 0 0 0 0 0 0 0 0 H H 0 0 H 0 0 0 I 0 H 0 0 0 0 0 +0 W 0 0 0 0 0 0 0 0 I 0 0 0 H 0 W 0 0 0 0 0 0 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index d78d7fd..a1f6f84 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + //makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 6c4f809..fa0ca2b 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -22,6 +22,9 @@ public void addToClauses(Clause clause){ } } if(clause.facts.size() == 1){ + if(factInClauses(clause.facts.get(0))){ + return; + } System.out.println("Added to kb Clause: "); System.out.print("\t"); Clause.printClause(clause); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index b3742da..d0431bb 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,26 +1,26 @@ 25 -0 I I 0 I 0 0 H G I W 0 0 W 0 0 0 0 0 0 0 0 0 0 0 -0 I I 0 0 0 I 0 0 I I I I 0 0 0 I 0 0 0 0 0 0 I I -0 I 0 I 0 I 0 I 0 H 0 0 I 0 0 H I 0 0 H 0 I 0 I 0 -H 0 0 0 0 I 0 0 0 I 0 I 0 0 I 0 W 0 I I 0 H 0 0 0 -0 0 0 0 I 0 H 0 0 I 0 H 0 0 0 0 I I 0 0 0 I 0 I I -I I 0 0 I 0 0 0 I I 0 0 0 I 0 0 0 0 0 I I I H 0 0 -0 I I I 0 0 I W H W 0 0 I 0 I 0 I 0 0 0 0 0 0 H I -0 0 I 0 0 0 0 0 0 I I 0 0 0 0 0 I 0 0 0 H 0 0 0 I -I I 0 I I 0 0 0 I 0 I 0 0 0 I I 0 0 0 0 H 0 0 0 W -0 0 0 H 0 H 0 0 I 0 0 0 0 I I 0 0 0 0 I 0 0 I I I -0 I 0 W H 0 W 0 H 0 W 0 I 0 0 0 0 I 0 0 I 0 I I I -0 I 0 I H I W I I 0 0 0 0 0 0 0 0 0 0 W I 0 0 I 0 -0 0 I 0 I 0 I I W 0 0 0 0 0 0 0 I I I 0 I 0 I 0 0 -0 H 0 0 0 0 I 0 W I 0 0 I I W 0 0 I 0 H 0 0 0 H I -0 0 0 W 0 0 I 0 0 0 I I 0 0 0 0 0 I 0 0 0 W 0 W 0 -W 0 I 0 0 I 0 0 I 0 H I I I 0 0 I I I 0 0 H I 0 0 -I 0 0 0 I I I 0 0 0 I 0 W 0 0 0 0 0 0 0 I I 0 I H -0 0 I I 0 I I 0 0 0 H 0 W 0 0 0 0 H H 0 I 0 I 0 H -0 0 0 W 0 0 I 0 0 I 0 I 0 I W H 0 0 0 I 0 0 I 0 I -I 0 0 0 0 0 W I I I 0 I 0 0 I 0 0 0 I 0 0 0 0 I 0 -W 0 0 0 0 I 0 I 0 0 0 0 I 0 0 I 0 0 0 0 W S 0 0 I -0 0 I I 0 0 0 0 0 I 0 0 0 0 I I I 0 0 0 H 0 0 0 I -0 I I I 0 H 0 0 0 0 I 0 I I W 0 0 I 0 H 0 0 I I 0 -0 0 I H 0 0 I I I 0 0 I H 0 0 I I 0 0 0 0 I I H 0 -0 0 I 0 I 0 0 0 0 I 0 I 0 0 0 I 0 0 I 0 0 I I 0 0 +0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 I I 0 0 0 0 0 0 +0 W 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 0 H +0 0 W 0 I 0 0 0 0 0 H 0 0 0 I 0 0 0 0 0 0 0 W 0 0 +0 0 0 0 0 W 0 0 0 I 0 0 0 I 0 0 0 H H 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 +0 0 0 0 0 W 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 I 0 0 0 +0 0 0 0 I 0 0 0 W 0 0 0 0 0 S 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 +0 0 0 W 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 W 0 0 0 0 0 W 0 0 0 0 I 0 0 0 0 I W 0 0 +0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 I 0 0 I 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 W 0 0 0 0 0 0 0 0 I +0 0 0 0 0 0 0 I 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 G 0 I 0 0 +0 0 0 0 0 0 0 W 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 W +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 +0 0 0 W 0 0 I 0 0 0 0 0 0 0 H H 0 0 0 0 0 H 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 +0 0 0 I H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 0 +0 0 H 0 0 0 0 0 I 0 0 0 0 0 I 0 0 0 0 I 0 0 0 W 0 +0 0 0 0 0 0 0 0 0 H H 0 0 H 0 0 0 I 0 H 0 0 0 0 0 +0 W 0 0 0 0 0 0 0 0 I 0 0 0 H 0 W 0 0 0 0 0 0 0 0 From c33a240b6971f40eff715cc3de0718eab0b6016d Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Fri, 21 Oct 2016 23:44:34 -0600 Subject: [PATCH 141/191] more wilson stuff --- Wumpus/PerceptBoard.txt | 32 ++++++-------------------------- Wumpus/clean.txt | 32 ++++++-------------------------- Wumpus/src/Driver.java | 2 +- Wumpus/src/InferenceEngine.java | 11 +++++++++++ Wumpus/src/KnowledgeBase.java | 4 ++++ Wumpus/src/LogicExplorer.java | 25 +++++++++++++++++-------- Wumpus/src/World.java | 5 +++-- Wumpus/world.txt | 32 ++++++-------------------------- 8 files changed, 54 insertions(+), 89 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 8c7de29..8e0b598 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,26 +1,6 @@ -25 7 14 -0 2 0 2 34 2 0 0 0 0 0 0 0 0 0 0 0 4 4 2 0 0 0 0 1 -2 32 2 2 34 2 0 0 0 0 1 0 0 0 0 0 0 0 2 32 2 0 2 1 16 -0 2 32 2 6 2 0 0 0 1 16 1 0 0 4 0 0 1 1 2 0 2 32 2 1 -0 0 2 0 2 32 2 0 0 4 1 0 1 4 0 0 1 17 17 1 0 0 2 0 0 -0 0 0 0 0 2 0 0 0 0 0 1 16 1 0 0 0 1 1 0 0 0 0 0 0 -0 0 4 0 0 2 0 0 0 0 0 0 1 1 0 0 0 4 0 0 0 0 0 0 0 -0 0 0 0 2 32 2 0 2 0 0 0 1 16 1 0 0 0 0 0 0 4 0 0 0 -0 0 0 0 4 2 0 2 32 2 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 -0 0 2 32 2 2 0 2 32 2 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 -0 0 0 2 2 32 2 0 2 0 2 32 2 0 0 0 4 0 0 0 0 6 32 2 0 -0 0 0 0 0 2 0 0 0 0 4 2 0 0 0 0 0 0 0 0 0 0 2 0 0 -0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 6 0 0 4 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 4 0 0 2 2 32 2 0 0 0 0 0 0 0 4 -0 0 0 0 0 0 0 4 0 0 0 0 2 32 2 2 1 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 2 0 0 0 0 0 2 0 1 16 1 0 0 8 0 4 0 2 -0 0 0 0 0 0 2 32 2 0 0 0 0 0 4 0 1 0 0 0 0 0 2 2 32 -0 0 0 2 0 0 0 2 0 0 0 0 0 0 1 1 0 0 0 0 0 3 32 2 2 -0 0 2 32 2 0 4 0 0 0 0 0 0 1 17 17 1 0 0 0 1 16 3 0 0 -0 0 0 2 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 5 0 0 0 -0 0 0 5 16 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 -0 0 1 0 1 0 0 0 0 1 16 1 0 1 16 1 0 0 0 0 0 0 0 2 0 -0 1 16 1 0 0 0 0 4 1 1 0 0 1 5 0 0 0 0 5 0 0 2 32 2 -0 2 1 0 0 0 0 0 1 17 17 1 1 16 1 0 2 4 1 16 1 0 0 2 0 -2 32 2 0 0 0 0 0 0 1 5 0 0 1 16 3 32 2 0 1 0 0 0 0 0 +5 3 4 +0 0 0 0 0 +0 0 0 0 0 +0 2 0 0 0 +2 32 2 0 0 +8 2 0 0 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index ab621c2..8129789 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,26 +1,6 @@ -25 -0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 I I 0 0 0 0 0 0 -0 W 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 0 H -0 0 W 0 I 0 0 0 0 0 H 0 0 0 I 0 0 0 0 0 0 0 W 0 0 -0 0 0 0 0 W 0 0 0 I 0 0 0 I 0 0 0 H H 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 -0 0 0 0 0 W 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 I 0 0 0 -0 0 0 0 I 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 -0 0 0 W 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 W 0 0 0 0 0 W 0 0 0 0 I 0 0 0 0 I W 0 0 -0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 I 0 0 I 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 W 0 0 0 0 0 0 0 0 I -0 0 0 0 0 0 0 I 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 I 0 0 -0 0 0 0 0 0 0 W 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 W -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 -0 0 0 W 0 0 I 0 0 0 0 0 0 0 H H 0 0 0 0 0 H 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 -0 0 0 I H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 0 -0 0 H 0 0 0 0 0 I 0 0 0 0 0 I 0 0 0 0 I 0 0 0 W 0 -0 0 0 0 0 0 0 0 0 H H 0 0 H 0 0 0 I 0 H 0 0 0 0 0 -0 W 0 0 0 0 0 0 0 0 I 0 0 0 H 0 W 0 0 0 0 0 0 0 0 +5 +0 0 0 0 0 +0 0 0 0 0 +0 0 0 0 0 +0 W 0 0 0 +0 0 0 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index a1f6f84..5c295ca 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - //makeGame(); + // makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 9926e4b..759a4e1 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -13,12 +13,23 @@ public boolean follows(Fact fact) { Clause clause = new Clause(); //Step 1: negate input fact fact.not = !fact.not; + clause.facts.add(fact); ArrayList tempClone = new ArrayList<>(kb.getClauses()); ArrayList kbClausesClone = new ArrayList<>(); for (Clause tempClause : tempClone) { kbClausesClone.add(new Clause(tempClause)); } + + //shortcut, check for direct contradiction to original fact + for(Clause factClause : kbClausesClone){ + if(factClause.facts.size() == 1){ + Fact holder = factClause.facts.get(0); + if(holder.predicate == fact.predicate && holder.not != fact.not && Unifier.equalWithSubs(holder, fact, new ArrayList())) + return true; + } + } + boolean keepGoing = true; //Step 2: Run negated facts against all known facts while (!kbClausesClone.isEmpty()) { diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index fa0ca2b..5b8e263 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -189,6 +189,10 @@ public boolean ask(Fact fact) { return inferenceEngine.follows(fact); } + + public void tell(Clause clause){ + addToClauses(clause); + } public void tell(Fact fact) { //If told DeadWumpus delete the wumpus entry at that position and add !Wumpus(x,y) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 548d78c..2c9655f 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -19,6 +19,8 @@ public LogicExplorer(World world, int startingArrows, int startingX, int startin kb.initializeRules(); this.searchedPositions = new boolean[World.size][World.size]; searchedPositions[location.x][location.y] = true; + percepts = world.getPercepts(); + processPercepts(); //initializeFrontier(); run(); } @@ -92,8 +94,9 @@ private void move(int action) { moveHistory.add(MOVE); System.out.println("Explorer Action: Moving"); percepts = (byte) world.action(MOVE); - searchedPositions[getForward().x][getForward().y] = true; - processPercepts(); + if(getForward().x >=0 && getForward().x =0 && getForward().y < World.size) + searchedPositions[getForward().x][getForward().y] = true; + processPercepts(); break; case TURN_LEFT: moveHistory.add(TURN_LEFT); @@ -135,10 +138,14 @@ public void removeFromFrontier(Location locToRemove) { private void processPercepts() { //there might still be an issue with wumpus death since its a seperate percept if ((percepts & DEATH_PIT) == DEATH_PIT || (percepts & DEATH_WUMPUS)!= 0) { System.out.println("Explorer died after moving"); - removeFromFrontier(getForward()); + Clause clause = new Clause(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null)); + clause.facts.add(new Fact("Pit",getForward().x,false,getForward().y,false,false,null,null)); + kb.tell(clause); + if(kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,true,null,null))) + removeFromFrontier(getForward()); //we want to keep it in frontier so we can kill wumpus moveHistory.remove(moveHistory.size() - 1);//why die again? } - if ((((percepts & BUMP) != BUMP) && (percepts & DEATH_PIT) != DEATH_PIT) && ((percepts & DEATH_WUMPUS) != DEATH_WUMPUS)) { + if ((((percepts & BUMP) != BUMP) && (percepts & DEATH_PIT) != DEATH_PIT) && ((percepts & DEATH_WUMPUS) != DEATH_WUMPUS) && ((percepts & SCREAM) == 0)) { updateLocation(); kb.tell(new Fact("Wumpus",location.x,false,location.y,false,true,null,null)); kb.tell(new Fact("Pit",location.x,false,location.y,false,true,null,null)); @@ -209,8 +216,10 @@ private void decideNextAction() { } else if (!frontier.isEmpty()) { rhwTraversal(frontier.get(frontier.size() - 1)); turnToSpace(frontier.get(frontier.size() - 1)); - frontier.remove(frontier.size() - 1); + //frontier.remove(frontier.size() - 1); move(MOVE); + if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) + frontier.remove(frontier.size() - 1); } } @@ -376,9 +385,9 @@ private boolean safeSpaceInFrontier() { if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, false, null, null))) { //there is specifically a wumpus, pit, or obsticle at this position, don't navigate to it. frontier.remove(i); - } else if (arrowCount == 0 && kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { - frontier.remove(i); - } + } //else if (arrowCount == 0 && kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { + //frontier.remove(i); + //} We want wumpus so we can kill it... } return false; } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 521c2ca..88322ce 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -242,8 +242,6 @@ public byte action(int action, boolean thing) { for (int i = y+1; i < perceptMap.length; i++) { if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); - score += 10; - killedWumpus++; return SCREAM; } else if ((perceptMap[x][i] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; @@ -322,5 +320,8 @@ private void removeWumpus(int x, int y) { perceptMap[x][y + 1] = (byte) (perceptMap[x][y + 1] & ~STENCH); } } + + killedWumpus++; + score+=10; } } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index d0431bb..a95b41e 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,26 +1,6 @@ -25 -0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 I I 0 0 0 0 0 0 -0 W 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 0 H -0 0 W 0 I 0 0 0 0 0 H 0 0 0 I 0 0 0 0 0 0 0 W 0 0 -0 0 0 0 0 W 0 0 0 I 0 0 0 I 0 0 0 H H 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 -0 0 0 0 0 W 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 I 0 0 0 -0 0 0 0 I 0 0 0 W 0 0 0 0 0 S 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 -0 0 0 W 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 W 0 0 0 0 0 W 0 0 0 0 I 0 0 0 0 I W 0 0 -0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 I 0 0 I 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 W 0 0 0 0 0 0 0 0 I -0 0 0 0 0 0 0 I 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 G 0 I 0 0 -0 0 0 0 0 0 0 W 0 0 0 0 0 0 I 0 0 0 0 0 0 0 0 0 W -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 -0 0 0 W 0 0 I 0 0 0 0 0 0 0 H H 0 0 0 0 0 H 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 -0 0 0 I H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 0 -0 0 H 0 0 0 0 0 I 0 0 0 0 0 I 0 0 0 0 I 0 0 0 W 0 -0 0 0 0 0 0 0 0 0 H H 0 0 H 0 0 0 I 0 H 0 0 0 0 0 -0 W 0 0 0 0 0 0 0 0 I 0 0 0 H 0 W 0 0 0 0 0 0 0 0 +5 +0 0 0 0 0 +0 0 0 0 0 +0 0 0 0 0 +0 W 0 0 S +G 0 0 0 0 From 57ba457e322a29a7a6d9e43e86e7ef5d6606571f Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 00:04:55 -0600 Subject: [PATCH 142/191] last push of the night I hope --- Wumpus/PerceptBoard.txt | 27 +++++++++++++++++++++------ Wumpus/clean.txt | 27 +++++++++++++++++++++------ Wumpus/src/Driver.java | 2 +- Wumpus/world.txt | 27 +++++++++++++++++++++------ 4 files changed, 64 insertions(+), 19 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 8e0b598..951460b 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,21 @@ -5 3 4 -0 0 0 0 0 -0 0 0 0 0 -0 2 0 0 0 -2 32 2 0 0 -8 2 0 0 0 +20 17 3 +0 3 17 17 1 1 17 1 1 0 0 0 0 1 1 17 17 3 33 18 +2 32 7 1 0 5 19 5 16 1 1 0 1 16 1 1 1 0 2 1 +0 2 0 2 8 2 33 19 1 1 16 1 0 1 16 1 0 0 1 2 +0 1 2 33 2 0 3 17 1 0 7 0 0 0 1 6 0 1 18 33 +1 19 33 18 1 4 0 1 0 6 32 2 0 0 2 34 6 0 5 6 +1 17 19 1 0 0 5 16 1 4 2 0 0 1 3 34 2 0 0 4 +1 17 5 4 0 2 0 1 0 0 0 2 5 17 17 3 0 4 0 0 +1 1 0 1 2 32 2 0 4 0 2 33 3 1 1 0 0 0 0 0 +16 1 1 16 1 6 0 0 0 6 1 19 17 5 0 0 0 0 0 0 +1 0 1 1 0 0 1 0 2 32 2 1 1 0 0 0 6 0 0 1 +0 1 16 1 0 1 16 1 0 2 0 0 0 0 0 2 32 6 1 16 +0 4 1 1 0 0 1 2 1 0 0 1 0 2 0 0 7 0 0 7 +0 0 1 16 1 0 3 33 18 5 1 16 3 32 2 1 16 1 3 32 +0 0 1 1 2 1 17 19 1 1 0 1 0 3 0 0 1 1 18 3 +0 1 16 3 32 2 1 1 1 16 1 0 1 18 1 0 2 2 33 2 +2 0 1 4 2 0 0 0 0 1 0 0 2 33 2 2 32 2 2 0 +32 2 5 0 4 0 0 0 3 16 1 0 0 2 4 1 2 0 0 0 +2 1 16 1 0 0 0 3 32 3 0 0 6 32 3 16 1 0 2 0 +0 4 5 0 2 4 1 16 3 32 2 0 0 2 0 1 16 3 32 2 +0 0 0 2 32 2 0 1 0 2 32 2 0 0 0 0 1 0 2 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 8129789..420f229 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,21 @@ -5 -0 0 0 0 0 -0 0 0 0 0 -0 0 0 0 0 -0 W 0 0 0 -0 0 0 0 0 +20 +0 0 H H 0 0 H 0 0 0 0 0 0 0 0 H H 0 W H +0 W I 0 0 I H I H 0 0 0 0 H 0 0 0 0 0 0 +0 0 0 0 0 0 W H 0 0 H 0 0 0 H 0 0 0 0 0 +0 0 0 W 0 0 0 H 0 0 I 0 0 0 0 I 0 0 H W +0 H W H 0 I 0 0 0 I W 0 0 0 0 W I 0 I I +0 H H 0 0 0 I H 0 I 0 0 0 0 0 W 0 0 0 I +0 H I I 0 0 0 0 0 0 0 0 I H H 0 0 I 0 0 +0 0 0 0 0 W 0 0 I 0 0 W 0 0 0 0 0 0 0 0 +H 0 0 H 0 I 0 0 0 I 0 H H I 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 I 0 0 0 +0 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 W I 0 H +0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 I +0 0 0 H 0 0 0 W H I 0 H 0 W 0 0 H 0 0 W +0 0 0 0 0 0 H H 0 0 0 0 0 0 0 0 0 0 H 0 +0 0 H 0 W 0 0 0 0 H 0 0 0 H 0 0 0 0 W 0 +0 0 0 I 0 0 0 0 0 0 0 0 0 W 0 0 W 0 0 0 +W 0 I 0 I 0 0 0 0 H 0 0 0 0 I 0 0 0 0 0 +0 0 H 0 0 0 0 0 W 0 0 0 I W 0 H 0 0 0 0 +0 I I 0 0 I 0 H 0 W 0 0 0 0 0 0 H 0 W 0 +0 0 0 0 W 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 5c295ca..d78d7fd 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index a95b41e..f093a20 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,21 @@ -5 -0 0 0 0 0 -0 0 0 0 0 -0 0 0 0 0 -0 W 0 0 S -G 0 0 0 0 +20 +0 0 H H 0 0 H 0 0 0 0 0 0 0 0 H H 0 W H +0 W I 0 0 I H I H 0 0 0 0 H 0 0 0 0 0 0 +0 0 0 0 G 0 W H 0 0 H 0 0 0 H 0 0 0 0 0 +0 0 0 W 0 0 0 H 0 0 I 0 0 0 0 I 0 0 H W +0 H W H 0 I 0 0 0 I W 0 0 0 0 W I 0 I I +0 H H 0 0 0 I H 0 I 0 0 0 0 0 W 0 0 0 I +0 H I I 0 0 0 0 0 0 0 0 I H H 0 0 I 0 0 +0 0 0 0 0 W 0 0 I 0 0 W 0 0 0 0 0 0 0 0 +H 0 0 H 0 I 0 0 0 I 0 H H I 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 I 0 0 0 +0 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 W I 0 H +0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 I +0 0 0 H 0 0 0 W H I 0 H 0 W 0 0 H 0 0 W +0 0 0 0 0 0 H H 0 0 0 0 0 0 0 0 0 0 H 0 +0 0 H 0 W 0 0 0 0 H 0 0 0 H 0 0 0 0 W 0 +0 0 0 I 0 0 0 0 0 0 0 0 0 W 0 0 W 0 0 0 +W 0 I 0 I 0 0 0 0 H 0 0 0 0 I 0 0 0 0 0 +0 0 H S 0 0 0 0 W 0 0 0 I W 0 H 0 0 0 0 +0 I I 0 0 I 0 H 0 W 0 0 0 0 0 0 H 0 W 0 +0 0 0 0 W 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 From 2d24e41a0c4d87e9852aeb4479254aee3067f699 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 09:35:43 -0600 Subject: [PATCH 143/191] quick fix --- Wumpus/PerceptBoard.txt | 27 ++++++-------------------- Wumpus/clean.txt | 27 ++++++-------------------- Wumpus/src/Driver.java | 2 +- Wumpus/src/KnowledgeBase.java | 36 ++++++++++++++++++++--------------- Wumpus/world.txt | 27 ++++++-------------------- 5 files changed, 40 insertions(+), 79 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 951460b..da42446 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,21 +1,6 @@ -20 17 3 -0 3 17 17 1 1 17 1 1 0 0 0 0 1 1 17 17 3 33 18 -2 32 7 1 0 5 19 5 16 1 1 0 1 16 1 1 1 0 2 1 -0 2 0 2 8 2 33 19 1 1 16 1 0 1 16 1 0 0 1 2 -0 1 2 33 2 0 3 17 1 0 7 0 0 0 1 6 0 1 18 33 -1 19 33 18 1 4 0 1 0 6 32 2 0 0 2 34 6 0 5 6 -1 17 19 1 0 0 5 16 1 4 2 0 0 1 3 34 2 0 0 4 -1 17 5 4 0 2 0 1 0 0 0 2 5 17 17 3 0 4 0 0 -1 1 0 1 2 32 2 0 4 0 2 33 3 1 1 0 0 0 0 0 -16 1 1 16 1 6 0 0 0 6 1 19 17 5 0 0 0 0 0 0 -1 0 1 1 0 0 1 0 2 32 2 1 1 0 0 0 6 0 0 1 -0 1 16 1 0 1 16 1 0 2 0 0 0 0 0 2 32 6 1 16 -0 4 1 1 0 0 1 2 1 0 0 1 0 2 0 0 7 0 0 7 -0 0 1 16 1 0 3 33 18 5 1 16 3 32 2 1 16 1 3 32 -0 0 1 1 2 1 17 19 1 1 0 1 0 3 0 0 1 1 18 3 -0 1 16 3 32 2 1 1 1 16 1 0 1 18 1 0 2 2 33 2 -2 0 1 4 2 0 0 0 0 1 0 0 2 33 2 2 32 2 2 0 -32 2 5 0 4 0 0 0 3 16 1 0 0 2 4 1 2 0 0 0 -2 1 16 1 0 0 0 3 32 3 0 0 6 32 3 16 1 0 2 0 -0 4 5 0 2 4 1 16 3 32 2 0 0 2 0 1 16 3 32 2 -0 0 0 2 32 2 0 1 0 2 32 2 0 0 0 0 1 0 2 0 +5 1 2 +0 10 34 34 6 +4 0 2 34 2 +6 2 32 2 0 +32 2 2 0 4 +2 0 0 4 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 420f229..79827a0 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,21 +1,6 @@ -20 -0 0 H H 0 0 H 0 0 0 0 0 0 0 0 H H 0 W H -0 W I 0 0 I H I H 0 0 0 0 H 0 0 0 0 0 0 -0 0 0 0 0 0 W H 0 0 H 0 0 0 H 0 0 0 0 0 -0 0 0 W 0 0 0 H 0 0 I 0 0 0 0 I 0 0 H W -0 H W H 0 I 0 0 0 I W 0 0 0 0 W I 0 I I -0 H H 0 0 0 I H 0 I 0 0 0 0 0 W 0 0 0 I -0 H I I 0 0 0 0 0 0 0 0 I H H 0 0 I 0 0 -0 0 0 0 0 W 0 0 I 0 0 W 0 0 0 0 0 0 0 0 -H 0 0 H 0 I 0 0 0 I 0 H H I 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 I 0 0 0 -0 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 W I 0 H -0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 I -0 0 0 H 0 0 0 W H I 0 H 0 W 0 0 H 0 0 W -0 0 0 0 0 0 H H 0 0 0 0 0 0 0 0 0 0 H 0 -0 0 H 0 W 0 0 0 0 H 0 0 0 H 0 0 0 0 W 0 -0 0 0 I 0 0 0 0 0 0 0 0 0 W 0 0 W 0 0 0 -W 0 I 0 I 0 0 0 0 H 0 0 0 0 I 0 0 0 0 0 -0 0 H 0 0 0 0 0 W 0 0 0 I W 0 H 0 0 0 0 -0 I I 0 0 I 0 H 0 W 0 0 0 0 0 0 H 0 W 0 -0 0 0 0 W 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 +5 +0 0 W W I +I 0 0 W 0 +I 0 W 0 0 +W 0 0 0 I +0 0 0 I 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index d78d7fd..b0d7f16 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + // makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 5b8e263..77a7c06 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -197,21 +197,8 @@ public void tell(Clause clause){ public void tell(Fact fact) { //If told DeadWumpus delete the wumpus entry at that position and add !Wumpus(x,y) if(fact.predicate.equals("DeadWumpus")){ - for(int i = 0; i < clauses.size(); i++){ - for(int j = 0; j < clauses.get(i).facts.size(); j++){ - if(clauses.get(i).facts.get(j).variables.get(0).value == fact.variables.get(0).value && clauses.get(i).facts.get(j).variables.get(1).value == fact.variables.get(1).value){ - - if(clauses.get(i).facts.get(j).predicate.equals("Wumpus")){ - clauses.get(i).facts.get(j).not = true; - } - else if(clauses.get(i).facts.get(j).predicate.equals("Stench")){ - clauses.get(i).facts.remove(j); - if(clauses.get(i).facts.size() == 0) - clauses.remove(i); - } - } - } - } + removeWumpusAndStench(fact); + } if(factInClauses(fact)){ return; @@ -220,6 +207,25 @@ else if(clauses.get(i).facts.get(j).predicate.equals("Stench")){ addToClauses(new Clause(fact)); //and check if there is a stench at any adjacent position, remove those facts too } + + private void removeWumpusAndStench(Fact fact){ + for(int i = clauses.size()-1; i >= 0; i--){ + if(clauses.get(i).facts.size()==1){ + Fact toRemove = clauses.get(i).facts.get(0); + if(toRemove.predicate.equals("Wumpus")){ + + if(toRemove.variables.get(0).value == fact.variables.get(0).value && toRemove.variables.get(1).value == fact.variables.get(1).value){ + toRemove.not = true; + } + } + else if(toRemove.predicate.equals("Stench")){ + if((toRemove.variables.get(0).value == fact.variables.get(0).value && Math.abs(toRemove.variables.get(1).value - fact.variables.get(1).value) == 1)||(toRemove.variables.get(1).value == fact.variables.get(1).value && Math.abs(toRemove.variables.get(0).value - fact.variables.get(0).value) == 1)){ + clauses.remove(i); + } + } + } + } + } private boolean factInClauses(Fact fact){ for(Clause clause : clauses){ if(clause.facts.size() == 1){ diff --git a/Wumpus/world.txt b/Wumpus/world.txt index f093a20..ff0fc47 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,21 +1,6 @@ -20 -0 0 H H 0 0 H 0 0 0 0 0 0 0 0 H H 0 W H -0 W I 0 0 I H I H 0 0 0 0 H 0 0 0 0 0 0 -0 0 0 0 G 0 W H 0 0 H 0 0 0 H 0 0 0 0 0 -0 0 0 W 0 0 0 H 0 0 I 0 0 0 0 I 0 0 H W -0 H W H 0 I 0 0 0 I W 0 0 0 0 W I 0 I I -0 H H 0 0 0 I H 0 I 0 0 0 0 0 W 0 0 0 I -0 H I I 0 0 0 0 0 0 0 0 I H H 0 0 I 0 0 -0 0 0 0 0 W 0 0 I 0 0 W 0 0 0 0 0 0 0 0 -H 0 0 H 0 I 0 0 0 I 0 H H I 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 I 0 0 0 -0 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 W I 0 H -0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 I -0 0 0 H 0 0 0 W H I 0 H 0 W 0 0 H 0 0 W -0 0 0 0 0 0 H H 0 0 0 0 0 0 0 0 0 0 H 0 -0 0 H 0 W 0 0 0 0 H 0 0 0 H 0 0 0 0 W 0 -0 0 0 I 0 0 0 0 0 0 0 0 0 W 0 0 W 0 0 0 -W 0 I 0 I 0 0 0 0 H 0 0 0 0 I 0 0 0 0 0 -0 0 H S 0 0 0 0 W 0 0 0 I W 0 H 0 0 0 0 -0 I I 0 0 I 0 H 0 W 0 0 0 0 0 0 H 0 W 0 -0 0 0 0 W 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 +5 +0 G W W I +I 0 S W 0 +I 0 W 0 0 +W 0 0 0 I +0 0 0 I 0 From 5888b6fea5bf7d2e9ff09787e7b41cff30f80892 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 09:43:21 -0600 Subject: [PATCH 144/191] fixed wumpus death stuff --- Wumpus/src/World.java | 56 +++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 88322ce..8cd60e0 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -17,10 +17,11 @@ public World(String fileName) { importMap(fileName); for (int i = 0; i < perceptMap.length; i++) { for (int j = 0; j < perceptMap.length; j++) { - if((perceptMap[i][j] & DEATH_WUMPUS) != 0) + if ((perceptMap[i][j] & DEATH_WUMPUS) != 0) { arrowCount++; + } } - + } System.out.println("Starting world:"); printWorld(); @@ -41,10 +42,10 @@ public void startGame(String id) { public void importMap(String fileName) { try { - + FileReader in1 = new FileReader(fileName); BufferedReader reader1 = new BufferedReader(in1); - + FileReader in = new FileReader(fileName); BufferedReader reader = new BufferedReader(in); String next = reader.readLine(); @@ -239,7 +240,7 @@ public byte action(int action, boolean thing) { score -= 10; switch (direction) { case NORTH: //shoot north - for (int i = y+1; i < perceptMap.length; i++) { + for (int i = y + 1; i < perceptMap.length; i++) { if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); return SCREAM; @@ -250,7 +251,7 @@ public byte action(int action, boolean thing) { return perceptMap[x][y]; case EAST: //shoot east - for (int i = x+1; i < perceptMap.length; i++) { + for (int i = x + 1; i < perceptMap.length; i++) { if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; @@ -261,7 +262,7 @@ public byte action(int action, boolean thing) { return perceptMap[x][y]; case SOUTH: //shoot south - for (int i = y-1; i >= 0; i--) { + for (int i = y - 1; i >= 0; i--) { if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); return SCREAM; @@ -272,7 +273,7 @@ public byte action(int action, boolean thing) { return perceptMap[x][y]; case WEST: //shoot west - for (int i = x-1; i > 0; i--) { + for (int i = x - 1; i > 0; i--) { if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; @@ -307,21 +308,40 @@ private void removeWumpus(int x, int y) { if (x > 0) { //remove stentch to left perceptMap[x - 1][y] = (byte) (perceptMap[x - 1][y] & ~STENCH); - if (x < size - 1) { - //remove stentch to right - perceptMap[x + 1][y] = (byte) (perceptMap[x + 1][y] & ~STENCH); - } + } + + if (x < size - 1) { + //remove stentch to right + perceptMap[x + 1][y] = (byte) (perceptMap[x + 1][y] & ~STENCH); } if (y > 0) { //remove stentch below perceptMap[x][y - 1] = (byte) (perceptMap[x][y - 1] & ~STENCH); - if (y < size - 1) { - //remove stentch above - perceptMap[x][y + 1] = (byte) (perceptMap[x][y + 1] & ~STENCH); - } } - + if (y < size - 1) { + //remove stentch above + perceptMap[x][y + 1] = (byte) (perceptMap[x][y + 1] & ~STENCH); + } + remakeStenches(); killedWumpus++; - score+=10; + score += 10; + } + + public void remakeStenches(){ + for (int i = 0; i < perceptMap.length; i++) { + for (int j = 0; j < perceptMap.length; j++) { + if((perceptMap[i][j] & DEATH_WUMPUS) != 0){ + if(i>0){ + perceptMap[i-1][j] |= STENCH; + } + if(i < size-1) + perceptMap[i+1][j] |= STENCH; + if(j > 0) + perceptMap[i][j-1] |= STENCH; + if(j < size-1) + perceptMap[i][j+1] |= STENCH; + } + } + } } } From e47146d0a92a959312d59a222b1cf3d719169c3e Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 11:45:24 -0600 Subject: [PATCH 145/191] Updates to movement, still a problem with shooting --- Wumpus/src/LogicExplorer.java | 163 +++++++++++++++++++++++----------- 1 file changed, 109 insertions(+), 54 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 2c9655f..5980f0e 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -50,13 +50,15 @@ public void updateLocation() { expandFrontier(); } - public void addToFrontier(Location loc){ - if(!searchedPositions[loc.x][loc.y]){ - if(inFrontier(loc)){ - removeFromFrontier(loc); + public void addToFrontier(Location loc) { + if (!searchedPositions[loc.x][loc.y]) { + if (inFrontier(loc)) { + removeFromFrontier(loc); + } + frontier.add(loc); } - frontier.add(loc);} } + public void expandFrontier() { if (location.x > 0 && !searchedPositions[location.x - 1][location.y]) { addToFrontier(new Location(location.x - 1, location.y)); @@ -75,9 +77,10 @@ public void expandFrontier() { private void run() { while (true) { - if(!notFirstMove){ - percepts = world.getPercepts(); - processPercepts();} + if (!notFirstMove) { + percepts = world.getPercepts(); + processPercepts(); + } decideNextAction(); } } @@ -94,8 +97,9 @@ private void move(int action) { moveHistory.add(MOVE); System.out.println("Explorer Action: Moving"); percepts = (byte) world.action(MOVE); - if(getForward().x >=0 && getForward().x =0 && getForward().y < World.size) + if (getForward().x >= 0 && getForward().x < World.size && getForward().y >= 0 && getForward().y < World.size) { searchedPositions[getForward().x][getForward().y] = true; + } processPercepts(); break; case TURN_LEFT: @@ -136,19 +140,20 @@ public void removeFromFrontier(Location locToRemove) { } private void processPercepts() { //there might still be an issue with wumpus death since its a seperate percept - if ((percepts & DEATH_PIT) == DEATH_PIT || (percepts & DEATH_WUMPUS)!= 0) { + if ((percepts & DEATH_PIT) == DEATH_PIT || (percepts & DEATH_WUMPUS) != 0) { System.out.println("Explorer died after moving"); - Clause clause = new Clause(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null)); - clause.facts.add(new Fact("Pit",getForward().x,false,getForward().y,false,false,null,null)); + Clause clause = new Clause(new Fact("Wumpus", getForward().x, false, getForward().y, false, false, null, null)); + clause.facts.add(new Fact("Pit", getForward().x, false, getForward().y, false, false, null, null)); kb.tell(clause); - if(kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,true,null,null))) + if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, true, null, null))) { removeFromFrontier(getForward()); //we want to keep it in frontier so we can kill wumpus + } moveHistory.remove(moveHistory.size() - 1);//why die again? } if ((((percepts & BUMP) != BUMP) && (percepts & DEATH_PIT) != DEATH_PIT) && ((percepts & DEATH_WUMPUS) != DEATH_WUMPUS) && ((percepts & SCREAM) == 0)) { updateLocation(); - kb.tell(new Fact("Wumpus",location.x,false,location.y,false,true,null,null)); - kb.tell(new Fact("Pit",location.x,false,location.y,false,true,null,null)); + kb.tell(new Fact("Wumpus", location.x, false, location.y, false, true, null, null)); + kb.tell(new Fact("Pit", location.x, false, location.y, false, true, null, null)); removeFromFrontier(location); } if ((percepts & BUMP) == BUMP) { //theres soemthing funky here, hes bumping when there arent obsticales @@ -156,59 +161,63 @@ private void processPercepts() { //there might still be an issue with wum moveHistory.remove(moveHistory.size() - 1);//why bump again? removeFromFrontier(getForward()); kb.tell(new Fact("Obsticle", getForward().x, false, getForward().y, false, false, null, null)); + } else if ((percepts & DEATH_PIT) == 0 && (percepts & DEATH_WUMPUS) == 0) {//you if you bump than the only inputted percept should've been bump + if ((percepts & STENCH) != 0) { + kb.tell(new Fact("Stench", location.x, false, location.y, false, false, null, null)); + } else { + kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); + } + if ((percepts & BREEZE) != 0) { + kb.tell(new Fact("Breeze", location.x, false, location.y, false, false, null, null)); + } else { + kb.tell(new Fact("Breeze", location.x, false, location.y, false, true, null, null)); + } + if ((percepts & SCREAM) != 0) { + kb.tell(new Fact("DeadWumpus", getForward().x, false, getForward().y, false, false, null, null)); + } } - else if((percepts & DEATH_PIT) == 0 && (percepts & DEATH_WUMPUS)==0){//you if you bump than the only inputted percept should've been bump - if ((percepts & STENCH) != 0) { - kb.tell(new Fact("Stench", location.x, false, location.y, false, false, null, null)); - } else { - kb.tell(new Fact("Stench", location.x, false, location.y, false, true, null, null)); - } - if ((percepts & BREEZE) != 0) { - kb.tell(new Fact("Breeze", location.x, false, location.y, false, false, null, null)); - } else { - kb.tell(new Fact("Breeze", location.x, false, location.y, false, true, null, null)); - } - if ((percepts & SCREAM) != 0) { - kb.tell(new Fact("DeadWumpus", getForward().x, false, getForward().y, false, false, null, null)); - }} } - private boolean inFrontier(Location loc){ - for(Location location : frontier){ - if(location.x == loc.x && location.y == loc.y) + private boolean inFrontier(Location loc) { + for (Location location : frontier) { + if (location.x == loc.x && location.y == loc.y) { return true; + } } return false; } + private void decideNextAction() { if ((percepts & GLITTER) != 0) { move(GRAB); } - + if (frontier.isEmpty()) { move(World.QUIT); } if (getForward().x >= 0 && getForward().x < World.size && getForward().y >= 0 && getForward().y < World.size) { if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, true, null, null))) { if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { - if(inFrontier(getForward())){ + if (inFrontier(getForward())) { move(MOVE); removeFromFrontier(getForward()); return; } - + } } } if (safeSpaceInFrontier()) { - if(!adjacent(safeSpace)) + if (!adjacent(safeSpace)) { rhwTraversal(safeSpace); + } turnToSpace(safeSpace); move(MOVE); removeFromFrontier(safeSpace); } else if (arrowCount > 0 && wumpusInFrontier()) { - if(!adjacent(wumpusSpace)) + if (!adjacent(wumpusSpace)) { rhwTraversal(wumpusSpace); + } turnToSpace(wumpusSpace); move(SHOOT); move(MOVE); @@ -218,8 +227,9 @@ private void decideNextAction() { turnToSpace(frontier.get(frontier.size() - 1)); //frontier.remove(frontier.size() - 1); move(MOVE); - if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) + if (!kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, false, null, null))) { frontier.remove(frontier.size() - 1); + } } } @@ -350,9 +360,9 @@ private void moveHistoryTraversal(Location loc) { private void rhwTraversal(Location location) { moveHistoryTraversal(location); - //if (!this.location.equals(location) && !adjacent(location)) { - // goTo(location.x, location.y); - //} +// if (!this.location.equals(location) && !adjacent(location)) { +// goTo(location.x, location.y); +// } //go to location zach NOOOO! } @@ -377,16 +387,16 @@ private boolean safeSpaceInFrontier() { if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, true, null, null))) { if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, true, null, null))) { //hopefully no obsticle is in frontier as it should be removed when found... - safeSpace = new Location(loc.x, loc.y); - return true; - + safeSpace = new Location(loc.x, loc.y); + return true; + } } if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, false, null, null))) { //there is specifically a wumpus, pit, or obsticle at this position, don't navigate to it. frontier.remove(i); } //else if (arrowCount == 0 && kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { - //frontier.remove(i); + //frontier.remove(i); //} We want wumpus so we can kill it... } return false; @@ -394,14 +404,20 @@ private boolean safeSpaceInFrontier() { private void goTo(int x, int y) { + System.out.println("Traversing to " + x + ", " + y); ArrayList path = new ArrayList<>(); path = searchForPath(location.x, location.y, x, y, path); + path.add(new Location(x, y)); + printPath(path); traversePath(path); + System.out.println("Done traversing to " + x + ", " + y); + System.out.println("Actual location: " + this.location.x + ", " + this.location.y); } private ArrayList searchForPath(int curX, int curY, int goalX, int goalY, ArrayList path) { - if (searchNext(curX, curY, goalX, goalY, path)) { + boolean[][] traversed = new boolean[World.size][World.size]; + if (searchNext(curX, curY, goalX, goalY, path, traversed)) { return path; } else { System.out.println("Path finding error, no path found."); @@ -409,25 +425,49 @@ private ArrayList searchForPath(int curX, int curY, int goalX, int goa } } - private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path) { + private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path, boolean[][] traversed) { - path.add(new Location(curX, curY)); + Location curLoc = new Location(curX, curY); + if (!this.location.equals(curLoc)) { + path.add(curLoc); + } + printPath(path); + traversed[curX][curY] = true; if (curX == goalX && curY == goalY) { return true; } - if (isValid(curX, curY + 1)) { //north is valid - return searchNext(curX, curY + 1, goalX, goalY, path); + if (checkAdjacent(curX, curY, goalX, goalY)) { + return true; + } + if (isValid(curX, curY + 1) && !traversed[curX][curY + 1]) { //north is valid + return searchNext(curX, curY + 1, goalX, goalY, path, traversed); + } + if (isValid(curX + 1, curY) && !traversed[curX + 1][curY]) { //east is valid + return searchNext(curX + 1, curY, goalX, goalY, path, traversed); } - if (isValid(curX + 1, curY)) { //east is valid - return searchNext(curX + 1, curY, goalX, goalY, path); + if (isValid(curX, curY - 1) && !traversed[curX][curY - 1]) { //south is valid + return searchNext(curX, curY - 1, goalX, goalY, path, traversed); } - if (isValid(curX - 1, curY)) { //west is valid - return searchNext(curX + 1, curY, goalX, goalY, path); + if (isValid(curX - 1, curY) && !traversed[curX - 1][curY]) { //west is valid + return searchNext(curX - 1, curY, goalX, goalY, path, traversed); } path.remove(path.size() - 1); return false; } + private void printPath(ArrayList path) { + if (!path.isEmpty()) { + System.out.print("Path: "); + for (Location l : path) { + System.out.print("[" + l.x + ", " + l.y + "] "); + } + System.out.println(); + } else { + System.out.println("Path: [ ]"); + } + + } + private boolean isValid(int x, int y) { if (x >= 0 && y >= 0 && x < World.size && y < World.size) { @@ -437,6 +477,21 @@ private boolean isValid(int x, int y) { } } + private boolean checkAdjacent(int curX, int curY, int goalX, int goalY) { + + if (curX == goalX) { + if (Math.abs(curY - goalY) == 1) { + return true; + } + } + if (curY == goalY) { + if (Math.abs(curX - goalX) == 1) { + return true; + } + } + return false; + } + private void traversePath(ArrayList path) { while (path.size() > 0) { From 21792ed27b547188ffc77a832945b9bf87bec889 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 11:58:16 -0600 Subject: [PATCH 146/191] Fixed wumpus slaying issue on border conditions, path traversal working? --- Wumpus/src/LogicExplorer.java | 8 ++++---- Wumpus/src/World.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 5980f0e..5f7577b 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -359,10 +359,10 @@ private void moveHistoryTraversal(Location loc) { } private void rhwTraversal(Location location) { - moveHistoryTraversal(location); -// if (!this.location.equals(location) && !adjacent(location)) { -// goTo(location.x, location.y); -// } + // moveHistoryTraversal(location); + if (!this.location.equals(location) && !adjacent(location)) { + goTo(location.x, location.y); + } //go to location zach NOOOO! } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 8cd60e0..adb24b0 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -273,7 +273,7 @@ public byte action(int action, boolean thing) { return perceptMap[x][y]; case WEST: //shoot west - for (int i = x - 1; i > 0; i--) { + for (int i = x - 1; i >= 0; i--) { if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; From 9dc5848823abd791e5a83ebe70ef1fab9e0335c3 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 11:59:56 -0600 Subject: [PATCH 147/191] small fix --- Wumpus/src/LogicExplorer.java | 6 ++++-- Wumpus/src/World.java | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 2c9655f..5d9dad5 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -218,8 +218,10 @@ private void decideNextAction() { turnToSpace(frontier.get(frontier.size() - 1)); //frontier.remove(frontier.size() - 1); move(MOVE); - if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) - frontier.remove(frontier.size() - 1); + //if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) +// frontier.remove(frontier.size() - 1); + if(kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) + addToFrontier(getForward());//since there is definitely a wumpus forward, put in frontier so we can kill it later maybe. } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 8cd60e0..adb24b0 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -273,7 +273,7 @@ public byte action(int action, boolean thing) { return perceptMap[x][y]; case WEST: //shoot west - for (int i = x - 1; i > 0; i--) { + for (int i = x - 1; i >= 0; i--) { if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; From 4aba24daf7547691d4669f892cbebc454f4978a1 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 13:09:38 -0600 Subject: [PATCH 148/191] Is it so? --- Wumpus/src/LogicExplorer.java | 185 ++++++++++++++++++---------------- Wumpus/src/World.java | 2 +- 2 files changed, 97 insertions(+), 90 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 1c4706f..39e13c2 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -29,17 +29,18 @@ public void initializeFrontier() { if (location.x > 0) { frontier.add(new Location(location.x - 1, location.y)); } - if (location.x < world.size - 1) { + if (location.x < World.size - 1) { frontier.add(new Location(location.x + 1, location.y)); } if (location.y > 0) { frontier.add(new Location(location.x, location.y - 1)); } - if (location.y < world.size - 1) { + if (location.y < World.size - 1) { frontier.add(new Location(location.x, location.y + 1)); } } + @Override public void updateLocation() { if (notFirstMove) { super.updateLocation(); @@ -63,13 +64,13 @@ public void expandFrontier() { if (location.x > 0 && !searchedPositions[location.x - 1][location.y]) { addToFrontier(new Location(location.x - 1, location.y)); } - if (location.x < world.size - 1 && !searchedPositions[location.x + 1][location.y]) { + if (location.x < World.size - 1 && !searchedPositions[location.x + 1][location.y]) { addToFrontier(new Location(location.x + 1, location.y)); } if (location.y > 0 && !searchedPositions[location.x][location.y - 1]) { addToFrontier(new Location(location.x, location.y - 1)); } - if (location.y < world.size - 1 && !searchedPositions[location.x][location.y + 1]) { + if (location.y < World.size - 1 && !searchedPositions[location.x][location.y + 1]) { addToFrontier(new Location(location.x, location.y + 1)); } } @@ -209,29 +210,29 @@ private void decideNextAction() { } if (safeSpaceInFrontier()) { if (!adjacent(safeSpace)) { - rhwTraversal(safeSpace); + goTo(safeSpace); } turnToSpace(safeSpace); move(MOVE); removeFromFrontier(safeSpace); } else if (arrowCount > 0 && wumpusInFrontier()) { if (!adjacent(wumpusSpace)) { - rhwTraversal(wumpusSpace); + goTo(wumpusSpace); } turnToSpace(wumpusSpace); move(SHOOT); move(MOVE); removeFromFrontier(wumpusSpace); } else if (!frontier.isEmpty()) { - rhwTraversal(frontier.get(frontier.size() - 1)); + goTo(frontier.get(frontier.size() - 1)); turnToSpace(frontier.get(frontier.size() - 1)); //frontier.remove(frontier.size() - 1); move(MOVE); //if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) // frontier.remove(frontier.size() - 1); - if(kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) + if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, false, null, null))) { addToFrontier(getForward());//since there is definitely a wumpus forward, put in frontier so we can kill it later maybe. - + } } } @@ -306,21 +307,20 @@ public void turnToSpace(Location loc) { } } - private Location neighborSafeSpace(Location location) { - Location loc = null; - if (location.x > 0 && searchedPositions[location.x - 1][location.y]) { - loc = new Location(location.x - 1, location.y); - - } else if (location.x < World.size - 1 && searchedPositions[location.x + 1][location.y]) { - loc = new Location(location.x + 1, location.y); - } else if (location.y > 0 && searchedPositions[location.x][location.y - 1]) { - loc = new Location(location.x, location.y - 1); - } else if (location.y < World.size - 1 && searchedPositions[location.x][location.y + 1]) { - loc = new Location(location.x, location.y + 1); - } - return loc; - } - +// private Location neighborSafeSpace(Location location) { +// Location loc = null; +// if (location.x > 0 && searchedPositions[location.x - 1][location.y]) { +// loc = new Location(location.x - 1, location.y); +// +// } else if (location.x < World.size - 1 && searchedPositions[location.x + 1][location.y]) { +// loc = new Location(location.x + 1, location.y); +// } else if (location.y > 0 && searchedPositions[location.x][location.y - 1]) { +// loc = new Location(location.x, location.y - 1); +// } else if (location.y < World.size - 1 && searchedPositions[location.x][location.y + 1]) { +// loc = new Location(location.x, location.y + 1); +// } +// return loc; +// } private boolean wumpusInFrontier() { for (Location loc : frontier) { if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { @@ -331,58 +331,41 @@ private boolean wumpusInFrontier() { return false; } - private void moveHistoryTraversal(Location loc) { - if (adjacent(loc)) { - return; - } - for (int i = moveHistory.size() - 1; i >= 0; i--) { - if (adjacent(loc)) { - return; - } - int move = moveHistory.get(i); - switch (move) { - case MOVE: - move(TURN_LEFT); - move(TURN_LEFT); - move(MOVE); - move(TURN_LEFT); - move(TURN_LEFT); - break; - case TURN_LEFT: - move(TURN_RIGHT); - break; - case TURN_RIGHT: - move(TURN_LEFT); - break; - default: - break; - } - } - } - - private void rhwTraversal(Location location) { - // moveHistoryTraversal(location); - if (!this.location.equals(location) && !adjacent(location)) { - goTo(location.x, location.y); - } - //go to location zach NOOOO! - } - - private boolean adjacent(Location location) { - - if (location.x == this.location.x) { - if (Math.abs(location.y - this.location.y) == 1) { - return true; - } - } - if (location.y == this.location.y) { - if (Math.abs(location.x - this.location.x) == 1) { - return true; - } - } - return false; - } - +// private void moveHistoryTraversal(Location loc) { +// if (adjacent(loc)) { +// return; +// } +// for (int i = moveHistory.size() - 1; i >= 0; i--) { +// if (adjacent(loc)) { +// return; +// } +// int move = moveHistory.get(i); +// switch (move) { +// case MOVE: +// move(TURN_LEFT); +// move(TURN_LEFT); +// move(MOVE); +// move(TURN_LEFT); +// move(TURN_LEFT); +// break; +// case TURN_LEFT: +// move(TURN_RIGHT); +// break; +// case TURN_RIGHT: +// move(TURN_LEFT); +// break; +// default: +// break; +// } +// } +// } +// private void rhwTraversal(Location location) { +// // moveHistoryTraversal(location); +// //if (!this.location.equals(location) && !adjacent(location)) { +// goTo(location.x, location.y); +// //} +// //go to location zach NOOOO! +// } private boolean safeSpaceInFrontier() { for (int i = frontier.size() - 1; i >= 0; i--) { Location loc = frontier.get(i); @@ -404,16 +387,19 @@ private boolean safeSpaceInFrontier() { return false; } - private void goTo(int x, int y) { + private void goTo(Location target) { - System.out.println("Traversing to " + x + ", " + y); - ArrayList path = new ArrayList<>(); - path = searchForPath(location.x, location.y, x, y, path); - path.add(new Location(x, y)); - printPath(path); - traversePath(path); - System.out.println("Done traversing to " + x + ", " + y); - System.out.println("Actual location: " + this.location.x + ", " + this.location.y); + if (!adjacent(target)) { + + System.out.println("Traversing to " + target.x + ", " + target.y); + ArrayList path = new ArrayList<>(); + path = searchForPath(location.x, location.y, target.x, target.y, path); + //path.add(new Location(target.x, y)); + printPath(path); + traversePath(path); + System.out.println("Done traversing to " + target.x + ", " + target.y); + System.out.println("Actual location: " + this.location.x + ", " + this.location.y); + } } private ArrayList searchForPath(int curX, int curY, int goalX, int goalY, ArrayList path) { @@ -435,9 +421,9 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path) { private boolean isValid(int x, int y) { if (x >= 0 && y >= 0 && x < World.size && y < World.size) { - return searchedPositions[x][y]; + boolean wumpus = !kb.ask(new Fact("Wumpus", x, false, y, false, true, null, null)); + boolean pit = !kb.ask(new Fact("Pit", x, false, y, false, true, null, null)); + boolean bump = kb.ask(new Fact("Obsticle", x, false, y, false, false, null, null)); + // boolean bump = kb.ask(new Fact("Obsticle", x, false, y, false, false, null, null)) + //System.out.println("Pit: " + pit + " Wumpus: " + wumpus + " Obsticle: " + bump); + return searchedPositions[x][y] && !bump && !pit && !wumpus; } else { return false; } @@ -494,6 +486,21 @@ private boolean checkAdjacent(int curX, int curY, int goalX, int goalY) { return false; } + private boolean adjacent(Location location) { + + if (location.x == this.location.x) { + if (Math.abs(location.y - this.location.y) == 1) { + return true; + } + } + if (location.y == this.location.y) { + if (Math.abs(location.x - this.location.x) == 1) { + return true; + } + } + return false; + } + private void traversePath(ArrayList path) { while (path.size() > 0) { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index adb24b0..87adc10 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -89,7 +89,7 @@ public void printWorld() { for (int i = perceptMap.length - 1; i >= 0; i--) { for (int j = 0; j < perceptMap.length; j++) { if (x == j && y == i) { - System.out.print("A "); + System.out.print("\u001B[31m" + "A " + "\u001B[0m"); } else { if ((perceptMap[j][i] & DEATH_WUMPUS) == DEATH_WUMPUS) { System.out.print("W "); From f9a0a453f548d1b6d4ca10e1ddf15bae29e68e6c Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 13:14:23 -0600 Subject: [PATCH 149/191] tweaks --- Wumpus/PerceptBoard.txt | 17 +++++++---- Wumpus/clean.txt | 17 +++++++---- Wumpus/src/Clause.java | 12 ++++---- Wumpus/src/Driver.java | 2 +- Wumpus/src/InferenceEngine.java | 52 +++++++++++++++++---------------- Wumpus/src/KnowledgeBase.java | 8 ++--- Wumpus/src/LogicExplorer.java | 6 +++- Wumpus/src/Rule.java | 52 ++++++++++++++++----------------- Wumpus/world.txt | 17 +++++++---- 9 files changed, 102 insertions(+), 81 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index da42446..64ebfff 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,11 @@ -5 1 2 -0 10 34 34 6 -4 0 2 34 2 -6 2 32 2 0 -32 2 2 0 4 -2 0 0 4 0 +10 3 9 +0 2 32 2 2 0 0 0 0 0 +0 0 2 2 32 2 1 0 2 0 +4 0 6 0 2 1 16 3 34 2 +0 2 32 3 0 2 1 2 34 2 +0 0 3 16 3 33 2 0 2 0 +0 0 0 1 1 18 1 0 1 2 +2 0 0 0 0 1 2 1 18 33 +32 2 0 0 0 2 32 2 5 2 +2 32 10 0 0 0 2 4 0 2 +0 2 0 0 0 0 0 0 2 32 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 79827a0..154ae7a 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,11 @@ -5 -0 0 W W I -I 0 0 W 0 -I 0 W 0 0 -W 0 0 0 I -0 0 0 I 0 +10 +0 0 W 0 0 0 0 0 0 0 +0 0 0 0 W 0 0 0 0 0 +I 0 I 0 0 0 H 0 W 0 +0 0 W 0 0 0 0 0 W 0 +0 0 0 H 0 W 0 0 0 0 +0 0 0 0 0 H 0 0 0 0 +0 0 0 0 0 0 0 0 H W +W 0 0 0 0 0 W 0 I 0 +0 W 0 0 0 0 0 I 0 0 +0 0 0 0 0 0 0 0 0 W diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 8266d31..528083d 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -22,12 +22,12 @@ public Clause(Fact fact) { } public static void printClause(Clause clause){ - for(Fact fact : clause.facts){ - fact.printFact(); - if(fact != clause.facts.get(clause.facts.size()-1)) - System.out.print(" v "); - } - System.out.println(""); +// for(Fact fact : clause.facts){ +// fact.printFact(); +// if(fact != clause.facts.get(clause.facts.size()-1)) +// System.out.print(" v "); +// } +// System.out.println(""); } @Override diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index b0d7f16..d473b36 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + // makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 759a4e1..2fe9b2d 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -13,23 +13,24 @@ public boolean follows(Fact fact) { Clause clause = new Clause(); //Step 1: negate input fact fact.not = !fact.not; - + clause.facts.add(fact); ArrayList tempClone = new ArrayList<>(kb.getClauses()); ArrayList kbClausesClone = new ArrayList<>(); for (Clause tempClause : tempClone) { kbClausesClone.add(new Clause(tempClause)); } - + //shortcut, check for direct contradiction to original fact - for(Clause factClause : kbClausesClone){ - if(factClause.facts.size() == 1){ + for (Clause factClause : kbClausesClone) { + if (factClause.facts.size() == 1) { Fact holder = factClause.facts.get(0); - if(holder.predicate == fact.predicate && holder.not != fact.not && Unifier.equalWithSubs(holder, fact, new ArrayList())) + if (holder.predicate.equals(fact.predicate) && holder.not != fact.not && Unifier.equalWithSubs(holder, fact, new ArrayList<>())) { return true; + } } } - + boolean keepGoing = true; //Step 2: Run negated facts against all known facts while (!kbClausesClone.isEmpty()) { @@ -41,23 +42,23 @@ public boolean follows(Fact fact) { if (kbFact.predicate.equals(followFact.predicate) && kbFact.not == !followFact.not) { //extend clause with everything in kbClause, remove kbFact and followFact, start over boolean skipOut = false; - for(int i = 0; i < kbFact.variables.size(); i++){ + for (int i = 0; i < kbFact.variables.size(); i++) { Variable var1 = kbFact.variables.get(i); Variable var2 = followFact.variables.get(i); - if(var1.value != var2.value){ + if (var1.value != var2.value) { skipOut = true; } } - if(!skipOut){ - kbClause.facts.remove(kbFact);//maybe commment out this line - clause.facts.remove(followFact); - clause.facts.addAll(kbClause.facts); + if (!skipOut) { + kbClause.facts.remove(kbFact);//maybe commment out this line + clause.facts.remove(followFact); + clause.facts.addAll(kbClause.facts); - //before starting over check if clause is empty... - if (clause.facts.isEmpty()) { - return true; - } - keepGoing = false; + //before starting over check if clause is empty... + if (clause.facts.isEmpty()) { + return true; + } + keepGoing = false; } } if (!keepGoing) { @@ -102,8 +103,9 @@ public void infer(Fact factStart) { if (var.isVariable && var.variableId == sub.varIdToSubstitute) { var.isVariable = false; var.value = sub.valToSubstituteWith; - if(var.function != null) + if (var.function != null) { var.value = var.function.process(var.value); + } } } for (Fact tempFact : clause.facts) { @@ -112,16 +114,17 @@ public void infer(Fact factStart) { if (var.isVariable && var.variableId == sub.varIdToSubstitute) { var.isVariable = false; var.value = sub.valToSubstituteWith; - if(var.function != null) + if (var.function != null) { var.value = var.function.process(var.value); + } } } } } clause.facts.remove(ruleFact); - System.out.println("Inferred:"); - Clause.printClause(clause); + // System.out.println("Inferred:"); + // Clause.printClause(clause); kb.addToClauses(clause); break; } @@ -169,14 +172,13 @@ public void infer(Clause clauseToCheck) { } } } - if(Unifier.equalWithSubs(fact, ruleFact, substitutions)){ + if (Unifier.equalWithSubs(fact, ruleFact, substitutions)) { clause.facts.remove(fact); - System.out.println("Inferred:"); - Clause.printClause(clause); + // System.out.println("Inferred:"); + // Clause.printClause(clause); kb.addToClauses(clause); return;//the smaller clause will be inferred against again, no need to keep trying rules immediately. } - continue; } } } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 77a7c06..b40ca9b 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -25,8 +25,8 @@ public void addToClauses(Clause clause){ if(factInClauses(clause.facts.get(0))){ return; } - System.out.println("Added to kb Clause: "); - System.out.print("\t"); + // System.out.println("Added to kb Clause: "); + // System.out.print("\t"); Clause.printClause(clause); clauses.add(clause); inferenceEngine.infer(clause.facts.get(0)); @@ -38,8 +38,8 @@ public void addToClauses(Clause clause){ clauses.add(clause);//We only need add the clause itself if we didn't infer anything, otherwise the inferred shortened clause which is more valuable got added - System.out.println("Added to kb Clause: "); - System.out.print("\t"); + // System.out.println("Added to kb Clause: "); + // System.out.print("\t"); Clause.printClause(clause); } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 39e13c2..e4d2d20 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -439,7 +439,11 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList Date: Sat, 22 Oct 2016 13:42:43 -0600 Subject: [PATCH 150/191] not adjacent if you're there... --- Wumpus/src/LogicExplorer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index e4d2d20..75dba7f 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -482,7 +482,7 @@ private boolean checkAdjacent(int curX, int curY, int goalX, int goalY) { return true; } } - if (curY == goalY) { + else if (curY == goalY) { if (Math.abs(curX - goalX) == 1) { return true; } @@ -497,7 +497,7 @@ private boolean adjacent(Location location) { return true; } } - if (location.y == this.location.y) { + else if (location.y == this.location.y) { if (Math.abs(location.x - this.location.x) == 1) { return true; } From 6c3d56aae84fa2714d6ae72ba24d6ea12b56bf0a Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 14:52:50 -0600 Subject: [PATCH 151/191] im mean, u can just uncomment things.. --- Wumpus/src/Clause.java | 12 ++-- Wumpus/src/KnowledgeBase.java | 10 ++-- Wumpus/src/LogicExplorer.java | 105 ++++++++++++++++++---------------- Wumpus/src/Rule.java | 52 ++++++++--------- 4 files changed, 92 insertions(+), 87 deletions(-) diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 528083d..8266d31 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -22,12 +22,12 @@ public Clause(Fact fact) { } public static void printClause(Clause clause){ -// for(Fact fact : clause.facts){ -// fact.printFact(); -// if(fact != clause.facts.get(clause.facts.size()-1)) -// System.out.print(" v "); -// } -// System.out.println(""); + for(Fact fact : clause.facts){ + fact.printFact(); + if(fact != clause.facts.get(clause.facts.size()-1)) + System.out.print(" v "); + } + System.out.println(""); } @Override diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index b40ca9b..e7ef451 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -11,7 +11,7 @@ public ArrayList getClauses(){ return clauses; } public void addToClauses(Clause clause){ - if(clause.facts.size() == 0) + if(clause.facts.isEmpty()) return; else{ for(Fact fact : clause.facts){ @@ -25,8 +25,8 @@ public void addToClauses(Clause clause){ if(factInClauses(clause.facts.get(0))){ return; } - // System.out.println("Added to kb Clause: "); - // System.out.print("\t"); + System.out.println("Added to kb Clause: "); + System.out.print("\t"); Clause.printClause(clause); clauses.add(clause); inferenceEngine.infer(clause.facts.get(0)); @@ -38,8 +38,8 @@ public void addToClauses(Clause clause){ clauses.add(clause);//We only need add the clause itself if we didn't infer anything, otherwise the inferred shortened clause which is more valuable got added - // System.out.println("Added to kb Clause: "); - // System.out.print("\t"); + System.out.println("Added to kb Clause: "); + System.out.print("\t"); Clause.printClause(clause); } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index e4d2d20..d1a9f5d 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -210,21 +210,21 @@ private void decideNextAction() { } if (safeSpaceInFrontier()) { if (!adjacent(safeSpace)) { - goTo(safeSpace); + traversal(safeSpace); } turnToSpace(safeSpace); move(MOVE); removeFromFrontier(safeSpace); } else if (arrowCount > 0 && wumpusInFrontier()) { if (!adjacent(wumpusSpace)) { - goTo(wumpusSpace); + traversal(wumpusSpace); } turnToSpace(wumpusSpace); move(SHOOT); move(MOVE); removeFromFrontier(wumpusSpace); } else if (!frontier.isEmpty()) { - goTo(frontier.get(frontier.size() - 1)); + traversal(frontier.get(frontier.size() - 1)); turnToSpace(frontier.get(frontier.size() - 1)); //frontier.remove(frontier.size() - 1); move(MOVE); @@ -331,41 +331,41 @@ private boolean wumpusInFrontier() { return false; } -// private void moveHistoryTraversal(Location loc) { -// if (adjacent(loc)) { -// return; -// } -// for (int i = moveHistory.size() - 1; i >= 0; i--) { -// if (adjacent(loc)) { -// return; -// } -// int move = moveHistory.get(i); -// switch (move) { -// case MOVE: -// move(TURN_LEFT); -// move(TURN_LEFT); -// move(MOVE); -// move(TURN_LEFT); -// move(TURN_LEFT); -// break; -// case TURN_LEFT: -// move(TURN_RIGHT); -// break; -// case TURN_RIGHT: -// move(TURN_LEFT); -// break; -// default: -// break; -// } -// } -// } -// private void rhwTraversal(Location location) { -// // moveHistoryTraversal(location); -// //if (!this.location.equals(location) && !adjacent(location)) { -// goTo(location.x, location.y); -// //} -// //go to location zach NOOOO! -// } + private void moveHistoryTraversal(Location loc) { + if (adjacent(loc)) { + return; + } + for (int i = moveHistory.size() - 1; i >= 0; i--) { + if (adjacent(loc)) { + return; + } + int move = moveHistory.get(i); + switch (move) { + case MOVE: + move(TURN_LEFT); + move(TURN_LEFT); + move(MOVE); + move(TURN_LEFT); + move(TURN_LEFT); + break; + case TURN_LEFT: + move(TURN_RIGHT); + break; + case TURN_RIGHT: + move(TURN_LEFT); + break; + default: + break; + } + } + } + private void traversal(Location location) { + // moveHistoryTraversal(location); + //if (!this.location.equals(location) && !adjacent(location)) { + goTo(location); + //} + //go to location zach NOOOO! + } private boolean safeSpaceInFrontier() { for (int i = frontier.size() - 1; i >= 0; i--) { Location loc = frontier.get(i); @@ -397,8 +397,11 @@ private void goTo(Location target) { //path.add(new Location(target.x, y)); printPath(path); traversePath(path); - System.out.println("Done traversing to " + target.x + ", " + target.y); - System.out.println("Actual location: " + this.location.x + ", " + this.location.y); + if (adjacent(target)) { + System.out.println("Done traversing to " + target.x + ", " + target.y); + } else { + System.out.println("Failed to reach " + target.x + ", " + target.y); + } } } @@ -409,6 +412,7 @@ private ArrayList searchForPath(int curX, int curY, int goalX, int goa return path; } else { System.out.println("Path finding error, no path found."); + path.removeAll(path); return path; } } @@ -424,28 +428,29 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path) { diff --git a/Wumpus/src/Rule.java b/Wumpus/src/Rule.java index c70fe82..813c4af 100644 --- a/Wumpus/src/Rule.java +++ b/Wumpus/src/Rule.java @@ -28,32 +28,32 @@ public Rule(Fact fact) { } public void printRule() { -// if (justFact) { -// fact.printFact(); -// return; -// } -// for (Quantifier quantifier : quantifiers) { -// quantifier.printQuantifier(); -// } -// System.out.print(" ("); -// leftRule.printRule(); -// System.out.print(") "); -// switch (connector) { -// case OR: -// System.out.print("OR"); -// break; -// case AND: -// System.out.print("AND"); -// break; -// case IFF: -// System.out.print("IFF"); -// break; -// case IMPLIES: -// System.out.print("IMPLIES"); -// } -// System.out.print(" ("); -// rightRule.printRule(); -// System.out.println(")"); + if (justFact) { + fact.printFact(); + return; + } + for (Quantifier quantifier : quantifiers) { + quantifier.printQuantifier(); + } + System.out.print(" ("); + leftRule.printRule(); + System.out.print(") "); + switch (connector) { + case OR: + System.out.print("OR"); + break; + case AND: + System.out.print("AND"); + break; + case IFF: + System.out.print("IFF"); + break; + case IMPLIES: + System.out.print("IMPLIES"); + } + System.out.print(" ("); + rightRule.printRule(); + System.out.println(")"); } } From ca78371150d019012cddd2e1ae875a7e511922d5 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 14:58:25 -0600 Subject: [PATCH 152/191] kek --- Wumpus/src/LogicExplorer.java | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index a7dde77..0c8a769 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -364,7 +364,6 @@ private void traversal(Location location) { //if (!this.location.equals(location) && !adjacent(location)) { goTo(location); //} - //go to location zach NOOOO! } private boolean safeSpaceInFrontier() { for (int i = frontier.size() - 1; i >= 0; i--) { @@ -408,13 +407,15 @@ private void goTo(Location target) { private ArrayList searchForPath(int curX, int curY, int goalX, int goalY, ArrayList path) { boolean[][] traversed = new boolean[World.size][World.size]; - if (searchNext(curX, curY, goalX, goalY, path, traversed)) { - return path; - } else { - System.out.println("Path finding error, no path found."); - path.removeAll(path); - return path; - } + searchNext(curX, curY, goalX, goalY, path, traversed); + return path; +// if (searchNext(curX, curY, goalX, goalY, path, traversed)) { +// return path; +// } else { +// System.out.println("Path finding error, no path found."); +// path.removeAll(path); +// return path; +// } } private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path, boolean[][] traversed) { @@ -445,9 +446,9 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList Date: Sat, 22 Oct 2016 15:09:08 -0600 Subject: [PATCH 153/191] closer? --- Wumpus/src/LogicExplorer.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 0c8a769..498046c 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -446,9 +446,12 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList Date: Sat, 22 Oct 2016 15:12:58 -0600 Subject: [PATCH 154/191] now regularly solves 15x15 boards? --- Wumpus/src/LogicExplorer.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 498046c..59fae2e 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -450,10 +450,7 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList Date: Sat, 22 Oct 2016 15:17:23 -0600 Subject: [PATCH 155/191] is it so? --- Wumpus/src/LogicExplorer.java | 51 +++++++---------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 59fae2e..7cb577d 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -306,21 +306,7 @@ public void turnToSpace(Location loc) { } } } - -// private Location neighborSafeSpace(Location location) { -// Location loc = null; -// if (location.x > 0 && searchedPositions[location.x - 1][location.y]) { -// loc = new Location(location.x - 1, location.y); -// -// } else if (location.x < World.size - 1 && searchedPositions[location.x + 1][location.y]) { -// loc = new Location(location.x + 1, location.y); -// } else if (location.y > 0 && searchedPositions[location.x][location.y - 1]) { -// loc = new Location(location.x, location.y - 1); -// } else if (location.y < World.size - 1 && searchedPositions[location.x][location.y + 1]) { -// loc = new Location(location.x, location.y + 1); -// } -// return loc; -// } + private boolean wumpusInFrontier() { for (Location loc : frontier) { if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { @@ -359,12 +345,12 @@ private void moveHistoryTraversal(Location loc) { } } } + private void traversal(Location location) { - // moveHistoryTraversal(location); - //if (!this.location.equals(location) && !adjacent(location)) { - goTo(location); - //} + //moveHistoryTraversal(location); + goTo(location); } + private boolean safeSpaceInFrontier() { for (int i = frontier.size() - 1; i >= 0; i--) { Location loc = frontier.get(i); @@ -393,13 +379,12 @@ private void goTo(Location target) { System.out.println("Traversing to " + target.x + ", " + target.y); ArrayList path = new ArrayList<>(); path = searchForPath(location.x, location.y, target.x, target.y, path); - //path.add(new Location(target.x, y)); printPath(path); traversePath(path); if (adjacent(target)) { - System.out.println("Done traversing to " + target.x + ", " + target.y); + System.out.println("Done traversing to " + target.x + ", " + target.y); } else { - System.out.println("Failed to reach " + target.x + ", " + target.y); + System.out.println("Failed to reach " + target.x + ", " + target.y); } } } @@ -409,13 +394,6 @@ private ArrayList searchForPath(int curX, int curY, int goalX, int goa boolean[][] traversed = new boolean[World.size][World.size]; searchNext(curX, curY, goalX, goalY, path, traversed); return path; -// if (searchNext(curX, curY, goalX, goalY, path, traversed)) { -// return path; -// } else { -// System.out.println("Path finding error, no path found."); -// path.removeAll(path); -// return path; -// } } private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path, boolean[][] traversed) { @@ -426,9 +404,6 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList Date: Sat, 22 Oct 2016 15:23:07 -0600 Subject: [PATCH 156/191] tweaks --- Wumpus/src/LogicExplorer.java | 2 +- Wumpus/src/World.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 7cb577d..4936667 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -402,7 +402,7 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList path) { private boolean isValid(int x, int y) { if (x >= 0 && y >= 0 && x < World.size && y < World.size) { - boolean wumpus = !kb.ask(new Fact("Wumpus", x, false, y, false, true, null, null)); - boolean pit = !kb.ask(new Fact("Pit", x, false, y, false, true, null, null)); - boolean bump = kb.ask(new Fact("Obsticle", x, false, y, false, false, null, null)); - return searchedPositions[x][y] && !bump && !pit && !wumpus; + boolean wumpus = kb.ask(new Fact("Wumpus", x, false, y, false, true, null, null)); + boolean pit = kb.ask(new Fact("Pit", x, false, y, false, true, null, null)); + boolean bump = !kb.ask(new Fact("Obsticle", x, false, y, false, true, null, null)); + return searchedPositions[x][y] && bump && pit && wumpus; } else { return false; } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 7db3e30..3ededc3 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -144,17 +144,21 @@ public byte action(int action, boolean thing) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus: arrows remaining = " + arrowCount); + System.out.println("(World) thinks location after move: " + x + ", " + y); return DEATH_WUMPUS; } if ((perceptMap[x][y + 1] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); + System.out.println("(World) thinks location after move: " + x + ", " + y); return DEATH_PIT; } y = y + 1; + System.out.println("(World) thinks location after move: " + x + ", " + y); return perceptMap[x][y]; } else { + System.out.println("(World) thinks location after move: " + x + ", " + y); return BUMP; } case EAST: @@ -164,16 +168,20 @@ public byte action(int action, boolean thing) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus: arrows remaining = " + arrowCount); + System.out.println("(World) thinks location after move: " + x + ", " + y); return DEATH_WUMPUS; } if ((perceptMap[x + 1][y] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; + System.out.println("(World) thinks location after move: " + x + ", " + y); return DEATH_PIT; } x = x + 1; + System.out.println("(World) thinks location after move: " + x + ", " + y); return perceptMap[x][y]; } else { + System.out.println("(World) thinks location after move: " + x + ", " + y); return BUMP; } case SOUTH: @@ -183,17 +191,21 @@ public byte action(int action, boolean thing) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus: arrows remaining = " + arrowCount); + System.out.println("(World) thinks location after move: " + x + ", " + y); return DEATH_WUMPUS; } if ((perceptMap[x][y - 1] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); + System.out.println("(World) thinks location after move: " + x + ", " + y); return DEATH_PIT; } y -= 1; + System.out.println("(World) thinks location after move: " + x + ", " + y); return perceptMap[x][y]; } else { + System.out.println("(World) thinks location after move: " + x + ", " + y); return BUMP; } case WEST: @@ -203,22 +215,27 @@ public byte action(int action, boolean thing) { score -= 1000; wumpusDeaths++; System.out.println("Death to wumpus: arrows remaining = " + arrowCount); + System.out.println("(World) thinks location after move: " + x + ", " + y); return DEATH_WUMPUS; } if ((perceptMap[x - 1][y] & DEATH_PIT) == DEATH_PIT) { score -= 1000; pitDeaths++; System.out.println("Death to pit."); + System.out.println("(World) thinks location after move: " + x + ", " + y); return DEATH_PIT; } x -= 1; + System.out.println("(World) thinks location after move: " + x + ", " + y); return perceptMap[x][y]; } else { + System.out.println("(World) thinks location after move: " + x + ", " + y); return BUMP; } default: System.out.println("Defaulted"); System.out.println(direction); + System.out.println("(World) thinks location after move: " + x + ", " + y); break; } break; diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 881dd09..4f22930 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,11 +1,6 @@ -10 -0 0 W 0 0 0 0 0 0 0 -0 0 0 0 W 0 0 0 0 0 -I 0 I 0 0 0 H 0 W 0 -0 0 W 0 0 0 0 0 W S -0 0 0 H 0 W 0 0 0 0 -0 0 0 0 0 H 0 0 0 0 -0 0 0 0 0 0 0 0 H W -W 0 0 0 0 0 W 0 I 0 -0 W G 0 0 0 0 I 0 0 -0 0 0 0 0 0 0 0 0 W +5 +0 0 H 0 0 +0 0 W S H +0 0 0 0 G +0 0 0 H 0 +0 0 0 0 W From d83d4e720b1fd365936e5a8befab196e072b177f Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 15:59:13 -0600 Subject: [PATCH 159/191] driver --- Wumpus/src/Driver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 6bdbf7e..9e27fe0 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + // makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } From ae122bad641f19da8144488f446e0725c4017c3f Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 16:02:00 -0600 Subject: [PATCH 160/191] this fix got lost fucking spacing --- Wumpus/PerceptBoard.txt | 17 ++++++----------- Wumpus/clean.txt | 17 ++++++----------- Wumpus/src/Driver.java | 2 +- Wumpus/src/LogicExplorer.java | 2 +- Wumpus/world.txt | 17 ++++++----------- 5 files changed, 20 insertions(+), 35 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index d00075e..5291bac 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,11 +1,6 @@ -10 3 9 -0 2 32 2 2 0 0 0 0 0 -0 0 2 2 32 2 1 0 2 0 -4 0 6 0 2 1 16 3 34 2 -0 2 32 3 0 2 1 2 34 2 -0 0 3 16 3 33 2 0 2 0 -0 0 0 1 1 18 1 0 1 2 -2 0 0 0 0 1 2 1 18 33 -32 2 0 0 0 2 32 2 5 2 -2 32 10 0 0 0 2 4 0 2 -0 2 0 0 0 0 0 0 2 32 +5 4 3 +11 32 3 17 17 +16 7 1 1 1 +1 17 19 5 0 +17 19 33 18 3 +1 17 3 3 32 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 9add639..3d66785 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,11 +1,6 @@ -10 -0 0 W 0 0 0 0 0 0 0 -0 0 0 0 W 0 0 0 0 0 -I 0 I 0 0 0 H 0 W 0 -0 0 W 0 0 0 0 0 W 0 -0 0 0 H 0 W 0 0 0 0 -0 0 0 0 0 H 0 0 0 0 -0 0 0 0 0 0 0 0 H W -W 0 0 0 0 0 W 0 I 0 -0 W 0 0 0 0 0 I 0 0 -0 0 0 0 0 0 0 0 0 W +5 +0 W 0 H H +H I 0 0 0 +0 H H I 0 +H H W H 0 +0 H 0 0 W diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index d473b36..7e6d634 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - // makeGame(); + //makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 4936667..bc5acaf 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -229,7 +229,7 @@ private void decideNextAction() { //frontier.remove(frontier.size() - 1); move(MOVE); //if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) -// frontier.remove(frontier.size() - 1); + frontier.remove(frontier.size() - 1); if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, false, null, null))) { addToFrontier(getForward());//since there is definitely a wumpus forward, put in frontier so we can kill it later maybe. } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 881dd09..d86c739 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,11 +1,6 @@ -10 -0 0 W 0 0 0 0 0 0 0 -0 0 0 0 W 0 0 0 0 0 -I 0 I 0 0 0 H 0 W 0 -0 0 W 0 0 0 0 0 W S -0 0 0 H 0 W 0 0 0 0 -0 0 0 0 0 H 0 0 0 0 -0 0 0 0 0 0 0 0 H W -W 0 0 0 0 0 W 0 I 0 -0 W G 0 0 0 0 I 0 0 -0 0 0 0 0 0 0 0 0 W +5 +G W 0 H H +H I 0 0 0 +0 H H I 0 +H H W H 0 +0 H 0 S W From 99da3b26b7949d21f194ae81518ee35530a4067a Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 16:08:46 -0600 Subject: [PATCH 161/191] Heres a case where the frontier is never > 0 , exception thrown --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/clean.txt | 10 +++++----- Wumpus/src/Driver.java | 10 +++++----- Wumpus/world.txt | 10 +++++----- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 5291bac..818cfc6 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 4 3 -11 32 3 17 17 -16 7 1 1 1 -1 17 19 5 0 -17 19 33 18 3 -1 17 3 3 32 +5 4 0 +0 0 0 1 16 +0 0 2 4 1 +0 2 32 2 0 +4 2 2 0 8 +2 32 2 0 4 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 3d66785..23d73a4 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,6 @@ 5 -0 W 0 H H -H I 0 0 0 -0 H H I 0 -H H W H 0 -0 H 0 0 W +0 0 0 0 H +0 0 0 I 0 +0 0 W 0 0 +I 0 0 0 0 +0 W 0 0 I diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 7e6d634..d5fe6a5 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - //makeGame(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } @@ -28,15 +28,15 @@ public static void makeGame() throws IOException { int[] prob = new int[3]; System.out.println("Size of board: "); - int size = Integer.parseInt(dataIn.readLine()); + int size = 5;// Integer.parseInt(dataIn.readLine()); System.out.print("% chance of generating pit: "); - prob[0] = Integer.parseInt(dataIn.readLine()); + prob[0] = 5;//Integer.parseInt(dataIn.readLine()); System.out.println(); System.out.print("% chance of generating obstacle: "); - prob[1] = Integer.parseInt(dataIn.readLine()); + prob[1] = 5;//Integer.parseInt(dataIn.readLine()); System.out.println(""); System.out.print("% chance of generating wumpus: "); - prob[2] = Integer.parseInt(dataIn.readLine()); + prob[2] = 5;//Integer.parseInt(dataIn.readLine()); System.out.println(""); game = new WumpusGame(size, prob); } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index d86c739..70726e7 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -G W 0 H H -H I 0 0 0 -0 H H I 0 -H H W H 0 -0 H 0 S W +0 0 0 0 H +0 0 0 I 0 +0 0 W 0 0 +I 0 0 0 G +S W 0 0 I From f849ae08a2322a4af061a74bdceff14daa378ae6 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 16:11:56 -0600 Subject: [PATCH 162/191] May have fixed --- Wumpus/src/Driver.java | 2 +- Wumpus/src/LogicExplorer.java | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index d5fe6a5..f774b86 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + //makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index d04d262..1ba32dd 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -230,7 +230,13 @@ private void decideNextAction() { //frontier.remove(frontier.size() - 1); move(MOVE); //if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) + try { frontier.remove(frontier.size() - 1); + } catch (ArrayIndexOutOfBoundsException ex) { + System.out.println("Exception caught: ArrayOutOfBounds"); + world.action(QUIT); + } + if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, false, null, null))) { addToFrontier(getForward());//since there is definitely a wumpus forward, put in frontier so we can kill it later maybe. } @@ -307,7 +313,7 @@ public void turnToSpace(Location loc) { } } } - + private boolean wumpusInFrontier() { for (Location loc : frontier) { if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { @@ -348,7 +354,7 @@ private void moveHistoryTraversal(Location loc) { } private void traversal(Location location) { - //moveHistoryTraversal(location); + //moveHistoryTraversal(location); goTo(location); } From 6c800a89d6eccd4f36fe6c422febfdb06bdcd5e5 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 16:16:21 -0600 Subject: [PATCH 163/191] fixy fix --- Wumpus/PerceptBoard.txt | 17 +++++++++++------ Wumpus/src/LogicExplorer.java | 7 ++++--- Wumpus/world.txt | 17 +++++++++++------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 5291bac..8df863b 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,11 @@ -5 4 3 -11 32 3 17 17 -16 7 1 1 1 -1 17 19 5 0 -17 19 33 18 3 -1 17 3 3 32 +10 2 4 +2 33 2 0 0 0 1 1 1 18 +5 18 1 0 0 1 17 17 7 33 +0 1 0 2 1 5 17 1 0 2 +0 0 2 33 18 1 1 0 2 0 +0 0 0 2 9 16 1 2 32 2 +1 0 0 0 2 5 0 0 2 0 +16 1 2 2 32 2 0 1 2 4 +1 2 32 2 2 0 1 18 33 2 +0 2 2 2 32 2 0 5 2 2 +2 32 2 0 2 4 0 0 2 32 diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index d04d262..414babd 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -225,12 +225,13 @@ private void decideNextAction() { move(MOVE); removeFromFrontier(wumpusSpace); } else if (!frontier.isEmpty()) { - traversal(frontier.get(frontier.size() - 1)); - turnToSpace(frontier.get(frontier.size() - 1)); + Location goalLoc = frontier.get(frontier.size() -1); + traversal(goalLoc); + turnToSpace(goalLoc); //frontier.remove(frontier.size() - 1); move(MOVE); //if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) - frontier.remove(frontier.size() - 1); + removeFromFrontier(goalLoc); if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, false, null, null))) { addToFrontier(getForward());//since there is definitely a wumpus forward, put in frontier so we can kill it later maybe. } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index d86c739..98e439c 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,11 @@ -5 -G W 0 H H -H I 0 0 0 -0 H H I 0 -H H W H 0 -0 H 0 S W +10 +0 W 0 0 0 0 0 0 0 H +I H 0 0 0 0 H H I W +0 0 0 0 S I H 0 0 0 +0 0 0 W H 0 0 0 0 0 +0 0 0 0 G H 0 0 W 0 +0 0 0 0 0 I 0 0 0 0 +H 0 0 0 W 0 0 0 0 I +0 0 W 0 0 0 0 H W 0 +0 0 0 0 W 0 0 I 0 0 +0 W 0 0 0 I 0 0 0 W From 208427a027a49539cf1449d449d388e94d2d2578 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 18:05:11 -0600 Subject: [PATCH 164/191] Wumpus killing problem --- Wumpus/PerceptBoard.txt | 17 ++- Wumpus/clean.txt | 10 +- Wumpus/src/Driver.java | 10 +- Wumpus/src/InferenceEngine.java | 4 +- Wumpus/src/KnowledgeBase.java | 176 +++++++++++++++++--------------- Wumpus/src/LogicExplorer.java | 9 +- Wumpus/world.txt | 17 ++- 7 files changed, 122 insertions(+), 121 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 8df863b..e0172a1 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,11 +1,6 @@ -10 2 4 -2 33 2 0 0 0 1 1 1 18 -5 18 1 0 0 1 17 17 7 33 -0 1 0 2 1 5 17 1 0 2 -0 0 2 33 18 1 1 0 2 0 -0 0 0 2 9 16 1 2 32 2 -1 0 0 0 2 5 0 0 2 0 -16 1 2 2 32 2 0 1 2 4 -1 2 32 2 2 0 1 18 33 2 -0 2 2 2 32 2 0 5 2 2 -2 32 2 0 2 4 0 0 2 32 +5 3 2 +4 2 4 3 17 +11 33 2 33 19 +17 19 33 2 3 +1 1 2 35 34 +4 0 1 18 35 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 23d73a4..4db253b 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,6 @@ 5 -0 0 0 0 H -0 0 0 I 0 -0 0 W 0 0 -I 0 0 0 0 -0 W 0 0 I +I 0 I 0 H +0 W 0 W H +H H W 0 0 +0 0 0 W W +I 0 0 H W diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index f774b86..0b4a838 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - //makeGame(); + //makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } @@ -28,15 +28,15 @@ public static void makeGame() throws IOException { int[] prob = new int[3]; System.out.println("Size of board: "); - int size = 5;// Integer.parseInt(dataIn.readLine()); + int size = Integer.parseInt(dataIn.readLine()); System.out.print("% chance of generating pit: "); - prob[0] = 5;//Integer.parseInt(dataIn.readLine()); + prob[0] = Integer.parseInt(dataIn.readLine()); System.out.println(); System.out.print("% chance of generating obstacle: "); - prob[1] = 5;//Integer.parseInt(dataIn.readLine()); + prob[1] = Integer.parseInt(dataIn.readLine()); System.out.println(""); System.out.print("% chance of generating wumpus: "); - prob[2] = 5;//Integer.parseInt(dataIn.readLine()); + prob[2] = Integer.parseInt(dataIn.readLine()); System.out.println(""); game = new WumpusGame(size, prob); } diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 2fe9b2d..c2d8cfb 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -123,8 +123,8 @@ public void infer(Fact factStart) { } clause.facts.remove(ruleFact); - // System.out.println("Inferred:"); - // Clause.printClause(clause); + System.out.println("Inferred:"); + Clause.printClause(clause); kb.addToClauses(clause); break; } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index e7ef451..96b547c 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -1,28 +1,30 @@ import java.util.ArrayList; - public class KnowledgeBase { InferenceEngine inferenceEngine = new InferenceEngine(this); private ArrayList clauses = new ArrayList<>(); ArrayList rules = new ArrayList<>(); - public ArrayList getClauses(){ + + public ArrayList getClauses() { return clauses; } - public void addToClauses(Clause clause){ - if(clause.facts.isEmpty()) + + public void addToClauses(Clause clause) { + if (clause.facts.isEmpty()) { return; - else{ - for(Fact fact : clause.facts){ - for(Variable var : fact.variables){ - if(var.isVariable) + } else { + for (Fact fact : clause.facts) { + for (Variable var : fact.variables) { + if (var.isVariable) { return; + } } } } - if(clause.facts.size() == 1){ - if(factInClauses(clause.facts.get(0))){ + if (clause.facts.size() == 1) { + if (factInClauses(clause.facts.get(0))) { return; } System.out.println("Added to kb Clause: "); @@ -30,37 +32,34 @@ public void addToClauses(Clause clause){ Clause.printClause(clause); clauses.add(clause); inferenceEngine.infer(clause.facts.get(0)); - } - else{ + } else { int beforeSize = clauses.size(); inferenceEngine.infer(clause); - if(clauses.size() == beforeSize){ + if (clauses.size() == beforeSize) { clauses.add(clause);//We only need add the clause itself if we didn't infer anything, otherwise the inferred shortened clause which is more valuable got added - - - System.out.println("Added to kb Clause: "); - System.out.print("\t"); - Clause.printClause(clause); + + System.out.println("Added to kb Clause: "); + System.out.print("\t"); + Clause.printClause(clause); } } //clauses.add(clause); - - + } - + public void initializeRules() { //Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function) //Special predicate: Evaluate - + //Stench(x,y)<=>(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) //Stench(x,y)=>(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) //!Stench(x,y) v Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) Clause stench = new Clause(); - Fact stenchXY = new Fact("Stench",0,true,1,true,true,null,null); - Fact wumpusxminy = new Fact("Wumpus",0,true,1,true,false,new MinusFunction(),null); - Fact wumpusxplusy = new Fact("Wumpus",0,true,1,true,false,new PlusFunction(),null); - Fact wumpusxymin = new Fact("Wumpus",0,true,1,true,false,null,new MinusFunction()); - Fact wumpusxyplus = new Fact("Wumpus",0,true,1,true,false,null,new PlusFunction()); + Fact stenchXY = new Fact("Stench", 0, true, 1, true, true, null, null); + Fact wumpusxminy = new Fact("Wumpus", 0, true, 1, true, false, new MinusFunction(), null); + Fact wumpusxplusy = new Fact("Wumpus", 0, true, 1, true, false, new PlusFunction(), null); + Fact wumpusxymin = new Fact("Wumpus", 0, true, 1, true, false, null, new MinusFunction()); + Fact wumpusxyplus = new Fact("Wumpus", 0, true, 1, true, false, null, new PlusFunction()); stench.facts.add(stenchXY); stench.facts.add(wumpusxminy); stench.facts.add(wumpusxplusy); @@ -79,7 +78,7 @@ public void initializeRules() { stench1.facts.add(stenchXY1); stench1.facts.add(wumpusxminy1); rules.add(stench1); - + Clause stench2 = new Clause(); Fact stenchXY2 = new Fact(stenchXY); stenchXY2.not = false; @@ -88,7 +87,7 @@ public void initializeRules() { stench2.facts.add(stenchXY2); stench2.facts.add(wumpusxplusy1); rules.add(stench2); - + Clause stench3 = new Clause(); Fact stenchXY3 = new Fact(stenchXY); stenchXY3.not = false; @@ -97,7 +96,7 @@ public void initializeRules() { stench3.facts.add(stenchXY3); stench3.facts.add(wumpusxymin1); rules.add(stench3); - + Clause stench4 = new Clause(); Fact stenchXY4 = new Fact(stenchXY); stenchXY4.not = false; @@ -106,22 +105,22 @@ public void initializeRules() { stench4.facts.add(stenchXY4); stench4.facts.add(wumpusxyplus1); rules.add(stench4); - + //Breeze(x,y)<=>(Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) //!Breeze(x,y) v Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) Clause breeze = new Clause(); - Fact breezeXY = new Fact("Breeze",0,true,1,true,true,null,null); - Fact pitxminy = new Fact("Pit",0,true,1,true,false,new MinusFunction(),null); - Fact pitxplusy = new Fact("Pit",0,true,1,true,false,new PlusFunction(),null); - Fact pitxymin = new Fact("Pit",0,true,1,true,false,null,new MinusFunction()); - Fact pitxyplus = new Fact("Pit",0,true,1,true,false,null,new PlusFunction()); + Fact breezeXY = new Fact("Breeze", 0, true, 1, true, true, null, null); + Fact pitxminy = new Fact("Pit", 0, true, 1, true, false, new MinusFunction(), null); + Fact pitxplusy = new Fact("Pit", 0, true, 1, true, false, new PlusFunction(), null); + Fact pitxymin = new Fact("Pit", 0, true, 1, true, false, null, new MinusFunction()); + Fact pitxyplus = new Fact("Pit", 0, true, 1, true, false, null, new PlusFunction()); breeze.facts.add(breezeXY); breeze.facts.add(pitxminy); breeze.facts.add(pitxplusy); breeze.facts.add(pitxymin); breeze.facts.add(pitxyplus); rules.add(breeze); - + Clause breeze1 = new Clause(); Fact breezeXY1 = new Fact(breezeXY); breezeXY1.not = false; @@ -130,27 +129,27 @@ public void initializeRules() { breeze1.facts.add(breezeXY1); breeze1.facts.add(pitxminy1); rules.add(breeze1); - + Clause breeze2 = new Clause(); Fact breezeXY2 = new Fact(breezeXY); - + breezeXY2.not = false; Fact pitxplusy1 = new Fact(pitxplusy); pitxplusy1.not = true; breeze2.facts.add(breezeXY2); breeze2.facts.add(pitxplusy1); rules.add(breeze2); - + Clause breeze3 = new Clause(); Fact breezeXY3 = new Fact(breezeXY); - + breezeXY3.not = false; Fact pitxymin1 = new Fact(pitxymin); pitxymin1.not = true; breeze3.facts.add(breezeXY3); breeze3.facts.add(pitxymin1); rules.add(breeze3); - + Clause breeze4 = new Clause(); Fact breezeXY4 = new Fact(breezeXY); breezeXY4.not = false; @@ -159,86 +158,97 @@ public void initializeRules() { breeze4.facts.add(breezeXY4); breeze4.facts.add(pitxyplus1); rules.add(breeze4); - + //!Wumpus(-1,y) ^ !Wumpus(x,-1) ^ !Wumpus(x,World.size)^ !Wumpus(World.size,y) //!Wumpus(-1,y), !Wumpus(x,-1), !Wumpus(x,World.size), !Wumpus(World.size,y) - rules.add(new Clause(new Fact("Wumpus",-1,false,1,true,true,null,null))); - rules.add(new Clause(new Fact("Wumpus",0,true,-1,false,true,null,null))); - rules.add(new Clause(new Fact("Wumpus",0,true,World.size,false,true,null,null))); - rules.add(new Clause(new Fact("Wumpus",World.size,false,1,true,true,null,null))); + rules.add(new Clause(new Fact("Wumpus", -1, false, 1, true, true, null, null))); + rules.add(new Clause(new Fact("Wumpus", 0, true, -1, false, true, null, null))); + rules.add(new Clause(new Fact("Wumpus", 0, true, World.size, false, true, null, null))); + rules.add(new Clause(new Fact("Wumpus", World.size, false, 1, true, true, null, null))); //!Pit(-1,y), !Pit(x,-1), !Pit(x,World.size), !Wumpus(World.size,y) - rules.add(new Clause(new Fact("Pit",-1,false,1,true,true,null,null))); - rules.add(new Clause(new Fact("Pit",0,true,-1,false,true,null,null))); - rules.add(new Clause(new Fact("Pit",0,true,World.size,false,true,null,null))); - rules.add(new Clause(new Fact("Pit",World.size,false,1,true,true,null,null))); - + rules.add(new Clause(new Fact("Pit", -1, false, 1, true, true, null, null))); + rules.add(new Clause(new Fact("Pit", 0, true, -1, false, true, null, null))); + rules.add(new Clause(new Fact("Pit", 0, true, World.size, false, true, null, null))); + rules.add(new Clause(new Fact("Pit", World.size, false, 1, true, true, null, null))); + //Wumpus(x,y)=>!Pit(x,y) //!Wumpus(x,y) v !Pit(x,y) Clause notWumpusOrNotPit = new Clause(); - Fact notWumpus = new Fact("Wumpus",0,true,1,true,true,null,null); - Fact notPit = new Fact("Pit",0,true,1,true,true,null,null); + Fact notWumpus = new Fact("Wumpus", 0, true, 1, true, true, null, null); + Fact notPit = new Fact("Pit", 0, true, 1, true, true, null, null); notWumpusOrNotPit.facts.add(notWumpus); notWumpusOrNotPit.facts.add(notPit); rules.add(notWumpusOrNotPit); } public boolean ask(Fact fact) { - + //if question follows from known facts ==> return true //else return false - return inferenceEngine.follows(fact); } - - public void tell(Clause clause){ + + public void tell(Clause clause) { addToClauses(clause); } public void tell(Fact fact) { //If told DeadWumpus delete the wumpus entry at that position and add !Wumpus(x,y) - if(fact.predicate.equals("DeadWumpus")){ + if (fact.predicate.equals("DeadWumpus")) { removeWumpusAndStench(fact); - + } - if(factInClauses(fact)){ + if (factInClauses(fact)) { return; } - + addToClauses(new Clause(fact)); //and check if there is a stench at any adjacent position, remove those facts too } - - private void removeWumpusAndStench(Fact fact){ - for(int i = clauses.size()-1; i >= 0; i--){ - if(clauses.get(i).facts.size()==1){ + + private void removeWumpusAndStench(Fact fact) { + for (int i = clauses.size() - 1; i >= 0; i--) { + if (clauses.get(i).facts.size() == 1) { Fact toRemove = clauses.get(i).facts.get(0); - if(toRemove.predicate.equals("Wumpus")){ - - if(toRemove.variables.get(0).value == fact.variables.get(0).value && toRemove.variables.get(1).value == fact.variables.get(1).value){ + if (toRemove.predicate.equals("Wumpus")) { + + if (toRemove.variables.get(0).value == fact.variables.get(0).value && toRemove.variables.get(1).value == fact.variables.get(1).value) { toRemove.not = true; } - } - else if(toRemove.predicate.equals("Stench")){ - if((toRemove.variables.get(0).value == fact.variables.get(0).value && Math.abs(toRemove.variables.get(1).value - fact.variables.get(1).value) == 1)||(toRemove.variables.get(1).value == fact.variables.get(1).value && Math.abs(toRemove.variables.get(0).value - fact.variables.get(0).value) == 1)){ + } else if (toRemove.predicate.equals("Stench")) { + if ((toRemove.variables.get(0).value == fact.variables.get(0).value && Math.abs(toRemove.variables.get(1).value - fact.variables.get(1).value) == 1) || (toRemove.variables.get(1).value == fact.variables.get(1).value && Math.abs(toRemove.variables.get(0).value - fact.variables.get(0).value) == 1)) { clauses.remove(i); } } + } else { + for (Fact toRemoveFact : clauses.get(i).facts) { + if (toRemoveFact.predicate.equals("Wumpus")) { + if (toRemoveFact.variables.get(0).value == fact.variables.get(0).value && toRemoveFact.variables.get(1).value == fact.variables.get(1).value) { + if (toRemoveFact.not == fact.not) { + clauses.get(i).facts.remove(toRemoveFact); + break; + } + } + } + } } - } + } } - private boolean factInClauses(Fact fact){ - for(Clause clause : clauses){ - if(clause.facts.size() == 1){ + + private boolean factInClauses(Fact fact) { + for (Clause clause : clauses) { + if (clause.facts.size() == 1) { Fact clauseFact = clause.facts.get(0); - if(clauseFact.predicate.equals(fact.predicate)){ - for(int i = 0; i < fact.variables.size(); i++){ - - if(clauseFact.variables.get(i).value == fact.variables.get(i).value){ - if(i == fact.variables.size()-1) + if (clauseFact.predicate.equals(fact.predicate)) { + for (int i = 0; i < fact.variables.size(); i++) { + + if (clauseFact.variables.get(i).value == fact.variables.get(i).value) { + if (i == fact.variables.size() - 1) { return true; - } - else + } + } else { break; + } } } } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 414babd..57dd409 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -231,10 +231,10 @@ private void decideNextAction() { //frontier.remove(frontier.size() - 1); move(MOVE); //if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) - removeFromFrontier(goalLoc); + removeFromFrontier(goalLoc); if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, false, null, null))) { - addToFrontier(getForward());//since there is definitely a wumpus forward, put in frontier so we can kill it later maybe. - } + frontier.add(getForward());//since there is definitely a wumpus forward, put in frontier so we can kill it later maybe. + }//we add straight to frontier even if it was searched in this case, that's why it's not addToFrontier } } @@ -423,7 +423,8 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList 0) + path.remove(path.size() - 1); printPath(path); } //traversed[curX][curY] = false; diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 98e439c..3c301c5 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,11 +1,6 @@ -10 -0 W 0 0 0 0 0 0 0 H -I H 0 0 0 0 H H I W -0 0 0 0 S I H 0 0 0 -0 0 0 W H 0 0 0 0 0 -0 0 0 0 G H 0 0 W 0 -0 0 0 0 0 I 0 0 0 0 -H 0 0 0 W 0 0 0 0 I -0 0 W 0 0 0 0 H W 0 -0 0 0 0 W 0 0 I 0 0 -0 W 0 0 0 I 0 0 0 W +5 +I 0 I 0 H +G W 0 W H +H H W 0 0 +0 0 S W W +I 0 0 H W From 6fe27c9ea845f3ea0c875a3cd2284392d49f8516 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 18:27:50 -0600 Subject: [PATCH 165/191] fixed --- Wumpus/src/KnowledgeBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 96b547c..9e9d3c2 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -225,7 +225,7 @@ private void removeWumpusAndStench(Fact fact) { if (toRemoveFact.predicate.equals("Wumpus")) { if (toRemoveFact.variables.get(0).value == fact.variables.get(0).value && toRemoveFact.variables.get(1).value == fact.variables.get(1).value) { if (toRemoveFact.not == fact.not) { - clauses.get(i).facts.remove(toRemoveFact); + toRemoveFact.not = true; break; } } From dba387f4ca8764877fa8043c062508bfd4c554fa Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 20:43:23 -0600 Subject: [PATCH 166/191] very small but important fix --- Wumpus/src/LogicExplorer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 57dd409..cb3f7a9 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -201,8 +201,8 @@ private void decideNextAction() { if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, true, null, null))) { if (kb.ask(new Fact("Pit", getForward().x, false, getForward().y, false, true, null, null))) { if (inFrontier(getForward())) { - move(MOVE); removeFromFrontier(getForward()); + move(MOVE); return; } From 64f7a883e4f116ac79160aa72bc9db1c7f211efe Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sat, 22 Oct 2016 21:18:48 -0600 Subject: [PATCH 167/191] Bad thing --- Wumpus/PerceptBoard.txt | 12 ++++++------ Wumpus/clean.txt | 10 +++++----- Wumpus/src/KnowledgeBase.java | 4 +++- Wumpus/src/LogicExplorer.java | 2 +- Wumpus/world.txt | 10 +++++----- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index e0172a1..fa18877 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,6 @@ -5 3 2 -4 2 4 3 17 -11 33 2 33 19 -17 19 33 2 3 -1 1 2 35 34 -4 0 1 18 35 +5 1 3 +2 34 34 7 16 +32 6 2 0 1 +2 0 0 0 4 +32 2 6 0 2 +2 10 32 2 32 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 4db253b..42d6663 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,6 @@ 5 -I 0 I 0 H -0 W 0 W H -H H W 0 0 -0 0 0 W W -I 0 0 H W +0 W W I H +W I 0 0 0 +0 0 0 0 I +W 0 I 0 0 +0 0 W 0 W diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 9e9d3c2..332f5a9 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -172,12 +172,14 @@ public void initializeRules() { rules.add(new Clause(new Fact("Pit", World.size, false, 1, true, true, null, null))); //Wumpus(x,y)=>!Pit(x,y) - //!Wumpus(x,y) v !Pit(x,y) + //!Wumpus(x,y) v !Pit(x,y) v !Obstacle Clause notWumpusOrNotPit = new Clause(); Fact notWumpus = new Fact("Wumpus", 0, true, 1, true, true, null, null); Fact notPit = new Fact("Pit", 0, true, 1, true, true, null, null); + // Fact notObstacle = new Fact("Obstacle",0,true,1,true,true,null,null); notWumpusOrNotPit.facts.add(notWumpus); notWumpusOrNotPit.facts.add(notPit); + // notWumpusOrNotPit.facts.add(notObstacle); rules.add(notWumpusOrNotPit); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index cb3f7a9..483e3f9 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -364,7 +364,7 @@ private boolean safeSpaceInFrontier() { } } - if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obsticle", loc.x, false, loc.y, false, false, null, null))) { + if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obstacle", loc.x, false, loc.y, false, false, null, null))) { //there is specifically a wumpus, pit, or obsticle at this position, don't navigate to it. frontier.remove(i); } //else if (arrowCount == 0 && kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 3c301c5..5ad6a06 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,6 @@ 5 -I 0 I 0 H -G W 0 W H -H H W 0 0 -0 0 S W W -I 0 0 H W +0 W W I H +W I 0 S 0 +0 0 0 0 I +W 0 I 0 0 +0 G W 0 W From 1bae56303d71cc2929c440c18cefd147850c6c24 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sat, 22 Oct 2016 21:40:10 -0600 Subject: [PATCH 168/191] Some code cleanup, cause zach is ocd and likes to fk with wilson's spacing. Currently solving ~80% of 10x10 boards. Theres still an issue with repeated seppuku --- Wumpus/PerceptBoard.txt | 17 +++-- Wumpus/clean.txt | 17 +++-- Wumpus/src/Agent.java | 17 +++-- Wumpus/src/Driver.java | 10 +-- Wumpus/src/LogicExplorer.java | 137 +++++++++++----------------------- Wumpus/world.txt | 17 +++-- 6 files changed, 92 insertions(+), 123 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index fa18877..d7da475 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,6 +1,11 @@ -5 1 3 -2 34 34 7 16 -32 6 2 0 1 -2 0 0 0 4 -32 2 6 0 2 -2 10 32 2 32 +10 0 6 +0 1 0 0 0 0 0 4 0 0 +1 18 1 0 0 0 0 4 0 0 +2 33 6 4 2 0 0 4 4 0 +1 2 0 2 33 2 0 0 0 4 +16 1 0 1 18 1 0 0 0 0 +1 0 0 0 1 4 0 0 0 0 +0 0 0 0 0 0 5 0 0 0 +0 0 0 1 0 1 16 1 0 0 +0 0 1 16 5 0 1 16 1 2 +0 0 0 1 0 0 8 1 2 32 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 42d6663..ce00f6b 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,6 +1,11 @@ -5 -0 W W I H -W I 0 0 0 -0 0 0 0 I -W 0 I 0 0 -0 0 W 0 W +10 +0 0 0 0 0 0 0 I 0 0 +0 H 0 0 0 0 0 I 0 0 +0 W I I 0 0 0 I I 0 +0 0 0 0 W 0 0 0 0 I +H 0 0 0 H 0 0 0 0 0 +0 0 0 0 0 I 0 0 0 0 +0 0 0 0 0 0 I 0 0 0 +0 0 0 0 0 0 H 0 0 0 +0 0 0 H I 0 0 H 0 0 +0 0 0 0 0 0 0 0 0 W diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index 82a0da2..bd27fdd 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -21,14 +21,15 @@ public Agent(World world, int startingArrowCount, int startingX, int startingY, } public Location getForward() { - if (direction == NORTH) { - return new Location(location.x, location.y + 1); - } else if (direction == EAST) { - return new Location(location.x + 1, location.y); - } else if (direction == SOUTH) { - return new Location(location.x, location.y - 1); - } else { - return new Location(location.x - 1, location.y); + switch (direction) { + case NORTH: + return new Location(location.x, location.y + 1); + case EAST: + return new Location(location.x + 1, location.y); + case SOUTH: + return new Location(location.x, location.y - 1); + default: + return new Location(location.x - 1, location.y); } } diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 0b4a838..5bc32ef 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - //makeGame(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } @@ -28,15 +28,15 @@ public static void makeGame() throws IOException { int[] prob = new int[3]; System.out.println("Size of board: "); - int size = Integer.parseInt(dataIn.readLine()); + int size = 10;//Integer.parseInt(dataIn.readLine()); System.out.print("% chance of generating pit: "); - prob[0] = Integer.parseInt(dataIn.readLine()); + prob[0] = 5;// Integer.parseInt(dataIn.readLine()); System.out.println(); System.out.print("% chance of generating obstacle: "); - prob[1] = Integer.parseInt(dataIn.readLine()); + prob[1] = 5;//Integer.parseInt(dataIn.readLine()); System.out.println(""); System.out.print("% chance of generating wumpus: "); - prob[2] = Integer.parseInt(dataIn.readLine()); + prob[2] = 5;//Integer.parseInt(dataIn.readLine()); System.out.println(""); game = new WumpusGame(size, prob); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 483e3f9..8fd9233 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -4,10 +4,8 @@ public class LogicExplorer extends Agent { private final KnowledgeBase kb; - private int previousAction; private ArrayList frontier = new ArrayList<>(); private boolean[][] searchedPositions; - private boolean navigatingToSafePosition; private Location safeSpace; private Location wumpusSpace; private ArrayList moveHistory = new ArrayList<>(); @@ -21,27 +19,37 @@ public LogicExplorer(World world, int startingArrows, int startingX, int startin searchedPositions[location.x][location.y] = true; percepts = world.getPercepts(); processPercepts(); - //initializeFrontier(); run(); } - public void initializeFrontier() { - if (location.x > 0) { - frontier.add(new Location(location.x - 1, location.y)); - } - if (location.x < World.size - 1) { - frontier.add(new Location(location.x + 1, location.y)); - } - if (location.y > 0) { - frontier.add(new Location(location.x, location.y - 1)); - } - if (location.y < World.size - 1) { - frontier.add(new Location(location.x, location.y + 1)); + private void run() { + + while (true) { + if (!notFirstMove) { + percepts = world.getPercepts(); + processPercepts(); + } + decideNextAction(); } } +// public void initializeFrontier() { +// if (location.x > 0) { +// frontier.add(new Location(location.x - 1, location.y)); +// } +// if (location.x < World.size - 1) { +// frontier.add(new Location(location.x + 1, location.y)); +// } +// if (location.y > 0) { +// frontier.add(new Location(location.x, location.y - 1)); +// } +// if (location.y < World.size - 1) { +// frontier.add(new Location(location.x, location.y + 1)); +// } +// } @Override public void updateLocation() { + if (notFirstMove) { super.updateLocation(); } else { @@ -52,6 +60,7 @@ public void updateLocation() { } public void addToFrontier(Location loc) { + if (!searchedPositions[loc.x][loc.y]) { if (inFrontier(loc)) { removeFromFrontier(loc); @@ -61,6 +70,7 @@ public void addToFrontier(Location loc) { } public void expandFrontier() { + if (location.x > 0 && !searchedPositions[location.x - 1][location.y]) { addToFrontier(new Location(location.x - 1, location.y)); } @@ -75,17 +85,6 @@ public void expandFrontier() { } } - private void run() { - - while (true) { - if (!notFirstMove) { - percepts = world.getPercepts(); - processPercepts(); - } - decideNextAction(); - } - } - private void move(int action) { switch (action) { @@ -181,8 +180,9 @@ private void processPercepts() { //there might still be an issue with wum } private boolean inFrontier(Location loc) { - for (Location location : frontier) { - if (location.x == loc.x && location.y == loc.y) { + + for (Location front : frontier) { + if (front.x == loc.x && front.y == loc.y) { return true; } } @@ -190,11 +190,12 @@ private boolean inFrontier(Location loc) { } private void decideNextAction() { + if ((percepts & GLITTER) != 0) { move(GRAB); } - if (frontier.isEmpty()) { + System.out.println("Frontier is empty, quitting.."); move(World.QUIT); } if (getForward().x >= 0 && getForward().x < World.size && getForward().y >= 0 && getForward().y < World.size) { @@ -210,23 +211,23 @@ private void decideNextAction() { } } if (safeSpaceInFrontier()) { - if (!adjacent(safeSpace)) { - traversal(safeSpace); + if (!checkAdjacent(safeSpace.x, safeSpace.y, location.x, location.y)) { + goTo(safeSpace); } turnToSpace(safeSpace); move(MOVE); removeFromFrontier(safeSpace); } else if (arrowCount > 0 && wumpusInFrontier()) { - if (!adjacent(wumpusSpace)) { - traversal(wumpusSpace); + if (!checkAdjacent(wumpusSpace.x, wumpusSpace.y, location.x, location.y)) { + goTo(wumpusSpace); } turnToSpace(wumpusSpace); move(SHOOT); move(MOVE); removeFromFrontier(wumpusSpace); } else if (!frontier.isEmpty()) { - Location goalLoc = frontier.get(frontier.size() -1); - traversal(goalLoc); + Location goalLoc = frontier.get(frontier.size() - 1); + goTo(goalLoc); turnToSpace(goalLoc); //frontier.remove(frontier.size() - 1); move(MOVE); @@ -239,6 +240,7 @@ private void decideNextAction() { } public void turnToSpace(Location loc) { + if (loc.x < location.x) { switch (direction) { case WEST: @@ -308,7 +310,7 @@ public void turnToSpace(Location loc) { } } } - + private boolean wumpusInFrontier() { for (Location loc : frontier) { if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { @@ -319,40 +321,6 @@ private boolean wumpusInFrontier() { return false; } - private void moveHistoryTraversal(Location loc) { - if (adjacent(loc)) { - return; - } - for (int i = moveHistory.size() - 1; i >= 0; i--) { - if (adjacent(loc)) { - return; - } - int move = moveHistory.get(i); - switch (move) { - case MOVE: - move(TURN_LEFT); - move(TURN_LEFT); - move(MOVE); - move(TURN_LEFT); - move(TURN_LEFT); - break; - case TURN_LEFT: - move(TURN_RIGHT); - break; - case TURN_RIGHT: - move(TURN_LEFT); - break; - default: - break; - } - } - } - - private void traversal(Location location) { - //moveHistoryTraversal(location); - goTo(location); - } - private boolean safeSpaceInFrontier() { for (int i = frontier.size() - 1; i >= 0; i--) { Location loc = frontier.get(i); @@ -376,14 +344,13 @@ private boolean safeSpaceInFrontier() { private void goTo(Location target) { - if (!adjacent(target)) { - + if (!checkAdjacent(target.x, target.y, location.x, location.y)) { System.out.println("Traversing to " + target.x + ", " + target.y); ArrayList path = new ArrayList<>(); path = searchForPath(location.x, location.y, target.x, target.y, path); - printPath(path); + // printPath(path); traversePath(path); - if (adjacent(target)) { + if (checkAdjacent(target.x, target.y, location.x, location.y)) { System.out.println("Done traversing to " + target.x + ", " + target.y); } else { System.out.println("Failed to reach " + target.x + ", " + target.y); @@ -404,7 +371,7 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList 0) + if (path.size() > 0) { path.remove(path.size() - 1); - printPath(path); + } + // printPath(path); } //traversed[curX][curY] = false; return done; @@ -441,7 +409,6 @@ private void printPath(ArrayList path) { } else { System.out.println("Path: [ ]"); } - } private boolean isValid(int x, int y) { @@ -470,20 +437,6 @@ private boolean checkAdjacent(int curX, int curY, int goalX, int goalY) { return false; } - private boolean adjacent(Location location) { - - if (location.x == this.location.x) { - if (Math.abs(location.y - this.location.y) == 1) { - return true; - } - } else if (location.y == this.location.y) { - if (Math.abs(location.x - this.location.x) == 1) { - return true; - } - } - return false; - } - private void traversePath(ArrayList path) { while (path.size() > 0) { diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 5ad6a06..02b5aac 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,6 +1,11 @@ -5 -0 W W I H -W I 0 S 0 -0 0 0 0 I -W 0 I 0 0 -0 G W 0 W +10 +0 0 0 0 0 0 S I 0 0 +0 H 0 0 0 0 0 I 0 0 +0 W I I 0 0 0 I I 0 +0 0 0 0 W 0 0 0 0 I +H 0 0 0 H 0 0 0 0 0 +0 0 0 0 0 I 0 0 0 0 +0 0 0 0 0 0 I 0 0 0 +0 0 0 0 0 0 H 0 0 0 +0 0 0 H I 0 0 H 0 0 +0 0 0 0 0 0 G 0 0 W From da3ea426e16e3660b51c7304bbcfce4716fff446 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sun, 23 Oct 2016 08:27:45 -0600 Subject: [PATCH 169/191] Obstacle, bitch --- Wumpus/PerceptBoard.txt | 27 ++++++++++++++++----------- Wumpus/clean.txt | 27 ++++++++++++++++----------- Wumpus/src/Driver.java | 8 ++++---- Wumpus/src/LogicExplorer.java | 2 +- Wumpus/world.txt | 27 ++++++++++++++++----------- 5 files changed, 53 insertions(+), 38 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index d7da475..7a6256d 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,11 +1,16 @@ -10 0 6 -0 1 0 0 0 0 0 4 0 0 -1 18 1 0 0 0 0 4 0 0 -2 33 6 4 2 0 0 4 4 0 -1 2 0 2 33 2 0 0 0 4 -16 1 0 1 18 1 0 0 0 0 -1 0 0 0 1 4 0 0 0 0 -0 0 0 0 0 0 5 0 0 0 -0 0 0 1 0 1 16 1 0 0 -0 0 1 16 5 0 1 16 1 2 -0 0 0 1 0 0 8 1 2 32 +15 7 12 +0 0 7 32 2 2 2 0 8 0 0 1 0 5 0 +0 1 16 3 34 34 34 2 4 1 1 16 1 16 1 +1 0 1 18 3 34 2 0 1 16 1 1 0 5 18 +16 1 2 33 6 2 0 0 2 1 16 3 1 18 33 +1 18 33 19 1 0 0 3 32 6 3 32 2 5 18 +2 33 19 19 1 0 1 16 3 0 0 2 1 16 1 +4 2 3 33 2 0 4 1 4 1 0 0 0 1 2 +0 4 6 2 1 0 0 0 1 18 1 0 1 18 33 +0 2 33 7 16 1 1 4 3 33 7 0 0 5 2 +1 5 19 19 7 1 16 5 18 3 16 1 0 0 0 +18 3 35 35 34 2 1 2 33 18 1 0 0 0 0 +35 35 2 2 2 0 0 0 7 1 2 0 0 0 0 +3 18 1 4 0 4 2 7 17 7 32 2 2 5 2 +17 1 0 2 0 2 34 35 19 1 2 2 33 19 33 +17 1 2 32 2 0 2 2 33 2 0 0 7 17 7 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index ce00f6b..4acedc3 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,11 +1,16 @@ -10 -0 0 0 0 0 0 0 I 0 0 -0 H 0 0 0 0 0 I 0 0 -0 W I I 0 0 0 I I 0 -0 0 0 0 W 0 0 0 0 I -H 0 0 0 H 0 0 0 0 0 -0 0 0 0 0 I 0 0 0 0 -0 0 0 0 0 0 I 0 0 0 -0 0 0 0 0 0 H 0 0 0 -0 0 0 H I 0 0 H 0 0 -0 0 0 0 0 0 0 0 0 W +15 +0 0 I W 0 0 0 0 0 0 0 0 0 I 0 +0 0 H 0 W W W 0 I 0 0 H 0 H 0 +0 0 0 H 0 W 0 0 0 H 0 0 0 I H +H 0 0 W I 0 0 0 0 0 H 0 0 H W +0 H W H 0 0 0 0 W I 0 W 0 I H +0 W H H 0 0 0 H 0 0 0 0 0 H 0 +I 0 0 W 0 0 I 0 I 0 0 0 0 0 0 +0 I I 0 0 0 0 0 0 H 0 0 0 H W +0 0 W I H 0 0 I 0 W I 0 0 I 0 +0 I H H I 0 H I H 0 H 0 0 0 0 +H 0 W W W 0 0 0 W H 0 0 0 0 0 +W W 0 0 0 0 0 0 I 0 0 0 0 0 0 +0 H 0 I 0 I 0 I H I W 0 0 I 0 +H 0 0 0 0 0 W W H 0 0 0 W H W +H 0 0 W 0 0 0 0 W 0 0 0 I H I diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 5bc32ef..72472d1 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -28,15 +28,15 @@ public static void makeGame() throws IOException { int[] prob = new int[3]; System.out.println("Size of board: "); - int size = 10;//Integer.parseInt(dataIn.readLine()); + int size = Integer.parseInt(dataIn.readLine()); System.out.print("% chance of generating pit: "); - prob[0] = 5;// Integer.parseInt(dataIn.readLine()); + prob[0] = Integer.parseInt(dataIn.readLine()); System.out.println(); System.out.print("% chance of generating obstacle: "); - prob[1] = 5;//Integer.parseInt(dataIn.readLine()); + prob[1] = Integer.parseInt(dataIn.readLine()); System.out.println(""); System.out.print("% chance of generating wumpus: "); - prob[2] = 5;//Integer.parseInt(dataIn.readLine()); + prob[2] = Integer.parseInt(dataIn.readLine()); System.out.println(""); game = new WumpusGame(size, prob); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 8fd9233..8c1e38e 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -416,7 +416,7 @@ private boolean isValid(int x, int y) { if (x >= 0 && y >= 0 && x < World.size && y < World.size) { boolean wumpus = kb.ask(new Fact("Wumpus", x, false, y, false, true, null, null)); boolean pit = kb.ask(new Fact("Pit", x, false, y, false, true, null, null)); - boolean bump = !kb.ask(new Fact("Obsticle", x, false, y, false, true, null, null)); + boolean bump = !kb.ask(new Fact("Obstacle", x, false, y, false, true, null, null)); return searchedPositions[x][y] && bump && pit && wumpus; } else { return false; diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 02b5aac..0789fe7 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,11 +1,16 @@ -10 -0 0 0 0 0 0 S I 0 0 -0 H 0 0 0 0 0 I 0 0 -0 W I I 0 0 0 I I 0 -0 0 0 0 W 0 0 0 0 I -H 0 0 0 H 0 0 0 0 0 -0 0 0 0 0 I 0 0 0 0 -0 0 0 0 0 0 I 0 0 0 -0 0 0 0 0 0 H 0 0 0 -0 0 0 H I 0 0 H 0 0 -0 0 0 0 0 0 G 0 0 W +15 +0 0 I W 0 0 0 0 G 0 0 0 0 I 0 +0 0 H 0 W W W 0 I 0 0 H 0 H 0 +0 0 0 H 0 W 0 0 0 H 0 0 0 I H +H 0 0 W I 0 0 0 0 0 H 0 0 H W +0 H W H 0 0 0 0 W I 0 W 0 I H +0 W H H 0 0 0 H 0 0 0 0 0 H 0 +I 0 0 W 0 0 I 0 I 0 0 0 0 0 0 +0 I I 0 0 0 0 0 0 H 0 0 S H W +0 0 W I H 0 0 I 0 W I 0 0 I 0 +0 I H H I 0 H I H 0 H 0 0 0 0 +H 0 W W W 0 0 0 W H 0 0 0 0 0 +W W 0 0 0 0 0 0 I 0 0 0 0 0 0 +0 H 0 I 0 I 0 I H I W 0 0 I 0 +H 0 0 0 0 0 W W H 0 0 0 W H W +H 0 0 W 0 0 0 0 W 0 0 0 I H I From 7476c4200a82b852b554ca632908705be43099ec Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sun, 23 Oct 2016 08:28:42 -0600 Subject: [PATCH 170/191] x2 --- Wumpus/src/LogicExplorer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 8c1e38e..c3a963b 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -161,7 +161,7 @@ private void processPercepts() { //there might still be an issue with wum System.out.println("Explorer bumped after moving"); moveHistory.remove(moveHistory.size() - 1);//why bump again? removeFromFrontier(getForward()); - kb.tell(new Fact("Obsticle", getForward().x, false, getForward().y, false, false, null, null)); + kb.tell(new Fact("Obstacle", getForward().x, false, getForward().y, false, false, null, null)); } else if ((percepts & DEATH_PIT) == 0 && (percepts & DEATH_WUMPUS) == 0) {//you if you bump than the only inputted percept should've been bump if ((percepts & STENCH) != 0) { kb.tell(new Fact("Stench", location.x, false, location.y, false, false, null, null)); @@ -326,7 +326,7 @@ private boolean safeSpaceInFrontier() { Location loc = frontier.get(i); if (kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, true, null, null))) { if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, true, null, null))) { - //hopefully no obsticle is in frontier as it should be removed when found... + //hopefully no obstacle is in frontier as it should be removed when found... safeSpace = new Location(loc.x, loc.y); return true; From 4d7ac494dfbccc5b63812c4d78ba5624903d0bc7 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sun, 23 Oct 2016 09:00:21 -0600 Subject: [PATCH 171/191] sexy fix --- Wumpus/PerceptBoard.txt | 32 ++++++++++++++++---------------- Wumpus/clean.txt | 30 +++++++++++++++--------------- Wumpus/src/Driver.java | 2 +- Wumpus/src/KnowledgeBase.java | 16 ++++++++++++++-- Wumpus/src/LogicExplorer.java | 6 ++++-- Wumpus/world.txt | 30 +++++++++++++++--------------- 6 files changed, 65 insertions(+), 51 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 7a6256d..24559f3 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,16 +1,16 @@ -15 7 12 -0 0 7 32 2 2 2 0 8 0 0 1 0 5 0 -0 1 16 3 34 34 34 2 4 1 1 16 1 16 1 -1 0 1 18 3 34 2 0 1 16 1 1 0 5 18 -16 1 2 33 6 2 0 0 2 1 16 3 1 18 33 -1 18 33 19 1 0 0 3 32 6 3 32 2 5 18 -2 33 19 19 1 0 1 16 3 0 0 2 1 16 1 -4 2 3 33 2 0 4 1 4 1 0 0 0 1 2 -0 4 6 2 1 0 0 0 1 18 1 0 1 18 33 -0 2 33 7 16 1 1 4 3 33 7 0 0 5 2 -1 5 19 19 7 1 16 5 18 3 16 1 0 0 0 -18 3 35 35 34 2 1 2 33 18 1 0 0 0 0 -35 35 2 2 2 0 0 0 7 1 2 0 0 0 0 -3 18 1 4 0 4 2 7 17 7 32 2 2 5 2 -17 1 0 2 0 2 34 35 19 1 2 2 33 19 33 -17 1 2 32 2 0 2 2 33 2 0 0 7 17 7 +15 3 2 +4 2 32 2 0 0 2 33 3 2 5 18 33 18 1 +0 2 2 2 0 0 1 19 19 33 18 5 6 1 0 +2 32 6 32 2 0 0 5 1 2 1 4 4 2 0 +0 6 2 3 0 2 0 4 2 1 16 1 2 32 2 +0 2 33 18 3 32 2 2 33 2 9 0 0 2 0 +6 32 2 5 4 2 6 33 18 1 0 0 0 0 0 +0 2 0 1 2 7 33 2 5 0 0 0 0 0 5 +0 0 1 18 33 19 19 1 0 1 0 0 1 1 16 +0 1 18 35 2 5 1 0 1 17 1 1 16 1 5 +1 16 3 34 6 0 4 0 1 17 17 1 1 1 0 +0 1 1 2 0 0 1 1 16 1 1 0 1 16 1 +1 17 17 1 2 5 17 17 1 4 0 5 0 7 0 +1 1 1 2 32 2 3 1 5 4 1 17 3 32 2 +16 1 0 2 6 34 34 3 16 5 1 17 1 3 0 +1 16 3 34 34 2 3 16 1 0 0 1 5 16 1 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 4acedc3..f4f20d2 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,16 +1,16 @@ 15 -0 0 I W 0 0 0 0 0 0 0 0 0 I 0 -0 0 H 0 W W W 0 I 0 0 H 0 H 0 -0 0 0 H 0 W 0 0 0 H 0 0 0 I H -H 0 0 W I 0 0 0 0 0 H 0 0 H W -0 H W H 0 0 0 0 W I 0 W 0 I H -0 W H H 0 0 0 H 0 0 0 0 0 H 0 -I 0 0 W 0 0 I 0 I 0 0 0 0 0 0 -0 I I 0 0 0 0 0 0 H 0 0 0 H W -0 0 W I H 0 0 I 0 W I 0 0 I 0 -0 I H H I 0 H I H 0 H 0 0 0 0 -H 0 W W W 0 0 0 W H 0 0 0 0 0 -W W 0 0 0 0 0 0 I 0 0 0 0 0 0 -0 H 0 I 0 I 0 I H I W 0 0 I 0 -H 0 0 0 0 0 W W H 0 0 0 W H W -H 0 0 W 0 0 0 0 W 0 0 0 I H I +I 0 W 0 0 0 0 W 0 0 I H W H 0 +0 0 0 0 0 0 0 H H W H I I 0 0 +0 W I W 0 0 0 I 0 0 0 I I 0 0 +0 I 0 0 0 0 0 I 0 0 H 0 0 W 0 +0 0 W H 0 W 0 0 W 0 0 0 0 0 0 +I W 0 I I 0 I W H 0 0 0 0 0 0 +0 0 0 0 0 I W 0 I 0 0 0 0 0 I +0 0 0 H W H H 0 0 0 0 0 0 0 H +0 0 H W 0 I 0 0 0 H 0 0 H 0 I +0 H 0 W I 0 I 0 0 H H 0 0 0 0 +0 0 0 0 0 0 0 0 H 0 0 0 0 H 0 +0 H H 0 0 I H H 0 I 0 I 0 I 0 +0 0 0 0 W 0 0 0 I I 0 H 0 W 0 +H 0 0 0 I W W 0 H I 0 H 0 0 0 +0 H 0 W W 0 0 H 0 0 0 0 I H 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 72472d1..286ab91 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - makeGame(); + //makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 332f5a9..40d129f 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -172,15 +172,27 @@ public void initializeRules() { rules.add(new Clause(new Fact("Pit", World.size, false, 1, true, true, null, null))); //Wumpus(x,y)=>!Pit(x,y) - //!Wumpus(x,y) v !Pit(x,y) v !Obstacle + //!Wumpus(x,y) v !Pit(x,y) Clause notWumpusOrNotPit = new Clause(); Fact notWumpus = new Fact("Wumpus", 0, true, 1, true, true, null, null); Fact notPit = new Fact("Pit", 0, true, 1, true, true, null, null); // Fact notObstacle = new Fact("Obstacle",0,true,1,true,true,null,null); notWumpusOrNotPit.facts.add(notWumpus); notWumpusOrNotPit.facts.add(notPit); - // notWumpusOrNotPit.facts.add(notObstacle); rules.add(notWumpusOrNotPit); + // !Wumpus(x,y) v !Obstacle(x,y) + Clause notWumpusOrNotObstacle = new Clause(); + Fact notObstacle = new Fact("Obstacle",0,true,1,true,true,null,null); + notWumpusOrNotObstacle.facts.add(new Fact(notWumpus)); + notWumpusOrNotObstacle.facts.add(notObstacle); + + rules.add(notWumpusOrNotObstacle); + //!Pit v !Obstacle + Clause notPitOrNotObstacle = new Clause(); + notPitOrNotObstacle.facts.add(new Fact(notObstacle)); + notPitOrNotObstacle.facts.add(new Fact(notPit)); + rules.add(notPitOrNotObstacle); + } public boolean ask(Fact fact) { diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index c3a963b..7eca830 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -118,7 +118,8 @@ private void move(int action) { System.out.println("Explorer Action: shooting"); arrowCount--; percepts = (byte) world.action(SHOOT); - processPercepts(); + if((percepts & SCREAM) != 0) + processPercepts(); break; case QUIT: System.out.println("no possible solution"); @@ -155,6 +156,7 @@ private void processPercepts() { //there might still be an issue with wum updateLocation(); kb.tell(new Fact("Wumpus", location.x, false, location.y, false, true, null, null)); kb.tell(new Fact("Pit", location.x, false, location.y, false, true, null, null)); + kb.tell(new Fact("Obstacle", location.x, false, location.y, false, true, null, null)); removeFromFrontier(location); } if ((percepts & BUMP) == BUMP) { //theres soemthing funky here, hes bumping when there arent obsticales @@ -416,7 +418,7 @@ private boolean isValid(int x, int y) { if (x >= 0 && y >= 0 && x < World.size && y < World.size) { boolean wumpus = kb.ask(new Fact("Wumpus", x, false, y, false, true, null, null)); boolean pit = kb.ask(new Fact("Pit", x, false, y, false, true, null, null)); - boolean bump = !kb.ask(new Fact("Obstacle", x, false, y, false, true, null, null)); + boolean bump = kb.ask(new Fact("Obstacle", x, false, y, false, true, null, null)); return searchedPositions[x][y] && bump && pit && wumpus; } else { return false; diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 0789fe7..fc5d52c 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,16 +1,16 @@ 15 -0 0 I W 0 0 0 0 G 0 0 0 0 I 0 -0 0 H 0 W W W 0 I 0 0 H 0 H 0 -0 0 0 H 0 W 0 0 0 H 0 0 0 I H -H 0 0 W I 0 0 0 0 0 H 0 0 H W -0 H W H 0 0 0 0 W I 0 W 0 I H -0 W H H 0 0 0 H 0 0 0 0 0 H 0 -I 0 0 W 0 0 I 0 I 0 0 0 0 0 0 -0 I I 0 0 0 0 0 0 H 0 0 S H W -0 0 W I H 0 0 I 0 W I 0 0 I 0 -0 I H H I 0 H I H 0 H 0 0 0 0 -H 0 W W W 0 0 0 W H 0 0 0 0 0 -W W 0 0 0 0 0 0 I 0 0 0 0 0 0 -0 H 0 I 0 I 0 I H I W 0 0 I 0 -H 0 0 0 0 0 W W H 0 0 0 W H W -H 0 0 W 0 0 0 0 W 0 0 0 I H I +I 0 W 0 0 0 0 W 0 0 I H W H 0 +0 0 0 0 0 0 0 H H W H I I 0 0 +0 W I W 0 0 0 I 0 0 0 I I 0 0 +0 I S 0 0 0 0 I 0 0 H 0 0 W 0 +0 0 W H 0 W 0 0 W 0 G 0 0 0 0 +I W 0 I I 0 I W H 0 0 0 0 0 0 +0 0 0 0 0 I W 0 I 0 0 0 0 0 I +0 0 0 H W H H 0 0 0 0 0 0 0 H +0 0 H W 0 I 0 0 0 H 0 0 H 0 I +0 H 0 W I 0 I 0 0 H H 0 0 0 0 +0 0 0 0 0 0 0 0 H 0 0 0 0 H 0 +0 H H 0 0 I H H 0 I 0 I 0 I 0 +0 0 0 0 W 0 0 0 I I 0 H 0 W 0 +H 0 0 0 I W W 0 H I 0 H 0 0 0 +0 H 0 W W 0 0 H 0 0 0 0 I H 0 From bcc9c7dc229eb6cd312aa9e0c6e674881d36797e Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sun, 23 Oct 2016 09:38:13 -0600 Subject: [PATCH 172/191] added stuff to keep track of 'major decisions' Major decisions pretty much involve navigating to a new square --- Wumpus/PerceptBoard.txt | 19 +++---------------- Wumpus/clean.txt | 19 +++---------------- Wumpus/src/Driver.java | 2 +- Wumpus/src/LogicExplorer.java | 1 + Wumpus/src/World.java | 11 ++++++++--- Wumpus/world.txt | 19 +++---------------- 6 files changed, 19 insertions(+), 52 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 24559f3..430f3c7 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,16 +1,3 @@ -15 3 2 -4 2 32 2 0 0 2 33 3 2 5 18 33 18 1 -0 2 2 2 0 0 1 19 19 33 18 5 6 1 0 -2 32 6 32 2 0 0 5 1 2 1 4 4 2 0 -0 6 2 3 0 2 0 4 2 1 16 1 2 32 2 -0 2 33 18 3 32 2 2 33 2 9 0 0 2 0 -6 32 2 5 4 2 6 33 18 1 0 0 0 0 0 -0 2 0 1 2 7 33 2 5 0 0 0 0 0 5 -0 0 1 18 33 19 19 1 0 1 0 0 1 1 16 -0 1 18 35 2 5 1 0 1 17 1 1 16 1 5 -1 16 3 34 6 0 4 0 1 17 17 1 1 1 0 -0 1 1 2 0 0 1 1 16 1 1 0 1 16 1 -1 17 17 1 2 5 17 17 1 4 0 5 0 7 0 -1 1 1 2 32 2 3 1 5 4 1 17 3 32 2 -16 1 0 2 6 34 34 3 16 5 1 17 1 3 0 -1 16 3 34 34 2 3 16 1 0 0 1 5 16 1 +2 1 1 +0 8 +0 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index f4f20d2..fdf0b40 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,16 +1,3 @@ -15 -I 0 W 0 0 0 0 W 0 0 I H W H 0 -0 0 0 0 0 0 0 H H W H I I 0 0 -0 W I W 0 0 0 I 0 0 0 I I 0 0 -0 I 0 0 0 0 0 I 0 0 H 0 0 W 0 -0 0 W H 0 W 0 0 W 0 0 0 0 0 0 -I W 0 I I 0 I W H 0 0 0 0 0 0 -0 0 0 0 0 I W 0 I 0 0 0 0 0 I -0 0 0 H W H H 0 0 0 0 0 0 0 H -0 0 H W 0 I 0 0 0 H 0 0 H 0 I -0 H 0 W I 0 I 0 0 H H 0 0 0 0 -0 0 0 0 0 0 0 0 H 0 0 0 0 H 0 -0 H H 0 0 I H H 0 I 0 I 0 I 0 -0 0 0 0 W 0 0 0 I I 0 H 0 W 0 -H 0 0 0 I W W 0 H I 0 H 0 0 0 -0 H 0 W W 0 0 H 0 0 0 0 I H 0 +2 +0 0 +0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 286ab91..72472d1 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -13,7 +13,7 @@ public static void main(String[] args) throws IOException { //tester.testPathFinder(); //tester.testUnify(); // tester.testInferenceEngine(); - //makeGame(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index 7eca830..c02f05d 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -192,6 +192,7 @@ private boolean inFrontier(Location loc) { } private void decideNextAction() { + world.addMajorDecision(); if ((percepts & GLITTER) != 0) { move(GRAB); diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 3ededc3..a680541 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -9,10 +9,11 @@ public final class World { public static final int GRAB = 1, MOVE = 2, TURN_LEFT = 3, TURN_RIGHT = 4, SHOOT = 5, QUIT = 6; protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH_PIT = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; + private int majorDecisions = 0; private int arrowCount, x, y, direction = 0, score = 0, numMoves = 0, pitDeaths = 0, wumpusDeaths = 0, killedWumpus = 0; public static int size; private byte[][] perceptMap; - + private Agent explorer; public World(String fileName) { importMap(fileName); for (int i = 0; i < perceptMap.length; i++) { @@ -28,7 +29,6 @@ public World(String fileName) { } public void startGame(String id) { - Agent explorer; switch (id) { case "LogicExplorer": explorer = new LogicExplorer(this, arrowCount, x, y, direction); @@ -74,9 +74,14 @@ public void importMap(String fileName) { public byte getPercepts() { return perceptMap[x][y]; } - + + public void addMajorDecision(){ + majorDecisions++; + } + public void printStats() { System.out.println("Number of Actions: " + numMoves); + System.out.println("Number of major decisions: " + majorDecisions); System.out.println("Final score: " + score); System.out.println("Wumpus Deaths: " + wumpusDeaths); System.out.println("Pit Deaths: " + pitDeaths); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index fc5d52c..17e9b02 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,16 +1,3 @@ -15 -I 0 W 0 0 0 0 W 0 0 I H W H 0 -0 0 0 0 0 0 0 H H W H I I 0 0 -0 W I W 0 0 0 I 0 0 0 I I 0 0 -0 I S 0 0 0 0 I 0 0 H 0 0 W 0 -0 0 W H 0 W 0 0 W 0 G 0 0 0 0 -I W 0 I I 0 I W H 0 0 0 0 0 0 -0 0 0 0 0 I W 0 I 0 0 0 0 0 I -0 0 0 H W H H 0 0 0 0 0 0 0 H -0 0 H W 0 I 0 0 0 H 0 0 H 0 I -0 H 0 W I 0 I 0 0 H H 0 0 0 0 -0 0 0 0 0 0 0 0 H 0 0 0 0 H 0 -0 H H 0 0 I H H 0 I 0 I 0 I 0 -0 0 0 0 W 0 0 0 I I 0 H 0 W 0 -H 0 0 0 I W W 0 H I 0 H 0 0 0 -0 H 0 W W 0 0 H 0 0 0 0 I H 0 +2 +0 G +0 S From 8f81eb5be1bf47b1a7850c4163f55411047630ed Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sun, 23 Oct 2016 10:33:33 -0600 Subject: [PATCH 173/191] Zach's great code clean up, removed a bunch of deprecated functions, formatted all of Wilson's sloppy code, mostly just get pissed Wilson.. now working on reactive explorer. --- Wumpus/PerceptBoard.txt | 27 ++-- Wumpus/clean.txt | 27 ++-- Wumpus/src/Clause.java | 19 +-- Wumpus/src/Driver.java | 58 ++++---- Wumpus/src/Fact.java | 42 +++--- Wumpus/src/InferenceEngine.java | 9 +- Wumpus/src/KnowledgeBase.java | 10 +- Wumpus/src/MinusFunction.java | 4 +- Wumpus/src/PlusFunction.java | 4 +- Wumpus/src/Predicate.java | 4 - Wumpus/src/Rule.java | 25 ++-- Wumpus/src/Space.java | 1 + Wumpus/src/Tester.java | 72 +++++----- Wumpus/src/Unifier.java | 232 ++++---------------------------- Wumpus/src/Variable.java | 43 +++--- Wumpus/src/World.java | 63 ++++----- Wumpus/src/WumpusGame.java | 102 +++++++------- Wumpus/world.txt | 27 ++-- 18 files changed, 273 insertions(+), 496 deletions(-) delete mode 100644 Wumpus/src/Predicate.java diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 7a6256d..8b15063 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,16 +1,11 @@ -15 7 12 -0 0 7 32 2 2 2 0 8 0 0 1 0 5 0 -0 1 16 3 34 34 34 2 4 1 1 16 1 16 1 -1 0 1 18 3 34 2 0 1 16 1 1 0 5 18 -16 1 2 33 6 2 0 0 2 1 16 3 1 18 33 -1 18 33 19 1 0 0 3 32 6 3 32 2 5 18 -2 33 19 19 1 0 1 16 3 0 0 2 1 16 1 -4 2 3 33 2 0 4 1 4 1 0 0 0 1 2 -0 4 6 2 1 0 0 0 1 18 1 0 1 18 33 -0 2 33 7 16 1 1 4 3 33 7 0 0 5 2 -1 5 19 19 7 1 16 5 18 3 16 1 0 0 0 -18 3 35 35 34 2 1 2 33 18 1 0 0 0 0 -35 35 2 2 2 0 0 0 7 1 2 0 0 0 0 -3 18 1 4 0 4 2 7 17 7 32 2 2 5 2 -17 1 0 2 0 2 34 35 19 1 2 2 33 19 33 -17 1 2 32 2 0 2 2 33 2 0 0 7 17 7 +10 5 9 +16 1 16 1 0 4 0 0 0 4 +1 0 1 0 0 0 0 0 2 0 +0 0 0 0 0 0 0 2 32 2 +0 1 4 0 0 0 0 0 10 32 +1 16 1 0 1 0 0 0 0 2 +1 5 1 1 17 1 0 0 0 0 +17 1 16 1 17 1 0 0 2 0 +17 17 1 0 1 0 0 2 32 2 +1 1 0 4 2 0 0 0 2 0 +0 0 4 2 32 2 0 4 0 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 4acedc3..23cb570 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,16 +1,11 @@ -15 -0 0 I W 0 0 0 0 0 0 0 0 0 I 0 -0 0 H 0 W W W 0 I 0 0 H 0 H 0 -0 0 0 H 0 W 0 0 0 H 0 0 0 I H -H 0 0 W I 0 0 0 0 0 H 0 0 H W -0 H W H 0 0 0 0 W I 0 W 0 I H -0 W H H 0 0 0 H 0 0 0 0 0 H 0 -I 0 0 W 0 0 I 0 I 0 0 0 0 0 0 -0 I I 0 0 0 0 0 0 H 0 0 0 H W -0 0 W I H 0 0 I 0 W I 0 0 I 0 -0 I H H I 0 H I H 0 H 0 0 0 0 -H 0 W W W 0 0 0 W H 0 0 0 0 0 -W W 0 0 0 0 0 0 I 0 0 0 0 0 0 -0 H 0 I 0 I 0 I H I W 0 0 I 0 -H 0 0 0 0 0 W W H 0 0 0 W H W -H 0 0 W 0 0 0 0 W 0 0 0 I H I +10 +H 0 H 0 0 I 0 0 0 I +0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 W 0 +0 0 I 0 0 0 0 0 0 W +0 H 0 0 0 0 0 0 0 0 +0 I 0 0 H 0 0 0 0 0 +H 0 H 0 H 0 0 0 0 0 +H H 0 0 0 0 0 0 W 0 +0 0 0 I 0 0 0 0 0 0 +0 0 I 0 W 0 0 I 0 0 diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 8266d31..69326b2 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -4,7 +4,7 @@ /* A clause contains a list of predicates */ -public class Clause extends Fact { +public class Clause { ArrayList facts = new ArrayList<>(); @@ -18,6 +18,7 @@ public Clause(Clause clause) { } public Clause(Fact fact) { + facts.add(fact); } @@ -29,20 +30,4 @@ public static void printClause(Clause clause){ } System.out.println(""); } - - @Override - public boolean contains(Variable var) { - - for (Fact f : facts) { - if (f.contains(var) || f.equals(var)) { - return true; - } - } - return false; - } - - @Override - public ArrayList getArgs() { - return facts; - } } diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 72472d1..6f7a926 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -12,8 +12,8 @@ public static void main(String[] args) throws IOException { //Tester tester = new Tester(); //tester.testPathFinder(); //tester.testUnify(); - // tester.testInferenceEngine(); - makeGame(); + // tester.testInferenceEngine(); + makeGame(); World world = new World("PerceptBoard.txt"); world.startGame("LogicExplorer"); } @@ -21,38 +21,38 @@ public static void main(String[] args) throws IOException { public static void makeGame() throws IOException { boolean newBoard = true; - boolean newStart = true; - WumpusGame game; - if (newBoard) { - BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); - int[] prob = new int[3]; - System.out.println("Size of board: "); - - int size = Integer.parseInt(dataIn.readLine()); - System.out.print("% chance of generating pit: "); - prob[0] = Integer.parseInt(dataIn.readLine()); - System.out.println(); - System.out.print("% chance of generating obstacle: "); - prob[1] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - System.out.print("% chance of generating wumpus: "); - prob[2] = Integer.parseInt(dataIn.readLine()); - System.out.println(""); - game = new WumpusGame(size, prob); - } - if (newStart) { - game = new WumpusGame("clean.txt"); - } - World world = new World("PerceptBoard.txt"); - world.startGame("LogicExplorer"); + boolean newStart = true; + WumpusGame game; + if (newBoard) { + BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); + int[] prob = new int[3]; + System.out.println("Size of board: "); + + int size = 10;//Integer.parseInt(dataIn.readLine()); + System.out.print("% chance of generating pit: "); + prob[0] = 5;// Integer.parseInt(dataIn.readLine()); + System.out.println(); + System.out.print("% chance of generating obstacle: "); + prob[1] = 5;//Integer.parseInt(dataIn.readLine()); + System.out.println(""); + System.out.print("% chance of generating wumpus: "); + prob[2] = 5;//Integer.parseInt(dataIn.readLine()); + System.out.println(""); + game = new WumpusGame(size, prob); + } + if (newStart) { + game = new WumpusGame("clean.txt"); + } + World world = new World("PerceptBoard.txt"); + world.startGame("LogicExplorer"); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); - // World world = new World("PerceptBoard.txt"); + // World world = new World("PerceptBoard.txt"); //world.startGame("LogicExplorer"); - // world.startGame("ReactiveExplorer"); + // world.startGame("ReactiveExplorer"); //Agent explorer = new LogicExplorer(world); //world = new World("PerceptBoard.txt"); - // Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction, world.getPercepts(), world.arrowCount); + // Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction, world.getPercepts(), world.arrowCount); } } diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index b6085ec..345d61a 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -3,14 +3,18 @@ public class Fact extends Variable { - ArrayList variables = new ArrayList<>(); - String predicate; - boolean not; + protected ArrayList variables = new ArrayList<>(); + protected String predicate; + protected boolean not; + + public Fact() { + + } public Fact(Fact fact) { + not = fact.not; - - for(Variable var : fact.variables){ + for (Variable var : fact.variables) { variables.add(new Variable(var)); } predicate = fact.predicate; @@ -18,43 +22,29 @@ public Fact(Fact fact) { } - public Fact() { - } - public Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function) { + Variable var1 = new Variable(var1Val, var1Var, var1Function); Variable var2 = new Variable(var2Val, var2Var, var2Function); variables.add(var1); variables.add(var2); this.predicate = predicate; - + this.not = not; } public void printFact() { - if(not) + + if (not) { System.out.print("!"); + } System.out.print(predicate + "("); for (Variable var : variables) { var.printVariable(); - if(var != variables.get(variables.size()-1)) + if (var != variables.get(variables.size() - 1)) { System.out.print(", "); - } - System.out.print(") "); - } - - @Override - public boolean contains(Variable var) { - for (Variable v : variables) { - if (v.equals(var)) { - return true; } } - return false; - } - - @Override - public ArrayList getArgs() { - return variables; + System.out.print(") "); } } diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index c2d8cfb..76977c1 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -6,10 +6,12 @@ public class InferenceEngine { KnowledgeBase kb; public InferenceEngine(KnowledgeBase kb) { + this.kb = kb; } public boolean follows(Fact fact) { + Clause clause = new Clause(); //Step 1: negate input fact fact.not = !fact.not; @@ -31,7 +33,7 @@ public boolean follows(Fact fact) { } } - boolean keepGoing = true; + boolean keepGoing; //Step 2: Run negated facts against all known facts while (!kbClausesClone.isEmpty()) { keepGoing = true; @@ -83,6 +85,7 @@ public boolean follows(Fact fact) { } public void infer(Fact factStart) { + //look at each fact in each clause in kb.rules, if predicates match, and negations are opposite Fact fact = new Fact(factStart); ArrayList tempClone = new ArrayList<>(kb.rules); @@ -174,8 +177,8 @@ public void infer(Clause clauseToCheck) { } if (Unifier.equalWithSubs(fact, ruleFact, substitutions)) { clause.facts.remove(fact); - // System.out.println("Inferred:"); - // Clause.printClause(clause); + //System.out.println("Inferred:"); + //Clause.printClause(clause); kb.addToClauses(clause); return;//the smaller clause will be inferred against again, no need to keep trying rules immediately. } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 332f5a9..941a42f 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -176,25 +176,25 @@ public void initializeRules() { Clause notWumpusOrNotPit = new Clause(); Fact notWumpus = new Fact("Wumpus", 0, true, 1, true, true, null, null); Fact notPit = new Fact("Pit", 0, true, 1, true, true, null, null); - // Fact notObstacle = new Fact("Obstacle",0,true,1,true,true,null,null); + // Fact notObstacle = new Fact("Obstacle",0,true,1,true,true,null,null); notWumpusOrNotPit.facts.add(notWumpus); notWumpusOrNotPit.facts.add(notPit); - // notWumpusOrNotPit.facts.add(notObstacle); + // notWumpusOrNotPit.facts.add(notObstacle); rules.add(notWumpusOrNotPit); } public boolean ask(Fact fact) { - //if question follows from known facts ==> return true - //else return false return inferenceEngine.follows(fact); } public void tell(Clause clause) { + addToClauses(clause); } public void tell(Fact fact) { + //If told DeadWumpus delete the wumpus entry at that position and add !Wumpus(x,y) if (fact.predicate.equals("DeadWumpus")) { removeWumpusAndStench(fact); @@ -209,6 +209,7 @@ public void tell(Fact fact) { } private void removeWumpusAndStench(Fact fact) { + for (int i = clauses.size() - 1; i >= 0; i--) { if (clauses.get(i).facts.size() == 1) { Fact toRemove = clauses.get(i).facts.get(0); @@ -238,6 +239,7 @@ private void removeWumpusAndStench(Fact fact) { } private boolean factInClauses(Fact fact) { + for (Clause clause : clauses) { if (clause.facts.size() == 1) { Fact clauseFact = clause.facts.get(0); diff --git a/Wumpus/src/MinusFunction.java b/Wumpus/src/MinusFunction.java index fcd1176..7ad2309 100644 --- a/Wumpus/src/MinusFunction.java +++ b/Wumpus/src/MinusFunction.java @@ -3,15 +3,17 @@ public class MinusFunction implements IFunction { @Override public int process(int value) { + return value - 1; } @Override public Variable processVariable(Variable variable) { + Variable processedVariable = new Variable(); processedVariable.function = variable.function; processedVariable.isVariable = variable.isVariable; - processedVariable.modifier = variable.modifier - 1;//This is the important one + processedVariable.modifier = variable.modifier - 1; processedVariable.value = variable.value; processedVariable.variableId = variable.variableId; return processedVariable; diff --git a/Wumpus/src/PlusFunction.java b/Wumpus/src/PlusFunction.java index 07806d6..506c8df 100644 --- a/Wumpus/src/PlusFunction.java +++ b/Wumpus/src/PlusFunction.java @@ -3,15 +3,17 @@ public class PlusFunction implements IFunction { @Override public int process(int value) { + return value + 1; } @Override public Variable processVariable(Variable variable) { + Variable processedVariable = new Variable(); processedVariable.function = variable.function; processedVariable.isVariable = variable.isVariable; - processedVariable.modifier = variable.modifier + 1;//This is the important one + processedVariable.modifier = variable.modifier + 1; processedVariable.value = variable.value; processedVariable.variableId = variable.variableId; return processedVariable; diff --git a/Wumpus/src/Predicate.java b/Wumpus/src/Predicate.java deleted file mode 100644 index 33da3e3..0000000 --- a/Wumpus/src/Predicate.java +++ /dev/null @@ -1,4 +0,0 @@ - -public class Predicate { - -} diff --git a/Wumpus/src/Rule.java b/Wumpus/src/Rule.java index 813c4af..dde38f4 100644 --- a/Wumpus/src/Rule.java +++ b/Wumpus/src/Rule.java @@ -3,31 +3,26 @@ public class Rule { - public static final int AND = 1; - public static final int OR = 2; - public static final int IMPLIES = 3; - public static final int IFF = 4; - - ArrayList quantifiers = new ArrayList<>(); - boolean negated; - //boolean leftRuleNot; - //boolean rightRuleNot; - Fact fact; - Rule leftRule; - Rule rightRule; - boolean justFact = false; - int connector; + private static final int AND = 1, OR = 2, IMPLIES = 3, IFF = 4; + + private final ArrayList quantifiers = new ArrayList<>(); + private Fact fact; + private Rule leftRule, rightRule; + private boolean justFact = false; + private int connector; public Rule() { - + } public Rule(Fact fact) { + this.fact = fact; justFact = true; } public void printRule() { + if (justFact) { fact.printFact(); return; diff --git a/Wumpus/src/Space.java b/Wumpus/src/Space.java index 1859d78..f20515d 100644 --- a/Wumpus/src/Space.java +++ b/Wumpus/src/Space.java @@ -17,6 +17,7 @@ public boolean isHasWumpus() { } public void toggleWumpus() { + if (hasWumpus == true) { hasWumpus = false; filled = false; diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java index bbfa1cf..499f3e2 100644 --- a/Wumpus/src/Tester.java +++ b/Wumpus/src/Tester.java @@ -40,42 +40,42 @@ public void testUnify() { } public void testInferenceEngine() { - InferenceEngine engine = new InferenceEngine(new KnowledgeBase()); - - //FORALL x, y, Commutative(x,y) IFF Commutative(y,x) - Rule rule = new Rule(); - Quantifier x = new Quantifier(); - x.variableId = 0; - Quantifier y = new Quantifier(); - y.variableId = 1; - rule.quantifiers.add(x); - rule.quantifiers.add(y); - Rule leftRule = new Rule(); - Fact leftFact = new Fact(); - leftFact.predicate = "Commutative"; - Variable varX = new Variable(); - varX.isVariable = true; - varX.variableId = x.variableId; - Variable varY = new Variable(); - varY.isVariable = true; - varY.variableId = y.variableId; - leftFact.variables.add(varX); - leftFact.variables.add(varY); - leftRule.justFact = true; - leftRule.fact = leftFact; - Fact rightFact = new Fact(); - Rule rightRule = new Rule(); - rightFact.predicate = "Commutative"; - rightFact.variables.add(varY); - rightFact.variables.add(varX); - rightRule.justFact = true; - rightRule.fact = rightFact; - rule.leftRule = leftRule; - rule.rightRule = rightRule; - rule.connector = Rule.IFF; - - rule.printRule(); - Rule newRule = new Rule(); +// InferenceEngine engine = new InferenceEngine(new KnowledgeBase()); +// +// //FORALL x, y, Commutative(x,y) IFF Commutative(y,x) +// Rule rule = new Rule(); +// Quantifier x = new Quantifier(); +// x.variableId = 0; +// Quantifier y = new Quantifier(); +// y.variableId = 1; +// rule.quantifiers.add(x); +// rule.quantifiers.add(y); +// Rule leftRule = new Rule(); +// Fact leftFact = new Fact(); +// leftFact.predicate = "Commutative"; +// Variable varX = new Variable(); +// varX.isVariable = true; +// varX.variableId = x.variableId; +// Variable varY = new Variable(); +// varY.isVariable = true; +// varY.variableId = y.variableId; +// leftFact.variables.add(varX); +// leftFact.variables.add(varY); +// leftRule.justFact = true; +// leftRule.fact = leftFact; +// Fact rightFact = new Fact(); +// Rule rightRule = new Rule(); +// rightFact.predicate = "Commutative"; +// rightFact.variables.add(varY); +// rightFact.variables.add(varX); +// rightRule.justFact = true; +// rightRule.fact = rightFact; +// rule.leftRule = leftRule; +// rule.rightRule = rightRule; +// rule.connector = Rule.IFF; +// +// rule.printRule(); +// Rule newRule = new Rule(); // //Vd,t Facing(d,t) => Facing(d,t+1) OR Action(Turnleft,t) OR Action(TurnRight,t) // Rule rule2 = new Rule(); diff --git a/Wumpus/src/Unifier.java b/Wumpus/src/Unifier.java index d2c2540..f3086fd 100644 --- a/Wumpus/src/Unifier.java +++ b/Wumpus/src/Unifier.java @@ -1,90 +1,28 @@ import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; public class Unifier { - public Unifier() { + public static ArrayList unify(Fact f1, Fact f2) { - } - - /* - So...Unification sucks, ive tried a ton of different things and followed numerous textbook's psedocodes. - I'm going to leave some notes and resources ive used here and work on something else for the time being. - First off, in a nutshell the unify method needs to take in two sentences the kb sends it. These sentences can be - atomic variables, lists of variables (facts), or lists of facts (clauses). The function needs to return a set of substitutions - that make the two arguments the same, else it returns failure in some fassion (throw an exception maybe). I've tried using HashMaps to store these sets - of substitution bindings, which is what most people do online, but never got it to quite work. - - here are some links that may or may not be useful: - http://web.cecs.pdx.edu/~york/cs441/Luger_Idioms.pdf (part 4, pages ~300 - 330 or so) - https://github.com/aimacode/aima-java this is a repo provided by our textbook that has code implementations for everything. They go about it in a very complicated way though - https://en.wikipedia.org/wiki/Unification_(computer_science) the wiki is super technical, but I did learn a fair amount from it - https://baylor-ir.tdl.org/baylor-ir/bitstream/handle/2104/2887/AshishArteThesis.pdf?sequence=4 this is dense af, but has some good information and explaination of pseduo code - - - */ - public static ArrayList unify1(Fact fact1, Fact fact2) { - if (!canUnify(fact1, fact2)) { - return new ArrayList(); - } - - return new ArrayList(); - } - - public static boolean canUnify(Fact fact1, Fact fact2) { - //Predicates don't match - if (!fact1.predicate.equals(fact2.predicate)) { - return false; - } - return true; - } - - //this is the general outline of how to go about unification... - public static Substitute unify1(Variable p, Variable q, Substitute theta) { - - if (p.equals(q)) { //success - theta.varIdToSubstitute = p.variableId; - theta.valToSubstituteWith = q.variableId; - return theta; - } else if (p.variableId != q.variableId) { - //bind p to q and insert (p/q) into theta - - return theta; - } else if (q.variableId != q.variableId) { - //bind q to p and insert (q/p) into theta - return theta; - } else if (true) { //if r is a variable? Occurs checks need to be done here i think - //theta = the union of (theta, {r/s}) - //unify(substitutionOf(theta, p), substitutionOf(theta, q), theta) - } else if (true) { //if s is a variable? Occurs checks need to be done here i think - //theta = the union of (theta, {s/r}) - //unify(substitutionOf(theta, p), substitutionOf(theta, q), theta) - } else { - //failure - } - return null; - } - public static ArrayList unify(Fact f1, Fact f2){ ArrayList subs = new ArrayList<>(); - if(!f1.predicate.equals(f2.predicate)) + if (!f1.predicate.equals(f2.predicate)) { return subs; - + } + Fact fact1 = new Fact(f1); Fact fact2 = new Fact(f2); //check if fact1 has a variable for (int i = 0; i < fact1.variables.size(); i++) { Variable tempVar = fact1.variables.get(i); - if(tempVar.isVariable){ - if(!fact2.variables.get(i).isVariable){ + if (tempVar.isVariable) { + if (!fact2.variables.get(i).isVariable) { Substitute sub = new Substitute(); sub.varIdToSubstitute = tempVar.variableId; sub.valToSubstituteWith = fact2.variables.get(i).value; - if(tempVar.function != null){ + if (tempVar.function != null) { int val = tempVar.function.process(0); - sub.valToSubstituteWith += -1*val; + sub.valToSubstituteWith += -1 * val; } subs.add(sub); } @@ -92,162 +30,50 @@ public static ArrayList unify(Fact f1, Fact f2){ } for (int i = 0; i < fact2.variables.size(); i++) { Variable tempVar = fact2.variables.get(i); - if(tempVar.isVariable){ - if(!fact1.variables.get(i).isVariable){ + if (tempVar.isVariable) { + if (!fact1.variables.get(i).isVariable) { Substitute sub = new Substitute(); sub.varIdToSubstitute = tempVar.variableId; sub.valToSubstituteWith = fact1.variables.get(i).value; - if(tempVar.function != null){ + if (tempVar.function != null) { int val = tempVar.function.process(0); - sub.valToSubstituteWith += -1*val; + sub.valToSubstituteWith += -1 * val; } subs.add(sub); } } } - - if(equalWithSubs(fact1,fact2,subs)) + + if (equalWithSubs(fact1, fact2, subs)) { return subs; - else return new ArrayList(); + } else { + return new ArrayList<>(); + } } - - public static boolean equalWithSubs(Fact fact1, Fact fact2, ArrayList subs){ + + public static boolean equalWithSubs(Fact fact1, Fact fact2, ArrayList subs) { + substitute(subs, fact1); substitute(subs, fact2); - for(int i = 0; i < fact1.variables.size(); i++){ + for (int i = 0; i < fact1.variables.size(); i++) { Variable var1 = fact1.variables.get(i); Variable var2 = fact2.variables.get(i); - if(var1.value != var2.value) + if (var1.value != var2.value) { return false; - } - return true; - } - - public static void substitute(ArrayList subs, Fact fact){ - for(Substitute sub : subs){ - for(Variable var: fact.variables){ - if(var.isVariable && var.variableId == sub.varIdToSubstitute){ - var.isVariable = false; - var.value = sub.valToSubstituteWith; - } } } + return true; } - public static ArrayList unify2(Fact f1, Fact f2) { - - ArrayList subs = new ArrayList<>(); - if(!f1.predicate.equals(f2.predicate)){ - return subs; - } - - ArrayList vars = new ArrayList<>(); - Fact temp = new Fact(); - temp.variables.add(f1.variables.get(0)); - temp.variables.add(f2.variables.get(0)); - vars.add(temp); - - temp = new Fact(); - temp.variables.add(f1.variables.get(1)); - temp.variables.add(f2.variables.get(1)); - vars.add(temp); + public static void substitute(ArrayList subs, Fact fact) { - while (!vars.isEmpty()) { - Fact tempFact = vars.remove(0); - if (!tempFact.variables.get(0).equals(tempFact.variables.get(1))) { - if (tempFact.variables.get(0).isVariable) { - Substitute s = new Substitute(); - s.varIdToSubstitute = tempFact.variables.get(0).value; - s.valToSubstituteWith = tempFact.variables.get(1).value; - subs.add(s); - } else if (tempFact.variables.get(1).isVariable) { - Substitute s = new Substitute(); - s.varIdToSubstitute = tempFact.variables.get(1).value; - s.valToSubstituteWith = tempFact.variables.get(0).value; - subs.add(s); - } - else{ - return new ArrayList(); + for (Substitute sub : subs) { + for (Variable var : fact.variables) { + if (var.isVariable && var.variableId == sub.varIdToSubstitute) { + var.isVariable = false; + var.value = sub.valToSubstituteWith; } } } - return subs; - } - - private static boolean occursCheck(Variable var, Variable term) { - //determins if term is in var - return true; } } - -// public SubstitutionString unify(Variable x, Variable y) { -// return (SubstitutionString) unify(x, y, (SubstitutionString) new LinkedHashMap<>()); -// } -// -// private Map unify(Variable x, Variable y, Map theta) { -// -// if (theta == null) { -// return null; -// } else if (x.equals(y)) { -// return theta; -// } else if (x instanceof Clause) { -// return unifyClause((Clause) x, y, theta); -// } else if (y instanceof Clause) { -// return unifyClause((Clause) y, x, theta); -// } else if (!x.isUnary() && !y.isUnary()) { -// // return unify(x.getArgs(), y.getArgs(), unifyOps(x.getOp(), y.getOp(), theta)); -// return theta; -// } else { -// return null; -// } -// } -// -// private Map unifyClause(Clause c, Variable v, Map theta) { -// -// if (!Fact.class.isInstance(v)) { -// return null; -// } else if (theta.keySet().contains(c)) { -// return unify(theta.get(c), v, theta); -// } else if (theta.keySet().contains(v)) { -// return unify(c, theta.get(v), theta); -// } else if (occurCheck(theta, c, v)) { -// return null; -// } else { -// cascadeSubstitution(theta, c, (Fact) v); -// return theta; -// } -// } -// -// private Map cascadeSubstitution(Map theta, Clause c, Fact f) { -// -// return theta; -// } -// -// private Map unifyOps(String x, String y, Map theta) { -// -// if (theta == null) { -// return null; -// } else if (x.equals(y)) { -// return theta; -// } else { -// return null; -// } -// } -// -// private boolean occurCheck(Map theta, Clause clause, Variable var) { -// -// if (clause.equals(var)) { -// return true; -// } else if (!theta.containsKey(var)) { -// if (!var.isVariable) { //is function -// //recursivly perform occursCheck on each of functions arguments? -// if (true) { //tem -// return true; -// } -// } -// } else { -// return occurCheck(theta, clause, theta.get(var)); -// } -// return false; -// } -//} diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index e74034a..17bd874 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -1,7 +1,4 @@ -import java.util.ArrayList; - - public class Variable { boolean isVariable; @@ -9,44 +6,36 @@ public class Variable { IFunction function; int variableId; int modifier; - public Variable(Variable var){ + + public Variable(Variable var) { + isVariable = var.isVariable; value = var.value; function = var.function; variableId = var.variableId; modifier = var.modifier; } - public Variable(){} - public Variable(int value, boolean isVariable, IFunction function){ + + public Variable() { + } + + public Variable(int value, boolean isVariable, IFunction function) { + this.isVariable = isVariable; - if(!isVariable) + if (!isVariable) { this.value = value; - else + } else { this.variableId = value; + } this.function = function; } + public void printVariable() { - + if (isVariable) { System.out.print((char) (variableId + 97)); - } - else + } else { System.out.print(value); - } - - public int getValue() { - return value; - } - - public boolean contains(Variable var) { - return false; - } - - public String getOp() { - return "" + modifier; - } - - public ArrayList getArgs() { - return null; + } } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 3ededc3..5fb53b4 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -15,13 +15,12 @@ public final class World { public World(String fileName) { importMap(fileName); - for (int i = 0; i < perceptMap.length; i++) { + for (byte[] cell : perceptMap) { for (int j = 0; j < perceptMap.length; j++) { - if ((perceptMap[i][j] & DEATH_WUMPUS) != 0) { + if ((cell[j] & DEATH_WUMPUS) != 0) { arrowCount++; } } - } System.out.println("Starting world:"); printWorld(); @@ -37,15 +36,11 @@ public void startGame(String id) { explorer = new ReactiveExplorer(this, arrowCount, x, y, direction); break; } - } public void importMap(String fileName) { - try { - - FileReader in1 = new FileReader(fileName); - BufferedReader reader1 = new BufferedReader(in1); + try { FileReader in = new FileReader(fileName); BufferedReader reader = new BufferedReader(in); String next = reader.readLine(); @@ -56,7 +51,7 @@ public void importMap(String fileName) { y = Integer.parseInt(next.substring(0, next.indexOf(" "))); perceptMap = new byte[size][size]; int i = 0; - while ((next = reader.readLine()) != null) {//((Integer) reader.read()).toString()).equals("-1")) { + while ((next = reader.readLine()) != null) { int j = 0; while (next.contains(" ") && !next.equals(" ")) { perceptMap[i][j] = (byte) Integer.parseInt(next.substring(0, next.indexOf(" "))); @@ -76,6 +71,7 @@ public byte getPercepts() { } public void printStats() { + System.out.println("Number of Actions: " + numMoves); System.out.println("Final score: " + score); System.out.println("Wumpus Deaths: " + wumpusDeaths); @@ -96,7 +92,7 @@ public void printWorld() { } else if ((perceptMap[j][i] & DEATH_PIT) == DEATH_PIT) { System.out.print("P "); } else if ((perceptMap[j][i] & GLITTER) == GLITTER) { - System.out.print("G "); + System.out.print("\u001B[32m" + "G " + "\u001B[0m"); } else if ((perceptMap[j][i] & BUMP) == BUMP) { System.out.print("B "); } else if (perceptMap[j][i] > 9) { @@ -112,14 +108,9 @@ public void printWorld() { } public byte action(int action) { - byte percepts = action(action, true); - printWorld(); - return percepts; - } - public byte action(int action, boolean thing) { - System.out.println("Action: " + action); - System.out.println(""); + printWorld(); + System.out.println("Action: " + action + "\n"); numMoves++; switch (action) { case GRAB: @@ -133,7 +124,6 @@ public byte action(int action, boolean thing) { } else { System.out.println("Error: Gold not found"); } - break; case MOVE: score--; switch (direction) { @@ -249,9 +239,9 @@ public byte action(int action, boolean thing) { return perceptMap[x][y]; case SHOOT: //shoot logic - if (arrowCount == 0) { + if (arrowCount <= 0) { System.out.println("Error: Out of arrows"); - return -1; //out of arrows, which shouldn't be possible + return -1; } arrowCount--; score -= 10; @@ -261,7 +251,7 @@ public byte action(int action, boolean thing) { if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); return SCREAM; - } else if ((perceptMap[x][i] & BUMP) != 0) { //hits Obstacle + } else if ((perceptMap[x][i] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -272,7 +262,7 @@ public byte action(int action, boolean thing) { if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; - } else if ((perceptMap[i][y] & BUMP) != 0) { //hits Obstacle + } else if ((perceptMap[i][y] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -283,7 +273,7 @@ public byte action(int action, boolean thing) { if ((perceptMap[x][i] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(x, i); return SCREAM; - } else if ((perceptMap[x][i] & BUMP) != 0) { //hits Obstacle + } else if ((perceptMap[x][i] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -294,7 +284,7 @@ public byte action(int action, boolean thing) { if ((perceptMap[i][y] & DEATH_WUMPUS) != 0) { //hits Wumpus removeWumpus(i, y); return SCREAM; - } else if ((perceptMap[i][y] & BUMP) != 0) { //hits Obstacle + } else if ((perceptMap[i][y] & BUMP) != 0) { //hits Obstacle return perceptMap[x][y]; } } @@ -343,20 +333,23 @@ private void removeWumpus(int x, int y) { killedWumpus++; score += 10; } - - public void remakeStenches(){ + + public void remakeStenches() { for (int i = 0; i < perceptMap.length; i++) { for (int j = 0; j < perceptMap.length; j++) { - if((perceptMap[i][j] & DEATH_WUMPUS) != 0){ - if(i>0){ - perceptMap[i-1][j] |= STENCH; + if ((perceptMap[i][j] & DEATH_WUMPUS) != 0) { + if (i > 0) { + perceptMap[i - 1][j] |= STENCH; + } + if (i < size - 1) { + perceptMap[i + 1][j] |= STENCH; + } + if (j > 0) { + perceptMap[i][j - 1] |= STENCH; + } + if (j < size - 1) { + perceptMap[i][j + 1] |= STENCH; } - if(i < size-1) - perceptMap[i+1][j] |= STENCH; - if(j > 0) - perceptMap[i][j-1] |= STENCH; - if(j < size-1) - perceptMap[i][j+1] |= STENCH; } } } diff --git a/Wumpus/src/WumpusGame.java b/Wumpus/src/WumpusGame.java index 4c0aebc..8a479b1 100644 --- a/Wumpus/src/WumpusGame.java +++ b/Wumpus/src/WumpusGame.java @@ -8,21 +8,20 @@ import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; -import java.util.HashMap; import java.util.Random; public class WumpusGame { private int boardSize, wumpus, startX, startY; private Space[][] board; - private Random random = new Random(); - private HashMap probabilityGeneration; + private final Random random = new Random(); private int[] prob; private byte[][] perceptBoard; - protected final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; - private PrintWriter out = new PrintWriter(new File("PerceptBoard.txt")); + private final byte BREEZE = 0b00000001, STENCH = 0b0000010, BUMP = 0b00000100, GLITTER = 0b00001000, DEATH = 0b00010000, DEATH_WUMPUS = 0b00100000, SCREAM = 0b01000000; + private final PrintWriter out = new PrintWriter(new File("PerceptBoard.txt")); public WumpusGame(String fileName) throws FileNotFoundException { + newStart(fileName); System.setOut(new PrintStream(new FileOutputStream("world.txt"))); printBoards(); @@ -30,14 +29,15 @@ public WumpusGame(String fileName) throws FileNotFoundException { } public WumpusGame(int boardSize, int[] prob) throws FileNotFoundException { + this.boardSize = boardSize; this.prob = prob; perceptBoard = new byte[boardSize][boardSize]; board = new Space[boardSize][boardSize]; setBoard(); initializeBoard(); - PrintStream out = new PrintStream(new FileOutputStream("world.txt")); - System.setOut(out); + PrintStream worldOut = new PrintStream(new FileOutputStream("world.txt")); + System.setOut(worldOut); printBoards(); System.setOut(new PrintStream(new FileOutputStream("clean.txt"))); printBoards(); @@ -45,15 +45,17 @@ public WumpusGame(int boardSize, int[] prob) throws FileNotFoundException { } - public void setBoard() { - for (int i = 0; i < board.length; i++) { - for (int j = 0; j < board[i].length; j++) { - board[i][j] = new Space(); + private void setBoard() { + + for (Space[] space : board) { + for (int j = 0; j < space.length; j++) { + space[j] = new Space(); } } } - public void newStart(String fileName) { + private void newStart(String fileName) { + try { FileReader in = new FileReader(fileName); BufferedReader reader = new BufferedReader(in); @@ -68,14 +70,18 @@ public void newStart(String fileName) { int j = 0; while (next.contains(" ") && !next.equals(" ")) { String temp = next.substring(0, next.indexOf(" ")); - if (temp.equals("W")) { - placeWumpus(i, j); - } else if (temp.equals("I")) { - placeObstacle(i, j); - } else if (temp.equals("H")) { - placePit(i, j); - } else { - + switch (temp) { + case "W": + placeWumpus(i, j); + break; + case "I": + placeObstacle(i, j); + break; + case "H": + placePit(i, j); + break; + default: + break; } next = next.substring(next.indexOf(" ") + 1, next.length()); @@ -83,8 +89,8 @@ public void newStart(String fileName) { } i++; } - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException | NumberFormatException ex) { + System.out.println("Error with world generation: " + ex); } int x = random.nextInt(boardSize); @@ -101,11 +107,12 @@ public void newStart(String fileName) { } startX = x; startY = y; - placeStart(x, y); + board[x][y].setStart(true); } - public void initializeBoard() { + private void initializeBoard() { + for (int i = board.length - 1; i >= 0; i--) { for (int j = 0; j < board[i].length; j++) { chooseState(i, j); @@ -113,7 +120,7 @@ public void initializeBoard() { } } - public void chooseState(int x, int y) { + private void chooseState(int x, int y) { if ((random.nextInt(100) + 1) <= prob[0]) { placePit(x, y); @@ -124,11 +131,13 @@ public void chooseState(int x, int y) { } } - public void placePercept(int x, int y, byte percept) { + private void placePercept(int x, int y, byte percept) { + perceptBoard[x][y] |= percept; } - public void placeAdjacentPercept(int x, int y, byte percept) { + private void placeAdjacentPercept(int x, int y, byte percept) { + if (x > 0) { placePercept(x - 1, y, percept); } @@ -143,43 +152,41 @@ public void placeAdjacentPercept(int x, int y, byte percept) { } } - public void placeObstacle(int x, int y) { + private void placeObstacle(int x, int y) { + board[x][y].setHasObstacle(true); placePercept(x, y, BUMP); } - public void placePit(int x, int y) { + private void placePit(int x, int y) { + board[x][y].setHasHole(true); placeAdjacentPercept(x, y, BREEZE); placePercept(x, y, DEATH); } - public void placeGold(int x, int y) { + private void placeGold(int x, int y) { + board[x][y].setHasGold(true); placePercept(x, y, GLITTER); } - public void placeWumpus(int x, int y) { + private void placeWumpus(int x, int y) { + board[x][y].toggleWumpus(); placeAdjacentPercept(x, y, STENCH); placePercept(x, y, DEATH_WUMPUS); wumpus++; } - public void placeStart(int x, int y) { - board[x][y].setStart(true); - } - - public void checkBlockedStart() { + private void printBoards() { - } - - public void printBoards() { printBoard(); printPerceptBoard(); } - public void printPerceptBoard() { + private void printPerceptBoard() { + out.print(boardSize + " "); out.print(startX + " "); out.print(startY + " "); @@ -193,19 +200,20 @@ public void printPerceptBoard() { out.close(); } - public void printBoard() { + private void printBoard() { + System.out.println(boardSize); for (int i = 0; i < boardSize; i++) { - for (int j = 0; j < board[i].length; j++) { - if (board[i][j].isHasWumpus()) { + for (Space item : board[i]) { + if (item.isHasWumpus()) { System.out.print("W "); - } else if (board[i][j].isHasHole()) { + } else if (item.isHasHole()) { System.out.print("H "); - } else if (board[i][j].isHasObstacle()) { + } else if (item.isHasObstacle()) { System.out.print("I "); - } else if (board[i][j].isHasGold()) { + } else if (item.isHasGold()) { System.out.print("G "); - } else if (board[i][j].isStart()) { + } else if (item.isStart()) { System.out.print("S "); } else { System.out.print("0 "); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 0789fe7..8acc975 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,16 +1,11 @@ -15 -0 0 I W 0 0 0 0 G 0 0 0 0 I 0 -0 0 H 0 W W W 0 I 0 0 H 0 H 0 -0 0 0 H 0 W 0 0 0 H 0 0 0 I H -H 0 0 W I 0 0 0 0 0 H 0 0 H W -0 H W H 0 0 0 0 W I 0 W 0 I H -0 W H H 0 0 0 H 0 0 0 0 0 H 0 -I 0 0 W 0 0 I 0 I 0 0 0 0 0 0 -0 I I 0 0 0 0 0 0 H 0 0 S H W -0 0 W I H 0 0 I 0 W I 0 0 I 0 -0 I H H I 0 H I H 0 H 0 0 0 0 -H 0 W W W 0 0 0 W H 0 0 0 0 0 -W W 0 0 0 0 0 0 I 0 0 0 0 0 0 -0 H 0 I 0 I 0 I H I W 0 0 I 0 -H 0 0 0 0 0 W W H 0 0 0 W H W -H 0 0 W 0 0 0 0 W 0 0 0 I H I +10 +H 0 H 0 0 I 0 0 0 I +0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 W 0 +0 0 I 0 0 0 0 0 G W +0 H 0 0 0 0 0 0 0 0 +0 I 0 0 H 0 0 0 0 S +H 0 H 0 H 0 0 0 0 0 +H H 0 0 0 0 0 0 W 0 +0 0 0 I 0 0 0 0 0 0 +0 0 I 0 W 0 0 I 0 0 From 2d9742fb9619677a421b00fb3c82759356e9b9b4 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sun, 23 Oct 2016 11:32:39 -0600 Subject: [PATCH 174/191] and now we're synced? --- Wumpus/src/KnowledgeBase.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 2c2e54a..377dc77 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -179,10 +179,6 @@ public void initializeRules() { // Fact notObstacle = new Fact("Obstacle",0,true,1,true,true,null,null); notWumpusOrNotPit.facts.add(notWumpus); notWumpusOrNotPit.facts.add(notPit); -<<<<<<< HEAD -======= - // notWumpusOrNotPit.facts.add(notObstacle); ->>>>>>> 8f81eb5be1bf47b1a7850c4163f55411047630ed rules.add(notWumpusOrNotPit); // !Wumpus(x,y) v !Obstacle(x,y) Clause notWumpusOrNotObstacle = new Clause(); From 2c694617de1453bea0cae3189ca74b76cfec247a Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sun, 23 Oct 2016 15:31:34 -0600 Subject: [PATCH 175/191] quiting --- Wumpus/src/ReactiveExplorer.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 5f25349..d2857a1 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -9,6 +9,7 @@ public class ReactiveExplorer extends Agent { private static final int FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3; public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { + super(world, arrows, x, y, direction); prevLocation = location; percepts = world.getPercepts(); @@ -22,17 +23,19 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { } private void run() { + int i = 0; while (i < 500) { move(); i++; } + world.action(QUIT); } private void move() { if ((percepts & STENCH) != STENCH && (percepts & BREEZE) != BREEZE) { //all adjacent spaces are safe - if((percepts & GLITTER) != GLITTER){ + if ((percepts & GLITTER) != GLITTER) { world.action(GRAB); } updateSafe(); @@ -65,8 +68,8 @@ private void move() { turnRight(); percepts = world.action(MOVE); } - } else { - //if forward is safe, go forward + } else { //if forward is safe, go forward + ArrayList safeMoves = new ArrayList(); if (getSafe(FORWARD)) { safeMoves.add(FORWARD); From 286df5e3d89d29af6576d22d8f7717b5bf70e69d Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Sun, 23 Oct 2016 15:36:38 -0600 Subject: [PATCH 176/191] Testing with reactive --- Wumpus/PerceptBoard.txt | 12 +++--------- Wumpus/clean.txt | 6 ------ Wumpus/src/Driver.java | 10 +++++----- Wumpus/src/ReactiveExplorer.java | 8 ++++---- Wumpus/world.txt | 12 +++--------- 5 files changed, 15 insertions(+), 33 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index dec75fe..7f7d1ba 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,17 +1,11 @@ -<<<<<<< HEAD -2 1 1 -0 8 -0 0 -======= -10 5 9 +10 3 3 16 1 16 1 0 4 0 0 0 4 1 0 1 0 0 0 0 0 2 0 0 0 0 0 0 0 0 2 32 2 -0 1 4 0 0 0 0 0 10 32 +0 1 4 0 0 0 0 0 2 32 1 16 1 0 1 0 0 0 0 2 1 5 1 1 17 1 0 0 0 0 -17 1 16 1 17 1 0 0 2 0 +17 1 16 1 17 1 0 8 2 0 17 17 1 0 1 0 0 2 32 2 1 1 0 4 2 0 0 0 2 0 0 0 4 2 32 2 0 4 0 0 ->>>>>>> 8f81eb5be1bf47b1a7850c4163f55411047630ed diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index ef56be0..23cb570 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,8 +1,3 @@ -<<<<<<< HEAD -2 -0 0 -0 0 -======= 10 H 0 H 0 0 I 0 0 0 I 0 0 0 0 0 0 0 0 0 0 @@ -14,4 +9,3 @@ H 0 H 0 H 0 0 0 0 0 H H 0 0 0 0 0 0 W 0 0 0 0 I 0 0 0 0 0 0 0 0 I 0 W 0 0 I 0 0 ->>>>>>> 8f81eb5be1bf47b1a7850c4163f55411047630ed diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 6f7a926..5d959db 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -14,14 +14,14 @@ public static void main(String[] args) throws IOException { //tester.testUnify(); // tester.testInferenceEngine(); makeGame(); - World world = new World("PerceptBoard.txt"); - world.startGame("LogicExplorer"); +// World world = new World("PerceptBoard.txt"); +// world.startGame("LogicExplorer"); } public static void makeGame() throws IOException { - boolean newBoard = true; - boolean newStart = true; + boolean newBoard = false; + boolean newStart = false; WumpusGame game; if (newBoard) { BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); @@ -44,7 +44,7 @@ public static void makeGame() throws IOException { game = new WumpusGame("clean.txt"); } World world = new World("PerceptBoard.txt"); - world.startGame("LogicExplorer"); + world.startGame("ReactiveExplorer"); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); // World world = new World("PerceptBoard.txt"); //world.startGame("LogicExplorer"); diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 5f25349..8e5be84 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -31,10 +31,11 @@ private void run() { private void move() { + if ((percepts & GLITTER) == GLITTER) { + world.action(GRAB); + } + if ((percepts & STENCH) != STENCH && (percepts & BREEZE) != BREEZE) { //all adjacent spaces are safe - if((percepts & GLITTER) != GLITTER){ - world.action(GRAB); - } updateSafe(); //go in random direction int rand = random.nextInt(3); @@ -49,7 +50,6 @@ private void move() { turnLeft(); percepts = world.action(MOVE); if ((percepts & BUMP) != BUMP) { - System.out.println("Not Reseting"); return; } turnRight(); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index e046a99..7fc8498 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,17 +1,11 @@ -<<<<<<< HEAD -2 -0 G -0 S -======= 10 H 0 H 0 0 I 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 -0 0 I 0 0 0 0 0 G W +0 0 I S 0 0 0 0 0 W 0 H 0 0 0 0 0 0 0 0 -0 I 0 0 H 0 0 0 0 S -H 0 H 0 H 0 0 0 0 0 +0 I 0 0 H 0 0 0 0 0 +H 0 H 0 H 0 0 G 0 0 H H 0 0 0 0 0 0 W 0 0 0 0 I 0 0 0 0 0 0 0 0 I 0 W 0 0 I 0 0 ->>>>>>> 8f81eb5be1bf47b1a7850c4163f55411047630ed From 162348d758743ce4ac9382dcb44088df2a02d714 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Sun, 23 Oct 2016 15:56:31 -0600 Subject: [PATCH 177/191] stupid fucking switches --- Wumpus/src/ReactiveExplorer.java | 19 +++++++++++++++---- Wumpus/src/World.java | 1 + 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index e10c1ef..03d4e6b 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -25,7 +25,7 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { private void run() { int i = 0; - while (i < 500) { + while (i < 10) { move(); i++; } @@ -37,6 +37,8 @@ private void move() { if ((percepts & GLITTER) == GLITTER) { world.action(GRAB); } + + System.out.println("Making new move"); if ((percepts & STENCH) != STENCH && (percepts & BREEZE) != BREEZE) { //all adjacent spaces are safe updateSafe(); @@ -46,27 +48,32 @@ private void move() { switch (rand) { case 0: //try to go forward percepts = world.action(MOVE); - if ((percepts & BUMP) != BUMP) { + if ((percepts & BUMP) == BUMP) { return; } + break; case 1: //try to go left turnLeft(); percepts = world.action(MOVE); - if ((percepts & BUMP) != BUMP) { + if ((percepts & BUMP) == BUMP) { + System.out.println("Didn't reset position"); return; } turnRight(); + break; case 2: //try go right turnRight(); percepts = world.action(MOVE); - if ((percepts & BUMP) != BUMP) { + if ((percepts & BUMP) == BUMP) { return; } turnLeft(); + break; default: //turn around turnRight(); turnRight(); percepts = world.action(MOVE); + break; } } else { //if forward is safe, go forward @@ -92,6 +99,7 @@ private void move() { if ((percepts & BUMP) != BUMP) { return; } + break; case 1: turnLeft(); percepts = world.action(MOVE); @@ -99,6 +107,7 @@ private void move() { return; } turnRight(); + break; case 2: turnRight(); percepts = world.action(MOVE); @@ -106,10 +115,12 @@ private void move() { return; } turnLeft(); + break; default: turnRight(); turnRight(); percepts = world.action(MOVE); + break; } } } diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 6442c55..f007262 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -224,6 +224,7 @@ public byte action(int action) { System.out.println("(World) thinks location after move: " + x + ", " + y); return perceptMap[x][y]; } else { + System.out.println("Bumped"); System.out.println("(World) thinks location after move: " + x + ", " + y); return BUMP; } From 85cf5f4d72ce3c045ebc9f0255610acb8e6ed001 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Sun, 23 Oct 2016 17:04:22 -0600 Subject: [PATCH 178/191] Removed the return statements after bumping, still want the direction to reset --- Wumpus/src/ReactiveExplorer.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 03d4e6b..2c70412 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -48,25 +48,15 @@ private void move() { switch (rand) { case 0: //try to go forward percepts = world.action(MOVE); - if ((percepts & BUMP) == BUMP) { - return; - } break; case 1: //try to go left turnLeft(); percepts = world.action(MOVE); - if ((percepts & BUMP) == BUMP) { - System.out.println("Didn't reset position"); - return; - } turnRight(); break; case 2: //try go right turnRight(); percepts = world.action(MOVE); - if ((percepts & BUMP) == BUMP) { - return; - } turnLeft(); break; default: //turn around From fdba5a6e188bd6a23d4ba4874b3e09d3baacabe2 Mon Sep 17 00:00:00 2001 From: Ryan Brand Date: Sun, 23 Oct 2016 17:09:04 -0600 Subject: [PATCH 179/191] Undone what I did. Causes it to not break for some reason --- Wumpus/src/ReactiveExplorer.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 2c70412..b0a4fe9 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -25,7 +25,7 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { private void run() { int i = 0; - while (i < 10) { + while (i < 50000) { move(); i++; } @@ -48,15 +48,25 @@ private void move() { switch (rand) { case 0: //try to go forward percepts = world.action(MOVE); + if ((percepts & BUMP) != BUMP) { + return; + } break; case 1: //try to go left turnLeft(); percepts = world.action(MOVE); + if ((percepts & BUMP) == BUMP) { + System.out.println("Didn't reset position"); + return; + } turnRight(); break; case 2: //try go right turnRight(); percepts = world.action(MOVE); + if ((percepts & BUMP) != BUMP) { + return; + } turnLeft(); break; default: //turn around From fb398ce3ec5d5ddf9fcb505642055d94fd85293b Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sun, 23 Oct 2016 17:13:51 -0600 Subject: [PATCH 180/191] Can now kill wimps --- Wumpus/src/Driver.java | 6 +++--- Wumpus/src/ReactiveExplorer.java | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 5d959db..2a55b6e 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -20,15 +20,15 @@ public static void main(String[] args) throws IOException { public static void makeGame() throws IOException { - boolean newBoard = false; - boolean newStart = false; + boolean newBoard = true; + boolean newStart = true; WumpusGame game; if (newBoard) { BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); int[] prob = new int[3]; System.out.println("Size of board: "); - int size = 10;//Integer.parseInt(dataIn.readLine()); + int size = 5;//Integer.parseInt(dataIn.readLine()); System.out.print("% chance of generating pit: "); prob[0] = 5;// Integer.parseInt(dataIn.readLine()); System.out.println(); diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 2c70412..aff9017 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -25,7 +25,7 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { private void run() { int i = 0; - while (i < 10) { + while (i < 1000) { move(); i++; } @@ -48,21 +48,25 @@ private void move() { switch (rand) { case 0: //try to go forward percepts = world.action(MOVE); + processPercepts(); break; case 1: //try to go left turnLeft(); percepts = world.action(MOVE); + processPercepts(); turnRight(); break; case 2: //try go right turnRight(); percepts = world.action(MOVE); + processPercepts(); turnLeft(); break; default: //turn around turnRight(); turnRight(); percepts = world.action(MOVE); + processPercepts(); break; } } else { //if forward is safe, go forward @@ -115,6 +119,20 @@ private void move() { } } } + + private void processPercepts() { + + if ((percepts & BUMP) == BUMP) { + return; + } + if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { + killWumpus(); + } + if ((percepts & DEATH_PIT) == DEATH_PIT) { + Location forward = getForward(); + safeMap[forward.x][forward.y] = false; + } + } private boolean getSafe(int direction) { From c85c536df4c2e28f3ea6e91fe09d606443b44a41 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sun, 23 Oct 2016 17:19:12 -0600 Subject: [PATCH 181/191] This guy's fuckin stupid.. --- Wumpus/src/ReactiveExplorer.java | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index aff9017..5c170a6 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -9,7 +9,7 @@ public class ReactiveExplorer extends Agent { private static final int FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3; public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { - + super(world, arrows, x, y, direction); prevLocation = location; percepts = world.getPercepts(); @@ -23,7 +23,7 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { } private void run() { - + int i = 0; while (i < 1000) { move(); @@ -37,7 +37,7 @@ private void move() { if ((percepts & GLITTER) == GLITTER) { world.action(GRAB); } - + System.out.println("Making new move"); if ((percepts & STENCH) != STENCH && (percepts & BREEZE) != BREEZE) { //all adjacent spaces are safe @@ -70,7 +70,7 @@ private void move() { break; } } else { //if forward is safe, go forward - + ArrayList safeMoves = new ArrayList(); if (getSafe(FORWARD)) { safeMoves.add(FORWARD); @@ -119,16 +119,18 @@ private void move() { } } } - + private void processPercepts() { - + if ((percepts & BUMP) == BUMP) { - return; - } - if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { + + Location forward = getForward(); + if (forward.x >= 0 && forward.x < World.size && forward.y >= 0 && forward.y < World.size) { + safeMap[forward.x][forward.y] = false; + } + } else if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { killWumpus(); - } - if ((percepts & DEATH_PIT) == DEATH_PIT) { + } else if ((percepts & DEATH_PIT) == DEATH_PIT) { Location forward = getForward(); safeMap[forward.x][forward.y] = false; } From 5ed80e5b68021376a93f2e27b4274015e3df180d Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sun, 23 Oct 2016 17:20:25 -0600 Subject: [PATCH 182/191] Forgot a boundary check --- Wumpus/src/ReactiveExplorer.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 5c170a6..bd82cdc 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -132,7 +132,9 @@ private void processPercepts() { killWumpus(); } else if ((percepts & DEATH_PIT) == DEATH_PIT) { Location forward = getForward(); - safeMap[forward.x][forward.y] = false; + if (forward.x >= 0 && forward.x < World.size && forward.y >= 0 && forward.y < World.size) { + safeMap[forward.x][forward.y] = false; + } } } From 77278e5cd7a1a30dd94590f4d8ff8bf5fa8124c3 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sun, 23 Oct 2016 17:30:57 -0600 Subject: [PATCH 183/191] Theres a problem right now with not keeping track of location properly...also there are state conflicts between setting a space with a bump to be unsafe, but if an adjacent space is entered with no stench or breeze that gets overwritten --- Wumpus/src/ReactiveExplorer.java | 135 +++++++------------------------ 1 file changed, 28 insertions(+), 107 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index bd82cdc..57104ec 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -54,13 +54,11 @@ private void move() { turnLeft(); percepts = world.action(MOVE); processPercepts(); - turnRight(); break; case 2: //try go right turnRight(); percepts = world.action(MOVE); processPercepts(); - turnLeft(); break; default: //turn around turnRight(); @@ -90,30 +88,23 @@ private void move() { switch (rand) { case 0: percepts = world.action(MOVE); - if ((percepts & BUMP) != BUMP) { - return; - } + processPercepts(); break; case 1: turnLeft(); percepts = world.action(MOVE); - if ((percepts & BUMP) != BUMP) { - return; - } - turnRight(); + processPercepts(); break; case 2: turnRight(); percepts = world.action(MOVE); - if ((percepts & BUMP) != BUMP) { - return; - } - turnLeft(); + processPercepts(); break; default: turnRight(); turnRight(); percepts = world.action(MOVE); + processPercepts(); break; } } @@ -123,7 +114,6 @@ private void move() { private void processPercepts() { if ((percepts & BUMP) == BUMP) { - Location forward = getForward(); if (forward.x >= 0 && forward.x < World.size && forward.y >= 0 && forward.y < World.size) { safeMap[forward.x][forward.y] = false; @@ -135,6 +125,9 @@ private void processPercepts() { if (forward.x >= 0 && forward.x < World.size && forward.y >= 0 && forward.y < World.size) { safeMap[forward.x][forward.y] = false; } + } else { + this.prevLocation = this.location; + this.location = getForward(); } } @@ -161,105 +154,33 @@ private boolean getSafe(int direction) { private void updateSafe() { - if (location.x < World.size - 1) { - safeMap[location.x + 1][location.y] = true; - } - if (location.x > 0) { - safeMap[location.x - 1][location.y] = true; - } - if (location.y < World.size - 1) { - safeMap[location.x][location.y + 1] = true; - } - if (location.y > 0) { - safeMap[location.x][location.y - 1] = true; + try { + if (location.x < World.size - 1) { + safeMap[location.x + 1][location.y] = true; + } + if (location.x > 0) { + safeMap[location.x - 1][location.y] = true; + } + if (location.y < World.size - 1) { + safeMap[location.x][location.y + 1] = true; + } + if (location.y > 0) { + safeMap[location.x][location.y - 1] = true; + } + } catch (Exception e) { + System.out.println("Location: " + location.x + ", " + location.y); + throw e; } + } -// private byte move(int action) { -// -// switch (action) { -// case GRAB: -// return world.action(GRAB); -// case MOVE: -// percepts = world.action(MOVE); -// if ((percepts & BUMP) != BUMP) { //did not bump into anything -// prevLocation = location; -// prevState = curState; -// updateLocation(); -// } else if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { //killed by a wumpus, therefore use revive potion and take revenge -// killWumpus(); -// } else if ((percepts & DEATH) == DEATH) { //killed by a pit -// Location temp = location; -// location = prevLocation; -// prevLocation = temp; -// prevState = State.UNSAFE; -// curState = State.EXPLORED; -// } -// return percepts; -// case TURN_LEFT: -// turnLeft(); -// return world.action(TURN_LEFT); -// case TURN_RIGHT: -// turnRight(); -// return world.action(TURN_RIGHT); -// case SHOOT: -// arrowCount--; -// return world.action(SHOOT); -// case QUIT: -// return world.action(QUIT); -// } -// return 0b00000000; -// } -// public void decideNextAction() { //select safe neighboring cell else select unsafe neighboring cell -// -// if (((percepts & STENCH) != 0) && ((percepts & BREEZE) != 0)) { //all adjacent spaces are safe -// int rand = random.nextInt(3); -// switch (rand) { -// case 0: //go forward -// move(MOVE); -// break; -// case 1: //turn left and go forward -// move(TURN_LEFT); -// move(MOVE); -// break; -// case 2: //turn right and go forward -// move(TURN_RIGHT); -// move(MOVE); -// break; -// default: -// System.out.println("Invalid case: random action, reactive explorer (safe) rand = " + rand); -// } -// } else { //neighboring cells may not be safe -// -// if (prevState == State.SAFE) { //there might be a situation where the agent move back and forth between 3 safe safe spaces here we might need to account for -// move(TURN_LEFT); -// move(TURN_LEFT); -// move(MOVE); -// } else { //pick random move -// int rand = random.nextInt(3); -// switch (random.nextInt(3)) { -// case 0: //go forward -// move(MOVE); -// break; -// case 1: //turn left and go forward -// move(TURN_LEFT); -// move(MOVE); -// break; -// case 2: //turn right and go forward -// move(TURN_RIGHT); -// move(MOVE); -// break; -// default: -// System.out.println("Invalid case: random action, reactive explorer (unsafe) rand = " + rand); -// } -// } -// } -// } private void killWumpus() { - System.out.println("kill wumpus"); - location = prevLocation; + + System.out.println("Killing wumpus.."); percepts = world.getPercepts(); world.action(SHOOT); world.action(MOVE); + this.prevLocation = this.location; + this.location = getForward(); } } From c6e6e363b74df2614b316f7cf11c824f1f226172 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sun, 23 Oct 2016 17:58:52 -0600 Subject: [PATCH 184/191] Theres an out of bounds problem, investigating.. --- Wumpus/src/ReactiveExplorer.java | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 57104ec..b551fbd 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -4,8 +4,7 @@ public class ReactiveExplorer extends Agent { private Location prevLocation; - private State curState, prevState; - private boolean safeMap[][]; + private State safeMap[][]; private static final int FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3; public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { @@ -13,12 +12,8 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { super(world, arrows, x, y, direction); prevLocation = location; percepts = world.getPercepts(); - if (((percepts & STENCH) != STENCH) && ((percepts & BREEZE) != BREEZE)) { - curState = State.SAFE; - prevState = State.SAFE; - } - safeMap = new boolean[World.size][World.size]; - safeMap[location.x][location.y] = true; + safeMap = new State[World.size][World.size]; + safeMap[location.x][location.y] = State.SAFE; run(); } @@ -116,14 +111,14 @@ private void processPercepts() { if ((percepts & BUMP) == BUMP) { Location forward = getForward(); if (forward.x >= 0 && forward.x < World.size && forward.y >= 0 && forward.y < World.size) { - safeMap[forward.x][forward.y] = false; + safeMap[forward.x][forward.y] = State.UNSAFE; } } else if ((percepts & DEATH_WUMPUS) == DEATH_WUMPUS) { killWumpus(); } else if ((percepts & DEATH_PIT) == DEATH_PIT) { Location forward = getForward(); if (forward.x >= 0 && forward.x < World.size && forward.y >= 0 && forward.y < World.size) { - safeMap[forward.x][forward.y] = false; + safeMap[forward.x][forward.y] = State.UNSAFE; } } else { this.prevLocation = this.location; @@ -137,13 +132,13 @@ private boolean getSafe(int direction) { int trueDirection = (this.direction - direction + 4) % 4; switch (trueDirection) { case NORTH: - return safeMap[location.x][location.y + 1]; + return (safeMap[location.x][location.y + 1] == State.SAFE); case SOUTH: - return safeMap[location.x][location.y - 1]; + return safeMap[location.x][location.y - 1] == State.SAFE; case EAST: - return safeMap[location.x + 1][location.y]; + return safeMap[location.x + 1][location.y] == State.SAFE; case WEST: - return safeMap[location.x - 1][location.y]; + return safeMap[location.x - 1][location.y] == State.SAFE; } System.out.println("Invalid direction mapping, getSafe(): " + direction + " " + trueDirection); return true; @@ -156,16 +151,16 @@ private void updateSafe() { try { if (location.x < World.size - 1) { - safeMap[location.x + 1][location.y] = true; + safeMap[location.x + 1][location.y] = State.SAFE; } if (location.x > 0) { - safeMap[location.x - 1][location.y] = true; + safeMap[location.x - 1][location.y] = State.SAFE; } if (location.y < World.size - 1) { - safeMap[location.x][location.y + 1] = true; + safeMap[location.x][location.y + 1] = State.SAFE; } if (location.y > 0) { - safeMap[location.x][location.y - 1] = true; + safeMap[location.x][location.y - 1] = State.SAFE; } } catch (Exception e) { System.out.println("Location: " + location.x + ", " + location.y); From 3a2e0ec98f5c535c24e4d91ef519c69ea5670935 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sun, 23 Oct 2016 18:06:28 -0600 Subject: [PATCH 185/191] Fixing out of bounds...WIP --- Wumpus/src/ReactiveExplorer.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index b551fbd..a68ad6e 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -121,8 +121,7 @@ private void processPercepts() { safeMap[forward.x][forward.y] = State.UNSAFE; } } else { - this.prevLocation = this.location; - this.location = getForward(); + goForward(); } } @@ -168,6 +167,12 @@ private void updateSafe() { } } + + private void goForward() { + + this.location = getForward(); + this.prevLocation = this.location; + } private void killWumpus() { From 8c96ec5dea751f7d84364a869cf3d4416f2b10d9 Mon Sep 17 00:00:00 2001 From: SirLagsalot Date: Sun, 23 Oct 2016 18:35:19 -0600 Subject: [PATCH 186/191] fixed? --- Wumpus/src/ReactiveExplorer.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index a68ad6e..e9de329 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -20,7 +20,7 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { private void run() { int i = 0; - while (i < 1000) { + while (i < 10000) { move(); i++; } @@ -167,11 +167,15 @@ private void updateSafe() { } } - + private void goForward() { - - this.location = getForward(); - this.prevLocation = this.location; + + Location forward = getForward(); + if (forward.x >= 0 && forward.x < World.size && forward.y >= 0 && forward.y < World.size) { + this.location = getForward(); + this.prevLocation = this.location; + } + } private void killWumpus() { From 966dd9c68a672495cbfe4f1185ae4c5ceec56a6f Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sun, 23 Oct 2016 19:17:15 -0600 Subject: [PATCH 187/191] nice convenience things --- Wumpus/PerceptBoard.txt | 32 +++++++++++++++++++++----------- Wumpus/clean.txt | 32 +++++++++++++++++++++----------- Wumpus/src/Driver.java | 2 +- Wumpus/src/World.java | 27 ++++++++++++++++++++++++++- Wumpus/world.txt | 32 +++++++++++++++++++++----------- 5 files changed, 90 insertions(+), 35 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 7f7d1ba..8a3a1e5 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,11 +1,21 @@ -10 3 3 -16 1 16 1 0 4 0 0 0 4 -1 0 1 0 0 0 0 0 2 0 -0 0 0 0 0 0 0 2 32 2 -0 1 4 0 0 0 0 0 2 32 -1 16 1 0 1 0 0 0 0 2 -1 5 1 1 17 1 0 0 0 0 -17 1 16 1 17 1 0 8 2 0 -17 17 1 0 1 0 0 2 32 2 -1 1 0 4 2 0 0 0 2 0 -0 0 4 2 32 2 0 4 0 0 +20 4 13 +32 3 0 1 0 0 4 0 0 0 1 16 1 2 0 0 0 0 0 0 +3 16 1 16 1 0 0 0 4 0 4 1 3 32 2 0 0 0 0 0 +0 1 0 1 1 0 0 2 0 2 0 1 16 3 0 0 1 0 0 0 +4 0 0 3 16 1 6 32 3 32 2 0 1 0 2 1 16 1 0 0 +0 0 2 32 3 4 0 3 16 3 1 1 16 3 32 2 1 0 1 0 +0 0 0 2 0 0 2 32 3 1 16 3 3 0 2 1 2 1 16 1 +0 0 0 4 0 0 0 6 1 17 3 34 34 6 1 18 33 2 1 1 +0 0 4 1 0 0 0 0 1 17 1 6 34 2 0 1 2 0 5 16 +4 0 1 16 1 0 0 0 0 1 0 0 2 2 0 0 2 0 0 1 +0 4 0 1 0 0 0 0 0 0 0 2 34 34 2 2 32 3 0 0 +0 0 0 0 0 0 0 2 0 0 0 0 3 2 0 0 3 16 5 0 +0 1 0 0 0 0 2 32 2 0 0 1 16 5 0 1 18 1 1 0 +1 16 5 0 4 4 0 2 8 0 0 0 1 0 0 2 33 3 16 1 +0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 0 +0 1 0 0 0 0 0 0 0 0 0 1 2 0 0 2 32 2 0 0 +1 16 1 4 1 0 0 0 0 0 5 18 33 2 0 0 2 4 0 0 +0 1 0 1 16 1 0 0 0 0 0 1 2 0 0 0 0 0 0 0 +0 1 0 1 1 0 0 0 0 0 0 2 4 0 0 0 0 0 0 0 +1 16 1 16 1 0 0 0 0 0 2 32 2 0 4 0 0 1 0 0 +0 1 0 1 4 0 0 0 0 0 0 2 0 0 0 0 1 16 1 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 23cb570..21af1b1 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,11 +1,21 @@ -10 -H 0 H 0 0 I 0 0 0 I -0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 W 0 -0 0 I 0 0 0 0 0 0 W -0 H 0 0 0 0 0 0 0 0 -0 I 0 0 H 0 0 0 0 0 -H 0 H 0 H 0 0 0 0 0 -H H 0 0 0 0 0 0 W 0 -0 0 0 I 0 0 0 0 0 0 -0 0 I 0 W 0 0 I 0 0 +20 +W 0 0 0 0 0 I 0 0 0 0 H 0 0 0 0 0 0 0 0 +0 H 0 H 0 0 0 0 I 0 I 0 0 W 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 +I 0 0 0 H 0 I W 0 W 0 0 0 0 0 0 H 0 0 0 +0 0 0 W 0 I 0 0 H 0 0 0 H 0 W 0 0 0 0 0 +0 0 0 0 0 0 0 W 0 0 H 0 0 0 0 0 0 0 H 0 +0 0 0 I 0 0 0 I 0 H 0 W W I 0 H W 0 0 0 +0 0 I 0 0 0 0 0 0 H 0 I W 0 0 0 0 0 I H +I 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 I 0 0 0 0 0 0 0 0 0 0 W W 0 0 W 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H I 0 +0 0 0 0 0 0 0 W 0 0 0 0 H I 0 0 H 0 0 0 +0 H I 0 I I 0 0 0 0 0 0 0 0 0 0 W 0 H 0 +0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 +0 H 0 I 0 0 0 0 0 0 I H W 0 0 0 0 I 0 0 +0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 +0 H 0 H 0 0 0 0 0 0 0 W 0 0 I 0 0 0 0 0 +0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 2a55b6e..5f325ab 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -28,7 +28,7 @@ public static void makeGame() throws IOException { int[] prob = new int[3]; System.out.println("Size of board: "); - int size = 5;//Integer.parseInt(dataIn.readLine()); + int size = 20;//Integer.parseInt(dataIn.readLine()); System.out.print("% chance of generating pit: "); prob[0] = 5;// Integer.parseInt(dataIn.readLine()); System.out.println(); diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index f007262..1dcd102 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -112,10 +112,33 @@ public void printWorld() { System.out.println(""); } + public void printAction(int action){ + switch(action){ + case GRAB: + System.out.println("GRAB"); + break; + case MOVE: + System.out.println("MOVE"); + break; + case TURN_LEFT: + System.out.println("TURN_LEFT"); + break; + case TURN_RIGHT: + System.out.println("TURN_RIGHT"); + break; + case SHOOT: + System.out.println("SHOOT"); + break; + case QUIT: + System.out.println("QUIT"); + } + } public byte action(int action) { printWorld(); - System.out.println("Action: " + action + "\n"); + System.out.print("World Action: "); + printAction(action); + System.out.println(""); numMoves++; switch (action) { case GRAB: @@ -301,6 +324,8 @@ public byte action(int action) { } case QUIT: System.out.println("Agent elected to end game."); + printStats(); + System.out.println("Gold was not found"); System.exit(0); break; default: diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 7fc8498..96e7699 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,11 +1,21 @@ -10 -H 0 H 0 0 I 0 0 0 I -0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 W 0 -0 0 I S 0 0 0 0 0 W -0 H 0 0 0 0 0 0 0 0 -0 I 0 0 H 0 0 0 0 0 -H 0 H 0 H 0 0 G 0 0 -H H 0 0 0 0 0 0 W 0 -0 0 0 I 0 0 0 0 0 0 -0 0 I 0 W 0 0 I 0 0 +20 +W 0 0 0 0 0 I 0 0 0 0 H 0 0 0 0 0 0 0 0 +0 H 0 H 0 0 0 0 I 0 I 0 0 W 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 +I 0 0 0 H 0 I W 0 W 0 0 0 0 0 0 H 0 0 0 +0 0 0 W 0 I 0 0 H 0 0 0 H S W 0 0 0 0 0 +0 0 0 0 0 0 0 W 0 0 H 0 0 0 0 0 0 0 H 0 +0 0 0 I 0 0 0 I 0 H 0 W W I 0 H W 0 0 0 +0 0 I 0 0 0 0 0 0 H 0 I W 0 0 0 0 0 I H +I 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 I 0 0 0 0 0 0 0 0 0 0 W W 0 0 W 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H I 0 +0 0 0 0 0 0 0 W 0 0 0 0 H I 0 0 H 0 0 0 +0 H I 0 I I 0 0 G 0 0 0 0 0 0 0 W 0 H 0 +0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 +0 H 0 I 0 0 0 0 0 0 I H W 0 0 0 0 I 0 0 +0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 +0 H 0 H 0 0 0 0 0 0 0 W 0 0 I 0 0 0 0 0 +0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 From c37ae3492f737ae77ecdb8ee87c7c2cbf88ca549 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sun, 23 Oct 2016 19:31:59 -0600 Subject: [PATCH 188/191] wilson stuff on reactive --- Wumpus/PerceptBoard.txt | 42 +++++++++++++-------------- Wumpus/clean.txt | 40 +++++++++++++------------- Wumpus/src/ReactiveExplorer.java | 49 ++++++++++++++++---------------- Wumpus/world.txt | 40 +++++++++++++------------- 4 files changed, 86 insertions(+), 85 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 8a3a1e5..14bb8cc 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,21 +1,21 @@ -20 4 13 -32 3 0 1 0 0 4 0 0 0 1 16 1 2 0 0 0 0 0 0 -3 16 1 16 1 0 0 0 4 0 4 1 3 32 2 0 0 0 0 0 -0 1 0 1 1 0 0 2 0 2 0 1 16 3 0 0 1 0 0 0 -4 0 0 3 16 1 6 32 3 32 2 0 1 0 2 1 16 1 0 0 -0 0 2 32 3 4 0 3 16 3 1 1 16 3 32 2 1 0 1 0 -0 0 0 2 0 0 2 32 3 1 16 3 3 0 2 1 2 1 16 1 -0 0 0 4 0 0 0 6 1 17 3 34 34 6 1 18 33 2 1 1 -0 0 4 1 0 0 0 0 1 17 1 6 34 2 0 1 2 0 5 16 -4 0 1 16 1 0 0 0 0 1 0 0 2 2 0 0 2 0 0 1 -0 4 0 1 0 0 0 0 0 0 0 2 34 34 2 2 32 3 0 0 -0 0 0 0 0 0 0 2 0 0 0 0 3 2 0 0 3 16 5 0 -0 1 0 0 0 0 2 32 2 0 0 1 16 5 0 1 18 1 1 0 -1 16 5 0 4 4 0 2 8 0 0 0 1 0 0 2 33 3 16 1 -0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 0 -0 1 0 0 0 0 0 0 0 0 0 1 2 0 0 2 32 2 0 0 -1 16 1 4 1 0 0 0 0 0 5 18 33 2 0 0 2 4 0 0 -0 1 0 1 16 1 0 0 0 0 0 1 2 0 0 0 0 0 0 0 -0 1 0 1 1 0 0 0 0 0 0 2 4 0 0 0 0 0 0 0 -1 16 1 16 1 0 0 0 0 0 2 32 2 0 4 0 0 1 0 0 -0 1 0 1 4 0 0 0 0 0 0 2 0 0 0 0 1 16 1 0 +20 9 11 +16 1 0 0 0 4 0 0 0 4 0 2 0 0 0 1 1 0 1 16 +1 0 0 0 0 0 0 0 0 0 2 32 2 0 1 17 17 1 0 3 +0 0 0 0 0 0 0 0 2 0 0 2 0 0 0 1 1 0 2 32 +1 0 0 0 0 0 0 2 34 2 0 0 0 0 0 1 0 0 0 2 +16 1 0 0 0 0 0 2 34 2 2 0 0 0 1 17 1 0 0 4 +1 0 0 0 1 0 0 0 2 2 32 2 0 0 1 17 1 0 0 0 +0 4 0 1 16 1 4 0 0 0 2 4 0 0 0 1 0 0 4 4 +2 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 +32 2 0 0 0 0 0 1 17 17 17 1 0 1 0 0 0 0 0 0 +2 0 0 0 0 1 1 16 1 1 1 0 1 16 1 0 0 0 0 0 +0 0 0 0 1 16 1 1 0 0 4 0 0 1 0 4 0 0 0 0 +1 0 0 0 0 1 5 16 1 0 1 0 0 0 0 0 0 0 0 0 +16 1 0 0 2 2 0 1 0 1 16 5 0 0 0 0 0 2 0 0 +1 1 0 2 34 34 2 0 0 0 3 0 0 0 0 0 2 32 2 0 +1 16 1 0 2 2 0 0 0 3 32 2 0 0 0 0 0 3 0 2 +0 1 0 0 0 8 0 1 1 16 3 0 0 0 0 0 1 16 3 32 +4 0 0 0 0 0 1 16 1 1 0 0 0 1 0 0 0 1 0 2 +0 0 0 0 0 0 0 1 0 0 0 0 1 16 3 0 4 0 0 0 +1 0 0 1 4 4 0 4 2 0 0 0 2 3 32 2 0 1 0 0 +16 5 1 16 1 0 0 2 32 2 0 2 32 2 2 0 1 16 1 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 21af1b1..e45c939 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,21 +1,21 @@ 20 -W 0 0 0 0 0 I 0 0 0 0 H 0 0 0 0 0 0 0 0 -0 H 0 H 0 0 0 0 I 0 I 0 0 W 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 -I 0 0 0 H 0 I W 0 W 0 0 0 0 0 0 H 0 0 0 -0 0 0 W 0 I 0 0 H 0 0 0 H 0 W 0 0 0 0 0 -0 0 0 0 0 0 0 W 0 0 H 0 0 0 0 0 0 0 H 0 -0 0 0 I 0 0 0 I 0 H 0 W W I 0 H W 0 0 0 -0 0 I 0 0 0 0 0 0 H 0 I W 0 0 0 0 0 I H -I 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 I 0 0 0 0 0 0 0 0 0 0 W W 0 0 W 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H I 0 -0 0 0 0 0 0 0 W 0 0 0 0 H I 0 0 H 0 0 0 -0 H I 0 I I 0 0 0 0 0 0 0 0 0 0 W 0 H 0 -0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 -0 H 0 I 0 0 0 0 0 0 I H W 0 0 0 0 I 0 0 -0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 -0 H 0 H 0 0 0 0 0 0 0 W 0 0 I 0 0 0 0 0 -0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 +H 0 0 0 0 I 0 0 0 I 0 0 0 0 0 0 0 0 0 H +0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 H H 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W +0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 +H 0 0 0 0 0 0 0 W 0 0 0 0 0 0 H 0 0 0 I +0 0 0 0 0 0 0 0 0 0 W 0 0 0 0 H 0 0 0 0 +0 I 0 0 H 0 I 0 0 0 0 I 0 0 0 0 0 0 I I +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +W 0 0 0 0 0 0 0 H H H 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 H 0 0 0 0 0 H 0 0 0 0 0 0 +0 0 0 0 0 H 0 0 0 0 I 0 0 0 0 I 0 0 0 0 +0 0 0 0 0 0 I H 0 0 0 0 0 0 0 0 0 0 0 0 +H 0 0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 0 0 +0 0 0 0 W W 0 0 0 0 0 0 0 0 0 0 0 W 0 0 +0 H 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 H 0 W +I 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 I 0 0 0 +0 0 0 0 I I 0 I 0 0 0 0 0 0 W 0 0 0 0 0 +H I 0 H 0 0 0 0 W 0 0 0 W 0 0 0 0 H 0 0 diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index e9de329..0bf57ab 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -39,23 +39,22 @@ private void move() { updateSafe(); //go in random direction int rand = random.nextInt(3); - System.out.println("Safe space, action is : " + rand); switch (rand) { - case 0: //try to go forward - percepts = world.action(MOVE); - processPercepts(); + case FORWARD: //try to go forward + System.out.println("Safe space, action is : " + "Go forward"); + doAction(FORWARD, true); break; - case 1: //try to go left - turnLeft(); - percepts = world.action(MOVE); - processPercepts(); + case LEFT: //try to go left + System.out.println("Safe space, action is : " + "Go left"); + doAction(LEFT, true); break; case 2: //try go right - turnRight(); - percepts = world.action(MOVE); - processPercepts(); + + System.out.println("Safe space, action is : " + "Go right"); + doAction(RIGHT, true); break; default: //turn around + System.out.println("Safe space, action is : " + "Move backwards"); turnRight(); turnRight(); percepts = world.action(MOVE); @@ -76,36 +75,38 @@ private void move() { } if (safeMoves.size() > 0) { int rand = random.nextInt(safeMoves.size()); + doAction(safeMoves.get(rand), true); percepts = world.action(safeMoves.get(rand) + 1); } else { int rand = random.nextInt(3); - System.out.println("Unsafe space, action is : " + rand); - switch (rand) { - case 0: + System.out.println("No safe action, action is : " + rand); + doAction(rand, true); + } + } + } + + public void doAction(int rand, boolean processPercepts){ + switch (rand) { + case FORWARD: percepts = world.action(MOVE); - processPercepts(); break; - case 1: + case LEFT: turnLeft(); percepts = world.action(MOVE); - processPercepts(); break; - case 2: + case RIGHT: turnRight(); percepts = world.action(MOVE); - processPercepts(); break; - default: + case BACK: turnRight(); turnRight(); percepts = world.action(MOVE); - processPercepts(); break; } - } - } + if(processPercepts) + processPercepts(); } - private void processPercepts() { if ((percepts & BUMP) == BUMP) { diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 96e7699..13e529f 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,21 +1,21 @@ 20 -W 0 0 0 0 0 I 0 0 0 0 H 0 0 0 0 0 0 0 0 -0 H 0 H 0 0 0 0 I 0 I 0 0 W 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 -I 0 0 0 H 0 I W 0 W 0 0 0 0 0 0 H 0 0 0 -0 0 0 W 0 I 0 0 H 0 0 0 H S W 0 0 0 0 0 -0 0 0 0 0 0 0 W 0 0 H 0 0 0 0 0 0 0 H 0 -0 0 0 I 0 0 0 I 0 H 0 W W I 0 H W 0 0 0 -0 0 I 0 0 0 0 0 0 H 0 I W 0 0 0 0 0 I H -I 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 I 0 0 0 0 0 0 0 0 0 0 W W 0 0 W 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H I 0 -0 0 0 0 0 0 0 W 0 0 0 0 H I 0 0 H 0 0 0 -0 H I 0 I I 0 0 G 0 0 0 0 0 0 0 W 0 H 0 -0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 -0 H 0 I 0 0 0 0 0 0 I H W 0 0 0 0 I 0 0 -0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 0 0 0 0 0 -0 H 0 H 0 0 0 0 0 0 0 W 0 0 I 0 0 0 0 0 -0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 +H 0 0 0 0 I 0 0 0 I 0 0 0 0 0 0 0 0 0 H +0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 H H 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W +0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 +H 0 0 0 0 0 0 0 W 0 0 0 0 0 0 H 0 0 0 I +0 0 0 0 0 0 0 0 0 0 W 0 0 0 0 H 0 0 0 0 +0 I 0 0 H 0 I 0 0 0 0 I 0 0 0 0 0 0 I I +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +W 0 0 0 0 0 0 0 H H H 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 H 0 0 0 S 0 H 0 0 0 0 0 0 +0 0 0 0 0 H 0 0 0 0 I 0 0 0 0 I 0 0 0 0 +0 0 0 0 0 0 I H 0 0 0 0 0 0 0 0 0 0 0 0 +H 0 0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 0 0 +0 0 0 0 W W 0 0 0 0 0 0 0 0 0 0 0 W 0 0 +0 H 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 G 0 0 0 H 0 0 0 0 0 0 0 H 0 W +I 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 I 0 0 0 +0 0 0 0 I I 0 I 0 0 0 0 0 0 W 0 0 0 0 0 +H I 0 H 0 0 0 0 W 0 0 0 W 0 0 0 0 H 0 0 From 292ab583ef433f5dce6ebf9ec1cfce9865a02147 Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sun, 23 Oct 2016 19:40:49 -0600 Subject: [PATCH 189/191] Wilson changes --- Wumpus/PerceptBoard.txt | 42 ++++++++++++++++---------------- Wumpus/clean.txt | 38 ++++++++++++++--------------- Wumpus/src/ReactiveExplorer.java | 2 +- Wumpus/world.txt | 38 ++++++++++++++--------------- 4 files changed, 60 insertions(+), 60 deletions(-) diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 14bb8cc..3a53ef3 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,21 +1,21 @@ -20 9 11 -16 1 0 0 0 4 0 0 0 4 0 2 0 0 0 1 1 0 1 16 -1 0 0 0 0 0 0 0 0 0 2 32 2 0 1 17 17 1 0 3 -0 0 0 0 0 0 0 0 2 0 0 2 0 0 0 1 1 0 2 32 -1 0 0 0 0 0 0 2 34 2 0 0 0 0 0 1 0 0 0 2 -16 1 0 0 0 0 0 2 34 2 2 0 0 0 1 17 1 0 0 4 -1 0 0 0 1 0 0 0 2 2 32 2 0 0 1 17 1 0 0 0 -0 4 0 1 16 1 4 0 0 0 2 4 0 0 0 1 0 0 4 4 -2 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 -32 2 0 0 0 0 0 1 17 17 17 1 0 1 0 0 0 0 0 0 -2 0 0 0 0 1 1 16 1 1 1 0 1 16 1 0 0 0 0 0 -0 0 0 0 1 16 1 1 0 0 4 0 0 1 0 4 0 0 0 0 -1 0 0 0 0 1 5 16 1 0 1 0 0 0 0 0 0 0 0 0 -16 1 0 0 2 2 0 1 0 1 16 5 0 0 0 0 0 2 0 0 -1 1 0 2 34 34 2 0 0 0 3 0 0 0 0 0 2 32 2 0 -1 16 1 0 2 2 0 0 0 3 32 2 0 0 0 0 0 3 0 2 -0 1 0 0 0 8 0 1 1 16 3 0 0 0 0 0 1 16 3 32 -4 0 0 0 0 0 1 16 1 1 0 0 0 1 0 0 0 1 0 2 -0 0 0 0 0 0 0 1 0 0 0 0 1 16 3 0 4 0 0 0 -1 0 0 1 4 4 0 4 2 0 0 0 2 3 32 2 0 1 0 0 -16 5 1 16 1 0 0 2 32 2 0 2 32 2 2 0 1 16 1 0 +20 1 12 +0 0 0 3 16 1 0 0 0 4 0 0 0 1 4 0 0 0 0 4 +0 0 2 32 3 17 1 0 0 0 0 0 1 16 1 0 0 0 0 0 +4 0 0 2 1 17 5 8 0 0 0 0 0 1 0 0 0 1 0 4 +4 0 0 0 0 1 0 0 0 0 0 0 2 0 2 0 1 17 5 0 +0 0 0 0 0 0 0 0 0 0 4 6 32 2 32 3 1 17 1 4 +0 4 0 0 0 0 0 0 0 0 0 4 2 0 3 16 1 1 0 0 +1 1 2 0 0 0 2 0 0 0 0 0 0 1 0 1 0 1 0 0 +17 19 33 2 2 2 32 2 0 0 0 0 1 17 1 0 1 16 5 0 +1 3 2 2 34 34 2 0 0 0 1 0 1 17 1 0 0 1 4 0 +6 33 2 0 2 2 0 0 0 1 18 5 0 1 0 0 0 0 2 0 +1 18 3 0 0 0 0 0 1 3 33 2 0 4 0 0 0 6 32 2 +0 3 32 2 0 0 0 1 17 17 19 1 0 5 0 2 0 0 2 0 +0 0 2 4 0 0 0 1 17 1 1 4 1 17 3 32 2 0 0 0 +0 2 32 2 4 0 1 0 1 0 1 0 1 17 17 3 0 0 0 0 +0 0 2 0 0 1 16 1 4 1 16 1 0 1 1 0 0 0 0 0 +0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 1 1 +0 0 0 0 0 0 0 0 0 2 0 0 0 0 1 17 19 1 17 17 +0 0 0 0 0 2 0 0 2 32 2 0 0 0 0 3 33 2 1 1 +0 0 0 4 2 32 2 0 0 2 0 0 0 0 0 0 2 32 2 0 +0 0 0 0 4 2 0 0 0 0 0 0 0 0 0 0 0 2 0 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index e45c939..0c81497 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,21 +1,21 @@ 20 -H 0 0 0 0 I 0 0 0 I 0 0 0 0 0 0 0 0 0 H -0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 H H 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W -0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 -H 0 0 0 0 0 0 0 W 0 0 0 0 0 0 H 0 0 0 I -0 0 0 0 0 0 0 0 0 0 W 0 0 0 0 H 0 0 0 0 -0 I 0 0 H 0 I 0 0 0 0 I 0 0 0 0 0 0 I I +0 0 0 0 H 0 0 0 0 I 0 0 0 0 I 0 0 0 0 I +0 0 0 W 0 H 0 0 0 0 0 0 0 H 0 0 0 0 0 0 +I 0 0 0 0 H I 0 0 0 0 0 0 0 0 0 0 0 0 I +I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H I 0 +0 0 0 0 0 0 0 0 0 0 I I W 0 W 0 0 H 0 I +0 I 0 0 0 0 0 0 0 0 0 I 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -W 0 0 0 0 0 0 0 H H H 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 H 0 0 0 0 0 H 0 0 0 0 0 0 -0 0 0 0 0 H 0 0 0 0 I 0 0 0 0 I 0 0 0 0 -0 0 0 0 0 0 I H 0 0 0 0 0 0 0 0 0 0 0 0 -H 0 0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 0 0 -0 0 0 0 W W 0 0 0 0 0 0 0 0 0 0 0 W 0 0 -0 H 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 H 0 W -I 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 I 0 0 0 -0 0 0 0 I I 0 I 0 0 0 0 0 0 W 0 0 0 0 0 -H I 0 H 0 0 0 0 W 0 0 0 W 0 0 0 0 H 0 0 +H H W 0 0 0 W 0 0 0 0 0 0 H 0 0 0 H I 0 +0 0 0 0 W W 0 0 0 0 0 0 0 H 0 0 0 0 I 0 +I W 0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 0 0 +0 H 0 0 0 0 0 0 0 0 W 0 0 I 0 0 0 I W 0 +0 0 W 0 0 0 0 0 H H H 0 0 I 0 0 0 0 0 0 +0 0 0 I 0 0 0 0 H 0 0 I 0 H 0 W 0 0 0 0 +0 0 W 0 I 0 0 0 0 0 0 0 0 H H 0 0 0 0 0 +0 0 0 0 0 0 H 0 I 0 H 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H H 0 H H +0 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 W 0 0 0 +0 0 0 I 0 W 0 0 0 0 0 0 0 0 0 0 0 W 0 0 +0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 diff --git a/Wumpus/src/ReactiveExplorer.java b/Wumpus/src/ReactiveExplorer.java index 0bf57ab..2322fa3 100644 --- a/Wumpus/src/ReactiveExplorer.java +++ b/Wumpus/src/ReactiveExplorer.java @@ -20,7 +20,7 @@ public ReactiveExplorer(World world, int arrows, int x, int y, int direction) { private void run() { int i = 0; - while (i < 10000) { + while (i < 50000) { move(); i++; } diff --git a/Wumpus/world.txt b/Wumpus/world.txt index 13e529f..ee5340c 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,21 +1,21 @@ 20 -H 0 0 0 0 I 0 0 0 I 0 0 0 0 0 0 0 0 0 H -0 0 0 0 0 0 0 0 0 0 0 W 0 0 0 H H 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W -0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 0 -H 0 0 0 0 0 0 0 W 0 0 0 0 0 0 H 0 0 0 I -0 0 0 0 0 0 0 0 0 0 W 0 0 0 0 H 0 0 0 0 -0 I 0 0 H 0 I 0 0 0 0 I 0 0 0 0 0 0 I I +0 0 0 0 H 0 0 0 0 I 0 0 0 0 I 0 0 0 0 I +0 0 0 W 0 H 0 0 0 0 0 0 S H 0 0 0 0 0 0 +I 0 0 0 0 H I G 0 0 0 0 0 0 0 0 0 0 0 I +I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H I 0 +0 0 0 0 0 0 0 0 0 0 I I W 0 W 0 0 H 0 I +0 I 0 0 0 0 0 0 0 0 0 I 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -W 0 0 0 0 0 0 0 H H H 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 H 0 0 0 S 0 H 0 0 0 0 0 0 -0 0 0 0 0 H 0 0 0 0 I 0 0 0 0 I 0 0 0 0 -0 0 0 0 0 0 I H 0 0 0 0 0 0 0 0 0 0 0 0 -H 0 0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 0 0 -0 0 0 0 W W 0 0 0 0 0 0 0 0 0 0 0 W 0 0 -0 H 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 G 0 0 0 H 0 0 0 0 0 0 0 H 0 W -I 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 I 0 0 0 -0 0 0 0 I I 0 I 0 0 0 0 0 0 W 0 0 0 0 0 -H I 0 H 0 0 0 0 W 0 0 0 W 0 0 0 0 H 0 0 +H H W 0 0 0 W 0 0 0 0 0 0 H 0 0 0 H I 0 +0 0 0 0 W W 0 0 0 0 0 0 0 H 0 0 0 0 I 0 +I W 0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 0 0 +0 H 0 0 0 0 0 0 0 0 W 0 0 I 0 0 0 I W 0 +0 0 W 0 0 0 0 0 H H H 0 0 I 0 0 0 0 0 0 +0 0 0 I 0 0 0 0 H 0 0 I 0 H 0 W 0 0 0 0 +0 0 W 0 I 0 0 0 0 0 0 0 0 H H 0 0 0 0 0 +0 0 0 0 0 0 H 0 I 0 H 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H H 0 H H +0 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 W 0 0 0 +0 0 0 I 0 W 0 0 0 0 0 0 0 0 0 0 0 W 0 0 +0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 From dafb6a4464889060536d041e7aa21fbc7a972a2f Mon Sep 17 00:00:00 2001 From: WilsonSunBritten Date: Sun, 23 Oct 2016 21:39:45 -0600 Subject: [PATCH 190/191] default variable value to -1 --- Wumpus/src/Variable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index 17bd874..42b98b4 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -2,7 +2,7 @@ public class Variable { boolean isVariable; - int value; + int value = -1;//default to avoid mixups IFunction function; int variableId; int modifier; From d0177fe9ef7ed67f23c2ecc86a2c89a127358ec9 Mon Sep 17 00:00:00 2001 From: Zach Connelly Date: Mon, 24 Oct 2016 10:54:43 -0600 Subject: [PATCH 191/191] Zach's big clean up for to submit. --- Wumpus/PerceptBoard.txt | 42 ++++++++-------- Wumpus/clean.txt | 36 ++++++------- Wumpus/src/Agent.java | 22 ++++---- Wumpus/src/Clause.java | 13 ++--- Wumpus/src/Driver.java | 2 +- Wumpus/src/Fact.java | 4 ++ Wumpus/src/InferenceEngine.java | 18 ++----- Wumpus/src/KnowledgeBase.java | 31 +++--------- Wumpus/src/LogicExplorer.java | 42 ++++------------ Wumpus/src/MinusFunction.java | 4 +- Wumpus/src/Quantifier.java | 2 +- Wumpus/src/Rule.java | 16 ++++++ Wumpus/src/Space.java | 9 +--- Wumpus/src/Substitute.java | 2 +- Wumpus/src/Tester.java | 89 --------------------------------- Wumpus/src/Variable.java | 8 ++- Wumpus/src/World.java | 12 +++-- Wumpus/world.txt | 36 ++++++------- 18 files changed, 134 insertions(+), 254 deletions(-) delete mode 100644 Wumpus/src/Tester.java diff --git a/Wumpus/PerceptBoard.txt b/Wumpus/PerceptBoard.txt index 3a53ef3..bfd47a4 100644 --- a/Wumpus/PerceptBoard.txt +++ b/Wumpus/PerceptBoard.txt @@ -1,21 +1,21 @@ -20 1 12 -0 0 0 3 16 1 0 0 0 4 0 0 0 1 4 0 0 0 0 4 -0 0 2 32 3 17 1 0 0 0 0 0 1 16 1 0 0 0 0 0 -4 0 0 2 1 17 5 8 0 0 0 0 0 1 0 0 0 1 0 4 -4 0 0 0 0 1 0 0 0 0 0 0 2 0 2 0 1 17 5 0 -0 0 0 0 0 0 0 0 0 0 4 6 32 2 32 3 1 17 1 4 -0 4 0 0 0 0 0 0 0 0 0 4 2 0 3 16 1 1 0 0 -1 1 2 0 0 0 2 0 0 0 0 0 0 1 0 1 0 1 0 0 -17 19 33 2 2 2 32 2 0 0 0 0 1 17 1 0 1 16 5 0 -1 3 2 2 34 34 2 0 0 0 1 0 1 17 1 0 0 1 4 0 -6 33 2 0 2 2 0 0 0 1 18 5 0 1 0 0 0 0 2 0 -1 18 3 0 0 0 0 0 1 3 33 2 0 4 0 0 0 6 32 2 -0 3 32 2 0 0 0 1 17 17 19 1 0 5 0 2 0 0 2 0 -0 0 2 4 0 0 0 1 17 1 1 4 1 17 3 32 2 0 0 0 -0 2 32 2 4 0 1 0 1 0 1 0 1 17 17 3 0 0 0 0 -0 0 2 0 0 1 16 1 4 1 16 1 0 1 1 0 0 0 0 0 -0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 1 1 -0 0 0 0 0 0 0 0 0 2 0 0 0 0 1 17 19 1 17 17 -0 0 0 0 0 2 0 0 2 32 2 0 0 0 0 3 33 2 1 1 -0 0 0 4 2 32 2 0 0 2 0 0 0 0 0 0 2 32 2 0 -0 0 0 0 4 2 0 0 0 0 0 0 0 0 0 0 0 2 0 0 +20 11 0 +0 0 0 0 0 0 0 1 16 5 2 0 0 0 1 0 4 0 1 18 +0 0 0 0 0 0 1 0 1 2 33 2 0 1 16 1 0 0 2 33 +0 0 0 0 4 1 16 1 0 1 18 3 1 0 1 0 0 0 0 2 +0 0 0 0 0 0 1 0 0 0 3 33 18 1 1 0 4 0 0 0 +0 0 1 0 0 0 0 0 0 4 0 2 1 1 16 1 0 0 0 0 +0 1 16 1 0 1 2 0 0 0 0 0 0 0 1 0 0 0 0 0 +0 1 1 0 1 18 33 2 0 2 0 0 0 0 0 0 2 0 0 1 +1 16 1 0 0 1 2 0 2 32 2 0 0 0 0 2 32 2 1 16 +0 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2 0 0 1 +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 +1 16 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 32 2 2 +0 1 4 0 2 0 0 0 0 2 0 0 0 0 0 0 0 2 2 32 +0 0 4 2 32 2 0 0 2 32 2 0 0 0 0 0 0 0 0 2 +4 0 0 0 2 0 0 0 4 2 0 1 4 0 0 0 0 4 0 0 +0 0 0 2 32 2 0 0 0 0 1 16 1 0 0 4 0 0 0 0 +0 2 0 0 2 2 0 1 0 0 0 1 0 0 0 0 0 4 0 0 +3 32 2 4 2 32 3 16 1 0 0 0 0 0 0 0 0 0 0 0 +16 3 0 0 0 2 1 1 0 0 0 0 2 0 0 0 8 0 2 0 +1 0 0 0 0 5 16 1 0 4 0 2 32 2 0 1 0 2 32 6 +0 0 0 0 0 4 1 0 4 0 0 0 2 0 1 16 1 0 2 0 diff --git a/Wumpus/clean.txt b/Wumpus/clean.txt index 0c81497..055344e 100644 --- a/Wumpus/clean.txt +++ b/Wumpus/clean.txt @@ -1,21 +1,21 @@ 20 -0 0 0 0 H 0 0 0 0 I 0 0 0 0 I 0 0 0 0 I -0 0 0 W 0 H 0 0 0 0 0 0 0 H 0 0 0 0 0 0 -I 0 0 0 0 H I 0 0 0 0 0 0 0 0 0 0 0 0 I -I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H I 0 -0 0 0 0 0 0 0 0 0 0 I I W 0 W 0 0 H 0 I -0 I 0 0 0 0 0 0 0 0 0 I 0 0 0 H 0 0 0 0 +0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 I 0 0 H +0 0 0 0 0 0 0 0 0 0 W 0 0 0 H 0 0 0 0 W +0 0 0 0 I 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 W H 0 0 0 I 0 0 0 +0 0 0 0 0 0 0 0 0 I 0 0 0 0 H 0 0 0 0 0 +0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 H W 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 H 0 0 0 0 0 0 0 W 0 0 0 0 0 0 W 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -H H W 0 0 0 W 0 0 0 0 0 0 H 0 0 0 H I 0 -0 0 0 0 W W 0 0 0 0 0 0 0 H 0 0 0 0 I 0 -I W 0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 0 0 -0 H 0 0 0 0 0 0 0 0 W 0 0 I 0 0 0 I W 0 -0 0 W 0 0 0 0 0 H H H 0 0 I 0 0 0 0 0 0 -0 0 0 I 0 0 0 0 H 0 0 I 0 H 0 W 0 0 0 0 -0 0 W 0 I 0 0 0 0 0 0 0 0 H H 0 0 0 0 0 -0 0 0 0 0 0 H 0 I 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H H 0 H H -0 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 W 0 0 0 -0 0 0 I 0 W 0 0 0 0 0 0 0 0 0 0 0 W 0 0 -0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 +0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W +0 0 I 0 W 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 +I 0 0 0 0 0 0 0 I 0 0 0 I 0 0 0 0 I 0 0 +0 0 0 0 W 0 0 0 0 0 0 H 0 0 0 I 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 +0 W 0 I 0 W 0 H 0 0 0 0 0 0 0 0 0 0 0 0 +H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 I H 0 0 I 0 0 W 0 0 0 0 0 W I +0 0 0 0 0 I 0 0 I 0 0 0 0 0 0 H 0 0 0 0 diff --git a/Wumpus/src/Agent.java b/Wumpus/src/Agent.java index bd27fdd..1d2cfff 100644 --- a/Wumpus/src/Agent.java +++ b/Wumpus/src/Agent.java @@ -1,5 +1,9 @@ import java.util.Random; +/* + Agent super class, holds functions variables and constants relavent to both + logical and reactive exploreres + */ public class Agent { @@ -51,14 +55,6 @@ public int getLeft() { return (direction + 3) % 4; } - public enum State { //im not sure were going to need this - - SAFE, - UNSAFE, - EXPLORED, - UNEXPLORED; - } - public void updateLocation() { switch (direction) { case NORTH: @@ -83,6 +79,14 @@ public void updateLocation() { } } + public enum State { + + SAFE, + UNSAFE, + EXPLORED, + UNEXPLORED; + } + protected class Location { int x; @@ -92,7 +96,7 @@ public Location(int x, int y) { this.x = x; this.y = y; } - + public boolean equals(Location location) { return (this.x == location.x && this.y == location.y); } diff --git a/Wumpus/src/Clause.java b/Wumpus/src/Clause.java index 69326b2..5a0dedc 100644 --- a/Wumpus/src/Clause.java +++ b/Wumpus/src/Clause.java @@ -2,7 +2,7 @@ import java.util.ArrayList; /* - A clause contains a list of predicates + A clause contains a list of predicates */ public class Clause { @@ -18,15 +18,16 @@ public Clause(Clause clause) { } public Clause(Fact fact) { - + facts.add(fact); } - - public static void printClause(Clause clause){ - for(Fact fact : clause.facts){ + + public static void printClause(Clause clause) { + for (Fact fact : clause.facts) { fact.printFact(); - if(fact != clause.facts.get(clause.facts.size()-1)) + if (fact != clause.facts.get(clause.facts.size() - 1)) { System.out.print(" v "); + } } System.out.println(""); } diff --git a/Wumpus/src/Driver.java b/Wumpus/src/Driver.java index 5f325ab..954779c 100644 --- a/Wumpus/src/Driver.java +++ b/Wumpus/src/Driver.java @@ -44,7 +44,7 @@ public static void makeGame() throws IOException { game = new WumpusGame("clean.txt"); } World world = new World("PerceptBoard.txt"); - world.startGame("ReactiveExplorer"); + world.startGame("LogicExplorer"); //Agent explorer = new ReactiveExplorer(world, world.getLocation(), world.direction,world.getPercepts(), world.arrowCount); // World world = new World("PerceptBoard.txt"); //world.startGame("LogicExplorer"); diff --git a/Wumpus/src/Fact.java b/Wumpus/src/Fact.java index 345d61a..f8d30f9 100644 --- a/Wumpus/src/Fact.java +++ b/Wumpus/src/Fact.java @@ -1,6 +1,10 @@ import java.util.ArrayList; +/* + A Fact contains a set of variables and a predicate string, extends object variable +*/ + public class Fact extends Variable { protected ArrayList variables = new ArrayList<>(); diff --git a/Wumpus/src/InferenceEngine.java b/Wumpus/src/InferenceEngine.java index 76977c1..9c79297 100644 --- a/Wumpus/src/InferenceEngine.java +++ b/Wumpus/src/InferenceEngine.java @@ -75,13 +75,11 @@ public boolean follows(Fact fact) { break; } } - if (keepGoing)//if we didn't do any changes, keepGoing will be true, if we hit here, then we exhausted every clause comparison with no results, terminate - { + if (keepGoing) { return false; } } return false; - //Step 3: If all facts become empty, return true, else if all facts are exhausted, return false } public void infer(Fact factStart) { @@ -133,18 +131,10 @@ public void infer(Fact factStart) { } } } - //unify the two rules - //apply the unification substitution on a copy of the kb.rules fact thing - //remove the matching fact from that rule - //add the rule copy with the substitution to kb.clauses } public void infer(Clause clauseToCheck) { - //look in kb.rules, only use rules with a single fact - //for each such fact, go through the clause and look for a matching predicate - //if negations are opposite, apply unification, if unification exists.. - //with the substitution, apply to copies of both - //strip fact from input clause + ArrayList tempClone = new ArrayList<>(kb.rules); tempClone.addAll(kb.getClauses()); ArrayList kbClausesClone = new ArrayList<>(); @@ -177,10 +167,8 @@ public void infer(Clause clauseToCheck) { } if (Unifier.equalWithSubs(fact, ruleFact, substitutions)) { clause.facts.remove(fact); - //System.out.println("Inferred:"); - //Clause.printClause(clause); kb.addToClauses(clause); - return;//the smaller clause will be inferred against again, no need to keep trying rules immediately. + return; } } } diff --git a/Wumpus/src/KnowledgeBase.java b/Wumpus/src/KnowledgeBase.java index 377dc77..8922c68 100644 --- a/Wumpus/src/KnowledgeBase.java +++ b/Wumpus/src/KnowledgeBase.java @@ -3,9 +3,9 @@ public class KnowledgeBase { - InferenceEngine inferenceEngine = new InferenceEngine(this); - private ArrayList clauses = new ArrayList<>(); - ArrayList rules = new ArrayList<>(); + protected final InferenceEngine inferenceEngine = new InferenceEngine(this); + protected final ArrayList clauses = new ArrayList<>(); + protected final ArrayList rules = new ArrayList<>(); public ArrayList getClauses() { return clauses; @@ -48,12 +48,7 @@ public void addToClauses(Clause clause) { } public void initializeRules() { - //Fact(String predicate, int var1Val, boolean var1Var, int var2Val, boolean var2Var, boolean not, IFunction var1Function, IFunction var2Function) - //Special predicate: Evaluate - //Stench(x,y)<=>(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) - //Stench(x,y)=>(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) - //!Stench(x,y) v Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1) Clause stench = new Clause(); Fact stenchXY = new Fact("Stench", 0, true, 1, true, true, null, null); Fact wumpusxminy = new Fact("Wumpus", 0, true, 1, true, false, new MinusFunction(), null); @@ -66,10 +61,6 @@ public void initializeRules() { stench.facts.add(wumpusxymin); stench.facts.add(wumpusxyplus); rules.add(stench); - //(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1)) =>Stench(x,y) - //!(Wumpus(x-1,y) v Wumpus(x+1,y) v Wumpus(x,y-1) v Wumpus(x,y+1)) v Stench(x,y) - //(!Wumpus(x-1,y) ^ !Wumpus(x+1,y) ^ !Wumpus(x,y-1) ^ !Wumpus(x,y+1)) v Stench(x,y) - //(Stench(x,y) v !Wumpus(x-1,y)), (Stench(x,y) v !Wumpus(x+1,y)), (Stench(x,y) v !Wumpus(x,y-1)), (Stench(x,y) v !Wumpus(x,y+1) Clause stench1 = new Clause(); Fact stenchXY1 = new Fact(stenchXY); stenchXY1.not = false; @@ -106,8 +97,6 @@ public void initializeRules() { stench4.facts.add(wumpusxyplus1); rules.add(stench4); - //Breeze(x,y)<=>(Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) - //!Breeze(x,y) v Pit(x-1,y) v Pit(x+1,y) v Pit(x,y-1) v Pit(x, y+1) Clause breeze = new Clause(); Fact breezeXY = new Fact("Breeze", 0, true, 1, true, true, null, null); Fact pitxminy = new Fact("Pit", 0, true, 1, true, false, new MinusFunction(), null); @@ -159,40 +148,32 @@ public void initializeRules() { breeze4.facts.add(pitxyplus1); rules.add(breeze4); - //!Wumpus(-1,y) ^ !Wumpus(x,-1) ^ !Wumpus(x,World.size)^ !Wumpus(World.size,y) - //!Wumpus(-1,y), !Wumpus(x,-1), !Wumpus(x,World.size), !Wumpus(World.size,y) rules.add(new Clause(new Fact("Wumpus", -1, false, 1, true, true, null, null))); rules.add(new Clause(new Fact("Wumpus", 0, true, -1, false, true, null, null))); rules.add(new Clause(new Fact("Wumpus", 0, true, World.size, false, true, null, null))); rules.add(new Clause(new Fact("Wumpus", World.size, false, 1, true, true, null, null))); - //!Pit(-1,y), !Pit(x,-1), !Pit(x,World.size), !Wumpus(World.size,y) rules.add(new Clause(new Fact("Pit", -1, false, 1, true, true, null, null))); rules.add(new Clause(new Fact("Pit", 0, true, -1, false, true, null, null))); rules.add(new Clause(new Fact("Pit", 0, true, World.size, false, true, null, null))); rules.add(new Clause(new Fact("Pit", World.size, false, 1, true, true, null, null))); - //Wumpus(x,y)=>!Pit(x,y) - //!Wumpus(x,y) v !Pit(x,y) Clause notWumpusOrNotPit = new Clause(); Fact notWumpus = new Fact("Wumpus", 0, true, 1, true, true, null, null); Fact notPit = new Fact("Pit", 0, true, 1, true, true, null, null); - // Fact notObstacle = new Fact("Obstacle",0,true,1,true,true,null,null); notWumpusOrNotPit.facts.add(notWumpus); notWumpusOrNotPit.facts.add(notPit); rules.add(notWumpusOrNotPit); - // !Wumpus(x,y) v !Obstacle(x,y) Clause notWumpusOrNotObstacle = new Clause(); - Fact notObstacle = new Fact("Obstacle",0,true,1,true,true,null,null); + Fact notObstacle = new Fact("Obstacle", 0, true, 1, true, true, null, null); notWumpusOrNotObstacle.facts.add(new Fact(notWumpus)); notWumpusOrNotObstacle.facts.add(notObstacle); - + rules.add(notWumpusOrNotObstacle); - //!Pit v !Obstacle Clause notPitOrNotObstacle = new Clause(); notPitOrNotObstacle.facts.add(new Fact(notObstacle)); notPitOrNotObstacle.facts.add(new Fact(notPit)); rules.add(notPitOrNotObstacle); - + } public boolean ask(Fact fact) { diff --git a/Wumpus/src/LogicExplorer.java b/Wumpus/src/LogicExplorer.java index c02f05d..5ec071e 100644 --- a/Wumpus/src/LogicExplorer.java +++ b/Wumpus/src/LogicExplorer.java @@ -4,11 +4,11 @@ public class LogicExplorer extends Agent { private final KnowledgeBase kb; - private ArrayList frontier = new ArrayList<>(); - private boolean[][] searchedPositions; + private final ArrayList frontier = new ArrayList<>(); + private final boolean[][] searchedPositions; private Location safeSpace; private Location wumpusSpace; - private ArrayList moveHistory = new ArrayList<>(); + private final ArrayList moveHistory = new ArrayList<>(); boolean notFirstMove = false; public LogicExplorer(World world, int startingArrows, int startingX, int startingY, int direction) { @@ -32,21 +32,7 @@ private void run() { decideNextAction(); } } - -// public void initializeFrontier() { -// if (location.x > 0) { -// frontier.add(new Location(location.x - 1, location.y)); -// } -// if (location.x < World.size - 1) { -// frontier.add(new Location(location.x + 1, location.y)); -// } -// if (location.y > 0) { -// frontier.add(new Location(location.x, location.y - 1)); -// } -// if (location.y < World.size - 1) { -// frontier.add(new Location(location.x, location.y + 1)); -// } -// } + @Override public void updateLocation() { @@ -118,8 +104,9 @@ private void move(int action) { System.out.println("Explorer Action: shooting"); arrowCount--; percepts = (byte) world.action(SHOOT); - if((percepts & SCREAM) != 0) + if ((percepts & SCREAM) != 0) { processPercepts(); + } break; case QUIT: System.out.println("no possible solution"); @@ -232,18 +219,16 @@ private void decideNextAction() { Location goalLoc = frontier.get(frontier.size() - 1); goTo(goalLoc); turnToSpace(goalLoc); - //frontier.remove(frontier.size() - 1); move(MOVE); - //if(!kb.ask(new Fact("Wumpus",getForward().x,false,getForward().y,false,false,null,null))) removeFromFrontier(goalLoc); if (kb.ask(new Fact("Wumpus", getForward().x, false, getForward().y, false, false, null, null))) { - frontier.add(getForward());//since there is definitely a wumpus forward, put in frontier so we can kill it later maybe. - }//we add straight to frontier even if it was searched in this case, that's why it's not addToFrontier + frontier.add(getForward()); + } } } public void turnToSpace(Location loc) { - + if (loc.x < location.x) { switch (direction) { case WEST: @@ -338,9 +323,7 @@ private boolean safeSpaceInFrontier() { if (kb.ask(new Fact("Pit", loc.x, false, loc.y, false, false, null, null)) || kb.ask(new Fact("Obstacle", loc.x, false, loc.y, false, false, null, null))) { //there is specifically a wumpus, pit, or obsticle at this position, don't navigate to it. frontier.remove(i); - } //else if (arrowCount == 0 && kb.ask(new Fact("Wumpus", loc.x, false, loc.y, false, false, null, null))) { - //frontier.remove(i); - //} We want wumpus so we can kill it... + } } return false; } @@ -351,7 +334,6 @@ private void goTo(Location target) { System.out.println("Traversing to " + target.x + ", " + target.y); ArrayList path = new ArrayList<>(); path = searchForPath(location.x, location.y, target.x, target.y, path); - // printPath(path); traversePath(path); if (checkAdjacent(target.x, target.y, location.x, location.y)) { System.out.println("Done traversing to " + target.x + ", " + target.y); @@ -374,7 +356,6 @@ private boolean searchNext(int curX, int curY, int goalX, int goalY, ArrayList 0) { path.remove(path.size() - 1); } - // printPath(path); } - //traversed[curX][curY] = false; return done; } private void printPath(ArrayList path) { if (!path.isEmpty()) { + System.out.print("Path: "); for (Location l : path) { System.out.print("[" + l.x + ", " + l.y + "] "); diff --git a/Wumpus/src/MinusFunction.java b/Wumpus/src/MinusFunction.java index 7ad2309..766a28c 100644 --- a/Wumpus/src/MinusFunction.java +++ b/Wumpus/src/MinusFunction.java @@ -3,13 +3,13 @@ public class MinusFunction implements IFunction { @Override public int process(int value) { - + return value - 1; } @Override public Variable processVariable(Variable variable) { - + Variable processedVariable = new Variable(); processedVariable.function = variable.function; processedVariable.isVariable = variable.isVariable; diff --git a/Wumpus/src/Quantifier.java b/Wumpus/src/Quantifier.java index 88dfba0..49bb8fc 100644 --- a/Wumpus/src/Quantifier.java +++ b/Wumpus/src/Quantifier.java @@ -3,7 +3,7 @@ public class Quantifier { int variableId; char variableRep; - boolean isExistential; //if false, assumed universal... + boolean isExistential; boolean not; public void printQuantifier() { diff --git a/Wumpus/src/Rule.java b/Wumpus/src/Rule.java index dde38f4..12201ea 100644 --- a/Wumpus/src/Rule.java +++ b/Wumpus/src/Rule.java @@ -51,4 +51,20 @@ public void printRule() { System.out.println(")"); } + + class Quantifier { + + private int variableId; + private boolean isExistential; + + public void printQuantifier() { + if (isExistential) { + System.out.print("EXIST("); + } else { + System.out.print("FORALL("); + } + System.out.print((char) (variableId + 97) + ") "); + } + } + } diff --git a/Wumpus/src/Space.java b/Wumpus/src/Space.java index f20515d..a232e40 100644 --- a/Wumpus/src/Space.java +++ b/Wumpus/src/Space.java @@ -1,12 +1,7 @@ public class Space { - private boolean hasWumpus = false; - private boolean hasHole = false; - private boolean hasObstacle = false; - private boolean hasGold = false; - private boolean start; - private boolean filled = false; + private boolean hasWumpus = false, hasHole = false, hasObstacle = false, hasGold = false, start, filled = false; public Space() { @@ -17,7 +12,7 @@ public boolean isHasWumpus() { } public void toggleWumpus() { - + if (hasWumpus == true) { hasWumpus = false; filled = false; diff --git a/Wumpus/src/Substitute.java b/Wumpus/src/Substitute.java index 2d450c9..65bdaca 100644 --- a/Wumpus/src/Substitute.java +++ b/Wumpus/src/Substitute.java @@ -1,6 +1,6 @@ public class Substitute { + int varIdToSubstitute; int valToSubstituteWith; - } diff --git a/Wumpus/src/Tester.java b/Wumpus/src/Tester.java deleted file mode 100644 index 499f3e2..0000000 --- a/Wumpus/src/Tester.java +++ /dev/null @@ -1,89 +0,0 @@ - -import java.util.ArrayList; - - -public class Tester { - - public void testUnify() { - Fact f1 = new Fact(); - Variable v1 = new Variable(); - - v1.isVariable = true; - v1.variableId = 0; - - f1.variables.add(v1); - - v1 = new Variable(); - v1.isVariable = true; - v1.variableId = 1; - - f1.variables.add(v1); - - Fact f2 = new Fact(); - v1 = new Variable(); - v1.isVariable = true; - v1.variableId = 2; - - f2.variables.add(v1); - - v1 = new Variable(); - v1.isVariable = true; - v1.variableId = 3; - - f2.variables.add(v1); - - ArrayList subs = Unifier.unify(f1, f2); - System.out.println(subs.size()); - for(Substitute sub: subs){ - System.out.println(sub.varIdToSubstitute + "/" + sub.valToSubstituteWith); - } - } - - public void testInferenceEngine() { -// InferenceEngine engine = new InferenceEngine(new KnowledgeBase()); -// -// //FORALL x, y, Commutative(x,y) IFF Commutative(y,x) -// Rule rule = new Rule(); -// Quantifier x = new Quantifier(); -// x.variableId = 0; -// Quantifier y = new Quantifier(); -// y.variableId = 1; -// rule.quantifiers.add(x); -// rule.quantifiers.add(y); -// Rule leftRule = new Rule(); -// Fact leftFact = new Fact(); -// leftFact.predicate = "Commutative"; -// Variable varX = new Variable(); -// varX.isVariable = true; -// varX.variableId = x.variableId; -// Variable varY = new Variable(); -// varY.isVariable = true; -// varY.variableId = y.variableId; -// leftFact.variables.add(varX); -// leftFact.variables.add(varY); -// leftRule.justFact = true; -// leftRule.fact = leftFact; -// Fact rightFact = new Fact(); -// Rule rightRule = new Rule(); -// rightFact.predicate = "Commutative"; -// rightFact.variables.add(varY); -// rightFact.variables.add(varX); -// rightRule.justFact = true; -// rightRule.fact = rightFact; -// rule.leftRule = leftRule; -// rule.rightRule = rightRule; -// rule.connector = Rule.IFF; -// -// rule.printRule(); -// Rule newRule = new Rule(); - -// //Vd,t Facing(d,t) => Facing(d,t+1) OR Action(Turnleft,t) OR Action(TurnRight,t) -// Rule rule2 = new Rule(); -// x.variableId = 0; -// y.variableId = 1; -// rule2.quantifiers.add(x); -// rule2.quantifiers.add(y); -// leftFact.predicate = "Facing"; - //engine.convertToCNF(null); - } -} diff --git a/Wumpus/src/Variable.java b/Wumpus/src/Variable.java index 42b98b4..2bcf208 100644 --- a/Wumpus/src/Variable.java +++ b/Wumpus/src/Variable.java @@ -1,11 +1,9 @@ public class Variable { - boolean isVariable; - int value = -1;//default to avoid mixups - IFunction function; - int variableId; - int modifier; + protected boolean isVariable; + protected IFunction function; + protected int variableId, modifier, value = -1; public Variable(Variable var) { diff --git a/Wumpus/src/World.java b/Wumpus/src/World.java index 1dcd102..776f729 100644 --- a/Wumpus/src/World.java +++ b/Wumpus/src/World.java @@ -14,6 +14,7 @@ public final class World { public static int size; private byte[][] perceptMap; private Agent explorer; + public World(String fileName) { importMap(fileName); for (byte[] cell : perceptMap) { @@ -69,11 +70,11 @@ public void importMap(String fileName) { public byte getPercepts() { return perceptMap[x][y]; } - - public void addMajorDecision(){ + + public void addMajorDecision() { majorDecisions++; } - + public void printStats() { System.out.println("Number of Actions: " + numMoves); @@ -112,8 +113,8 @@ public void printWorld() { System.out.println(""); } - public void printAction(int action){ - switch(action){ + public void printAction(int action) { + switch (action) { case GRAB: System.out.println("GRAB"); break; @@ -133,6 +134,7 @@ public void printAction(int action){ System.out.println("QUIT"); } } + public byte action(int action) { printWorld(); diff --git a/Wumpus/world.txt b/Wumpus/world.txt index ee5340c..8adc1ea 100644 --- a/Wumpus/world.txt +++ b/Wumpus/world.txt @@ -1,21 +1,21 @@ 20 -0 0 0 0 H 0 0 0 0 I 0 0 0 0 I 0 0 0 0 I -0 0 0 W 0 H 0 0 0 0 0 0 S H 0 0 0 0 0 0 -I 0 0 0 0 H I G 0 0 0 0 0 0 0 0 0 0 0 I -I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H I 0 -0 0 0 0 0 0 0 0 0 0 I I W 0 W 0 0 H 0 I -0 I 0 0 0 0 0 0 0 0 0 I 0 0 0 H 0 0 0 0 +0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 I 0 0 H +0 0 0 0 0 0 0 0 0 0 W 0 0 0 H 0 0 0 0 W +0 0 0 0 I 0 H 0 0 0 H 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 W H 0 0 0 I 0 0 0 +0 0 0 0 0 0 0 0 0 I 0 0 0 0 H 0 0 0 0 0 +0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 H W 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 H 0 0 0 0 0 0 0 W 0 0 0 0 0 0 W 0 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -H H W 0 0 0 W 0 0 0 0 0 0 H 0 0 0 H I 0 -0 0 0 0 W W 0 0 0 0 0 0 0 H 0 0 0 0 I 0 -I W 0 0 0 0 0 0 0 0 H I 0 0 0 0 0 0 0 0 -0 H 0 0 0 0 0 0 0 0 W 0 0 I 0 0 0 I W 0 -0 0 W 0 0 0 0 0 H H H 0 0 I 0 0 0 0 0 0 -0 0 0 I 0 0 0 0 H 0 0 I 0 H 0 W 0 0 0 0 -0 0 W 0 I 0 0 0 0 0 0 0 0 H H 0 0 0 0 0 -0 0 0 0 0 0 H 0 I 0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H H 0 H H -0 0 0 0 0 0 0 0 0 W 0 0 0 0 0 0 W 0 0 0 -0 0 0 I 0 W 0 0 0 0 0 0 0 0 0 0 0 W 0 0 -0 0 0 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W 0 0 +S 0 I 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 W +0 0 I 0 W 0 0 0 0 W 0 0 0 0 0 0 0 0 0 0 +I 0 0 0 0 0 0 0 I 0 0 0 I 0 0 0 0 I 0 0 +0 0 0 0 W 0 0 0 0 0 0 H 0 0 0 I 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I 0 0 +0 W 0 I 0 W 0 H 0 0 0 0 0 0 0 0 0 0 0 0 +H 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 G 0 0 0 +0 0 0 0 0 I H 0 0 I 0 0 W 0 0 0 0 0 W I +0 0 0 0 0 I 0 0 I 0 0 0 0 0 0 H 0 0 0 0