Question: That was wrong. Here is some of my code: ' ' ' CMSI 2 1 3 0 - Homework 1 Author: Modify only this file

That was wrong. Here is some of my code: '''
CMSI 2130- Homework 1
Author:
Modify only this file as part of your submission, as it will contain all of the logic
necessary for implementing the A* pathfinder that solves the target practice problem.
'''
import queue
from dataclasses import *
from typing import *
import heapq
from maze_problem import MazeProblem
from dataclasses import dataclass
from typing import Optional, List, Tuple, Dict
from typing import List, Tuple
@dataclass
class SearchTreeNode:
player_loc: Tuple[int, int]
action: str
parent: Optional["SearchTreeNode"]
# TODO: Add any other attributes and method overrides as necessary!
cost: int =0
total_cost: int =0 # g(n) path cost + h(n) future cost
def __lt__(self, other)-> bool:
return self.total_cost other.total_cost
def __eq__(self, other)-> bool:
return self.total_cost == other.total_cost # and self.player_loc == other.player_loc
def __str__(self)-> str:
return f"@: {self.player_loc}, cost: {self.cost}"
def heuristic(current: Tuple[int, int], goal: Tuple[int, int])-> int:
return abs(current[0]- goal[0])+ abs(current[1]- goal[1])
def pathfind(problem: "MazeProblem")-> Optional[list[str]]:
targets_left = problem.get_initial_targets()
frontier: List[SearchTreeNode]=[]
initial_state = SearchTreeNode(problem.get_initial_loc(),"", None, 0,0)
frontier.append(initial_state)
graveyard: Set[Tuple[Tuple[int, int], int]]= set()
while frontier:
current_node = frontier.pop(0)
# Process neighbors
neighbors = problem.get_transitions(current_node.player_loc, targets_left)
for action, transition in neighbors.items():
next_loc = transition["next_loc"]
cost = current_node.cost + transition["cost"]
targets_hit = transition["targets_hit"]
# Update remaining targets
new_targets_left = targets_left - targets_hit
# Check if all targets are destroyed
if not new_targets_left:
solution_path: List[str]=[]
while current_node.parent is not None:
solution_path.insert(0, current_node.action)
current_node = current_node.parent
return solution_path
# Calculate total cost with heuristic
total_cost = cost + heuristic(next_loc, current_node.player_loc)
# Create a new search tree node
new_node = SearchTreeNode(next_loc, action, current_node, cost, total_cost)
# Check if this state has been visited before
state_signature =(new_node.player_loc, len(new_targets_left))
if state_signature not in graveyard:
frontier.append(new_node)
graveyard.add(state_signature)
return None # If no solution found
Now please edit my code to have the tests come out right. I only go the nosoultions (2/8 tests passed) but need 8/8 to be passed.
DO NOT MODFIY ANYTHING FROM BELOW. Just above code
maze_problem.py
def get_initial_loc (self)->> tuple[int, int]:
Returns the player"s starting position in the maze ("g").
Returns:
tuple[int, int]:
The player's starting location in the maze: (col, row)=(x,y).
"n=
def get_goal_loc (self)-> tuple[int, int]:
""=
Returns the location of the goal that must be reached.
Returns:
tuple[int, int]:
The goal's location tuple: (col, row)=(x,y).
n"
def get_transitions(self, player_loc: tuple[int, int])-> dict:
Returns a dictionary describing all possible transitions that a player may take from their
given position.
Parameters:
player_loc(tuple[int, int]):
The current location of the player in the maze.
Returns:
dict:
A dictionary whose keys are the possible actions fron the given player_loc, with mapped
values next_loc (tuple[int, int]) values that show the player's location after taking that action.
Exanple:
For example, if only the actions " D","U", and "L" were possible from the current player_loc of (3,3),
we might see an output of:
{
"D":(3,4),
"U": (3,2),
"L":(2,3)
}
That was wrong. Here is some of my code: ' ' '

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!