Constraints on operators within each function within TemplateExpressionSpec #786
-
I am using the following
I was wondering if there was the possibility of setting a list of allowed operators for each function f,g,h, and eventually separate constraints within each function? Thanks for the great work! The |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Thanks @Andrea-gm. Right now there is no built-in way to do this. But if you are willing to get your hands dirty, you could hack it in? You can overload the function https://github.com/MilesCranmer/SymbolicRegression.jl/blob/433669068acf9d285deacb1dfa7912f739f14112/src/TemplateExpression.jl#L489-L513 for your particular expression type. I guess since this is a very specific constraint, it might just be easier to write up a constraint manually? For example: using SymbolicRegression: TemplateExpression, TemplateStructure, AbstractOptions, compute_complexity, get_contents
using SymbolicRegression.TemplateExpressionModule: has_invalid_variables
# Overload the behavior of check_constraints:
import SymbolicRegression: check_constraints
function check_constraints(
ex::TemplateExpression{T,F},
options::AbstractOptions,
maxsize::Int,
cursize::Union{Int,Nothing}=nothing,
)::Bool where {T,F<:TemplateStructure{(:k, :f, :g, :h)}}
###################################################
# Standard `check_constraints` for this expression type
# https://github.com/MilesCranmer/SymbolicRegression.jl/blob/433669068acf9d285deacb1dfa7912f739f14112/src/TemplateExpression.jl#L489-L513
###################################################
# First, we check the variable constraints at the top level:
if has_invalid_variables(ex)
return false
end
# We also check the combined complexity:
@something(cursize, compute_complexity(ex, options)) > maxsize &&
return false
# Then, we check other constraints for inner expressions:
raw_contents = get_contents(ex)
for t in values(raw_contents)
if !check_constraints(t, options, maxsize, nothing)
return false
end
end
###################################################
# Our custom constraint on "f":
f_tree = raw_contents.f.tree
for node in f_tree # Iterate through nodes and test some condition
if (
node.degree == 2 && # (binary operators)
node.op == 1 # (first operator in your list – we want to prevent this from being found in f!)
)
return false
end
end
return true
end Note that the To run this in Python, just do: from pysr import jl
jl.seval("""
## The Julia code ##
""") |
Beta Was this translation helpful? Give feedback.
Thanks @Andrea-gm. Right now there is no built-in way to do this. But if you are willing to get your hands dirty, you could hack it in? You can overload the function https://github.com/MilesCranmer/SymbolicRegression.jl/blob/433669068acf9d285deacb1dfa7912f739f14112/src/TemplateExpression.jl#L489-L513 for your particular expression type. I guess since this is a very specific constraint, it might just be easier to write up a constraint manually?
For example: