dynadojo.systems.santi.NBodySystem#

class dynadojo.systems.santi.NBodySystem#

Bases: SimpleSystem

N-Body system.

Note

We implement a very simple version of the n-body problem. We assume a system with \(n\) planetary bodies and 1 sun. All planetary bodies have the same mass. The initial conditions are a matrix with 4 dimensions for every planetary body. These dimensions give us the \((x, y, z, vy)\) of the planetary body where the first three coordinates are the \(xyz\)-position and the last coordinate \(vy\) is the \(y\) velocity. For simplicity, this is the only component of velocity that you can specify. So if we wanted 5 planetary bodies, then our latent dimension would be \(5 \times 4 = 20\).

Example

>>> from dynadojo.wrappers import SystemChecker
>>> n_bodies = 3
>>> latent_dim = 4 * n_bodies
>>> embed_dim = latent_dim
>>> n = 1  # we only want 1 system with several bodies
>>> timesteps = 10
>>> system = SystemChecker(NBodySystem(latent_dim, embed_dim, plot_data=True, seed=0))
>>> x0 = system.make_init_conds(n)
>>> x = system.make_data(x0, timesteps=timesteps)
../_images/santi.png
>>> from dynadojo.challenges import FixedComplexity
>>> from dynadojo.baselines.dnn import DNN
>>> challenge = FixedComplexity(l=4, e=None, t=10, N=[3, 5, 10], reps=3, system_cls=NBodySystem, test_examples=1, test_timesteps=5)
>>> data = challenge.evaluate(algo_cls=DNN)
>>> challenge.plot(data)
../_images/nbody_fixed_complexity.png

Methods

__init__([latent_dim, embed_dim, mass, ...])

Initialize the class.

calc_control_cost(control)

Calculates the L2 norm / dimension of every vector in the control

calc_dynamics(t, x)

Calculates the dynamics for the system.

calc_error(x, y)

Returns the MSE error normalized by the embedded dimension.

make_data(init_conds, control, timesteps[, ...])

Uses the calc_dynamics() method to generate data.

make_init_conds(n[, in_dist])

Uniformly samples embedded-dimensional points from an inside or outside distribution

Attributes

controller

The controller matrix.

embed_dim

The embedded dimension for the system.

embedder

The embedder matrix.

latent_dim

The latent dimension for the system.

seed

The random seed for the system.

__init__(latent_dim=4, embed_dim=4, mass=0.01, plot_data=False, IND_range=(-1, 1), OOD_range=(-1, 1), **kwargs)#

Initialize the class.

Parameters:
  • latent_dim (int) – The latent dimension should be \(4 \times \text{# of desired planetary bodies}\)

  • embed_dim (int) – Must be the same as the latent dimension.

  • mass (float) – The mass for all planetary bodies. In practice, small values between 0 and 1 will work best.

  • plot_data (bool) – If True, visualize the results of the system in a Jupyter notebook. Defaults to False.

calc_control_cost(control)#

Calculates the L2 norm / dimension of every vector in the control

Parameters:

control (ndarray) –

Return type:

float

calc_dynamics(t, x)#

Calculates the dynamics for the system. Your class must implement this.

calc_error(x, y)#

Returns the MSE error normalized by the embedded dimension.

Return type:

float

property controller#

The controller matrix. For example, in a system \(\dot{x} = Ax + Bu\), the controller is \(B\).

property embed_dim#

The embedded dimension for the system.

property embedder#

The embedder matrix. An invertible map from the latent space to the embedding space.

property latent_dim#

The latent dimension for the system.

make_data(init_conds, control, timesteps, noisy=False)#

Uses the calc_dynamics() method to generate data. Mathematically, data is generated like \(\dot{x} = f(x) + Bu\). Where \(f(x)\) is given by calc_dynamics().

Parameters:
  • init_conds (numpy.ndarray) – (n, embed_dim) Initial conditions matrix.

  • control (numpy.ndarray) – (n, timesteps, embed_dim) Controls tensor.

  • timesteps (int) – Timesteps per training trajectory (per action horizon).

  • noisy (bool, optional) – If True, add noise to trajectories. Defaults to False. If False, no noise is added.

Returns:

(n, timesteps, embed_dim) Trajectories tensor.

Return type:

numpy.ndarray

make_init_conds(n, in_dist=True)#

Uniformly samples embedded-dimensional points from an inside or outside distribution

Note

Systems developers determine what counts as in vs out-of-distribution. DynaDojo doesn’t provide any verification that this distinction makes sense or even exists. See LDSystem for a principled example.

Parameters:
  • n (int) – Number of initial conditions.

  • in_dist (bool, optional) – If True, generate in-distribution initial conditions. Defaults to True. If False, generate out-of-distribution initial conditions.

Returns:

(n, embed_dim) Initial conditions matrix.

Return type:

numpy.ndarray

property seed#

The random seed for the system.