chessai.core.agentinfo

 1import typing
 2
 3import edq.util.serial
 4
 5import chessai.util.alias
 6import chessai.util.reflection
 7
 8DEFAULT_STATE_EVAL: str = chessai.util.alias.STATE_EVAL_BASE.long
 9
10class AgentInfo(edq.util.serial.DictConverter):
11    """
12    Argument used to construct an agent.
13    Common arguments used by the engine are stored as top-level fields,
14    while arguments that specific child agents may use are stored in a general dict.
15
16    Then additional arguments should be kept to simple types
17    that can be serialized/deserialized via the standard Python JSON library.
18    """
19
20    def __init__(self,
21            name: str | chessai.util.reflection.Reference = '',
22            state_eval_func: chessai.util.reflection.Reference | str = DEFAULT_STATE_EVAL,
23            display_name: str = '',
24            extra_arguments: dict[str, typing.Any] | None = None,
25            **kwargs: typing.Any) -> None:
26        if (isinstance(name, str)):
27            name = chessai.util.reflection.Reference(name)
28
29        self.name: chessai.util.reflection.Reference = name
30        """ The name of the agent's class. """
31
32        self.state_eval_func: chessai.util.reflection.Reference = chessai.util.reflection.Reference(state_eval_func)
33        """ The state evaluation function this agent will use. """
34
35        if (display_name == ''):
36            display_name = str(self.name)
37
38        self.display_name = display_name
39        """ The display name of the agent. """
40
41        self.extra_arguments: dict[str, typing.Any] = {}
42        """
43        Additional arguments to the agent.
44        These are typically used by agent subclasses.
45        """
46
47        self.extra_arguments.update(kwargs)
48        if (extra_arguments is not None):
49            self.extra_arguments.update(extra_arguments)
50
51    def set_from_string(self, name: str, value: str) -> None:
52        """ Set an attribute by name. """
53
54        if (name == 'name'):
55            self.name = chessai.util.reflection.Reference(value)
56        elif (name == 'state_eval_func'):
57            self.state_eval_func = chessai.util.reflection.Reference(value)
58        else:
59            self.extra_arguments[name] = value
60
61    def update(self, other: 'AgentInfo') -> None:
62        """ Update this agent info data from the given agent info. """
63
64        self.name = other.name
65        self.state_eval_func = other.state_eval_func
66        self.display_name = other.display_name
67        self.extra_arguments.update(other.extra_arguments)
68
69    def to_flat_dict(self) -> dict[str, typing.Any]:
70        """
71        Convert this information to a flat dictionary,
72        where the extra arguments are on the same level as name and move_delay.
73        """
74
75        result = self.extra_arguments.copy()
76
77        result['name'] = self.name
78        result['state_eval_func'] = str(self.state_eval_func)
79        result['display_name'] = self.display_name
80
81        return result
DEFAULT_STATE_EVAL: str = 'chessai.chess.gamestate.base_eval'
class AgentInfo(edq.util.serial.DictConverter):
11class AgentInfo(edq.util.serial.DictConverter):
12    """
13    Argument used to construct an agent.
14    Common arguments used by the engine are stored as top-level fields,
15    while arguments that specific child agents may use are stored in a general dict.
16
17    Then additional arguments should be kept to simple types
18    that can be serialized/deserialized via the standard Python JSON library.
19    """
20
21    def __init__(self,
22            name: str | chessai.util.reflection.Reference = '',
23            state_eval_func: chessai.util.reflection.Reference | str = DEFAULT_STATE_EVAL,
24            display_name: str = '',
25            extra_arguments: dict[str, typing.Any] | None = None,
26            **kwargs: typing.Any) -> None:
27        if (isinstance(name, str)):
28            name = chessai.util.reflection.Reference(name)
29
30        self.name: chessai.util.reflection.Reference = name
31        """ The name of the agent's class. """
32
33        self.state_eval_func: chessai.util.reflection.Reference = chessai.util.reflection.Reference(state_eval_func)
34        """ The state evaluation function this agent will use. """
35
36        if (display_name == ''):
37            display_name = str(self.name)
38
39        self.display_name = display_name
40        """ The display name of the agent. """
41
42        self.extra_arguments: dict[str, typing.Any] = {}
43        """
44        Additional arguments to the agent.
45        These are typically used by agent subclasses.
46        """
47
48        self.extra_arguments.update(kwargs)
49        if (extra_arguments is not None):
50            self.extra_arguments.update(extra_arguments)
51
52    def set_from_string(self, name: str, value: str) -> None:
53        """ Set an attribute by name. """
54
55        if (name == 'name'):
56            self.name = chessai.util.reflection.Reference(value)
57        elif (name == 'state_eval_func'):
58            self.state_eval_func = chessai.util.reflection.Reference(value)
59        else:
60            self.extra_arguments[name] = value
61
62    def update(self, other: 'AgentInfo') -> None:
63        """ Update this agent info data from the given agent info. """
64
65        self.name = other.name
66        self.state_eval_func = other.state_eval_func
67        self.display_name = other.display_name
68        self.extra_arguments.update(other.extra_arguments)
69
70    def to_flat_dict(self) -> dict[str, typing.Any]:
71        """
72        Convert this information to a flat dictionary,
73        where the extra arguments are on the same level as name and move_delay.
74        """
75
76        result = self.extra_arguments.copy()
77
78        result['name'] = self.name
79        result['state_eval_func'] = str(self.state_eval_func)
80        result['display_name'] = self.display_name
81
82        return result

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.

AgentInfo( name: str | chessai.util.reflection.Reference = '', state_eval_func: chessai.util.reflection.Reference | str = 'chessai.chess.gamestate.base_eval', display_name: str = '', extra_arguments: dict[str, typing.Any] | None = None, **kwargs: Any)
21    def __init__(self,
22            name: str | chessai.util.reflection.Reference = '',
23            state_eval_func: chessai.util.reflection.Reference | str = DEFAULT_STATE_EVAL,
24            display_name: str = '',
25            extra_arguments: dict[str, typing.Any] | None = None,
26            **kwargs: typing.Any) -> None:
27        if (isinstance(name, str)):
28            name = chessai.util.reflection.Reference(name)
29
30        self.name: chessai.util.reflection.Reference = name
31        """ The name of the agent's class. """
32
33        self.state_eval_func: chessai.util.reflection.Reference = chessai.util.reflection.Reference(state_eval_func)
34        """ The state evaluation function this agent will use. """
35
36        if (display_name == ''):
37            display_name = str(self.name)
38
39        self.display_name = display_name
40        """ The display name of the agent. """
41
42        self.extra_arguments: dict[str, typing.Any] = {}
43        """
44        Additional arguments to the agent.
45        These are typically used by agent subclasses.
46        """
47
48        self.extra_arguments.update(kwargs)
49        if (extra_arguments is not None):
50            self.extra_arguments.update(extra_arguments)

The name of the agent's class.

The state evaluation function this agent will use.

display_name

The display name of the agent.

extra_arguments: dict[str, typing.Any]

Additional arguments to the agent. These are typically used by agent subclasses.

def set_from_string(self, name: str, value: str) -> None:
52    def set_from_string(self, name: str, value: str) -> None:
53        """ Set an attribute by name. """
54
55        if (name == 'name'):
56            self.name = chessai.util.reflection.Reference(value)
57        elif (name == 'state_eval_func'):
58            self.state_eval_func = chessai.util.reflection.Reference(value)
59        else:
60            self.extra_arguments[name] = value

Set an attribute by name.

def update(self, other: AgentInfo) -> None:
62    def update(self, other: 'AgentInfo') -> None:
63        """ Update this agent info data from the given agent info. """
64
65        self.name = other.name
66        self.state_eval_func = other.state_eval_func
67        self.display_name = other.display_name
68        self.extra_arguments.update(other.extra_arguments)

Update this agent info data from the given agent info.

def to_flat_dict(self) -> dict[str, typing.Any]:
70    def to_flat_dict(self) -> dict[str, typing.Any]:
71        """
72        Convert this information to a flat dictionary,
73        where the extra arguments are on the same level as name and move_delay.
74        """
75
76        result = self.extra_arguments.copy()
77
78        result['name'] = self.name
79        result['state_eval_func'] = str(self.state_eval_func)
80        result['display_name'] = self.display_name
81
82        return result

Convert this information to a flat dictionary, where the extra arguments are on the same level as name and move_delay.