📄 bpnet.cpp
字号:
else
{dfile_ptr >> hold; sample_number += 1;}
}while(lock > 0);
dfile_ptr.close();
sample_number = int(sample_number / signal_dimensions);
}
void ART_Training_Data::request_ART_data(int net_no)
{
cout << "Please enter the file name containing the training data for ART network no. "<< net_no << "\n";
cin >> filename; cout << "\n";
specify_signal_sample_size();
}
void ART_Test_Data::request_ART_data(int net_no)
{
cout << "Please enter the file name containing the test data for ART network no. " << net_no << "\n";
cin >> filename; cout << "\n";
specify_signal_sample_size();
}
//****************************************************************************//
NeuralA::~NeuralA()
{ delete [] ART_Test; }
void NeuralA::construct_ART_network(void)
{
int looploc = 0;
clrscr();
cout << " **** Adaptive Resonance Theory network for binary signals **** " <<"\n\n\n";
do
{
cout <<"\n";
cout << "Do you wish to" << "\n\n";
cout << "C. Create your own ART1 Network " << "\n";
cout << "U. Upload an existing ART1 Network " << "\n\n";
cout << "Your choice?: "; cin >> ART_Design.netcreate;
cout << "\n\n";
ART_Design.netcreate = toupper(ART_Design.netcreate);
if((ART_Design.netcreate == 'C') || (ART_Design.netcreate == 'U')) {looploc = 1;}
} while(looploc <= 0);
if(ART_Design.netcreate == 'U')
{ART_Design.upload_network();}
else
{
cout << "\n";
cout << "Please enter the dimensions of the ART network's input signal vector: ";
cin >> ART_Design.dimensions_of_signal; cout << "\n";
cout << "Please enter the vigilance parameter of the ART network: ";
cin >> ART_Design.vigilance_parameter; cout << "\n";
}
}
void NeuralA::initialize_ART_training_storage_array(int AN)
{
int AT = AN;
ART_Train.acquire_net_info(ART_Design.dimensions_of_signal, ART_Design.number_of_cluster_units);
ART_Train.request_ART_data(AT);
if(ART_Design.netcreate == 'C') // constructing new network
{
ART_Design.number_of_cluster_units = ART_Train.sample_number;
ART_Design.establish_net_topology();
}
}
void NeuralA::train_ART_network(int ARTN)
{
int dim, nodes_available_for_clustering;
char savetrain;
int dolock = 0;
clrscr();
cout << "\n\n";
cout << "For Neural Network #" << ARTN << "\n";
do
{
cout << "do you wish to save the ART Training results to a file? (Y or N): ";
cin >> savetrain;
savetrain = toupper(savetrain);
if((savetrain == 'N') || (savetrain == 'Y')) {dolock = 1;}
cout << "\n";
} while(dolock <= 0);
if(savetrain == 'Y')
{
cout << "please enter the name of the file to hold the results of the ART Training" << "\n";
cin >> ART_Train.resultsname; cout << "\n";
}
for(int pattern = 0; pattern < ART_Train.sample_number; pattern++)
{
// present pattern to input layer
for(dim = 0; dim < ART_Design.dimensions_of_signal; dim++)
{ART_Design.node_in_input_layer[dim].signal_value = ART_Train.number_of_samples[pattern].data_in_sample[dim];}
nodes_available_for_clustering = ART_Design.number_of_cluster_units;
do
{
ART_Design.transmit_pattern_to_interface();
ART_Design.broadcast_output_to_cluster_layer();
ART_Design.cluster_nodes_compete_for_activation(1);
ART_Design.update_the_network();
nodes_available_for_clustering = nodes_available_for_clustering - ART_Design.reset_value;
if(nodes_available_for_clustering < 1) // input pattern cannot be clustered
{
// clrscr();
cout << "Input pattern #" << pattern + 1 << ": ";
for(dim = 0; dim < ART_Design.dimensions_of_signal; dim++)
{cout << int(ART_Design.node_in_input_layer[dim].signal_value);}
cout << " cannot be clustered" << "\n";
break;
}
} while (ART_Design.reset_value >=1);
if(savetrain == 'Y')
{
ofstream ART_savefile_ptr(ART_Train.resultsname, ios::out|ios::app);
ART_savefile_ptr << pattern + 1 << " ";
for(dim = 0; dim < ART_Design.dimensions_of_signal; dim++)
{ART_savefile_ptr << int(ART_Design.node_in_input_layer[dim].signal_value);}
ART_savefile_ptr << " " << ART_Design.node_in_cluster_layer[ART_Design.cluster_champ].cluster_tag << "\n";
ART_savefile_ptr.close();
}
ART_Design.set_cluster_activation_to_zero();
}
// delete array containing training data
ART_Train.delete_signal_array();
}
void NeuralA::establish_ART_test_battery_size(void)
{
cout <<"Please enter the number of tests you wish to run on the ART neural network: ";
cin >> number_of_ART_tests; cout <<"\n";
// create testing array
if(number_of_ART_tests > 0)
{
ART_Test = new ART_Test_Data[number_of_ART_tests];
for(int t = 0; t < number_of_ART_tests; t++)
{ART_Test[t].acquire_net_info(ART_Design.dimensions_of_signal, ART_Design.number_of_cluster_units);}
}
}
void NeuralA::test_ART_network(int ANET)
{
int tnet, dim, pattern;
tnet = ANET;
for(int Atest = 0; Atest < number_of_ART_tests; Atest++)
{
ART_Test[Atest].request_ART_data(tnet);
cout << "For ART1 neural network #" << ANET <<" and test #"<<Atest+1<<":" <<"\n";
cout << "please enter the name of the file to hold the results of the ART Testing " << "\n";
cin >> ART_Test[Atest].resultsname; cout << "\n";
ofstream ART_savefile_ptr(ART_Test[Atest].resultsname);
for(pattern = 0; pattern < ART_Test[Atest].sample_number; pattern++)
{
for(dim = 0; dim < ART_Design.dimensions_of_signal; dim++)
{ART_Design.node_in_input_layer[dim].signal_value = ART_Test[Atest].number_of_samples[pattern].data_in_sample[dim];}
ART_Design.transmit_pattern_to_cluster();
ART_Design.cluster_nodes_compete_for_activation(2);
ART_savefile_ptr <<pattern + 1<<" ";
for(dim = 0; dim < ART_Design.dimensions_of_signal; dim++)
{ART_savefile_ptr << int(ART_Design.node_in_input_layer[dim].signal_value);}
ART_savefile_ptr << " " << ART_Design.node_in_cluster_layer[ART_Design.cluster_champ].cluster_tag << "\n";
}
ART_savefile_ptr.close(); // end of test
ART_Test[Atest].delete_signal_array();
}
}
void NeuralA::network_training_testing(int TT)
{
int tt = TT;
int menu_choice;
clrscr();
cout << "\n\n\n\n";
cout << "**************** Operations Menu ****************" << "\n\n";
cout << " Please select one of the following options:" <<"\n\n";
cout << " 1. Train ART1 network only " <<"\n\n";
if(ART_Design.netcreate == 'U')
{
cout << " 2. Test ART1 network only " <<"\n\n";
cout << " 3. Train and Test ART1 network" <<"\n\n";
}
else
{
cout << " 2. Train and Test ART1 network" <<"\n\n";
}
cout << "*************************************************" << "\n\n";
cout << " Your choice?: "; cin >> menu_choice;
cout << "\n\n";
if((menu_choice == 2) && (ART_Design.netcreate == 'C')) {menu_choice = 3;}
if((menu_choice == 3) && (ART_Design.netcreate == 'U')) {menu_choice = 3;}
switch(menu_choice)
{
case 1:
initialize_ART_training_storage_array(tt);
train_ART_network(tt);
break;
case 2:
establish_ART_test_battery_size();
if(number_of_ART_tests > 0)
{test_ART_network(tt);}
break;
case 3:
initialize_ART_training_storage_array(tt);
train_ART_network(tt);
establish_ART_test_battery_size();
if(number_of_ART_tests > 0)
{test_ART_network(tt);}
break;
default:network_training_testing(tt);
}
}
Kohonen_units::Kohonen_units()
{number_of_outputs = 1;}
void Kohonen_units::establish_input_weight_vector_array(void)
{input_weight_vector = new float[number_of_inputs];}
void Kohonen_units::initialize_inputs_and_weights(void)
{
for(int k = 0; k < number_of_inputs; k++)
{input_weight_vector[k] = bedlam((long*)(&gaset));}
}
void Kohonen_units::calculate_sum_square_Euclidean_distance(void)
{
double sumsquare;
float ss1;
int ci;
output_value[0] = 0.0;
for(int k = 0; k < number_of_inputs; k++)
{
ci = k;
if(input_value[ci] == 0.0)
{
sumsquare = pow(input_weight_vector[ci], 2.0);
}
else
{
sumsquare = pow(fabs(input_weight_vector[ci] - input_value[ci]), 2.0);
}
output_value[0] += sumsquare;
// cout << output_value[0] << "\n";
// cin >> output_value[0];
}
ss1 = output_value[0];
output_value[0] = sqrt(fabs(ss1));
}
void Kohonen_units::update_the_weights(float learning_rate)
{
for(int k = 0; k < number_of_inputs; k++)
{input_weight_vector[k] = input_weight_vector[k] + (learning_rate * (input_value[k] - input_weight_vector[k]));}
}
// RBFN //
void Kohonen_units::execute_Gaussian_transfer_function(void)
{
float transfer_ratio = (-1.0) * pow((output_value[0] / transfer_function_width), 2.0);
Gaussian_transfer_output = exp(transfer_ratio);
}
Kohonen_Topology::Kohonen_Topology()
{interim_learning_rate = 1.0;}
Kohonen_Topology::~Kohonen_Topology()
{delete [] node_in_cluster_layer;}
void Kohonen_Topology::establish_Kohonen_topology(int netuse)
{
char netcreate;
int looploc = 0;
if(netuse == 1)
{
do
{
cout <<"\n";
cout << "Do you wish to" << "\n\n";
cout << "C. Create your own Kohonen Map " << "\n";
cout << "U. Upload an existing Kohonen Map " << "\n\n";
cout << "Your choice?: "; cin >> netcreate;
cout << "\n\n";
netcreate = toupper(netcreate);
if((netcreate == 'C') || (netcreate == 'U')) {looploc = 1;}
} while(looploc <= 0);
}
else
{
netcreate = 'C';
}
if((netcreate == 'U') && (netuse == 1))
{upload_network();}
else
{
if(netuse == 1)
{
cout <<"Please enter the dimensions of the network's input signal vector: ";
cin >> dimensions_of_signal; cout <<"\n";
}
cout << "please enter the maximum number of clusters to be formed: ";
cin >> maximum_number_of_clusters; cout << "\n";
// establish clustering layer of Kohonen network
node_in_cluster_layer = new Kohonen_units[maximum_number_of_clusters];
for(int c = 0; c < maximum_number_of_clusters; c++)
{
node_in_cluster_layer[c].number_of_inputs = dimensions_of_signal;
node_in_cluster_layer[c].establish_input_output_arrays();
node_in_cluster_layer[c].establish_input_weight_vector_array();
node_in_cluster_layer[c].initialize_inputs_and_weights();
}
}
}
void Kohonen_Topology::upload_network(void)
{
char getname[13];
ifstream get_ptr;
int netid, nodes, dim;
int dolock = 0;
do
{
cout << "\n\n";
cout << "Please enter the name of the file which holds the Kohonen Map" << "\n";
cin >> getname; cout << "\n";
get_ptr.open(getname, ios::in);
get_ptr >> netid;
if(netid == 3) {dolock = 1;}
else
{
cout << "Error** file contents do not match Kohonen specifications" << "\n";
cout << "try again" << "\n";
get_ptr.close();
}
} while(dolock <= 0);
get_ptr >> dimensions_of_signal;
get_ptr >> maximum_number_of_clusters;
node_in_cluster_layer = new Kohonen_units[maximum_number_of_clusters];
for(nodes = 0; nodes < maximum_number_of_clusters; nodes++)
{
node_in_cluster_layer[nodes].number_of_inputs = dimensions_of_signal;
node_in_cluster_layer[nodes].establish_input_output_arrays();
node_in_cluster_layer[nodes].establish_input_weight_vector_array();
}
for(nodes = 0; nodes < maximum_number_of_clusters; nodes++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -