-
Notifications
You must be signed in to change notification settings - Fork 296
/
Matching.mo
6829 lines (6582 loc) · 245 KB
/
Matching.mo
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package Matching
" file: Matching.mo
package: Matching
description: Matching contains functions for matching algorithms"
import BackendDAE;
import BackendDAEFunc;
import DAE;
protected
import Array;
import BackendDAEEXT;
import BackendDAEUtil;
import BackendDump;
import BackendEquation;
import BackendVariable;
import ClockIndexes;
import Config;
import Debug;
import Differentiate;
import DumpGraphML;
import ElementSource;
import Error;
import Expression;
import Flags;
import IndexReduction;
import Inline;
import List;
import MetaModelica.Dangerous;
import Util;
import Sorting;
import System;
// =============================================================================
// just a matching algorithm
// - PerfectMatching
// - RegularMatching
//
// =============================================================================
public function PerfectMatching "
This function fails if there is no perfect matching for the given system."
input BackendDAE.AdjacencyMatrix m;
output array<Integer> ass1 "eqn := ass1[var]";
output array<Integer> ass2 "var := ass2[eqn]";
protected
Integer N = arrayLength(m);
algorithm
ass1 := arrayCreate(N, -1);
ass2 := arrayCreate(N, -1);
(ass1, ass2, true) := ContinueMatching(m, N, N, ass1, ass2, true);
end PerfectMatching;
public function RegularMatching "
This function returns at least a partial matching for singular systems, starting from scratch.
Unmatched nodes are represented by -1."
input BackendDAE.AdjacencyMatrix m;
input Integer nVars;
input Integer nEqns;
output array<Integer> ass1 "eqn := ass1[var]";
output array<Integer> ass2 "var := ass2[eqn]";
output Boolean perfectMatching;
algorithm
ass1 := arrayCreate(nVars, -1);
ass2 := arrayCreate(nEqns, -1);
(ass1, ass2, perfectMatching) := ContinueMatching(m, nVars, nEqns, ass1, ass2, false);
end RegularMatching;
public function ContinueMatching "
This function returns at least a partial matching for singular systems.
Unmatched nodes are represented by -1."
input BackendDAE.AdjacencyMatrix m;
input Integer nVars;
input Integer nEqns;
input output array<Integer> ass1 "eqn := ass1[var]";
input output array<Integer> ass2 "var := ass2[eqn]";
input Boolean stopAtSingularity = false;
output Boolean perfectMatching = true;
protected
Integer i, j;
array<Boolean> eMark, vMark;
array<Integer> eMarkIx, vMarkIx;
Integer eMarkN=0, vMarkN=0;
Boolean success;
algorithm
vMark := arrayCreate(nVars, false);
eMark := arrayCreate(nEqns, false);
vMarkIx := arrayCreate(nVars, 0);
eMarkIx := arrayCreate(nEqns, 0);
i := 1;
while i<=nEqns loop
j := ass2[i];
if not (j>0 and ass1[j] == i) then
clearArrayWithKnownSetIndexes(eMark, eMarkIx, eMarkN);
clearArrayWithKnownSetIndexes(vMark, vMarkIx, vMarkN);
(success, eMarkN, vMarkN) := BBPathFound(i, m, eMark, vMark, ass1, ass2, eMarkIx, vMarkIx, 0, 0);
if not success then
perfectMatching := false;
if stopAtSingularity then
return;
end if;
end if;
end if;
i := i+1;
end while;
end ContinueMatching;
public function BBMatching
input BackendDAE.EqSystem inSys;
input BackendDAE.Shared inShared;
input Boolean clearMatching;
input BackendDAE.MatchingOptions inMatchingOptions;
input BackendDAEFunc.StructurallySingularSystemHandlerFunc sssHandler;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
output BackendDAE.EqSystem outSys = inSys;
output BackendDAE.Shared outShared = inShared;
output BackendDAE.StructurallySingularSystemHandlerArg outArg = inArg;
protected
Integer i;
Boolean success = true;
BackendDAE.AdjacencyMatrix m;
Integer nVars, nEqns, j;
array<Integer> ass1, ass2;
array<Boolean> eMark, vMark;
array<Integer> eMarkIx, vMarkIx;
Integer eMarkN=0, vMarkN=0;
BackendDAE.EquationArray eqns;
BackendDAE.Variables vars;
list<Integer> mEqns;
algorithm
//BackendDAE.EQSYSTEM(m=SOME(m), matching=BackendDAE.MATCHING(ass1=ass1, ass2=ass2)) := outSys;
SOME(m) := outSys.m;
nEqns := BackendDAEUtil.systemSize(outSys);
nVars := BackendVariable.daenumVariables(outSys);
// Be carefull, since matching may have been generated with not distinguishing between
// state and their derivative, which leads to wrong traversing of bibartite graph!!!!
//(ass1, ass2) := getAssignment(clearMatching, nVars, nEqns, inSys);
//if clearMatching then
ass2 := arrayCreate(nEqns, -1);
ass1 := arrayCreate(nVars, -1);
//BBCheapMatching(nEqns, m, ass1, ass2);
//end if;
vMark := arrayCreate(nVars, false);
eMark := arrayCreate(nEqns, false);
vMarkIx := arrayCreate(nVars, 0);
eMarkIx := arrayCreate(nEqns, 0);
i := 1;
while i<=nEqns and success loop
j := ass2[i];
if ((j>0) and ass1[j] == i) then
success :=true;
else
clearArrayWithKnownSetIndexes(eMark, eMarkIx, eMarkN);
clearArrayWithKnownSetIndexes(vMark, vMarkIx, vMarkN);
(success,eMarkN,vMarkN) := BBPathFound(i, m, eMark, vMark, ass1, ass2, eMarkIx, vMarkIx, 0, 0);
if not success then
mEqns := {};
for j in 1:nEqns loop
if eMark[j] then
mEqns:=j::mEqns;
end if;
end for;
(_, i, outSys, outShared, ass1, ass2, outArg) := sssHandler({mEqns}, i, outSys, outShared, ass1, ass2, outArg);
SOME(m) := outSys.m;
//nEqns := BackendDAEUtil.systemSize(outSys);
//nVars := BackendVariable.daenumVariables(outSys);
//ass1 := assignmentsArrayExpand(ass1, nVars, arrayLength(ass1), -1);
//ass2 := assignmentsArrayExpand(ass2, nEqns, arrayLength(ass2), -1);
//vMark := assignmentsArrayBooleanExpand(vMark, nVars, arrayLength(vMark), false);
//eMark := assignmentsArrayBooleanExpand(eMark, nEqns, arrayLength(eMark), false);
success := true;
i := i-1;
end if;
end if;
i := i+1;
end while;
if success then
outSys := BackendDAEUtil.setEqSystMatching(outSys, BackendDAE.MATCHING(ass1, ass2, {}));
else
print("\nSingular System!!!\n");
end if;
end BBMatching;
protected function BBPathFound
input Integer i;
input BackendDAE.AdjacencyMatrix m;
input array<Boolean> eMark;
input array<Boolean> vMark;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
input array<Integer> eMarkIx;
input array<Integer> vMarkIx;
output Boolean success=false;
input output Integer eMarkN, vMarkN;
algorithm
if arrayGet(eMark, i) then
return;
end if;
arrayUpdate(eMark, i, true);
eMarkN := eMarkN+1;
arrayUpdate(eMarkIx, eMarkN, i);
for j in m[i] loop
// negative entries in adjacence matrix belong to states!!!
if (j>0 and ass1[j] <= 0) then
success := true;
arrayUpdate(ass1, j, i);
arrayUpdate(ass2, i, j);
return;
end if;
end for;
for j in m[i] loop
// negative entries in adjacence matrix belong to states!!!
if (j>0 and not vMark[j]) then
arrayUpdate(vMark, j, true);
vMarkN := vMarkN+1;
arrayUpdate(vMarkIx, vMarkN, j);
(success, eMarkN, vMarkN) := BBPathFound(ass1[j], m, eMark, vMark, ass1, ass2, eMarkIx, vMarkIx, eMarkN, vMarkN);
if success then
arrayUpdate(ass1, j, i);
arrayUpdate(ass2, i, j);
return;
end if;
end if;
end for;
end BBPathFound;
protected function BBCheapMatching
input Integer nEqns;
input BackendDAE.AdjacencyMatrix m;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
protected
Integer i, j;
Boolean success=false;
list<Integer> vars;
algorithm
for i in 1:nEqns loop
vars := m[i];
while not success and not listEmpty(vars) loop
j::vars := vars;
// negative entries in adjacence matrix belong to states!!!
if (j>0 and ass1[j] <= 0) then
success := true;
arrayUpdate(ass1, j, i);
arrayUpdate(ass2, i, j);
end if;
end while;
end for;
end BBCheapMatching;
public function invertMatching "author: lochel
ass1 <-> ass2"
input array<Integer> inAss;
output array<Integer> outAss;
protected
Integer N = arrayLength(inAss);
Integer j;
algorithm
outAss := arrayCreate(N, -1);
for i in 1:N loop
j := inAss[i];
if j > 0 then
outAss[inAss[i]] := i;
end if;
end for;
end invertMatching;
// =============================================================================
// Matching Algorithms
//
// =============================================================================
public function DFSLH
"depth first search with look ahead feature. basically the same like MC21A."
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input Boolean clearMatching;
input BackendDAE.MatchingOptions inMatchingOptions;
input BackendDAEFunc.StructurallySingularSystemHandlerFunc sssHandler;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output BackendDAE.StructurallySingularSystemHandlerArg outArg;
algorithm
(osyst,oshared,outArg) :=
matchcontinue (isyst,ishared,clearMatching,inMatchingOptions,sssHandler,inArg)
local
Integer nvars,neqns;
array<Integer> vec1,vec2,emark,vmark;
BackendDAE.StructurallySingularSystemHandlerArg arg;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
BackendDAE.AdjacencyMatrix m;
BackendDAE.AdjacencyMatrixT mt;
case (BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mt)),_,_,_,_,_)
equation
neqns = BackendDAEUtil.systemSize(isyst);
nvars = BackendVariable.daenumVariables(isyst);
true = intGt(nvars,0);
true = intGt(neqns,0);
vmark = arrayCreate(nvars,-1);
emark = arrayCreate(neqns,-1);
(vec1,vec2) = getAssignment(clearMatching,nvars,neqns,isyst);
_ = cheapmatchingalgorithm(nvars,neqns,m,mt,vec1,vec2,false);
(vec1,vec2,syst,shared,arg) = DFSLH2(isyst,ishared,nvars,neqns,1,emark,vmark,vec1,vec2,inMatchingOptions,sssHandler,inArg);
syst = BackendDAEUtil.setEqSystMatching(syst,BackendDAE.MATCHING(vec1,vec2,{}));
then
(syst,shared,arg);
// fail case if system is empty
case (_,_,_,_,_,_)
equation
neqns = BackendDAEUtil.systemSize(isyst);
nvars = BackendVariable.daenumVariables(isyst);
false = intGt(nvars,0);
false = intGt(neqns,0);
vec1 = listArray({});
vec2 = listArray({});
syst = BackendDAEUtil.setEqSystMatching(isyst,BackendDAE.MATCHING(vec2,vec1,{}));
then
(syst,ishared,inArg);
else
equation
if Flags.isSet(Flags.FAILTRACE) then
Debug.trace("- Matching.DFSLH failed\n");
end if;
then
fail();
end matchcontinue;
end DFSLH;
protected function DFSLH2
"author: PA
This is the outer loop of the matching algorithm
The find_path algorithm is called for each equation/variable.
inputs: (BackendDAE,AdjacencyMatrix, AdjacencyMatrixT
,int /* number of vars */
,int /* number of eqns */
,int /* current var */
,Assignments /* assignments, array of eqn indices */
,Assignments /* assignments, array of var indices */
,MatchingOptions) /* options for matching alg. */
outputs: (Assignments, /* assignments, array of equation indices */
Assignments, /* assignments, list of variable indices */
BackendDAE, BackendDAE.AdjacencyMatrix, AdjacencyMatrixT)"
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input Integer nv;
input Integer nf;
input Integer i;
input array<Integer> emark;
input array<Integer> vmark;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
input BackendDAE.MatchingOptions match_opts;
input BackendDAEFunc.StructurallySingularSystemHandlerFunc sssHandler;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
output array<Integer> outAssignments1;
output array<Integer> outAssignments2;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output BackendDAE.StructurallySingularSystemHandlerArg outArg;
algorithm
(outAssignments1,outAssignments2,osyst,oshared,outArg):=
matchcontinue (isyst,ishared,nv,nf,i,emark,vmark,ass1,ass2,match_opts,sssHandler,inArg)
local
array<Integer> ass1_1,ass2_1,ass1_2,ass2_2,ass1_3,ass2_3,emark1,vmark1;
BackendDAE.AdjacencyMatrix m;
BackendDAE.AdjacencyMatrixT mt;
Integer i_1,nv_1,nf_1;
BackendDAE.EquationArray eqns;
list<Integer> eqn_lst,var_lst,meqns;
BackendDAE.StructurallySingularSystemHandlerArg arg,arg1;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
case (syst as BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mt)),_,_,_,_,_,_,_,_,_,_,_)
equation
true = intGe(i,nv);
(ass1_1,ass2_1) = pathFound(m, mt, i, i,emark, vmark, ass1, ass2) "eMark(i)=vMark(i)=false; eMark(i)=vMark(i)=false exit loop";
then
(ass1_1,ass2_1,syst,ishared,inArg);
case (syst as BackendDAE.EQSYSTEM(m=SOME(_),mT=SOME(_)),_,_,_,_,_,_,_,_,_,_,_)
equation
i_1 = i + 1;
true = intGt(ass2[i],0);
(ass1_2,ass2_2,syst,shared,arg) = DFSLH2(syst, ishared, nv, nf, i_1, emark, vmark, ass1, ass2, match_opts, sssHandler, inArg);
then
(ass1_2,ass2_2,syst,shared,arg);
case (syst as BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mt)),_,_,_,_,_,_,_,_,_,_,_)
equation
i_1 = i + 1;
(ass1_1,ass2_1) = pathFound(m, mt, i, i,emark, vmark, ass1, ass2) "eMark(i)=vMark(i)=false";
(ass1_2,ass2_2,syst,shared,arg) = DFSLH2(syst, ishared, nv, nf, i_1, emark, vmark, ass1_1, ass2_1, match_opts, sssHandler, inArg);
then
(ass1_2,ass2_2,syst,shared,arg);
case (_,_,_,_,_,_,_,_,_,(BackendDAE.INDEX_REDUCTION(),_),_,_)
equation
meqns = getMarked(nf,i,emark,{});
(_,i_1,syst,shared,ass1_1,ass2_1,arg) = sssHandler({meqns},i,isyst,ishared,ass1,ass2,inArg)
"path_found failed, Try index reduction using dummy derivatives.
When a constraint exist between states and index reduction is needed
the dummy derivative will select one of the states as a dummy state
(and the derivative of that state as a dummy derivative).
For instance, u1=u2 is a constraint between states. Choose u1 as dummy state
and der(u1) as dummy derivative, named der_u1. The differentiated function
then becomes: der_u1 = der(u2).
In the dummy derivative method this equation is added and the original equation
u1=u2 is kept. This is not the case for the original pantilides algorithm, where
the original equation is removed from the system.";
eqns = BackendEquation.getEqnsFromEqSystem(syst);
nf_1 = BackendEquation.equationArraySize(eqns) "and try again, restarting. This could be optimized later. It should not
be necessary to restart the matching, according to Bernard Bachmann. Instead one
could continue the matching as usual. This was tested (2004-11-22) and it does not
work to continue without restarting.
For instance the Influenca model \"../testsuite/mofiles/Influenca.mo\" does not work if
not restarting.
2004-12-29 PA. This was a bug, assignment lists needed to be expanded with the size
of the system in order to work. SO: Matching is not needed to be restarted from
scratch.";
nv_1 = BackendVariable.varsSize(BackendVariable.daeVars(syst));
ass1_2 = assignmentsArrayExpand(ass1_1, nv_1,arrayLength(ass1_1),-1);
ass2_2 = assignmentsArrayExpand(ass2_1, nf_1,arrayLength(ass2_1),-1);
vmark1 = assignmentsArrayExpand(vmark, nv_1,arrayLength(vmark),-1);
emark1 = assignmentsArrayExpand(emark, nf_1,arrayLength(emark),-1);
(ass1_3,ass2_3,syst,shared,arg1) = DFSLH2(syst,shared,nv_1,nf_1,i_1,emark1, vmark1,ass1_2,ass2_2,match_opts,sssHandler,arg);
then
(ass1_3,ass2_3,syst,shared,arg1);
case (_,_,_,_,_,_,_,_,_,_,_,_)
equation
eqn_lst = getMarked(nf,i,emark,{});
singularSystemError({eqn_lst},i,isyst,ishared,ass1,ass2,inArg);
then
fail();
end matchcontinue;
end DFSLH2;
protected function pathFound "author: PA
This function is part of the matching algorithm.
It tries to find a matching for the equation index given as
third argument, i.
inputs: (AdjacencyMatrix, BackendDAE.AdjacencyMatrixT, int /* equation */,
Assignments, Assignments)
outputs: (Assignments, Assignments)"
input BackendDAE.AdjacencyMatrix m;
input BackendDAE.AdjacencyMatrixT mt;
input Integer i;
input Integer imark;
input array<Integer> emark;
input array<Integer> vmark;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
output array<Integer> outAssignments1;
output array<Integer> outAssignments2;
algorithm
(outAssignments1,outAssignments2):=
matchcontinue (m,mt,i,imark,emark,vmark,ass1,ass2)
local
array<Integer> ass1_1,ass2_1;
case (_,_,_,_,_,_,_,_)
equation
arrayUpdate(emark,i,imark) "Side effect";
(ass1_1,ass2_1) = assignOneInEqn(m, mt, i, ass1, ass2);
then
(ass1_1,ass2_1);
case (_,_,_,_,_,_,_,_)
equation
(ass1_1,ass2_1) = forallUnmarkedVarsInEqn(m, mt, i, imark, emark, vmark, ass1, ass2);
then
(ass1_1,ass2_1);
end matchcontinue;
end pathFound;
protected function assignOneInEqn "author: PA
Helper function to pathFound."
input BackendDAE.AdjacencyMatrix m;
input BackendDAE.AdjacencyMatrixT mt;
input Integer i;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
output array<Integer> outAssignments1;
output array<Integer> outAssignments2;
protected
list<Integer> vars;
algorithm
vars := BackendDAEUtil.varsInEqn(m, i);
(outAssignments1,outAssignments2):= assignFirstUnassigned(i, vars, ass1, ass2);
end assignOneInEqn;
protected function assignFirstUnassigned
"author: PA
This function assigns the first unassign variable to the equation
given as first argument. It is part of the matching algorithm.
inputs: (int /* equation */,
int list /* variables */,
BackendDAE.Assignments /* ass1 */,
BackendDAE.Assignments /* ass2 */)
outputs: (Assignments, /* ass1 */
Assignments) /* ass2 */"
input Integer i;
input list<Integer> inIntegerLst2;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
output array<Integer> outAssignments1;
output array<Integer> outAssignments2;
algorithm
(outAssignments1,outAssignments2):=
matchcontinue (i,inIntegerLst2,ass1,ass2)
local
array<Integer> ass1_1,ass2_1;
Integer v;
list<Integer> vs;
case (_,(v :: _),_,_)
equation
false = intGt(ass1[v],0);
ass1_1 = arrayUpdate(ass1,v,i);
ass2_1 = arrayUpdate(ass2,i,v);
then
(ass1_1,ass2_1);
case (_,(_ :: vs),_,_)
equation
(ass1_1,ass2_1) = assignFirstUnassigned(i, vs, ass1, ass2);
then
(ass1_1,ass2_1);
end matchcontinue;
end assignFirstUnassigned;
protected function forallUnmarkedVarsInEqn
"author: PA
This function is part of the matching algorithm.
It loops over all umarked variables in an equation.
inputs: (AdjacencyMatrix,
AdjacencyMatrixT,
int,
BackendDAE.Assignments /* ass1 */,
BackendDAE.Assignments /* ass2 */)
outputs: (Assignments, Assignments)"
input BackendDAE.AdjacencyMatrix m;
input BackendDAE.AdjacencyMatrixT mt;
input Integer i;
input Integer imark;
input array<Integer> emark;
input array<Integer> vmark;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
output array<Integer> outAssignments1;
output array<Integer> outAssignments2;
protected
list<Integer> vars,vars_1;
algorithm
vars := BackendDAEUtil.varsInEqn(m, i);
vars_1 := List.filter1OnTrue(vars, isNotVMarked, (imark, vmark));
(outAssignments1,outAssignments2) := forallUnmarkedVarsInEqnBody(m, mt, i, imark, emark, vmark, vars_1, ass1, ass2);
end forallUnmarkedVarsInEqn;
protected function isNotVMarked
"author: PA
This function succeds for variables that are not marked."
input Integer i;
input tuple<Integer,array<Integer>> inTpl;
output Boolean outB;
protected
Integer imark;
array<Integer> vmark;
algorithm
(imark,vmark) := inTpl;
outB := not intEq(imark,vmark[i]);
end isNotVMarked;
protected function forallUnmarkedVarsInEqnBody
"author: PA
This function is part of the matching algorithm.
It is the body of the loop over all unmarked variables.
inputs: (AdjacencyMatrix, BackendDAE.AdjacencyMatrixT,
int,
int list /* var list */
Assignments
Assignments)
outputs: (Assignments, Assignments)"
input BackendDAE.AdjacencyMatrix m;
input BackendDAE.AdjacencyMatrixT mt;
input Integer i;
input Integer imark;
input array<Integer> emark;
input array<Integer> vmark;
input list<Integer> inIntegerLst4;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
output array<Integer> outAssignments1;
output array<Integer> outAssignments2;
algorithm
(outAssignments1,outAssignments2):=
matchcontinue (m,mt,i,imark,emark,vmark,inIntegerLst4,ass1,ass2)
local
Integer assarg,v;
array<Integer> ass1_1,ass2_1,ass1_2,ass2_2;
list<Integer> vars,vs;
case (_,_,_,_,_,_,((v :: _)),_,_)
equation
arrayUpdate(vmark,v,imark);
assarg = ass1[v];
(ass1_1,ass2_1) = pathFound(m, mt, assarg, imark, emark, vmark, ass1, ass2);
ass1_2 = arrayUpdate(ass1_1,v,i);
ass2_2 = arrayUpdate(ass2_1,i,v);
then
(ass1_2,ass2_2);
case (_,_,_,_,_,_,((_ :: vs)),_,_)
equation
(ass1_1,ass2_1) = forallUnmarkedVarsInEqnBody(m, mt, i, imark, emark, vmark, vs, ass1, ass2);
then
(ass1_1,ass2_1);
end matchcontinue;
end forallUnmarkedVarsInEqnBody;
public function BFSB
" complexity O(n*tau)
author: Frenkel TUD 2012-03"
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input Boolean clearMatching;
input BackendDAE.MatchingOptions inMatchingOptions;
input BackendDAEFunc.StructurallySingularSystemHandlerFunc sssHandler;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output BackendDAE.StructurallySingularSystemHandlerArg outArg;
algorithm
(osyst,oshared,outArg) :=
matchcontinue (isyst,ishared,clearMatching,inMatchingOptions,sssHandler,inArg)
local
Integer nvars,neqns;
array<Integer> vec1,vec2;
BackendDAE.StructurallySingularSystemHandlerArg arg;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
array<Integer> rowmarks,parentcolum;
BackendDAE.AdjacencyMatrix m;
BackendDAE.AdjacencyMatrixT mt;
case (BackendDAE.EQSYSTEM(m=SOME(m),mT=SOME(mt)),_,_,_,_,_)
equation
neqns = BackendDAEUtil.systemSize(isyst);
nvars = BackendVariable.daenumVariables(isyst);
true = intGt(nvars,0);
true = intGt(neqns,0);
rowmarks = arrayCreate(nvars,-1);
parentcolum = arrayCreate(nvars,-1);
(vec1,vec2) = getAssignment(clearMatching,nvars,neqns,isyst);
_ = cheapmatchingalgorithm(nvars,neqns,m,mt,vec1,vec2,false);
(vec1,vec2,syst,shared,arg) = BFSB1(1,1,nvars,neqns,m,mt,rowmarks,parentcolum,vec1,vec2,isyst,ishared,inMatchingOptions, sssHandler, inArg);
syst = BackendDAEUtil.setEqSystMatching(syst,BackendDAE.MATCHING(vec2,vec1,{}));
then
(syst,shared,arg);
// fail case if system is empty
case (_,_,_,_,_,_)
equation
neqns = BackendDAEUtil.systemSize(isyst);
nvars = BackendVariable.daenumVariables(isyst);
false = intGt(nvars,0);
false = intGt(neqns,0);
vec1 = listArray({});
vec2 = listArray({});
syst = BackendDAEUtil.setEqSystMatching(isyst,BackendDAE.MATCHING(vec2,vec1,{}));
then
(syst,ishared,inArg);
else
equation
if Flags.isSet(Flags.FAILTRACE) then
Debug.trace("- Matching.BFSB failed\n");
end if;
then
fail();
end matchcontinue;
end BFSB;
protected function BFSB1
"function helper for BFSB, traverses all colums and perform a BFSB phase on each
author: Frenkel TUD 2012-03"
input Integer i;
input Integer rowmark;
input Integer nv;
input Integer ne;
input BackendDAE.AdjacencyMatrix m "m[eqnindx] = list(varindx)";
input BackendDAE.AdjacencyMatrixT mT "mT[varindx] = list(eqnindx)";
input array<Integer> rowmarks;
input array<Integer> parentcolum;
input array<Integer> ass1 "ass[eqnindx]=varindx";
input array<Integer> ass2 "ass[varindx]=eqnindx";
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input BackendDAE.MatchingOptions inMatchingOptions;
input BackendDAEFunc.StructurallySingularSystemHandlerFunc sssHandler;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
output array<Integer> outAss1;
output array<Integer> outAss2;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output BackendDAE.StructurallySingularSystemHandlerArg outArg;
algorithm
(outAss1,outAss2,osyst,oshared,outArg):=
matchcontinue (i,rowmark,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2,isyst,ishared,inMatchingOptions,sssHandler,inArg)
local
list<Integer> visitedcolums;
BackendDAE.AdjacencyMatrix m1,mt1;
Integer nv_1,ne_1,i_1;
BackendDAE.StructurallySingularSystemHandlerArg arg;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
array<Integer> ass1_1,ass1_2,ass2_1,ass2_2,rowmarks1,parentcolum1;
case (_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)
equation
true=intGt(i,ne);
then
(ass1,ass2,isyst,ishared,inArg);
case (_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)
equation
// not assigned
false = intGt(ass1[i],0);
// search augmenting paths
visitedcolums = BFSBphase({i},rowmark,i,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2,{},{});
// if visitedcolums is not zero matching fails -> try index reduction and matching aggain
(_,i_1,syst as BackendDAE.EQSYSTEM(m=SOME(m1),mT=SOME(mt1)),shared,nv_1,ne_1,ass1_1,ass2_1,arg) = reduceIndexifNecessary(visitedcolums,i,isyst,ishared,nv,ne,ass1,ass2,inMatchingOptions,sssHandler,inArg);
rowmarks1 = assignmentsArrayExpand(rowmarks,nv_1,arrayLength(rowmarks),-1);
parentcolum1 = assignmentsArrayExpand(parentcolum,nv_1,arrayLength(parentcolum),-1);
(ass1_2,ass2_2,syst,shared,arg) = BFSB1(i_1,rowmark+1,nv_1,ne_1,m1,mt1,rowmarks1,parentcolum1,ass1_1,ass2_1,syst,shared,inMatchingOptions,sssHandler,arg);
then
(ass1_2,ass2_2,syst,shared,arg);
case (_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)
equation
true = intGt(ass1[i],0);
(ass1_1,ass2_1,syst,shared,arg) = BFSB1(i+1,rowmark,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2,isyst,ishared,inMatchingOptions,sssHandler,inArg);
then
(ass1_1,ass2_1,syst,shared,arg);
else
equation
Error.addInternalError("function BFSB1 failed in equation " + intString(i), sourceInfo());
then
fail();
end matchcontinue;
end BFSB1;
protected function BFSBphase
"function helper for BFSB, traverses all colums and perform a BFSB phase on each
author: Frenkel TUD 2012-03"
input list<Integer> queue;
input Integer rowmark;
input Integer i;
input Integer nv;
input Integer ne;
input BackendDAE.AdjacencyMatrix m;
input BackendDAE.AdjacencyMatrixT mT;
input array<Integer> rowmarks;
input array<Integer> parentcolum;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
input list<Integer> nextQueue;
input list<Integer> inVisitedColums;
output list<Integer> outVisitedColums "This list stores all visited collums, if no augmenting path is found
it could be used to prune the nodes, if a path is found the list is empty";
algorithm
outVisitedColums :=
match (queue,rowmark,i,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2,nextQueue,inVisitedColums)
local
list<Integer> rest,queue1,rows;
Integer c;
Boolean b;
case ({},_,_,_,_,_,_,_,_,_,_,{},_) then inVisitedColums;
case ({},_,_,_,_,_,_,_,_,_,_,_,_)
then
BFSBphase(nextQueue,rowmark,i,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2,{},inVisitedColums);
case (c::rest,_,_,_,_,_,_,_,_,_,_,_,_)
equation
// traverse all adiacent rows
rows = List.select(m[c], Util.intPositive);
(queue1,b) = BFSBtraverseRows(rows,nextQueue,rowmark,i,c,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2);
then
BFSBphase1(b,rest,rowmark,i,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2,queue1,c::inVisitedColums);
else
equation
Error.addInternalError("function BFSBphase failed in equation " + intString(i), sourceInfo());
then
fail();
end match;
end BFSBphase;
protected function BFSBphase1
"function helper for BFSB, traverses all colums and perform a BFSB phase on each
author: Frenkel TUD 2012-03"
input Boolean inPathFound;
input list<Integer> queue;
input Integer rowmark;
input Integer i;
input Integer nv;
input Integer ne;
input BackendDAE.AdjacencyMatrix m;
input BackendDAE.AdjacencyMatrixT mT;
input array<Integer> rowmarks;
input array<Integer> parentcolum;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
input list<Integer> nextQueue;
input list<Integer> inVisitedColums;
output list<Integer> outVisitedColums;
algorithm
outVisitedColums :=
match (inPathFound,queue,rowmark,i,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2,nextQueue,inVisitedColums)
case (true,_,_,_,_,_,_,_,_,_,_,_,_,_) then {};
case (false,_,_,_,_,_,_,_,_,_,_,_,_,_)
then
BFSBphase(queue,rowmark,i,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2,nextQueue,inVisitedColums);
else
equation
Error.addInternalError("function BFSBphase1 failed", sourceInfo());
then
fail();
end match;
end BFSBphase1;
protected function BFSBtraverseRows
"function helper for BFSB, traverses all vars of a equations and search a augmenting path
author: Frenkel TUD 2012-03"
input list<Integer> rows;
input list<Integer> queue;
input Integer rowmark;
input Integer i;
input Integer c;
input Integer nv;
input Integer ne;
input BackendDAE.AdjacencyMatrix m;
input BackendDAE.AdjacencyMatrixT mT;
input array<Integer> rowmarks;
input array<Integer> parentcolum;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
output list<Integer> outEqnqueue;
output Boolean pathFound;
algorithm
(outEqnqueue,pathFound):=
matchcontinue (rows,queue,rowmark,i,c,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2)
local
list<Integer> rest,queue1,queue2;
Integer rc,r;
Boolean b;
case ({},_,_,_,_,_,_,_,_,_,_,_,_) then (listReverse(queue),false);
case (r::_,_,_,_,_,_,_,_,_,_,_,_,_)
equation
// row is unmatched -> augmenting path found
true = intLt(ass2[r],0);
BFSBreasign(i,c,parentcolum,r,ass1,ass2);
then
({},true);
case (r::rest,_,_,_,_,_,_,_,_,_,_,_,_)
equation
// row is matched
rc = ass2[r];
false = intLt(rc,0);
queue1 = BFSBenque(queue,rowmark,c,rc,r,intLt(rowmarks[r],rowmark),rowmarks,parentcolum);
(queue2,b) = BFSBtraverseRows(rest,queue1,rowmark,i,c,nv,ne,m,mT,rowmarks,parentcolum,ass1,ass2);
then
(queue2,b);
else
equation
Error.addInternalError("function BFSBtraverseRows failed in equation " + intString(i), sourceInfo());
then
fail();
end matchcontinue;
end BFSBtraverseRows;
protected function BFSBreasign
"function helper for BFSB, reasignment(rematching) allong the augmenting path
remove all edges from the assignments that are in the path
add all other edges to the assignment
author: Frenkel TUD 2012-03"
input Integer i;
input Integer c;
input array<Integer> parentcolum;
input Integer l;
input array<Integer> ass1 "eqn := ass1[var]";
input array<Integer> ass2 "var := ass2[eqn]";
algorithm
_ := matchcontinue (i,c,parentcolum,l,ass1,ass2)
local
Integer r;
case (_,_,_,_,_,_)
equation
true = intEq(i,c);
arrayUpdate(ass1,c,l);
arrayUpdate(ass2,l,c);
then ();
case (_,_,_,_,_,_)
equation
r = ass1[c];
arrayUpdate(ass1,c,l);
arrayUpdate(ass2,l,c);
BFSBreasign(i,parentcolum[r],parentcolum,r,ass1,ass2);
then
();
else
equation
Error.addInternalError("function BFSBreasign failed", sourceInfo());
then
fail();
end matchcontinue;
end BFSBreasign;
protected function BFSBenque
"function helper for BFSB, enque a collum if the row is not visited
author: Frenkel TUD 2012-03"
input list<Integer> queue;
input Integer rowmark;
input Integer c;
input Integer rc;
input Integer r;
input Boolean visited;
input array<Integer> rowmarks;
input array<Integer> parentcolum;
output list<Integer> outEqnqueue;
algorithm
outEqnqueue:=
match (queue,rowmark,c,rc,r,visited,rowmarks,parentcolum)
case (_,_,_,_,_,false,_,_) then queue;
case (_,_,_,_,_,true,_,_)
equation
// mark row
arrayUpdate(rowmarks,r,rowmark);
// store parent colum
arrayUpdate(parentcolum,r,c);
then
(rc::queue);
else
equation
Error.addInternalError("function BFSBenque failed", sourceInfo());
then
fail();
end match;
end BFSBenque;
public function DFSB
" complexity O(n*tau)
author: Frenkel TUD 2012-03"
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input Boolean clearMatching;
input BackendDAE.MatchingOptions inMatchingOptions;
input BackendDAEFunc.StructurallySingularSystemHandlerFunc sssHandler;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;