MilesCranmer / SymbolicRegression.jl

Distributed High-Performance Symbolic Regression in Julia
https://astroautomata.com/SymbolicRegression.jl/dev/
Apache License 2.0
576 stars 71 forks source link

[Feature]: Using custom dimensions using `DynamicQuantities` #335

Open HenryDane opened 1 month ago

HenryDane commented 1 month ago

Feature Request

(Originally a discussion post on the PySR github repository).

I think that it would be useful to be able to use custom AbstractDimensions with this library.

An example of this usecase could be:


# create our dimensions
struct ExtendedDimensions{R} <: AbstractDimensions{R}
    length::R
    mass::R
    time::R
    current::R
    temperature::R
    luminosity::R
    amount::R
    angle::R
    mass_h2o::R
    mass_air::R
    count::R
end

# ... 

const mmr   = Quantity(1.0, ExtendedDimensions(mass_h2o=1, mass_air=-1))
const nconc = Quantity(1.0, ExtendedDimensions(count=1, mass_air=-1))
const dens  = Quantity(1.0, ExtendedDimensions(mass_air=1, length=-3))

X = (; A=data.A * mmr, B=data.B * nconc)
y = data.C * mmr

# create model
model = SRRegressor(
    binary_operators=[*,^,/],
    dimensional_constraint_penalty=10^5,
    nested_constraints=[(^) => [(^) => 0]],
    constraints=[(^) => (-1,1)]
)

Please let me know if there is anything I can do to help with this feature.

MilesCranmer commented 1 month ago

Just copying my comment for anybody else reading this:

as a quick hack, you could treat the other existing units as the units you are interested in. So, e.g., luminosity (cd) could be mass_h20.

But would be nice to allow actual custom dimensions.

MilesCranmer commented 1 month ago

Here's a reduced test that fails:

using SymbolicRegression.InterfaceDynamicQuantitiesModule: get_units, get_si_units
using DynamicQuantities

struct AngleDimensions{R} <: AbstractDimensions{R}
    length::R
    mass::R
    time::R
    current::R
    temperature::R
    luminosity::R
    amount::R
    rad::R
end

const rad = Quantity(1.0, AngleDimensions(; rad=1))

get_si_units(Float64, [dimension(y)])

Basically this code: https://github.com/MilesCranmer/SymbolicRegression.jl/blob/cd23a6e25c64d00565c3ae3905d06dc3c63033ed/src/InterfaceDynamicQuantities.jl#L16-L57 assumes Dimensions is the only AbstractDimensions for units.