// Constructor public MainForm() { InitializeComponent(); // Create network network = new DistanceNetwork(3, 100 * 100); // Create map bitmap mapBitmap = new Bitmap(200, 200, PixelFormat.Format24bppRgb); // RandomizeNetwork(); UpdateSettings(); }
// Update map private void UpdateMap(DistanceNetwork network) { // get first layer Layer layer = network.Layers[0]; // lock Monitor.Enter(this); // run through all neurons for (int i = 0; i < layer.Neurons.Length; i++) { Neuron neuron = layer.Neurons[i]; int x = i % networkSize; int y = i / networkSize; map[y, x, 0] = (int)neuron.Weights[0]; map[y, x, 1] = (int)neuron.Weights[1]; map[y, x, 2] = 0; } // collect active neurons for (int i = 0; i < pointsCount; i++) { network.Compute(trainingSet[i]); int w = network.GetWinner(); map[w / networkSize, w % networkSize, 2] = 1; } // unlock Monitor.Exit(this); // mapPanel.Invalidate(); }
// Worker thread void SearchSolution() { // create network DistanceNetwork network = new DistanceNetwork(2, networkSize * networkSize); // set random generators range foreach (var layer in network.Layers) foreach (var neuron in layer.Neurons) neuron.RandGenerator = new UniformContinuousDistribution( new Range(0, Math.Max(pointsPanel.ClientRectangle.Width, pointsPanel.ClientRectangle.Height))); // create learning algorithm SOMLearning trainer = new SOMLearning(network, networkSize, networkSize); // create map map = new int[networkSize, networkSize, 3]; double fixedLearningRate = learningRate / 10; double driftingLearningRate = fixedLearningRate * 9; // iterations int i = 0; // loop while (!needToStop) { trainer.LearningRate = driftingLearningRate * (iterations - i) / iterations + fixedLearningRate; trainer.LearningRadius = (double)learningRadius * (iterations - i) / iterations; // run training epoch trainer.RunEpoch(trainingSet); // update map UpdateMap(network); // increase current iteration i++; // set current iteration's info SetText(currentIterationBox, i.ToString()); // stop ? if (i >= iterations) break; } // enable settings controls EnableControls(true); }
// Worker thread void SearchSolution() { // create network DistanceNetwork network = new DistanceNetwork(2, neurons); // set random generators range foreach (var layer in network.Layers) foreach (var neuron in layer.Neurons) neuron.RandGenerator = new UniformContinuousDistribution(new Range(0, 1000)); // create learning algorithm ElasticNetworkLearning trainer = new ElasticNetworkLearning(network); double fixedLearningRate = learningRate / 20; double driftingLearningRate = fixedLearningRate * 19; // path double[,] path = new double[neurons + 1, 2]; // input double[] input = new double[2]; // iterations int i = 0; // loop while (!needToStop) { // update learning speed & radius trainer.LearningRate = driftingLearningRate * (iterations - i) / iterations + fixedLearningRate; trainer.LearningRadius = learningRadius * (iterations - i) / iterations; // set network input int currentCity = rand.Next(citiesCount); input[0] = map[currentCity, 0]; input[1] = map[currentCity, 1]; // run one training iteration trainer.Run(input); // show current path for (int j = 0; j < neurons; j++) { path[j, 0] = network.Layers[0].Neurons[j].Weights[0]; path[j, 1] = network.Layers[0].Neurons[j].Weights[1]; } path[neurons, 0] = network.Layers[0].Neurons[0].Weights[0]; path[neurons, 1] = network.Layers[0].Neurons[0].Weights[1]; chart.UpdateDataSeries("path", path); // increase current iteration i++; // set current iteration's info SetText(currentIterationBox, i.ToString()); // stop ? if (i >= iterations) break; } // enable settings controls EnableControls(true); }