E-IG0005 — Unbounded dictionary keys in random generation
Error Type |
|
|---|---|
Phase |
generating random instance data |
Python exception |
|
Message
Cannot generate random data for a dictionary whose key type has unboundedly many values, such as `int` or `str`.
Possible fix: do one of the following:
- use a key type with finitely many values, such as a natural-number expression or a category label
- for `str` keys, list the keys to generate with the `keys` option of this placeholder
- prepare the instance data for this placeholder yourself
Cause
A partial dictionary placeholder, or a placeholder whose elements are partial dictionaries, has a key type with unboundedly many values, such as int or str. To generate a dictionary, the generator picks its keys from the list of all allowed keys, and no such list exists for these key types. For str keys, the keys option of the placeholder supplies that list instead.
Fix
Declare the keys with a type that has finitely many values, such as a natural-number expression N (the keys 0 to N - 1) or a category label:
import jijmodeling as jm
problem = jm.Problem("example")
N = problem.Natural("N")
weights = problem.Float("weights", dict_keys=N, partial_dict=True)
data = problem.generate_random_dataset(options={"N": {"value": range(3, 6)}})
For str keys, list the keys to generate with the keys option:
problem = jm.Problem("example")
prices = problem.Float("prices", dict_keys=str, partial_dict=True)
data = problem.generate_random_dataset(options={"prices": {"keys": ["apple", "pear"]}})
Otherwise, prepare the instance data for this placeholder yourself instead of generating it.