update evaluation and cost function

This commit is contained in:
Dominik Cupał 2021-04-28 08:50:58 +02:00
parent 799e8df4d3
commit 61fa6d90c5
2 changed files with 5 additions and 9 deletions

View File

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

View File

@ -11,7 +11,7 @@ __all__ = (
'M_GO_FORWARD', 'M_ROTATE_LEFT', 'M_ROTATE_RIGHT',
'A_SOW', 'A_HARVEST', 'A_HYDRATE', 'A_FERTILIZE', 'A_DO_NOTHING',
'D_NORTH', 'D_EAST', 'D_SOUTH', 'D_WEST',
'VALUE_OF_MOVEMENT', 'VALUE_OF_CROPS', 'VALUE_OF_PLANT',
'VALUE_OF_CROPS', 'VALUE_OF_PLANT',
'VALUE_OF_SAND', 'VALUE_OF_CLAY'
)
@ -66,8 +66,7 @@ A_HYDRATE = "hydrate"
A_FERTILIZE = "fertilize"
A_DO_NOTHING = "do nothing"
# Costs movement and fields:
VALUE_OF_MOVEMENT = 4
# Costs fields:
VALUE_OF_CROPS = 1
VALUE_OF_PLANT = 3
VALUE_OF_SAND = 7