### Symbolic Regression Toy Example: Simple Search Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Demonstrates a basic symbolic regression search using the library. This example serves as a starting point for understanding the fundamental usage of the package. ```julia # Example: Simple search # This section would contain Julia code demonstrating a basic symbolic regression search. ``` -------------------------------- ### Data Generation Setup Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Initializes the number of experiments and the random number generator for data simulation. ```julia n = 1000 rng = Random.MersenneTwister(0); ``` -------------------------------- ### Symbolic Regression Toy Example: Seeding Search with Initial Guesses Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Demonstrates how to seed the symbolic regression search with initial candidate expressions. This can help guide the search towards more relevant solutions. ```julia # Example: Seeding search with initial guesses # This section would contain Julia code for seeding the search with initial expressions. ``` -------------------------------- ### Seeding Search with Initial Guesses Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Demonstrates how to seed the symbolic regression search with initial guesses to optimize complex functions. It includes generating data for a complex expression and then setting up an SRRegressor with specific guesses to guide the search. ```julia using SymbolicRegression, MLJ X = randn(6, 2048) y = @. sin(X[1, :] * X[2, :] + 0.1f0) + cos(X[3, :]) * X[4, :] + X[5, :] / (X[6, :] * X[6, :] + 1) ``` ```julia using SymbolicRegression, MLJ model = SRRegressor( binary_operators=[+, -, *, /], unary_operators=[sin, cos], maxsize=35, niterations=35, guesses=["sin(x1 * x2) + cos(x3) * x4 + x5 / (x6 * x6 + 0.9)", #= can provide additional guesses here =#], batching=true, batch_size=32, ) mach = machine(model, X', y) fit!(mach) ``` -------------------------------- ### Install SymbolicRegression.jl Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Installs the SymbolicRegression.jl package using the Julia package manager. ```julia using Pkg Pkg.add("SymbolicRegression") ``` -------------------------------- ### Toggle Spotlight On Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Enables the Spotlight feature. ```UI ##### ONOn Turn on Spotlight. ``` -------------------------------- ### Define Options for Template Expressions Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Sets up `Options` for creating template expressions, specifying the allowed binary and unary operators. This configuration guides the symbolic regression process. ```julia options = Options(; binary_operators=(+, *, /, -), unary_operators=(sin, cos, sqrt, exp)) ``` -------------------------------- ### Symbolic Regression Toy Example: Template Expressions Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Demonstrates the use of template expressions to guide the symbolic regression search. This allows users to specify a desired structure or set of operators for the resulting equations. ```julia # Example: Template Expressions # This section would contain Julia code for using template expressions in the search. ``` -------------------------------- ### Creating a Template Expression Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Constructs a TemplateExpression using a defined structure, operators, and variable names. This example shows how to set up symbolic variables and their relationships. ```julia #= Note that we could have also used the `@template_spec` macro which is more convenient. First, let's look at an example of how this would be used in a TemplateExpression, for some guess at the form of the solution: =# options = Options(; binary_operators=(+, *, /, -), unary_operators=(sin, cos, sqrt, exp)) ## The inner operators are an `DynamicExpressions.OperatorEnum` which is used by `Expression`: operators = options.operators t = ComposableExpression(Node{Float64}(; feature=1); operators, variable_names) T = ComposableExpression(Node{Float64}(; feature=5); operators, variable_names) B_x = B_y = B_z = 2.1 * cos(t) F_d_scale = 1.0 * sqrt(T) ex = TemplateExpression( (; B_x, B_y, B_z, F_d_scale); structure, operators, variable_names ) ``` -------------------------------- ### Symbolic Regression Toy Example: Working with Expressions Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Provides examples of manipulating and analyzing symbolic expressions generated by the library. This covers operations like simplification, substitution, and analysis of expression structure. ```julia # Example: Working with Expressions # This section would contain Julia code for manipulating and analyzing symbolic expressions. ``` -------------------------------- ### Import Libraries for Symbolic Regression Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Imports the necessary libraries, SymbolicRegression and MLJ, for performing symbolic regression tasks. ```julia using SymbolicRegression using MLJ ``` -------------------------------- ### Logging Symbolic Regression with TensorBoard Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Shows how to integrate TensorBoard logging with SymbolicRegression.jl using SRLogger and TensorBoardLogger. This allows tracking the progress of the search process. ```julia using SymbolicRegression using TensorBoardLogger using MLJ logger = SRLogger(TBLogger("logs/sr_run")) ``` -------------------------------- ### Symbolic Regression with Template Expressions Overview Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Provides a comprehensive explanation of template expressions in SymbolicRegression.jl, detailing their benefits for imposing structure, handling vector-valued outputs, incorporating domain knowledge, and sharing sub-expressions. It outlines a tutorial example for learning particle motion equations. ```APIDOC # Searching with template expressions Template expressions are a powerful feature in SymbolicRegression.jl that allow you to impose structure on the symbolic regression search. Rather than searching for a completely free-form expression, you can specify a template that combines multiple sub-expressions in a prescribed way. This is particularly useful when any of the following are true: - You have domain knowledge about the functional form of your solution - You want to learn expressions for a vector-valued output - You need to enforce constraints on which variables can appear in different parts of the expression - You want to share sub-expressions between multiple components For example, you might know that your system follows a pattern like: `sin(f(x1, x2)) + g(x3)^2` where `f` and `g` are unknown functions you want to learn. With template expressions, you can encode this structure while still letting the symbolic regression search discover the optimal form of the sub-expressions. In this tutorial, we'll walk through a complete example of using template expressions to learn the components of a particle's motion under magnetic and drag forces. We'll see how to: 1. Define the structure of our template 2. Specify constraints on which variables each sub-expression can access 3. Set up the symbolic regression search 4. Interpret and use the results Let's get started! using SymbolicRegression using SymbolicRegression: ValidVector using Random using MLJBase: machine, fit!, predict, report ``` -------------------------------- ### Symbolic Regression Toy Example: Other Types Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Covers the handling of various data types and their integration into the symbolic regression process. This example explores flexibility in input data formats. ```julia # Example: Other types # This section would contain Julia code related to handling different data types. ``` -------------------------------- ### Example of Dimensionally Consistent Expression Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Provides an example string representing a dimensionally consistent expression found by the model. It shows how units are handled and indicates the presence of a free unit constant. ```julia "y[m s⁻² kg] = (M[kg] * 2.6353e-22[?])" ``` -------------------------------- ### Symbolic Regression Toy Example: Logging with TensorBoard Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Shows how to integrate the library's logging capabilities with TensorBoard for visualizing the progress and results of the symbolic regression search. ```julia # Example: Logging with TensorBoard # This section would contain Julia code demonstrating TensorBoard integration for logging. ``` -------------------------------- ### Initialize Random Number Generator Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Sets up a Mersenne Twister random number generator with a fixed seed for reproducible data generation. ```Julia rng = Random.MersenneTwister(0); ``` -------------------------------- ### Define Input Variable Names Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Lists the names of the input variables used in the symbolic regression problem. ```Julia variable_names = ["t", "v_x", "v_y", "v_z", "T"] ``` -------------------------------- ### Symbolic Regression Toy Example: Additional Features Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Covers various other features and functionalities of the Symbolic Regression library not categorized elsewhere. This serves as a catch-all for supplementary capabilities. ```julia # Example: Additional features # This section would contain Julia code for various additional features. ``` -------------------------------- ### Example Template Expression String Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Shows a string representation of a template expression, illustrating how variables and functions are combined. ```julia B_x = 2.1 * cos(#1); B_y = 2.1 * cos(#1); B_z = 2.1 * cos(#1); F_d_scale = 1.0 * sqrt(x5) ``` -------------------------------- ### Working with Expressions in SymbolicRegression.jl Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Demonstrates how to define, construct, and evaluate expressions using the Expression type in SymbolicRegression.jl. It shows how to set up operators, variables, and build complex expressions. ```julia using SymbolicRegression # Define options with operators and structure options = Options( binary_operators=[+, -, *], unary_operators=[cos], ) operators = options.operators variable_names = ["x1", "x2"] x1 = Expression( Node{Float64}(feature=1), operators=operators, variable_names=variable_names, ) x2 = Expression( Node{Float64}(feature=2), operators=operators, variable_names=variable_names, ) # Construct and evaluate expression expr = x1 * cos(x2 - 3.2) X = rand(Float64, 2, 100) output = expr(X) ``` ```julia get_contents(expr) ``` -------------------------------- ### Create and Train Machine Learning Model Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Creates a machine learning model using the configured SRRegressor and data (X, y), and prepares it for training. ```julia mach = machine(model, X, y) ``` -------------------------------- ### MLJ Interface Example Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Demonstrates using SymbolicRegression.jl with the MLJ framework, including model definition, fitting, reporting, and prediction. ```julia import SymbolicRegression: SRRegressor import MLJ: machine, fit!, predict, report # Dataset with two named features: X = (a = rand(500), b = rand(500)) # and one target: y = @. 2 * cos(X.a * 23.5) - X.b ^ 2 # with some noise: y = y .+ randn(500) .* 1e-3 model = SRRegressor( niterations=50, binary_operators=[+, -, *], unary_operators=[cos], ) mach = machine(model, X, y) fit!(mach) report(mach) predict(mach, X) # Select an equation from the Pareto front manually: predict(mach, (data=X, idx=2)) ``` -------------------------------- ### Generate Temperature Data Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Creates an array of temperatures for 1000 experiments, based on a baseline temperature with a small random variation. ```Julia T = 298.15 .+ 0.5 .* rand(rng, n) ``` -------------------------------- ### Prepare Data for MLJ with Units Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Formats the data with physical units into a structure suitable for MLJ, using named tuples for features (X) and the target variable (y). ```julia X = (; M=M, m=m, r=r) y = F ``` -------------------------------- ### Toggle Spotlight Off Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Disables the Spotlight feature. ```UI ##### OFFOff Turn off Spotlight. ``` -------------------------------- ### Retrieve and Display Regression Results Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Retrieves the report from a fitted symbolic regression model and displays the results, including the best tradeoff expression. ```julia r = report(mach) r ``` ```julia r.equations[r.best_idx] ``` -------------------------------- ### Cross Product Helper Function Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Defines a helper function to compute the cross product of two 3D vectors. ```julia cross((a1, a2, a3), (b1, b2, b3)) = (a2 * b3 - a3 * b2, a3 * b1 - a1 * b3, a1 * b2 - a2 * b1) ``` -------------------------------- ### Prepare Input Variables for Regressor Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Formats the input data into a named tuple suitable for the symbolic regression model, extracting velocity components. ```Julia X = (; t=data.t, v_x=[vi[1] for vi in data.v], v_y=[vi[2] for vi in data.v], v_z=[vi[3] for vi in data.v], T=data.T, ) keys(X) ``` -------------------------------- ### Fit the Machine Learning Model Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Trains the machine learning model using the `fit!` function, which will utilize the custom `combine_strings` function during the search process. ```julia fit!(mach) ``` -------------------------------- ### Importing SymbolicRegression.jl and Dependencies Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Imports the necessary libraries for using SymbolicRegression.jl, including its ValidVector type, and other utilities for random number generation and machine learning tasks. ```julia using SymbolicRegression using SymbolicRegression: ValidVector using Random using MLJBase: machine, fit!, predict, report ``` -------------------------------- ### Initial Velocity Data Generation Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Generates initial velocities for the particle in 3D space, with components ranging from -1 to 1. ```julia v = [ntuple(_ -> 2 * rand(rng) - 1, 3) for _ in 1:n] v[1:3] ``` -------------------------------- ### Accessing Fit Results Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Demonstrates how to fit the machine and access the results, including the discovered equations, which are a vector of TemplateExpression objects that dominated the Pareto front. ```julia #= At this point, you would run: \```julia fit!(mach) \``` which should print using your `combine_strings` function during the search. The final result is accessible with: \```julia report(mach) \``` which would return a named tuple of the fitted results, including the `.equations` field, which is a vector of `TemplateExpression` objects that dominated the Pareto front. =# ``` -------------------------------- ### Create Template Structure Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Initializes a `TemplateStructure` with specified symbols for subexpressions and the `compute_force` function. This structure defines how to build and evaluate composite expressions. ```julia structure = TemplateStructure{(:B_x, :B_y, :B_z, :F_d_scale)}(compute_force) ``` -------------------------------- ### Symbolic Regression Toy Example: Plotting an Expression Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Demonstrates how to plot the symbolic expressions discovered by the library. This helps in visualizing the functional form of the found equations. ```julia # Example: Plotting an expression # This section would contain Julia code for plotting the resulting symbolic expressions. ``` -------------------------------- ### Dataset Creation Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Combines all generated data (time, velocity, temperature, forces, magnetic field components) into a single named tuple dataset. ```julia data = (; t, v, T, F, B, F_d, F_mag) keys(data) ``` -------------------------------- ### Create and Fit Model with Logger Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Demonstrates how to initialize an SRRegressor with specific binary operators, maximum size, and number of iterations, and then fit the model to generated data. It also shows how to launch TensorBoard to visualize the training logs. ```julia using SymbolicRegression model = SRRegressor( binary_operators=[+, -, *], maxsize=40, niterations=100, logger=logger ) X = (a=rand(500), b=rand(500)) y = @. 2 * cos(X.a * 23.5) - X.b^2 mach = machine(model, X, y) fit!(mach) ``` ```bash tensorboard --logdir logs ``` -------------------------------- ### Basic Symbolic Regression Example Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/api Demonstrates how to use the SRRegressor from the SymbolicRegression package in Julia. It loads data, fits the model, makes predictions, and reports the best equation found. ```julia using MLJ SRRegressor = @load SRRegressor pkg=SymbolicRegression X, y = @load_boston model = SRRegressor(binary_operators=[+, -, *], unary_operators=[exp], niterations=100) mach = machine(model, X, y) fit!(mach) y_hat = predict(mach, X) # View the equation used: r = report(mach) println("Equation used:", r.equation_strings[r.best_idx]) ``` -------------------------------- ### VitePress Layout Options Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Documentation for adjusting the layout style of VitePress to suit different reading needs and screen sizes. Includes options for expanding the sidebar and content area, with adjustable widths. ```APIDOC Layout Switch: Adjust the layout style of VitePress to adapt to different reading needs and screens. Options: - Expand all: The sidebar and content area occupy the entire width of the screen. - Expand sidebar with adjustable values: Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same. - Expand all with adjustable values: Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same. - Original width: The original layout width of VitePress. ``` -------------------------------- ### Report and Display Multi-target Equations Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Retrieves and prints the best equations found by the MultitargetSRRegressor. It iterates through the reported equations and their best indices to display each target's expression. ```julia r = report(mach) for i in 1:3 println("y[$(i)] = ", r.equations[i][r.best_idx[i]]) end ``` -------------------------------- ### Convert Equation to LaTeX String Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Uses the Latexify package to convert a symbolic expression (represented as a string) into its LaTeX equivalent for easy rendering in documents or notebooks. ```julia using Latexify latexify(string(eqn)) ``` -------------------------------- ### SymbolicRegression.jl API Documentation Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/types This section provides API documentation for the SymbolicRegression.jl package. It details the available functions, their parameters, return values, and usage examples. ```APIDOC SymbolicRegression.jl API Reference: Equations: - Represents mathematical equations. Expressions: - Template Expressions: - Define custom equation structures. - Parametric Expressions: - Allow parameters within equations. Population: - Represents a collection of candidate equations. Population members: - Individual equations within a population. Hall of Fame: - Stores the best performing equations found during a search. Dataset: - Holds the input data for symbolic regression. ``` -------------------------------- ### Configuring SRRegressor with Template Expressions Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Sets up the SRRegressor for symbolic regression, incorporating a TemplateExpressionSpec to guide the search. It also defines a custom elementwise loss function suitable for vector outputs. ```julia #= So we can see that it prints the expression as we've defined it. Now, we can create a regressor that builds template expressions which follow this structure, by defining a `TemplateExpressionSpec` which wraps the `structure` object. This will result in generating expressions like the above `ex` object. =# model = SRRegressor(; binary_operators=(+, -, *, /), unary_operators=(sin, cos, sqrt, exp), niterations=500, maxsize=35, expression_spec=TemplateExpressionSpec(; structure), ## Note that the elementwise loss needs to operate directly on each row of `y`: elementwise_loss=(F1, F2) -> (F1.x - F2.x)^2 + (F1.y - F2.y)^2 + (F1.z - F2.z)^2, batching=true, batch_size=30, ); ``` -------------------------------- ### Create Dataset Tuple Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Combines all generated data arrays (time, velocity, temperature, forces, magnetic field components) into a named tuple for organized access. ```Julia data = (; t, v, T, F, B, F_d, F_mag) keys(data) ``` -------------------------------- ### Symbolic Regression Toy Example: Multiple Outputs Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Shows how to perform symbolic regression when the target is a system of multiple equations or outputs. This is useful for problems with multivariate relationships. ```julia # Example: Multiple outputs # This section would contain Julia code for symbolic regression with multiple output variables. ``` -------------------------------- ### Define Composable and Template Expressions Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Initializes ComposableExpression and TemplateExpression objects with specified features, operators, and variable names. These are foundational for building structured expressions. ```julia operators = options.operators t = ComposableExpression(Node{Float64}(; feature=1); operators, variable_names) T = ComposableExpression(Node{Float64}(; feature=5); operators, variable_names) B_x = B_y = B_z = 2.1 * cos(t) F_d_scale = 1.0 * sqrt(T) ex = TemplateExpression( (; B_x, B_y, B_z, F_d_scale); structure, operators, variable_names ) ``` -------------------------------- ### VitePress Spotlight Feature Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Documentation for the Spotlight feature in VitePress, which highlights the line currently under the mouse cursor. This feature aids users with reading and focusing difficulties by improving line visibility. ```APIDOC Spotlight: Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties. Options: - ON: Turn on Spotlight. - OFF: Turn off Spotlight. ``` -------------------------------- ### Symbolic Regression Example with MLJ Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/api Demonstrates how to use the `MultitargetSRRegressor` from the SymbolicRegression.jl package within the MLJ framework. It covers model loading, data preparation, model fitting, prediction, and reporting. ```julia using MLJ MultitargetSRRegressor = @load MultitargetSRRegressor pkg=SymbolicRegression X = (a=rand(100), b=rand(100), c=rand(100)) Y = (y1=(@. cos(X.c) * 2.1 - 0.9), y2=(@. X.a * X.b + X.c)) model = MultitargetSRRegressor(binary_operators=[+, -, *], unary_operators=[exp], niterations=100) mach = machine(model, X, Y) fit!(mach) y_hat = predict(mach, X) # View the equations used: r = report(mach) for (output_index, (eq, i)) in enumerate(zip(r.equation_strings, r.best_idx)) println("Equation used for ", output_index, ": ", eq[i]) end ``` -------------------------------- ### Symbolic Regression with Units and Variable Names Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/api Shows an example of using SRRegressor with units and custom variable names using the DynamicQuantities package in Julia. This allows for physically consistent modeling. ```julia using MLJ using DynamicQuantities SRegressor = @load SRRegressor pkg=SymbolicRegression X = (; x1=rand(32) .* us"km/h", x2=rand(32) .* us"km") y = @. X.x2 / X.x1 + 0.5us"h" model = SRRegressor(binary_operators=[+, -, *, /]) mach = machine(model, X, y) fit!(mach) y_hat = predict(mach, X) ``` -------------------------------- ### Higher-arity Operators Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Explains and demonstrates how to use operators with more than two arguments by explicitly passing an OperatorEnum. It provides an example using a ternary conditional operator (scalar_ifelse) to express piecewise logic. ```julia using SymbolicRegression, MLJ scalar_ifelse(a, b, c) = a > 0 ? b : c X = randn(3, 100) y = [X[1, i] > 0 ? 2*X[2, i] : X[3, i] for i in 1:100] ``` ```julia using SymbolicRegression, MLJ model = SRRegressor( operators=OperatorEnum( 1 => (), 2 => (+, -, *, /), 3 => (scalar_ifelse,) ), niterations=35, ) mach = machine(model, X', y) fit!(mach) ``` -------------------------------- ### SymbolicRegression.jl Slurm Cluster Example Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/slurm An example Julia script demonstrating how to use SymbolicRegression.jl with SlurmClusterManager.jl for distributed computing. It shows how to configure the number of tasks, define a custom loss function, and pass it to SRRegressor. ```julia using SymbolicRegression using Distributed: addprocs using SlurmClusterManager: SlurmManager using MLJ: machine, fit! # Figure out how large a job we are launching num_tasks = parse(Int, ENV["SLURM_NTASKS"]) # Define a custom loss function (this will # be automatically passed to the workers) my_loss(pred, targ) = abs2(pred - targ) ``` -------------------------------- ### Custom Loss Function Example Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/api Demonstrates how to define a custom loss function in Julia for symbolic regression. The function takes an expression tree, dataset, and options, returning a scalar loss value. It supports batching by accepting an optional index argument. ```Julia function my_loss(tree, dataset::Dataset{T,L}, options)::L where {T,L} prediction, flag = eval_tree_array(tree, dataset.X, options) if !flag return L(Inf) end return sum((prediction .- dataset.y) .^ 2) / dataset.n end ``` -------------------------------- ### Custom Loss Function Example Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/api Demonstrates how to define a custom loss function in Julia for symbolic regression. The function takes an expression tree, dataset, and options, returning a scalar loss value. It supports batching by accepting an optional index argument. ```Julia function my_loss(tree, dataset::Dataset{T,L}, options)::L where {T,L} prediction, flag = eval_tree_array(tree, dataset.X, options) if !flag return L(Inf) end return sum((prediction .- dataset.y) .^ 2) / dataset.n end ``` -------------------------------- ### Fitting the Symbolic Regression Model Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/parameterized_function Creates a machine learning pipeline using MLJBase to fit the configured SymbolicRegression model to the generated data. It shows how to initialize the machine and the command to start the fitting process. ```julia using MLJBase: machine, fit!, predict, report mach = machine(model, X, y) # To fit the model: # fit!(mach) # To extract results: # report(mach).equations[end] ``` -------------------------------- ### Time Data Generation Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Generates random time points between 0 and 10 seconds for each experiment. ```julia t = 10 .* rand(rng, n) t[1:3] ``` -------------------------------- ### VitePress Layout and Spotlight Options Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/api Configuration options for customizing the VitePress documentation theme, including sidebar and content width adjustments, and enabling/disabling a spotlight feature for improved readability. ```APIDOC Layout Switch: Adjust the layout style of VitePress to adapt to different reading needs and screens. - Expand all - Expand sidebar with adjustable values - Expand all with adjustable values - Original width Page Layout Max Width: Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens. - Adjust the maximum width of the page layout (ranged slider) Content Layout Max Width: Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens. - Adjust the maximum width of the content layout (ranged slider) Spotlight: Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties. - ON: Turn on Spotlight. - OFF: Turn off Spotlight. ``` -------------------------------- ### TemplateExpression Type Definition and Usage Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/types Defines the TemplateExpression type for structured expressions and provides an example of its construction. ```julia TemplateExpression{T,F,N,E,TS,D} <: AbstractExpression{T,N} ``` A symbolic expression that allows the combination of multiple sub-expressions in a structured way, with constraints on variable usage. `TemplateExpression` is designed for symbolic regression tasks where domain-specific knowledge or constraints must be imposed on the model's structure. **Constructor** * `TemplateExpression(trees; structure, operators, variable_names)` * `trees`: A `NamedTuple` holding the sub-expressions (e.g., `f = Expression(...)`, `g = Expression(...)`). * `structure`: A `TemplateStructure` which holds functions that define how the sub-expressions are combined in different contexts. * `operators`: An `OperatorEnum` that defines the allowed operators for the sub-expressions. * `variable_names`: An optional `Vector` of `String` that defines the names of the variables in the dataset. **Example** Let's create an example `TemplateExpression` that combines two sub-expressions `f(x1, x2)` and `g(x3)`: julia``` # Define operators and variable names options = Options(; binary_operators=(+, *, /, -), unary_operators=(sin, cos)) operators = options.operators variable_names = ["x1", "x2", "x3"] # Create sub-expressions x1 = Expression(Node{Float64}(; feature=1); operators, variable_names) x2 = Expression(Node{Float64}(; feature=2); operators, variable_names) x3 = Expression(Node{Float64}(; feature=3); operators, variable_names) ``` -------------------------------- ### Spotlight Feature Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Highlights the line currently hovered by the mouse to assist users with reading and focusing difficulties. ```UI #### Spotlight Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties. ``` -------------------------------- ### Set up Machine Learning Pipeline Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/parameterized_function Creates a machine learning pipeline using MLJBase, associating the configured SRRegressor model with the generated data (X and y). ```julia mach = machine(model, X, y) ``` -------------------------------- ### Total Force Calculation Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Calculates the total force F by summing the drag force and magnetic force. ```julia F = [fd .+ fm for (fd, fm) in zip(F_d, F_mag)] F[1:3] ``` -------------------------------- ### VitePress Page Layout Width Control Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples API documentation for controlling the maximum page width in VitePress. Allows users to adjust the layout's exact width to fit various reading requirements and screen dimensions using a slider. ```APIDOC Page Layout Max Width: Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens. Control: - Adjust the maximum width of the page layout: A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go. ``` -------------------------------- ### Generate Time Data Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Generates an array of random time points between 0 and 10 seconds for each of the 1000 experiments. ```Julia t = 10 .* rand(rng, n) ``` -------------------------------- ### Drag Force Model Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Specifies the drag force as being proportional to velocity and dependent on temperature via a power law. ```math \mathbf{F}_\text{drag} = -\alpha T^{1/2} \mathbf{v} ``` -------------------------------- ### Using Differential Operators Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples Shows how to use differential operators with SymbolicRegression.jl and DynamicDiff.jl to discover symbolic expressions for integrals. It includes generating data, defining an expression specification with a derivative operator, setting up the SRRegressor, fitting the model, and accessing the learned expression. ```julia using SymbolicRegression using Random rng = MersenneTwister(42) x = 1 .+ rand(rng, 1000) * 9 # Sampling points in the range [1, 10] y = @. 1 / (x^2 * sqrt(x^2 - 1)) # Values of the integrand ``` ```julia using SymbolicRegression: D expression_spec = @template_spec(expressions=(f,)) do x D(f, 1)(x) end ``` ```julia using SymbolicRegression using MLJ model = SRRegressor( binary_operators=(+, -, *, /), unary_operators=(sqrt,), maxsize=20, expression_spec=expression_spec, ) X = (; x=x) mach = machine(model, X, y) fit!(mach) ``` ```julia r = report(mach) best_expr = r.equations[r.best_idx] println("Learned expression: ", best_expr) ``` -------------------------------- ### Output Struct Definition Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Defines a `Force` struct to represent the 3D force vector, which will be the target variable for regression. ```julia struct Force{T} x::T y::T z::T end y = [Force(F...) for F in data.F] y[1:3] ``` -------------------------------- ### Symbolic Regression API - Options Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Documentation for the various configuration options available within the Symbolic Regression library. This covers parameters that can be tuned to influence the search process and model behavior. ```APIDOC Options: - Configuration parameters for the symbolic regression search. - Examples include population size, mutation rates, and stopping criteria. ``` -------------------------------- ### VitePress Content Layout Width Control Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples API documentation for controlling the maximum content width within the VitePress layout. Users can customize the document content's width using a slider for optimal viewing on different screens. ```APIDOC Content Layout Max Width: Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens. Control: - Adjust the maximum width of the content layout: A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go. ``` -------------------------------- ### Temperature Data Generation Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Generates synthetic temperature data for each experiment, with a base temperature and slight random variations. ```julia T = 298.15 .+ 0.5 .* rand(rng, n) T[1:3] ``` -------------------------------- ### Calculate Magnetic Force Vectors Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Computes the magnetic force vectors using the cross product of velocity and the magnetic field. ```Julia cross((a1, a2, a3), (b1, b2, b3)) = (a2 * b3 - a3 * b2, a3 * b1 - a1 * b3, a1 * b2 - a2 * b1) F_mag = [cross(vi, Bi) for (vi, Bi) in zip(v, B)] ``` -------------------------------- ### Import Libraries Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/parameterized_function Imports necessary libraries for symbolic regression, random number generation, and machine learning utilities. ```julia using SymbolicRegression using Random: MersenneTwister using MLJBase: machine, fit!, predict, report using Test ``` -------------------------------- ### Calculate Drag Force Vectors Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Computes the drag force vectors for each experiment, using the temperature-dependent drag coefficient model. ```Julia F_d = [-1e-5 * Ti^(1//2) .* vi for (Ti, vi) in zip(T, v)] ``` -------------------------------- ### Calculate Total Force Vectors Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Sums the drag and magnetic forces to obtain the total force acting on the particle for each experiment. ```Julia F = [fd .+ fm for (fd, fm) in zip(F_d, F_mag)] ``` -------------------------------- ### Symbolic Regression API - Logging Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/index Documentation for the logging capabilities of the Symbolic Regression library, including integration with tools like TensorBoard for monitoring and visualization of the search process. ```APIDOC Logging: - Support for logging search progress and results. - Integration with TensorBoard for visualization. ``` -------------------------------- ### Generate Velocity Data Source: https://ai.damtp.cam.ac.uk/symbolicregression/dev/examples/template_expression Creates a list of 3D velocity vectors for each experiment, with each component randomly sampled between -1 and 1. ```Julia v = [ntuple(_ -> 2 * rand(rng) - 1, 3) for _ in 1:n] ```