pacai.core.agentinfo
1import typing 2 3import edq.util.json 4 5import pacai.util.alias 6import pacai.util.reflection 7 8DEFAULT_MOVE_DELAY: int = 100 9""" The default delay between agent moves. """ 10 11DEFAULT_STATE_EVAL: str = pacai.util.alias.STATE_EVAL_BASE.long 12 13class AgentInfo(edq.util.json.DictConverter): 14 """ 15 Argument used to construct an agent. 16 Common arguments used by the engine are stored as top-level fields, 17 while arguments that specific child agents may use are stored in a general dict. 18 19 Then additional arguments should be kept to simple types 20 that can be serialized/deserialized via the standard Python JSON library. 21 """ 22 23 def __init__(self, 24 name: str | pacai.util.reflection.Reference = '', 25 move_delay: int | None = DEFAULT_MOVE_DELAY, 26 state_eval_func: pacai.util.reflection.Reference | str = DEFAULT_STATE_EVAL, 27 extra_arguments: dict[str, typing.Any] | None = None, 28 **kwargs: typing.Any) -> None: 29 if (isinstance(name, str)): 30 name = pacai.util.reflection.Reference(name) 31 32 self.name: pacai.util.reflection.Reference = name 33 """ The name of the agent's class. """ 34 35 if (move_delay is None): 36 move_delay = DEFAULT_MOVE_DELAY 37 38 if (move_delay <= 0): 39 raise ValueError("Agent move delay must be > 0.") 40 41 self.move_delay: int = move_delay 42 """ The move delay of the agent. """ 43 44 self.state_eval_func: pacai.util.reflection.Reference = pacai.util.reflection.Reference(state_eval_func) 45 """ The state evaluation function this agent will use. """ 46 47 self.extra_arguments: dict[str, typing.Any] = {} 48 """ 49 Additional arguments to the agent. 50 These are typically used by agent subclasses. 51 """ 52 53 self.extra_arguments.update(kwargs) 54 if (extra_arguments is not None): 55 self.extra_arguments.update(extra_arguments) 56 57 def set_from_string(self, name: str, value: str) -> None: 58 """ Set an attribute by name. """ 59 60 if (name == 'name'): 61 self.name = pacai.util.reflection.Reference(value) 62 elif (name == 'move_delay'): 63 self.move_delay = int(value) 64 elif (name == 'state_eval_func'): 65 self.state_eval_func = pacai.util.reflection.Reference(value) 66 else: 67 self.extra_arguments[name] = value 68 69 def update(self, other: 'AgentInfo') -> None: 70 """ Update this agent info data from the given agent info. """ 71 72 self.name = other.name 73 self.move_delay = other.move_delay 74 self.state_eval_func = other.state_eval_func 75 self.extra_arguments.update(other.extra_arguments) 76 77 def to_flat_dict(self) -> dict[str, typing.Any]: 78 """ 79 Convert this information to a flat dictionary, 80 where the extra arguments are on the same level as name and move_delay. 81 """ 82 83 result = self.extra_arguments.copy() 84 85 result['name'] = self.name 86 result['move_delay'] = self.move_delay 87 result['state_eval_func'] = str(self.state_eval_func) 88 89 return result 90 91 def to_dict(self) -> dict[str, typing.Any]: 92 data = vars(self).copy() 93 data['name'] = self.name.to_dict() 94 data['state_eval_func'] = self.state_eval_func.to_dict() 95 return data 96 97 @classmethod 98 def from_dict(cls, data: dict[str, typing.Any]) -> typing.Any: 99 data = data.copy() 100 data['name'] = pacai.util.reflection.Reference.from_dict(data['name']) 101 102 if ('state_eval_func' in data): 103 data['state_eval_func'] = pacai.util.reflection.Reference.from_dict(data['state_eval_func']) 104 105 return cls(**data)
The default delay between agent moves.
14class AgentInfo(edq.util.json.DictConverter): 15 """ 16 Argument used to construct an agent. 17 Common arguments used by the engine are stored as top-level fields, 18 while arguments that specific child agents may use are stored in a general dict. 19 20 Then additional arguments should be kept to simple types 21 that can be serialized/deserialized via the standard Python JSON library. 22 """ 23 24 def __init__(self, 25 name: str | pacai.util.reflection.Reference = '', 26 move_delay: int | None = DEFAULT_MOVE_DELAY, 27 state_eval_func: pacai.util.reflection.Reference | str = DEFAULT_STATE_EVAL, 28 extra_arguments: dict[str, typing.Any] | None = None, 29 **kwargs: typing.Any) -> None: 30 if (isinstance(name, str)): 31 name = pacai.util.reflection.Reference(name) 32 33 self.name: pacai.util.reflection.Reference = name 34 """ The name of the agent's class. """ 35 36 if (move_delay is None): 37 move_delay = DEFAULT_MOVE_DELAY 38 39 if (move_delay <= 0): 40 raise ValueError("Agent move delay must be > 0.") 41 42 self.move_delay: int = move_delay 43 """ The move delay of the agent. """ 44 45 self.state_eval_func: pacai.util.reflection.Reference = pacai.util.reflection.Reference(state_eval_func) 46 """ The state evaluation function this agent will use. """ 47 48 self.extra_arguments: dict[str, typing.Any] = {} 49 """ 50 Additional arguments to the agent. 51 These are typically used by agent subclasses. 52 """ 53 54 self.extra_arguments.update(kwargs) 55 if (extra_arguments is not None): 56 self.extra_arguments.update(extra_arguments) 57 58 def set_from_string(self, name: str, value: str) -> None: 59 """ Set an attribute by name. """ 60 61 if (name == 'name'): 62 self.name = pacai.util.reflection.Reference(value) 63 elif (name == 'move_delay'): 64 self.move_delay = int(value) 65 elif (name == 'state_eval_func'): 66 self.state_eval_func = pacai.util.reflection.Reference(value) 67 else: 68 self.extra_arguments[name] = value 69 70 def update(self, other: 'AgentInfo') -> None: 71 """ Update this agent info data from the given agent info. """ 72 73 self.name = other.name 74 self.move_delay = other.move_delay 75 self.state_eval_func = other.state_eval_func 76 self.extra_arguments.update(other.extra_arguments) 77 78 def to_flat_dict(self) -> dict[str, typing.Any]: 79 """ 80 Convert this information to a flat dictionary, 81 where the extra arguments are on the same level as name and move_delay. 82 """ 83 84 result = self.extra_arguments.copy() 85 86 result['name'] = self.name 87 result['move_delay'] = self.move_delay 88 result['state_eval_func'] = str(self.state_eval_func) 89 90 return result 91 92 def to_dict(self) -> dict[str, typing.Any]: 93 data = vars(self).copy() 94 data['name'] = self.name.to_dict() 95 data['state_eval_func'] = self.state_eval_func.to_dict() 96 return data 97 98 @classmethod 99 def from_dict(cls, data: dict[str, typing.Any]) -> typing.Any: 100 data = data.copy() 101 data['name'] = pacai.util.reflection.Reference.from_dict(data['name']) 102 103 if ('state_eval_func' in data): 104 data['state_eval_func'] = pacai.util.reflection.Reference.from_dict(data['state_eval_func']) 105 106 return cls(**data)
Argument used to construct an agent. Common arguments used by the engine are stored as top-level fields, while arguments that specific child agents may use are stored in a general dict.
Then additional arguments should be kept to simple types that can be serialized/deserialized via the standard Python JSON library.
24 def __init__(self, 25 name: str | pacai.util.reflection.Reference = '', 26 move_delay: int | None = DEFAULT_MOVE_DELAY, 27 state_eval_func: pacai.util.reflection.Reference | str = DEFAULT_STATE_EVAL, 28 extra_arguments: dict[str, typing.Any] | None = None, 29 **kwargs: typing.Any) -> None: 30 if (isinstance(name, str)): 31 name = pacai.util.reflection.Reference(name) 32 33 self.name: pacai.util.reflection.Reference = name 34 """ The name of the agent's class. """ 35 36 if (move_delay is None): 37 move_delay = DEFAULT_MOVE_DELAY 38 39 if (move_delay <= 0): 40 raise ValueError("Agent move delay must be > 0.") 41 42 self.move_delay: int = move_delay 43 """ The move delay of the agent. """ 44 45 self.state_eval_func: pacai.util.reflection.Reference = pacai.util.reflection.Reference(state_eval_func) 46 """ The state evaluation function this agent will use. """ 47 48 self.extra_arguments: dict[str, typing.Any] = {} 49 """ 50 Additional arguments to the agent. 51 These are typically used by agent subclasses. 52 """ 53 54 self.extra_arguments.update(kwargs) 55 if (extra_arguments is not None): 56 self.extra_arguments.update(extra_arguments)
Additional arguments to the agent. These are typically used by agent subclasses.
58 def set_from_string(self, name: str, value: str) -> None: 59 """ Set an attribute by name. """ 60 61 if (name == 'name'): 62 self.name = pacai.util.reflection.Reference(value) 63 elif (name == 'move_delay'): 64 self.move_delay = int(value) 65 elif (name == 'state_eval_func'): 66 self.state_eval_func = pacai.util.reflection.Reference(value) 67 else: 68 self.extra_arguments[name] = value
Set an attribute by name.
70 def update(self, other: 'AgentInfo') -> None: 71 """ Update this agent info data from the given agent info. """ 72 73 self.name = other.name 74 self.move_delay = other.move_delay 75 self.state_eval_func = other.state_eval_func 76 self.extra_arguments.update(other.extra_arguments)
Update this agent info data from the given agent info.
78 def to_flat_dict(self) -> dict[str, typing.Any]: 79 """ 80 Convert this information to a flat dictionary, 81 where the extra arguments are on the same level as name and move_delay. 82 """ 83 84 result = self.extra_arguments.copy() 85 86 result['name'] = self.name 87 result['move_delay'] = self.move_delay 88 result['state_eval_func'] = str(self.state_eval_func) 89 90 return result
Convert this information to a flat dictionary, where the extra arguments are on the same level as name and move_delay.
92 def to_dict(self) -> dict[str, typing.Any]: 93 data = vars(self).copy() 94 data['name'] = self.name.to_dict() 95 data['state_eval_func'] = self.state_eval_func.to_dict() 96 return data
Return a dict that can be used to represent this object. If the dict is passed to from_dict(), an identical object should be reconstructed.
A general (but inefficient) implementation is provided by default.
98 @classmethod 99 def from_dict(cls, data: dict[str, typing.Any]) -> typing.Any: 100 data = data.copy() 101 data['name'] = pacai.util.reflection.Reference.from_dict(data['name']) 102 103 if ('state_eval_func' in data): 104 data['state_eval_func'] = pacai.util.reflection.Reference.from_dict(data['state_eval_func']) 105 106 return cls(**data)
Return an instance of this subclass created using the given dict. If the dict came from to_dict(), the returned object should be identical to the original.
A general (but inefficient) implementation is provided by default.