summaryrefslogtreecommitdiff
path: root/src/theory/arith/soi_simplex.cpp
blob: 1b22bc81c8cd253255c9ca89724d41d1210a9da8 (plain)
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
/******************************************************************************
 * Top contributors (to current version):
 *   Tim King, Aina Niemetz, Morgan Deters
 *
 * This file is part of the cvc5 project.
 *
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 * in the top-level source directory and their institutional affiliations.
 * All rights reserved.  See the file COPYING in the top-level source
 * directory for licensing information.
 * ****************************************************************************
 *
 * This is an implementation of the Simplex Module for the Simplex for
 * DPLL(T) decision procedure.
 */
#include "theory/arith/soi_simplex.h"

#include <algorithm>

#include "base/output.h"
#include "options/arith_options.h"
#include "smt/smt_statistics_registry.h"
#include "theory/arith/constraint.h"
#include "theory/arith/error_set.h"
#include "theory/arith/tableau.h"
#include "util/statistics_stats.h"

using namespace std;

namespace cvc5 {
namespace theory {
namespace arith {

SumOfInfeasibilitiesSPD::SumOfInfeasibilitiesSPD(LinearEqualityModule& linEq,
                                                 ErrorSet& errors,
                                                 RaiseConflict conflictChannel,
                                                 TempVarMalloc tvmalloc)
    : SimplexDecisionProcedure(linEq, errors, conflictChannel, tvmalloc),
      d_soiVar(ARITHVAR_SENTINEL),
      d_pivotBudget(0),
      d_prevWitnessImprovement(AntiProductive),
      d_witnessImprovementInARow(0),
      d_sgnDisagreements(),
      d_statistics("theory::arith::SOI", d_pivots)
{ }

SumOfInfeasibilitiesSPD::Statistics::Statistics(const std::string& name,
                                                uint32_t& pivots)
    : d_initialSignalsTime(
        smtStatisticsRegistry().registerTimer(name + "initialProcessTime")),
      d_initialConflicts(
          smtStatisticsRegistry().registerInt(name + "UpdateConflicts")),
      d_soiFoundUnsat(smtStatisticsRegistry().registerInt(name + "FoundUnsat")),
      d_soiFoundSat(smtStatisticsRegistry().registerInt(name + "FoundSat")),
      d_soiMissed(smtStatisticsRegistry().registerInt(name + "Missed")),
      d_soiConflicts(
          smtStatisticsRegistry().registerInt(name + "ConfMin::num")),
      d_hasToBeMinimal(
          smtStatisticsRegistry().registerInt(name + "HasToBeMin")),
      d_maybeNotMinimal(
          smtStatisticsRegistry().registerInt(name + "MaybeNotMin")),
      d_soiTimer(smtStatisticsRegistry().registerTimer(name + "Time")),
      d_soiFocusConstructionTimer(
          smtStatisticsRegistry().registerTimer(name + "Construction")),
      d_soiConflictMinimization(smtStatisticsRegistry().registerTimer(
          name + "Conflict::Minimization")),
      d_selectUpdateForSOI(
          smtStatisticsRegistry().registerTimer(name + "selectSOI")),
      d_finalCheckPivotCounter(
          smtStatisticsRegistry().registerReference<uint32_t>(
              name + "lastPivots", pivots))
{
}

Result::Sat SumOfInfeasibilitiesSPD::findModel(bool exactResult){
  Assert(d_conflictVariables.empty());
  Assert(d_sgnDisagreements.empty());

  d_pivots = 0;
  static thread_local unsigned int instance = 0;
  instance = instance + 1;
  static const bool verbose = false;

  if(d_errorSet.errorEmpty() && !d_errorSet.moreSignals()){
    Debug("soi::findModel") << "soiFindModel("<< instance <<") trivial" << endl;
    Assert(d_conflictVariables.empty());
    return Result::SAT;
  }

  // We need to reduce this because of
  d_errorSet.reduceToSignals();

  // We must start tracking NOW
  d_errorSet.setSelectionRule(options::ErrorSelectionRule::SUM_METRIC);

  if(initialProcessSignals()){
    d_conflictVariables.purge();
    if (verbose)
    {
      CVC4Message() << "fcFindModel(" << instance << ") early conflict" << endl;
    }
    Debug("soi::findModel") << "fcFindModel("<< instance <<") early conflict" << endl;
    Assert(d_conflictVariables.empty());
    return Result::UNSAT;
  }else if(d_errorSet.errorEmpty()){
    Debug("soi::findModel") << "fcFindModel("<< instance <<") fixed itself" << endl;
    Assert(!d_errorSet.moreSignals());
    Assert(d_conflictVariables.empty());
    return Result::SAT;
  }

  Debug("soi::findModel") << "fcFindModel(" << instance <<") start non-trivial" << endl;

  exactResult |= options::arithStandardCheckVarOrderPivots() < 0;

  d_prevWitnessImprovement = HeuristicDegenerate;
  d_witnessImprovementInARow = 0;

  Result::Sat result = Result::SAT_UNKNOWN;

  if(result == Result::SAT_UNKNOWN){
    if(exactResult){
      d_pivotBudget = -1;
    }else{
      d_pivotBudget = options::arithStandardCheckVarOrderPivots();
    }

    result = sumOfInfeasibilities();

    if(result ==  Result::UNSAT){
      ++(d_statistics.d_soiFoundUnsat);
      if (verbose)
      {
        CVC4Message() << "fc found unsat";
      }
    }else if(d_errorSet.errorEmpty()){
      ++(d_statistics.d_soiFoundSat);
      if (verbose)
      {
        CVC4Message() << "fc found model";
      }
    }else{
      ++(d_statistics.d_soiMissed);
      if (verbose)
      {
        CVC4Message() << "fc missed";
      }
    }
  }
  if (verbose)
  {
    CVC4Message() << "(" << instance << ") pivots " << d_pivots << endl;
  }

  Assert(!d_errorSet.moreSignals());
  if(result == Result::SAT_UNKNOWN && d_errorSet.errorEmpty()){
    result = Result::SAT;
  }

  // ensure that the conflict variable is still in the queue.
  d_conflictVariables.purge();

  Debug("soi::findModel") << "end findModel() " << instance << " " << result <<  endl;

  Assert(d_conflictVariables.empty());
  return result;
}


void SumOfInfeasibilitiesSPD::logPivot(WitnessImprovement w){
  if(d_pivotBudget > 0) {
    --d_pivotBudget;
  }
  Assert(w != AntiProductive);

  if(w == d_prevWitnessImprovement){
    ++d_witnessImprovementInARow;
    if(d_witnessImprovementInARow == 0){
      --d_witnessImprovementInARow;
    }
  }else{
    if(w != BlandsDegenerate){
      d_witnessImprovementInARow = 1;
    }
    d_prevWitnessImprovement = w;
  }
  if(strongImprovement(w)){
    d_leavingCountSinceImprovement.purge();
  }

  Debug("logPivot") << "logPivot " << d_prevWitnessImprovement << " "  << d_witnessImprovementInARow << endl;
}

uint32_t SumOfInfeasibilitiesSPD::degeneratePivotsInARow() const {
  switch(d_prevWitnessImprovement){
  case ConflictFound:
  case ErrorDropped:
  case FocusImproved:
    return 0;
  case HeuristicDegenerate:
  case BlandsDegenerate:
    return d_witnessImprovementInARow;
  // Degenerate is unreachable for its own reasons
  case Degenerate:
  case FocusShrank:
  case AntiProductive:
    Unreachable();
    return -1;
  }
  Unreachable();
}

void SumOfInfeasibilitiesSPD::adjustFocusAndError(const UpdateInfo& up, const AVIntPairVec& focusChanges){
  uint32_t newErrorSize = d_errorSet.errorSize();
  adjustInfeasFunc(d_statistics.d_soiFocusConstructionTimer, d_soiVar, focusChanges);
  d_errorSize = newErrorSize;
}


UpdateInfo SumOfInfeasibilitiesSPD::selectUpdate(LinearEqualityModule::UpdatePreferenceFunction upf, LinearEqualityModule::VarPreferenceFunction bpf) {
  UpdateInfo selected;

  static int instance = 0 ;
  ++instance;

  Debug("soi::selectPrimalUpdate")
    << "selectPrimalUpdate " << instance << endl
    << d_soiVar << " " << d_tableau.basicRowLength(d_soiVar)
    << " " << d_linEq.debugBasicAtBoundCount(d_soiVar) << endl;

  typedef std::vector<Cand> CandVector;
  CandVector candidates;

  for(Tableau::RowIterator ri = d_tableau.basicRowIterator(d_soiVar); !ri.atEnd(); ++ri){
    const Tableau::Entry& e = *ri;
    ArithVar curr = e.getColVar();
    if(curr == d_soiVar){ continue; }

    int sgn = e.getCoefficient().sgn();
    bool candidate =
      (sgn > 0 && d_variables.cmpAssignmentUpperBound(curr) < 0) ||
      (sgn < 0 && d_variables.cmpAssignmentLowerBound(curr) > 0);

    Debug("soi::selectPrimalUpdate")
      << "storing " << d_soiVar
      << " " << curr
      << " " << candidate
      << " " << e.getCoefficient()
      << " " << sgn << endl;

    if(candidate) {
      candidates.push_back(Cand(curr, 0, sgn, &e.getCoefficient()));
    }
  }

  CompPenaltyColLength colCmp(&d_linEq);
  CandVector::iterator i = candidates.begin();
  CandVector::iterator end = candidates.end();
  std::make_heap(i, end, colCmp);

  // For the first 3 pivots take the best
  // After that, once an improvement is found on look at a
  // small number of pivots after finding an improvement
  // the longer the search to more willing we are to look at more candidates
  int maxCandidatesAfterImprove =
    (d_pivots <= 2) ?  std::numeric_limits<int>::max() : d_pivots/5;

  int candidatesAfterFocusImprove = 0;
  while(i != end && candidatesAfterFocusImprove <= maxCandidatesAfterImprove){
    std::pop_heap(i, end, colCmp);
    --end;
    Cand& cand = (*end);
    ArithVar curr = cand.d_nb;
    const Rational& coeff = *cand.d_coeff;

    LinearEqualityModule::UpdatePreferenceFunction leavingPrefFunc = selectLeavingFunction(curr);
    UpdateInfo currProposal = d_linEq.speculativeUpdate(curr, coeff, leavingPrefFunc);

    Debug("soi::selectPrimalUpdate")
      << "selected " << selected << endl
      << "currProp " << currProposal << endl
      << "coeff " << coeff << endl;

    Assert(!currProposal.uninitialized());

    if(candidatesAfterFocusImprove > 0){
      candidatesAfterFocusImprove++;
    }

    if(selected.uninitialized() || (d_linEq.*upf)(selected, currProposal)){
      selected = currProposal;
      WitnessImprovement w = selected.getWitness(false);
      Debug("soi::selectPrimalUpdate") << "selected " << w << endl;
      //setPenalty(curr, w);
      if(improvement(w)){
        bool exitEarly;
        switch(w){
        case ConflictFound: exitEarly = true; break;
        case FocusImproved:
          candidatesAfterFocusImprove = 1;
          exitEarly = false;
          break;
        default:
          exitEarly = false; break;
        }
        if(exitEarly){ break; }
      }
    }else{
      Debug("soi::selectPrimalUpdate") << "dropped "<< endl;
    }

  }
  return selected;
}

bool debugCheckWitness(const UpdateInfo& inf, WitnessImprovement w, bool useBlands){
  if(inf.getWitness(useBlands) == w){
    switch(w){
    case ConflictFound: return inf.foundConflict();
    case ErrorDropped: return inf.errorsChange() < 0;
    case FocusImproved: return inf.focusDirection() > 0;
    case FocusShrank: return false; // This is not a valid output
    case Degenerate: return false; // This is not a valid output
    case BlandsDegenerate: return useBlands;
    case HeuristicDegenerate: return !useBlands;
    case AntiProductive: return false;
    }
  }
  return false;
}


void SumOfInfeasibilitiesSPD::debugPrintSignal(ArithVar updated) const{
  Debug("updateAndSignal") << "updated basic " << updated;
  Debug("updateAndSignal") << " length " << d_tableau.basicRowLength(updated);
  Debug("updateAndSignal") << " consistent " << d_variables.assignmentIsConsistent(updated);
  int dir = !d_variables.assignmentIsConsistent(updated) ?
    d_errorSet.getSgn(updated) : 0;
  Debug("updateAndSignal") << " dir " << dir;
  Debug("updateAndSignal") << " debugBasicAtBoundCount " << d_linEq.debugBasicAtBoundCount(updated) << endl;
}


void SumOfInfeasibilitiesSPD::updateAndSignal(const UpdateInfo& selected, WitnessImprovement w){
  ArithVar nonbasic = selected.nonbasic();

  static bool verbose = false;

  Debug("updateAndSignal") << "updateAndSignal " << selected << endl;

  stringstream ss;
  if(verbose){
    d_errorSet.debugPrint(ss);
    if(selected.describesPivot()){
      ArithVar leaving = selected.leaving();
      ss << "leaving " << leaving
         << " " << d_tableau.basicRowLength(leaving)
         << " " << d_linEq.debugBasicAtBoundCount(leaving)
         << endl;
    }
    if(degenerate(w) && selected.describesPivot()){
      ArithVar leaving = selected.leaving();
      CVC4Message() << "degenerate " << leaving << ", atBounds "
                    << d_linEq.basicsAtBounds(selected) << ", len "
                    << d_tableau.basicRowLength(leaving) << ", bc "
                    << d_linEq.debugBasicAtBoundCount(leaving) << endl;
    }
  }

  if(selected.describesPivot()){
    ConstraintP limiting = selected.limiting();
    ArithVar basic = limiting->getVariable();
    Assert(d_linEq.basicIsTracked(basic));
    d_linEq.pivotAndUpdate(basic, nonbasic, limiting->getValue());
  }else{
    Assert(!selected.unbounded() || selected.errorsChange() < 0);

    DeltaRational newAssignment =
      d_variables.getAssignment(nonbasic) + selected.nonbasicDelta();

    d_linEq.updateTracked(nonbasic, newAssignment);
  }
  d_pivots++;

  increaseLeavingCount(nonbasic);

  vector< pair<ArithVar, int> > focusChanges;
  while(d_errorSet.moreSignals()){
    ArithVar updated = d_errorSet.topSignal();
    int prevFocusSgn = d_errorSet.popSignal();

    if(d_tableau.isBasic(updated)){
      Assert(!d_variables.assignmentIsConsistent(updated)
             == d_errorSet.inError(updated));
      if(Debug.isOn("updateAndSignal")){debugPrintSignal(updated);}
      if(!d_variables.assignmentIsConsistent(updated)){
        if(checkBasicForConflict(updated)){
          reportConflict(updated);
          //Assert(debugUpdatedBasic(selected, updated));
        }
      }
    }else{
      Debug("updateAndSignal") << "updated nonbasic " << updated << endl;
    }
    int currFocusSgn = d_errorSet.focusSgn(updated);
    if(currFocusSgn != prevFocusSgn){
      int change = currFocusSgn - prevFocusSgn;
      focusChanges.push_back(make_pair(updated, change));
    }
  }

  if (verbose)
  {
    CVC4Message() << "conflict variable " << selected << endl;
    CVC4Message() << ss.str();
  }
  if(Debug.isOn("error")){ d_errorSet.debugPrint(Debug("error")); }

  //Assert(debugSelectedErrorDropped(selected, d_errorSize, d_errorSet.errorSize()));

  adjustFocusAndError(selected, focusChanges);
}

void SumOfInfeasibilitiesSPD::qeAddRange(uint32_t begin, uint32_t end){
  Assert(!d_qeInSoi.empty());
  for(uint32_t i = begin; i != end; ++i){
    ArithVar v = d_qeConflict[i];
    addToInfeasFunc(d_statistics.d_soiConflictMinimization, d_soiVar, v);
    d_qeInSoi.add(v);
  }
}

void SumOfInfeasibilitiesSPD::qeRemoveRange(uint32_t begin, uint32_t end){
  for(uint32_t i = begin; i != end; ++i){
    ArithVar v = d_qeConflict[i];
    removeFromInfeasFunc(d_statistics.d_soiConflictMinimization, d_soiVar, v);
    d_qeInSoi.remove(v);
  }
  Assert(!d_qeInSoi.empty());
}

void SumOfInfeasibilitiesSPD::qeSwapRange(uint32_t N, uint32_t r, uint32_t s){
  for(uint32_t i = 0; i < N; ++i){
    std::swap(d_qeConflict[r+i], d_qeConflict[s+i]);
  }
}

/**
 * Region notation:
 * A region is either
 *  - A single element X@i with the name X at the position i
 *  - A sequence of indices X@[i,j) with the name X and the elements between i [inclusive] and j exclusive
 *  - A concatenation of regions R1 and R2, R1;R2
 *
 * Given the fixed assumptions C @ [0,cEnd) and a set of candidate minimizations U@[cEnd, uEnd)
 * s.t. C \cup U is known to be in conflict ([0,uEnd) has a conflict), find a minimal
 * subset of U, Delta, s.t. C \cup Delta is in conflict.
 *
 * Pre:
 *  [0, uEnd) is a set and is in conflict.
 *    uEnd <= assumptions.size()
 *  [0, cEnd) is in d_inSoi.
 *
 * Invariants: [0,cEnd) is never modified
 *
 * Post:
 *  [0, cEnd); [cEnd, deltaEnd) is in conflict
 *  [0, deltaEnd) is a set
 *  [0, deltaEnd) is in d_inSoi
 */
uint32_t SumOfInfeasibilitiesSPD::quickExplainRec(uint32_t cEnd, uint32_t uEnd){
  Assert(cEnd <= uEnd);
  Assert(d_qeInUAndNotInSoi.empty());
  Assert(d_qeGreedyOrder.empty());

  const Tableau::Entry* spoiler = NULL;

  if(d_soiVar != ARITHVAR_SENTINEL && d_linEq.selectSlackEntry(d_soiVar, false) == NULL){
    // already in conflict
    return cEnd;
  }

  Assert(cEnd < uEnd);

  // Phase 1 : Construct the conflict greedily

  for(uint32_t i = cEnd; i < uEnd; ++i){
    d_qeInUAndNotInSoi.add(d_qeConflict[i]);
  }
  if(d_soiVar == ARITHVAR_SENTINEL){ // special case for d_soiVar being empty
    ArithVar first = d_qeConflict[cEnd];
    d_soiVar = constructInfeasiblityFunction(d_statistics.d_soiConflictMinimization, first);
    d_qeInSoi.add(first);
    d_qeInUAndNotInSoi.remove(first);
    d_qeGreedyOrder.push_back(first);
  }
  while((spoiler = d_linEq.selectSlackEntry(d_soiVar, false)) != NULL){
    Assert(!d_qeInUAndNotInSoi.empty());

    ArithVar nb = spoiler->getColVar();
    int oppositeSgn = -(spoiler->getCoefficient().sgn());
    Assert(oppositeSgn != 0);

    ArithVar basicWithOp = find_basic_in_sgns(d_qeSgns, nb, oppositeSgn, d_qeInUAndNotInSoi, true);
    Assert(basicWithOp != ARITHVAR_SENTINEL);

    addToInfeasFunc(d_statistics.d_soiConflictMinimization, d_soiVar, basicWithOp);
    d_qeInSoi.add(basicWithOp);
    d_qeInUAndNotInSoi.remove(basicWithOp);
    d_qeGreedyOrder.push_back(basicWithOp);
  }
  Assert(spoiler == NULL);

  // Compact the set u
  uint32_t newEnd = cEnd + d_qeGreedyOrder.size();
  std::copy(d_qeGreedyOrder.begin(), d_qeGreedyOrder.end(), d_qeConflict.begin()+cEnd);

  d_qeInUAndNotInSoi.purge();
  d_qeGreedyOrder.clear();

   // Phase 2 : Recursively determine the minimal set of rows

  uint32_t xPos = cEnd;
  std::swap(d_qeGreedyOrder[xPos], d_qeGreedyOrder[newEnd - 1]);
  uint32_t uBegin = xPos + 1;
  uint32_t split = (newEnd - uBegin)/2 + uBegin;

  //assumptions : C @ [0, cEnd); X @ xPos; U1 @ [u1Begin, split); U2 @ [split, newEnd)
  // [0, newEnd) == d_inSoi

  uint32_t compactU2;
  if(split == newEnd){ // U2 is empty
    compactU2 = newEnd;
  }else{
    // Remove U2 from Soi
    qeRemoveRange(split, newEnd);
    // [0, split) == d_inSoi

    // pre assumptions: C + X + U1 @ [0,split); U2 [split, newEnd)
    compactU2 = quickExplainRec(split, newEnd);
    // post:
    //  assumptions: C + X + U1 @ [0, split); delta2 @ [split - compactU2)
    //  d_inSoi = [0, compactU2)
  }
  uint32_t deltaSize = compactU2 - split;
  qeSwapRange(deltaSize, uBegin, split);
  uint32_t d2End = uBegin+deltaSize;
  // assumptions : C @ [0, cEnd); X @ xPos; delta2 @ [uBegin, d2End); U1 @ [d2End, compactU2)
  //  d_inSoi == [0, compactU2)

  uint32_t d1End;
  if(d2End == compactU2){ // U1 is empty
    d1End = d2End;
  }else{
    qeRemoveRange(d2End, compactU2);

    //pre assumptions : C + X + delta2 @ [0, d2End); U1 @ [d2End, compactU2);
    d1End = quickExplainRec(d2End, compactU2);
    //post:
    // assumptions : C + X + delta2 @ [0, d2End); delta1 @ [d2End, d1End);
    // d_inSoi = [0, d1End)
  }
  //After both:
  // d_inSoi == [0, d1End), C @ [0, cEnd); X + delta2 + delta 1 @ [xPos, d1End);

  Assert(d_qeInUAndNotInSoi.empty());
  Assert(d_qeGreedyOrder.empty());
  return d1End;
}

void SumOfInfeasibilitiesSPD::quickExplain(){
  Assert(d_qeInSoi.empty());
  Assert(d_qeInUAndNotInSoi.empty());
  Assert(d_qeGreedyOrder.empty());
  Assert(d_soiVar == ARITHVAR_SENTINEL);
  Assert(d_qeSgns.empty());

  d_qeConflict.clear();
  d_errorSet.pushFocusInto(d_qeConflict);

  //cout <<  d_qeConflict.size() << " ";
  uint32_t size = d_qeConflict.size();

  if(size > 2){
    for(ErrorSet::focus_iterator iter = d_errorSet.focusBegin(), end = d_errorSet.focusEnd(); iter != end; ++iter){
      ArithVar e = *iter;
      addRowSgns(d_qeSgns, e, d_errorSet.getSgn(e));
    }
    uint32_t end = quickExplainRec(0u, size);
    Assert(end <= d_qeConflict.size());
    Assert(d_soiVar != ARITHVAR_SENTINEL);
    Assert(!d_qeInSoi.empty());

    d_qeConflict.resize(end);
    tearDownInfeasiblityFunction(d_statistics.d_soiConflictMinimization, d_soiVar);
    d_soiVar = ARITHVAR_SENTINEL;
    d_qeInSoi.purge();
    d_qeSgns.clear();
  }

  //cout << d_qeConflict.size() << endl;

  Assert(d_qeInSoi.empty());
  Assert(d_qeInUAndNotInSoi.empty());
  Assert(d_qeGreedyOrder.empty());
  Assert(d_soiVar == ARITHVAR_SENTINEL);
  Assert(d_qeSgns.empty());
}

unsigned SumOfInfeasibilitiesSPD::trySet(const ArithVarVec& set){
  Assert(d_soiVar == ARITHVAR_SENTINEL);
  bool success = false;
  if(set.size() >= 2){
    d_soiVar = constructInfeasiblityFunction(d_statistics.d_soiConflictMinimization, set);
    success = d_linEq.selectSlackEntry(d_soiVar, false) == NULL;

    tearDownInfeasiblityFunction(d_statistics.d_soiConflictMinimization, d_soiVar);
    d_soiVar = ARITHVAR_SENTINEL;
  }
  return success ? set.size() : std::numeric_limits<int>::max();
}

std::vector< ArithVarVec > SumOfInfeasibilitiesSPD::greedyConflictSubsets(){
  Debug("arith::greedyConflictSubsets") << "greedyConflictSubsets start" << endl;

  std::vector< ArithVarVec > subsets;
  Assert(d_soiVar == ARITHVAR_SENTINEL);

  if(d_errorSize <= 2){
    ArithVarVec inError;
    d_errorSet.pushFocusInto(inError);

    Assert(debugIsASet(inError));
    subsets.push_back(inError);
    return subsets;
  }
  Assert(d_errorSize > 2);

  //sgns_table< <nonbasic,sgn>, [basics] >;
  // Phase 0: Construct the sgns table
  sgn_table sgns;
  DenseSet hasParticipated; //Has participated in a conflict
  for(ErrorSet::focus_iterator iter = d_errorSet.focusBegin(), end = d_errorSet.focusEnd(); iter != end; ++iter){
    ArithVar e = *iter;
    addRowSgns(sgns, e, d_errorSet.getSgn(e));

    Debug("arith::greedyConflictSubsets") << "basic error var: " << e << endl;
    if(Debug.isOn("arith::greedyConflictSubsets")){
      d_tableau.debugPrintIsBasic(e);
      d_tableau.printBasicRow(e, Debug("arith::greedyConflictSubsets"));
    }
  }

  // Phase 1: Try to find at least 1 pair for every element
  ArithVarVec tmp;
  tmp.push_back(0);
  tmp.push_back(0);
  for(ErrorSet::focus_iterator iter = d_errorSet.focusBegin(), end = d_errorSet.focusEnd(); iter != end; ++iter){
    ArithVar e = *iter;
    tmp[0] = e;

    int errSgn = d_errorSet.getSgn(e);
    bool decreasing = errSgn < 0;
    const Tableau::Entry* spoiler = d_linEq.selectSlackEntry(e, decreasing);
    Assert(spoiler != NULL);
    ArithVar nb = spoiler->getColVar();
    int oppositeSgn = -(errSgn * (spoiler->getCoefficient().sgn()));

    sgn_table::const_iterator opposites = find_sgns(sgns, nb, oppositeSgn);
    Assert(opposites != sgns.end());

    const ArithVarVec& choices = (*opposites).second;
    for(ArithVarVec::const_iterator j = choices.begin(), jend = choices.end(); j != jend; ++j){
      ArithVar b = *j;
      if(b < e){ continue; }
      tmp[0] = e;
      tmp[1] = b;
      if(trySet(tmp) == 2){
        Debug("arith::greedyConflictSubsets")  << "found a pair " << b << " " << e << endl;
        hasParticipated.softAdd(b);
        hasParticipated.softAdd(e);
        Assert(debugIsASet(tmp));
        subsets.push_back(tmp);
        ++(d_statistics.d_soiConflicts);
        ++(d_statistics.d_hasToBeMinimal);
      }
    }
  }


  // Phase 2: If there is a variable that has not participated attempt to start a conflict
  ArithVarVec possibleStarts; //List of elements that can be tried for starts.
  d_errorSet.pushFocusInto(possibleStarts);
  while(!possibleStarts.empty()){
    Assert(d_soiVar == ARITHVAR_SENTINEL);

    ArithVar v = possibleStarts.back();
    possibleStarts.pop_back();
    if(hasParticipated.isMember(v)){ continue; }

    hasParticipated.add(v);

    Assert(d_soiVar == ARITHVAR_SENTINEL);
    //d_soiVar's row =  \sumofinfeasibilites underConstruction
    ArithVarVec underConstruction;
    underConstruction.push_back(v);
    d_soiVar = constructInfeasiblityFunction(d_statistics.d_soiConflictMinimization, v);

    Debug("arith::greedyConflictSubsets") << "trying " << v << endl;

    const Tableau::Entry* spoiler = NULL;
    while( (spoiler = d_linEq.selectSlackEntry(d_soiVar, false)) != NULL){
      ArithVar nb = spoiler->getColVar();
      int oppositeSgn = -(spoiler->getCoefficient().sgn());
      Assert(oppositeSgn != 0);

      Debug("arith::greedyConflictSubsets") << "looking for " << nb << " " << oppositeSgn << endl;

      ArithVar basicWithOp = find_basic_in_sgns(sgns, nb, oppositeSgn, hasParticipated, false);

      if(basicWithOp == ARITHVAR_SENTINEL){
        Debug("arith::greedyConflictSubsets") << "search did not work  for " << nb << endl;
        // greedy construction has failed
        break;
      }else{
        Debug("arith::greedyConflictSubsets") << "found  " << basicWithOp << endl;

        addToInfeasFunc(d_statistics.d_soiConflictMinimization, d_soiVar, basicWithOp);
        hasParticipated.softAdd(basicWithOp);
        underConstruction.push_back(basicWithOp);
      }
    }
    if(spoiler == NULL){
      Debug("arith::greedyConflictSubsets") << "success" << endl;
      //then underConstruction contains a conflicting subset
      Assert(debugIsASet(underConstruction));
      subsets.push_back(underConstruction);
      ++d_statistics.d_soiConflicts;
      if(underConstruction.size() == 3){
        ++d_statistics.d_hasToBeMinimal;
      }else{
        ++d_statistics.d_maybeNotMinimal;
      }
    }else{
      Debug("arith::greedyConflictSubsets") << "failure" << endl;
    }
    tearDownInfeasiblityFunction(d_statistics.d_soiConflictMinimization, d_soiVar);
    d_soiVar = ARITHVAR_SENTINEL;
    // if(false && spoiler == NULL){
    //   ArithVarVec tmp;
    //   int smallest = tryAllSubsets(underConstruction, 0, tmp);
    //   cout << underConstruction.size() << " " << smallest << endl;
    //   Assert(smallest >= underConstruction.size());
    //   if(smallest < underConstruction.size()){
    //     exit(-1);
    //   }
    // }
  }

  Assert(d_soiVar == ARITHVAR_SENTINEL);
  Debug("arith::greedyConflictSubsets") << "greedyConflictSubsets done" << endl;
  return subsets;
}

bool SumOfInfeasibilitiesSPD::generateSOIConflict(const ArithVarVec& subset){
  Assert(d_soiVar == ARITHVAR_SENTINEL);
  d_soiVar = constructInfeasiblityFunction(d_statistics.d_soiConflictMinimization, subset);
  Assert(!subset.empty());
  Assert(!d_conflictBuilder->underConstruction());

  Debug("arith::generateSOIConflict") << "SumOfInfeasibilitiesSPD::generateSOIConflict(...) start" << endl;

  bool success = false;
    
  for(ArithVarVec::const_iterator iter = subset.begin(), end = subset.end(); iter != end; ++iter){
    ArithVar e = *iter;
    ConstraintP violated = d_errorSet.getViolated(e);
    Assert(violated != NullConstraint);

    int sgn = d_errorSet.getSgn(e);
    const Rational& violatedCoeff = sgn > 0 ? d_negOne : d_posOne;
    Debug("arith::generateSOIConflict") << "basic error var: "
                                        << "(" <<  violatedCoeff << ")"
                                        << " " << violated
                                        << endl;


    d_conflictBuilder->addConstraint(violated, violatedCoeff);
    Assert(violated->hasProof());
    if(!success && !violated->negationHasProof()){
      success = true;
      d_conflictBuilder->makeLastConsequent();
    }
  }
  
  if(!success){
    // failure
    d_conflictBuilder->reset();
  } else {
    // pick a violated constraint arbitrarily. any of them may be selected for the conflict
    Assert(d_conflictBuilder->underConstruction());
    Assert(d_conflictBuilder->consequentIsSet());

    for(Tableau::RowIterator i = d_tableau.basicRowIterator(d_soiVar); !i.atEnd(); ++i){
      const Tableau::Entry& entry = *i;
      ArithVar v = entry.getColVar();
      if(v == d_soiVar){ continue; }
      const Rational& coeff = entry.getCoefficient();

      ConstraintP c = (coeff.sgn() > 0) ?
        d_variables.getUpperBoundConstraint(v) :
        d_variables.getLowerBoundConstraint(v);
      
      Debug("arith::generateSOIConflict") << "non-basic var: "
                                          << "(" <<  coeff << ")"
                                          << " " << c
                                          << endl;
      d_conflictBuilder->addConstraint(c, coeff);
    }
    ConstraintCP conflicted = d_conflictBuilder->commitConflict();
    d_conflictChannel.raiseConflict(conflicted,
                                    InferenceId::ARITH_CONF_SOI_SIMPLEX);
  }

  tearDownInfeasiblityFunction(d_statistics.d_soiConflictMinimization, d_soiVar);
  d_soiVar = ARITHVAR_SENTINEL;
  Debug("arith::generateSOIConflict") << "SumOfInfeasibilitiesSPD::generateSOIConflict(...) done" << endl;
  Assert(d_soiVar == ARITHVAR_SENTINEL);
  Assert(!d_conflictBuilder->underConstruction());
  return success;
}


WitnessImprovement SumOfInfeasibilitiesSPD::SOIConflict(){
  static int instance = 0;
  instance++;
  
  Debug("arith::SOIConflict") << "SumOfInfeasibilitiesSPD::SOIConflict() start "
                              << instance << ": |E| = " << d_errorSize << endl;
  if(Debug.isOn("arith::SOIConflict")){
    d_errorSet.debugPrint(cout);
  }
  Debug("arith::SOIConflict") << endl;

  tearDownInfeasiblityFunction(d_statistics.d_soiConflictMinimization, d_soiVar);
  d_soiVar = ARITHVAR_SENTINEL;

  if(options::soiQuickExplain()){
    quickExplain();
    generateSOIConflict(d_qeConflict);
  }else{
    vector<ArithVarVec> subsets = greedyConflictSubsets();
    Assert(d_soiVar == ARITHVAR_SENTINEL);
    bool anySuccess = false;
    Assert(!subsets.empty());
    for(vector<ArithVarVec>::const_iterator i = subsets.begin(), end = subsets.end(); i != end; ++i){
      const ArithVarVec& subset = *i;
      Assert(debugIsASet(subset));
      anySuccess = generateSOIConflict(subset) || anySuccess;
      //Node conflict = generateSOIConflict(subset);
      //cout << conflict << endl;

      //reportConflict(conf); do not do this. We need a custom explanations!
      //d_conflictChannel(conflict);
    }
    Assert(anySuccess);
  }
  Assert(d_soiVar == ARITHVAR_SENTINEL);
  d_soiVar = constructInfeasiblityFunction(d_statistics.d_soiConflictMinimization);

  //reportConflict(conf); do not do this. We need a custom explanations!
  d_conflictVariables.add(d_soiVar);

  Debug("arith::SOIConflict") << "SumOfInfeasibilitiesSPD::SOIConflict() done "
                              << instance << "end" << endl;
  return ConflictFound;
}

WitnessImprovement SumOfInfeasibilitiesSPD::soiRound() {
  Assert(d_soiVar != ARITHVAR_SENTINEL);

  bool useBlands = degeneratePivotsInARow() >= s_maxDegeneratePivotsBeforeBlandsOnLeaving;
  LinearEqualityModule::UpdatePreferenceFunction upf;
  if(useBlands) {
    upf = &LinearEqualityModule::preferWitness<false>;
  } else {
    upf = &LinearEqualityModule::preferWitness<true>;
  }

  LinearEqualityModule::VarPreferenceFunction bpf = useBlands ?
    &LinearEqualityModule::minVarOrder :
    &LinearEqualityModule::minRowLength;
  bpf = &LinearEqualityModule::minVarOrder;

  UpdateInfo selected = selectUpdate(upf, bpf);

  if(selected.uninitialized()){
    Debug("selectFocusImproving") << "SOI is optimum, but we don't have sat/conflict yet" << endl;
    return SOIConflict();
  }else{
    Assert(!selected.uninitialized());
    WitnessImprovement w = selected.getWitness(false);
    Assert(debugCheckWitness(selected, w, false));

    updateAndSignal(selected, w);
    logPivot(w);
    return w;
  }
}

bool SumOfInfeasibilitiesSPD::debugSOI(WitnessImprovement w, ostream& out, int instance) const{
  return true;
  // out << "DLV("<<instance<<") ";
  // switch(w){
  // case ConflictFound:
  //   out << "found conflict" << endl;
  //   return !d_conflictVariables.empty();
  // case ErrorDropped:
  //   return false;
  //   // out << "dropped " << prevErrorSize - d_errorSize << endl;
  //   // return d_errorSize < prevErrorSize;
  // case FocusImproved:
  //   out << "focus improved"<< endl;
  //   return d_errorSize == prevErrorSize;
  // case FocusShrank:
  //   Unreachable();
  //   return false;
  // case BlandsDegenerate:
  //   out << "bland degenerate"<< endl;
  //   return true;
  // case HeuristicDegenerate:
  //   out << "heuristic degenerate"<< endl;
  //   return true;
  // case AntiProductive:
  // case Degenerate:
  //   return false;
  // }
  // return false;
}

Result::Sat SumOfInfeasibilitiesSPD::sumOfInfeasibilities(){
  static int instance = 0;
  static bool verbose = false;

  TimerStat::CodeTimer codeTimer(d_statistics.d_soiTimer);

  Assert(d_sgnDisagreements.empty());
  Assert(d_pivotBudget != 0);
  Assert(d_errorSize == d_errorSet.errorSize());
  Assert(d_errorSize > 0);
  Assert(d_conflictVariables.empty());
  Assert(d_soiVar == ARITHVAR_SENTINEL);

  //d_scores.purge();
  d_soiVar = constructInfeasiblityFunction(d_statistics.d_soiFocusConstructionTimer);


  while(d_pivotBudget != 0  && d_errorSize > 0 && d_conflictVariables.empty()){
    ++instance;
    Debug("dualLike") << "dualLike " << instance << endl;

    Assert(d_errorSet.noSignals());
    // Possible outcomes:
    // - conflict
    // - budget was exhausted
    // - focus went down
    Debug("dualLike") << "selectFocusImproving " << endl;
    WitnessImprovement w = soiRound();

    Assert(d_errorSize == d_errorSet.errorSize());

    if (verbose)
    {
      debugSOI(w, CVC4Message(), instance);
    }
    Assert(debugSOI(w, Debug("dualLike"), instance));
  }


  if(d_soiVar != ARITHVAR_SENTINEL){
    tearDownInfeasiblityFunction(d_statistics.d_soiFocusConstructionTimer, d_soiVar);
    d_soiVar = ARITHVAR_SENTINEL;
  }

  Assert(d_soiVar == ARITHVAR_SENTINEL);
  if(!d_conflictVariables.empty()){
    return Result::UNSAT;
  }else if(d_errorSet.errorEmpty()){
    Assert(d_errorSet.noSignals());
    return Result::SAT;
  }else{
    Assert(d_pivotBudget == 0);
    return Result::SAT_UNKNOWN;
  }
}

}  // namespace arith
}  // namespace theory
}  // namespace cvc5
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback