pacai.util.alias
This file provides useful aliases/shortcuts or short names for different objects in pacai.
For example, the web alias can be used to reference pacai.ui.web.WebUI.
1""" 2This file provides useful aliases/shortcuts or short names for different objects in pacai. 3For example, the `web` alias can be used to reference `pacai.ui.web.WebUI`. 4""" 5 6class Alias: 7 """ An alias for some object name. """ 8 9 _alias_map: dict[str, str] = {} 10 """ Keep track of all aliases for testing purposes. """ 11 12 _all_aliases: list['Alias'] = [] 13 """ Keep track of all aliases mappings for lookup. """ 14 15 def __init__(self, 16 short: str, long: str, 17 is_qualified_name: bool = True, skip_windows_test: bool = False, 18 ) -> None: 19 self.short: str = short 20 """ The short name for this alias. """ 21 22 self.long: str = long 23 """ The long name for this alias. """ 24 25 self.is_qualified_name: bool = is_qualified_name 26 """ 27 Whether this alias represents a qualified name. 28 Alias that are qualified names will be tested with reflection. 29 """ 30 31 self.skip_windows_test: bool = skip_windows_test 32 """ If this alias' reflection test should be skipped on Windows. """ 33 34 if ('.' in short): 35 raise ValueError(f"Dots ('.') are not allowed in aliases. Found '{short}'.") 36 37 if (short in Alias._alias_map): 38 raise ValueError(f"Found duplicate alias: '{short}' -> '{long}'.") 39 40 Alias._alias_map[short] = long 41 Alias._all_aliases.append(self) 42 43 def __repr__(self) -> str: 44 return f"('{self.short}' -> '{self.long}')" 45 46def lookup(short: str, default: str | None = None) -> str: 47 """ 48 Lookup the long name for an alias. 49 Return the alias long name if the alias is found, 50 and the default value if the alias is not found. 51 If the alias is not found and no default is specified, 52 then raise an error. 53 """ 54 55 if (short in Alias._alias_map): 56 return Alias._alias_map[short] 57 58 if (default is not None): 59 return default 60 61 raise ValueError(f"Could not find alias: '{short}'.") 62 63AGENT_CAPTURE_DEFENSIVE: Alias = Alias('agent-capture-defensive', 'pacai.capture.agents.DefensiveAgent') 64AGENT_CAPTURE_OFFENSIVE: Alias = Alias('agent-capture-offensive', 'pacai.capture.agents.OffensiveAgent') 65AGENT_CHEATING: Alias = Alias('agent-cheating', 'pacai.agents.cheating.CheatingAgent') 66AGENT_DUMMY: Alias = Alias('agent-dummy', 'pacai.agents.dummy.DummyAgent') 67AGENT_GO_WEST: Alias = Alias('agent-go-west', 'pacai.agents.gowest.GoWestAgent') 68AGENT_GREEDY: Alias = Alias('agent-greedy', 'pacai.agents.greedy.GreedyAgent') 69AGENT_LEFT_TURN: Alias = Alias('agent-left-turn', 'pacai.agents.leftturn.LeftTurnAgent') 70AGENT_MINIMAX: Alias = Alias('agent-minimax', 'pacai.student.multiagents.MyMinimaxLikeAgent') 71AGENT_QLEARNING: Alias = Alias('agent-qlearning', 'pacai.student.learning.QLearningAgent') 72AGENT_QLEARNING_APPROX: Alias = Alias('agent-qlearning-approx', 'pacai.student.learning.ApproximateQLearningAgent') 73AGENT_QLEARNING_USER: Alias = Alias('agent-qlearning-user', 'pacai.student.learning.QLearningUserInputAgent') 74AGENT_RANDOM: Alias = Alias('agent-random', 'pacai.agents.random.RandomAgent') 75AGENT_REFLEX: Alias = Alias('agent-reflex', 'pacai.student.multiagents.ReflexAgent') 76AGENT_SCRIPTED: Alias = Alias('agent-scripted', 'pacai.agents.scripted.ScriptedAgent') 77AGENT_SEARCH_APPROX: Alias = Alias('agent-search-approx', 'pacai.student.singlesearch.ApproximateSearchAgent') 78AGENT_SEARCH_CLOSEST_DOT: Alias = Alias('agent-search-closest-dot', 'pacai.student.singlesearch.ClosestDotSearchAgent') 79AGENT_SEARCH_PROBLEM: Alias = Alias('agent-search-problem', 'pacai.agents.searchproblem.SearchProblemAgent') 80AGENT_USER_INPUT: Alias = Alias('agent-user-input', 'pacai.agents.userinput.UserInputAgent') 81AGENT_TIMEOUT: Alias = Alias('agent-timeout', 'pacai.agents.testing.TimeoutAgent') 82AGENT_VALUE_ITERATION: Alias = Alias('agent-value-iteration', 'pacai.student.learning.ValueIterationAgent') 83 84AGENT_SHORT_NAMES: list[str] = [ 85 AGENT_CAPTURE_DEFENSIVE.short, 86 AGENT_CAPTURE_OFFENSIVE.short, 87 AGENT_CHEATING.short, 88 AGENT_DUMMY.short, 89 AGENT_GO_WEST.short, 90 AGENT_GREEDY.short, 91 AGENT_LEFT_TURN.short, 92 AGENT_MINIMAX.short, 93 AGENT_QLEARNING.short, 94 AGENT_QLEARNING_APPROX.short, 95 AGENT_QLEARNING_USER.short, 96 AGENT_RANDOM.short, 97 AGENT_REFLEX.short, 98 AGENT_SCRIPTED.short, 99 AGENT_SEARCH_APPROX.short, 100 AGENT_SEARCH_CLOSEST_DOT.short, 101 AGENT_SEARCH_PROBLEM.short, 102 AGENT_USER_INPUT.short, 103 AGENT_TIMEOUT.short, 104 AGENT_VALUE_ITERATION.short, 105] 106 107CAPTURE_TEAM_DUMMY: Alias = Alias('capture-team-dummy', 'pacai.capture.team.create_team_dummy') 108CAPTURE_TEAM_RANDOM: Alias = Alias('capture-team-random', 'pacai.capture.team.create_team_random') 109CAPTURE_TEAM_BASELINE: Alias = Alias('capture-team-baseline', 'pacai.capture.team.create_team_baseline') 110CAPTURE_TEAM_STUDENT: Alias = Alias('capture-team-student', 'pacai.student.capture.create_team') 111 112CAPTURE_TEAM_SHORT_NAMES: list[str] = [ 113 CAPTURE_TEAM_DUMMY.short, 114 CAPTURE_TEAM_RANDOM.short, 115 CAPTURE_TEAM_BASELINE.short, 116 CAPTURE_TEAM_STUDENT.short, 117] 118 119DISTANCE_EUCLIDEAN: Alias = Alias('distance-euclidean', 'pacai.search.distance.euclidean_distance') 120DISTANCE_MANHATTAN: Alias = Alias('distance-manhattan', 'pacai.search.distance.manhattan_distance') 121DISTANCE_MAZE: Alias = Alias('distance-maze', 'pacai.search.distance.maze_distance') 122 123DISTANCE_SHORT_NAMES: list[str] = [ 124 DISTANCE_EUCLIDEAN.short, 125 DISTANCE_MANHATTAN.short, 126 DISTANCE_MAZE.short, 127] 128 129COST_FUNC_LONGITUDINAL: Alias = Alias('cost-longitudinal', 'pacai.search.common.longitudinal_cost_function') 130COST_FUNC_STAY_EAST: Alias = Alias('cost-stay-east', 'pacai.search.common.stay_east_cost_function') 131COST_FUNC_STAY_WEST: Alias = Alias('cost-stay-west', 'pacai.search.common.stay_west_cost_function') 132COST_FUNC_UNIT: Alias = Alias('cost-unit', 'pacai.search.common.unit_cost_function') 133 134COST_FUNC_SHORT_NAMES: list[str] = [ 135 COST_FUNC_LONGITUDINAL.short, 136 COST_FUNC_STAY_EAST.short, 137 COST_FUNC_STAY_WEST.short, 138 COST_FUNC_UNIT.short, 139] 140 141FEATURE_EXTRACTOR_BOARD: Alias = Alias('feature-extractor-board', 'pacai.core.features.board_feature_extractor') 142FEATURE_EXTRACTOR_PACMAN_SIMPLE: Alias = Alias('feature-extractor-pacman-simple', 'pacai.pacman.features.simple_feature_extractor') 143FEATURE_EXTRACTOR_SCORE: Alias = Alias('feature-extractor-score', 'pacai.core.features.score_feature_extractor') 144 145FEATURE_EXTRACTOR_SHORT_NAMES: list[str] = [ 146 FEATURE_EXTRACTOR_BOARD.short, 147 FEATURE_EXTRACTOR_SCORE.short, 148] 149 150HEURISTIC_CORNERS: Alias = Alias('heuristic-corners', 'pacai.student.singlesearch.corners_heuristic') 151HEURISTIC_EUCLIDEAN: Alias = Alias('heuristic-euclidean', 'pacai.search.distance.euclidean_heuristic') 152HEURISTIC_FOOD: Alias = Alias('heuristic-food', 'pacai.student.singlesearch.food_heuristic') 153HEURISTIC_MANHATTAN: Alias = Alias('heuristic-manhattan', 'pacai.search.distance.manhattan_heuristic') 154HEURISTIC_NULL: Alias = Alias('heuristic-null', 'pacai.search.common.null_heuristic') 155 156HEURISTIC_SHORT_NAMES: list[str] = [ 157 HEURISTIC_CORNERS.short, 158 HEURISTIC_EUCLIDEAN.short, 159 HEURISTIC_FOOD.short, 160 HEURISTIC_MANHATTAN.short, 161 HEURISTIC_NULL.short, 162] 163 164MDP_STATE_CLASS_BOARD: Alias = Alias('mdp-state-class-board', 'pacai.core.mdp.MDPStateBoard') 165MDP_STATE_CLASS_POSITION: Alias = Alias('mdp-state-class-position', 'pacai.core.mdp.MDPStatePosition') 166 167MDP_STATE_CLASS_SHORT_NAMES: list[str] = [ 168 MDP_STATE_CLASS_BOARD.short, 169 MDP_STATE_CLASS_POSITION.short, 170] 171 172SEARCH_PROBLEM_CORNERS: Alias = Alias('search-problem-corners', 'pacai.student.singlesearch.CornersSearchProblem') 173SEARCH_PROBLEM_FOOD: Alias = Alias('search-problem-food', 'pacai.search.food.FoodSearchProblem') 174SEARCH_PROBLEM_POSITION: Alias = Alias('search-problem-position', 'pacai.search.position.PositionSearchProblem') 175 176SEARCH_PROBLEM_SHORT_NAMES: list[str] = [ 177 SEARCH_PROBLEM_CORNERS.short, 178 SEARCH_PROBLEM_FOOD.short, 179 SEARCH_PROBLEM_POSITION.short, 180] 181 182SEARCH_SOLVER_ASTAR: Alias = Alias('search-solver-astar', 'pacai.student.singlesearch.astar_search') 183SEARCH_SOLVER_BFS: Alias = Alias('search-solver-bfs', 'pacai.student.singlesearch.breadth_first_search') 184SEARCH_SOLVER_DFS: Alias = Alias('search-solver-dfs', 'pacai.student.singlesearch.depth_first_search') 185SEARCH_SOLVER_MAZE_TINY: Alias = Alias('search-solver-maze-tiny', 'pacai.search.mazetiny.maze_tiny_search') 186SEARCH_SOLVER_RANDOM: Alias = Alias('search-solver-random', 'pacai.search.random.random_search') 187SEARCH_SOLVER_UCS: Alias = Alias('search-solver-ucs', 'pacai.student.singlesearch.uniform_cost_search') 188 189SEARCH_SOLVER_SHORT_NAMES: list[str] = [ 190 SEARCH_SOLVER_ASTAR.short, 191 SEARCH_SOLVER_BFS.short, 192 SEARCH_SOLVER_DFS.short, 193 SEARCH_SOLVER_MAZE_TINY.short, 194 SEARCH_SOLVER_RANDOM.short, 195 SEARCH_SOLVER_UCS.short, 196] 197 198STATE_EVAL_BASE: Alias = Alias('state-eval-base', 'pacai.core.gamestate.base_eval') 199STATE_EVAL_MINIMAX_BETTER: Alias = Alias('state-eval-minimax-better', 'pacai.student.multiagents.better_state_eval') 200 201STATE_EVAL_SHORT_NAMES: list[str] = [ 202 STATE_EVAL_BASE.short, 203 STATE_EVAL_MINIMAX_BETTER.short, 204] 205 206UI_NULL: Alias = Alias('null', 'pacai.ui.null.NullUI') 207UI_STDIO: Alias = Alias('text', 'pacai.ui.text.StdioUI', skip_windows_test = True) 208UI_STDIO_PACMAN: Alias = Alias('text-pacman', 'pacai.pacman.textui.StdioUI', skip_windows_test = True) 209UI_RAW_TEXT: Alias = Alias('raw-text', 'pacai.ui.text.TextUI', skip_windows_test = True) 210UI_TK: Alias = Alias('tk', 'pacai.ui.tk.TkUI') 211UI_WEB: Alias = Alias('web', 'pacai.ui.web.WebUI') 212 213UI_SHORT_NAMES: list[str] = [ 214 UI_NULL.short, 215 UI_STDIO.short, 216 UI_TK.short, 217 UI_WEB.short, 218]
class
Alias:
7class Alias: 8 """ An alias for some object name. """ 9 10 _alias_map: dict[str, str] = {} 11 """ Keep track of all aliases for testing purposes. """ 12 13 _all_aliases: list['Alias'] = [] 14 """ Keep track of all aliases mappings for lookup. """ 15 16 def __init__(self, 17 short: str, long: str, 18 is_qualified_name: bool = True, skip_windows_test: bool = False, 19 ) -> None: 20 self.short: str = short 21 """ The short name for this alias. """ 22 23 self.long: str = long 24 """ The long name for this alias. """ 25 26 self.is_qualified_name: bool = is_qualified_name 27 """ 28 Whether this alias represents a qualified name. 29 Alias that are qualified names will be tested with reflection. 30 """ 31 32 self.skip_windows_test: bool = skip_windows_test 33 """ If this alias' reflection test should be skipped on Windows. """ 34 35 if ('.' in short): 36 raise ValueError(f"Dots ('.') are not allowed in aliases. Found '{short}'.") 37 38 if (short in Alias._alias_map): 39 raise ValueError(f"Found duplicate alias: '{short}' -> '{long}'.") 40 41 Alias._alias_map[short] = long 42 Alias._all_aliases.append(self) 43 44 def __repr__(self) -> str: 45 return f"('{self.short}' -> '{self.long}')"
An alias for some object name.
Alias( short: str, long: str, is_qualified_name: bool = True, skip_windows_test: bool = False)
16 def __init__(self, 17 short: str, long: str, 18 is_qualified_name: bool = True, skip_windows_test: bool = False, 19 ) -> None: 20 self.short: str = short 21 """ The short name for this alias. """ 22 23 self.long: str = long 24 """ The long name for this alias. """ 25 26 self.is_qualified_name: bool = is_qualified_name 27 """ 28 Whether this alias represents a qualified name. 29 Alias that are qualified names will be tested with reflection. 30 """ 31 32 self.skip_windows_test: bool = skip_windows_test 33 """ If this alias' reflection test should be skipped on Windows. """ 34 35 if ('.' in short): 36 raise ValueError(f"Dots ('.') are not allowed in aliases. Found '{short}'.") 37 38 if (short in Alias._alias_map): 39 raise ValueError(f"Found duplicate alias: '{short}' -> '{long}'.") 40 41 Alias._alias_map[short] = long 42 Alias._all_aliases.append(self)
def
lookup(short: str, default: str | None = None) -> str:
47def lookup(short: str, default: str | None = None) -> str: 48 """ 49 Lookup the long name for an alias. 50 Return the alias long name if the alias is found, 51 and the default value if the alias is not found. 52 If the alias is not found and no default is specified, 53 then raise an error. 54 """ 55 56 if (short in Alias._alias_map): 57 return Alias._alias_map[short] 58 59 if (default is not None): 60 return default 61 62 raise ValueError(f"Could not find alias: '{short}'.")
Lookup the long name for an alias. Return the alias long name if the alias is found, and the default value if the alias is not found. If the alias is not found and no default is specified, then raise an error.
AGENT_CAPTURE_DEFENSIVE: Alias =
('agent-capture-defensive' -> 'pacai.capture.agents.DefensiveAgent')
AGENT_CAPTURE_OFFENSIVE: Alias =
('agent-capture-offensive' -> 'pacai.capture.agents.OffensiveAgent')
AGENT_QLEARNING_APPROX: Alias =
('agent-qlearning-approx' -> 'pacai.student.learning.ApproximateQLearningAgent')
AGENT_QLEARNING_USER: Alias =
('agent-qlearning-user' -> 'pacai.student.learning.QLearningUserInputAgent')
AGENT_SEARCH_APPROX: Alias =
('agent-search-approx' -> 'pacai.student.singlesearch.ApproximateSearchAgent')
AGENT_SEARCH_CLOSEST_DOT: Alias =
('agent-search-closest-dot' -> 'pacai.student.singlesearch.ClosestDotSearchAgent')
AGENT_SEARCH_PROBLEM: Alias =
('agent-search-problem' -> 'pacai.agents.searchproblem.SearchProblemAgent')
AGENT_VALUE_ITERATION: Alias =
('agent-value-iteration' -> 'pacai.student.learning.ValueIterationAgent')
AGENT_SHORT_NAMES: list[str] =
['agent-capture-defensive', 'agent-capture-offensive', 'agent-cheating', 'agent-dummy', 'agent-go-west', 'agent-greedy', 'agent-left-turn', 'agent-minimax', 'agent-qlearning', 'agent-qlearning-approx', 'agent-qlearning-user', 'agent-random', 'agent-reflex', 'agent-scripted', 'agent-search-approx', 'agent-search-closest-dot', 'agent-search-problem', 'agent-user-input', 'agent-timeout', 'agent-value-iteration']
CAPTURE_TEAM_BASELINE: Alias =
('capture-team-baseline' -> 'pacai.capture.team.create_team_baseline')
CAPTURE_TEAM_SHORT_NAMES: list[str] =
['capture-team-dummy', 'capture-team-random', 'capture-team-baseline', 'capture-team-student']
DISTANCE_SHORT_NAMES: list[str] =
['distance-euclidean', 'distance-manhattan', 'distance-maze']
COST_FUNC_LONGITUDINAL: Alias =
('cost-longitudinal' -> 'pacai.search.common.longitudinal_cost_function')
COST_FUNC_SHORT_NAMES: list[str] =
['cost-longitudinal', 'cost-stay-east', 'cost-stay-west', 'cost-unit']
FEATURE_EXTRACTOR_BOARD: Alias =
('feature-extractor-board' -> 'pacai.core.features.board_feature_extractor')
FEATURE_EXTRACTOR_PACMAN_SIMPLE: Alias =
('feature-extractor-pacman-simple' -> 'pacai.pacman.features.simple_feature_extractor')
FEATURE_EXTRACTOR_SCORE: Alias =
('feature-extractor-score' -> 'pacai.core.features.score_feature_extractor')
FEATURE_EXTRACTOR_SHORT_NAMES: list[str] =
['feature-extractor-board', 'feature-extractor-score']
HEURISTIC_SHORT_NAMES: list[str] =
['heuristic-corners', 'heuristic-euclidean', 'heuristic-food', 'heuristic-manhattan', 'heuristic-null']
MDP_STATE_CLASS_SHORT_NAMES: list[str] =
['mdp-state-class-board', 'mdp-state-class-position']
SEARCH_PROBLEM_CORNERS: Alias =
('search-problem-corners' -> 'pacai.student.singlesearch.CornersSearchProblem')
SEARCH_PROBLEM_POSITION: Alias =
('search-problem-position' -> 'pacai.search.position.PositionSearchProblem')
SEARCH_PROBLEM_SHORT_NAMES: list[str] =
['search-problem-corners', 'search-problem-food', 'search-problem-position']
SEARCH_SOLVER_BFS: Alias =
('search-solver-bfs' -> 'pacai.student.singlesearch.breadth_first_search')
SEARCH_SOLVER_MAZE_TINY: Alias =
('search-solver-maze-tiny' -> 'pacai.search.mazetiny.maze_tiny_search')
SEARCH_SOLVER_UCS: Alias =
('search-solver-ucs' -> 'pacai.student.singlesearch.uniform_cost_search')
SEARCH_SOLVER_SHORT_NAMES: list[str] =
['search-solver-astar', 'search-solver-bfs', 'search-solver-dfs', 'search-solver-maze-tiny', 'search-solver-random', 'search-solver-ucs']
STATE_EVAL_MINIMAX_BETTER: Alias =
('state-eval-minimax-better' -> 'pacai.student.multiagents.better_state_eval')
STATE_EVAL_SHORT_NAMES: list[str] =
['state-eval-base', 'state-eval-minimax-better']
UI_SHORT_NAMES: list[str] =
['null', 'text', 'tk', 'web']