Expression Generator
SRToolkit.utils.expression_generator
Monte-Carlo sampling of symbolic expressions from a grammar or symbol library.
generate_n_expressions
generate_n_expressions(expression_description: Union[str, SymbolLibrary, Grammar], num_expressions: int, unique: bool = True, max_expression_length: int = 50, verbose: bool = True, max_consecutive_generation_failures: int = 100, max_consecutive_uniqueness_failures: int = 200, max_derivation_steps: int = 1000, start: str = 'E') -> List[List[str]]
Sample num_expressions expressions from a grammar or symbol library.
Examples:
>>> len(generate_n_expressions(SymbolLibrary.default_symbols(5), 100, verbose=False))
100
>>> generate_n_expressions(SymbolLibrary.from_symbol_list([], 1), 3, unique=False, verbose=False, max_expression_length=1)
[['X_0'], ['X_0'], ['X_0']]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression_description
|
Union[str, SymbolLibrary, Grammar]
|
Grammar source — one of:
|
required |
num_expressions
|
int
|
Number of expressions to generate. |
required |
unique
|
bool
|
If |
True
|
max_expression_length
|
int
|
Maximum token count per expression. Values ≤ |
50
|
verbose
|
bool
|
Display a progress bar showing total attempts, the ratio of invalid
expressions (derivation failed or exceeded |
True
|
max_consecutive_generation_failures
|
int
|
Maximum number of consecutive attempts that
produce an invalid expression (derivation failed or result exceeded
|
100
|
max_consecutive_uniqueness_failures
|
int
|
Maximum number of consecutive valid expressions
that are already in the output set before stopping early and returning what has
been collected so far. Only relevant when |
200
|
max_derivation_steps
|
int
|
Maximum number of rule applications per single derivation
attempt before it is abandoned. Increase for grammars with deep recursion that
legitimately require many steps. Default |
1000
|
start
|
str
|
Start non-terminal used when |
'E'
|
Returns:
| Type | Description |
|---|---|
List[List[str]]
|
List of expressions, each represented as a list of string tokens in infix notation. |
List[List[str]]
|
May contain fewer than |
List[List[str]]
|
search space is exhausted before the target count is reached. |
Raises:
| Type | Description |
|---|---|
Exception
|
If |
Exception
|
If generation fails |
Warns:
| Type | Description |
|---|---|
UserWarning
|
If more than 80 % of attempts produce an invalid expression (after at
least |
UserWarning
|
If |
Source code in SRToolkit/utils/expression_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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |