Bjr à vous,
Etant sur une implémentation d'un algo de recherche de plus court chemin dans un grand graphe, je cale complètement.
J'utilise ce document comme base: https://www.ljll.math.upmc.fr/pegon/...T/TP06_cor.pdf
Je suis dans la première partie de Q2 du corrigé, entre les lignes 7 et 20.
Après correction d'un oubli, le 'While' se termine et me renvoie une liste de sommets parcourus
je cale maintenant sur la seconde partie de l'algo (lignes 23 à 29 de l'algo): comme les sommets de mon graphe sont reliés par deux arcs de sens opposé, çà boucle indéfiniment.
Par ailleurs, quels sont les tarifs pour une sous-traitance de ce petit développement ?
Utilisation:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 function TGraphe.RechercherIdxNearestNodeOf(const QCurrNoeud: TGrapheNoeud; out QDistance: double): TNumeroNoeud; var i : integer; NbArcsEntrants, NbArcsSortants: Integer; EWE: String; QNoeud: TGrapheNoeud; QArc: TGrapheArc; begin FAfficherMessage(Format('RechercherIdxNearestNodeOf( %d )', [QCurrNoeud.IDStation])); QDistance := Infinity; Result := -1; NbArcsEntrants := length(QCurrNoeud.ListeArcsEntrants); if (NbArcsEntrants > 0) then begin EWE := '-- Arcs entrants: '; for i := 0 to NbArcsEntrants-1 do EWE += format('%d, ', [QCurrNoeud.ListeArcsEntrants[i]]); FAfficherMessage(EWE); end; FAfficherMessage('001'); NbArcsSortants := length(QCurrNoeud.ListeArcsSortants); if (NbArcsSortants > 0) then begin EWE := '-- Arcs sortants: '; for i := 0 to NbArcsSortants-1 do EWE += format('%d, ', [QCurrNoeud.ListeArcsSortants[i]]); FAfficherMessage(EWE); end; if (0 = (NbArcsEntrants + NbArcsSortants)) then exit(-1); if (NbArcsEntrants > 0) then begin for i := 0 to NbArcsEntrants - 1 do begin QArc := GetArc(QCurrNoeud.ListeArcsEntrants[i]); QNoeud := GetNoeud(QArc.IdxNoeudDepart); if (QNoeud.NoeudVisite) then Continue; if (QArc.Longueur < QDistance) then begin QDistance := QArc.Longueur; Result := QArc.IdxNoeudDepart; end; end; end; if (NbArcsSortants > 0) then begin for i := 0 to NbArcsSortants - 1 do begin QArc := GetArc(QCurrNoeud.ListeArcsSortants[i]); QNoeud := GetNoeud(QArc.IdxNoeudArrivee); if (QNoeud.NoeudVisite) then Continue; if (QArc.Longueur < QDistance) then begin QDistance := QArc.Longueur; Result := QArc.IdxNoeudArrivee; end; end; end; FAfficherMessage(Format('*** RechercherIdxNearestNodeOf: %d - Dist = %.2f', [Result, QDistance])); end;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 program Dijkstra; {$mode delphi}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes, SysUtils, CustApp, { you can add units after this } unitgraphes1 ; type TGraphesDijkstra = class(TCustomApplication) private FGraphe: TGraphe; procedure AfficherMessage(const Msg: string; const DoClear: boolean = false); procedure CalculerGraphe(const Ser1, St1, Ser2, St2: integer); procedure FinaliserGraphe(); procedure GenererGraphe(); protected procedure DoRun; override; public constructor Create(TheOwner: TComponent); override; destructor Destroy; override; procedure WriteHelp; virtual; end; { TGraphesDijkstra } procedure TGraphesDijkstra.DoRun; var ErrorMsg: String; begin // quick check parameters ErrorMsg:=CheckOptions('h', 'help'); if ErrorMsg<>'' then begin ShowException(Exception.Create(ErrorMsg)); Terminate; Exit; end; // parse parameters if HasOption('h', 'help') then begin WriteHelp; Terminate; Exit; end; { add your program here } AfficherMessage('Algo de Dijkstra'); GenererGraphe(); CalculerGraphe(1,0, 1,7); FinaliserGraphe(); // stop program loop Terminate; end;[ATTACH=CONFIG]578986[/ATTACH] constructor TGraphesDijkstra.Create(TheOwner: TComponent); begin inherited Create(TheOwner); StopOnException:=True; end; destructor TGraphesDijkstra.Destroy; begin inherited Destroy; end; procedure TGraphesDijkstra.WriteHelp; begin { add your help code here } writeln('Usage: ', ExeName, ' -h'); end; procedure TGraphesDijkstra.AfficherMessage(const Msg: string; const DoClear: boolean = false); begin WriteLn(Msg); end; procedure TGraphesDijkstra.GenererGraphe(); var MyStation, MyStationDep, MyStationArr, ST1, ST2: TGrapheNoeud; i, Nb, QSr, QSt, QNbA, s: Integer; MyVisee, QArc: TGrapheArc; function DispMessageErreur(const ACaption: string): boolean; var EWE: TGrapheLastError; begin EWE := FGraphe.GetLastError(); Result := (EWE.ErrCode <> 0); if (Result) then AfficherMessage(Format('*** %s : %d: %s', [ACaption, EWE.ErrCode, EWE.ErrMsg])); end; begin FGraphe := TGraphe.Create; //try FGraphe.Initialiser(AfficherMessage); FGraphe.BeginNodeList(); FGraphe.AddStation(1, 0, 0.0, 0.0, 0.0); FGraphe.AddStation(1, 1, 10.0, 10.0, 0.0); FGraphe.AddStation(1, 2, 100.0, 0.0, 0.0); FGraphe.AddStation(1, 3, 200.0, 0.0, 0.0); FGraphe.AddStation(1, 4, 300.0, 0.0, 0.0); FGraphe.AddStation(1, 5, 300.0, 100.0, 0.0); FGraphe.AddStation(1, 6, 300.0, 200.0, 0.0); FGraphe.AddStation(1, 7, 450.0, 210.0, 0.0); FGraphe.AddStation(2, 1, 200.0, 100.0, 0.0); FGraphe.AddStation(2, 2, 100.0, 100.0, 0.0); FGraphe.AddStation(2, 3, 100.0, 200.0, 0.0); FGraphe.AddStation(2, 4, 0.0, 200.0, 0.0); FGraphe.AddStation(3, 1, 0.0, 100.0, 0.0); FGraphe.AddStation(4, 1, 200.0, 200.0, 0.0); FGraphe.EndNodesList(); if (DispMessageErreur('Noeuds')) then exit; Nb := FGraphe.GetNbNoeuds(); AfficherMessage(Format('%d noeuds', [Nb])); AfficherMessage(''); FGraphe.BeginArcsList(); FGraphe.AddArcBetweenStations(1,0, 1,1); FGraphe.AddArcBetweenStations(1,1, 1,2); FGraphe.AddArcBetweenStations(1,2, 1,3); FGraphe.AddArcBetweenStations(1,3, 1,4); FGraphe.AddArcBetweenStations(1,4, 1,5); FGraphe.AddArcBetweenStations(1,5, 1,6); FGraphe.AddArcBetweenStations(1,6, 1,7); FGraphe.AddArcBetweenStations(1,3, 2,1); FGraphe.AddArcBetweenStations(2,1, 2,2); FGraphe.AddArcBetweenStations(2,2, 2,3); FGraphe.AddArcBetweenStations(2,3, 2,4); FGraphe.AddArcBetweenStations(2,4, 3,1); FGraphe.AddArcBetweenStations(3,1, 1,1); FGraphe.AddArcBetweenStations(2,3, 4,1); FGraphe.AddArcBetweenStations(4,1, 1,6); FGraphe.EndArcsList(); if (DispMessageErreur('Arcs')) then exit; FGraphe.ListerLesNoeuds('Graphe initial', True); FGraphe.ListerLesArcs('Graphe initial'); end; procedure TGraphesDijkstra.CalculerGraphe(const Ser1, St1, Ser2, St2: integer); begin FGraphe.RechercherPlusCourtChemin(Ser1, St1, Ser2, St2); end; procedure TGraphesDijkstra.FinaliserGraphe(); begin FGraphe.Finaliser(); FreeAndNil(FGraphe); end; var Application: TGraphesDijkstra; begin Application:=TGraphesDijkstra.Create(nil); Application.Title:='Graphes et algo de Dijkstra'; Application.Run; Application.Free; end.
Unité implémentant le graphe:
Sortie
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930 unit UnitGraphes1; // Calcul du chemin minimal dans un réseau GHTopo {$WARNING: A exécuter sans débogage} {$mode delphi} interface uses Classes, SysUtils, math; // messages d'erreur const ERR_GRAPHE_NO_ERROR : integer = 0; ERR_GRAPHE_INITIALISATION : integer = -1; ERR_GRAPHE_EMPTY_LIST_NODES : integer = -2; ERR_GRAPHE_EMPTY_LIST_ARCS : integer = -3; ERR_GRAPHE_NODE_NOT_FOUND : integer = -4; ERR_GRAPHE_ARC_NOT_FOUND : integer = -5; ERR_GRAPHE_SAME_START_END : integer = -6; type TProcAfficherMessage = procedure(const Msg: string; const DoClear: boolean = false) of object; type TNumeroNoeud = type Integer; type TNumeroArc = type Integer; // liste générique (fonctionne très bien) type TListeSimple<T> = class(TFPList) private public procedure ClearListe(); function GetNbElements(): integer; inline; procedure InsertElement(const Idx: integer; const E: T); procedure AddElement(const E: T); // et Rros Minet function GetElement(const Idx: integer): T; inline; procedure PutElement(const Idx: integer; const E: T); inline; function RemoveElement(const Idx: integer): boolean; function RemoveLastElement(): boolean; end; //****************************************************************************** type TIDStation = type Int64; type TGrapheNoeud = record IDStation: TIDStation; X : double; Y : double; Z : double; DistanceMin : double; NoeudVisite : boolean; ListeArcsSortants : array of TNumeroArc; ListeArcsEntrants : array of TNumeroArc; ListeIdxPredecesseurs: array of TNumeroNoeud; ListeIdxSuccesseurs : array of TNumeroNoeud; end; type TGrapheArc = record IdxNoeudDepart : TNumeroNoeud; IdxNoeudArrivee : TNumeroNoeud; Longueur : double; Azimut : double; Pente : double; Parcouru : boolean; end; type TGrapheLastError = record ErrCode: integer; ErrMsg : string; end; //****************************************************************************** // liste des noeuds type TGrapheListeDesNoeuds = class(TListeSimple<TGrapheNoeud>) private public procedure TrierParIDStations(); procedure TrierParDistance(); end; //****************************************************************************** // liste des arcs type TGrapheListeDesArcs = class(TListeSimple<TGrapheArc>) private public end; // liste des noeuds de passage du parcours type TGrapheChemin = class(TListeSimple<TGrapheNoeud>) private public end; //****************************************************************************** type TGraphe = class strict private FXMini: double; FXMaxi: double; FYMini: double; FYMaxi: double; FLastError: TGrapheLastError; procedure MarquerNoeudFromIdx(const Idx: TNumeroNoeud; const B: boolean); function RechercherIdxNearestNodeOf(const QCurrNoeud: TGrapheNoeud; out QDistance: double): TNumeroNoeud; function SetLastError(const QErrCode: integer; const QErrMsg: string): boolean; private FListeDesNoeuds : TGrapheListeDesNoeuds; FListeDesArcs : TGrapheListeDesArcs; FLeCheminTrouve : TGrapheChemin; //FListeDesStationsPartantes : TGrapheListeDesStationsPartantes; FTabArcs : array of integer; FAfficherMessage : TProcAfficherMessage; FNombreNoeudsVisites : integer; function FindNoeudByIDStation(const IDS: TIDStation; out ST: TGrapheNoeud; out IndexOf: TNumeroNoeud): boolean; procedure RecenserLesLiaisons(); procedure SetMinMax(); // complètement du graphe procedure MakeArcsReciproques(); function GetNbNoeudsVisites(): integer; function NoeudsTousVisites(): boolean; public property XMini: double read FXMini; property YMini: double read FYMini; property XMaxi: double read FXMaxi; property YMaxi: double read FYMaxi; // Les utilitaires function Initialiser(const P: TProcAfficherMessage): boolean; procedure Finaliser(); function GetLastError(): TGrapheLastError; function FormatterTIDStation(const QId: TIDStation): string; procedure ListerLesNoeuds(const Caption: string; const DoDisplayDependances: boolean = false); procedure ListerLesArcs(const Caption: string); // Les noeuds procedure BeginNodeList(); procedure AddNoeud(const QNoeud: TGrapheNoeud); function GetNoeud(const Idx: integer): TGrapheNoeud; procedure PutNoeud(const Idx: integer; const QNoeud: TGrapheNoeud); function GetNbNoeuds(): integer; procedure AddStation(const QSerie, QStation: integer; const QX, QY, QZ: double); procedure EndNodesList(); // Les arcs procedure BeginArcsList(); procedure AddArc(const QArc: TGrapheArc); overload; function GetArc(const Idx: integer): TGrapheArc; procedure PutArc(const Idx: integer; const QArc: TGrapheArc); function GetNbArcs(): integer; procedure AddArcBetweenStations(const Ser1, St1, Ser2, St2: integer); overload; procedure EndArcsList(); // spécifique stations topo function GetNoeudCheminTrouve(const Idx: integer): TGrapheNoeud; function GetNbNoeudsCheminTrouve(): integer; function RechercherPlusCourtChemin(const Ser1, St1, Ser2, St2: integer): boolean; end; function MakeTIDStation(const Ser, St: integer): integer; procedure ExtractSerStFromTIDStation(const ID: integer; out Ser, St: integer); implementation uses DummyUnit; // pour contrer l'erreur 'Fin du source non trouvée' const MULT_SERIES: integer = 100000; function MakeTIDStation(const Ser, St: integer): integer; begin Result := Ser * MULT_SERIES + St; end; procedure ExtractSerStFromTIDStation(const ID: integer; out Ser, St: integer); begin Ser := ID div MULT_SERIES; St := ID mod MULT_SERIES; end; // Trier les GrapheStations par ZOrder function SortGrapheNoeudsByIDStation(Item1, Item2: Pointer): Integer; var E1, E2: ^TGrapheNoeud; begin E1 := Item1; E2 := Item2; if (E1^.IDStation < E2^.IDStation) then Result := -1 else if (E1^.IDStation = E2^.IDStation) then Result := 0 else Result := 1; end; // TODO: Revoir cette fonction (bug avec les grades) function GetAzimut(const dx, dy: Double; const Unite: double): double; const TWO_PI = 2 * PI; var a: double; begin a := ArcTan2(dy, dx + 1e-12); if (a < 0) then a := a + TWO_PI; a := 0.50 * PI - a; if (a < 0) then a := a + TWO_PI; Result := a * 0.50 * Unite / pi; end; // retourne la longueur, direction et pente pour dx, dy, dz procedure GetBearingInc(const dx, dy, dz: double; var Dist, Az, Inc: double; const fUB, fUC: Double); var dp: Double; begin; dp := Hypot(dx, dy); Dist := Hypot(dp,dz); Inc := ArcTan2(dz, dp) * 0.5 * fUC / pi; Az := GetAzimut(dx,dy, fUB); end; /////////////////////////////////////////////////////////////////////////////////////////// procedure TListeSimple<T>.ClearListe(); var i, n: Integer; begin //AfficherMessage(Format('%s.ClearListe()', [classname])); n := self.Count; if (n > 0) then for i:=Count-1 downto 0 Do begin if (self.Items[i] <> Nil) then Dispose(self.Items[i]); // Libération self.Delete(i); // Suppression de l'élément end; end; function TListeSimple<T>.GetNbElements: integer; begin Result := self.Count; end; procedure TListeSimple<T>.InsertElement(const Idx: integer; const E: T); var pE: ^T; begin New(pE); pE^ := E; self.Insert(Idx, pE); end; procedure TListeSimple<T>.AddElement(const E: T); var pE: ^T; begin New(pE); pE^ := E; self.Add(pE); end; function TListeSimple<T>.GetElement(const Idx: integer): T; begin Result := T(Items[Idx]^); end; procedure TListeSimple<T>.PutElement(const Idx: integer; const E: T); begin try if (Idx < 0) then exit; T(Items[Idx]^) := E; except end; end; function TListeSimple<T>.RemoveElement(const Idx: integer): boolean; begin Result := False; try Dispose(self.Items[Idx]); self.Delete(Idx); Result := True; except end; end; function TListeSimple<T>.RemoveLastElement(): boolean; var Nb: Integer; begin Nb := self.Count; if (0 = Nb) then Exit(false); result := self.RemoveElement(Nb - 1); end; //****************************************************************************** { TGrapheListeDesNoeuds } procedure TGrapheListeDesNoeuds.TrierParIDStations(); begin self.Sort(SortGrapheNoeudsByIDStation); end; procedure TGrapheListeDesNoeuds.TrierParDistance(); begin ;; end; //****************************************************************************** { TGraphe } function TGraphe.Initialiser(const P: TProcAfficherMessage): boolean; begin result := false; SetLastError(ERR_GRAPHE_NO_ERROR, ''); FNombreNoeudsVisites := 0; FAfficherMessage := P; FListeDesNoeuds := TGrapheListeDesNoeuds.Create; FListeDesArcs := TGrapheListeDesArcs.Create; FLeCheminTrouve := TGrapheChemin.Create; //FMinStations := maxLongint; // initialisé à "plus l'infini" : on suppose qu'on ne prendra pas plus de 50 stations try FListeDesNoeuds.ClearListe(); FListeDesArcs.ClearListe(); FLeCheminTrouve.ClearListe(); Result := True; SetLastError(ERR_GRAPHE_NO_ERROR, ''); FAfficherMessage('Initialisation du graphe', True); except SetLastError(-1, 'Erreur d''initialisation'); end; end; procedure TGraphe.Finaliser(); begin try FListeDesNoeuds.ClearListe(); FListeDesArcs.ClearListe(); FLeCheminTrouve.ClearListe(); finally FreeAndNil(FListeDesArcs); FreeAndNil(FListeDesNoeuds); FreeAndNil(FLeCheminTrouve); end; end; // Les utilitaires function TGraphe.SetLastError(const QErrCode: integer; const QErrMsg: string): boolean; begin FLastError.ErrCode := QErrCode; FLastError.ErrMsg := QErrMsg; result := (FLastError.ErrCode <> ERR_GRAPHE_NO_ERROR); end; function TGraphe.GetLastError(): TGrapheLastError; begin result := FLastError; end; function TGraphe.FormatterTIDStation(const QId: TIDStation): string; var Qser, QSt: integer; begin ExtractSerStFromTIDStation(QId, Qser, QSt); result := format('%d.%d', [Qser, QSt]); end; procedure TGraphe.SetMinMax(); var Nb, i: Integer; MyNoeud: TGrapheNoeud; begin FXMini := Infinity; FYMini := Infinity; FXMaxi := -Infinity; FYMaxi := -Infinity; Nb := GetNbNoeuds(); for i := 0 to Nb -1 do begin MyNoeud := GetNoeud(i); FXMaxi := Max(FXMaxi, MyNoeud.X); FYMaxi := Max(FYMaxi, MyNoeud.Y); FXMini := Min(FXMini, MyNoeud.X); FYMini := Min(FYMini, MyNoeud.Y); end; end; procedure TGraphe.ListerLesNoeuds(const Caption: string; const DoDisplayDependances: boolean = false); var Nb, i, s, QSr, QSt: Integer; QNbArcsEntrants, QNbArcsSortants: integer; MyStation, ST1, ST2: TGrapheNoeud; QArc: TGrapheArc; begin Nb := self.GetNbNoeuds(); FAfficherMessage(''); FAfficherMessage(Caption); FAfficherMessage(Format('%d noeuds (%f, %f) -> %f, %f', [Nb, self.XMini, self.YMini, self.XMaxi, self.YMaxi])); for i := 0 to Nb - 1 do begin MyStation := self.GetNoeud(i); ExtractSerStFromTIDStation(MyStation.IDStation, QSr, QSt); QNbArcsEntrants := Length(MyStation.ListeArcsEntrants); QNbArcsSortants := Length(MyStation.ListeArcsEntrants); FAfficherMessage(Format(' %d: %d: %d.%d %f, %f: %d arcs entrants, %d arcs sortants, DistMini: %.2f, Visité: %s', [i, MyStation.IDStation, QSr, QSt, MyStation.X, MyStation.Y, QNbArcsEntrants, QNbArcsSortants, MyStation.DistanceMin, BoolToStr(MyStation.NoeudVisite, 'OUI', 'non')])); if (DoDisplayDependances) then begin FAfficherMessage('Arcs entrants:'); if (QNbArcsEntrants > 0) then begin for s := 0 to QNbArcsEntrants - 1 do begin QArc := self.GetArc(Abs(MyStation.ListeArcsEntrants[s])); ST1 := self.GetNoeud(QArc.IdxNoeudDepart); ST2 := self.GetNoeud(QArc.IdxNoeudArrivee); FAfficherMessage(Format('---- %06d: %06d [%s] -> %06d [%s]', [MyStation.ListeArcsEntrants[s], QArc.IdxNoeudDepart , self.FormatterTIDStation(ST1.IDStation), QArc.IdxNoeudArrivee, self.FormatterTIDStation(ST2.IDStation)])); end; end; FAfficherMessage('Arcs sortants:'); if (QNbArcsSortants > 0) then begin for s := 0 to QNbArcsSortants - 1 do begin QArc := self.GetArc(Abs(MyStation.ListeArcsSortants[s])); ST1 := self.GetNoeud(QArc.IdxNoeudDepart); ST2 := self.GetNoeud(QArc.IdxNoeudArrivee); FAfficherMessage(Format('---- %06d: %06d [%s] -> %06d [%s]', [MyStation.ListeArcsSortants[s], QArc.IdxNoeudDepart , self.FormatterTIDStation(ST1.IDStation), QArc.IdxNoeudArrivee, self.FormatterTIDStation(ST2.IDStation)])); end; end; end; end; FAfficherMessage(''); end; procedure TGraphe.ListerLesArcs(const Caption: string); var i, Nb: Integer; MyVisee: TGrapheArc; MyStationArr, MyStationDep: TGrapheNoeud; begin Nb := self.GetNbArcs(); FAfficherMessage(''); FAfficherMessage(Caption); FAfficherMessage(Format('%d arcs', [Nb])); for i := 0 to Nb - 1 do begin MyVisee := self.GetArc(i); MyStationDep := self.GetNoeud(MyVisee.IdxNoeudDepart); MyStationArr := self.GetNoeud(MyVisee.IdxNoeudArrivee); FAfficherMessage(Format(' %d: Nd%d [%s] -> Nd%d [%s]: L = %.3f m, Az: %.2f, P: %.2f', [i, MyVisee.IdxNoeudDepart , self.FormatterTIDStation(MyStationDep.IDStation), MyVisee.IdxNoeudArrivee, self.FormatterTIDStation(MyStationArr.IDStation), MyVisee.Longueur, MyVisee.Azimut, MyVisee.Pente])); end; FAfficherMessage(''); end; // Les noeuds procedure TGraphe.BeginNodeList(); begin FListeDesNoeuds.ClearListe(); end; procedure TGraphe.AddNoeud(const QNoeud: TGrapheNoeud); var EWE: TGrapheNoeud; begin EWE := QNoeud; EWE.DistanceMin := Infinity; EWE.NoeudVisite := false; Setlength(EWE.ListeArcsSortants, 0); Setlength(EWE.ListeArcsEntrants, 0); Setlength(EWE.ListeIdxPredecesseurs, 0); Setlength(EWE.ListeIdxSuccesseurs, 0); FListeDesNoeuds.AddElement(EWE); end; function TGraphe.GetNoeud(const Idx: integer): TGrapheNoeud; begin Result := FListeDesNoeuds.GetElement(Idx); end; procedure TGraphe.PutNoeud(const Idx: integer; const QNoeud: TGrapheNoeud); begin FListeDesNoeuds.PutElement(Idx, QNoeud); end; function TGraphe.GetNbNoeuds(): integer; begin Result := FListeDesNoeuds.GetNbElements(); end; procedure TGraphe.AddStation(const QSerie, QStation: integer; const QX, QY, QZ: double); var ST: TGrapheNoeud; begin ST.IDStation := MakeTIDStation(QSerie, QStation); ST.X := QX; ST.Y := QY; ST.Z := QZ; AddNoeud(ST); end; procedure TGraphe.EndNodesList(); var Nb: Integer; begin Nb := GetNbNoeuds(); if (Nb > 0) then begin FListeDesNoeuds.TrierParIDStations(); SetMinMax(); SetLastError(ERR_GRAPHE_NO_ERROR, ''); end else SetLastError(ERR_GRAPHE_EMPTY_LIST_NODES, 'La liste des noeuds est vide'); end; function TGraphe.FindNoeudByIDStation(const IDS: TIDStation; out ST: TGrapheNoeud; out IndexOf: TNumeroNoeud): boolean; function FindDepth(const I1, I2: TNumeroNoeud; const QIDX: TIDStation): TNumeroNoeud; var PVT: integer; C1 : TGrapheNoeud; begin Result := -1; // coupure en deux => calcul index médian PVT := (I2 + I1) div 2; // début > fin >> sortie directe avec erreur if (I1 > I2) then Exit(-1); C1 := GetNoeud(PVT); //GetBasePoint(PVT); // comparaison. Si vrai >> sortie avec numéro d'index if (C1.IDStation = QIDX) then Exit(PVT); // sinon, recherche en profondeur avec un niveau supplémentaire if (QIDX < C1.IDStation) then begin Result := FindDepth(I1, PVT-1, QIDX); Exit; end; Result := FindDepth(PVT+1, I2, QIDX); end; begin Result := false; IndexOf := FindDepth(0, GetNbNoeuds() - 1, IDS); if (IndexOf >= 0) then begin ST := GetNoeud(IndexOf); Exit(True); end; end; function TGraphe.GetNbNoeudsVisites(): integer; var i: Integer; Nd: TGrapheNoeud; begin Result := 0; for i := 0 to GetNbNoeuds() - 1 do begin Nd := GetNoeud(i); if (Nd.NoeudVisite) then Result += 1; end; end; procedure TGraphe.MarquerNoeudFromIdx(const Idx: TNumeroNoeud; const B: boolean); var EWE: TGrapheNoeud; begin EWE := GetNoeud(Idx); EWE.NoeudVisite := B; PutNoeud(Idx, EWE); end; function TGraphe.NoeudsTousVisites(): boolean; begin Result := (GetNbNoeuds() = FNombreNoeudsVisites); end; // Les arcs procedure TGraphe.BeginArcsList(); begin FListeDesArcs.ClearListe(); end; procedure TGraphe.AddArc(const QArc: TGrapheArc); begin FListeDesArcs.AddElement(QArc); end; function TGraphe.GetArc(const Idx: integer): TGrapheArc; begin Result := FListeDesArcs.GetElement(Idx); end; procedure TGraphe.PutArc(const Idx: integer; const QArc: TGrapheArc); begin FListeDesArcs.PutElement(Idx, QArc); end; function TGraphe.GetNbArcs(): integer; begin Result := FListeDesArcs.GetNbElements; end; procedure TGraphe.EndArcsList(); var Nb: Integer; begin Nb := GetNbArcs(); if (Nb > 0) then begin MakeArcsReciproques(); RecenserLesLiaisons(); SetLastError(ERR_GRAPHE_NO_ERROR, ''); end else SetLastError(ERR_GRAPHE_EMPTY_LIST_ARCS, 'La liste des arcs est vide'); end; procedure TGraphe.AddArcBetweenStations(const Ser1, St1, Ser2, St2: integer); var MyArc: TGrapheArc; BS1, BS2: TGrapheNoeud; begin FindNoeudByIDStation(MakeTIDStation(Ser1, St1), BS1, MyArc.IdxNoeudDepart); FindNoeudByIDStation(MakeTIDStation(Ser2, St2), BS2, MyArc.IdxNoeudArrivee); GetBearingInc(BS2.X - BS1.X, BS2.Y - BS1.Y, BS2.Z - BS1.Z, MyArc.Longueur, MyArc.Azimut, MyArc.Pente, 360.00, 360.00); self.AddArc(MyArc); end; // les galeries étant à double sens et le graphe orienté, construire les arcs opposés procedure TGraphe.MakeArcsReciproques(); var i, Nb: Integer; MyArcIn, MyArcOut: TGrapheArc; QSt1, QSt2: TGrapheNoeud; begin Nb := GetNbArcs(); for i := 0 to Nb - 1 do begin MyArcIn := getArc(i); MyArcOut.Parcouru := MyArcIn.Parcouru; MyArcOut.IdxNoeudDepart := MyArcIn.IdxNoeudArrivee; MyArcOut.IdxNoeudArrivee := MyArcIn.IdxNoeudDepart; QSt1 := GetNoeud(MyArcOut.IdxNoeudDepart); QSt2 := GetNoeud(MyArcOut.IdxNoeudArrivee); GetBearingInc(QSt2.X - QSt1.X, QSt2.Y - QSt1.Y, QSt2.Z - QSt1.Z, MyArcOut.Longueur, MyArcOut.Azimut, MyArcOut.Pente, 360.00, 360.00); AddArc(MyArcOut); end; end; //****************************************************************************** // Les fonctions de calcul procedure TGraphe.RecenserLesLiaisons(); var NbNoeuds, NbArcs, N, A, nv: integer; MyNoeud: TGrapheNoeud; MyArc: TGrapheArc; procedure QAddIdxNdSuccesseur(var MN: TGrapheNoeud; const Idx: TNumeroNoeud); var wu: Integer; begin wu := length(MN.ListeIdxSuccesseurs); SetLength(MN.ListeIdxSuccesseurs, wu + 1); MN.ListeIdxSuccesseurs[wu] := Idx; end; procedure QAddIdxNdPredecesseur(var MN: TGrapheNoeud; const Idx: TNumeroNoeud); var wu: Integer; begin wu := length(MN.ListeIdxPredecesseurs); SetLength(MN.ListeIdxPredecesseurs, wu + 1); MN.ListeIdxPredecesseurs[wu] := Idx; end; procedure QAddIdxArcSortant(var MN: TGrapheNoeud; const Idx: TNumeroArc); var wu: Integer; begin wu := length(MN.ListeArcsSortants); SetLength(MN.ListeArcsSortants, wu + 1); MN.ListeArcsSortants[wu] := Idx; end; procedure QAddIdxArcEntrant(var MN: TGrapheNoeud; const Idx: TNumeroArc); var wu: Integer; begin wu := length(MN.ListeArcsEntrants); SetLength(MN.ListeArcsEntrants, wu + 1); MN.ListeArcsEntrants[wu] := Idx; end; begin NbNoeuds := GetNbNoeuds(); NbArcs := GetNbArcs(); FAfficherMessage(Format('%s.RecenserLesLiaisons: %d noeuds, %d arcs', [classname, NbNoeuds, NbArcs])); if ((0 = NbNoeuds) or (0 = NbArcs)) then exit; for N := 0 to NbNoeuds - 1 do begin MyNoeud := GetNoeud(N); for A := 0 to NbArcs - 1 do begin MyArc := GetArc(A); if (N = MyArc.IdxNoeudDepart) then begin // ajout du successeur QAddIdxNdSuccesseur(MyNoeud, MyArc.IdxNoeudArrivee); QAddIdxArcSortant(MyNoeud, A); end; if (N = MyArc.IdxNoeudArrivee) then begin // ajout du successeur QAddIdxNdPredecesseur(MyNoeud, MyArc.IdxNoeudDepart); QAddIdxArcEntrant(MyNoeud, A); end; end; PutNoeud(N, MyNoeud); end; end; function TGraphe.GetNoeudCheminTrouve(const Idx: integer): TGrapheNoeud; begin Result := FLeCheminTrouve.GetElement(Idx); end; function TGraphe.GetNbNoeudsCheminTrouve(): integer; begin result := FLeCheminTrouve.GetNbElements(); end; // Noeud non visité le plus proche function TGraphe.RechercherIdxNearestNodeOf(const QCurrNoeud: TGrapheNoeud; out QDistance: double): TNumeroNoeud; var i : integer; NbArcsEntrants, NbArcsSortants: Integer; EWE: String; QNoeud: TGrapheNoeud; QArc: TGrapheArc; begin FAfficherMessage(Format('RechercherIdxNearestNodeOf( %d )', [QCurrNoeud.IDStation])); QDistance := Infinity; Result := -1; NbArcsEntrants := length(QCurrNoeud.ListeArcsEntrants); if (NbArcsEntrants > 0) then begin EWE := '-- Arcs entrants: '; for i := 0 to NbArcsEntrants-1 do EWE += format('%d, ', [QCurrNoeud.ListeArcsEntrants[i]]); FAfficherMessage(EWE); end; NbArcsSortants := length(QCurrNoeud.ListeArcsSortants); if (NbArcsSortants > 0) then begin EWE := '-- Arcs sortants: '; for i := 0 to NbArcsSortants-1 do EWE += format('%d, ', [QCurrNoeud.ListeArcsSortants[i]]); FAfficherMessage(EWE); end; if (0 = (NbArcsEntrants + NbArcsSortants)) then exit(-1); if (NbArcsEntrants > 0) then begin for i := 0 to NbArcsEntrants - 1 do begin QArc := GetArc(QCurrNoeud.ListeArcsEntrants[i]); FAfficherMessage(Format('-- Arc entrant: %d - L = %.2f, D = %.2f -> %d', [QCurrNoeud.ListeArcsEntrants[i], QArc.Longueur, QDistance, Result])); QNoeud := GetNoeud(QArc.IdxNoeudDepart); if (QNoeud.NoeudVisite) then Continue; if (QArc.Longueur < QDistance) then begin QDistance := QArc.Longueur; Result := QArc.IdxNoeudDepart; end; end; end; if (NbArcsSortants > 0) then begin for i := 0 to NbArcsSortants - 1 do begin QArc := GetArc(QCurrNoeud.ListeArcsSortants[i]); FAfficherMessage(Format('-- Arc sortant: %d - L = %.2f, D = %.2f -> %d', [QCurrNoeud.ListeArcsSortants[i], QArc.Longueur, QDistance, Result])); QNoeud := GetNoeud(QArc.IdxNoeudArrivee); if (QNoeud.NoeudVisite) then Continue; if (QArc.Longueur < QDistance) then begin QDistance := QArc.Longueur; Result := QArc.IdxNoeudArrivee; end; end; end; FAfficherMessage(Format('*** RechercherIdxNearestNodeOf: %d: %d - Dist = %.2f', [QCurrNoeud.IDStation, Result, QDistance])); end; function TGraphe.RechercherPlusCourtChemin(const Ser1, St1, Ser2, St2: integer): boolean; var QDistanceMin: Double; QNoeudDepart, QNoeudArrivee, QNoeudCourant, QST1, QNoeudSuccesseur: TGrapheNoeud; IdxNoeudDepart, IdxNoeudArrivee, QIdxPredecesseur: TNumeroNoeud; i, QIdxNearestNode, a, NbArcsSortants, NbPasses: integer; Q1, Q2: Boolean; QArc: TGrapheArc; qIdxArc: TNumeroArc; QDistance: double; begin Result := false; FNombreNoeudsVisites := 0; FLeCheminTrouve.ClearListe(); SetLastError(0, ''); Q1 := FindNoeudByIDStation(MakeTIDStation(Ser1, St1), QNoeudDepart, IdxNoeudDepart); if (Not Q1) then Exit(SetLastError(ERR_GRAPHE_NODE_NOT_FOUND, Format('Noeud "%d.%d" introuvable', [Ser1, St1]))); Q1 := FindNoeudByIDStation(MakeTIDStation(Ser2, St2), QNoeudArrivee, IdxNoeudArrivee); if (Not Q1) then Exit(SetLastError(ERR_GRAPHE_NODE_NOT_FOUND, Format('Noeud "%d.%d" introuvable', [Ser2, St2]))); FAfficherMessage(Format('%s.RechercherPlusCourtChemin(): %d: %d.%d -> %d: %d.%d', [ClassName, IdxNoeudDepart, Ser1, St1, IdxNoeudArrivee, Ser2, St2])); Q1 := (Ser1 = Ser2) and (St1 = St2); if (Q1) then Exit(SetLastError(ERR_GRAPHE_SAME_START_END, '-- Les stations de départ et arrivée sont identiques')); // acquittement de la recherche de noeuds QNoeudDepart := GetNoeud(IdxNoeudDepart); QNoeudArrivee := GetNoeud(IdxNoeudArrivee); QNoeudCourant := QNoeudDepart; FNombreNoeudsVisites += 1; // et incrémenter le compteur de noeuds visités // sécurité: On met les distances minimales à +Inf et le statut Visité à False for i := 0 to GetNbNoeuds() - 1 do begin QST1 := GetNoeud(i); QST1.NoeudVisite := false; //(i = IdxNoeudDepart); QST1.DistanceMin := ifthen(i = IdxNoeudDepart, 0.00, Infinity); PutNoeud(i, QST1); end; ListerLesNoeuds('Après initialisation de la première itération', false); NbPasses := 0; QDistanceMin := Infinity; QDistance := Infinity; while (Not NoeudsTousVisites()) do begin //if (NbPasses > 200) then break; // Noeud non visité de distance minimale QIdxNearestNode := RechercherIdxNearestNodeOf(QNoeudCourant, QDistanceMin); if (QIdxNearestNode = -1) then begin FAfficherMessage('Noeud non trouvé') ; exit; end; QNoeudCourant := GetNoeud(QIdxNearestNode); QNoeudCourant.NoeudVisite := True; QNoeudCourant.DistanceMin := QDistanceMin; PutNoeud(QIdxNearestNode, QNoeudCourant); if (QNoeudCourant.IDStation = QNoeudArrivee.IDStation) then begin FAfficherMessage(Format('%d == %d', [QNoeudCourant.IDStation , QNoeudArrivee.IDStation])); break; end; // Pour les arcs sortants NbArcsSortants := Length(QNoeudCourant.ListeArcsSortants); if (NbArcsSortants > 0) then begin FAfficherMessage(Format('%d: %d arcs sortants', [QNoeudCourant.IDStation, NbArcsSortants])); for a := 0 to NbArcsSortants - 1 do begin qIdxArc := QNoeudCourant.ListeArcsSortants[a]; QArc := GetArc(qIdxArc); QDistance := QNoeudCourant.DistanceMin + QArc.Longueur; // ???? Incompréhensible dans le corrigé // On attrappe le successeur QNoeudSuccesseur := GetNoeud(QArc.IdxNoeudArrivee); FAfficherMessage(Format('+++ Distance actuelle: %f, Distance du successeur: %d: %d: %f', [QDistance, a, QNoeudSuccesseur.IDStation, QNoeudSuccesseur.DistanceMin])); if (QDistance < QNoeudSuccesseur.DistanceMin) then begin QNoeudSuccesseur.DistanceMin := QDistance; QNoeudSuccesseur.NoeudVisite := True; PutNoeud(QArc.IdxNoeudArrivee, QNoeudSuccesseur); QArc.IdxNoeudDepart := QIdxNearestNode; PutArc(qIdxArc, QArc); FAfficherMessage(Format('Changement du noeud courant: %d devient %d', [QNoeudCourant.IDStation, QNoeudSuccesseur.IDStation])); QNoeudCourant := QNoeudSuccesseur; end; end; end; Inc(NbPasses); ListerLesNoeuds(Format('Passe %d', [NbPasses])); end; // while(Not NoeudsTousVisites()) do //exit; FAfficherMessage(Format('%d passes', [NbPasses])); // Le chemin //FLeCheminTrouve.ClearListe(); // si le noeud d'arrivée a un prédécesseur: FAfficherMessage('Backtracking'); if (Length(QNoeudArrivee.ListeArcsEntrants) > 0) then begin FAfficherMessage(Format('Le noeud %d a un prédécesseur', [QNoeudCourant.IDStation])); QNoeudCourant := QNoeudArrivee; FLeCheminTrouve.AddElement(QNoeudArrivee); NbPasses := 0; while (Length(QNoeudCourant.ListeArcsEntrants) > 0) do begin if (NbPasses > 200) then break; FAfficherMessage(Format('Noeud courant: %d', [QNoeudCourant.IDStation])); FLeCheminTrouve.InsertElement(0, QNoeudCourant); for i := 0 to Length(QNoeudCourant.ListeArcsEntrants) - 1 do begin QArc := GetArc(QNoeudCourant.ListeArcsEntrants[i]); QIdxPredecesseur := QArc.IdxNoeudDepart; QST1 := GetNoeud(QIdxPredecesseur); FAfficherMessage(Format('QNoeudCourant: %d: Arc entrant: %d provenant de %d [%d] - %s', [QNoeudCourant.IDStation, QNoeudCourant.ListeArcsEntrants[i], QIdxPredecesseur, QST1.IDStation, BooltoStr(QSt1.NoeudVisite, 'Visité', '--')])); if (QSt1.NoeudVisite) then break; end; if (QNoeudDepart.IDStation = QST1.IDStation) then Break; QNoeudCourant := QST1; Inc(NbPasses); end; end; FAfficherMessage('Parcours terminé'); //*) end; end.
Partager