示例#1
0
        /// <summary>
        /// Before we start GP prepare all neccessery information
        /// </summary>
        /// <param name="termSet"></param>
        /// <param name="funSet"></param>
        /// <param name="annParams"></param>
        public override void PrepareAlgorithm(Experiment expData, ANNParameters annParams = null)
        {
            if (annParams == null || expData == null)
            {
                throw new Exception("Argument value cannot be null");
            }


            //reset iteration and network
            if (m_Network == null)
            {
                m_IterationCounter = 0;
                m_Network          = new BPNeuralNetwork(annParams, expData.GetColumnInputCount_FromNormalizedValue(), expData.GetColumnOutputCount_FromNormalizedValue());
                m_Network.InitializeNetwork();
            }

            m_Experiment = expData;
            m_Parameters = annParams;

            IsAlgorthmPrepared = true;
            StopIteration      = false;

            m_expRowCount = m_Experiment.GetRowCount();

            //Send report for iteration
            var rp = new ProgressIndicatorEventArgs()
            {
                ReportType       = ProgramState.Started,
                LearningError    = -1,
                CurrentIteration = 0,
                LearnOutput      = null,
            };

            ReportProgress(rp);
        }
示例#2
0
        public void PrepareAlgorithm(Experiment expData, NeuralNetwork ann)
        {
            if (ann == null || expData == null)
            {
                throw new Exception("Argument value cannot be null");
            }

            //preparing the variables
            m_Network          = ann;
            m_Experiment       = expData;
            m_Parameters       = m_Network.Parameters;
            m_IterationCounter = 0;

            IsAlgorthmPrepared = true;
            StopIteration      = false;

            //report
            var rp = new ProgressIndicatorEventArgs()
            {
                ReportType       = ProgramState.Started,
                LearningError    = float.MaxValue,
                CurrentIteration = 0,
                LearnOutput      = null,
            };

            ReportProgress(rp);
        }
示例#3
0
 protected void ReportProgress(ProgressIndicatorEventArgs rp)
 {
     //Report the iteration is ready to start
     if (ReportIteration != null)
     {
         ReportIteration(this, rp);
     }
 }
示例#4
0
        protected override void FinishIteration()
        {
            //Send report for iteration
            var rp = new ProgressIndicatorEventArgs()
            {
                ReportType       = ProgramState.Finished,
                LearningError    = m_ExpectedValue,
                CurrentIteration = m_IterationCounter,
                LearnOutput      = null,
            };

            ReportProgress(rp);
        }
示例#5
0
        public override float CalculateModel(ProgramState state = ProgramState.Running)
        {
            //prepare training result
            var model      = CalculateModel(false);
            var prediction = CalculateModel(true);

            //Send report for iteration
            var rp = new ProgressIndicatorEventArgs()
            {
                ReportType       = state,
                LearningError    = (float)m_psoAlgorithm.m_BestGlobalFitness,
                CurrentIteration = m_IterationCounter,
                LearnOutput      = model,
                PredicOutput     = prediction
            };

            ReportProgress(rp);
            return(0);
        }
示例#6
0
        internal override float RunIteration()
        {
            //calculate model
            var newFitness = m_psoAlgorithm.RunSwarm();
            var model      = CalculateModel(false);
            var prediction = CalculateModel(true);

            //Send report for iteration
            var rp = new ProgressIndicatorEventArgs()
            {
                ReportType       = ProgramState.Running,
                LearningError    = (float)newFitness,
                CurrentIteration = m_IterationCounter,
                LearnOutput      = model,
                PredicOutput     = prediction
            };

            ReportProgress(rp);

            return(0);
        }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="strFile"></param>
        /// <returns></returns>
        private bool Open(string strFile)
        {
            if (!File.Exists(strFile))
            {
                MessageBox.Show("File doesnt exist!");
                return(false);
            }

            _filePath = strFile;

            string buffer;

            ResetProgram();
            // open selected file and retrieve the content
            using (StreamReader reader = System.IO.File.OpenText(strFile))
            {
                //read TrainingData in to buffer
                buffer = reader.ReadToEnd();
                reader.DiscardBufferedData();
                //reader.Close();
            }

            //define the lines from file
            var lines = (from l in buffer.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
                         where l[0] != '!' && l[0] != '\r'
                         select l.IndexOf('!') == -1 ? l : l.Remove(l.IndexOf('!'))
                         ).ToArray();

            if (lines == null || lines.Count() == 0)
            {
                MessageBox.Show("The file is empty!", Properties.Resources.SR_ApplicationName);

                return(false);
            }
            //Line 1: GPModel
            int mod = int.Parse(lines[0].Replace("\r", ""));

            _GPModel = (GPModelType)mod;

            //Optimization of analitic function is not implemented yet
            if (_GPModel == GPModelType.AO)
            {
                MessageBox.Show("Open Analytic function optimization file is not implemented.!");
                return(false);
            }

            //
            LoadModelWizard(_GPModel);


            //Line 2: Training data
            if (_dataPanel != null)
            {
                double[][] data = _dataPanel.loadData(lines[1].Replace("\r", ""));
                if (data != null)
                {
                    _dataPanel.LoadTrainingData(data, _GPModel);
                }
            }


            //Line 3: Testing data
            if (_dataPanel != null)
            {
                //
                double[][] data = _dataPanel.loadData(lines[2].Replace("\r", ""));
                if (data != null)
                {
                    _dataPanel.LoadTestingData(data);
                }
            }



            //Line 4: TimeSeries  data
            if (_dataPanel != null)
            {
                //
                double[][] data = _dataPanel.loadSeriesData(lines[3].Replace("\r", ""));
                if (data != null)
                {
                    _dataPanel.LoadSeriesData(data);
                }
            }

            //Line 4: Experiment Data - suported in V4.0
            if (_experimentPanel != null)
            {
                _experimentPanel.ExperimentFromString(lines[1].Replace("\r", ""));
                //Events from experiment panel about loading dataset
                if (_experimentPanel != null)
                {
                    _experimentPanel.StartModelling();
                }
            }

            //Line 5: GP Parameters
            if (_setPanel != null)
            {
                _setPanel.SetParameters(lines[4]);
            }
            else if (_setANNPanel != null)
            {
                _setANNPanel.SetParametersFromString(lines[4]);
            }
            else
            {
                throw new Exception("File is not valid.");
            }

            //Line 6:  GP Functions
            if (_funPanel != null)
            {
                _funPanel.SelectFunctions(lines[5]);
            }

            //Line 7: GP type of Running program
            if (_baseRunPanel != null)
            {
                _baseRunPanel.SetTypeofRun(lines[6]);
            }
            else if (_runANNPanel != null)
            {
                _runANNPanel.SetTypeofRun(lines[6]);
            }
            else
            {
                throw new Exception("File is not valid.");
            }


            int currentLine = 0;

            if (_mainGPFactory != null /* && _mainGPFactory.GetFunctionSet() != null */)
            {
                //Line 8 to Line8+popSize: Current GP Population
                currentLine = MainPopulationFromString(lines, _mainGPFactory, 6);
                if (_mainGPFactory.GetFunctionSet() != null)
                {
                    var e = new ProgressIndicatorEventArgs();
                    e.ReportType       = ProgramState.Finished;
                    e.CurrentIteration = 0;
                    e.BestChromosome   = _mainGPFactory.BestChromosome();
                    e.AverageFitness   = _mainGPFactory.GetAverageFitness();
                    if (_GPModel == GPModelType.GPMODEL)
                    {
                        e.LearnOutput  = _mainGPFactory.CalculateTrainModel(e.BestChromosome as GPChromosome);
                        e.PredicOutput = _mainGPFactory.CalculateTestModel(e.BestChromosome as GPChromosome);
                    }
                    //enable GP engine
                    _runningEngine = 1;
                    ReportEvolution(e);
                    _runningEngine = 0;
                }
            }
            else if (_mainANNFactory != null)
            {
                // Line 8 ANN weights
                PrepareANN();
                _mainANNFactory.LoadFactory(lines[7]);//load weights
                _mainANNFactory.CalculateModel(ProgramState.Finished);
                currentLine = 8;
            }
            else
            {
                currentLine++;
            }

            //If GA exist read maximum and minumum values of variables
            if (currentLine < lines.Length)
            {
                currentLine++;
                string ss = lines[currentLine];
                if (_optimizePanel != null)
                {
                    _optimizePanel.SetMaximumAndMinimumValues(ss);
                }
            }

            //Line 8+popSIze to Current GA Population if exist
            if (currentLine < lines.Length)
            {
                currentLine = SecondPopulationFromString(lines, _secondFactory, currentLine, 2);

                if (_secondFactory != null && _secondFactory.GetFunctionSet() != null)
                {
                    var e = new ProgressIndicatorEventArgs();
                    e.ReportType       = ProgramState.Finished;
                    e.CurrentIteration = 0;
                    e.BestChromosome   = _secondFactory.BestChromosome();
                    e.AverageFitness   = _secondFactory.GetAverageFitness();
                    //enable GP engine
                    _runningEngine = 2;
                    ReportEvolution(e);
                    _runningEngine = 0;
                }
            }

            //

            StringBuilder sb            = new StringBuilder();
            bool          isStartedLine = false;

            for (int i = currentLine; i < lines.Length; i++)
            {
                if (lines[i].StartsWith("{"))
                {
                    isStartedLine = true;
                }
                if (isStartedLine)
                {
                    sb.Append(lines[i]);
                }
            }
            var rtf = sb.ToString();

            if (rtf != null && rtf[0] != '-')
            {
                _infoPanel.InfoText = rtf;
            }


            _isFileDirty = false;
            return(true);
        }
示例#8
0
        public override float CalculateModel(ProgramState state = ProgramState.Running)
        {
            //prepare training result
            int outputCount = m_Experiment.GetColumnOutputCount();

            double[][] model = new double[outputCount][];

            for (int j = 0; j < outputCount; j++)
            {
                model[j] = new double[m_expRowCount];
            }

            var temp = m_ExpectedValue;

            // run learning procedure for all samples
            for (int i = 0; i < m_expRowCount; i++)
            {
                //retrieve input and output for specific row
                var input  = m_Experiment.GetNormalizedInput(i);
                var output = m_Experiment.GetRowFromOutput(i);

                // compute the network's output
                var norOut = m_Network.CalculateOutputs(input);

                //denormalize output
                var outVal = m_Experiment.GetDenormalizedOutputRow(norOut);
                for (int j = 0; j < outputCount; j++)
                {
                    model[j][i] = outVal[j];
                }

                //calculate learning error
                for (int j = 0; j < outputCount; j++)
                {
                    m_ExpectedValue += Math.Abs((float)(outVal[j] - output[j]));
                }
            }

            //prediction if exist
            double[][] prediction = null;
            if (m_Experiment.IsTestDataExist())
            {
                int testCount = m_Experiment.GetRowCount(true);

                if (testCount > 0)
                {
                    prediction = new double[outputCount][];

                    for (int j = 0; j < outputCount; j++)
                    {
                        prediction[j] = new double[testCount];
                    }

                    for (int i = 0; i < testCount; i++)
                    {
                        //retrieve input and output for specific row
                        var input = m_Experiment.GetNormalizedInput(i, true);

                        // compute the network's output
                        var norOut = m_Network.CalculateOutputs(input);

                        //denormalize output
                        var outVal = m_Experiment.GetDenormalizedOutputRow(norOut);
                        for (int j = 0; j < outputCount; j++)
                        {
                            prediction[j][i] = outVal[j];
                        }
                    }
                }
            }



            //Send report for iteration
            var rp = new ProgressIndicatorEventArgs()
            {
                ReportType       = state,
                LearningError    = m_ExpectedValue,
                CurrentIteration = m_IterationCounter,
                LearnOutput      = model,
                PredicOutput     = prediction
            };

            ReportProgress(rp);
            //return average error
            return(0);
        }
示例#9
0
        /// <summary>
        /// Before we start solver prepare all neccessery information
        /// </summary>
        /// <param name="termSet"></param>
        /// <param name="funSet"></param>
        /// <param name="annParams"></param>
        public override void PrepareAlgorithm(Experiment expData, ANNParameters annParams = null)
        {
            if (annParams == null || expData == null)
            {
                throw new Exception("Argument value cannot be null");
            }


            //reset iteration and network
            if (m_Network == null)
            {
                m_IterationCounter = 0;


                //depending on the type of the colum create adequate neural network
                var colType = expData.GetOutputColumnType();

                if (colType == ColumnDataType.Binary)//Binary Clasification
                {
                    m_Network = new BCNeuralNetwork(annParams, expData.GetColumnInputCount_FromNormalizedValue(), expData.GetColumnOutputCount_FromNormalizedValue());
                }
                else//multiclass classification
                {
                    m_Network = new MCNeuralNetwork(annParams, expData.GetColumnInputCount_FromNormalizedValue(), expData.GetColumnOutputCount_FromNormalizedValue());
                }

                m_Network.InitializeNetwork();
            }

            //
            m_Experiment = expData;

            PSOParameters swarm = null;

            if (m_Parameters != null)
            {
                swarm = m_Parameters.m_PSOParameters;
            }
            else
            {
                m_psoAlgorithm = null;
                swarm          = annParams.m_PSOParameters;
            }


            //
            m_Parameters = annParams;
            m_Parameters.m_PSOParameters = swarm;

            m_expRowCount      = m_Experiment.GetRowCount();
            IsAlgorthmPrepared = true;
            StopIteration      = false;

            float newfitness = 0;

            if (m_psoAlgorithm == null)
            {
                //initilaize swarm
                m_Parameters.m_PSOParameters.m_Dimension = m_Network.GetWeightsAndBiasCout();
                m_psoAlgorithm = new ParticleSwarm(m_Parameters.m_PSOParameters, CrossEntropy);
                //init
                newfitness = m_psoAlgorithm.InitSwarm();
            }
            // else
            newfitness = m_psoAlgorithm.RunSwarm();


            var model = CalculateModel(false);

            //Send report for iteration
            var rp = new ProgressIndicatorEventArgs()
            {
                ReportType       = ProgramState.Started,
                LearningError    = newfitness,
                CurrentIteration = 0,
                LearnOutput      = model,
            };

            ReportProgress(rp);
        }