Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: nested_constraints incompatible with TemplateExpression #378

Closed
Moelf opened this issue Dec 8, 2024 · 1 comment · Fixed by #379
Closed

[BUG]: nested_constraints incompatible with TemplateExpression #378

Moelf opened this issue Dec 8, 2024 · 1 comment · Fixed by #379
Assignees
Labels
bug Something isn't working

Comments

@Moelf
Copy link

Moelf commented Dec 8, 2024

What happened?

I think conceptually these two options should work together.

What I want is for log to never be nested inside another log in b(...) or e(...)

atlas_template = TemplateStructure{(:b, :e)}(
     ((; b, e), (x,)) -> b((1-x))^e(x)
)

sr_model2 = SRRegressor(
    niterations=150,
    binary_operators=[+, -, *, ^],
    unary_operators=[log],
    should_optimize_constants=true,
    seed=nothing,
    nested_constraints = [log => [log => 0]],
    expression_type=TemplateExpression,
    expression_options=(; structure=atlas_template),
)

sr_mach2 = machine(sr_model2, X, ys)

fit!(sr_mach2)

Version

1.1.0

Operating System

Linux

Interface

Jupyter Notebook

Relevant log output

nested task error: TaskFailedException
    Stacktrace:
     [1] wait(t::Task)
       @ Base ./task.jl:370
     [2] fetch
       @ ./task.jl:390 [inlined]
     [3] (::SymbolicRegression.var"#56#61"{SymbolicRegression.SearchUtilsModule.SearchState{Float64, Float64, TemplateExpression{Float64, TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, Node{Float64}, ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, @NamedTuple{b::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, e::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}}, @NamedTuple{structure::TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, Task, Channel}, Int64, Int64})()
       @ SymbolicRegression ~/.julia/packages/SymbolicRegression/glgvH/src/SymbolicRegression.jl:816
    
        nested task error: AssertionError: options.should_simplify
        Stacktrace:
          [1] mutate!(tree::TemplateExpression{Float64, TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, Node{Float64}, ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, @NamedTuple{b::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, e::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}}, @NamedTuple{structure::TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, member::PopMember{Float64, Float64, TemplateExpression{Float64, TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, Node{Float64}, ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, @NamedTuple{b::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, e::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}}, @NamedTuple{structure::TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}}, ::Val{:simplify}, ::MutationWeights, options::Options{SymbolicRegression.CoreModule.OptionsStructModule.ComplexityMapping{Int64, Int64}, DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, Node, TemplateExpression, @NamedTuple{structure::TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}}, MutationWeights, false, false, nothing, Nothing, 5}; recorder::Dict{String, Any}, parent_ref::Int64, kws::@Kwargs{temperature::Float64, dataset::Dataset{Float64, Float64, Matrix{Float64}, Vector{Float64}, Nothing, @NamedTuple{}, Nothing, Nothing, Nothing, Nothing}, score::Float64, loss::Float64, curmaxsize::Int64, nfeatures::Int64})
            @ SymbolicRegression.MutateModule ~/.julia/packages/SymbolicRegression/glgvH/src/Mutate.jl:555
          [2] mutate!
            @ ~/.julia/packages/SymbolicRegression/glgvH/src/Mutate.jl:545 [inlined]
          [3] macro expansion
            @ ~/.julia/packages/SymbolicRegression/glgvH/src/Mutate.jl:361 [inlined]
          [4] _dispatch_mutations!(tree::TemplateExpression{Float64, TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, Node{Float64}, ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, @NamedTuple{b::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, e::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}}, @NamedTuple{structure::TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, member::PopMember{Float64, Float64, TemplateExpression{Float64, TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, Node{Float64}, ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, @NamedTuple{b::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}, e::ComposableExpression{Float64, Node{Float64}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}}, @NamedTuple{structure::TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}, operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, variable_names::Nothing}}}, mutation_choice::Symbol, weights::MutationWeights, options::Options{SymbolicRegression.CoreModule.OptionsStructModule.ComplexityMapping{Int64, Int64}, DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{typeof(+), typeof(-), typeof(*), typeof(safe_pow)}, Tuple{typeof(safe_log)}}, Node, TemplateExpression, @NamedTuple{structure::TemplateStructure{(:b, :e), var"#11#12", @NamedTuple{b::Int64, e::Int64}}}, MutationWeights, false, false, nothing, Nothing, 5}; kws::@Kwargs{recorder::Dict{String, Any}, temperature::Float64, dataset::Dataset{Float64, Float64, Matrix{Float64}, Vector{Float64}, Nothing, @NamedTuple{}, Nothing, Nothing, Nothing, Nothing}, score::Float64, loss::Float64, parent_ref::Int64, curmaxsize::Int64, nfeatures::Int64})
            @ SymbolicRegression.MutateModule ~/.julia/packages/SymbolicRegression/glgvH/src/Mutate.jl:346
          [5] _dispatch_mutations!

Extra Info

No response

@Moelf Moelf added the bug Something isn't working label Dec 8, 2024
@MilesCranmer
Copy link
Owner

Thanks. This actually looks like a bug for a different reason –

    if should_simplify === nothing
        should_simplify = (
            loss_function === nothing &&
            nested_constraints === nothing &&
            constraints === nothing &&
            bin_constraints === nothing &&
            una_constraints === nothing
        )
    end

Basically, simplification is turned off if any of these are set.

But then the mutation simplify was called. This doesn't normally happen, because:

    if !options.should_simplify
        weights.simplify = 0.0
    end

However, TemplateExpression overrides the function where that happens, meaning that weights.simplify is never set to 0.0. Oops!

MilesCranmer added a commit that referenced this issue Dec 8, 2024
fix: add missing `condition_mutation_weights!` to fix #378
MilesCranmer added a commit that referenced this issue Dec 8, 2024
[Diff since v1.1.0](v1.1.0...v1.2.0)

**Merged pull requests:**
- fix: add missing `condition_mutation_weights!` to fix #378 (#379) (@MilesCranmer)

**Closed issues:**
- [BUG]: `nested_constraints` incompatible with `TemplateExpression` (#378)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants