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
|
from __future__ import print_function, unicode_literals, absolute_import, division
from .error import InternalError
from .utils import approx_equal, REQUIRED, STRONG, repr_strength
###########################################################################
# Variables
#
# Variables are the atomic unit of linear programming, describing the
# quantities that are to be solved and constrained.
###########################################################################
class AbstractVariable(object):
def __init__(self, name):
self.name = name
self.is_dummy = False
self.is_external = False
self.is_pivotable = False
self.is_restricted = False
def __rmul__(self, x):
return self.__mul__(x)
def __mul__(self, x):
if isinstance(x, (float, int)):
return Expression(self, x)
elif isinstance(x, Expression):
if x.is_constant:
return Expression(self, value=x.constant)
else:
raise TypeError('Cannot multiply variable by non-constant expression')
else:
raise TypeError('Cannot multiply variable by object of type %s' % type(x))
def __truediv__(self, x):
return self.__div__(x)
def __div__(self, x):
if isinstance(x, (float, int)):
if approx_equal(x, 0):
raise ZeroDivisionError()
return Expression(self, 1.0 / x)
elif isinstance(x, Expression):
if x.is_constant:
return Expression(self, value=1.0/x.constant)
else:
raise TypeError('Cannot add non-constant expression to variable')
else:
raise TypeError('Cannot divide variable by object of type %s' % type(x))
def __radd__(self, x):
return self.__add__(x)
def __add__(self, x):
if isinstance(x, (int, float)):
return Expression(self, constant=x)
elif isinstance(x, Expression):
return Expression(self) + x
elif isinstance(x, AbstractVariable):
return Expression(self) + Expression(x)
else:
raise TypeError('Cannot add object of type %s to expression' % type(x))
def __rsub__(self, x):
if isinstance(x, (int, float)):
return Expression(self, -1.0, constant=x)
elif isinstance(x, Expression):
return x - Expression(self)
elif isinstance(x, AbstractVariable):
return Expression(x) - Expression(self)
else:
raise TypeError('Cannot subtract variable from object of type %s' % type(x))
def __sub__(self, x):
if isinstance(x, (int, float)):
return Expression(self, constant=-x)
elif isinstance(x, Expression):
return Expression(self) - x
elif isinstance(x, AbstractVariable):
return Expression(self) - Expression(x)
else:
raise TypeError('Cannot subtract object of type %s from variable' % type(x))
class Variable(AbstractVariable):
def __init__(self, name, value=0.0):
super(Variable, self).__init__(name)
self.value = float(value)
self.is_external = True
def __repr__(self):
return '%s[%s]' % (self.name, self.value)
__hash__ = object.__hash__
def __eq__(self, other):
if isinstance(other, (Expression, Variable, float, int)):
return Constraint(self, Constraint.EQ, other)
else:
raise TypeError('Cannot compare variable with object of type %s' % type(other))
def __lt__(self, other):
# < and <= are equivalent in the API; it's effectively true
# due to float arithmetic, and it makes the API a little less hostile,
# because all the comparison operators exist.
return self.__le__(other)
def __le__(self, other):
if isinstance(other, (Expression, Variable, float, int)):
return Constraint(self, Constraint.LEQ, other)
else:
raise TypeError('Cannot compare variable with object of type %s' % type(other))
def __gt__(self, other):
# > and >= are equivalent in the API; it's effectively true
# due to float arithmetic, and it makes the API a little less hostile,
# because all the comparison operators exist.
return self.__ge__(other)
def __ge__(self, other):
if isinstance(other, (Expression, Variable, float, int)):
return Constraint(self, Constraint.GEQ, other)
else:
raise TypeError('Cannot compare variable with object of type %s' % type(other))
class DummyVariable(AbstractVariable):
def __init__(self, number):
super(DummyVariable, self).__init__(name='d%s' % (number))
self.is_dummy = True
self.is_restricted = True
def __repr__(self):
return '%s:dummy' % self.name
class ObjectiveVariable(AbstractVariable):
def __init__(self, name):
super(ObjectiveVariable, self).__init__(name)
def __repr__(self):
return '%s:obj' % self.name
class SlackVariable(AbstractVariable):
def __init__(self, prefix, number):
super(SlackVariable, self).__init__(name='%s%s' % (prefix, number))
self.is_pivotable = True
self.is_restricted = True
def __repr__(self):
return '%s:slack' % self.name
###########################################################################
# Expressions
#
# Expressions are combinations of variables with multipliers and constants
###########################################################################
class Expression(object):
def __init__(self, variable=None, value=1.0, constant=0.0):
assert isinstance(constant, (float, int))
assert variable is None or isinstance(variable, AbstractVariable)
self.constant = float(constant)
self.terms = {}
if variable:
self.set_variable(variable, float(value))
def __repr__(self):
parts = []
if not approx_equal(self.constant, 0.0) or self.is_constant:
parts.append(repr(self.constant))
for clv, coeff in sorted(self.terms.items(), key=lambda x:repr(x)):
if approx_equal(coeff, 1.0):
parts.append(repr(clv))
else:
parts.append(repr(coeff) + "*" + repr(clv))
return ' + '.join(parts)
@property
def is_constant(self):
return not self.terms
def clone(self):
expr = Expression(constant=self.constant)
for clv, value in self.terms.items():
expr.set_variable(clv, value)
return expr
######################################################################
# Mathematical operators
######################################################################
def __rmul__(self, x):
return self.__mul__(x)
def __mul__(self, x):
if isinstance(x, Expression):
if self.is_constant:
result = x * self.constant
elif x.is_constant:
result = self * x.constant
else:
raise TypeError('Cannot multiply expression by non-constant')
elif isinstance(x, Variable):
if self.is_constant:
result = Expression(x, self.constant)
else:
raise TypeError('Cannot multiply a variable by a non-constant expression')
elif isinstance(x, (float, int)):
result = Expression(constant=self.constant * x)
for clv, value in self.terms.items():
result.set_variable(clv, value * x)
else:
raise TypeError('Cannot multiply expression by object of type %s' % type(x))
return result
def __truediv__(self, x):
return self.__div__(x)
def __div__(self, x):
if isinstance(x, (float, int)):
if approx_equal(x, 0):
raise ZeroDivisionError()
result = Expression(constant=self.constant / x)
for clv, value in self.terms.items():
result.set_variable(clv, value / x)
else:
if x.is_constant:
result = self / x.constant
else:
raise TypeError('Cannot divide expression by non-constant')
return result
def __radd__(self, x):
return self.__add__(x)
def __add__(self, x):
if isinstance(x, Expression):
result = self.clone()
result.add_expression(x, 1.0)
return result
elif isinstance(x, Variable):
result = self.clone()
result.add_variable(x, 1.0)
return result
elif isinstance(x, (int, float)):
result = self.clone()
result.add_expression(Expression(constant=x), 1.0)
return result
else:
raise TypeError('Cannot add object of type %s to expression' % type(x))
def __rsub__(self, x):
if isinstance(x, Expression):
result = self.clone()
result.multiply(-1.0)
result.add_expression(x, 1.0)
return result
elif isinstance(x, Variable):
result = self.clone()
result.multiply(-1.0)
result.add_variable(x, 1.0)
return result
elif isinstance(x, (int, float)):
result = self.clone()
result.multiply(-1.0)
result.add_expression(Expression(constant=x), 1.0)
return result
else:
raise TypeError('Cannot subtract object of type %s from expression' % type(x))
def __sub__(self, x):
if isinstance(x, Expression):
result = self.clone()
result.add_expression(x, -1.0)
return result
elif isinstance(x, Variable):
result = self.clone()
result.add_variable(x, -1.0)
return result
elif isinstance(x, (int, float)):
result = self.clone()
result.add_expression(Expression(constant=x), -1.0)
return result
else:
raise TypeError('Cannot subtract object of type %s from expression' % type(x))
######################################################################
# Mathematical operators
######################################################################
__hash__ = object.__hash__
def __eq__(self, other):
if isinstance(other, (Expression, Variable, float, int)):
return Constraint(self, Constraint.EQ, other)
else:
raise TypeError('Cannot compare expression with object of type %s' % type(other))
def __lt__(self, other):
# < and <= are equivalent in the API; it's effectively true
# due to float arithmetic, and it makes the API a little less hostile,
# because all the comparison operators exist.
return self.__le__(other)
def __le__(self, other):
if isinstance(other, (Expression, Variable, float, int)):
return Constraint(self, Constraint.LEQ, other)
else:
raise TypeError('Cannot compare expression with object of type %s' % type(other))
def __gt__(self, other):
# > and >= are equivalent in the API; it's effectively true
# due to float arithmetic, and it makes the API a little less hostile,
# because all the comparison operators exist.
return self.__ge__(other)
def __ge__(self, other):
if isinstance(other, (Expression, Variable, float, int)):
return Constraint(self, Constraint.GEQ, other)
else:
raise TypeError('Cannot compare expression with object of type %s' % type(other))
######################################################################
# Internal mechanisms
######################################################################
def add_expression(self, expr, n=1.0, subject=None, solver=None):
if isinstance(expr, AbstractVariable):
expr = Expression(variable=expr)
self.constant = self.constant + n * expr.constant
for clv, coeff in expr.terms.items():
self.add_variable(clv, coeff * n, subject, solver)
def add_variable(self, v, cd=1.0, subject=None, solver=None):
# print 'expression: add_variable', v, cd
coeff = self.terms.get(v)
if coeff:
new_coefficient = coeff + cd
if approx_equal(new_coefficient, 0.0):
if solver:
solver.note_removed_variable(v, subject)
self.remove_variable(v)
else:
self.set_variable(v, new_coefficient)
else:
if not approx_equal(cd, 0.0):
self.set_variable(v, cd)
if solver:
solver.note_added_variable(v, subject)
def set_variable(self, v, c):
self.terms[v] = float(c)
def remove_variable(self, v):
del self.terms[v]
def any_pivotable_variable(self):
if self.is_constant:
raise InternalError('any_pivotable_variable called on a constant')
retval = None
for clv, c in self.terms.items():
if clv.is_pivotable:
retval = clv
break
return retval
def substitute_out(self, outvar, expr, subject=None, solver=None):
multiplier = self.terms.pop(outvar)
self.constant = self.constant + multiplier * expr.constant
for clv, coeff in expr.terms.items():
old_coefficient = self.terms.get(clv)
if old_coefficient:
new_coefficient = old_coefficient + multiplier * coeff
if approx_equal(new_coefficient, 0):
solver.note_removed_variable(clv, subject)
del self.terms[clv]
else:
self.set_variable(clv, new_coefficient)
else:
self.set_variable(clv, multiplier * coeff)
if solver:
solver.note_added_variable(clv, subject)
def change_subject(self, old_subject, new_subject):
self.set_variable(old_subject, self.new_subject(new_subject))
def multiply(self, x):
self.constant = self.constant * float(x)
for clv, value in self.terms.items():
self.set_variable(clv, value * x)
def new_subject(self, subject):
# print "new_subject", subject
value = self.terms.pop(subject)
reciprocal = 1.0 / value
self.multiply(-reciprocal)
return reciprocal
def coefficient_for(self, clv):
return self.terms.get(clv, 0.0)
###########################################################################
# Constraint
#
# Constraints are the restrictions on linear programming; an equality or
# inequality between two expressions.
###########################################################################
class AbstractConstraint(object):
def __init__(self, strength, weight=1.0):
self.strength = strength
self.weight = weight
self.is_edit_constraint = False
self.is_inequality = False
self.is_stay_constraint = False
@property
def is_required(self):
return self.strength == REQUIRED
def __repr__(self):
return '%s:{%s}(%s)' % (repr_strength(self.strength), self.weight, self.expression)
class EditConstraint(AbstractConstraint):
def __init__(self, variable, strength=STRONG, weight=1.0):
super(EditConstraint, self).__init__(strength, weight)
self.variable = variable
self.expression = Expression(variable, -1.0, variable.value)
self.is_edit_constraint = True
def __repr__(self):
return 'edit:%s' % super(EditConstraint, self).__repr__()
class StayConstraint(AbstractConstraint):
def __init__(self, variable, strength=STRONG, weight=1.0):
super(StayConstraint, self).__init__(strength, weight)
self.variable = variable
self.expression = Expression(variable, -1.0, variable.value)
self.is_stay_constraint=True
def __repr__(self):
return 'stay:%s' % super(StayConstraint, self).__repr__()
class Constraint(AbstractConstraint):
LEQ = -1
EQ = 0
GEQ = 1
def __init__(self, param1, operator=EQ, param2=None, strength=REQUIRED, weight=1.0):
"""Define a new linear constraint.
param1 may be an expression or variable
param2 may be an expression, variable, or constant, or may be ommitted entirely.
If param2 is specified, the operator must be either LEQ, EQ, or GEQ
"""
if isinstance(param1, Expression):
if param2 is None:
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = param1
elif isinstance(param2, Expression):
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = param1.clone()
if operator == self.LEQ:
self.expression.multiply(-1.0)
self.expression.add_expression(param2, 1.0)
elif operator == self.EQ:
self.expression.add_expression(param2, -1.0)
elif operator == self.GEQ:
self.expression.add_expression(param2, -1.0)
else:
raise InternalError("Invalid operator in Constraint constructor")
elif isinstance(param2, Variable):
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = param1.clone()
if operator == self.LEQ:
self.expression.multiply(-1.0)
self.expression.add_variable(param2, 1.0)
elif operator == self.EQ:
self.expression.add_variable(param2, -1.0)
elif operator == self.GEQ:
self.expression.add_variable(param2, -1.0)
else:
raise InternalError("Invalid operator in Constraint constructor")
elif isinstance(param2, (float, int)):
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = param1.clone()
if operator == self.LEQ:
self.expression.multiply(-1.0)
self.expression.add_expression(Expression(constant=param2), 1.0)
elif operator == self.EQ:
self.expression.add_expression(Expression(constant=param2), -1.0)
elif operator == self.GEQ:
self.expression.add_expression(Expression(constant=param2), -1.0)
else:
raise InternalError("Invalid operator in Constraint constructor")
else:
raise InternalError("Invalid parameters to Constraint constructor")
elif isinstance(param1, Variable):
if param2 is None:
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = Expression(param1)
elif isinstance(param2, Expression):
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = param2.clone()
if operator == self.LEQ:
self.expression.add_variable(param1, -1.0)
elif operator == self.EQ:
self.expression.add_variable(param1, -1.0)
elif operator == self.GEQ:
self.expression.multiply(-1.0)
self.expression.add_variable(param1, 1.0)
else:
raise InternalError("Invalid operator in Constraint constructor")
elif isinstance(param2, Variable):
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = Expression(param2)
if operator == self.LEQ:
self.expression.add_variable(param1, -1.0)
elif operator == self.EQ:
self.expression.add_variable(param1, -1.0)
elif operator == self.GEQ:
self.expression.multiply(-1.0)
self.expression.add_variable(param1, 1.0)
else:
raise InternalError("Invalid operator in Constraint constructor")
elif isinstance(param2, (float, int)):
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = Expression(constant=param2)
if operator == self.LEQ:
self.expression.add_variable(param1, -1.0)
elif operator == self.EQ:
self.expression.add_variable(param1, -1.0)
elif operator == self.GEQ:
self.expression.multiply(-1.0)
self.expression.add_variable(param1, 1.0)
else:
raise InternalError("Invalid operator in Constraint constructor")
else:
raise InternalError("Invalid parameters to Constraint constructor")
elif isinstance(param1, (float, int)):
if param2 is None:
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = Expression(constant=param1)
elif isinstance(param2, Expression):
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = param2.clone()
if operator == self.LEQ:
self.expression.add_expression(Expression(constant=param1), -1.0)
elif operator == self.EQ:
self.expression.add_expression(Expression(constant=param1), -1.0)
elif operator == self.GEQ:
self.expression.multiply(-1.0)
self.expression.add_expression(Expression(constant=param1), 1.0)
else:
raise InternalError("Invalid operator in Constraint constructor")
elif isinstance(param2, Variable):
super(Constraint, self).__init__(strength=strength, weight=weight)
self.expression = Expression(constant=param1)
if operator == self.LEQ:
self.expression.add_variable(param2, -1.0)
elif operator == self.EQ:
self.expression.add_variable(param2, -1.0)
elif operator == self.GEQ:
self.expression.multiply(-1.0)
self.expression.add_variable(param2, 1.0)
else:
raise InternalError("Invalid operator in Constraint constructor")
elif isinstance(param2, (float, int)):
raise InternalError("Cannot create an inequality between constants")
else:
raise InternalError("Invalid parameters to Constraint constructor")
else:
raise InternalError("Invalid parameters to Constraint constructor")
self.is_inequality = operator != self.EQ
def clone(self):
c = Constraint(self.expression, strength=self.strength, weight=self.weight)
c.is_inequality = self.is_inequality
return c
|