chessai.util.alias
This file provides useful aliases/shortcuts or short names for different objects in chessai.
For example, the agent-random alias can be used to reference chessai.agents.random.RandomAgent.
1""" 2This file provides useful aliases/shortcuts or short names for different objects in chessai. 3For example, the `agent-random` alias can be used to reference `chessai.agents.random.RandomAgent`. 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_AGGRESSIVE: Alias = Alias('agent-aggressive', 'chessai.agents.aggressive.AggressiveAgent') 64AGENT_DUMMY: Alias = Alias('agent-dummy', 'chessai.agents.dummy.DummyAgent') 65AGENT_GAURD: Alias = Alias('agent-gaurd', 'chessai.agents.gaurd.GaurdAgent') 66AGENT_GREEDY: Alias = Alias('agent-greedy', 'chessai.agents.greedy.GreedyAgent') 67AGENT_MINIMAX: Alias = Alias('agent-minimax', 'chessai.student.multiagents.MyMinimaxLikeAgent') 68AGENT_MULTI_SCRIPTED: Alias = Alias('agent-multi-scripted', 'chessai.agents.multiscripted.MultiScriptedAgent') 69AGENT_PICK_FIRST: Alias = Alias('agent-pick-first', 'chessai.agents.pickfirst.PickFirstAgent') 70AGENT_RANDOM: Alias = Alias('agent-random', 'chessai.agents.random.RandomAgent') 71AGENT_REFLEX: Alias = Alias('agent-reflex', 'chessai.student.multiagents.ReflexAgent') 72AGENT_SCRIPTED: Alias = Alias('agent-scripted', 'chessai.agents.scripted.ScriptedAgent') 73AGENT_SEARCH_PROBLEM: Alias = Alias('agent-search-problem', 'chessai.agents.searchproblem.SearchProblemAgent') 74AGENT_TIMEOUT: Alias = Alias('agent-timeout', 'chessai.agents.testing.TimeoutAgent') 75AGENT_USER_INPUT: Alias = Alias('agent-user-input', 'chessai.agents.userinput.UserInputAgent') 76 77AGENT_SHORT_NAMES: list[str] = [ 78 AGENT_AGGRESSIVE.short, 79 AGENT_DUMMY.short, 80 AGENT_MINIMAX.short, 81 AGENT_MULTI_SCRIPTED.short, 82 AGENT_GAURD.short, 83 AGENT_GREEDY.short, 84 AGENT_RANDOM.short, 85 AGENT_SCRIPTED.short, 86 AGENT_SEARCH_PROBLEM.short, 87 AGENT_TIMEOUT.short, 88 AGENT_USER_INPUT.short, 89] 90 91DISTANCE_EUCLIDEAN: Alias = Alias('distance-euclidean', 'chessai.search.distance.euclidean_distance') 92DISTANCE_MANHATTAN: Alias = Alias('distance-manhattan', 'chessai.search.distance.manhattan_distance') 93DISTANCE_KNIGHT: Alias = Alias('distance-knight', 'chessai.search.distance.knight_distance') 94 95DISTANCE_SHORT_NAMES: list[str] = [ 96 DISTANCE_EUCLIDEAN.short, 97 DISTANCE_MANHATTAN.short, 98 DISTANCE_KNIGHT.short, 99] 100 101COST_FUNC_LONGITUDINAL: Alias = Alias('cost-longitudinal', 'chessai.search.common.longitudinal_cost_function') 102COST_FUNC_STAY_EAST: Alias = Alias('cost-stay-east', 'chessai.search.common.stay_east_cost_function') 103COST_FUNC_STAY_WEST: Alias = Alias('cost-stay-west', 'chessai.search.common.stay_west_cost_function') 104COST_FUNC_UNIT: Alias = Alias('cost-unit', 'chessai.search.common.unit_cost_function') 105 106COST_FUNC_SHORT_NAMES: list[str] = [ 107 COST_FUNC_LONGITUDINAL.short, 108 COST_FUNC_STAY_EAST.short, 109 COST_FUNC_STAY_WEST.short, 110 COST_FUNC_UNIT.short, 111] 112 113HEURISTIC_EUCLIDEAN: Alias = Alias('heuristic-euclidean', 'chessai.search.distance.euclidean_heuristic') 114HEURISTIC_KNIGHT: Alias = Alias('heuristic-knight', 'chessai.student.singlesearch.knights_tour_heuristic') 115HEURISTIC_MANHATTAN: Alias = Alias('heuristic-manhattan', 'chessai.search.distance.manhattan_heuristic') 116HEURISTIC_NULL: Alias = Alias('heuristic-null', 'chessai.search.common.null_heuristic') 117 118HEURISTIC_SHORT_NAMES: list[str] = [ 119 HEURISTIC_EUCLIDEAN.short, 120 HEURISTIC_KNIGHT.short, 121 HEURISTIC_MANHATTAN.short, 122 HEURISTIC_NULL.short, 123] 124 125SEARCH_PROBLEM_POSITION: Alias = Alias('search-problem-position', 'chessai.search.position.PositionSearchProblem') 126 127SEARCH_PROBLEM_SHORT_NAMES: list[str] = [ 128 SEARCH_PROBLEM_POSITION.short, 129] 130 131SEARCH_SOLVER_ASTAR: Alias = Alias('search-solver-astar', 'chessai.student.singlesearch.astar_search') 132SEARCH_SOLVER_BFS: Alias = Alias('search-solver-bfs', 'chessai.student.singlesearch.breadth_first_search') 133SEARCH_SOLVER_DFS: Alias = Alias('search-solver-dfs', 'chessai.student.singlesearch.depth_first_search') 134SEARCH_SOLVER_TOUR_BASE: Alias = Alias('search-solver-tour-base', 'chessai.search.tourbase.tour_base_search') 135SEARCH_SOLVER_RANDOM: Alias = Alias('search-solver-random', 'chessai.search.random.random_search') 136SEARCH_SOLVER_UCS: Alias = Alias('search-solver-ucs', 'chessai.student.singlesearch.uniform_cost_search') 137 138SEARCH_SOLVER_SHORT_NAMES: list[str] = [ 139 SEARCH_SOLVER_ASTAR.short, 140 SEARCH_SOLVER_BFS.short, 141 SEARCH_SOLVER_DFS.short, 142 SEARCH_SOLVER_TOUR_BASE.short, 143 SEARCH_SOLVER_RANDOM.short, 144 SEARCH_SOLVER_UCS.short, 145] 146 147STATE_EVAL_BASE: Alias = Alias('state-eval-base', 'chessai.chess.gamestate.base_eval') 148STATE_EVAL_MINIMAX_BETTER: Alias = Alias('state-eval-minimax-better', 'chessai.student.multiagents.better_state_eval') 149 150STATE_EVAL_SHORT_NAMES: list[str] = [ 151 STATE_EVAL_BASE.short, 152 STATE_EVAL_MINIMAX_BETTER.short, 153] 154 155UI_NULL: Alias = Alias('null', 'chessai.ui.null.NullUI') 156UI_STDIO: Alias = Alias('text', 'chessai.ui.text.StdioUI', skip_windows_test = True) 157# UI_STDIO_PACMAN: Alias = Alias('text-pacman', 'chessai.pacman.textui.StdioUI', skip_windows_test = True) 158UI_RAW_TEXT: Alias = Alias('raw-text', 'chessai.ui.text.TextUI', skip_windows_test = True) 159# UI_TK: Alias = Alias('tk', 'chessai.ui.tk.TkUI') 160# UI_WEB: Alias = Alias('web', 'chessai.ui.web.WebUI') 161 162UI_SHORT_NAMES: list[str] = [ 163 UI_NULL.short, 164 UI_STDIO.short, 165 # UI_TK.short, 166 # UI_WEB.short, 167]
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_MULTI_SCRIPTED: Alias =
('agent-multi-scripted' -> 'chessai.agents.multiscripted.MultiScriptedAgent')
AGENT_SEARCH_PROBLEM: Alias =
('agent-search-problem' -> 'chessai.agents.searchproblem.SearchProblemAgent')
AGENT_SHORT_NAMES: list[str] =
['agent-aggressive', 'agent-dummy', 'agent-minimax', 'agent-multi-scripted', 'agent-gaurd', 'agent-greedy', 'agent-random', 'agent-scripted', 'agent-search-problem', 'agent-timeout', 'agent-user-input']
DISTANCE_SHORT_NAMES: list[str] =
['distance-euclidean', 'distance-manhattan', 'distance-knight']
COST_FUNC_LONGITUDINAL: Alias =
('cost-longitudinal' -> 'chessai.search.common.longitudinal_cost_function')
COST_FUNC_SHORT_NAMES: list[str] =
['cost-longitudinal', 'cost-stay-east', 'cost-stay-west', 'cost-unit']
HEURISTIC_EUCLIDEAN: Alias =
('heuristic-euclidean' -> 'chessai.search.distance.euclidean_heuristic')
HEURISTIC_KNIGHT: Alias =
('heuristic-knight' -> 'chessai.student.singlesearch.knights_tour_heuristic')
HEURISTIC_MANHATTAN: Alias =
('heuristic-manhattan' -> 'chessai.search.distance.manhattan_heuristic')
HEURISTIC_SHORT_NAMES: list[str] =
['heuristic-euclidean', 'heuristic-knight', 'heuristic-manhattan', 'heuristic-null']
SEARCH_PROBLEM_POSITION: Alias =
('search-problem-position' -> 'chessai.search.position.PositionSearchProblem')
SEARCH_PROBLEM_SHORT_NAMES: list[str] =
['search-problem-position']
SEARCH_SOLVER_BFS: Alias =
('search-solver-bfs' -> 'chessai.student.singlesearch.breadth_first_search')
SEARCH_SOLVER_DFS: Alias =
('search-solver-dfs' -> 'chessai.student.singlesearch.depth_first_search')
SEARCH_SOLVER_TOUR_BASE: Alias =
('search-solver-tour-base' -> 'chessai.search.tourbase.tour_base_search')
SEARCH_SOLVER_UCS: Alias =
('search-solver-ucs' -> 'chessai.student.singlesearch.uniform_cost_search')
SEARCH_SOLVER_SHORT_NAMES: list[str] =
['search-solver-astar', 'search-solver-bfs', 'search-solver-dfs', 'search-solver-tour-base', 'search-solver-random', 'search-solver-ucs']
STATE_EVAL_MINIMAX_BETTER: Alias =
('state-eval-minimax-better' -> 'chessai.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']