From 788dfe47138841a2bcb90b8db75fcdbe4a6b6f20 Mon Sep 17 00:00:00 2001 From: przemek Date: Tue, 10 Apr 2018 21:12:15 +0200 Subject: [PATCH] es02 4+5 refactored; ex6 --- es02/Makefile | 6 +++++- es02/ex4.py | 4 +++- es02/ex5.py | 7 ++++++- es02/ex6.py | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 es02/ex6.py diff --git a/es02/Makefile b/es02/Makefile index 823966f..5a18178 100644 --- a/es02/Makefile +++ b/es02/Makefile @@ -13,4 +13,8 @@ ex4: python validator.py zad4 python3 ex4.py ex5: - python validator.py zad5 python3 ex5.py \ No newline at end of file + python validator.py zad5 python3 ex5.py + +ex6: + echo Running As with non admissible heuristic + python validator.py zad5 python3 ex6.py \ No newline at end of file diff --git a/es02/ex4.py b/es02/ex4.py index 6b72813..b6d4052 100644 --- a/es02/ex4.py +++ b/es02/ex4.py @@ -43,13 +43,15 @@ class Commando: DIR = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1), "B": (0, 0)} MOVES = ["U", "D", "L", "R"] - def __init__(self, board, uncert=False): + def __init__(self, board, uncert=False, non_admissible=False): self.board, self.goals, self.starts = self.stripBoard(board) self.initState = ComaState(self.starts) if uncert: self.initState = self.uncertainty(self.initState) + + self.non_admissible = non_admissible def stripBoard(self, board): """ diff --git a/es02/ex5.py b/es02/ex5.py index 944799b..5707adb 100644 --- a/es02/ex5.py +++ b/es02/ex5.py @@ -17,7 +17,12 @@ def computeF(self, state, goals): Note: F = g = self.depth is just an ordinary BFS """ - state.F = state.depth + max([self.dists[s] for s in state.state]) + if self.non_admissible is True: + # Ex 6. + state.F = state.depth + sum([self.dists[s] for s in state.state]) + else: + # Ex 5. + state.F = state.depth + max([self.dists[s] for s in state.state]) return state def preProcessDists(self): diff --git a/es02/ex6.py b/es02/ex6.py new file mode 100644 index 0000000..f14344e --- /dev/null +++ b/es02/ex6.py @@ -0,0 +1,20 @@ +from ex5 import Commando +from ex4 import readBoard +import sys + + +if __name__ == '__main__': + + finput = 'zad_input.txt' + foutput = 'zad_output.txt' + + if len(sys.argv) == 2: + finput = sys.argv[1] + + board = readBoard(finput) + + coma = Commando(board, non_admissible=True) + ans = coma.astar() + + fout = open(foutput, "w") + print(ans, file=fout) \ No newline at end of file