update evaluation and cost function
This commit is contained in:
parent
799e8df4d3
commit
61fa6d90c5
@ -57,16 +57,14 @@ class AStar(Graphsearch):
|
|||||||
break
|
break
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def g(board: Board, node: Node) -> tuple[int,int]:
|
def g(board: Board, node: Node) -> 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()
|
||||||
i += 1
|
return result
|
||||||
return result, i
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def h(node: Node) -> int:
|
def h(node: Node) -> int:
|
||||||
@ -77,8 +75,7 @@ class AStar(Graphsearch):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def f(board: Board, node: Node) -> int:
|
def f(board: Board, node: Node) -> int:
|
||||||
"""evaluation function"""
|
"""evaluation function"""
|
||||||
cost_value, amount_of_moves = AStar.g(board, node)
|
return AStar.g(board, node) + AStar.h(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,
|
||||||
|
@ -11,7 +11,7 @@ __all__ = (
|
|||||||
'M_GO_FORWARD', 'M_ROTATE_LEFT', 'M_ROTATE_RIGHT',
|
'M_GO_FORWARD', 'M_ROTATE_LEFT', 'M_ROTATE_RIGHT',
|
||||||
'A_SOW', 'A_HARVEST', 'A_HYDRATE', 'A_FERTILIZE', 'A_DO_NOTHING',
|
'A_SOW', 'A_HARVEST', 'A_HYDRATE', 'A_FERTILIZE', 'A_DO_NOTHING',
|
||||||
'D_NORTH', 'D_EAST', 'D_SOUTH', 'D_WEST',
|
'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'
|
'VALUE_OF_SAND', 'VALUE_OF_CLAY'
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -66,8 +66,7 @@ A_HYDRATE = "hydrate"
|
|||||||
A_FERTILIZE = "fertilize"
|
A_FERTILIZE = "fertilize"
|
||||||
A_DO_NOTHING = "do nothing"
|
A_DO_NOTHING = "do nothing"
|
||||||
|
|
||||||
# Costs movement and fields:
|
# Costs fields:
|
||||||
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
|
||||||
|
Loading…
Reference in New Issue
Block a user