Skip to content

stochastic_generator

StochasticGenerator(domains, *, test_case_count=None, seed=0)

Bases: IncrementalEnvironment

A generator that produces random tests based on given domains.

Initialize the stochastic generator.

Parameters:

Name Type Description Default
domains list[Domain]

A list of domains to generate random values for. Each domain must be associated with exactly one input feature of the model that shall be tested.

required
test_case_count int | None

The number of test cases to generate. If None, the generator will run indefinitely.

None
seed int

The seed for the random number generator. The default is 0, which means a random seed will be used.

0
Source code in src/flowcean/core/tool/testing/generator/stochastic_generator.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def __init__(
    self,
    domains: list[Domain],
    *,
    test_case_count: int | None = None,
    seed: int = 0,
) -> None:
    """Initialize the stochastic generator.

    Args:
        domains: A list of domains to generate random values for.
            Each domain must be associated with exactly one input
            feature of the model that shall be tested.
        test_case_count: The number of test cases to generate. If None,
            the generator will run indefinitely.
        seed: The seed for the random number generator. The default is 0,
            which means a random seed will be used.
    """
    super().__init__()

    # Check if no duplicate feature names are present
    feature_names_count: dict[str, int] = {}
    for domain in domains:
        feature_names_count[domain.feature_name] = (
            feature_names_count.get(
                domain.feature_name,
                0,
            )
            + 1
        )
    if len(feature_names_count) != len(domains):
        msg = "Duplicate features found: "
        msg += ", ".join(
            f"{feature_name}: {count}"
            for feature_name, count in feature_names_count.items()
            if count > 1
        )
        raise ValueError(msg)

    # Seed all domains
    rng = random.Random(seed) if seed != 0 else random.Random()
    for domain in domains:
        domain.set_seed(rng.randint(0, 2**32 - 1))

    self.domains = domains
    self.count = 0
    self.number_test_cases = test_case_count
    # Perform the first step to initialize the generator
    self.step()