en_pyssant.engine module

Various chess engines.

class Engine(position=None, history=None, ruleset=None, **kwargs)[source]

Bases: object

Abstract base class for engine implementations.

Variables:
  • _stop (Event) – An event that is set when thinking should stop.
  • _stopped (Event) – An event that is set when the engine is not thinking.
  • _state_lock (RLock) – A lock for the internal state.
  • _no_one_wants_state (Event) – An event that is set when nobody else wants access to the internal state. The thinking function interrupts thinking until this is set.
  • _thinking_lock (Lock) – A lock that prevents multiple threads from thinking.
Parameters:
  • position (Optional[Position]) – Chess position.
  • history (Optional[Sequence[HistoryRecord]]) – History of plays.
  • ruleset – Game ruleset.
best_move()[source]

Return the move that the engine thinks is best. This probably requires some processing in advance with think_for(). Thinking will not be stopped by calling this method.

Return type:Move
Returns:The best move.
do_move(move, force=False, want_state=False)[source]

Change the engine’s internal state after performing move.

This function does not cause the engine to stop thinking.

want_state is ignored if an exception is raised.

Return the resulting position.

Parameters:
  • move (Move) – Move to perform on position.
  • force (bool) – Whether to perform the move without any verification.
  • want_state (bool) – A flag to keep _no_one_wants_state cleared and _state_lock locked. This is primarily useful for extending this method.
Result:

The resulting position.

Raises:

ValueErrormove is invalid.

Return type:

Position

is_thinking()[source]
Return type:bool
Returns:Whether the engine is currently thinking.
stop_thinking(blocking=True)[source]

Stop the loop in think_for().

Parameters:blocking (bool) – Wait until the loop is well and truly stopped.
Return type:None
think_for(seconds)[source]

Perform analysis of the board for an estimated maximum of seconds. It is perfectly permissible that this function blocks for a lower amount of time, or goes slightly over.

If seconds is 0, block forever until stop_thinking() is called.

This method always returns True after it has finished thinking, whether prematurely stopped or not. However, if it is called while it is already thinking, then it will return False.

Parameters:seconds (float) – Amount of seconds to process.
Result:Whether the function was successfully able to start.
Return type:bool
history

Current history.

Return type:Sequence[HistoryRecord]
position

Current position.

Return type:Position
ruleset

The ruleset. This is not editable.

class MCTSEngine(position=None, history=None, ruleset=None, processes=None, **kwargs)[source]

Bases: en_pyssant.engine.ParallelEngine

An Engine implementation that uses Monte Carlo Tree Search.

best_move()[source]

Return the move that the engine thinks is best. This probably requires some processing in advance with think_for(). Thinking will not be stopped by calling this method.

Return type:Move
Returns:The best move.
do_move(move, force=False, want_state=False)[source]

Change the engine’s internal state after performing move.

This function does not cause the engine to stop thinking.

want_state is ignored if an exception is raised.

Return the resulting position.

Parameters:
  • move (Move) – Move to perform on position.
  • force (bool) – Whether to perform the move without any verification.
  • want_state (bool) – A flag to keep _no_one_wants_state cleared and _state_lock locked. This is primarily useful for extending this method.
Result:

The resulting position.

Raises:

ValueErrormove is invalid.

Return type:

Position

load_precomputed(source=None)[source]

Load a precomputed state from source. If source is None, load from pickle included with the distribution.

Parameters:source (Optional[Binaryio]) – A binary stream of a pickled _Node.
Raises:FileNotFoundError – There was no included pickle.
Return type:None
think_for(seconds)[source]

Perform analysis of the board for an estimated maximum of seconds. It is perfectly permissible that this function blocks for a lower amount of time, or goes slightly over.

If seconds is 0, block forever until stop_thinking() is called.

This method always returns True after it has finished thinking, whether prematurely stopped or not. However, if it is called while it is already thinking, then it will return False.

Parameters:seconds (float) – Amount of seconds to process.
Result:Whether the function was successfully able to start.
Return type:bool
class ParallelEngine(position=None, history=None, ruleset=None, processes=None, **kwargs)[source]

Bases: en_pyssant.engine.Engine

Abstract base class for engines that do parallel execution with the multiprocessing module.

Variables:
  • _pool (Pool or None) – A pool of processes.
  • _pool_lock (Lock) – A lock for access to the pool.
Parameters:
  • position (Optional[Position]) – Chess position.
  • history (Optional[Sequence[HistoryRecord]]) – History of plays.
  • ruleset – Game ruleset.
  • processes (Optional[int]) – Amount of processes to spawn. Defaults to the amount of cores. If equal to 0, do not spawn any processes.
processes

The amount of processes spawned by this engine. This can be changed on-the-fly.

Return type:int
class RandomEngine(position=None, history=None, ruleset=None, **kwargs)[source]

Bases: en_pyssant.engine.Engine

An Engine implementation that returns random moves. The purpose of this engine is solely to prototype.

best_move()[source]

Return the move that the engine thinks is best. This probably requires some processing in advance with think_for(). Thinking will not be stopped by calling this method.

Return type:Move
Returns:The best move.
think_for(seconds)[source]

Perform analysis of the board for an estimated maximum of seconds. It is perfectly permissible that this function blocks for a lower amount of time, or goes slightly over.

If seconds is 0, block forever until stop_thinking() is called.

This method always returns True after it has finished thinking, whether prematurely stopped or not. However, if it is called while it is already thinking, then it will return False.

Parameters:seconds (float) – Amount of seconds to process.
Result:Whether the function was successfully able to start.
Return type:bool