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
|
# pylint: disable-msg=W0142
# copyright 2002-2021 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of logilab-constraint.
#
# logilab-constraint is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 2.1 of the License, or (at your
# option) any later version.
#
# logilab-constraint is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with logilab-constraint. If not, see <http://www.gnu.org/licenses/>.
"""Tools to work with finite interval domain interval and constraints
"""
from logilab.constraint.distributors import AbstractDistributor
from logilab.constraint.propagation import (
AbstractDomain,
ConsistencyFailure,
AbstractConstraint,
)
class Interval:
"""representation of an interval
This class is used to give back results from a FiniteIntervalDomain
"""
def __init__(self, start, end):
self._start = start
self._end = end
def __repr__(self):
return f"<Interval [{self._start:.2f} {self._end:.2f}[>"
def __eq__(self, other):
return self._start == other._start and self._end == other._end
def __hash__(self):
return hash((self.__class__, self._start, self._end))
class FiniteIntervalDomain(AbstractDomain):
"""
Domain for a variable with interval values.
"""
def __init__(
self, lowestMin, highestMax, min_length, max_length=None, resolution=1
):
"""
lowestMin is the lowest value of a low boundary for a variable (inclusive).
highestMax is the highest value of a high boundary for a variable (exclusive).
min_length is the minimum width of the interval.
max_length is the maximum width of the interval.
Use None to have max = min.
resolution is the precision to use for constraint satisfaction. Defaults to 1
"""
assert highestMax >= lowestMin
if max_length is None:
max_length = min_length
assert 0 <= min_length <= max_length
assert min_length <= highestMax - lowestMin
assert resolution > 0
AbstractDomain.__init__(self)
self.lowestMin = lowestMin
self.highestMax = highestMax
self._min_length = min_length
max_length = min(max_length, highestMax - lowestMin)
self._max_length = max_length
self._resolution = resolution
def __eq__(self, other):
return (
self.lowestMin == other.lowestMin
and self.highestMax == other.highestMax
and self._min_length == other._min_length
and self._max_length == other._max_length
and self._resolution == other._resolution
)
def __hash__(self):
return hash(
(
self.__class__,
self.lowestMin,
self.highestMax,
self._min_length,
self._max_length,
self._resolution,
)
)
def getValues(self):
return list(self.iter_values())
def iter_values(self):
length = self._min_length
while length <= self._max_length:
start = self.lowestMin
while start + length <= self.highestMax:
yield Interval(start, start + length)
start += self._resolution
length += self._resolution
def size(self):
"""computes the size of a finite interval"""
size = 0
length = self._min_length
while length <= self._max_length:
size += ((self.highestMax - length) - self.lowestMin) / self._resolution + 1
length += self._resolution
return size
def _highestMin(self):
return self.highestMax - self._min_length
def _lowestMax(self):
return self.lowestMin + self._min_length
lowestMax = property(_lowestMax, None, None, "")
highestMin = property(_highestMin, None, None, "")
def copy(self):
"""clone the domain"""
return FiniteIntervalDomain(
self.lowestMin,
self.highestMax,
self._min_length,
self._max_length,
self._resolution,
)
def setLowestMin(self, new_lowestMin):
self.lowestMin = new_lowestMin
self._valueRemoved()
def setHighestMax(self, new_highestMax):
self.highestMax = new_highestMax
self._valueRemoved()
def setMinLength(self, new_min):
self._min_length = new_min
self._valueRemoved()
def setMaxLength(self, new_max):
self._max_length = new_max
self._valueRemoved()
def overlap(self, other):
return other.highestMax > self.lowestMin and other.lowestMin < self.highestMax
def no_overlap_impossible(self, other):
return self.lowestMax > other.highestMin and other.lowestMax > self.highestMin
def hasSingleLength(self):
return self._max_length == self._min_length
def _valueRemoved(self):
if self.lowestMin >= self.highestMax:
raise ConsistencyFailure(
"earliest start [%.2f] higher than latest end [%.2f]"
% (self.lowestMin, self.highestMax)
)
if self._min_length > self._max_length:
raise ConsistencyFailure(
"min length [%.2f] greater than max length [%.2f]"
% (self._min_length, self._max_length)
)
self._max_length = min(self._max_length, self.highestMax - self.lowestMin)
AbstractDomain._valueRemoved(self)
def __repr__(self):
return "<FiniteIntervalDomain % 3d from [%.2f, %.2f[ to [%.2f, %.2f[>" % (
self.size(),
self.lowestMin,
self.lowestMax,
self.highestMin,
self.highestMax,
)
##
# Distributors
##
class FiniteIntervalDistributor(AbstractDistributor):
"""Distributes a set of FiniteIntervalDomain
The distribution strategy is the following:
- the smallest domain of size > 1 is picked
- if its max_length is greater than its min_length, a subdomain if size
min_length is distributed, with the same boundaries
- otherwise, a subdomain [lowestMin, lowestMax[ is distributed
"""
def __init__(self):
AbstractDistributor.__init__(self)
def _split_values(self, copy1, copy2):
lm = copy1.lowestMin
hM = copy1.highestMax
if copy1.hasSingleLength():
r = copy1._resolution
L = copy1._min_length
m = (hM - L + lm) // (2 * r) * r
# copy1.highestMax = copy1.lowestMin + copy1._min_length
# copy2.lowestMin += copy2._resolution
copy1.highestMax = m + L
copy2.lowestMin = m + r
else:
copy1._max_length = copy1._min_length
copy2._min_length += copy2._resolution
def _distribute(self, dom1, dom2):
variable = self.findSmallestDomain(dom1)
if self.verbose:
print("Distributing domain for variable", variable)
splitted = dom1[variable]
cpy1 = splitted.copy()
cpy2 = splitted.copy()
self._split_values(cpy1, cpy2)
dom1[variable] = cpy1
dom2[variable] = cpy2
return cpy1, cpy2
##
# Constraints
##
class AbstractFIConstraint(AbstractConstraint):
def __init__(self, var1, var2):
AbstractConstraint.__init__(self, (var1, var2))
def estimateCost(self, domains):
return 1
def __repr__(self):
return f"<{self.__class__.__name__} {str(self._variables)}>"
def __eq__(self, other):
return self.__class__ is other.__class__ and tuple(
sorted(self._variables)
) == tuple(sorted(other._variables))
def __hash__(self):
# FIXME: to be able to add constraints in Sets (and compare them)
# FIXME: improve implementation
variables = tuple(sorted(self._variables))
return hash((self.__class__.__name__, variables))
def narrow(self, domains):
"""narrowing algorithm for the constraint"""
dom1 = domains[self._variables[0]]
dom2 = domains[self._variables[1]]
return self._doNarrow(dom1, dom2)
def _doNarrow(self, dom1, dom2):
"""virtual method which does the real work"""
raise NotImplementedError
# FIXME: deal with more than 2 domains at once ?
class NoOverlap(AbstractFIConstraint):
def _doNarrow(self, dom1, dom2):
if not dom1.overlap(dom2):
return 1
elif dom1.no_overlap_impossible(dom2):
raise ConsistencyFailure
elif dom1.lowestMax == dom2.highestMin and dom2.lowestMax > dom1.highestMin:
dom1.setHighestMax(dom2.highestMin)
dom2.setLowestMin(dom1.lowestMax)
return 1
elif dom1.lowestMax > dom2.highestMin and dom2.lowestMax == dom1.highestMin:
dom2.setHighestMax(dom1.highestMin)
dom1.setLowestMin(dom2.lowestMax)
return 1
return 0
class StartsBeforeStart(AbstractFIConstraint):
def _doNarrow(self, dom1, dom2):
if dom1.lowestMin > dom2.highestMin:
raise ConsistencyFailure
if dom1.highestMin < dom2.lowestMin:
return 1
return 0
class StartsBeforeEnd(AbstractFIConstraint):
def _doNarrow(self, dom1, dom2):
if dom1.lowestMin > dom2.highestMax:
raise ConsistencyFailure
if dom1.highestMin < dom2.lowestMax:
return 1
return 0
class EndsBeforeStart(AbstractFIConstraint):
def _doNarrow(self, dom1, dom2):
if dom1.lowestMax > dom2.highestMin:
raise ConsistencyFailure
if dom1.highestMax < dom2.lowestMin:
return 1
if dom1.highestMax > dom2.highestMin:
dom1.setHighestMax(dom2.highestMin)
return 0
class EndsBeforeEnd(AbstractFIConstraint):
def _doNarrow(self, dom1, dom2):
if dom1.lowestMax > dom2.highestMax:
raise ConsistencyFailure
if dom1.highestMax < dom2.lowestMax:
return 1
if dom1.highestMax > dom2.highestMax:
dom1.setHighestMax(dom2.highestMax)
return 0
class StartsAfterStart(AbstractFIConstraint):
def _doNarrow(self, dom1, dom2):
if dom1.highestMin < dom2.lowestMin:
raise ConsistencyFailure
if dom1.lowestMin > dom2.highestMin:
return 1
if dom1.lowestMin < dom2.lowestMin:
dom1.setLowestMin(dom2.lowestMin)
return 0
class StartsAfterEnd(AbstractFIConstraint):
def _doNarrow(self, dom1, dom2):
if dom1.highestMin < dom2.lowestMax:
raise ConsistencyFailure
if dom1.lowestMin > dom2.highestMax:
return 1
if dom1.lowestMin < dom2.lowestMax:
dom1.setLowestMin(dom2.lowestMax)
if dom2.highestMax > dom1.highestMin:
dom2.setHighestMax(dom1.highestMin)
return 0
class EndsAfterStart(AbstractFIConstraint):
def _doNarrow(self, dom1, dom2):
if dom1.highestMax < dom2.lowestMin:
raise ConsistencyFailure
if dom1.lowestMax > dom2.highestMin:
return 1
return 0
class EndsAfterEnd(AbstractFIConstraint):
def _doNarrow(self, dom1, dom2):
if dom1.highestMax < dom2.lowestMax:
raise ConsistencyFailure
if dom1.lowestMax > dom2.highestMax:
return 1
return 0
|