Table of Contents

Class Graph

Namespace
Gym.Spaces
Assembly
Gym.dll

Graphs whose nodes and edges carry features from two spaces: every element is a GraphInstance of node features, edge features, and the (m, 2) links saying which two nodes each edge joins. The number of nodes and edges varies per sample.

public class Graph : Space<GraphInstance>, IDisposable, IEquatable<Graph>
Inheritance
Graph
Implements
Inherited Members
Extension Methods

Remarks

A sample draws its node and edge features from batches of the subspaces built for that sample, from copies of the subspaces' generators; each subspace's own generator then advances by one random() draw so the next sample differs. The links come from the graph's own generator.

The space owns its subspaces. Reference: gymnasium/spaces/graph.py.

Constructors

Graph(Space, Space?, SpaceSeed?)

Graphs with features from nodeSpace and edgeSpace (Gymnasium's Graph(node_space, edge_space, seed=seed)).

public Graph(Space nodeSpace, Space? edgeSpace, SpaceSeed? seed = null)

Parameters

nodeSpace Space

The space of one node's features, usually a Box or Discrete; owned by the graph from now on.

edgeSpace Space

The space of one edge's features, or null for graphs without edge features (and so without edges).

seed SpaceSeed?

An integer seed (it seeds the graph's generator, which draws the subspaces' seeds, then is seeded again with the same value), a generator to share, or null.

Remarks

Building the batched subspaces reads the subspaces' generators, creating them from entropy when they were not seeded, as Gymnasium's batch_space does.

Exceptions

TypeError

Thrown for a null node space, with batch_space's message as in Gymnasium.

Error

Thrown when an integer seed is negative.

Properties

BatchEdgeSpace

The edge space batched once (Gymnasium's batch_edge_space), or null without an edge space.

public Space? BatchEdgeSpace { get; }

Property Value

Space

BatchNodeSpace

The node space batched once (Gymnasium's batch_node_space), for membership and JSON.

public Space BatchNodeSpace { get; }

Property Value

Space

EdgeSpace

The space of one edge's features (Gymnasium's edge_space), or null.

public Space? EdgeSpace { get; }

Property Value

Space

IsNpFlattenable

A graph never flattens to a box: its size varies.

public override bool IsNpFlattenable { get; }

Property Value

bool

NodeSpace

The space of one node's features (Gymnasium's node_space).

public Space NodeSpace { get; }

Property Value

Space

Methods

Contains(object?)

Whether x is an element (Gymnasium's contains): every node in the node space and, when the graph has edges, every edge in the edge space with integer links of shape (m, 2) that point at existing nodes; a graph without edges must have neither edges nor links.

public override bool Contains(object? x)

Parameters

x object

The candidate.

Returns

bool

true for an element.

Exceptions

TypeError

Thrown, as Gymnasium's does, when the nodes or edges cannot be iterated as the batched subspace's elements.

Dispose(bool)

Releases what a derived space owns (a Box's bounds); called once, by Dispose().

protected override void Dispose(bool disposing)

Parameters

disposing bool

true from Dispose(); no space has a finalizer.

Equals(Graph?)

Gymnasium's ==: equal node spaces and equal (or both absent) edge spaces.

public bool Equals(Graph? other)

Parameters

other Graph

The other space.

Returns

bool

true when equal.

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

FromJsonable(object)

Converts Gymnasium's JSON dicts back to graphs (Gymnasium's from_jsonable); links become int32 arrays.

public override IList<GraphInstance> FromJsonable(object jsonable)

Parameters

jsonable object

The list of dicts.

Returns

IList<GraphInstance>

The graphs, owned by the caller.

Exceptions

ArgumentException

Thrown when an entry is not a dict with "nodes".

AssertionError

Thrown for an entry with edges in a graph without an edge space (Gymnasium's bare assert).

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

Sample(object?, object?)

Draws a graph of 10 nodes and a random number of edges (Gymnasium's sample() defaults).

public override GraphInstance Sample(object? mask = null, object? probability = null)

Parameters

mask object

null, or a pair (node_mask, edge_mask); see Sample(object?, object?, long, long?).

probability object

null, or a pair (node_probability, edge_probability).

Returns

GraphInstance

The graph; the caller owns it and its arrays.

Exceptions

ValueError

Thrown when both masks are given.

Sample(object?, object?, long, long?)

Draws a graph (Gymnasium's sample): numNodes node features, the edges (a random count below n·(n-1) unless numEdges fixes it) and their links.

public GraphInstance Sample(object? mask = null, object? probability = null, long numNodes = 10, long? numEdges = null)

Parameters

mask object

null, or a pair (node_mask, edge_mask) (an object?[], a tuple or a list of two): the node mask goes to the batched node space; the edge mask to the batched edge space, repeated once per edge when the edge count is drawn. Box subspaces take no mask.

probability object

null, or the same pair of probability masks.

numNodes long

The number of nodes, at least 1 (Gymnasium's num_nodes, default 10).

numEdges long?

The number of edges, or null to draw it (Gymnasium's num_edges).

Returns

GraphInstance

The graph; the caller owns it and its arrays. Its edges and links are null when there is no edge space or no edge.

Remarks

A fixed numEdges with no edge space only warns, as in Gymnasium, and the graph has no edges.

Exceptions

AssertionError

Thrown for numNodes below 1 or a negative numEdges, with Gymnasium's messages.

ValueError

Thrown when both masks are given, or a mask is not a pair (Python's unpacking error).

TypeError

Thrown for a mask that cannot be unpacked at all.

SeedCore(BigInteger?)

Gymnasium's Graph.seed for an integer or null: null seeds the graph and its subspaces from entropy; an integer seeds the graph's generator, draws the subspaces' seeds (one, or two with an edge space, below int32's maximum), and seeds the generator again with the same value.

protected override object SeedCore(BigInteger? seed)

Parameters

seed BigInteger?

The seed, or null.

Returns

object

The graph's seed followed by the node space's (and the edge space's), as a list.

Exceptions

Error

Thrown for a negative seed.

SeedListCore(IReadOnlyList<object?>)

Gymnasium's Graph.seed for a list: the graph's seed, the node space's, and the edge space's when there is one.

protected override object SeedListCore(IReadOnlyList<object?> seeds)

Parameters

seeds IReadOnlyList<object>

Two seeds, or three with an edge space.

Returns

object

The seeds used.

Exceptions

ValueError

Thrown for a list of the wrong length, with Gymnasium's messages.

Error

Thrown when the graph's own seed is negative or not a Python int.

SeedTypeError(object?)

Gymnasium's Graph.seed error for a seed that is not an integer, a list, a tuple or null.

protected override Exception SeedTypeError(object? seed)

Parameters

seed object

The refused seed.

Returns

Exception

TypeError: Expects None, int or tuple of ints, actual type: <class 'dict'>.

ToJsonable(IEnumerable<GraphInstance>)

Converts samples to Gymnasium's JSON form: one dict per sample with "nodes" (the batched node space's JSON of the one sample) and, when the sample has edges, "edges" and "edge_links" (nested lists).

public override object ToJsonable(IEnumerable<GraphInstance> samples)

Parameters

samples IEnumerable<GraphInstance>

Samples of this space.

Returns

object

The list of dicts.

Exceptions

NullReferenceException

Thrown for a sample with edges in a graph without an edge space, as Gymnasium fails on None.

ToString()

Gymnasium's repr: Graph(Box(-1.0, 1.0, (3,), float32), Discrete(3)), the edge space None when absent.

public override string ToString()

Returns

string

The text.