// Worker thread
void SearchSolution()
{
// number of learning samples
int samples = data.GetLength(0);
// data transformation factor
double yFactor = 1.7 / chart.RangeY.Length;
double yMin = chart.RangeY.Min;
double xFactor = 2.0 / chart.RangeX.Length;
double xMin = chart.RangeX.Min;
// prepare learning data
double[][] input = new double[samples][];
double[][] output = new double[samples][];
for (int i = 0; i < samples; i++)
{
input[i] = new double[1];
output[i] = new double[1];
// set input
input[i][0] = (data[i, 0] - xMin) * xFactor - 1.0;
// set output
output[i][0] = (data[i, 1] - yMin) * yFactor - 0.85;
}
// create multi-layer neural network
ActivationNetwork network = new ActivationNetwork(
new BipolarSigmoidFunction(sigmoidAlphaValue),
1, neuronsInFirstLayer, 1);
if (useNguyenWidrow)
{
NguyenWidrow initializer = new NguyenWidrow(network);
initializer.Randomize();
}
// create teacher
var teacher = new ParallelResilientBackpropagationLearning(network);
// iterations
int iteration = 1;
// solution array
double[,] solution = new double[50, 2];
double[] networkInput = new double[1];
// calculate X values to be used with solution function
for (int j = 0; j < 50; j++)
{
solution[j, 0] = chart.RangeX.Min + (double)j * chart.RangeX.Length / 49;
}
// loop
while (!needToStop)
{
// run epoch of learning procedure
double error = teacher.RunEpoch(input, output) / samples;
// calculate solution
for (int j = 0; j < 50; j++)
{
networkInput[0] = (solution[j, 0] - xMin) * xFactor - 1.0;
solution[j, 1] = (network.Compute(networkInput)[0] + 0.85) / yFactor + yMin;
}
chart.UpdateDataSeries("solution", solution);
// calculate error
double learningError = 0.0;
for (int j = 0, k = data.GetLength(0); j < k; j++)
{
networkInput[0] = input[j][0];
learningError += Math.Abs(data[j, 1] - ((network.Compute(networkInput)[0] + 0.85) / yFactor + yMin));
}
// set current iteration's info
SetText(currentIterationBox, iteration.ToString());
SetText(currentErrorBox, learningError.ToString("F3"));
// increase current iteration
iteration++;
// check if we need to stop
if ((iterations != 0) && (iteration > iterations))
break;
}
// enable settings controls
EnableControls(true);
}