public void Querys() { string archivo = "AB5,BC4,CD8,DC8,DE6,AD5,CE2,EB3,AE7"; clsGrafo obGrafo = new clsGrafo(); #region GENERACION LISTA ADYACENTE string[] lstCordenadas = archivo.Trim().ToUpper().Split(','); char[] lstVertexName = archivo.ToUpper().Where(x => char.IsLetter(x)).Distinct().OrderBy(x => x).ToArray(); List <clsVertex> lstVertex = new List <clsVertex>(); foreach (var item in lstVertexName) { obGrafo.addVertex(new clsVertex { Name = item.ToString(), lstEdge = new List <clsEdge>(), Estado = false }); } foreach (var item in lstCordenadas) { if (!char.IsNumber(item[0]) && !char.IsNumber(item[1]) && char.IsNumber(item[2]) && !item[0].Equals(item[1])) { obGrafo.addEdge(new clsEdge { Vertex1 = item[0], Vertex2 = item[1], Cost = float.Parse(item[2].ToString()) }); } } #endregion FIN GENERACION LISTA ADYACENTE string item1 = obGrafo.getDistance("ABC"); string item2 = obGrafo.getDistance("AD"); string item3 = obGrafo.getDistance("ADC"); string item4 = obGrafo.getDistance("AEBCD"); string item5 = obGrafo.getDistance("AED"); List <clsCamino> Item6 = new List <clsCamino>(); obGrafo.getRouteByStops(obGrafo.lstVertex, "C", "C", 4, "MAX", Item6, string.Empty); string item6 = Item6.Count().ToString(); List <clsCamino> Item7 = new List <clsCamino>(); obGrafo.getRouteByStops(obGrafo.lstVertex, "A", "C", 5, "EQUAL", Item7, string.Empty); string item7 = Item7.Count().ToString(); List <clsCamino> Item8 = new List <clsCamino>(); obGrafo.getRoutes(obGrafo.lstVertex, "A", "C", Item8, string.Empty); string item8 = Item8.Min(x => x.costo).ToString(); List <clsCamino> Item9 = new List <clsCamino>(); obGrafo.getRoutes(obGrafo.lstVertex, "B", "B", Item9, string.Empty); string item9 = Item9.Min(x => x.costo).ToString(); }
static void Main(string[] args) { //Inicializacion de Variables int alpha = 1; int betha = 5; int Q = 100; int N = 30; int G = 10; double p = 0.5; double FInicial = 0.000001; Random Rand = new Random(); //Inicializacion del Grafo clsGrafo Grafo = new clsGrafo("1", "4"); clsVertice V = new clsVertice("1"); V.Aristas.Add(new clsArista("1", "2", 5, FInicial)); V.Aristas.Add(new clsArista("1", "3", 3.1, FInicial)); V.Aristas.Add(new clsArista("1", "6", 5.2, FInicial)); Grafo.Vertices.Add(V); V = new clsVertice("2"); V.Aristas.Add(new clsArista("2", "1", 5, FInicial)); V.Aristas.Add(new clsArista("2", "3", 4.9, FInicial)); V.Aristas.Add(new clsArista("2", "7", 5.2, FInicial)); Grafo.Vertices.Add(V); V = new clsVertice("3"); V.Aristas.Add(new clsArista("3", "1", 3.1, FInicial)); V.Aristas.Add(new clsArista("3", "2", 4.9, FInicial)); V.Aristas.Add(new clsArista("3", "5", 6, FInicial)); V.Aristas.Add(new clsArista("3", "6", 3.2, FInicial)); V.Aristas.Add(new clsArista("3", "7", 3, FInicial)); Grafo.Vertices.Add(V); V = new clsVertice("4"); V.Aristas.Add(new clsArista("4", "5", 4.8, FInicial)); V.Aristas.Add(new clsArista("4", "7", 5.5, FInicial)); Grafo.Vertices.Add(V); V = new clsVertice("5"); V.Aristas.Add(new clsArista("5", "3", 6, FInicial)); V.Aristas.Add(new clsArista("5", "4", 5.5, FInicial)); V.Aristas.Add(new clsArista("5", "6", 4.7, FInicial)); Grafo.Vertices.Add(V); V = new clsVertice("6"); V.Aristas.Add(new clsArista("6", "1", 5.2, FInicial)); V.Aristas.Add(new clsArista("6", "3", 3.2, FInicial)); V.Aristas.Add(new clsArista("6", "5", 4.7, FInicial)); Grafo.Vertices.Add(V); V = new clsVertice("7"); V.Aristas.Add(new clsArista("7", "2", 5.2, FInicial)); V.Aristas.Add(new clsArista("7", "3", 3, FInicial)); V.Aristas.Add(new clsArista("7", "4", 4.8, FInicial)); Grafo.Vertices.Add(V); List <clsHormiga> Hormigas; string Recorrido; for (int i = 0; i < G; i++) { Hormigas = new List <clsHormiga>(); for (int j = 0; j < N; j++) { clsHormiga H = new clsHormiga(); clsVertice VerticeAnterior = new clsVertice(""); clsVertice VerticeActual = Grafo.getInicio(); double RecorridoCosto = 0; Recorrido = VerticeActual.Origen; while (!Grafo.isFinal(VerticeActual)) { //Seleccion por Ruleta double Sum = 0; double R = Rand.NextDouble(); int Len = VerticeActual.Aristas.Count; clsArista Camino = null; double CostoVertice = 0; foreach (clsArista A in VerticeActual.Aristas) { if (VerticeAnterior.Origen == A.Destino) { continue; } CostoVertice += (Math.Pow(A.Feromona, alpha) / Math.Pow(A.Costo, betha)); } foreach (clsArista A in VerticeActual.Aristas) { if (VerticeAnterior.Origen == A.Destino) { continue; } Sum += (Math.Pow(A.Feromona, alpha) / Math.Pow(A.Costo, betha)) / CostoVertice; if (Sum >= R) { Camino = A; break; } } if (Camino == null) { if (VerticeActual.Aristas[Len - 1].Origen == VerticeAnterior.Origen) { Len--; } Camino = VerticeActual.Aristas[Len - 1]; } //Asigna Siguiente Vertice VerticeAnterior = VerticeActual; VerticeActual = Grafo.findVertice(Camino.Destino); Recorrido += " - " + VerticeActual.Origen; RecorridoCosto += Camino.Costo; H.Visitados.Add(Camino); } //Fin Recorrido de Hormiga H.RecorridoTotal = RecorridoCosto; Console.WriteLine("G: " + i + " N: " + j + " Recorrido: " + Recorrido); Hormigas.Add(H); } //Deposito de Feromona foreach (clsVertice VG in Grafo.Vertices) { foreach (clsArista A in VG.Aristas) { double Sum = 0; foreach (clsHormiga H in Hormigas) { foreach (clsArista AH in H.Visitados) { if (A.Origen == AH.Origen && A.Destino == AH.Destino) { Sum += Q / H.RecorridoTotal; break; } } } A.Feromona = ((1 - p) * A.Feromona) + Sum; } } } Console.ReadKey(); }
public frmGrafo(clsGrafo clsGrafo) { this.clsGrafo = clsGrafo; InitializeComponent(); }
public ActionResult InformacionConsulta(string file) { clsGrafo obGrafo = new clsGrafo(); try { if (string.IsNullOrEmpty(file)) { throw new Exception("Problem occurred with file upload"); } string archivo = file; var allowedVertex = new[] { "A", "B", "C", "D", "E" }; List <clsVertex> lstVertex = new List <clsVertex>(); #region GENERACION LISTA ADYACENTE string[] lstCordenadas = archivo.Trim().ToUpper().Split(','); char[] lstVertexName = archivo.ToUpper().Where(x => char.IsLetter(x)).Distinct().OrderBy(x => x).ToArray(); foreach (var item in lstVertexName) { if (allowedVertex.Contains(item.ToString())) { obGrafo.addVertex(new clsVertex { Name = item.ToString(), lstEdge = new List <clsEdge>(), Estado = false }); } else { throw new Exception(string.Format("Can not be added to a vertex with the name: { 0 }", item.ToString())); } } foreach (var item in lstCordenadas) { if (!char.IsNumber(item[0]) && !char.IsNumber(item[1]) && char.IsNumber(item[2]) && !item[0].Equals(item[1])) { obGrafo.addEdge(new clsEdge { Vertex1 = item[0], Vertex2 = item[1], Cost = float.Parse(item[2].ToString()) }); } else { throw new Exception(string.Format("Coordinate: {0} is incorrect, check the rules", item.ToString())); } } #endregion FIN GENERACION LISTA ADYACENTE } catch (Exception ex) { obGrafo.MensajeError = ex.Message; return(View(obGrafo)); } #region COSNTRUCCION SALIDA List <clsQuery> lstSalida = new List <clsQuery>(); lstSalida.Add(new clsQuery { question = "The distance of the route A-B-C", answer = obGrafo.getDistance("ABC") }); lstSalida.Add(new clsQuery { question = "The distance of the route A-D", answer = obGrafo.getDistance("AD") }); lstSalida.Add(new clsQuery { question = "The distance of the route A-D-C ", answer = obGrafo.getDistance("ADC") }); lstSalida.Add(new clsQuery { question = "The distance of the route A-E-B-C-D", answer = obGrafo.getDistance("AEBCD") }); lstSalida.Add(new clsQuery { question = "The distance of the route A-E-D ", answer = obGrafo.getDistance("AED") }); List <clsCamino> Item6 = new List <clsCamino>(); obGrafo.getRouteByStops(obGrafo.lstVertex, "C", "C", 4, "MAX", Item6, string.Empty); string item6 = Item6.Count().ToString(); lstSalida.Add(new clsQuery { question = "The number of trips starting at C and ending at C with a maximum of 3 stops", answer = item6 }); List <clsCamino> Item7 = new List <clsCamino>(); obGrafo.getRouteByStops(obGrafo.lstVertex, "A", "C", 5, "EQUAL", Item7, string.Empty); string item7 = Item7.Count().ToString(); lstSalida.Add(new clsQuery { question = "The number of trips starting at A and ending at C with exactly 4 stops.", answer = item7 }); List <clsCamino> Item8 = new List <clsCamino>(); obGrafo.getRoutes(obGrafo.lstVertex, "A", "C", Item8, string.Empty); string item8 = Item8.Min(x => x.costo).ToString(); lstSalida.Add(new clsQuery { question = "The length of the shortest route (in terms of distance to travel) from A to C.", answer = item8 }); List <clsCamino> Item9 = new List <clsCamino>(); obGrafo.getRoutes(obGrafo.lstVertex, "B", "B", Item9, string.Empty); string item9 = Item9.Min(x => x.costo).ToString(); lstSalida.Add(new clsQuery { question = "The length of the shortest route (in terms of distance to travel) from B to B. ", answer = item9 }); obGrafo.lstQuerys = lstSalida; #endregion FIN CONSTRUCCION SALIDA return(View(obGrafo)); //List<clsCamino> Item8 = new List<clsCamino>(); //obGrafo.getRouteByCost(obGrafo.lstVertex, "C", "C", 30, "MAX", Item8, string.Empty); //string item8 = Item8.Count().ToString(); }
// GET: Grafo public ActionResult Index() { clsGrafo obGrafo = new clsGrafo(); return(View(obGrafo)); }