Template expressions allow you to constrain the functional form of the search. You define a structure using the @template_spec macro, which tells the algorithm to learn specific sub-expressions (e.g., $f(x_1, x_2) + g(x_2)$).
- The
@template_spec macro defines how components combine. - The
SRRegressor accepts an expression_spec argument. - Individual components of the template can be accessed from the report using
get_contents(best_expr).component_name. - The resulting
TemplateExpression can be evaluated directly: best_expr(X).
using SymbolicRegression
# Define the structure
expression_spec = @template_spec(expressions=(f, g)) do x1, x2, x3
f(x1, x2) + g(x2) - g(x3)
end
model = SRRegressor(
binary_operators=(+, -, *, /),
unary_operators=(cos,),
niterations=500,
maxsize=25,
expression_spec=expression_spec,
)
# ... fit model ...
r = report(mach)
best_expr = r.equations[r.best_idx]
# Access sub-components
println("f: ", get_contents(best_expr).f)
println("g: ", get_contents(best_expr).g)
# Evaluate the whole template
best_expr(randn(3, 20))