public static List <string> genetico(double inversion, double[] FNE, double VS, int periodo, int poblacionNumero) { Stopwatch tiempo2 = Stopwatch.StartNew(); double aproxInicial = aproximacioninicial(inversion, FNE); Random random = new Random(); double minimo = aproxInicial - 1000; double maximo = aproxInicial + 1000; List <double> poblacion = new List <double>(); while (poblacion.Count < poblacionNumero) { double numeroAleatorio = random.NextDouble() * (maximo - minimo) + minimo; if (!poblacion.Contains(numeroAleatorio)) { poblacion.Add(numeroAleatorio); } } int i = 1; double porcentajeconvergencia = 0; double porcentaje = (((double)100) / poblacionNumero); List <double> ResultadosFX; Random random2 = new Random(); do { ResultadosFX = fx(inversion, FNE, VS, poblacion, periodo); List <int> torneo1 = posTorneo(0, poblacion.Count / 2); List <int> torneo2 = posTorneo(poblacion.Count / 2, poblacion.Count); List <double> padre = Seleccion(torneo1, torneo2, ResultadosFX, poblacion); List <int> cruce1 = posTorneo(0, padre.Count / 2); List <int> cruce2 = posTorneo(padre.Count / 2, padre.Count); int c1 = cruce1.Count(); int c2 = cruce2.Count(); List <double> hijos_Generados = new List <double>(); double probCruz = random2.NextDouble(); if (probCruz < 0.9) { hijos_Generados = CruceTotal(padre, cruce1, cruce2, i); } else { hijos_Generados = padre; } poblacion.Clear(); poblacion = padre.Concat(hijos_Generados).ToList(); poblacion = DesordenarLista(poblacion); var agrupacion = poblacion.GroupBy(x => x).Select(g => g.Count()).ToList(); agrupacion = agrupacion.OrderByDescending(o => o).ToList(); var agrupacion2 = agrupacion.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); var agrupacion3 = agrupacion.GroupBy(x => x).Select(g => g.Key).ToList(); var valormax = agrupacion3.Max(); foreach (var el in agrupacion2) { if (el.Text == valormax && el.Count == 1) { double valor = Convert.ToDouble(el.Text); porcentajeconvergencia = porcentaje * valor; } break; } if (porcentajeconvergencia < (double)99.9) { i = i + 1; } } while (porcentajeconvergencia < (double)99.9); tiempo2.Stop(); var resultTIR = poblacion.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); resultTIR = resultTIR.OrderByDescending(o => o.Count).ToList(); var resultTMR = ResultadosFX.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); resultTMR = resultTMR.OrderByDescending(o => o.Count).ToList(); String Ztiempo2 = tiempo2.Elapsed.TotalSeconds.ToString(); List <string> resultados = new List <string>(); resultados.Add(Ztiempo2); resultados.Add(resultTIR[0].Text.ToString()); resultados.Add(aproxInicial.ToString()); resultados.Add(porcentajeconvergencia.ToString()); resultados.Add(resultTMR[0].Text.ToString()); resultados.Add(i.ToString()); return(resultados); }
public static List <double> CalcularTIR(double inversion, double[] FNE, double VS, int periodo) { int poblacionNumero = 1200; // System.Diagnostics.Debug.WriteLine("\nAproximacion inicial encontrada"); //System.Diagnostics.Debug.WriteLine("\n\n******************INICIO DE LA BUSQUEDA DE LA TIR.*****************\n\n"); //Stopwatch tiempo = Stopwatch.StartNew(); double aproxInicial = aproximacioninicial(inversion, FNE); Random random = new Random(); double minimo = aproxInicial - 1000; double maximo = aproxInicial + 1000; List <double> poblacion = new List <double>(); while (poblacion.Count < poblacionNumero) { double numeroAleatorio = random.NextDouble() * (maximo - minimo) + minimo; if (!poblacion.Contains(numeroAleatorio)) { poblacion.Add(numeroAleatorio); } } int i = 1; double porcentajeconvergencia = 0; double porcentaje = (((double)100) / poblacionNumero); List <double> ResultadosFX; Random random2 = new Random(); do { ResultadosFX = fx(inversion, FNE, VS, poblacion, periodo); List <int> torneo1 = posTorneo(0, poblacion.Count / 2); List <int> torneo2 = posTorneo(poblacion.Count / 2, poblacion.Count); List <double> padre = Seleccion(torneo1, torneo2, ResultadosFX, poblacion); List <int> cruce1 = posTorneo(0, padre.Count / 2); List <int> cruce2 = posTorneo(padre.Count / 2, padre.Count); List <double> hijos_Generados = new List <double>(); double probCruz = random2.NextDouble(); if (probCruz < 0.9) { hijos_Generados = CruceTotal(padre, cruce1, cruce2, i); } else { hijos_Generados = padre; } poblacion.Clear(); poblacion = padre.Concat(hijos_Generados).ToList(); poblacion = DesordenarLista(poblacion); var agrupacion = poblacion.GroupBy(x => x).Select(g => g.Count()).ToList(); agrupacion = agrupacion.OrderByDescending(o => o).ToList(); var agrupacion2 = agrupacion.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); var agrupacion3 = agrupacion.GroupBy(x => x).Select(g => g.Key).ToList(); var valormax = agrupacion3.Max(); foreach (var el in agrupacion2) { if (el.Text == valormax && el.Count == 1) { double valor = Convert.ToDouble(el.Text); porcentajeconvergencia = porcentaje * valor; } break; } // System.Diagnostics.Debug.WriteLine("\tGeneracion: {0} , Convergencia del: {1}\n", i, porcentajeconvergencia); if (porcentajeconvergencia < (double)99.9) { i = i + 1; } } while (porcentajeconvergencia < (double)99.9); //tiempo.Stop(); //System.Diagnostics.Debug.WriteLine("\n******************CONCLUIDA LA BUSQUEDA DE LA TIR.*****************"); var resultTIR = poblacion.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); resultTIR = resultTIR.OrderByDescending(o => o.Count).ToList(); var resultTMAR = ResultadosFX.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); resultTMAR = resultTMAR.OrderByDescending(o => o.Count).ToList(); /*System.Diagnostics.Debug.WriteLine("********RESULTADOS DE LA BUSQUEDA DE LA TIR*************"); * System.Diagnostics.Debug.WriteLine("\tAproximacion inicial es: {0}\n", aproxInicial); * System.Diagnostics.Debug.WriteLine("\n\tTotal de Generaciones del calculo de la TIR: {0} , Convergencia del: {1}", i, porcentajeconvergencia); * System.Diagnostics.Debug.WriteLine("\n\t\t RESULTADO TMAR: {0}", resultTMAR[0].Text); * System.Diagnostics.Debug.WriteLine("\n\t\t RESULTADO TIR: {0}", resultTIR[0].Text); * System.Diagnostics.Debug.WriteLine($"\n\t\t\tTiempo para la busqueda de la TIR y TMAR: {tiempo.Elapsed.TotalSeconds} segundos"); * System.Diagnostics.Debug.WriteLine("********RESULTADOS DE LA BUSQUEDA DE LA TIR*************");*/ List <double> resultados = new List <double>(); resultados.Add(resultTIR[0].Text); resultados.Add(resultTMAR[0].Text); Thread.Sleep(450); return(resultados); }
private void button1_Click(object sender, EventArgs e) { Resultados.Text = ""; ResultadosFNE.Text = ""; String inve, per, vss; double inversion, VS; int periodo; inve = inveTex.Text; per = pertex.Text; vss = vsstex.Text; double[] FNE = new double[Convert.ToInt32(per)]; FNE[0] = Convert.ToDouble(2000); FNE[1] = Convert.ToDouble(1000); FNE[2] = Convert.ToDouble(3000); FNE[3] = Convert.ToDouble(3000); FNE[4] = Convert.ToDouble(1000); /* for (int x = 0; x < Convert.ToInt32(per); x++) * { * Console.WriteLine("\tIntroduzca el FNE {0}: ", x + 1); * String temporal = Console.ReadLine(); * FNE[x] = Convert.ToDouble(temporal); * }*/ inversion = Convert.ToDouble(inve); periodo = Convert.ToInt32(per); VS = Convert.ToDouble(vss); String poblacionNumero; poblacionNumero = poblacionNumeroTex.Text; Resultados.Text = Resultados.Text + "******************INICIO DE LA BUSQUEDA DE LA TIR.*****************\r\n\n"; List <double> porcentajeconvergenciagrafica = new List <double>(); Stopwatch tiempo = Stopwatch.StartNew(); double aproxInicial = aproximacioninicial(inversion, FNE); Random random = new Random(); double minimo = aproxInicial - 1000; double maximo = aproxInicial + 1000; List <double> poblacion = new List <double>(); while (poblacion.Count < Int32.Parse(poblacionNumero)) { double numeroAleatorio = random.NextDouble() * (maximo - minimo) + minimo; if (!poblacion.Contains(numeroAleatorio)) { poblacion.Add(numeroAleatorio); } } List <double> poblacionGrafica = new List <double>(poblacion); int i = 1; double porcentajeconvergencia = 0; double porcentaje = (((double)100) / Int32.Parse(poblacionNumero)); List <double> ResultadosFX; Random random2 = new Random(); do { ResultadosFX = fx(inversion, FNE, VS, poblacion, periodo); List <int> torneo1 = posTorneo(0, poblacion.Count / 2); List <int> torneo2 = posTorneo(poblacion.Count / 2, poblacion.Count); List <double> padre = Seleccion(torneo1, torneo2, ResultadosFX, poblacion); List <int> cruce1 = posTorneo(0, padre.Count / 2); List <int> cruce2 = posTorneo(padre.Count / 2, padre.Count); List <double> hijos_Generados = new List <double>(); double probCruz = random2.NextDouble(); if (probCruz < 0.9) { hijos_Generados = CruceTotal(padre, cruce1, cruce2, i); } else { hijos_Generados = padre; } poblacion.Clear(); poblacion = padre.Concat(hijos_Generados).ToList(); poblacion = DesordenarLista(poblacion); var agrupacion = poblacion.GroupBy(x => x).Select(g => g.Count()).ToList(); agrupacion = agrupacion.OrderByDescending(o => o).ToList(); var agrupacion2 = agrupacion.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); var agrupacion3 = agrupacion.GroupBy(x => x).Select(g => g.Key).ToList(); var valormax = agrupacion3.Max(); foreach (var el in agrupacion2) { if (el.Text == valormax && el.Count == 1) { double valor = Convert.ToDouble(el.Text); porcentajeconvergencia = porcentaje * valor; } break; } Resultados.Text = Resultados.Text + "\tGeneracion: " + i + " , Convergencia del: " + porcentajeconvergencia + "\r\n"; // Console.WriteLine("\tGeneracion: {0} , Convergencia del: {1}\n", i, porcentajeconvergencia); porcentajeconvergenciagrafica.Add(porcentajeconvergencia); if (porcentajeconvergencia < (double)99.9) { i = i + 1; } } while (porcentajeconvergencia < (double)99.9); tiempo.Stop(); chart1.Series["Convergencia"].Points.Clear(); chart1.ChartAreas[0].AxisX.Title = "Generaciones"; chart1.ChartAreas[0].AxisY.Title = "Porcentaje de Convergencia"; int a0; for (a0 = 1; a0 <= porcentajeconvergenciagrafica.Count; a0++) { chart1.Series["Convergencia"].Points.AddXY(a0, porcentajeconvergenciagrafica[a0 - 1]); } /*Limites de ejes de grafica*/ chart1.ChartAreas[0].AxisY.Maximum = 100; chart1.ChartAreas[0].AxisY.Interval = 10; double valorConDecimal = (double)porcentajeconvergenciagrafica.Count / 10; long valorSinDecimal = (long)valorConDecimal; double decimales = valorConDecimal - (double)valorSinDecimal; if (decimales != 0) { valorSinDecimal = valorSinDecimal + 1; } int someIntT = (int)valorSinDecimal * 10; chart1.ChartAreas[0].AxisX.Maximum = someIntT; chart1.ChartAreas[0].AxisX.Interval = 5; chart2.Series["Poblacion Inicial"].Points.Clear(); chart2.Series["Poblacion Final"].Points.Clear(); chart2.ChartAreas[0].AxisX.Title = "Numero Cromosoma"; chart2.ChartAreas[0].AxisY.Title = "Valor de Cromosoma"; for (int a = 1; a <= Int32.Parse(poblacionNumero); a++) { chart2.Series["Poblacion Inicial"].Points.AddXY(a, poblacionGrafica[a - 1]); //chart2.Series["Poblacion Final"].Points.AddXY(a, poblacion[a]); } chart2.ChartAreas[0].AxisX.Interval = Int32.Parse(poblacionNumero) / 10; chart2.ChartAreas[0].AxisY.Interval = 200; double tem0 = maximo / 100; long valorSinDecimal0 = (long)tem0; valorSinDecimal0 = valorSinDecimal0 + 1; int someIntT0 = (int)valorSinDecimal0 * 100; chart2.ChartAreas[0].AxisY.Maximum = someIntT0; double tem = minimo / 100; long valorSinDecimal1 = (long)tem; valorSinDecimal1 = valorSinDecimal1 - 2; int someIntT1 = (int)valorSinDecimal1 * 100; chart2.ChartAreas[0].AxisY.Minimum = someIntT1; /*Limites de ejes de grafica*/ Resultados.Text = Resultados.Text + "\r\n******************CONCLUIDA LA BUSQUEDA DE LA TIR******************"; var resultTIR = poblacion.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); resultTIR = resultTIR.OrderByDescending(o => o.Count).ToList(); var resultTMR = ResultadosFX.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); resultTMR = resultTMR.OrderByDescending(o => o.Count).ToList(); ResultadosFNE.Text = ResultadosFNE.Text + "\r\r******************INICIO DE OPTIMIZACION DE FNE*******************\r\n"; /*Optimizacion de FLUJOS NETOS DE EFECTIVO*/ List <double> porcentajeconvergenciagrafica2 = new List <double>(); Stopwatch tiempofne = Stopwatch.StartNew(); List <double> FNEList = FNE.ToList(); double FNEMax = FNEList.Max(); double FNEMin = FNEList.Min(); double porcentajeFNEMax = FNEMax / 100; double porcentajeFNEMin = FNEMin / 100; FNEMax = FNEMax + (porcentajeFNEMax * 10); FNEMin = FNEMin - (porcentajeFNEMin * 10); List <List <double> > poblacion2 = new List <List <double> >(); while (poblacion2.Count < Int32.Parse(poblacionNumero)) { List <double> poblaciontem = new List <double>(); while (poblaciontem.Count < Convert.ToInt32(per)) { double numeroAleatorio = random.NextDouble() * ((FNEMax + 1) - (FNEMin - 1)) + (FNEMin - 1); if (!poblaciontem.Contains(numeroAleatorio)) { poblaciontem.Add(numeroAleatorio); } } poblacion2.Add(poblaciontem); } List <List <double> > poblacionGrafica2 = new List <List <double> >(poblacion2); int j = 1; double porcentajeconvergencia2 = 0; double porcentaje2 = (((double)100) / Int32.Parse(poblacionNumero)); List <double> ResultadosFX2; Random random2b = new Random(); do { ResultadosFX2 = fxFNE(inversion, poblacion2, VS, Convert.ToDouble(resultTIR[0].Text), periodo); List <int> torneo1b = posTorneo(0, poblacion2.Count / 2); List <int> torneo2b = posTorneo(poblacion2.Count / 2, poblacion2.Count); List <List <double> > padre2 = SeleccionFNE(torneo1b, torneo2b, ResultadosFX2, poblacion2, FNEMax); List <int> cruce1b = posTorneo(0, padre2.Count / 2); List <int> cruce2b = posTorneo(padre2.Count / 2, padre2.Count); List <List <double> > hijos_Generadosb = new List <List <double> >(); double probCruzb = random2b.NextDouble(); if (probCruzb < 0.9) { hijos_Generadosb = CruceTotal2(padre2, cruce1b, cruce2b, j); } else { hijos_Generadosb = padre2; } poblacion2.Clear(); poblacion2 = padre2.Concat(hijos_Generadosb).ToList(); poblacion2 = DesordenarLista(poblacion2); //List<List<double>> poblacionanalisis = new List<List<double>>(poblacion2); permite pasar en otra lista List <double> convergencia = medir_convergencia2(poblacion2); var agrupacion = convergencia.OrderByDescending(o => o).ToList(); var agrupacion2 = agrupacion.GroupBy(x => x).Select(g => new { Text = g.Key, Count = g.Count() }).ToList(); var valormax = agrupacion.Max(); foreach (var el in agrupacion2) { if (el.Text == valormax && el.Count == 1) { double valor = Convert.ToDouble(el.Text); porcentajeconvergencia2 = porcentaje2 * valor; } break; } ResultadosFNE.Text = ResultadosFNE.Text + "\tGeneracion: " + j + " , Convergencia del: " + porcentajeconvergencia2 + "\r\n"; porcentajeconvergenciagrafica2.Add(porcentajeconvergencia2); if (porcentajeconvergencia2 < (double)99.9) { j = j + 1; } } while (porcentajeconvergencia2 < (double)99.9); tiempofne.Stop(); ResultadosFNE.Text = ResultadosFNE.Text + "******************FINALIZACION DE OPTIMIZACION DE FNE*******************"; /*Optimiazacion de FLUJOS NETOS DE EFECTIVO*/ chart4.Series["Convergencia"].Points.Clear(); chart4.ChartAreas[0].AxisX.Title = "Generaciones"; chart4.ChartAreas[0].AxisY.Title = "Porcentaje de Convergencia"; for (int a = 1; a < porcentajeconvergenciagrafica2.Count + 1; a++) { chart4.Series["Convergencia"].Points.AddXY(a, porcentajeconvergenciagrafica2[a - 1]); } chart3.ChartAreas[0].AxisX.Title = "Numero Cromosoma"; chart3.ChartAreas[0].AxisY.Title = "Valor de Cromosoma"; for (int c = 0; c < periodo; c++) { try { chart3.Series.Add("Cromosoma " + c); chart3.Series["Cromosoma " + c].ChartType = SeriesChartType.Point; } catch (System.ArgumentException ae) { } } for (int c = 0; c < periodo; c++) { chart3.Series["Cromosoma " + c].Points.Clear(); } for (int a = 1; a < Int32.Parse(poblacionNumero) + 1; a++) { List <double> temporal = new List <double>(); temporal = poblacionGrafica2[a - 1]; for (int b = 0; b < temporal.Count; b++) { chart3.Series["Cromosoma " + b].Points.AddXY(a, temporal[b]); } } /*Limites de ejes de grafica*/ chart3.ChartAreas[0].AxisX.Interval = Int32.Parse(poblacionNumero) / 10; //chart3.ChartAreas[0].AxisY.Interval = 250; //chart3.ChartAreas[0].AxisY.Maximum = FNEMax; chart4.ChartAreas[0].AxisY.Maximum = 100; chart4.ChartAreas[0].AxisY.Interval = 10; double valorConDecimal2 = (double)porcentajeconvergenciagrafica2.Count / 10; long valorSinDecimal2 = (long)valorConDecimal2; double decimales2 = valorConDecimal2 - (double)valorSinDecimal2; if (decimales2 != 0) { valorSinDecimal2 = valorSinDecimal2 + 1; } int someIntT2 = (int)valorSinDecimal2 * 10; chart4.ChartAreas[0].AxisX.Maximum = someIntT2; chart4.ChartAreas[0].AxisX.Interval = 5; /*Limites de ejes de grafica*/ /* for (int c = 0; c < periodo; c++) * { * try * { * chart3.Series.Add("Cromosoma Optimo " + c); * chart3.Series["Cromosoma Optimo " + c].ChartType = SeriesChartType.Point; * } * catch (System.ArgumentException ae) * { * * } * } * * for (int c = 0; c < periodo; c++) * { * chart3.Series["Cromosoma Optimo " + c].Points.Clear(); * } * * for (int a = 0; a < Int32.Parse(poblacionNumero); a++) * { * List<double> temporal2 = new List<double>(); * temporal2 = poblacion2[a]; * for (int b = 0; b < temporal2.Count; b++) * { * chart3.Series["Cromosoma Optimo " + b].Points.AddXY(a, temporal2[b]); * * } * }*/ /*IMPRIMIENDO RESULTADOS FINALES*/ Resultados.Text = Resultados.Text + "\r\n\r\n******************RESULTADOS DE LA BUSQUEDA DE LA TIR*************"; Resultados.Text = Resultados.Text + "\r\n\r\n\tAproximacion inicial es:" + aproxInicial + "\r\n"; Resultados.Text = Resultados.Text + "\tTotal de Generaciones: " + i + " , Convergencia del: " + porcentajeconvergencia; Resultados.Text = Resultados.Text + "\r\n\r\n\t\t RESULTADO TMAR: " + resultTMR[0].Text; Resultados.Text = Resultados.Text + "\r\n\t\t RESULTADO TIR: " + resultTIR[0].Text; Resultados.Text = Resultados.Text + $"\r\n\r\n\t\tTiempo para la busqueda de la TIR y TMAR: {tiempo.Elapsed.TotalSeconds} segundos"; Resultados.Text = Resultados.Text + "\r\n\r\n********RESULTADOS DE LA BUSQUEDA DE LA TIR*************"; ResultadosFNE.Text = ResultadosFNE.Text + "\r\n\r\n******************RESULTADOS DE LA OPTIMIZACION DE LOS FNE******************"; ResultadosFNE.Text = ResultadosFNE.Text + "\r\n\r\n\tTotal de Generaciones: " + j + " , Convergencia del: " + porcentajeconvergencia2 + "\r\n"; List <double> poblacionMuestra = new List <double>(); poblacionMuestra = poblacion2[0]; for (int x = 0; x < poblacionMuestra.Count; x++) { ResultadosFNE.Text = ResultadosFNE.Text + "\r\n\t\t FNE Original " + x + ": " + FNE[x] + ", FNE Opimizado " + x + ": " + poblacionMuestra[x]; } ResultadosFNE.Text = ResultadosFNE.Text + "\r\n\r\n\t\t\tRESULTADO TMAR: " + ResultadosFX2[0]; ResultadosFNE.Text = ResultadosFNE.Text + "\r\n\t\t\tRESULTADO TIR: " + resultTIR[0].Text; ResultadosFNE.Text = ResultadosFNE.Text + $"\r\n\r\n\t\t\tTiempo para la optimizacion de los FNE: {tiempofne.Elapsed.TotalSeconds} segundos"; ResultadosFNE.Text = ResultadosFNE.Text + "\r\n\r\n********RESULTADOS DE LA OPTIMIZACION DE LOS FNE********"; /*IMPRIMIENDO RESULTADOS FINALES*/ }