Skip to content

How to compute the complexity of a function inside the custom objective function? #380

Answered by MilesCranmer
unary-code asked this question in Q&A
Discussion options

You must be logged in to vote
  1. You can do it with compute_complexity(tree, options)
  2. Yes, see nested_constraints: https://astroautomata.com/PySR/api/#working-with-complexities. If you mean limiting the total number in the expression, you will have to do that manually in the loss function.

For example, if we assume that exp is the 3rd operator in the list of unary operators, this would be:

num_exps = count(node -> node.degree == 1 && node.op == 3, tree)

Then you can return some large number if num_exps > 2. You can also do it like this if this is more readable:

num_exps = 0
for node in tree
    if node.degree == 1 && node.op == 3
        num_exps += 1
    end
end

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@unary-code
Comment options

@MilesCranmer
Comment options

Answer selected by MilesCranmer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
PySR PySR-related discussion SymbolicRegression.jl SymbolicRegression.jl-related discussion
2 participants