@@ -601,14 +601,14 @@ class Var:
601
601
"""Decision variable of the :class:`~mip.Model`. The creation of
602
602
variables is performed calling the :meth:`~mip.Model.add_var`."""
603
603
604
- __slots__ = ["__model " , "idx " ]
604
+ __slots__ = ["_model " , "_idx " ]
605
605
606
606
def __init__ (self , model : "mip.Model" , idx : int ):
607
- self .__model = model
608
- self .idx = idx
607
+ self ._model = model
608
+ self ._idx = idx
609
609
610
610
def __hash__ (self ) -> int :
611
- return self .idx
611
+ return self ._idx
612
612
613
613
def __add__ (
614
614
self , other : Union ["mip.Var" , LinExpr , numbers .Real ]
@@ -634,44 +634,40 @@ def __sub__(
634
634
) -> Union ["mip.Var" , LinExpr ]:
635
635
if isinstance (other , Var ):
636
636
return LinExpr ([self , other ], [1 , - 1 ])
637
- elif isinstance (other , LinExpr ):
637
+ if isinstance (other , LinExpr ):
638
638
return (- other ).__add__ (self )
639
- elif isinstance (other , numbers .Real ):
639
+ if isinstance (other , numbers .Real ):
640
640
if fabs (other ) < mip .EPS :
641
641
return self
642
642
return LinExpr ([self ], [1 ], - other )
643
- else :
644
- raise TypeError ("type {} not supported" .format (type (other )))
643
+
644
+ raise TypeError ("type {} not supported" .format (type (other )))
645
645
646
646
def __rsub__ (
647
647
self , other : Union ["mip.Var" , LinExpr , numbers .Real ]
648
648
) -> Union ["mip.Var" , LinExpr ]:
649
649
if isinstance (other , Var ):
650
650
return LinExpr ([self , other ], [- 1 , 1 ])
651
- elif isinstance (other , LinExpr ):
651
+ if isinstance (other , LinExpr ):
652
652
return other .__sub__ (self )
653
- elif isinstance (other , numbers .Real ):
653
+ if isinstance (other , numbers .Real ):
654
654
return LinExpr ([self ], [- 1 ], other )
655
- else :
656
- raise TypeError ("type {} not supported" .format (type (other )))
657
655
658
- def __mul__ (self , other : numbers .Real ) -> Union ["mip.Var" , numbers .Real , LinExpr ]:
656
+ raise TypeError ("type {} not supported" .format (type (other )))
657
+
658
+ def __mul__ (self , other : numbers .Real ) -> LinExpr :
659
659
if not isinstance (other , numbers .Real ):
660
660
raise TypeError ("Can not multiply with type {}" .format (type (other )))
661
- if fabs (other ) < mip .EPS :
662
- return other
663
- if fabs (other - 1 ) < mip .EPS :
664
- return self
665
661
return LinExpr ([self ], [other ])
666
662
667
- def __rmul__ (self , other : numbers .Real ) -> Union [ "mip.Var" , numbers . Real , LinExpr ] :
663
+ def __rmul__ (self , other : numbers .Real ) -> LinExpr :
668
664
return self .__mul__ (other )
669
665
670
- def __truediv__ (
671
- self , other : numbers .Real
672
- ) -> Union ["mip.Var" , numbers .Real , LinExpr ]:
666
+ def __truediv__ (self , other : numbers .Real ) -> LinExpr :
673
667
if not isinstance (other , numbers .Real ):
674
668
raise TypeError ("Can not divide with type {}" .format (type (other )))
669
+ if isinstance (other , numbers .Real ) and abs (other ) < mip .EPS :
670
+ raise ZeroDivisionError ("Variable division by zero" )
675
671
return self .__mul__ (1.0 / other )
676
672
677
673
def __neg__ (self ) -> LinExpr :
@@ -680,90 +676,84 @@ def __neg__(self) -> LinExpr:
680
676
def __eq__ (self , other ) -> LinExpr :
681
677
if isinstance (other , Var ):
682
678
return LinExpr ([self , other ], [1 , - 1 ], sense = "=" )
683
- elif isinstance (other , LinExpr ):
684
- return other == self
685
- elif isinstance (other , numbers .Real ):
686
- if other != 0 :
687
- return LinExpr ([self ], [1 ], - 1 * other , sense = "=" )
688
- return LinExpr ([self ], [1 ], sense = "=" )
689
- else :
690
- raise TypeError ("type {} not supported" .format (type (other )))
679
+ if isinstance (other , LinExpr ):
680
+ return LinExpr ([self ], [1 ]) == other
681
+ if isinstance (other , numbers .Real ):
682
+ return LinExpr ([self ], [1 ], - 1 * other , sense = "=" )
683
+
684
+ raise TypeError ("type {} not supported" .format (type (other )))
691
685
692
686
def __le__ (self , other : Union ["mip.Var" , LinExpr , numbers .Real ]) -> LinExpr :
693
687
if isinstance (other , Var ):
694
688
return LinExpr ([self , other ], [1 , - 1 ], sense = "<" )
695
- elif isinstance (other , LinExpr ):
696
- return other >= self
697
- elif isinstance (other , numbers .Real ):
698
- if other != 0 :
699
- return LinExpr ([self ], [1 ], - 1 * other , sense = "<" )
700
- return LinExpr ([self ], [1 ], sense = "<" )
701
- else :
702
- raise TypeError ("type {} not supported" .format (type (other )))
689
+ if isinstance (other , LinExpr ):
690
+ return LinExpr ([self ], [1 ]) <= other
691
+ if isinstance (other , numbers .Real ):
692
+ return LinExpr ([self ], [1 ], - 1 * other , sense = "<" )
693
+
694
+ raise TypeError ("type {} not supported" .format (type (other )))
703
695
704
696
def __ge__ (self , other : Union ["mip.Var" , LinExpr , numbers .Real ]) -> LinExpr :
705
697
if isinstance (other , Var ):
706
698
return LinExpr ([self , other ], [1 , - 1 ], sense = ">" )
707
- elif isinstance (other , LinExpr ):
708
- return other <= self
709
- elif isinstance (other , numbers .Real ):
710
- if other != 0 :
711
- return LinExpr ([self ], [1 ], - 1 * other , sense = ">" )
712
- return LinExpr ([self ], [1 ], sense = ">" )
713
- else :
714
- raise TypeError ("type {} not supported" .format (type (other )))
699
+ if isinstance (other , LinExpr ):
700
+ return LinExpr ([self ], [1 ]) >= other
701
+ if isinstance (other , numbers .Real ):
702
+ return LinExpr ([self ], [1 ], - 1 * other , sense = ">" )
703
+
704
+ raise TypeError ("type {} not supported" .format (type (other )))
715
705
716
706
@property
717
707
def name (self ) -> str :
718
708
"""Variable name."""
719
- return self .__model .solver .var_get_name (self .idx )
709
+ return self ._model .solver .var_get_name (self .idx )
720
710
721
711
def __str__ (self ) -> str :
722
712
return self .name
723
713
724
714
@property
725
715
def lb (self ) -> numbers .Real :
726
716
"""Variable lower bound."""
727
- return self .__model .solver .var_get_lb (self )
717
+ return self ._model .solver .var_get_lb (self )
728
718
729
719
@lb .setter
730
720
def lb (self , value : numbers .Real ):
731
- self .__model .solver .var_set_lb (self , value )
721
+ self ._model .solver .var_set_lb (self , value )
732
722
733
723
@property
734
724
def ub (self ) -> numbers .Real :
735
725
"""Variable upper bound."""
736
- return self .__model .solver .var_get_ub (self )
726
+ return self ._model .solver .var_get_ub (self )
737
727
738
728
@ub .setter
739
729
def ub (self , value : numbers .Real ):
740
- self .__model .solver .var_set_ub (self , value )
730
+ self ._model .solver .var_set_ub (self , value )
741
731
742
732
@property
743
733
def obj (self ) -> numbers .Real :
744
734
"""Coefficient of variable in the objective function."""
745
- return self .__model .solver .var_get_obj (self )
735
+ return self ._model .solver .var_get_obj (self )
746
736
747
737
@obj .setter
748
738
def obj (self , value : numbers .Real ):
749
- self .__model .solver .var_set_obj (self , value )
739
+ self ._model .solver .var_set_obj (self , value )
750
740
751
741
@property
752
742
def branch_priority (self ) -> numbers .Real :
753
743
"""
754
744
Variable's branching priority in the branch and bound process.
755
745
Note: variables with higher priority are selected first. Default value is zero.
756
746
"""
757
- return self .__model .solver .var_get_branch_priority (self )
747
+ return self ._model .solver .var_get_branch_priority (self )
758
748
759
749
@branch_priority .setter
760
750
def branch_priority (self , value : numbers .Real ):
761
- self .__model .solver .var_set_branch_priority (self , value )
751
+ self ._model .solver .var_set_branch_priority (self , value )
762
752
763
753
@property
764
754
def var_type (self ) -> str :
765
755
"""Variable type, ('B') BINARY, ('C') CONTINUOUS and ('I') INTEGER."""
766
- return self .__model .solver .var_get_var_type (self )
756
+ return self ._model .solver .var_get_var_type (self )
767
757
768
758
@var_type .setter
769
759
def var_type (self , value : str ):
@@ -773,42 +763,42 @@ def var_type(self, value: str):
773
763
(mip .BINARY , mip .CONTINUOUS , mip .INTEGER ), value
774
764
)
775
765
)
776
- self .__model .solver .var_set_var_type (self , value )
766
+ self ._model .solver .var_set_var_type (self , value )
777
767
778
768
@property
779
769
def column (self ) -> Column :
780
770
"""Variable coefficients in constraints.
781
771
782
772
:rtype: mip.Column
783
773
"""
784
- return self .__model .solver .var_get_column (self )
774
+ return self ._model .solver .var_get_column (self )
785
775
786
776
@column .setter
787
777
def column (self , value : Column ):
788
- self .__model .solver .var_set_column (self , value )
778
+ self ._model .solver .var_set_column (self , value )
789
779
790
780
@property
791
781
def rc (self ) -> Optional [numbers .Real ]:
792
782
"""Reduced cost, only available after a linear programming model (only
793
783
continuous variables) is optimized. Note that None is returned if no
794
784
optimum solution is available"""
795
785
796
- return self .__model .solver .var_get_rc (self )
786
+ return self ._model .solver .var_get_rc (self )
797
787
798
788
@property
799
789
def x (self ) -> Optional [numbers .Real ]:
800
790
"""Value of this variable in the solution. Note that None is returned
801
791
if no solution is not available."""
802
- return self .__model .solver .var_get_x (self )
792
+ return self ._model .solver .var_get_x (self )
803
793
804
794
def xi (self , i : int ) -> Optional [numbers .Real ]:
805
795
"""Value for this variable in the :math:`i`-th solution from the solution
806
796
pool. Note that None is returned if the solution is not available."""
807
- if self .__model .status in [
797
+ if self ._model .status in [
808
798
mip .OptimizationStatus .OPTIMAL ,
809
799
mip .OptimizationStatus .FEASIBLE ,
810
800
]:
811
- return self .__model .solver .var_get_xi (self , i )
801
+ return self ._model .solver .var_get_xi (self , i )
812
802
return None
813
803
814
804
def __float__ (self ):
@@ -821,7 +811,15 @@ def model(self) -> "mip.Model":
821
811
822
812
:rtype: mip.Model
823
813
"""
824
- return self .__model
814
+ return self ._model
815
+
816
+ @property
817
+ def idx (self ) -> int :
818
+ """Internal index of the variable to the model.
819
+
820
+ :rtype: int
821
+ """
822
+ return self ._idx
825
823
826
824
827
825
class ConflictGraph :
0 commit comments