Skip to content

Sample

sample_tree_raw_from_priors(max_num_constants=0, max_num_variables=1, feature_priors=None, function_priors=DEFAULT_FUNCTION_SPACE, operator_priors=DEFAULT_OPERATOR_SPACE, structure_priors={})

Sample a tree from priors, simplify and check if valid tree

Source code in src/equation_tree/sample.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
def sample_tree_raw_from_priors(
        max_num_constants: int = 0,
        max_num_variables: int = 1,
        feature_priors: Optional[Dict] = None,
        function_priors: PriorType = DEFAULT_FUNCTION_SPACE,
        operator_priors: PriorType = DEFAULT_OPERATOR_SPACE,
        structure_priors: PriorType = {},
):
    """
    Sample a tree from priors, simplify and check if valid tree
    """
    # Assertions
    if max_num_constants + max_num_variables < 1:
        raise Exception("Can not sample tree without leafs")

    # Get feature priors
    if feature_priors is not None:
        for key in feature_priors:
            if not (is_variable_formatted(key) or is_constant_formatted(key)):
                raise Exception(
                    "Use standard formats for feature priors: x_{int} for variables, "
                    "c_{int} for constants."
                )
        _feature_priors = feature_priors.copy()
    else:
        _feature_space = [f"x_{i}" for i in range(1, max_num_variables + 1)] + [
            f"c_{i}" for i in range(1, max_num_constants + 1)
        ]
        _feature_priors = priors_from_space(_feature_space)

    # Convert priors if space is given
    if isinstance(function_priors, List):
        _function_priors = priors_from_space(function_priors)
    else:
        _function_priors = function_priors.copy()

    if isinstance(operator_priors, List):
        _operator_priors = priors_from_space(operator_priors)
    else:
        _operator_priors = operator_priors.copy()

    if isinstance(structure_priors, List):
        _structure_priors = priors_from_space(structure_priors)
    else:
        _structure_priors = structure_priors.copy()

    # Create tree
    equation_tree = EquationTree.from_priors(
        feature_priors=_feature_priors,
        function_priors=_function_priors,
        operator_priors=_operator_priors,
        structure_priors=_structure_priors,
    )

    # Check if tree is valid
    if not equation_tree.check_validity():
        return None

    equation_tree.simplify(
        function_test=lambda x: x in _function_priors.keys(),
        operator_test=lambda x: x in _operator_priors.keys(),
    )

    # Check is nan
    if equation_tree.is_nan:
        return None

    # Check if duplicate constants
    if (
            equation_tree.n_non_numeric_constants
            > equation_tree.n_non_numeric_constants_unique
    ):
        return None

    # Check if more constants than max:
    if equation_tree.n_constants > max_num_constants:
        return None

    # Check if more variables than max:
    if equation_tree.n_variables > max_num_variables:
        return None

    if not equation_tree.check_validity():
        return None

    if not equation_tree.check_possible(
            _feature_priors, _function_priors, _operator_priors, _structure_priors
    ):
        return None

    equation_tree.get_evaluation()
    if not equation_tree.has_valid_value:
        return None

    return equation_tree