private double[][] Convert(DataSerieTD ds) { if (Equals(ds, null)) { return(null); } int iCount = ds.Data.Count; if (iCount < 1) { return(null); } int itmCount = ds.Data [0].List.Count(); double[][] result = new double[iCount][]; try { for (int i = 0; i < iCount; i++) { double[] dblList = new double[itmCount]; for (int j = 0; j < itmCount; j++) { dblList[j] = ds.Data[i].List[j]; } result[i] = dblList; } } catch (Exception ex) { throw ex; } return(result); }
static void LoadData(string file) { //string fileRoot = string.Format("{0}\\DataSource", System.IO.Directory.GetCurrentDirectory()); //string file = @"C:\Users\SD\Documents\Dataset_ANN_SVR\DataSet_Exemple.csv"; //Console.ReadLine(); if (System.IO.File.Exists(file) == false) { Console.WriteLine("No file [{0}] found ...", file); return; } CsvFileIO reader1 = new CsvFileIO(file); DataSet = reader1.Read_DST(false, false, true, false); if (Equals(DataSet, null)) { Console.WriteLine("No data set .."); return; } if (Equals(DataSet.Data, null)) { Console.WriteLine("No data .."); return; } Console.WriteLine("There is {0} records in : {1}", DataSet.GetRowsCount(), DataSet.Name); //Console.WriteLine(DataSet.ToString()); }
public void StandardizeData(DataSerieTD dseries, ActivationFunctionEnum activeFunction) { if (Equals(dseries, null)) { return; } if (Equals(dseries.Data, null)) { return; } bool doStand = false; switch (activeFunction) { case ActivationFunctionEnum.SigmoidFunction: foreach (DataItemTD ditem in dseries.Data) { foreach (double value in ditem.List) { if ((value < 0) || (value > 1)) { doStand = true; break; } } if (doStand) { break; } } if (doStand) { double[] mins = dseries.Min; double[] maxs = dseries.Max; int jCount = mins.Count(); foreach (DataItemTD ditem in dseries.Data) { for (int j = 0; j < jCount; j++) { ditem.List[j] = (ditem.List[j] - mins[j]) / (maxs[j] - mins[j]); } } } break; case ActivationFunctionEnum.BipolarSigmoidFunction: break; case ActivationFunctionEnum.LinearFunction: break; } }
public DataSerie1D Compute(DataSerieTD inputsDs) { if (Equals(inputsDs, null)) { return(null); } if (Equals(inputsDs.Data, null)) { return(null); } if (inputsDs.Data.Count < 1) { return(null); } if (Equals(Network, null)) { return(null); } StandardizeData(inputsDs, this.mActivationFunction); DataSerie1D resultDs = new DataSerie1D(); double[] result = null; int jCount = inputsDs.Data[0].List.Count(); if (jCount < 1) { return(null); } double[] input = new double[jCount]; foreach (DataItemTD dItem in inputsDs.Data) { for (int j = 0; j < jCount; j++) { input[j] = dItem.List[j]; } result = Network.Compute(input); resultDs.Add(dItem.Title, result[0]); } return(resultDs); }
/// <summary> /// Standardize Data between [0, 1]. /// </summary> /// <param name="dseries"></param> public void StandardizeData(DataSerieTD dseries) { if (Equals(dseries, null)) { return; } if (Equals(dseries.Data, null)) { return; } bool doStand = false; foreach (DataItemTD ditem in dseries.Data) { foreach (double value in ditem.List) { if ((value < 0) || (value > 1)) { doStand = true; break; } } if (doStand) { break; } } if (doStand) { double[] mins = dseries.Min; double[] maxs = dseries.Max; int jCount = mins.Count(); foreach (DataItemTD ditem in dseries.Data) { for (int j = 0; j < jCount; j++) { ditem.List[j] = (ditem.List[j] - mins[j]) / (maxs[j] - mins[j]); } } } }
static void LoadDataDST() { string fileRoot = string.Format("{0}\\DataSource", System.IO.Directory.GetCurrentDirectory()); string file1 = string.Format("{0}\\{1}", fileRoot, fileName_DST); if (System.IO.File.Exists(file1) == false) { Console.WriteLine("No file [{0}] found ...", file1); return; } CsvFileIO reader1 = new CsvFileIO(file1); DataSet = reader1.Read_DST(); if (Equals(DataSet, null)) { Console.WriteLine("No data set .."); return; } if (Equals(DataSet.Data, null)) { Console.WriteLine("No data .."); return; } Console.WriteLine(DataSet.ToString()); var x = DataSet.GetColumn(2); foreach (double itm in x) { Console.WriteLine(itm); } Console.WriteLine("There is {0} records in : {1}", DataSet.GetRowsCount(), DataSet.Name); }
public void Formate(bool includeMonths, int startingMonth) { if (includeMonths == false) { Formate(); } else { if (object.Equals(Data, null)) { return; } if (object.Equals(Data.Data, null)) { return; } int dataCount = Data.Count; if (dataCount < 1) { return; } TrainingCount = (int)((TrainingRate * dataCount) / 100); TestingCount = (dataCount - TrainingCount); mTrainingInputs = new DataSerieTD() { Name = "Training inputs" }; mTrainingOutputs = new DataSerie1D() { Name = "Training outputs" }; int monthIndex = startingMonth + mAnnInputLayerCount; double monthStand = 0; for (int i = 0; i < (TrainingCount - mAnnInputLayerCount); i++) { double[] qj = new double[mAnnInputLayerCount + 1]; for (int j = 0; j < mAnnInputLayerCount; j++) { qj[j] = Data.Data[(i + j)].X_Value; } //------------------------------------ monthStand = (GetMonthIndex(monthIndex) * 0.9) / 12; qj[mAnnInputLayerCount] = monthStand; monthIndex += 1; //------------------------------------ mTrainingInputs.Add(i.ToString(), qj); mTrainingOutputs.Add(i.ToString(), Data.Data[(i + mAnnInputLayerCount)].X_Value); } mTestingInputs = new DataSerieTD() { Name = "Testing inputs" }; mTestingOutputs = new DataSerie1D() { Name = "Testing outputs" }; int k = TrainingCount; for (int i = 0; i < (TestingCount - mAnnInputLayerCount); i++) { double[] qj = new double[mAnnInputLayerCount + 1]; for (int j = 0; j < mAnnInputLayerCount; j++) { qj[j] = Data.Data[(k + i + j)].X_Value; } //------------------------------------ monthStand = (GetMonthIndex(monthIndex) * 0.9) / 12; qj[mAnnInputLayerCount] = monthStand; monthIndex += 1; //------------------------------------ mTestingInputs.Add((i + k).ToString(), qj); mTestingOutputs.Add((i + k).ToString(), Data.Data[(k + i + mAnnInputLayerCount)].X_Value); } } }
public void FormateDefault() { if (object.Equals(Data, null)) { return; } if (object.Equals(Data.Data, null)) { return; } int dataCount = Data.Count; if (dataCount < 1) { return; } TrainingCount = (int)((TrainingRate * dataCount) / 100); TestingCount = (dataCount - TrainingCount); mTrainingInputs = new DataSerieTD() { Name = "Training inputs" }; mTrainingOutputs = new DataSerie1D() { Name = "Training outputs" }; for (int i = 0; i < (TrainingCount - mAnnInputLayerCount); i++) { double[] qj = new double[mAnnInputLayerCount]; for (int j = 0; j < mAnnInputLayerCount; j++) { qj[j] = Data.Data[(i + j)].X_Value; } mTrainingInputs.Add(i.ToString(), qj); mTrainingOutputs.Add(i.ToString(), Data.Data[(i + mAnnInputLayerCount)].X_Value); } mTestingInputs = new DataSerieTD() { Name = "Testing inputs" }; mTestingOutputs = new DataSerie1D() { Name = "Testing outputs" }; int k = TrainingCount; for (int i = 0; i < (TestingCount - mAnnInputLayerCount); i++) { double[] qj = new double[mAnnInputLayerCount]; for (int j = 0; j < mAnnInputLayerCount; j++) { qj[j] = Data.Data[(k + i + j)].X_Value; } mTestingInputs.Add((i + k).ToString(), qj); mTestingOutputs.Add((i + k).ToString(), Data.Data[(k + i + mAnnInputLayerCount)].X_Value); } }
/// <summary> /// Formate Training and Testing (inputs-outputs) by specific time series indexes. /// </summary> /// <param name="annInputModel"> The ANN input model </param> private void Formate(DataSerie1D annInputModel) { try { if (object.Equals(Data, null)) { return; } if (object.Equals(Data.Data, null)) { return; } int dataCount = Data.Count; if (dataCount < 1) { return; } if (object.Equals(annInputModel, null)) { throw new Exception("No time series indexes pattern are found."); } if (object.Equals(annInputModel.Data, null)) { throw new Exception("No time series indexes pattern are found."); } if (annInputModel.Count < 1) { throw new Exception("No input model is specified."); } if (annInputModel.Max > dataCount) { throw new Exception("Index great then data serie count."); } TrainingCount = (int)((TrainingRate * dataCount) / 100); TestingCount = (dataCount - TrainingCount); mTrainingInputs = new DataSerieTD() { Name = "Training inputs" }; mTrainingOutputs = new DataSerie1D() { Name = "Training outputs" }; int annInputCount = annInputModel.Count; int[] indexes = new int[annInputCount]; for (int i = 0; i < annInputCount; i++) { if (annInputModel.Data[i].X_Value > TrainingCount || annInputModel.Data[i].X_Value > TestingCount) { throw new Exception("ANN Input indexes must be lower than Training and Testing data serie lenght."); } indexes[i] = (int)(annInputModel.Data[i].X_Value - 1); } int indexMax = (indexes.Max() - 1); for (int i = 0; i < (TrainingCount - indexMax); i++) { double[] qj = new double[annInputCount]; for (int j = 0; j < annInputCount; j++) { qj[j] = Data.Data[(i + indexes[j])].X_Value; } mTrainingInputs.Add(i.ToString(), qj); mTrainingOutputs.Add(i.ToString(), Data.Data[(i + indexMax)].X_Value); } mTestingInputs = new DataSerieTD() { Name = "Testing inputs" }; mTestingOutputs = new DataSerie1D() { Name = "Testing outputs" }; int k = TrainingCount; for (int i = 0; i < (TestingCount - indexMax); i++) { double[] qj = new double[annInputCount]; for (int j = 0; j < annInputCount; j++) { qj[j] = Data.Data[(k + i + indexes[j])].X_Value; } mTestingInputs.Add((i + k).ToString(), qj); mTestingOutputs.Add((i + k).ToString(), Data.Data[(k + i + indexMax)].X_Value); } } catch (Exception ex) { throw ex; } }
public DataFormater(DataSerieTD dataset) { DataSet = dataset; }
public void Compute() { try { // Step 1 : Standerize Data and get Input data; StandardizeData(Obs_TrainingInputs); StandardizeData(Obs_TrainingOutputs); StandardizeData(mObs_TestingInputs); mObs_Training_Inputs = DataSerieTD.Convert(Obs_TrainingInputs); mObs_Training_Outputs = Obs_TrainingOutputs.GetArray(); ANN_Input_Count = mObs_Training_Inputs[0].Length; ANN_Output_Count = mObs_Training_Outputs[0].Length; //---------------Starting :---------------------------- Chronos.Start(); // Step 2 : Initialize optimizer : Initialize(); //------------Optimization------------------ switch (optimAlgo) { case OptimizationAlogrithmEnum.GA_Optimizer: throw new NotImplementedException(); break; case OptimizationAlogrithmEnum.GSA_Optimizer: throw new NotImplementedException(); break; case OptimizationAlogrithmEnum.GWO__Optimizer: throw new NotImplementedException(); break; case OptimizationAlogrithmEnum.HPSOGWO_Optimizer: throw new NotImplementedException(); // Set Optimization function : //HpsoGwoOptim.ObjectiveFunctionComputation += HpsoGwoOptim_ObjectiveFunctionComputation; //Computation: //HpsoGwoOptim.LuanchComputation(); //---------------------------------------------------------------------- //CopySolution(HpsoGwoOptim.BestSolution); //for (int j = 0; j < HpsoGwoOptim.BestChart.Count; j++) // { // Best_Chart.Add(j.ToString(), HpsoGwoOptim.BestChart[j]); // } break; } Chronos.Stop(); mComputationDuration = Chronos.ElapsedMilliseconds; //--Compute Training and Testing Outputs : if (Equals(BestNeuralNetwork, null)) { return; } if (Equals(this.Obs_Training_Inputs, null)) { return; } if (Equals(this.mObs_Testing_Inputs, null)) { return; } mComputed_TrainingOutputs = DataSerie1D.Convert(BestNeuralNetwork.Compute(this.Obs_Training_Inputs)); mComputed_Testing_Outputs = BestNeuralNetwork.Compute(this.mObs_Testing_Inputs); //----------------------------- } catch (Exception ex) { throw ex; } }