public RCConfiguration(DataProvider prov, int seed, int hidenNodesNumber, double interconnectivity, int warmUpCicles, double spectralRadious, ERCActivationFunctionType activationFunctionType) : base(hidenNodesNumber, activationFunctionType) { Prov = prov; Interconnectivity = interconnectivity; WarmUpCicles = warmUpCicles; Seed = seed; SpectralRadious = spectralRadious; }
public RedeMLP(DataProvider prov, double[][] entradasTreino, double[][] saidasTreino, double[][] entradasValidacao, double[][] saidasValidacao, double alpha, double beta, int maxCiclos, int qtdEscondidos, EnumTipoExecucao tipoExecucao, String algoritmo, int seed) { this.prov = prov; this.entradasTreino = entradasTreino; this.saidasTreino = saidasTreino; this.entradasValidacao = entradasValidacao; this.saidasValidacao = saidasValidacao; this.alpha = alpha; this.beta = beta; this.maxCiclos = maxCiclos; this.qtdEscondidos = qtdEscondidos; this.neuronios = new List<List<Neuronio>>(); this.melhorCaso = new List<List<Neuronio>>(); this.EMI = 0.0; this.EMQ = 0.0; // antes estava 10 aqui, não sei o motivo, não é necessário this.melhorEMQRede = 10.0; this.erros = new List<List<double>>(); this.erros.Add(new List<double>());//array de erros de treinamento this.erros.Add(new List<double>());//array de erros de validacao this.reaisprevistos = new List<List<double>>(); ; this.reaisprevistos.Add(new List<double>());//array de valores reais this.reaisprevistos.Add(new List<double>());//array de valores previstos this.qtdAcerto = 0; this.Ciclo = 0; this.tipoExecucao = tipoExecucao; this.EPA = 0.0; this.EPMA = 0.0; this.algoritmo = algoritmo; this._Seed = seed; this._Rand = new Random(_Seed); if (this.algoritmo.Equals(Algoritmos.BACKPROPAGATION)) { //Console.WriteLine(">>> BACKPROPAGATION <<<"); this.initBP(); } else if (this.algoritmo.Equals(Algoritmos.CLANAPSO)) { //Console.WriteLine(">>> ClanAPSO <<<"); this.initClanAPSO(); } else { //Console.WriteLine("ALGORITMO INEXISTENTE"); } }
public MLPEvaluator(RedeMLP mlp, DataProvider prov, EMLPEvaluationInfo evalInfo, bool keepDataNormalized) { _MLP = mlp; _Prov = prov; _EvaluationInfo = evalInfo; _EvaluateLikeONSDoes = (_EvaluationInfo & EMLPEvaluationInfo.EvaluateLikeONS) == EMLPEvaluationInfo.EvaluateLikeONS; _KeepDataNormalized = keepDataNormalized; NTrain = prov.TrainSet.Length; mTrain = prov.TrainSet[0].Output.Length; NVal = prov.ValidationSet.Length; mVal = prov.ValidationSet[0].Output.Length; NTest = prov.TestSet.Length; mTest = prov.TestSet[0].Output.Length; if (_EvaluateLikeONSDoes && mTrain < 12 && mVal < 12 && mTest < 12) throw new ArgumentException("O número de saídas para avaliação segundo o ONS deve ser 12"); }
static int WarmUpCicles = 25; //Aumenta de 2 em 2 => 50 #endregion Fields #region Methods static void Main(string[] args) { #region Paths DataSetPath = @"C:\Users\Edgar\Desktop\Dados\Furnas\lag2.csv"; SimulationResultPath = @"C:\Users\Edgar\Desktop\Dados\Furnas\Predicao\furnas_weight.csv"; #endregion #region Ajustando arquivo de resultados File = new System.IO.StreamWriter(SimulationResultPath, false); string info = "Seed; Hiden Nodes number; WarmUpCicles; InterConnectivity; RestricSpectralRadious; "; if (DataType == EExecutionType.Predction) info = string.Concat(info, (PerformanceInfo & EEvaluationInfo.EMQ) != EEvaluationInfo.None ? "EMQ-Train; EMQ-Val; " : string.Empty, (PerformanceInfo & EEvaluationInfo.DEV) != EEvaluationInfo.None ? "DEV-Train; DEV-Val; " : string.Empty, (PerformanceInfo & EEvaluationInfo.RMSE) != EEvaluationInfo.None ? "RMSE-Train; RMSE-Val; " : string.Empty, (PerformanceInfo & EEvaluationInfo.EPMA) != EEvaluationInfo.None ? "EPMA-Train; EPMA-Val; " : string.Empty); else if ((PerformanceInfo & EEvaluationInfo.SR) != EEvaluationInfo.None) info = string.Concat("SuccessRate-Train; SuccessRate-Val; "); info = string.Concat(info, "Time"); info = string.Concat(info, "; StopCount"); info = string.Concat(info, "; StopEvaluation"); File.WriteLine(info); File.Close(); #endregion Console.WriteLine("Processando arquivo: " + SimulationResultPath); for (int i = 1; i <= TestIterations; i++) { Console.WriteLine("Início iteração: " + i); int seed = Rand.Next(); Random _Random = new Random(seed); StartTime = DateTime.Now; DataProvider prov = new DataProvider(DataSetPath, DataType, seed, trainSize, validationSize, testSize); prov.ApplyLogToData(); prov.NormalizeData(inputA, inputB, outputA, outputB); prov.ShuffleDataSet(); prov.SplitData(); RCHS hs = new RCHS(InterConnectionsNumber, WarmUpCicles, SpectralRadious, prov, seed,MemorySize, MaxHiddenNodes, MaxEvaluations, ActivationFunction, EvaluationFunction, PerformanceInfo); HSResult<byte> returnVal = hs.Run(); int stopIteration = returnVal.StopIteration; int stopEvaluation = returnVal.StopEvaluations; RCHSParticle bestParticle = (RCHSParticle)returnVal.BestParticle; EndTime = DateTime.Now; string resultString = seed + "; " + bestParticle.Config.HidenNodesNumber + "; "+ bestParticle.Config.WarmUpCicles + "; " + bestParticle.Config.Interconnectivity.ToString("0.######") + "; " + bestParticle.Config.SpectralRadious.ToString() + "; "; if (DataType == EExecutionType.Predction) { if ((PerformanceInfo & EEvaluationInfo.EMQ) != EEvaluationInfo.None) { resultString += bestParticle.Evaluator.TrainEMQ[0].ToString("0.######") + "; "; resultString += bestParticle.Evaluator.ValidationEMQ[0].ToString("0.######") + "; "; } if ((PerformanceInfo & EEvaluationInfo.DEV) != EEvaluationInfo.None) { resultString += bestParticle.Evaluator.TrainDEV[0].ToString("0.######") + "; "; resultString += bestParticle.Evaluator.ValidationDEV[0].ToString("0.######") + "; "; } if ((PerformanceInfo & EEvaluationInfo.RMSE) != EEvaluationInfo.None) { resultString += bestParticle.Evaluator.TrainRMSE[0].ToString("0.######") + "; "; resultString += bestParticle.Evaluator.ValidationRMSE[0].ToString("0.######") + "; "; } if ((PerformanceInfo & EEvaluationInfo.EPMA) != EEvaluationInfo.None) { resultString += bestParticle.Evaluator.TrainEPMA[0].ToString("0.######") + "; "; resultString += bestParticle.Evaluator.ValidationEPMA[0].ToString("0.######") + "; "; } } else if ((PerformanceInfo & EEvaluationInfo.SR) != EEvaluationInfo.None) { resultString += bestParticle.Evaluator.TrainSR[0].ToString("0.######") + "; "; resultString += bestParticle.Evaluator.ValidationSR[0].ToString("0.######") + "; "; } resultString += EndTime.Subtract(StartTime).ToReadableString(); resultString += "; " + stopIteration; resultString += "; " + stopEvaluation; Console.WriteLine(info); Console.WriteLine(resultString); File = new System.IO.StreamWriter(SimulationResultPath, true); File.WriteLine(resultString); File.Close(); ////////////////// Salvando predição /////////////////////////////// if (SavePrediciton) { double real = 0; double prediction = 0; double Error = 0; File = new System.IO.StreamWriter(SimulationResultPath + "predicao" + i + ".csv", false); for (int m = 0; m < prov.TestSetlines; m++) { for (int j = 0; j < prov.OutputsN; j++) { real = prov.TestSet[m].RealOutput[j]; prediction = prov.DeNormalizeOutputData(bestParticle.Evaluator.RC.TestT[m][j], j); Error = real - prediction; File.WriteLine(real + ";" + prediction + ";" + Error); } } File.Close(); } ////////////////////////////////////// Console.WriteLine("Fim iteração: " + i); } Console.WriteLine("Fim simulação"); Console.ReadKey(); }
public MLPEvaluator(RedeMLP mlp, DataProvider prov, EMLPEvaluationInfo evalInfo) : this(mlp, prov, evalInfo, false) { }
public DataProvider ShrinkProvider(int inputs) { DataProvider shrinkProv = null; if (inputs < InputsN) { Data[] set = new Data[DataSetLines]; for (int i = 0; i < DataSetLines; i++) { set[i] = new Data(new double[inputs], DataSet[i].RealOutput.Clone() as double[]); for (int j = inputs - 1, k = InputsN - 1; j >= 0; j--, k--) { set[i].RealInput[j] = DataSet[i].RealInput[k]; } } shrinkProv = new DataProvider(set, ExecutionType, _Random.Next(), _TrainSize, _ValidationSize, _TestSize); if (_LogAppliedToData) shrinkProv.ApplyLogToData(); if (_DataNormalized) shrinkProv.NormalizeData(_InputBoundA, _InputBoundB, _OutputBoundA, _OutputBoundB); shrinkProv.ShuffleDataSet(); shrinkProv.SplitData(); } else if (inputs == InputsN) shrinkProv = this; else throw new ArgumentOutOfRangeException(); return shrinkProv; }
static void Main(string[] args) { string path = @"C:\Users\Edgar\Desktop\Dados\Itaipu\itaipu_serie.csv"; double alfa = 0.80; double beta = 0.20; int maxCicles = 5; int hiddenNeurons = 75; int inputLag = 48; int outputLag = 12; Console.WriteLine("Carregando dados da planilha..."); DataBuilder db = new DataBuilder(@path); db.LagData(inputLag, outputLag); Data[] set = db.BuildDataSet(); DataProvider prov = new DataProvider(set, EExecutionType.Predction, 1, trainSize, validationSize, testSize); Console.WriteLine("Aplicando tranformação logarítmica..."); prov.ApplyLogToData(); Console.WriteLine("Normalizando dados..."); prov.NormalizeData(inputA, inputB, outputA, outputB); Console.WriteLine("Embaralhando dados..."); prov.ShuffleDataSet(); Console.WriteLine("Dividindo conjuntos de dados..."); prov.SplitData(); double[][] entradasTreinamento = null; double[][] saidasTreinamento = null; double[][] entradasValidacao = null; double[][] saidasValidacao = null; double[][] entradasTeste = null; double[][] saidasTeste = null; entradasTreinamento = new double[prov.TrainSetLines][]; saidasTreinamento = new double[prov.TrainSetLines][]; for (int i = 0; i < prov.TrainSetLines; i++) { entradasTreinamento[i] = new double[prov.TrainSet[i].Input.Length]; saidasTreinamento[i] = new double[prov.TrainSet[i].Output.Length]; for (int j = 0; j < prov.TrainSet[i].Input.Length; j++) entradasTreinamento[i][j] = prov.TrainSet[i].Input[j]; for (int j = 0; j < prov.TrainSet[i].Output.Length; j++) saidasTreinamento[i][j] = prov.TrainSet[i].Output[j]; } entradasValidacao = new double[prov.ValidationSetLines][]; saidasValidacao = new double[prov.ValidationSetLines][]; for (int i = 0; i < prov.ValidationSetLines; i++) { entradasValidacao[i] = new double[prov.ValidationSet[i].Input.Length]; saidasValidacao[i] = new double[prov.ValidationSet[i].Output.Length]; for (int j = 0; j < prov.ValidationSet[i].Input.Length; j++) entradasValidacao[i][j] = prov.ValidationSet[i].Input[j]; for (int j = 0; j < prov.ValidationSet[i].Output.Length; j++) saidasValidacao[i][j] = prov.ValidationSet[i].Output[j]; } entradasTeste = new double[prov.TestSetlines][]; saidasTeste = new double[prov.TestSetlines][]; for (int i = 0; i < prov.TestSetlines; i++) { entradasTeste[i] = new double[prov.TestSet[i].Input.Length]; saidasTeste[i] = new double[prov.TestSet[i].Output.Length]; for (int j = 0; j < prov.TestSet[i].Input.Length; j++) entradasTeste[i][j] = prov.TestSet[i].Input[j]; for (int j = 0; j < prov.TestSet[i].Output.Length; j++) saidasTeste[i][j] = prov.TestSet[i].Output[j]; } Console.WriteLine("Treinando Rede neural..."); RedeMLP mlp; mlp = new RedeMLP(prov, entradasTreinamento,//array de entradas de treinamento saidasTreinamento,//array de saidas de treinamento entradasValidacao,//array de entradas de validacao saidasValidacao,//array de saidas de validacao alfa,//valor de alpha beta,//valor de beta maxCicles,//maximo de ciclos hiddenNeurons,//quatidade de neuronios escondida EnumTipoExecucao.Previsao,//booleano para definir se é previsao(true) ou classificacao(false) Algoritmos.BACKPROPAGATION,//constante q define o algoritmo a ser utilizado seed); Console.WriteLine("Testando Rede neural..."); mlp.testar(entradasTeste, saidasTeste); EMLPEvaluationInfo PerformanceInfo = EMLPEvaluationInfo.EPMA | EMLPEvaluationInfo.EMQ | EMLPEvaluationInfo.DEV | EMLPEvaluationInfo.EvaluateLikeONS; MLPEvaluator eval = new MLPEvaluator(mlp, prov, PerformanceInfo); eval.Evaluate(); Console.ReadLine(); // double[][] entradasTreinamento = null; // double[][] saidasTreinamento = null; // double[][] entradasValidacao = null; // double[][] saidasValidacao = null; // double[][] entradasTeste = null; // double[][] saidasTeste = null; // DataProvider prov = new DataProvider(@"C:\Users\Edgar\Desktop\Dados\Itaipu\lag.csv", EExecutionType.Predction, 1, 0.5, 0.25, 0.25); // prov.ApplyLogToData(); // prov.NormalizeData(inputA, inputB, outputA, outputB); // prov.ShuffleDataSet(); // prov.SplitData(); // entradasTreinamento = new double[prov.TrainSetLines][]; // saidasTreinamento = new double[prov.TrainSetLines][]; // for (int i = 0; i < prov.TrainSetLines; i++) // { // entradasTreinamento[i] = new double[prov.TrainSet[i].Input.Length]; // saidasTreinamento[i] = new double[prov.TrainSet[i].Output.Length]; // for (int j = 0; j < prov.TrainSet[i].Input.Length; j++) // entradasTreinamento[i][j] = prov.TrainSet[i].Input[j]; // for (int j = 0; j < prov.TrainSet[i].Output.Length; j++) // saidasTreinamento[i][j] = prov.TrainSet[i].Output[j]; // } // entradasValidacao = new double[prov.ValidationSetLines][]; // saidasValidacao = new double[prov.ValidationSetLines][]; // for (int i = 0; i < prov.ValidationSetLines; i++) // { // entradasValidacao[i] = new double[prov.ValidationSet[i].Input.Length]; // saidasValidacao[i] = new double[prov.ValidationSet[i].Output.Length]; // for (int j = 0; j < prov.ValidationSet[i].Input.Length; j++) // entradasValidacao[i][j] = prov.ValidationSet[i].Input[j]; // for (int j = 0; j < prov.ValidationSet[i].Output.Length; j++) // saidasValidacao[i][j] = prov.ValidationSet[i].Output[j]; // } // entradasTeste = new double[prov.TestSetlines][]; // saidasTeste = new double[prov.TestSetlines][]; // for (int i = 0; i < prov.TestSetlines; i++) // { // entradasTeste[i] = new double[prov.TestSet[i].Input.Length]; // saidasTeste[i] = new double[prov.TestSet[i].Output.Length]; // for (int j = 0; j < prov.TestSet[i].Input.Length; j++) // entradasTeste[i][j] = prov.TestSet[i].Input[j]; // for (int j = 0; j < prov.TestSet[i].Output.Length; j++) // saidasTeste[i][j] = prov.TestSet[i].Output[j]; // } // RedeMLP mlp; // mlp = new RedeMLP(entradasTreinamento,//array de entradas de treinamento // saidasTreinamento,//array de saidas de treinamento // entradasValidacao,//array de entradas de validacao // saidasValidacao,//array de saidas de validacao // 0.7,//valor de alpha // 0.4,//valor de beta // 600,//maximo de ciclos // 20,//quantidade de neuronios escondida // false,//booleano para definir se é previsao(true) ou classificacao(false) // Algoritmos.BACKPROPAGATION);//constante q define o algoritmo a ser utilizado // mlp.testar(entradasTeste, saidasTeste); // Console.WriteLine("erros: " + mlp.getErros()); // Console.ReadLine(); // //Grafico grafico = new Grafico(mlp.getErros()); // /*Teste Estatistico*/ //// int totalTestes = 30; //// for (int i = 0; i < totalTestes; i++) { //// mlp = new MLP(dados.getEntradasTreinamento().getArray(),//array de entradas de treinamento //// dados.getSaidasTreinamento().getArray(),//array de saidas de treinamento //// dados.getEntradasValidacao().getArray(),//array de entradas de validacao //// dados.getSaidasValidacao().getArray(),//array de saidas de validacao //// 0.7,//valor de alpha //// 0.4,//valor de beta //// 600,//maximo de ciclos //// 20,//quatidade de neuronios escondida //// true,//booleano para definir se é previsao(true) ou classificacao(false) //// Algoritmos.BACKPROPAGATION);//constante q define o algoritmo a ser utilizado //// mlp.testar(dados.getEntradasTeste().getArray(), dados.getSaidasTeste().getArray()); //// } // /*Fim Teste Estatistico*/ }
static void Main(string[] args) { try { // TestLags(); DateTime StartTime, EndTime; List<object[]> bestRCs = new List<object[]>(); int MaxRCMemory = 10; int seed = 1; int MaxHiddenNodesNumber = 150; int MaxWarmUpCicles = 100; double MaxInterConnectivity = 1; double SpectralRadious = 0.9; string dataSetPath = @"C:\Users\Edgar\Desktop\Dados\PLD\lag.csv"; for (int h = 10; h < MaxHiddenNodesNumber && bestRCs.Count < 1; h += 5) { for (double i = 0.0; i < MaxInterConnectivity && bestRCs.Count < 1; i += 0.02) { for (int w = 1; w < MaxWarmUpCicles && bestRCs.Count < 1; w += 5) { StartTime = DateTime.Now; DataProvider dp = new DataProvider(dataSetPath, EExecutionType.Predction, seed, trainSize, validationSize, testSize); //dp.ApplyLogToData(); dp.NormalizeData(inputA, inputB, outputA, outputB); dp.ShuffleDataSet(); dp.SplitData(); RC rc = new RC(dp.TrainSet, dp.ValidationSet, dp.TestSet, new RCConfiguration(dp, seed, h, i, w, SpectralRadious, ERCActivationFunctionType.HyperbolicTangent)); rc.Run(); RCEvaluator eval = new RCEvaluator(rc, dp, EEvaluationInfo.EMQ | EEvaluationInfo.DEV | EEvaluationInfo.EPMA | EEvaluationInfo.RMSE); eval.Evaluate(); RCEvaluator eval2 = new RCEvaluator(rc, dp, EEvaluationInfo.EMQ | EEvaluationInfo.DEV | EEvaluationInfo.EPMA | EEvaluationInfo.RMSE, true); eval2.Evaluate(); EndTime = DateTime.Now; Console.WriteLine("Neurônios camada escondida: " + h); Console.WriteLine("Interconectividade (%): " + i.ToString("0.##")); Console.WriteLine("Ciclos aquecimento: " + w); Console.WriteLine("EMQ(7): " + eval.TestEMQ[6].ToString("0.##")); Console.WriteLine("RMSE(7): " + eval.TestRMSE[6].ToString("0.##")); Console.WriteLine("EPMA(7): " + eval.TestEPMA[6].ToString("0.##")); Console.WriteLine("Tempo: " + EndTime.Subtract(StartTime).ToReadableString()); Console.WriteLine("#################################"); object[] b = new object[4]; b[0] = eval.TestEPMA[0]; b[1] = dp; b[2] = rc; b[3] = eval; bestRCs.Add(b); bestRCs.Sort(delegate(object[] p1, object[] p2) { return ((double)p1[0]).CompareTo(((double)p2[0])); }); if (bestRCs.Count > MaxRCMemory) bestRCs.RemoveAt(MaxRCMemory); } } } Console.WriteLine("Salvando Predição da melhor configuração"); #region Salvando Predição double prediction = 0; double Error = 0; DataProvider dataProv = bestRCs[0][1] as DataProvider; RC elm = bestRCs[0][2] as RC; RCEvaluator evaluator = bestRCs[0][3] as RCEvaluator; StreamWriter File = new System.IO.StreamWriter(@"C:\Users\Edgar\Desktop\Dados\Furnas\Predicao\predicao_" + elm.HiddenNodesNumber + "_" + elm.InterConnectivity + "_" + elm.WarmUpCicles + "_" + evaluator.TestEPMA[6] + ".csv", false); for (int m = 0; m < dataProv.TestSetlines; m++) { string temp = string.Empty; for (int j = 0; j < dataProv.OutputsN; j++) { prediction = dataProv.DeNormalizeOutputData(elm.TestT[m][j], j); Error = dataProv.TestSet[m].RealOutput[j] - prediction; temp = string.Concat(temp + dataProv.TestSet[m].RealOutput[j] + ";" + prediction + ";" + Error + ";"); } File.WriteLine(temp); } File.Close(); #endregion Console.WriteLine("FIM"); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); Console.ReadLine(); } }