private void button1_Click(object sender, EventArgs e) { reInitTableau(); Cell caseDepart = new Cell(comboBoxDepartX.SelectedIndex + 1, comboBoxDepartY.SelectedIndex + 1); if (!Obstacles.chercheObstacle(caseDepart)) { arrivee = new Cell(comboBoxArriveeX.SelectedIndex + 1, comboBoxArriveeY.SelectedIndex + 1); depart = new Cell(comboBoxDepartX.SelectedIndex + 1, comboBoxDepartY.SelectedIndex + 1); Cell.Arrivee = arrivee; Graph G = new Graph(); List <GenericNode> solution = G.RechercheSolutionAEtoile(depart); List <GenericNode> fermes = G.L_Fermes; foreach (GenericNode c in fermes) { Cell cBis = (Cell)c; tableauImage[cBis.X, cBis.Y].BackColor = Color.LightSteelBlue; } foreach (GenericNode c in solution) { Cell cBis = (Cell)c; tableauImage[cBis.X, cBis.Y].BackColor = Color.Blue; } } tableauImage[arrivee.X, arrivee.Y].BackColor = Color.Red; tableauImage[arrivee.X, arrivee.Y].ImageLocation = @"../../Zelda.png"; tableauImage[depart.X, depart.Y].BackColor = Color.Green; tableauImage[depart.X, depart.Y].ImageLocation = @"../../Link.png"; }
//Retourne le chemin le plus court entre depart et arrivée, avec le cout de ce chemin en valeur de sortie supplémentaire private List <GenericNode> getBestPath(Cell depart, Cell arrivee, out double cost) { Graph G = new Graph(); Cell.Arrivee = arrivee; List <GenericNode> solution = G.RechercheSolutionAEtoile(depart); cost = 0; for (int i = 1; i < solution.Count; i++) { cost += solution.ElementAt(i - 1).GetArcCost(solution.ElementAt(i)); } return(solution); }
private void button1_Click(object sender, EventArgs e) { reInitTableau(); Cell caseDepart = new Cell(comboBoxDepartX.SelectedIndex + 1, comboBoxDepartY.SelectedIndex + 1); List <Cell> points = new List <Cell>(); if (!Obstacles.chercheObstacle(caseDepart)) { points.Add(new Cell(comboBoxPoint1X.SelectedIndex + 1, comboBoxPoint1Y.SelectedIndex + 1)); points.Add(new Cell(comboBoxPoint2X.SelectedIndex + 1, comboBoxPoint2Y.SelectedIndex + 1)); points.Add(new Cell(comboBoxPoint3X.SelectedIndex + 1, comboBoxPoint3Y.SelectedIndex + 1)); points.Add(new Cell(comboBoxPoint4X.SelectedIndex + 1, comboBoxPoint4Y.SelectedIndex + 1)); depart = new Cell(comboBoxDepartX.SelectedIndex + 1, comboBoxDepartY.SelectedIndex + 1); //Tous les points dans un tableau (dont le départ) allPoints = new Cell[points.Count + 1]; allPoints[0] = depart; for (int i = 0; i < points.Count; i++) { allPoints[i + 1] = points[i]; } //distances[i,j] = longueur la plus courte entre allPoints[i] et allPoints[j] distances = new double[allPoints.Length, allPoints.Length]; //paths[i,j] = chemin le plus court entre allPoints[i] et allPoints[j] List <GenericNode>[,] paths = new List <GenericNode> [allPoints.Length, allPoints.Length]; for (int i = 0; i < allPoints.Length; i++) { for (int j = i; j < allPoints.Length; j++) { double cost = 0; List <GenericNode> bestPath = getBestPath(allPoints[i], allPoints[j], out cost); distances[i, j] = cost; distances[j, i] = cost; paths[i, j] = new List <GenericNode>(bestPath); bestPath.Reverse(); paths[j, i] = bestPath; } } Point.Arrivee = depart; Cell[] startingPath = new Cell[] { depart }; Point startingPoint = new Point(startingPath, allPoints); Graph G = new Graph(); List <GenericNode> solution = G.RechercheSolutionAEtoile(startingPoint); //Récupération de la solution en termes de cellules pour l'affichage List <GenericNode> usableSolution = new List <GenericNode>(); Point lastNode = (Point)solution.Last(); for (int i = 1; i < lastNode.Traverses.Length; i++) { int previousIndex = Array.FindIndex(allPoints, n => n.IsEqual(lastNode.Traverses[i - 1])); int nextIndex = Array.FindIndex(allPoints, n => n.IsEqual(lastNode.Traverses[i])); usableSolution = usableSolution.Concat(paths[previousIndex, nextIndex]).ToList(); } //Affichage foreach (GenericNode c in usableSolution) { Cell c2 = (Cell)c; tableauImage[c2.X, c2.Y].BackColor = Color.Blue; } for (int i = 0; i < points.Count; i++) { Cell c = points.ElementAt(i); tableauImage[c.X, c.Y].BackColor = Color.BlueViolet; tableauImage[c.X, c.Y].ImageLocation = @"../../Element" + (i + 1).ToString() + ".png"; } tableauImage[depart.X, depart.Y].BackColor = Color.Green; tableauImage[depart.X, depart.Y].ImageLocation = @"../../Link.png"; } }
private void Chemin_Click(object sender, EventArgs e) { NodeGraph depart = new NodeGraph(Depart.Text[0].ToString().ToUpper()); depart.setMatrice(matriceAdjacente); NodeGraph arrive = new NodeGraph(Arrive.Text[0].ToString().ToUpper()); depart.setEndNode(arrive.GetNom()); Graph g = new Graph(); List<GenericNode> Solution = g.RechercheSolutionAEtoile(depart); string reponse = "Le chemin est : "; string poids = "Son poids est : "; foreach (GenericNode Gn in Solution) { reponse += Gn.GetNom() + " "; } MessageBox.Show(reponse + "\n" + poids + CalculPoids(Solution).ToString()); g.GetSearchTree(arbre); }
private void CalculQuestion4_Click(object sender, EventArgs e) { string etape = this.etapeLaiterieRetour.Text; Regex myRegex = new Regex("(,)"); etape = myRegex.Replace(etape, ""); //N*N-1 Génération de parcours List<char> listeChar = new List<char>(); List<String> ListesChemins = new List<string>(); List<String> etapesList = new List<string>(); foreach (char lettre in etape) { etapesList.Add(lettre.ToString()); listeChar.Add(lettre); } #region Calcul d'une heuristique avec glouton /**Calcul rapide d'une heuristique en utilisant un algoithme glouton**/ NodeGraph depart = new NodeGraph("A"); depart.setMatrice(matriceAdjacente); double poidsMin = int.MaxValue; List<GenericNode> SolutionEtape = new List<GenericNode>(); string EtapeName = "A"; List<GenericNode> Solution = new List<GenericNode>(); Graph Graphe = new Graph(); int cptGlouton = 0; while (etapesList.Count != 0) { foreach (String nodeName in etapesList) { depart.setEndNode(nodeName); List<GenericNode> SolutionTmp = Graphe.RechercheSolutionAEtoile(depart); if (poidsMin > CalculPoids(SolutionTmp)) { EtapeName = nodeName; poidsMin = CalculPoids(SolutionTmp); SolutionEtape = SolutionTmp; } } poidsMin = int.MaxValue; foreach (GenericNode Gn in SolutionEtape) { if (Solution.Count != 0) { if (Solution.Last().GetNom() != Gn.GetNom()) { Solution.Add(Gn); if (Gn.GetNom() == "A") { cptGlouton = 0; } } } else Solution.Add(Gn); } cptGlouton++; //MessageBox.Show(cptGlouton.ToString()); if (cptGlouton == 4) { depart = new NodeGraph(EtapeName); depart.setEndNode("A"); List<GenericNode> RetourA = Graphe.RechercheSolutionAEtoile(depart); foreach (GenericNode Gn in RetourA) { if (Solution.Count != 0) { if (Solution.Last().GetNom() != Gn.GetNom()) { Solution.Add(Gn); } } else Solution.Add(Gn); } cptGlouton = 0; depart = new NodeGraph("A"); etapesList.Remove(EtapeName); } else { depart = new NodeGraph(EtapeName); etapesList.Remove(EtapeName); } } NodeGraph retourDepart = new NodeGraph(Solution.Last().GetNom()); retourDepart.setEndNode("A"); SolutionEtape = Graphe.RechercheSolutionAEtoile(retourDepart); foreach (GenericNode Gn in SolutionEtape) { if (Solution.Count != 0) { if (Solution.Last().GetNom() != Gn.GetNom()) { Solution.Add(Gn); } } else Solution.Add(Gn); } string reponse = "Le chemin est : "; string poids = "Son poids est : "; foreach (GenericNode Gn in Solution) { reponse += Gn.GetNom() + " "; } //MessageBox.Show(reponse + "\n" + poids + CalculPoids(Solution).ToString()); double borneMax = CalculPoids(Solution); #endregion #region Algorithme du voyageur du commerce avec retour GraphVoyageur Voyageur = new GraphVoyageur(); MethodeRecursive("", listeChar, ListesChemins); List<GenericNode> SolutionOptimale = new List<GenericNode>(); double poidsVoyageur = int.MaxValue; double poidsCourant = 0; foreach (string combinaison in ListesChemins) { List<String> CheminVoyageur = new List<string>(); int cptStandart = 0; List<GenericNode> SolutionTmp = new List<GenericNode>(); List<GenericNode> SolutionEtapeVoyageur = new List<GenericNode>(); foreach (char etapeVoyageur in combinaison) { CheminVoyageur.Add(etapeVoyageur.ToString()); } NodeGraph departVoyageur = new NodeGraph("A", poidsCourant); departVoyageur.setBorneMax((int)borneMax); bool fin = true; List<String> aIgnore = new List<string>(); foreach (String nodeName in CheminVoyageur) { if (!aIgnore.Contains(nodeName)) { double poidsTemp = CalculPoids(SolutionTmp); if (poidsTemp < borneMax && fin != false) { cptStandart++; departVoyageur.setEndNode(nodeName); SolutionEtapeVoyageur = Voyageur.RechercheSolutionAEtoile(departVoyageur); foreach (GenericNode Gn in SolutionEtapeVoyageur) { if (nodeName.Contains(Gn.GetNom()) && cptStandart != 4) { cptStandart++; aIgnore.Add(nodeName); } if (SolutionTmp.Count != 0) { if (SolutionTmp.Last().GetNom() != Gn.GetNom()) { SolutionTmp.Add(Gn); } } else SolutionTmp.Add(Gn); } if (cptStandart == 4) { poidsCourant = CalculPoids(SolutionTmp); departVoyageur = new NodeGraph(nodeName, poidsCourant); departVoyageur.setEndNode("A"); List <GenericNode> SolutionRetourA = Voyageur.RechercheSolutionAEtoile(departVoyageur); foreach (GenericNode Gn in SolutionRetourA) { if (SolutionTmp.Count != 0) { if (SolutionTmp.Last().GetNom() != Gn.GetNom()) { SolutionTmp.Add(Gn); } } else SolutionTmp.Add(Gn); } cptStandart = 0; poidsCourant = CalculPoids(SolutionTmp); departVoyageur = new NodeGraph("A", poidsCourant); } else { poidsCourant = CalculPoids(SolutionTmp); departVoyageur = new NodeGraph(nodeName, poidsCourant); } if (SolutionEtapeVoyageur.Count == 0) { fin = false; SolutionTmp.Clear(); } } } } departVoyageur.setEndNode("A"); SolutionEtapeVoyageur = Voyageur.RechercheSolutionAEtoile(departVoyageur); foreach (GenericNode Gn in SolutionEtapeVoyageur) { if (SolutionTmp.Count != 0) { if (SolutionTmp.Last().GetNom() != Gn.GetNom()) { SolutionTmp.Add(Gn); } } else SolutionTmp.Add(Gn); } if (poidsVoyageur > CalculPoids(SolutionTmp)) { poidsVoyageur = CalculPoids(SolutionTmp); SolutionOptimale = SolutionTmp; } } string reponseOptimale = "Le chemin optimal est : "; string poidsOptimal = "Son poids est : "; foreach (GenericNode Gn in SolutionOptimale) { reponseOptimale += Gn.GetNom() + " "; } MessageBox.Show(reponseOptimale + "\n" + poidsOptimal + CalculPoids(SolutionOptimale).ToString()); #endregion }
private int[,] matriceEtape(List<string> etapesList) { etapesList.Insert(0, "A"); int taille = etapesList.Count; int[,] matriceLaiterie = new int[taille, taille]; for (int i = 0; i < taille; i++) { for (int j = 0; j < taille; j++) { if (i != j) { NodeGraph depart = new NodeGraph((etapesList[i])); depart.setMatrice(matriceAdjacente); Graph Graphe = new Graph(); depart.setEndNode(etapesList[j]); List<GenericNode> SolutionTmp = Graphe.RechercheSolutionAEtoile(depart); matriceLaiterie[i, j] = Convert.ToInt32(CalculPoids(SolutionTmp)); } else { matriceLaiterie[i, j] = -1; } } } return matriceLaiterie; }