summaryrefslogtreecommitdiff
path: root/src/theory/arith/nl/icp/intersection.cpp
blob: 3dc0ca8155a3f592d2670dcb8cbe472d42f0c40b (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
/*********************                                                        */
/*! \file intersection.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Gereon Kremer
 ** This file is part of the CVC4 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.\endverbatim
 **
 ** \brief
 **/

#include "theory/arith/nl/icp/intersection.h"

#ifdef CVC4_POLY_IMP

#include <iostream>

#include <poly/polyxx.h>

#include "base/check.h"
#include "base/output.h"
#include "theory/arith/nl/poly_conversion.h"

namespace CVC4 {
namespace theory {
namespace arith {
namespace nl {
namespace icp {

PropagationResult intersect_interval_with(poly::Interval& cur,
                                          const poly::Interval& res,
                                          std::size_t size_threshold)
{
  Trace("nl-icp") << cur << " := " << cur << " intersected with " << res
                  << std::endl;

  if (bitsize(get_lower(res)) > size_threshold
      || bitsize(get_upper(res)) > size_threshold)
  {
    Trace("nl-icp") << "Reached bitsize threshold" << std::endl;
    return PropagationResult::NOT_CHANGED;
  }

  // Basic idea to organize this function:
  // The bounds for res have 5 positions:
  // 1 < 2 (lower(cur)) < 3 < 4 (upper(cur)) < 5

  if (get_upper(res) < get_lower(cur))
  {
    // upper(res) at 1
    Trace("nl-icp") << "res < cur -> conflict" << std::endl;
    return PropagationResult::CONFLICT;
  }
  if (get_upper(res) == get_lower(cur))
  {
    // upper(res) at 2
    if (get_upper_open(res) || get_lower_open(cur))
    {
      Trace("nl-icp") << "meet at lower, but one is open -> conflict"
                      << std::endl;
      return PropagationResult::CONFLICT;
    }
    if (!is_point(cur))
    {
      Trace("nl-icp") << "contracts to point interval at lower" << std::endl;
      cur = poly::Interval(get_upper(res));
      return PropagationResult::CONTRACTED;
    }
    return PropagationResult::NOT_CHANGED;
  }
  Assert(get_upper(res) > get_lower(cur))
      << "Comparison operator does weird stuff.";
  if (get_upper(res) < get_upper(cur))
  {
    // upper(res) at 3
    if (get_lower(res) < get_lower(cur))
    {
      // lower(res) at 1
      Trace("nl-icp") << "lower(cur) .. upper(res)" << std::endl;
      cur.set_upper(get_upper(res), get_upper_open(res));
      return PropagationResult::CONTRACTED;
    }
    if (get_lower(res) == get_lower(cur))
    {
      // lower(res) at 2
      Trace("nl-icp") << "meet at lower, lower(cur) .. upper(res)" << std::endl;
      cur = poly::Interval(get_lower(cur),
                           get_lower_open(cur) || get_lower_open(res),
                           get_upper(res),
                           get_upper_open(res));
      if (get_lower_open(cur) && !get_lower_open(res))
      {
        return PropagationResult::CONTRACTED;
      }
      return PropagationResult::CONTRACTED_WITHOUT_CURRENT;
    }
    Assert(get_lower(res) > get_lower(cur))
        << "Comparison operator does weird stuff.";
    // lower(res) at 3
    Trace("nl-icp") << "cur covers res" << std::endl;
    cur = res;
    return PropagationResult::CONTRACTED_WITHOUT_CURRENT;
  }
  if (get_upper(res) == get_upper(cur))
  {
    // upper(res) at 4
    if (get_lower(res) < get_lower(cur))
    {
      // lower(res) at 1
      Trace("nl-icp") << "res covers cur but meet at upper" << std::endl;
      if (get_upper_open(res) && !get_upper_open(cur))
      {
        cur.set_upper(get_upper(cur), true);
        return PropagationResult::CONTRACTED;
      }
      return PropagationResult::NOT_CHANGED;
    }
    if (get_lower(res) == get_lower(cur))
    {
      // lower(res) at 2
      Trace("nl-icp") << "same bounds but check openness" << std::endl;
      bool changed = false;
      if (get_lower_open(res) && !get_lower_open(cur))
      {
        changed = true;
        cur.set_lower(get_lower(cur), true);
      }
      if (get_upper_open(res) && !get_upper_open(cur))
      {
        changed = true;
        cur.set_upper(get_upper(cur), true);
      }
      if (changed)
      {
        if ((get_lower_open(res) || !get_upper_open(cur))
            && (get_upper_open(res) || !get_upper_open(cur)))
        {
          return PropagationResult::CONTRACTED_WITHOUT_CURRENT;
        }
        return PropagationResult::CONTRACTED;
      }
      return PropagationResult::NOT_CHANGED;
    }
    Assert(get_lower(res) > get_lower(cur))
        << "Comparison operator does weird stuff.";
    // lower(res) at 3
    Trace("nl-icp") << "cur covers res but meet at upper" << std::endl;
    cur = poly::Interval(get_lower(res),
                         get_lower_open(res),
                         get_upper(res),
                         get_upper_open(cur) || get_upper_open(res));
    if (get_upper_open(cur) && !get_upper_open(res))
    {
      return PropagationResult::CONTRACTED;
    }
    return PropagationResult::CONTRACTED_WITHOUT_CURRENT;
  }

  Assert(get_upper(res) > get_upper(cur))
      << "Comparison operator does weird stuff.";
  // upper(res) at 5

  if (get_lower(res) < get_lower(cur))
  {
    // lower(res) at 1
    Trace("nl-icp") << "res covers cur" << std::endl;
    return PropagationResult::NOT_CHANGED;
  }
  if (get_lower(res) == get_lower(cur))
  {
    // lower(res) at 2
    Trace("nl-icp") << "res covers cur but meet at lower" << std::endl;
    if (get_lower_open(res) && is_point(cur))
    {
      return PropagationResult::CONFLICT;
    }
    else if (get_lower_open(res) && !get_lower_open(cur))
    {
      cur.set_lower(get_lower(cur), true);
      return PropagationResult::CONTRACTED;
    }
    return PropagationResult::NOT_CHANGED;
  }
  Assert(get_lower(res) > get_lower(cur))
      << "Comparison operator does weird stuff.";
  if (get_lower(res) < get_upper(cur))
  {
    // lower(res) at 3
    Trace("nl-icp") << "lower(res) .. upper(cur)" << std::endl;
    cur.set_lower(get_lower(res), get_lower_open(res));
    return PropagationResult::CONTRACTED;
  }
  if (get_lower(res) == get_upper(cur))
  {
    // lower(res) at 4
    if (get_lower_open(res) || get_upper_open(cur))
    {
      Trace("nl-icp") << "meet at upper, but one is open -> conflict"
                      << std::endl;
      return PropagationResult::CONFLICT;
    }
    if (!is_point(cur))
    {
      Trace("nl-icp") << "contracts to point interval at upper" << std::endl;
      cur = poly::Interval(get_lower(res));
      return PropagationResult::CONTRACTED;
    }
    return PropagationResult::NOT_CHANGED;
  }

  Assert(get_lower(res) > get_upper(cur));
  // lower(res) at 5
  Trace("nl-icp") << "res > cur -> conflict" << std::endl;
  return PropagationResult::CONFLICT;
}

}  // namespace icp
}  // namespace nl
}  // namespace arith
}  // namespace theory
}  // namespace CVC4

#endif
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback