Skip to content

PolytopeNoise (or Polytope) is not deterministic #6

@Warggr

Description

@Warggr

I wanted to get deterministic noise across runs. I tried the following:

import numpy as np
from ampyc.noise import PolytopeNoise
from ampyc.utils import Polytope

def get_unit_cube_polytope(noise_dim):
    A = np.zeros((2 * noise_dim, noise_dim))
    for i in range(noise_dim):
        A[2 * i, i] = 1
        A[2 * i + 1, i] = -1
    b = np.ones((2 * noise_dim,))
    return Polytope(A, b)

noise = PolytopeNoise(get_unit_cube_polytope(6))
noise.seed(5)
w = noise.generate()

However, this is not deterministic, as can be seen with this:

noise2 = PolytopeNoise(get_unit_cube_polytope(6))
noise2.seed(5)
w2 = noise2.generate()
assert not np.all(w == w2)

This is because Polytope.__init__() indirectly depends on np.random.rand() (via Polytope.__init__ > extreme > quickhull).
So after Polytope.__init__(), the order of vertices in self.V is non-deterministically random. But PolytopeNoise relies on the order of vertices in self.V. I find this counter-intuitive (I would have expected the above code to work).

Could you please do one of the following:

  • Either document PolytopeNoise or Polytope with something like "To get deterministic behavior, call np.random.seed(0) before instantiating Polytope".
  • Or change PolytopeNoise so that it uses a deterministic order of the polytope's vertices.

I'll create a pull request for the 2nd option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions