Example #1
0
        public void Analyze(IForecastingDataSets datasets)
        {
            if (ModelStartRunning != null)
            {
                ModelStartRunning(this, new ComponentRunEventArgs(datasets));
            }
            int learningRadius = Math.Max(mSOMParameter.LayerWidth, mSOMParameter.LayerHeight) / 2;

            KohonenLayer inputLayer  = new KohonenLayer(datasets.InputData[0].Length);
            KohonenLayer outputLayer = new KohonenLayer(new Size(mSOMParameter.LayerWidth, mSOMParameter.LayerHeight),
                                                        mSOMParameter.NeighborhoodFunction, mSOMParameter.Topology);
            KohonenConnector connector = new KohonenConnector(inputLayer, outputLayer);

            connector.Initializer = new RandomFunction(0, 100);
            outputLayer.SetLearningRate(mSOMParameter.LearningRate, mSOMParameter.FinalLearningRate);
            outputLayer.IsRowCircular    = mSOMParameter.IsRowCircular;
            outputLayer.IsColumnCircular = mSOMParameter.IsColumnCircular;
            mNetwork = new KohonenNetwork(inputLayer, outputLayer);

            mNetwork.EndEpochEvent += new TrainingEpochEventHandler(
                delegate(object senderNetwork, TrainingEpochEventArgs args)
            {
                if (ModelRunningEpoch != null)
                {
                    ModelRunningEpoch(this, new ComponentRunEpochEventArgs(args.TrainingIteration));
                }
            });
            mTrainingSet = ForecastingDataSets.ConvertToUnSupervisedTrainingSet(datasets);
            mNetwork.Learn(mTrainingSet, mSOMParameter.Iterations);

            if (ModelFinishRunning != null)
            {
                ModelFinishRunning(this, new ComponentRunEventArgs(datasets));
            }
        }
Example #2
0
        void Solve()
        {
            CrowNetP NetP = new CrowNetP();

            if (netUP.netType == "som")
            {
                #region self organizing maps

                #region prepare and assign
                trainingSet.Clear();
                int trainVectorDimension = 3;
                if (trainDataArePoints)
                {
                    for (int i = 0; i < pointsList.Count; i++)
                    {
                        trainingSet.Add(new TrainingSample(new double[] { pointsList[i].Value.X, pointsList[i].Value.Y, pointsList[i].Value.Z }));
                    }
                }
                else
                {
                    trainVectorDimension = trainingVectorTree.Branches[0].Count;
                    trainingSet          = new TrainingSet(trainVectorDimension);
                    for (int i = 0; i < trainingVectorTree.Branches.Count; i++)
                    {
                        double[] values = new double[trainVectorDimension];

                        for (int j = 0; j < trainVectorDimension; j++)
                        {
                            values[j] = trainingVectorTree.Branches[i][j].Value;
                        }

                        trainingSet.Add(new TrainingSample(values));
                    }
                }


                ///  process
                ///  start learning

                int learningRadius = Math.Max(layerWidth, layerHeight) / 2;

                INeighborhoodFunction neighborhoodFunction = new GaussianFunction(learningRadius, netUP.neighborDistance) as INeighborhoodFunction;
                if (neighborhood)
                {
                    neighborhoodFunction = new MexicanHatFunction(learningRadius) as INeighborhoodFunction;
                }

                LatticeTopology topology = LatticeTopology.Rectangular;
                if (latticeTopology)
                {
                    topology = LatticeTopology.Hexagonal;
                }

                KohonenLayer     inputLayer  = new KohonenLayer(trainVectorDimension);
                KohonenLayer     outputLayer = new KohonenLayer(new Size(layerWidth, layerHeight), neighborhoodFunction, topology);
                KohonenConnector connector   = new KohonenConnector(inputLayer, outputLayer);
                connector.Initializer = randomizer;

                outputLayer.SetLearningRate(learningRate, 0.05d);
                outputLayer.IsRowCircular    = isCircularRows;
                outputLayer.IsColumnCircular = isCircularColumns;
                network = new KohonenNetwork(inputLayer, outputLayer);
                network.useRandomTrainingOrder = opt.UseRandomTraining;
                #endregion

                #region delegates
                network.BeginEpochEvent += new TrainingEpochEventHandler(
                    delegate(object senderNetwork, TrainingEpochEventArgs args)
                {
                    #region TrainingCycle
                    if (network == null || !GO)
                    {
                        return;
                    }


                    int iPrev     = layerWidth - 1;
                    allValuesTree = new GH_Structure <GH_Number>();
                    for (int i = 0; i < layerWidth; i++)
                    {
                        for (int j = 0; j < layerHeight; j++)
                        {
                            IList <ISynapse> synapses = (network.OutputLayer as KohonenLayer)[i, j].SourceSynapses;
                            double x = synapses[0].Weight;
                            double y = synapses[1].Weight;
                            double z = synapses[2].Weight;

                            for (int k = 0; k < trainVectorDimension; k++)
                            {
                                allValuesTree.Append(new GH_Number(synapses[k].Weight), new GH_Path(i, j));
                            }

                            rowX[j][i]    = x;
                            rowY[j][i]    = y;
                            rowZ[j][i]    = z;
                            columnX[i][j] = x;
                            columnY[i][j] = y;
                            columnZ[i][j] = z;

                            if (j % 2 == 1)
                            {
                                hexagonalX[i][j] = x;
                                hexagonalY[i][j] = y;
                                hexagonalZ[i][j] = z;
                            }
                            else
                            {
                                hexagonalX[iPrev][j] = x;
                                hexagonalY[iPrev][j] = y;
                                hexagonalZ[iPrev][j] = z;
                            }
                        }
                        iPrev = i;
                    }

                    if (isCircularRows)
                    {
                        for (int i = 0; i < layerHeight; i++)
                        {
                            rowX[i][layerWidth] = rowX[i][0];
                            rowY[i][layerWidth] = rowY[i][0];
                            rowZ[i][layerWidth] = rowZ[i][0];
                        }
                    }

                    if (isCircularColumns)
                    {
                        for (int i = 0; i < layerWidth; i++)
                        {
                            columnX[i][layerHeight]    = columnX[i][0];
                            columnY[i][layerHeight]    = columnY[i][0];
                            columnZ[i][layerHeight]    = columnZ[i][0];
                            hexagonalX[i][layerHeight] = hexagonalX[i][0];
                            hexagonalY[i][layerHeight] = hexagonalY[i][0];
                            hexagonalZ[i][layerHeight] = hexagonalZ[i][0];
                        }
                    }

                    Array.Clear(isWinner, 0, layerHeight * layerWidth);

                    #endregion
                    NetP = new CrowNetP("som", layerWidth, layerHeight, isCircularRows, isCircularColumns, latticeTopology, neighborhood, isWinner, rowX, rowY, rowZ, columnX, columnY, columnZ, hexagonalX, hexagonalY, hexagonalZ, allValuesTree);
                    counter++;
                });

                network.EndSampleEvent += new TrainingSampleEventHandler(
                    delegate(object senderNetwork, TrainingSampleEventArgs args)
                {
                    isWinner[network.Winner.Coordinate.X, network.Winner.Coordinate.Y] = true;
                });
                #endregion

                #endregion
            }

            network.Learn(trainingSet, cycles);
        }