cayleypy.Predictor

class cayleypy.Predictor(graph: CayleyGraph, models_or_heuristics)[source]

Estimates distance from central state to given states.

__init__(graph: CayleyGraph, models_or_heuristics)[source]

Initializes Predictor.

Parameters:
  • graph – Associated CayleyGraph object.

  • models_or_heuristics –

    One of the following:

    • ”zero” - will use predictor that returns 0 for any state.

    • ”hamming” - will use Hamming distance from central state.

    • torch.nn.Module - will use given neural network model.

    • Any object that has “predict” method (e.g. sklearn models).

    • Any callable object.

    A model estimating distances for all children of a state at once (Q-model) must have as many outputs as there are generators in graph, and must declare their number in attribute n_outputs - then score_children() will call it once per state instead of once per child. Models built by cayleypy.models.ModelConfig.build_model() declare it automatically.

Methods

__init__(graph, models_or_heuristics)

Initializes Predictor.

predict_batched(states)

Applies the underlying model to states, splitting them into batches if there are too many.

pretrained(graph)

Loads pre-trained predictor for this graph.

score_children(states)

Estimates distances from central state to all children (neighbors) of given states.

predict_batched(states: Tensor) → Tensor[source]

Applies the underlying model to states, splitting them into batches if there are too many.

The shape of the output is the shape the model returns: [n_states] for usual (single-output) models, and [n_states, n_outputs] for multi-output models (e.g. models predicting one score per generator). Output of a model that does not return tensors is converted to one.

Parameters:

states – States (in decoded representation) to apply the model to.

Returns:

Output of the model for states.

static pretrained(graph: CayleyGraph)[source]

Loads pre-trained predictor for this graph.

score_children(states: Tensor) → Tensor[source]

Estimates distances from central state to all children (neighbors) of given states.

Children are enumerated in the order of generators: element [i, j] of the answer is the estimated distance for the state obtained by applying generator j to states[i].

For a model having one output per generator (Q-model, i.e. n_outputs == n_generators), the answer is the output of the model applied to states, so one model evaluation per state is needed.

Otherwise (for a single-output model), the model is called for every child, which needs n_generators times more model evaluations than __call__().

Parameters:

states – States (in decoded representation) whose children to score.

Returns:

Tensor of shape [n_states, n_generators] with estimated distances for children.