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

control PSRN's operators via SR.jl Options #21

Open
Tracked by #7
x66ccff opened this issue Jan 26, 2025 · 4 comments
Open
Tracked by #7

control PSRN's operators via SR.jl Options #21

x66ccff opened this issue Jan 26, 2025 · 4 comments

Comments

@x66ccff
Copy link
Owner

x66ccff commented Jan 26, 2025

No description provided.

@x66ccff x66ccff mentioned this issue Jan 26, 2025
12 tasks
@x66ccff
Copy link
Owner Author

x66ccff commented Jan 26, 2025

Currently, the operators for PSRN are directly set in the source code. I hope to be able to set them directly through the options in SR.jl, but I haven't decided yet whether to adapt automatically or set them separately.

@x66ccff
Copy link
Owner Author

x66ccff commented Jan 26, 2025

@MilesCranmer Any thoughts on this?

if options.populations > 0 # TODO I don' know how to add a option for control whether use PSRN or not, cause Option too complex for me ...
println("Use PSRN")
# N_PSRN_INPUT = 3
N_PSRN_INPUT = 15 # TODO this can be tuned
psrn_manager = PSRNManager(;
N_PSRN_INPUT=N_PSRN_INPUT, # these operators must be the subset of options.operators
operators=["Add", "Mul", "Sub", "Div", "Identity", "Cos", "Sin", "Exp", "Log"], # TODO maybe we can place this in options
# operators=["Add", "Mul", "Sub", "Div", "Identity"], # TODO maybe we can place this in options
# operators=["Add", "Mul", "Neg", "Inv", "Identity", "Cos", "Sin", "Exp", "Log"], # TODO maybe we can place this in options
# operators = ["Sub", "Div", "Identity", "Cos", "Sin", "Exp", "Log"],
# operators = ["Sub", "Div", "Identity"],
# operators = ["Add", "Mul", "Neg", "Inv", "Identity"],
n_symbol_layers=2, # TODO if use 3 layer, easily crash (segfault), don't know why
options=options,
max_samples=20,
# max_samples = 10
)
else
println("Not use PSRN")
end

# Operator dictionary
const OPERATORS = Dict{String,Operator}(
"Identity" => UnaryOperator("Identity", identity_kernel!, true, identity),
"Sin" => UnaryOperator("Sin", sin_kernel!, true, sin),
"Cos" => UnaryOperator("Cos", cos_kernel!, true, cos),
"Exp" => UnaryOperator("Exp", exp_kernel!, true, exp),
"Log" => UnaryOperator("Log", log_kernel!, true, safe_log),
# "Add" => BinaryOperator("Add", add_kernel!, false, +),
# "Mul" => BinaryOperator("Mul", mul_kernel!, false, *),
"Add" => BinaryOperator("Add", add_kernel!, true, +),
"Mul" => BinaryOperator("Mul", mul_kernel!, true, *),
"Div" => BinaryOperator("Div", div_kernel!, true, /),
"Sub" => BinaryOperator("Sub", sub_kernel!, true, -),
"Inv" => UnaryOperator("Inv", inv_kernel!, true, x -> 1 / x),
"Neg" => UnaryOperator("Neg", neg_kernel!, true, x -> 0 - x),
)

@MilesCranmer
Copy link

Why are the strings needed? Is it to pass it back to Python?

If you were to do things from scratch I would probably just write a single kernel like

struct Kernel{F<:Function}
    f::F
end

(k::Kernel)(x, y) = k.f(x, y)
#= or extra functions with ::Kernel as an argument =#

and then define mappings like

to_string(::Base.Fix{typeof(+)}) = "Add"
to_string(::Base.Fix{typeof(*)}) = "Mul"
to_string(::Base.Fix{typeof(/)}) = "Div"

and so on

@MilesCranmer
Copy link

p.s., instead of 1 / x you should use Base.inv and instead of 0 - x you can just use - by itself

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

No branches or pull requests

2 participants