Skip to content

fix: use eltype(x0) instead of T for randn in constant optimization (backport to release-v1)#571

Merged
MilesCranmer merged 3 commits intoastroautomata:release-v1from
MilesCranmerBot:backport/issue-568-release-v1
Mar 23, 2026
Merged

fix: use eltype(x0) instead of T for randn in constant optimization (backport to release-v1)#571
MilesCranmer merged 3 commits intoastroautomata:release-v1from
MilesCranmerBot:backport/issue-568-release-v1

Conversation

@MilesCranmerBot
Copy link
Contributor

Backport of #570 to release-v1

Fixes #568

Bug Description

When using custom types (e.g., AbstractVector{<:Number}) with constant optimization and optimizer_nrestarts > 0, the code incorrectly used T (the custom type from the PopMember type parameter) instead of the scalar element type when calling randn(). This caused a MethodError because randn() only works with scalar types like Float64, not custom types.

Fix

Changed src/ConstantOptimization.jl to use eltype(x0) instead of T in the restart loop:

  • x0 is always a Vector{<:Number} (the scalar constants extracted from the expression)
  • eltype(x0) returns the scalar type (e.g., Float64)
  • T could be a custom type like MyVec2 <: AbstractVector{Float64}

Changes

  • Fixed _optimize_constants to use eltype(x0) for randn() and type conversions
  • Added regression test in test/unit/evolution-core/test_custom_type_constant_optimization.jl

@codecov
Copy link

codecov bot commented Feb 16, 2026

Codecov Report

❌ Patch coverage is 62.50000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.65%. Comparing base (210deac) to head (9b6e5bf).

Files with missing lines Patch % Lines
src/Configure.jl 20.00% 4 Missing ⚠️
src/Utils.jl 40.00% 3 Missing ⚠️
src/OptionsStruct.jl 0.00% 2 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           release-v1     #571      +/-   ##
==============================================
- Coverage       92.67%   92.65%   -0.03%     
==============================================
  Files              41       41              
  Lines            3660     3662       +2     
==============================================
+ Hits             3392     3393       +1     
- Misses            268      269       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MilesCranmerBot MilesCranmerBot force-pushed the backport/issue-568-release-v1 branch 3 times, most recently from 9b6e5bf to 0516756 Compare February 16, 2026 08:47
…backport to release-v1)

Issue astroautomata#568: When using custom types with constant optimization and
optimizer_nrestarts > 0, the code incorrectly used T (the custom type)
instead of the scalar element type when calling randn(). This caused a
MethodError because randn() only works with scalar types.

The fix uses eltype(x0) which returns the scalar type (e.g., Float64)
rather than T which could be a custom type like AbstractVector.

Also adds a regression test to prevent this bug from recurring.

Fixes astroautomata#568
@MilesCranmerBot MilesCranmerBot force-pushed the backport/issue-568-release-v1 branch from 0516756 to 784db51 Compare February 16, 2026 08:55
@nic-barbara
Copy link

Hey @MilesCranmer, is this PR likely to be merged any time soon? I noticed that the PR for the current version (not the backport) was merged a little while ago: #570

@MilesCranmer
Copy link
Collaborator

Hey, sorry, I just forgot about it (teaching this term so twice as busy as normal).

@MilesCranmerBot can you figure out why the CI is failing and fix it? Maybe merge in the latest changes from release-v1 as well.

@nic-barbara
Copy link

Thank you!

@codecov
Copy link

codecov bot commented Mar 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@nic-barbara
Copy link

Hi @MilesCranmer just following up again, looks like all checks have passed here so this one should be good to go?

@MilesCranmer
Copy link
Collaborator

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. 👍

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@MilesCranmer
Copy link
Collaborator

sorry for the delay!

@MilesCranmer MilesCranmer merged commit 13ca9c4 into astroautomata:release-v1 Mar 23, 2026
49 checks passed
github-actions bot referenced this pull request Mar 23, 2026
…release-v1--components--SymbolicRegression

chore(release-v1): release 1.13.1
@nic-barbara
Copy link

No worries, thanks for all your work on this package!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants