/// <summary> /// Resets programs /// </summary> private void ResetProgram() { //unscribe to events if (_mainGPFactory != null) { this._mainGPFactory.ReportEvolution -= new EvolutionHandler(gpFactory_ReportEvolution); } if (_secondFactory != null) { this._secondFactory.ReportEvolution -= new EvolutionHandler(gpFactory_ReportEvolution); } _mainGPFactory = null; _secondFactory = null; }
/// <summary> /// /// </summary> /// <param name="factory"></param> /// <returns></returns> private string[] PreparePopulationForSave(GPFactory factory) { string[] str; if (factory != null) { //var pop = factory.GetPopulation(); var popSize = factory.GetpopSize(); var chroms = factory.GetChromosomes(); var best = factory.BestChromosome(); if (popSize == 0) { str = new string[popSize + 1]; str[0] = "0;-"; return(str); } str = new string[popSize + 1]; str[0] = popSize.ToString() + ";"; str[0] += best == null ? "-;-" : best.ToString(); for (int i = 0; i < popSize; i++) { if (i < chroms.Count) { str[i + 1] = chroms[i].ToString(); } else { str[i + 1] = "0;1000"; } } return(str); } else { return(null); } }
/// <summary> /// /// </summary> /// <param name="lines"></param> /// <param name="factory"></param> /// <param name="curLine"></param> /// <param name="typeChromosome"></param> /// <returns></returns> private int SecondPopulationFromString(string[] lines, GPFactory factory, int curLine, int typeChromosome = 1) { curLine++; //Line 8: populationSize; maxFitness; BestChromosome if (lines.Length <= curLine) { //MessageBox.Show("Fie is corrupt!"); return(-1); } var str = lines[curLine].Split(';'); if (lines[curLine] == "-" || lines[curLine] == "-\r") { return(curLine += 3); } //first number is popSIze int popSize = 0; if (!int.TryParse(str[0], out popSize)) { popSize = 0; } if (_optimizePanel != null) { //last number i +s optimization type string optType = str[str.Length - 1]; if (optType == "0" || optType == "0\r") { _optimizePanel.SetOptType(false); } else { _optimizePanel.SetOptType(true); } if (typeChromosome == 1) { PrepareGP(false); } else if (typeChromosome == 2) { PrepareGA(false); GANumChromosome.functionSet = factory.GetFunctionSet(); } } curLine++; //Get Optimization function List <IChromosome> chromosomes = new List <IChromosome>(); curLine++; for (int i = 0; i < popSize; i++) { var ch = GANumChromosome.CreateFromString(lines[i + curLine]); chromosomes.Add(ch); } if (factory != null) { factory.SetChromosomes(chromosomes); factory.CalculatePopulation(); } return(popSize == 0?2:popSize + curLine); }
/// <summary> /// /// </summary> /// <param name="lines"></param> /// <param name="factory"></param> /// <param name="curLine"></param> /// <param name="typeChromosome"></param> /// <returns></returns> private int MainPopulationFromString(string[] lines, GPFactory factory, int curLine) { curLine++; //Line 8: populationSize; maxFitness; BestChromosome if (lines.Length <= curLine) { //MessageBox.Show("Fie is corrupt!"); return(-1); } var str = lines[curLine].Split(';'); if (lines[curLine] == "-" || lines[curLine] == "-\r") { return(curLine += 2); } if (_GPModel == GPModelType.TSP) { PrepareTSP(false); } else if (_GPModel == GPModelType.AP) { PrepareALOC(false); } else if (_GPModel == GPModelType.TP) { PrepareTrans(false); } else { PrepareGP(false); } int popSize = 0; if (!int.TryParse(str[0], out popSize)) { popSize = 0; } List <IChromosome> chromosomes = new List <IChromosome>(); for (int i = 0; i < popSize; i++) { IChromosome ch = null; if (_GPModel == GPModelType.TSP || _GPModel == GPModelType.AP) { ch = GAVChromosome.CreateFromString(lines[i + curLine + 1]); } else if (_GPModel == GPModelType.TP) { ch = GAMChromosome.CreateFromString(lines[i + curLine + 1]); } else { ch = GPChromosome.CreateFromString(lines[i + curLine + 1]); } chromosomes.Add(ch); } factory.SetChromosomes(chromosomes); factory.CalculatePopulation(); return(popSize + 7); }
/// <summary> /// /// </summary> /// <param name="strFile"></param> /// <returns></returns> bool SaveToFile(string strFile) { if (strFile == null) { return(false); } if (_setPanel == null && _setANNPanel == null) { return(false); } if ((Globals.gpterminals == null || Globals.gpterminals.TrainingData == null) && _GPModel != GPModelType.ANNMODEL) { MessageBox.Show("Cannot save empty model!"); return(false); } //Optimization of analitic function is not implemented yet if (_GPModel == GPModelType.AO) { MessageBox.Show("Saving Analytic function optimization model is not supported.!"); return(false); } // open selected file and retrieve the content using (TextWriter tw = new StreamWriter(strFile)) { tw.Flush(); //Line1: Moel type and header information //tw.WriteLine("!GPdotNET v2.0 File format "); tw.WriteLine("!GPdotNET v4.0 File format "); tw.WriteLine("!"); tw.WriteLine("!line 1: GP Model 1- symbolic regression; 2 -symbolic regression with optimisation, 3 - time series, 4- analytic optimisation, 5- TSP, 6-AP ,7-TP, 8-ANNMODEL, 9 - GPMODEL"); int model = (int)_GPModel; tw.WriteLine(model.ToString()); string data = null; //Line2: Training DATA if (model < 8) { tw.WriteLine("!line 2 Training Data"); if (_dataPanel != null) { data = _dataPanel.GetStringFromData(_dataPanel.Training); if (data == null) { tw.WriteLine("-"); } else { tw.WriteLine(data); } } else { tw.WriteLine("-"); } } if (model < 8) { //Line3: Teting DATA tw.WriteLine("!line 3 Testing Data"); if (_dataPanel != null) { data = _dataPanel.GetStringFromData(_dataPanel.Testing); if (data == null) { tw.WriteLine("-"); } else { tw.WriteLine(data); } } else { tw.WriteLine("-"); } } if (model < 8) { //Line4: Series DATA tw.WriteLine("!line 4 TimeSeries Data"); if (_dataPanel != null) { data = _dataPanel.GetStringFromData(_dataPanel.TimeSeries); if (data == null) { tw.WriteLine("-"); } else { tw.WriteLine(data); } } else { tw.WriteLine("-"); } } if (model >= 8) { //Line4: Series DATA tw.WriteLine("!line 4 Experimental Data"); if (_experimentPanel != null) { data = _experimentPanel.ExperimentToString(); if (data == null) { tw.WriteLine("-"); } else { tw.WriteLine(data); } } else { tw.WriteLine("-"); } tw.WriteLine("!line 3 Training Data"); tw.WriteLine("-"); tw.WriteLine("!line 4 Testing Data"); tw.WriteLine("-"); } //Line 5: GP Parameters tw.WriteLine("!line 5: GP Parameters are sorted in the following order"); tw.WriteLine("!popSIze;Fitness;Initialization;InitDepth;OperationDept;Elitism;Sel Method;Param1;Param2;Const_From;COnst_To;Con_COut;CrossOverProb;MutatProb;SeleProb;PermutationProb;EncaptulationProb;EnableEditing;EnableDecimation"); data = _setPanel != null?_setPanel.ParametersToString() : _setANNPanel.ParametersToString(); if (data == null) { tw.WriteLine("-"); } else { tw.WriteLine(data); } //Line 6: GP Functions tw.WriteLine("!line 6:- Selected Function"); data = _funPanel == null?null: _funPanel.GetFunctionState(); if (data == null) { tw.WriteLine("-"); } else { tw.WriteLine(data); } //Line 7: GP type of Running program tw.WriteLine("!line 7 Type of Running program 0- means generation number, 1 - fitness value ;"); tw.WriteLine("! e.g. 1;700 - run program until max fitness is greate or equel than 700 "); tw.WriteLine("! 0;500 - run program for 500 evolutions "); if (_baseRunPanel != null)//GP module { data = _baseRunPanel.GetTypeofRun(); if (data == null) { tw.WriteLine("-"); } else { tw.WriteLine(data); } } else if (_runANNPanel != null)//ANN module { data = _runANNPanel.GetTypeofRun(); if (data == null) { tw.WriteLine("-"); } else { tw.WriteLine(data); } } else { tw.WriteLine("-"); } if (_baseRunPanel != null) { //Line 8 to Line8+popSize: Current GP Population tw.WriteLine("!Line 8 Population: size;bestfitness:bestchromosometree"); GPFactory fac = _mainGPFactory; var str = PreparePopulationForSave(fac); if (str != null && str.Length > 0) { tw.WriteLine(str[0]); } else { tw.WriteLine("-"); } if (str != null && str.Length > 1) { tw.WriteLine("!Line represent chromosomes in population"); for (int i = 1; i < str.Length; i++) { tw.WriteLine(str[i]); } } else { tw.WriteLine("-"); } } else if (_runANNPanel != null)//ANN modul { tw.WriteLine("!Line 8 AnnFactory: weights"); var fac = _mainANNFactory; var str = fac.SaveFactory(); tw.WriteLine(str); tw.WriteLine("-"); } else { tw.WriteLine("!Line 8 Population: size;bestfitness:bestchromosometree"); tw.WriteLine("-"); tw.WriteLine("!Line represent chromosomes in population"); tw.WriteLine("-"); } //Next Line GA Population if exist if (_optimizePanel != null) { tw.WriteLine("!Line GA Terminals: maximum and minimum values of variables"); string st = PrepareMinMaxValues(); if (st != null) { tw.WriteLine(st); } else { tw.WriteLine("-"); } tw.WriteLine("!Line GA Population: size; bestfitness: bestchromosometree; typeOfOptimization;"); string[] str = PreparePopulationForSave(_secondFactory); if (str != null && str.Length > 0) { //wnen working with optimization wee need to know is Maximum or Minimum var s = str[0] /*+ ";"*/ + (_optimizePanel.IsMinimum() ? "1" : "0"); tw.WriteLine(s); //store function for optimization tw.WriteLine("!Line GA Function to optimize"); if (_GPModel == GPModelType.SRO) { var ch = _secondFactory.BestChromosome() as GPChromosome; if (ch != null) { s = ch.GetExpression().ToString(); } else { s = "-"; } } else if (_GPModel == GPModelType.AO) { s = _funDefinit.TreeNodeToGPNode().ToString(); } else { throw new Exception("Wrong GPModel type!"); } //write to file tw.WriteLine(s); } else { tw.WriteLine("-"); } if (str != null && str.Length > 1) { tw.WriteLine("!Line represent GA chromosomes in population"); for (int i = 1; i < str.Length; i++) { tw.WriteLine(str[i]); } } else { tw.WriteLine("-"); } } else { tw.WriteLine("!Line GA Terminals: maximum and minimum values of variables"); tw.WriteLine("-"); tw.WriteLine("!Line GA Population: size; bestfitness: bestchromosometree; typeOfOptimization; functionToOptimize"); tw.WriteLine("-"); tw.WriteLine("!Line GA Function to optimize"); tw.WriteLine("-"); tw.WriteLine("!Line represent GA chromosomes in population"); tw.WriteLine("-"); } //RTF Model Info tw.WriteLine("!Line RTF text represent Model Info"); tw.WriteLine(_infoPanel.InfoText); // tw.Close(); _isFileDirty = false; return(true); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void _experimentPanel_DataLoaded(object sender, EventArgs e) { //Depending on experimental type of output data (numerical or categorical or boolean) // prepare the panels for parameters and type of the factory. if (_GPModel == GPModelType.ANNMODEL) { //create facotry based on column data type if (_experimentPanel.IsCategoricalOutput) { this._mainANNFactory = new PSOFactory(); } else { this._mainANNFactory = new BPFactory(); } // if (_setANNPanel == null) { _setANNPanel = new ANNSettingsPanel(); loadGPPanelInMainWindow(this, _setANNPanel, "Settings"); //lock some parameters based on type of output variable if (_experimentPanel.Experiment.GetOutputColumnType() == Core.Experiment.ColumnDataType.Binary || _experimentPanel.Experiment.GetOutputColumnType() == Core.Experiment.ColumnDataType.Categorical ) { _setANNPanel.SetLearnigAlgorithm(1); } else { _setANNPanel.SetLearnigAlgorithm(0); } _setANNPanel.LockLearningAlgoritm(); } if (_runANNPanel == null) { _runANNPanel = new ANNRunPanel(); loadGPPanelInMainWindow(this, _runANNPanel, "Modeling"); } if (_infoPanel == null) { _infoPanel = new InfoPanel(); loadGPPanelInMainWindow(this, _infoPanel, "Info"); } if (_runANNPanel != null) { this._mainANNFactory.ReportIteration += new EvolutionHandler(annFactory_ReportIteration); _runANNPanel.UpdateChartDataPoint(_experimentPanel.GetOutputValues(), false); _isFileDirty = true; } } else//Preparing GP for modelling nd prediction { //create facotry based on column data type if (_experimentPanel.IsBinarylOutput || _experimentPanel.IsCategoricalOutput) { this._mainGPFactory = new GPFactoryClass(); } else if (_experimentPanel.GetOutputColumnType() == Core.Experiment.ColumnDataType.Numeric) { this._mainGPFactory = new GPFactory(); } else { throw new Exception("Unknown output value type!"); } if (_funPanel == null) { _funPanel = new FunctionPanel(); loadGPPanelInMainWindow(this, _funPanel, "Functions"); } if (_setPanel == null) { _setPanel = new SettingsPanel(); loadGPPanelInMainWindow(this, _setPanel, "Settings"); } //set problem type _setPanel.SetParamForClassification(_experimentPanel.GetOutputColumnType()); if (_runPanel == null) { _runPanel = new RunPanel(); loadGPPanelInMainWindow(this, _runPanel, "Run"); } if (_resultPanel == null) { _resultPanel = new ResultPanel(); loadGPPanelInMainWindow(this, _resultPanel, "Result"); } if (_infoPanel == null) { _infoPanel = new InfoPanel(); loadGPPanelInMainWindow(this, _infoPanel, "Info"); } //set base run panel on _runPAnel _baseRunPanel = _runPanel; if (_runPanel != null) { this._mainGPFactory.ReportEvolution += new EvolutionHandler(gpFactory_ReportEvolution); _runPanel.ResetSolution(); _runPanel.UpdateChartDataPoint(_experimentPanel.GetOutputValues(), false); _isFileDirty = true; } if (_funDefinit != null) { _funDefinit.btnFinishAnalFun.Click += btnFinishAnalFun_Click; } if (_setPanel != null) { _setPanel.ResetSolution += _setPanel_ResetSolution; } } }
/// <summary> /// /// </summary> /// <param name="model"></param> void LoadModelWizard(GPModelType model) { _GPModel = model; switch (_GPModel) { case GPModelType.SR: { _dataPanel = new DataPanel(); loadGPPanelInMainWindow(this, _dataPanel, "Load Data"); _dataPanel.SetProblemType(_GPModel); _funPanel = new FunctionPanel(); loadGPPanelInMainWindow(this, _funPanel, "Functions"); _setPanel = new SettingsPanel(); loadGPPanelInMainWindow(this, _setPanel, "Settings"); _runPanel = new RunPanel(); loadGPPanelInMainWindow(this, _runPanel, "Run"); _resultPanel = new ResultPanel(); loadGPPanelInMainWindow(this, _resultPanel, "Result"); _infoPanel = new InfoPanel(); loadGPPanelInMainWindow(this, _infoPanel, "Info"); this._mainGPFactory = new GPFactory(); this._mainGPFactory.ReportEvolution += new EvolutionHandler(gpFactory_ReportEvolution); //set base run panel on _runPAnel _baseRunPanel = _runPanel; } break; case GPModelType.SRO: { _dataPanel = new DataPanel(); loadGPPanelInMainWindow(this, _dataPanel, "Load Data"); _dataPanel.SetProblemType(_GPModel); _funPanel = new FunctionPanel(); loadGPPanelInMainWindow(this, _funPanel, "Functions"); _setPanel = new SettingsPanel(); loadGPPanelInMainWindow(this, _setPanel, "Settings"); _runPanel = new RunPanel(); loadGPPanelInMainWindow(this, _runPanel, "Run"); _optimizePanel = new OptimizePanel(); loadGPPanelInMainWindow(this, _optimizePanel, "Optimize Model"); _resultPanel = new ResultPanel(); loadGPPanelInMainWindow(this, _resultPanel, "Result"); _infoPanel = new InfoPanel(); loadGPPanelInMainWindow(this, _infoPanel, "Info"); this._mainGPFactory = new GPFactory(); this._mainGPFactory.ReportEvolution += new EvolutionHandler(gpFactory_ReportEvolution); this._secondFactory = new GPFactory(); this._secondFactory.ReportEvolution += new EvolutionHandler(gpFactory_ReportEvolution); //set base run panel on _runPAnel _baseRunPanel = _runPanel; } break; case GPModelType.TS: { _dataPanel = new DataPanel(); loadGPPanelInMainWindow(this, _dataPanel, "Load Data"); _dataPanel.SetProblemType(_GPModel); _funPanel = new FunctionPanel(); loadGPPanelInMainWindow(this, _funPanel, "Functions"); _setPanel = new SettingsPanel(); loadGPPanelInMainWindow(this, _setPanel, "Settings"); _setPanel.SetNumOfConstance(0); _runPanel = new RunPanel(); loadGPPanelInMainWindow(this, _runPanel, "Run"); _resultPanel = new ResultPanel(); loadGPPanelInMainWindow(this, _resultPanel, "Result"); _infoPanel = new InfoPanel(); loadGPPanelInMainWindow(this, _infoPanel, "Info"); this._mainGPFactory = new GPFactory(); this._mainGPFactory.ReportEvolution += new EvolutionHandler(gpFactory_ReportEvolution); //set base run panel on _runPAnel _baseRunPanel = _runPanel; } break; case GPModelType.AO: { _funDefinit = new AnaliticFunctionDef(); loadGPPanelInMainWindow(this, _funDefinit, "Analytic function"); _funPanel = new FunctionPanel(); loadGPPanelInMainWindow(this, _funPanel, "Functions"); _setPanel = new SettingsPanel(); loadGPPanelInMainWindow(this, _setPanel, "Settings"); _setPanel.ShowGAParams(); _optimizePanel = new OptimizePanel(); loadGPPanelInMainWindow(this, _optimizePanel, "Optimize Model"); //initi pages after creation // _funDefinit.LoadFuns(_funPanel.GPFunctions); tabControl1.TabPages.RemoveAt(1); this._secondFactory = new GPFactory(); this._secondFactory.ReportEvolution += new EvolutionHandler(gpFactory_ReportEvolution); //set base run panel on _optimizePanel _baseRunPanel = _optimizePanel; } break; case GPModelType.TSP: { _dataPanel = new DataPanel(); loadGPPanelInMainWindow(this, _dataPanel, "Load Data"); _dataPanel.SetProblemType(_GPModel); _setPanel = new SettingsPanel(); loadGPPanelInMainWindow(this, _setPanel, "Settings"); _setPanel.ShowGAParams(); var p = _setPanel.GetParameters(); p.popSize = 2500; p.eselectionMethod = GPSelectionMethod.SkrgicSelection; p.SelParam1 = 2.5f; _setPanel.SetParameters(p); _tspPanel = new TSPRunPanel(); loadGPPanelInMainWindow(this, _tspPanel, "Simulation"); this._mainGPFactory = new GPFactory(); this._mainGPFactory.ReportEvolution += new EvolutionHandler(gpFactory_ReportEvolution); _infoPanel = new InfoPanel(); loadGPPanelInMainWindow(this, _infoPanel, "Info"); //set base run panel on _TSPPanel _baseRunPanel = _tspPanel; } break; case GPModelType.AP: { _dataPanel = new DataPanel(); loadGPPanelInMainWindow(this, _dataPanel, "Load Data"); _dataPanel.SetProblemType(_GPModel); _setPanel = new SettingsPanel(); loadGPPanelInMainWindow(this, _setPanel, "Settings"); _setPanel.ShowGAParams(); var p = _setPanel.GetParameters(); p.popSize = 2500; p.eselectionMethod = GPSelectionMethod.SkrgicSelection; p.SelParam1 = 2.5f; _setPanel.SetParameters(p); _alocPanel = new ALOCRunPanel(); loadGPPanelInMainWindow(this, _alocPanel, "Simulation"); this._mainGPFactory = new GPFactory(); this._mainGPFactory.ReportEvolution += new EvolutionHandler(gpFactory_ReportEvolution); _infoPanel = new InfoPanel(); loadGPPanelInMainWindow(this, _infoPanel, "Info"); //set base run panel on _TSPPanel _baseRunPanel = _alocPanel; } break; case GPModelType.TP: { _dataPanel = new DataPanel(); loadGPPanelInMainWindow(this, _dataPanel, "Load Data"); _dataPanel.SetProblemType(_GPModel); _setPanel = new SettingsPanel(); loadGPPanelInMainWindow(this, _setPanel, "Settings"); _setPanel.ShowGAParams(); var p = _setPanel.GetParameters(); p.popSize = 2500; p.eselectionMethod = GPSelectionMethod.SkrgicSelection; p.SelParam1 = 2.5f; _setPanel.SetParameters(p); _alocPanel = new ALOCRunPanel(); loadGPPanelInMainWindow(this, _alocPanel, "Simulation"); this._mainGPFactory = new GPFactory(); this._mainGPFactory.ReportEvolution += new EvolutionHandler(gpFactory_ReportEvolution); _infoPanel = new InfoPanel(); loadGPPanelInMainWindow(this, _infoPanel, "Info"); //set base run panel on _TSPPanel _baseRunPanel = _alocPanel; } break; case GPModelType.ANNMODEL: case GPModelType.GPMODEL: { _experimentPanel = new ExperimentPanel(); loadGPPanelInMainWindow(this, _experimentPanel, "Load Experiment"); _experimentPanel.SetProblemType(_GPModel); //factory creating moved on place when the experimental data is created and prepared //this._mainANNFactory = new ANNFactory(); //this._mainANNFactory.ReportIteration += new EvolutionHandler(annFactory_ReportIteration); } break; default: break; } //Events from datapanel about loading dat if (_dataPanel != null) { _dataPanel.DataLoaded += _dataPanel_DataLoaded; } if (_dataPanel != null) { _dataPanel.DataPredictionLoaded += _dataPanel_DataPredictionLoaded; } //Events from experiment panel about loading dat if (_experimentPanel != null) { _experimentPanel.DataLoaded += _experimentPanel_DataLoaded; } if (_experimentPanel != null) { _experimentPanel.DataPredictionLoaded += _experimentalPanel_DataPredictionLoaded; } if (_funDefinit != null) { _funDefinit.btnFinishAnalFun.Click += btnFinishAnalFun_Click; } if (_setPanel != null) { _setPanel.ResetSolution += _setPanel_ResetSolution; } }