@@ -65,6 +65,9 @@ class LinExpr:
65
65
a = 10*x1 + 7*x4
66
66
print(a.x)
67
67
68
+ .. warning::
69
+ Do not pass identical objects in the ``variables`` argument when constructing
70
+ a LinExpr manually.
68
71
"""
69
72
70
73
__slots__ = ["__const" , "__expr" , "__sense" ]
@@ -542,6 +545,8 @@ def __add__(
542
545
self , other : Union ["mip.Var" , LinExpr , numbers .Real ]
543
546
) -> Union ["mip.Var" , LinExpr ]:
544
547
if isinstance (other , Var ):
548
+ if id (self ) == id (other ):
549
+ return LinExpr ([self ], [2 ])
545
550
return LinExpr ([self , other ], [1 , 1 ])
546
551
if isinstance (other , LinExpr ):
547
552
return other .__add__ (self )
@@ -561,6 +566,8 @@ def __sub__(
561
566
self , other : Union ["mip.Var" , LinExpr , numbers .Real ]
562
567
) -> Union ["mip.Var" , LinExpr ]:
563
568
if isinstance (other , Var ):
569
+ if id (self ) == id (other ):
570
+ return LinExpr ([self ], [0 ])
564
571
return LinExpr ([self , other ], [1 , - 1 ])
565
572
if isinstance (other , LinExpr ):
566
573
return (- other ).__add__ (self )
@@ -575,6 +582,8 @@ def __rsub__(
575
582
self , other : Union ["mip.Var" , LinExpr , numbers .Real ]
576
583
) -> Union ["mip.Var" , LinExpr ]:
577
584
if isinstance (other , Var ):
585
+ if id (self ) == id (other ):
586
+ return LinExpr ([self ], [0 ])
578
587
return LinExpr ([self , other ], [- 1 , 1 ])
579
588
if isinstance (other , LinExpr ):
580
589
return other .__sub__ (self )
@@ -603,6 +612,8 @@ def __neg__(self) -> LinExpr:
603
612
604
613
def __eq__ (self , other ) -> LinExpr :
605
614
if isinstance (other , Var ):
615
+ if id (self ) == id (other ):
616
+ return LinExpr ([self ], [0 ], sense = "=" )
606
617
return LinExpr ([self , other ], [1 , - 1 ], sense = "=" )
607
618
if isinstance (other , LinExpr ):
608
619
return LinExpr ([self ], [1 ]) == other
@@ -613,6 +624,8 @@ def __eq__(self, other) -> LinExpr:
613
624
614
625
def __le__ (self , other : Union ["mip.Var" , LinExpr , numbers .Real ]) -> LinExpr :
615
626
if isinstance (other , Var ):
627
+ if id (self ) == id (other ):
628
+ return LinExpr ([self ], [0 ], sense = "<" )
616
629
return LinExpr ([self , other ], [1 , - 1 ], sense = "<" )
617
630
if isinstance (other , LinExpr ):
618
631
return LinExpr ([self ], [1 ]) <= other
@@ -623,6 +636,8 @@ def __le__(self, other: Union["mip.Var", LinExpr, numbers.Real]) -> LinExpr:
623
636
624
637
def __ge__ (self , other : Union ["mip.Var" , LinExpr , numbers .Real ]) -> LinExpr :
625
638
if isinstance (other , Var ):
639
+ if id (self ) == id (other ):
640
+ return LinExpr ([self ], [0 ], sense = ">" )
626
641
return LinExpr ([self , other ], [1 , - 1 ], sense = ">" )
627
642
if isinstance (other , LinExpr ):
628
643
return LinExpr ([self ], [1 ]) >= other
0 commit comments