update cost and evaluation function

This commit is contained in:
Dominik Cupał 2021-04-27 22:30:25 +02:00
parent 7098fa397e
commit 799e8df4d3
2 changed files with 7 additions and 4 deletions

View File

@ -57,14 +57,16 @@ class AStar(Graphsearch):
break break
@staticmethod @staticmethod
def g(board: Board, node: Node) -> int: def g(board: Board, node: Node) -> tuple[int,int]:
"""cost function""" """cost function"""
result = 0 result = 0
i = 0
while node.get_node() is not None: while node.get_node() is not None:
field = board.get_field(node.get_x(), node.get_y()) field = board.get_field(node.get_x(), node.get_y())
result += field.get_value() result += field.get_value()
node = node.get_node() node = node.get_node()
return result i += 1
return result, i
@staticmethod @staticmethod
def h(node: Node) -> int: def h(node: Node) -> int:
@ -75,7 +77,8 @@ class AStar(Graphsearch):
@staticmethod @staticmethod
def f(board: Board, node: Node) -> int: def f(board: Board, node: Node) -> int:
"""evaluation function""" """evaluation function"""
return AStar.g(board, node) + AStar.h(node) + VALUE_OF_MOVEMENT cost_value, amount_of_moves = AStar.g(board, node)
return cost_value + AStar.h(node) + amount_of_moves * VALUE_OF_MOVEMENT
@staticmethod @staticmethod
def search(fringe: PriorityQueue, explored: Queue, istate: Node, def search(fringe: PriorityQueue, explored: Queue, istate: Node,

View File

@ -67,7 +67,7 @@ A_FERTILIZE = "fertilize"
A_DO_NOTHING = "do nothing" A_DO_NOTHING = "do nothing"
# Costs movement and fields: # Costs movement and fields:
VALUE_OF_MOVEMENT = 8 VALUE_OF_MOVEMENT = 4
VALUE_OF_CROPS = 1 VALUE_OF_CROPS = 1
VALUE_OF_PLANT = 3 VALUE_OF_PLANT = 3
VALUE_OF_SAND = 7 VALUE_OF_SAND = 7