Skip to content

How to constrain all the constants to be positive #449

Answered by MilesCranmer
HouJayson asked this question in Q&A
Discussion options

You must be logged in to vote

You could do this with a custom objective function, like:

function eval_loss(tree, dataset::Dataset{T,L}, options)::L where {T,L}
    # See https://astroautomata.com/SymbolicRegression.jl/dev/types/#DynamicExpressions.EquationModule.Node
    is_negative_constant(node) = node.degree == 0 && node.constant && node.val::T < 0
    # (The ::T part is not required, but it just speeds it up as then Julia knows it isn't `nothing`)

    # Will walk through tree and count number of times this function is true
    num_negative_constants = count(is_negative_constant, tree)
    #  (Tree utilities are defined here: https://github.com/SymbolicML/DynamicExpressions.jl/blob/master/src/base.jl,
    #  and l…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@HouJayson
Comment options

@frank-ccc
Comment options

@MilesCranmer
Comment options

Answer selected by HouJayson
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
3 participants