1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
'projet principal frmFloydWarshal
'----------------------------------------------------------------------
'-----------------Version Fichier Texte Delimite .CSV------------------
'----------------------------------------------------------------------
'1 AJOUTER REFERENCE À :
'- Microsoft.Glee.dll,Microsoft.Glee.Drawing.dll
'- QuickGraph.dll , QuickGraph.Glee.dll
'- Microsot Excel 11.0 Object Library(office 2003) ou bien
'- Microsot Excel 12.0 Object Library(office 2007)
'2 AJOUTE CONTROLES :
'- 2 TextBox,2 Button
'3 DANS BOITE OUTILS (rechercher ce controle):
'- ajouter un element->selectionne->GViewer.dll->ok
'4 AJOUTER LE CONTROLE GVIEWER
'- mettre le GVIEWER (qui apparait) sur la forme frmFloydWarshal
'5 GENERATION DU PROJET:
'- dans projet frmFloydWarshal ajouter reference->projets>InterFacevb->0k
'6- GENERER LA SOLUTION
Option Explicit On
Option Compare Text
Imports System
Imports System.Globalization
Imports System.Threading
Imports System.Windows
Imports System.Collections
Imports System.Drawing
'imports pour MS Office Excel 2003
Imports Microsoft.Office.Interop
'imports pour le controle Microsoft GLEE
Imports Microsoft
Imports Microsoft.Glee
Imports Microsoft.Glee.GleeGraph
Imports Microsoft.Glee.Drawing
Imports Microsoft.Glee.GraphViewerGdi
Imports Microsoft.Glee.Splines
Imports Microsoft.Glee.DataBase
Imports Microsoft.Glee.NativeMethods
'imports pour la librairie des graphes(QuickGraph)
Imports QuickGraph
Imports Algo_Extension = QuickGraph.Algorithms.AlgorithmExtensions
Imports Algo_PredObservSommetEnreg = QuickGraph.Algorithms.Observers.VertexPredecessorRecorderObserver(Of String, QuickGraph.Edge(Of String))
Imports Algo_PredObservArcEnreg = QuickGraph.Algorithms.Observers.EdgeRecorderObserver(Of String, QuickGraph.Edge(Of String))
Imports Algo_SommetDistObservEnreg = QuickGraph.Algorithms.Observers.VertexDistanceRecorderObserver(Of String, QuickGraph.Edge(Of String))
Imports FLOYDWARSHAL = QuickGraph.Algorithms.ShortestPath.FloydWarshallAllShortestPathAlgorithm(Of String, QuickGraph.Edge(Of String))
'imports pour les methodes d'extensions
Imports QuickGraph.Glee
'imports pour projet interfaceVB(classes Lien et LienChemin)
Imports InterfaceVB
Public Class frmFloydWarshal
'objets donnees liens de MS Excel
'--------------------------------------------------
Public listeLiens As List(Of Lien) = New List(Of Lien)
Public objLien As Lien
'Lib QuickGraph (Partie graphe)
'------------------------------
'objet public graphe d'adjacence simple
Public objGraph As AdjacencyGraph(Of String, Edge(Of String))
'objet un lien du graphe
Dim lienGraphe As QuickGraph.Edge(Of String)
'objet "cout d'un lien" de type Dictionnaire (ArcGraph,Cout)
Dim coutLien As Dictionary(Of Edge(Of String), Double)
'objet delegue "fonction cout lien" (methode d'aide bibliotheque)
Dim FNCout As Func(Of Edge(Of String), Double)
' objet Algorithme "FloydWarshal"
Public objFloydWarshall As FLOYDWARSHAL
'Lib MS GLEE(Visualisation graphique)
'-------------------------------------
Public grapheVisuel As Microsoft.Glee.Drawing.Graph
'Objet classeur MS Excel 2003
'----------------------------
Dim appExcel As Excel.Application
'-----------Ici declaration pour Fichier Texte-----------
'Objet Fichier Texte Delimite par virgule au lieu d'Excel
'--------------------------------------------------------
Public ligneCourante As String()
Public NbLigne As Integer
'Objets "choix d'un chemin a afficher"
'------------------------------------
Public objLienChemin As LienChemin
Public listeLienChemin As List(Of LienChemin) = New List(Of LienChemin)
Public objCheminSel As LienChemin
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent()
End Sub
'Recherche le fichier -Excel ou bien Texte-contenant les arcs du graphe
Private Sub btnOuvreFichArcs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOuvreFichLiens.Click
Dim nomFichierLiens As String = ""
Dim dlgOuvreFichier As OpenFileDialog = New OpenFileDialog
dlgOuvreFichier.Filter = "Fichier Arcs(*.xls)|*.xls|Fichier Texte(*.csv)|*.csv"
If dlgOuvreFichier.ShowDialog = Forms.DialogResult.OK Then
nomFichierLiens = dlgOuvreFichier.FileName
If Len(nomFichierLiens) = 0 Then
MessageBox.Show("entrer un nom de fichier svp...")
Exit Sub
Else
If nomFichierLiens.EndsWith("xls") Then
'Si fichier excel trouve
Call LitFichierGraphe(nomFichierLiens)
Else
'Si fichier texte delimite trouve
Call LitFichierGrapheTexte(nomFichierLiens)
End If
Call RemplitGraphe()
Call demarreFloydWarshal()
End If
End If
End Sub
'----------------Ici Sub Lecture Fichier Texte---------------
'Lit le fichier Texte et charge liens(arcs) du graphe
'lit autant lignes qu'il y en a jusqu'à Fin de Fichier
Private Sub LitFichierGrapheTexte(ByVal nomFichierLiens As String)
'Utilise fonction VB dedie au fichier texte delimite
Using Lecteur As New Microsoft.VisualBasic.FileIO.TextFieldParser _
(nomFichierLiens)
Lecteur.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
'Option pour le point virgule(ou virgule)
Lecteur.SetDelimiters(";")
NbLigne = 1
While Not Lecteur.EndOfData
Try
ligneCourante = Lecteur.ReadFields()
Dim champCourant As String = ""
Dim numChamp As Integer = 1
Dim monLien As Lien = New Lien
For Each champCourant In ligneCourante
'si nbligne =1 -> saute en-tete
'sinon traiter ligne
If NbLigne > 1 Then
Select Case numChamp
Case 1
monLien.sommetNom = champCourant
Case 2
monLien.Description = champCourant
Case 3
monLien.sommetLien = champCourant
Case 4
monLien.Cout = Double.Parse(champCourant)
Case 5
monLien.monType = champCourant
Case 6
monLien.motPasse = champCourant
Case Else
End Select
numChamp = numChamp + 1
End If
'MessageBox.Show(numChamp.ToString & "-" & champCourant)
Next
If NbLigne > 1 Then
'ajout à liste liens
listeLiens.Add(monLien)
End If
NbLigne = NbLigne + 1
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Ligne " & ex.Message & _
"n'est pas valide,malformee et sera sautée.")
End Try
End While
End Using
End Sub
'Lit le fichier excel et charge liens(arcs) du graphe
Private Sub LitFichierGraphe(ByVal nomFichierLiens As String)
Dim nbLig As Integer = 12
Dim nbCol As Integer = 5
If appExcel IsNot Nothing Then
appExcel.Quit()
End If
Try
Dim appExcel As Excel.Application = New Excel.Application
appExcel.Visible = True
Dim monClasseur As Excel.Workbook = appExcel.Workbooks.Open(nomFichierLiens)
Dim maFeuille As Excel.Worksheet = monClasseur.Worksheets(1)
For I As Integer = 2 To nbLig
objLien = New Lien
For J As Integer = 1 To nbCol
Select Case J
Case 1
objLien.sommetNom = CType(maFeuille.Cells(I, J).value, String)
Case 2
objLien.Description = CType(maFeuille.Cells(I, J).value, String)
Case 3
objLien.sommetLien = CType(maFeuille.Cells(I, J).value, String)
Case 4
objLien.Cout = CType(maFeuille.Cells(I, J).value, Double)
Case 5
objLien.monType = CType(maFeuille.Cells(I, J).value, String)
End Select
Next
'ajout à liste liens
listeLiens.Add(objLien)
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
If appExcel IsNot Nothing Then
appExcel.Quit()
End If
End Try
End Sub
'Remplit(populate) le graphe avec les donnes liens
Private Sub RemplitGraphe()
'parcourt liste des liens
'Ajoute Sommets et Liens au graphe
objGraph = New AdjacencyGraph(Of String, Edge(Of String))
coutLien = New Dictionary(Of Edge(Of String), Double)(objGraph.EdgeCount)
For Each objLien As Lien In listeLiens
lienGraphe = New QuickGraph.Edge(Of String)(objLien.sommetNom, objLien.sommetLien)
objGraph.AddVerticesAndEdge(lienGraphe)
'Ajoute "le cout du lien" au Dictionnaire "Cout Lien"
coutLien.Add(lienGraphe, Double.Parse(objLien.Cout))
Next
'Assigne à la fonction "FNCout" delegue
'le dictionnaire "CoutLien"
FNCout = Algo_Extension.GetIndexer(coutLien)
End Sub
'ExecuteFloydWarshal
Private Sub demarreFloydWarshal()
'Initialise Algo FLOYDWARSHAL
objFloydWarshall = New FLOYDWARSHAL(objGraph, FNCout)
'Execute Algo "FLOYDWARSHALL"
objFloydWarshall.Compute()
'Affiche lees chemins dans TextBox
Call AfficheChemin()
End Sub
'Affiche le chemin visite dans TextBox.
Private Sub AfficheChemin()
txtListeSommet.ForeColor = System.Drawing.Color.DarkOliveGreen
txtListeSommet.Font = New Font("courier new", 10, FontStyle.Bold)
'compte sommets(ou noeuds) du "graphe "
txtListeSommet.Text = "nombre Sommets :" & objGraph.Vertices.Count.ToString & vbCrLf & vbCrLf
'compte liens (ou arcs) du "graphe "
txtListeSommet.Text = txtListeSommet.Text & "nombre Liens :" & objGraph.Edges.Count.ToString & vbCrLf & vbCrLf
'Retrouve le cout chemin avec fonction TryGetDistance
'Retrouve les liens chemin avec fonction TryGetPath
txtChemin.Text = ""
Dim objCoutChemin As Double = 0.0
Dim objChemin As IEnumerable(Of QuickGraph.Edge(Of String)) = Nothing
For Each objSource In objGraph.Vertices
For Each objDestination In objGraph.Vertices
's'il y un chemin entre Source et Destination
'liste le nom chemin et cout
If objFloydWarshall.TryGetDistance(objSource, objDestination, objCoutChemin) Then
txtChemin.Text = txtChemin.Text & _
"chemin de : " & objSource & " -> " & objDestination & " Cout: " & objCoutChemin.ToString & vbCrLf
End If
'liste les liens du chemin
If objFloydWarshall.TryGetPath(objSource, objDestination, objChemin) Then
For Each objLienGraphe In objChemin
txtChemin.Text = txtChemin.Text & objLienGraphe.Source & " -" & objLienGraphe.Target & vbCrLf
Next
End If
txtChemin.Text = txtChemin.Text & " --------- " & vbCrLf
Next 'au chemin suivant
Next
End Sub
Private Sub btnAfficheGraphe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAfficheGraphe.Click
'Efface GViever
GViewer_FW.Graph = Nothing
GViewer_FW.Refresh()
'Raz de la liste chemin
listeLienChemin.Clear()
'Affiche forme de selection Chemin
Dim objFrm As frmChemin = New frmChemin
objFrm.ShowDialog()
'Evenements noeudAjoute and lienAjoute
Dim noeudAjoute As New QuickGraph.Glee.GleeVertexNodeEventHandler(Of String)(AddressOf populator_NodeAdded)
Dim arcAjoute As New QuickGraph.Glee.GleeEdgeEventHandler(Of String, Edge(Of String))(AddressOf populator_EdgeAdded)
'Populator sert à peupler le "graphe du gviewer" avec les elements du "graphe quickgraph"
Dim populator As QuickGraph.Glee.GleeGraphPopulator(Of String, QuickGraph.Edge(Of String)) = _
GleeGraphExtensions.CreateGleePopulator(objGraph)
populator.Compute()
'On obtient le graphe visuel pour MS GLEE
grapheVisuel = populator.VisitedGraph.ToGleeGraph(noeudAjoute, arcAjoute)
'prepare le visualiseur Gviewer
'active options Sauvegarde & Scroll
GViewer_FW.SaveButtonVisible = True
GViewer_FW.AutoScroll = True
'Affiche le "graphe visuel" dans Gviewer
GViewer_FW.Graph = grapheVisuel
End Sub
'cet evenement permet de mettre en forme les sommets(noeuds)
'de GViewer (declenche par populateur)
Private Sub populator_NodeAdded(ByVal sender As Object, ByVal e As QuickGraph.Glee.GleeVertexEventArgs(Of String))
Dim NoeudStyle As New Microsoft.Glee.Drawing.Style
NoeudStyle = Microsoft.Glee.Drawing.Style.Rounded
Dim noeudGlee As Microsoft.Glee.Drawing.Node = e.Node
Dim nomNoeud As String = e.Vertex
'mise en forme specifique pour sommet "A"
If nomNoeud = "A" Then
noeudGlee.Attr.Color = Microsoft.Glee.Drawing.Color.Black
noeudGlee.Attr.Fillcolor = Microsoft.Glee.Drawing.Color.Yellow
noeudGlee.Attr.LineWidth = 2.0
noeudGlee.Attr.Shape = Microsoft.Glee.Drawing.Shape.Box
noeudGlee.Attr.AddStyle(NoeudStyle)
noeudGlee.Attr.FontName = "VERDANA"
noeudGlee.Attr.Fontcolor = Microsoft.Glee.Drawing.Color.Red
noeudGlee.Attr.FontName = "Arial"
noeudGlee.Attr.Fontsize = 10
noeudGlee.Attr.Label = "Racine: " & nomNoeud
'mise en forme par defaut pour les sommets
Else
noeudGlee.Attr.Color = Microsoft.Glee.Drawing.Color.Black
noeudGlee.Attr.Fillcolor = Microsoft.Glee.Drawing.Color.GreenYellow
noeudGlee.Attr.LineWidth = 1.0
noeudGlee.Attr.Shape = Microsoft.Glee.Drawing.Shape.Box
noeudGlee.Attr.AddStyle(NoeudStyle)
noeudGlee.Attr.Fontcolor = Microsoft.Glee.Drawing.Color.Black
noeudGlee.Attr.FontName = "Times New Roman"
noeudGlee.Attr.Fontsize = 8
noeudGlee.Attr.Label = nomNoeud
End If
End Sub
'Cet evenement permet de mettre en forme les liens(arcs)
'de GViewer (declenche par populateur)
Private Sub populator_EdgeAdded(ByVal sender As Object, ByVal e As QuickGraph.Glee.GleeEdgeEventArgs(Of String, Edge(Of String)))
Dim arcStyle As Microsoft.Glee.Drawing.Style
arcStyle = Microsoft.Glee.Drawing.Style.Solid
Dim arcGlee As Microsoft.Glee.Drawing.Edge = e.GEdge
Dim coutChemin As Double = 0.0
'mise en forme par defaut pour les liens
arcGlee.Attr.Color = Microsoft.Glee.Drawing.Color.Blue
arcGlee.Attr.LineWidth = 1.0
arcGlee.Attr.AddStyle(arcStyle)
arcStyle = Microsoft.Glee.Drawing.Style.Bold
arcGlee.Attr.AddStyle(arcStyle)
arcGlee.Attr.FontName = "Verdana"
arcGlee.Attr.Fontsize = 8
arcGlee.Attr.Fontcolor = Microsoft.Glee.Drawing.Color.Red
If objFloydWarshall.TryGetDistance(e.Edge.Source, e.Edge.Target, coutChemin) Then
arcGlee.Attr.Label = coutChemin.ToString
End If
'mise en en forme pour un chemin selectionne
' Si un chemin a ete selectionne
If objCheminSel IsNot Nothing Then
For Each elemLien In listeLienChemin
'Si lien appartient à chemin selectionne
'alors chemin en gras et rouge
If elemLien.ID = objCheminSel.ID Then
If e.Edge.Source = elemLien.Source And e.Edge.Target = elemLien.Destination Then
arcGlee.Attr.LineWidth = 2
arcGlee.Attr.Color = Microsoft.Glee.Drawing.Color.Red
End If
End If
Next
End If
End Sub
End Class
'frmChemin boite de selection d'un chemin
'à ajouter au projet principal frmFloydWarshal
'-----------------------------------------
Imports InterfaceVB
Public Class frmChemin
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
Private Sub frmChemin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.AcceptButton = Me.btnOK
Me.CancelButton = Me.btnAnnuler
Me.MaximumSize = New Size(276, 158)
'Parcourir matrice sommet x sommet à la recheche des chemins
'trouve par algo FloydWarshal
'Remplir liste des chemins dans listeLienChemin
'Charge comboxbox avec listeLienChemin
Dim Chemin As IEnumerable(Of QuickGraph.Edge(Of String)) = Nothing
Dim objID As Integer = 0
For Each source In frmFloydWarshal.objGraph.Vertices
For Each destination In frmFloydWarshal.objGraph.Vertices
If frmFloydWarshal.objFloydWarshall.TryGetPath(source, destination, Chemin) Then
objID = objID + 1
For Each objLienGraphe In Chemin
frmFloydWarshal.objLienChemin = New LienChemin
frmFloydWarshal.objLienChemin.NomChemin = source & "->" & destination & " " & objID.ToString
frmFloydWarshal.objLienChemin.Source = objLienGraphe.Source
frmFloydWarshal.objLienChemin.Destination = objLienGraphe.Target
frmFloydWarshal.objLienChemin.ID = objID
frmFloydWarshal.listeLienChemin.Add(frmFloydWarshal.objLienChemin)
Me.cboChoixChemin.Items.Add(frmFloydWarshal.objLienChemin)
Next
End If
Next
Next
Me.cboChoixChemin.DisplayMember = "NomChemin"
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
'valide selection s'il y en a
Me.Close()
End Sub
Private Sub btnAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuler.Click
'Raz liste des chemins
frmFloydWarshal.listeLienChemin.Clear()
Me.Close()
End Sub
Private Sub cboChoixChemin_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboChoixChemin.SelectedValueChanged
frmFloydWarshal.objCheminSel = CType(cboChoixChemin.SelectedItem, LienChemin)
End Sub
End Class
'projet projet interfaceVB
'--------------------------
'classe Arc sert à stocker les arcs du fichier excel
Public Class Lien
Public sommetNom As String
Public Description As String
Public sommetLien As String
Public Cout As Double
Public monType As String
Public motPasse As String
Public Sub New()
Me.sommetNom = ""
Me.sommetLien = ""
Me.Description = "Neant"
Me.Cout = 0.0
Me.monType = ""
Me.motPasse = ""
End Sub
End Class
'Classe LienChemin à ajouter
'-----------------------------------
'classe LienChemin sert à stocker les arcs de chaque chemin trouve
Public Class LienChemin
Private m_sommetSrc, m_sommetDest As String
Private m_nomchemin As String
Private m_ID As Integer
Private m_coutChemin As Double
Public Sub New()
Me.m_sommetSrc = ""
Me.m_sommetDest = ""
Me.m_coutChemin = 0.0
m_nomchemin = ""
End Sub
Public Property Source() As String
Get
Return m_sommetSrc
End Get
Set(ByVal value As String)
m_sommetSrc = value
End Set
End Property
Public Property Destination() As String
Get
Return m_sommetDest
End Get
Set(ByVal value As String)
m_sommetDest = value
End Set
End Property
Public Property CoutChemin() As String
Get
Return m_coutChemin
End Get
Set(ByVal value As String)
m_coutChemin = value
End Set
End Property
Public Property ID() As Integer
Get
Return m_ID
End Get
Set(ByVal value As Integer)
m_ID = value
End Set
End Property
Public Property NomChemin() As String
Get
Return m_nomchemin
End Get
Set(ByVal value As String)
m_nomchemin = value
End Set
End Property
End Class |