ExpressionTree<T, TInput, TOutput>
Models & Types · AiDotNet.LinearAlgebra
Represents a symbolic expression tree for mathematical operations that can be used for symbolic regression.
Inheritance: ModelBase<T, TInput, TOutput> → ExpressionTree<T, TInput, TOutput>
How It Works
For Beginners: An ExpressionTree is like a mathematical formula represented as a tree structure. Each node in the tree is either a number, a variable, or an operation (like addition or multiplication). This allows the AI to create and evolve mathematical formulas that can model your data.
Example
// A symbolic expression tree for (x0 * 2.5) + x1. Every node is the same constructor: a node type,
// a value for leaves, and two children for operations.
var x0 = new ExpressionTree<double, Matrix<double>, Vector<double>>(
ExpressionNodeType.Variable, 0);
var scale = new ExpressionTree<double, Matrix<double>, Vector<double>>(
ExpressionNodeType.Constant, 2.5);
var x1 = new ExpressionTree<double, Matrix<double>, Vector<double>>(
ExpressionNodeType.Variable, 1);
var product = new ExpressionTree<double, Matrix<double>, Vector<double>>(
ExpressionNodeType.Multiply, left: x0, right: scale);
var tree = new ExpressionTree<double, Matrix<double>, Vector<double>>(
ExpressionNodeType.Add, left: product, right: x1);
double value = tree.Evaluate(new Vector<double>(new double[] { 3.0, 1.0 })); // (3.0 * 2.5) + 1.0
Constructors
| Constructor | Summary |
|---|---|
ExpressionTree(ExpressionNodeType type, T value, ExpressionTree<T, TInput, TOutput> left, ExpressionTree<T, TInput, TOutput> right, ILossFunction<T> lossFunction) |
Creates a new expression tree node with the specified properties. |
Properties
| Property | Summary |
|---|---|
ExpressionNodeType Type { get; set; } |
Gets the type of this node (constant, variable, or operation). |
ExpressionTree<T, TInput, TOutput> Left { get; set; } |
Gets the left child node of this node. |
ExpressionTree<T, TInput, TOutput> Parent { get; set; } |
Gets the parent node of this node. |
ExpressionTree<T, TInput, TOutput> Right { get; set; } |
Gets the right child node of this node. |
ILossFunction<T> DefaultLossFunction { get; } |
Gets the default loss function used by this model for gradient computation. |
T Value { get; set; } |
Gets the value stored in this node. |
Vector<T> Coefficients { get; } |
Gets a vector containing all coefficient values in this expression tree. |
int Complexity { get; } |
Gets the complexity of this expression tree, measured as the total number of nodes. |
int FeatureCount { get; } |
Gets the number of features (variables) used in this expression tree. |
int Id { get; } |
Gets the unique identifier for this node. |
int RequiredFeatureCount { get; } |
Gets the minimum number of features required for input data to this expression tree. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, T> GetFeatureImportance() |
Gets the feature importance scores for this expression tree. |
ExpressionTree<T, TInput, TOutput> Deserialize(BinaryReader reader) |
Deserializes an expression tree from a binary reader. |
ExpressionTree<T, TInput, TOutput> FindNodeById(int id) |
Finds a node in the tree by its unique identifier. |
IEnumerable<int> GetActiveFeatureIndices() |
Gets the indices of all features (variables) used in this expression tree. |
IFullModel<T, TInput, TOutput> Copy() |
Creates a deep copy of this expression tree. |
IFullModel<T, TInput, TOutput> Crossover(IFullModel<T, TInput, TOutput> other, double crossoverRate) |
Combines this expression tree with another to create a new “offspring” expression tree. |
IFullModel<T, TInput, TOutput> Mutate(double mutationRate) |
Creates a modified version of this expression tree by applying random mutations. |
IFullModel<T, TInput, TOutput> UpdateCoefficients(Vector<T> newCoefficients) |
Creates a new expression tree with updated coefficient values. |
IFullModel<T, TInput, TOutput> WithParameters(Vector<T> parameters) |
Creates a new expression tree with updated parameters. |
List<ExpressionTree<T, TInput, TOutput>> GetAllNodes() |
Gets a list of all nodes in this expression tree. |
ModelMetadata<T> GetModelMetadata() |
Gets metadata about this expression tree model. |
T Evaluate(Vector<T> input) |
Evaluates this expression tree for a given input vector. |
TOutput Predict(TInput input) |
Makes a prediction for an input example. |
Vector<T> ComputeGradients(TInput input, TOutput target, ILossFunction<T> lossFunction) |
Computes gradients of the loss function with respect to model parameters WITHOUT updating parameters. |
Vector<T> Predict(Matrix<T> input) |
Makes a prediction for an input example. |
bool IsFeatureUsed(int featureIndex) |
Checks if a specific feature (variable) is used in this expression tree. |
string ToString() |
Returns a string representation of this expression tree. |
void ApplyGradients(Vector<T> gradients, T learningRate) |
Applies pre-computed gradients to update the model parameters (constants in the expression tree). |
void Fit(Matrix<T> X, Vector<T> y) |
Fits the expression tree to the provided training data. |
void LoadModel(string filePath) |
Loads an expression tree model from a file. |
void LoadState(Stream stream) |
Loads the expression tree’s state (structure and values) from a stream. |
void SaveModel(string filePath) |
Saves the expression tree model to a file. |
void SaveState(Stream stream) |
Saves the expression tree’s current state (structure and values) to a stream. |
void Serialize(BinaryWriter writer) |
Writes this expression tree to a binary stream. |
void SetActiveFeatureIndices(IEnumerable<int> featureIndices) |
Sets the active feature indices for this expression tree. |
void SetLeft(ExpressionTree<T, TInput, TOutput> left) |
Sets the left child of this node and updates the parent reference of the child. |
void SetRight(ExpressionTree<T, TInput, TOutput> right) |
Sets the right child of this node and updates the parent reference of the child. |
void SetType(ExpressionNodeType type) |
Sets the type of this node. |
void SetValue(T value) |
Sets the value of this node. |
void Train(Matrix<T> x, Vector<T> y) |
Trains the expression tree on a single input-output pair. |
void Train(TInput input, TOutput expectedOutput) |
Trains the expression tree on a single input-output pair. |