mirror of
https://github.com/WallabyLester/RBF-aPID-Controller.git
synced 2026-06-02 04:47:00 +08:00
Matching syntax.
This commit is contained in:
@@ -3,10 +3,10 @@
|
|||||||
/**
|
/**
|
||||||
* @brief Constructor to initialize the RBF model.
|
* @brief Constructor to initialize the RBF model.
|
||||||
*/
|
*/
|
||||||
RBFModel::RBFModel(int numCenters, int inputDim, double sigma, bool randomCenters)
|
RBFModel::RBFModel(int n_centers, int input_dim, double sigma, bool random_centers)
|
||||||
: numCenters(numCenters), inputDim(inputDim), sigma(sigma) {
|
: n_centers(n_centers), input_dim(input_dim), sigma(sigma) {
|
||||||
centers = new double*[numCenters]; // Allocate memory for centers
|
centers = new double*[n_centers]; // Allocate memory for centers
|
||||||
weights = new double[numCenters]; // Allocate memory for weights
|
weights = new double[n_centers]; // Allocate memory for weights
|
||||||
|
|
||||||
// Check if memory allocation was successful
|
// Check if memory allocation was successful
|
||||||
if (!centers || !weights) {
|
if (!centers || !weights) {
|
||||||
@@ -15,14 +15,14 @@ RBFModel::RBFModel(int numCenters, int inputDim, double sigma, bool randomCenter
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize centers and weights
|
// Initialize centers and weights
|
||||||
for (int i = 0; i < numCenters; ++i) {
|
for (int i = 0; i < n_centers; ++i) {
|
||||||
centers[i] = new double[inputDim]; // Allocate memory for each center
|
centers[i] = new double[input_dim]; // Allocate memory for each center
|
||||||
if (randomCenters) {
|
if (random_centers) {
|
||||||
for (int j = 0; j < inputDim; ++j) {
|
for (int j = 0; j < input_dim; ++j) {
|
||||||
centers[i][j] = static_cast<double>(rand()) / RAND_MAX; // Random centers
|
centers[i][j] = static_cast<double>(rand()) / RAND_MAX; // Random centers
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int j = 0; j < inputDim; ++j) {
|
for (int j = 0; j < input_dim; ++j) {
|
||||||
centers[i][j] = static_cast<double>(i); // Fixed centers
|
centers[i][j] = static_cast<double>(i); // Fixed centers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ RBFModel::RBFModel(int numCenters, int inputDim, double sigma, bool randomCenter
|
|||||||
* @brief Destructor to free allocated memory.
|
* @brief Destructor to free allocated memory.
|
||||||
*/
|
*/
|
||||||
RBFModel::~RBFModel() {
|
RBFModel::~RBFModel() {
|
||||||
for (int i = 0; i < numCenters; ++i) {
|
for (int i = 0; i < n_centers; ++i) {
|
||||||
delete[] centers[i]; // Free memory for each center inside centers
|
delete[] centers[i]; // Free memory for each center inside centers
|
||||||
}
|
}
|
||||||
delete[] centers; // Free memory for centers
|
delete[] centers; // Free memory for centers
|
||||||
@@ -46,7 +46,7 @@ RBFModel::~RBFModel() {
|
|||||||
*/
|
*/
|
||||||
double RBFModel::gaussian(const double* input, const double* center) {
|
double RBFModel::gaussian(const double* input, const double* center) {
|
||||||
double norm = 0.0;
|
double norm = 0.0;
|
||||||
for (int i = 0; i < inputDim; ++i) {
|
for (int i = 0; i < input_dim; ++i) {
|
||||||
norm += pow(input[i] - center[i], 2);
|
norm += pow(input[i] - center[i], 2);
|
||||||
}
|
}
|
||||||
return exp(-0.5 * norm / (sigma * sigma));
|
return exp(-0.5 * norm / (sigma * sigma));
|
||||||
@@ -57,7 +57,7 @@ double RBFModel::gaussian(const double* input, const double* center) {
|
|||||||
*/
|
*/
|
||||||
double RBFModel::predict(const double* input) {
|
double RBFModel::predict(const double* input) {
|
||||||
double output = 0.0;
|
double output = 0.0;
|
||||||
for (int i = 0; i < numCenters; ++i) {
|
for (int i = 0; i < n_centers; ++i) {
|
||||||
output += weights[i] * gaussian(input, centers[i]);
|
output += weights[i] * gaussian(input, centers[i]);
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
@@ -66,10 +66,10 @@ double RBFModel::predict(const double* input) {
|
|||||||
/**
|
/**
|
||||||
* @brief Adapt weights based on the error and learning rate.
|
* @brief Adapt weights based on the error and learning rate.
|
||||||
*/
|
*/
|
||||||
void RBFModel::adapt(double error, double learningRate, const double* input) {
|
void RBFModel::adapt(double error, double learning_rate, const double* input) {
|
||||||
for (int i = 0; i < numCenters; ++i) {
|
for (int i = 0; i < n_centers; ++i) {
|
||||||
double influence = gaussian(input, centers[i]); // Calculate influence based on input
|
double influence = gaussian(input, centers[i]); // Calculate influence based on input
|
||||||
weights[i] += learningRate * error * influence; // Update weight based on error and influence
|
weights[i] += learning_rate * error * influence; // Update weight based on error and influence
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ void RBFModel::adapt(double error, double learningRate, const double* input) {
|
|||||||
* @brief Get the weight at a specific index.
|
* @brief Get the weight at a specific index.
|
||||||
*/
|
*/
|
||||||
double RBFModel::get_weight(int index) const {
|
double RBFModel::get_weight(int index) const {
|
||||||
if (index < 0 || index >= numCenters) return 0.0;
|
if (index < 0 || index >= n_centers) return 0.0;
|
||||||
return weights[index];
|
return weights[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +85,6 @@ double RBFModel::get_weight(int index) const {
|
|||||||
* @brief Set the weight at a specific index.
|
* @brief Set the weight at a specific index.
|
||||||
*/
|
*/
|
||||||
void RBFModel::set_weight(int index, double value) {
|
void RBFModel::set_weight(int index, double value) {
|
||||||
if (index < 0 || index >= numCenters) return;
|
if (index < 0 || index >= n_centers) return;
|
||||||
weights[index] = value;
|
weights[index] = value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,12 +21,12 @@ public:
|
|||||||
* of centers, input dimensions, and the spread (sigma) of the RBFs.
|
* of centers, input dimensions, and the spread (sigma) of the RBFs.
|
||||||
* Random initialization of the centers can be turned off.
|
* Random initialization of the centers can be turned off.
|
||||||
*
|
*
|
||||||
* @param num_centers The number of radial basis function centers.
|
* @param n_centers The number of radial basis function centers.
|
||||||
* @param input_dim The dimensionality of the input data.
|
* @param input_dim The dimensionality of the input data.
|
||||||
* @param sigma The spread of the RBFs (default is 1.0).
|
* @param sigma The spread of the RBFs (default is 1.0).
|
||||||
* @param random_centers Boolean to initialize centers randomly (default is true).
|
* @param random_centers Boolean to initialize centers randomly (default is true).
|
||||||
*/
|
*/
|
||||||
RBFModel(int num_centers, int input_dim, double sigma = 1.0, bool random_centers = true);
|
RBFModel(int n_centers, int input_dim, double sigma = 1.0, bool random_centers = true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Destructor to free allocated memory.
|
* @brief Destructor to free allocated memory.
|
||||||
@@ -69,7 +69,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
double** centers; // 2D array for centers
|
double** centers; // 2D array for centers
|
||||||
double* weights; // Array of weights
|
double* weights; // Array of weights
|
||||||
int num_centers; // Number of RBF centers
|
int n_centers; // Number of RBF centers
|
||||||
int input_dim; // Dimension of the input
|
int input_dim; // Dimension of the input
|
||||||
double sigma; // Spread of the RBF
|
double sigma; // Spread of the RBF
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user