56
56
Dict ,
57
57
Iterable ,
58
58
List ,
59
+ NoReturn ,
59
60
Optional ,
60
61
Sequence ,
61
62
Tuple ,
80
81
# usual arithmetic operators + - * / and with constant numbers, which makes the
81
82
# python API very intuitive. See../ samples/*.py for examples.
82
83
83
- INT_MIN = - 9223372036854775808 # hardcoded to be platform independent.
84
- INT_MAX = 9223372036854775807
85
- INT32_MAX = 2147483647
86
- INT32_MIN = - 2147483648
84
+ INT_MIN = - ( 2 ** 63 ) # hardcoded to be platform independent.
85
+ INT_MAX = 2 ** 63 - 1
86
+ INT32_MIN = - ( 2 ** 31 )
87
+ INT32_MAX = 2 ** 31 - 1
87
88
88
89
# CpSolver status (exported to avoid importing cp_model_cp2).
89
- UNKNOWN : cp_model_pb2 . CpSolverStatus = cp_model_pb2 .UNKNOWN
90
- MODEL_INVALID : cp_model_pb2 . CpSolverStatus = cp_model_pb2 .MODEL_INVALID
91
- FEASIBLE : cp_model_pb2 . CpSolverStatus = cp_model_pb2 .FEASIBLE
92
- INFEASIBLE : cp_model_pb2 . CpSolverStatus = cp_model_pb2 .INFEASIBLE
93
- OPTIMAL : cp_model_pb2 . CpSolverStatus = cp_model_pb2 .OPTIMAL
90
+ UNKNOWN = cp_model_pb2 .UNKNOWN
91
+ MODEL_INVALID = cp_model_pb2 .MODEL_INVALID
92
+ FEASIBLE = cp_model_pb2 .FEASIBLE
93
+ INFEASIBLE = cp_model_pb2 .INFEASIBLE
94
+ OPTIMAL = cp_model_pb2 .OPTIMAL
94
95
95
96
# Variable selection strategy
96
- CHOOSE_FIRST : cp_model_pb2 .DecisionStrategyProto .VariableSelectionStrategy = (
97
- cp_model_pb2 .DecisionStrategyProto .CHOOSE_FIRST
98
- )
99
- CHOOSE_LOWEST_MIN : (
100
- cp_model_pb2 .DecisionStrategyProto .VariableSelectionStrategy
101
- ) = cp_model_pb2 .DecisionStrategyProto .CHOOSE_LOWEST_MIN
102
- CHOOSE_HIGHEST_MAX : (
103
- cp_model_pb2 .DecisionStrategyProto .VariableSelectionStrategy
104
- ) = cp_model_pb2 .DecisionStrategyProto .CHOOSE_HIGHEST_MAX
105
- CHOOSE_MIN_DOMAIN_SIZE : (
106
- cp_model_pb2 .DecisionStrategyProto .VariableSelectionStrategy
107
- ) = cp_model_pb2 .DecisionStrategyProto .CHOOSE_MIN_DOMAIN_SIZE
108
- CHOOSE_MAX_DOMAIN_SIZE : (
109
- cp_model_pb2 .DecisionStrategyProto .VariableSelectionStrategy
110
- ) = cp_model_pb2 .DecisionStrategyProto .CHOOSE_MAX_DOMAIN_SIZE
97
+ CHOOSE_FIRST = cp_model_pb2 .DecisionStrategyProto .CHOOSE_FIRST
98
+ CHOOSE_LOWEST_MIN = cp_model_pb2 .DecisionStrategyProto .CHOOSE_LOWEST_MIN
99
+ CHOOSE_HIGHEST_MAX = cp_model_pb2 .DecisionStrategyProto .CHOOSE_HIGHEST_MAX
100
+ CHOOSE_MIN_DOMAIN_SIZE = cp_model_pb2 .DecisionStrategyProto .CHOOSE_MIN_DOMAIN_SIZE
101
+ CHOOSE_MAX_DOMAIN_SIZE = cp_model_pb2 .DecisionStrategyProto .CHOOSE_MAX_DOMAIN_SIZE
111
102
112
103
# Domain reduction strategy
113
- SELECT_MIN_VALUE : cp_model_pb2 .DecisionStrategyProto .DomainReductionStrategy = (
114
- cp_model_pb2 .DecisionStrategyProto .SELECT_MIN_VALUE
115
- )
116
- SELECT_MAX_VALUE : cp_model_pb2 .DecisionStrategyProto .DomainReductionStrategy = (
117
- cp_model_pb2 .DecisionStrategyProto .SELECT_MAX_VALUE
118
- )
119
- SELECT_LOWER_HALF : (
120
- cp_model_pb2 .DecisionStrategyProto .DomainReductionStrategy
121
- ) = cp_model_pb2 .DecisionStrategyProto .SELECT_LOWER_HALF
122
- SELECT_UPPER_HALF : (
123
- cp_model_pb2 .DecisionStrategyProto .DomainReductionStrategy
124
- ) = cp_model_pb2 .DecisionStrategyProto .SELECT_UPPER_HALF
104
+ SELECT_MIN_VALUE = cp_model_pb2 .DecisionStrategyProto .SELECT_MIN_VALUE
105
+ SELECT_MAX_VALUE = cp_model_pb2 .DecisionStrategyProto .SELECT_MAX_VALUE
106
+ SELECT_LOWER_HALF = cp_model_pb2 .DecisionStrategyProto .SELECT_LOWER_HALF
107
+ SELECT_UPPER_HALF = cp_model_pb2 .DecisionStrategyProto .SELECT_UPPER_HALF
125
108
126
109
# Search branching
127
110
AUTOMATIC_SEARCH = sat_parameters_pb2 .SatParameters .AUTOMATIC_SEARCH
144
127
VariableT = Union ["IntVar" , IntegralT ]
145
128
LinearExprT = Union ["LinearExpr" , "IntVar" , IntegralT ]
146
129
ObjLinearExprT = Union ["LinearExpr" , "IntVar" , NumberT ]
130
+ BoundedLinearExprT = Union ["BoundedLinearExpression" , bool ]
147
131
ArcT = Tuple [IntegralT , IntegralT , LiteralT ]
148
132
_IndexOrSeries = Union [pd .Index , pd .Series ]
149
133
@@ -405,110 +389,110 @@ def get_float_var_value_map(
405
389
break
406
390
return coeffs , constant , is_integer
407
391
408
- def __hash__ (self ):
392
+ def __hash__ (self ) -> int :
409
393
return object .__hash__ (self )
410
394
411
- def __abs__ (self ):
395
+ def __abs__ (self ) -> NoReturn :
412
396
raise NotImplementedError (
413
397
"calling abs() on a linear expression is not supported, "
414
398
"please use CpModel.add_abs_equality"
415
399
)
416
400
417
- def __add__ (self , arg ):
401
+ def __add__ (self , arg ) -> LinearExprT :
418
402
if cmh .is_zero (arg ):
419
403
return self
420
404
return _Sum (self , arg )
421
405
422
- def __radd__ (self , arg ):
406
+ def __radd__ (self , arg ) -> LinearExprT :
423
407
if cmh .is_zero (arg ):
424
408
return self
425
409
return _Sum (self , arg )
426
410
427
- def __sub__ (self , arg ):
411
+ def __sub__ (self , arg ) -> LinearExprT :
428
412
if cmh .is_zero (arg ):
429
413
return self
430
414
return _Sum (self , - arg )
431
415
432
- def __rsub__ (self , arg ):
416
+ def __rsub__ (self , arg ) -> LinearExprT :
433
417
return _Sum (- self , arg )
434
418
435
- def __mul__ (self , arg ):
419
+ def __mul__ (self , arg ) -> LinearExprT :
436
420
arg = cmh .assert_is_a_number (arg )
437
421
if cmh .is_one (arg ):
438
422
return self
439
423
elif cmh .is_zero (arg ):
440
424
return 0
441
425
return _ProductCst (self , arg )
442
426
443
- def __rmul__ (self , arg ):
427
+ def __rmul__ (self , arg ) -> LinearExprT :
444
428
arg = cmh .assert_is_a_number (arg )
445
429
if cmh .is_one (arg ):
446
430
return self
447
431
elif cmh .is_zero (arg ):
448
432
return 0
449
433
return _ProductCst (self , arg )
450
434
451
- def __div__ (self , _ ):
435
+ def __div__ (self , _ ) -> NoReturn :
452
436
raise NotImplementedError (
453
437
"calling / on a linear expression is not supported, "
454
438
"please use CpModel.add_division_equality"
455
439
)
456
440
457
- def __truediv__ (self , _ ):
441
+ def __truediv__ (self , _ ) -> NoReturn :
458
442
raise NotImplementedError (
459
443
"calling // on a linear expression is not supported, "
460
444
"please use CpModel.add_division_equality"
461
445
)
462
446
463
- def __mod__ (self , _ ):
447
+ def __mod__ (self , _ ) -> NoReturn :
464
448
raise NotImplementedError (
465
449
"calling %% on a linear expression is not supported, "
466
450
"please use CpModel.add_modulo_equality"
467
451
)
468
452
469
- def __pow__ (self , _ ):
453
+ def __pow__ (self , _ ) -> NoReturn :
470
454
raise NotImplementedError (
471
455
"calling ** on a linear expression is not supported, "
472
456
"please use CpModel.add_multiplication_equality"
473
457
)
474
458
475
- def __lshift__ (self , _ ):
459
+ def __lshift__ (self , _ ) -> NoReturn :
476
460
raise NotImplementedError (
477
461
"calling left shift on a linear expression is not supported"
478
462
)
479
463
480
- def __rshift__ (self , _ ):
464
+ def __rshift__ (self , _ ) -> NoReturn :
481
465
raise NotImplementedError (
482
466
"calling right shift on a linear expression is not supported"
483
467
)
484
468
485
- def __and__ (self , _ ):
469
+ def __and__ (self , _ ) -> NoReturn :
486
470
raise NotImplementedError (
487
471
"calling and on a linear expression is not supported, "
488
472
"please use CpModel.add_bool_and"
489
473
)
490
474
491
- def __or__ (self , _ ):
475
+ def __or__ (self , _ ) -> NoReturn :
492
476
raise NotImplementedError (
493
477
"calling or on a linear expression is not supported, "
494
478
"please use CpModel.add_bool_or"
495
479
)
496
480
497
- def __xor__ (self , _ ):
481
+ def __xor__ (self , _ ) -> NoReturn :
498
482
raise NotImplementedError (
499
483
"calling xor on a linear expression is not supported, "
500
484
"please use CpModel.add_bool_xor"
501
485
)
502
486
503
- def __neg__ (self ):
487
+ def __neg__ (self ) -> LinearExprT :
504
488
return _ProductCst (self , - 1 )
505
489
506
- def __bool__ (self ):
490
+ def __bool__ (self ) -> NoReturn :
507
491
raise NotImplementedError (
508
492
"Evaluating a LinearExpr instance as a Boolean is not implemented."
509
493
)
510
494
511
- def __eq__ (self , arg ):
495
+ def __eq__ (self , arg ) -> BoundedLinearExprT :
512
496
if arg is None :
513
497
return False
514
498
if cmh .is_integral (arg ):
@@ -517,21 +501,21 @@ def __eq__(self, arg):
517
501
else :
518
502
return BoundedLinearExpression (self - arg , [0 , 0 ])
519
503
520
- def __ge__ (self , arg ):
504
+ def __ge__ (self , arg ) -> BoundedLinearExprT :
521
505
if cmh .is_integral (arg ):
522
506
arg = cmh .assert_is_int64 (arg )
523
507
return BoundedLinearExpression (self , [arg , INT_MAX ])
524
508
else :
525
509
return BoundedLinearExpression (self - arg , [0 , INT_MAX ])
526
510
527
- def __le__ (self , arg ):
511
+ def __le__ (self , arg ) -> BoundedLinearExprT :
528
512
if cmh .is_integral (arg ):
529
513
arg = cmh .assert_is_int64 (arg )
530
514
return BoundedLinearExpression (self , [INT_MIN , arg ])
531
515
else :
532
516
return BoundedLinearExpression (self - arg , [INT_MIN , 0 ])
533
517
534
- def __lt__ (self , arg ):
518
+ def __lt__ (self , arg ) -> BoundedLinearExprT :
535
519
if cmh .is_integral (arg ):
536
520
arg = cmh .assert_is_int64 (arg )
537
521
if arg == INT_MIN :
@@ -540,7 +524,7 @@ def __lt__(self, arg):
540
524
else :
541
525
return BoundedLinearExpression (self - arg , [INT_MIN , - 1 ])
542
526
543
- def __gt__ (self , arg ):
527
+ def __gt__ (self , arg ) -> BoundedLinearExprT :
544
528
if cmh .is_integral (arg ):
545
529
arg = cmh .assert_is_int64 (arg )
546
530
if arg == INT_MAX :
@@ -549,7 +533,7 @@ def __gt__(self, arg):
549
533
else :
550
534
return BoundedLinearExpression (self - arg , [1 , INT_MAX ])
551
535
552
- def __ne__ (self , arg ):
536
+ def __ne__ (self , arg ) -> BoundedLinearExprT :
553
537
if arg is None :
554
538
return True
555
539
if cmh .is_integral (arg ):
@@ -904,7 +888,7 @@ def __str__(self) -> str:
904
888
def name (self ) -> str :
905
889
return "not(%s)" % str (self .__boolvar )
906
890
907
- def __bool__ (self ) -> bool :
891
+ def __bool__ (self ) -> NoReturn :
908
892
raise NotImplementedError (
909
893
"Evaluating a literal as a Boolean value is not implemented."
910
894
)
@@ -964,8 +948,9 @@ def bounds(self) -> Sequence[int]:
964
948
return self .__bounds
965
949
966
950
def __bool__ (self ) -> bool :
967
- if isinstance (self .__expr , LinearExpr ):
968
- coeffs_map , constant = self .__expr .get_integer_var_value_map ()
951
+ expr = self .__expr
952
+ if isinstance (expr , LinearExpr ):
953
+ coeffs_map , constant = expr .get_integer_var_value_map ()
969
954
all_coeffs = set (coeffs_map .values ())
970
955
same_var = set ([0 ])
971
956
eq_bounds = [0 , 0 ]
@@ -3181,7 +3166,7 @@ def sufficient_assumptions_for_infeasibility(self) -> Sequence[int]:
3181
3166
"""Returns the indices of the infeasible assumptions."""
3182
3167
return self ._solution .sufficient_assumptions_for_infeasibility
3183
3168
3184
- def status_name (self , status : Optional [cp_model_pb2 . CpSolverStatus ] = None ) -> str :
3169
+ def status_name (self , status : Optional [Any ] = None ) -> str :
3185
3170
"""Returns the name of the status returned by solve()."""
3186
3171
if status is None :
3187
3172
status = self ._solution .status
@@ -3245,7 +3230,7 @@ def Solve(
3245
3230
def SolutionInfo (self ) -> str :
3246
3231
return self .solution_info ()
3247
3232
3248
- def StatusName (self , status : Optional [cp_model_pb2 . CpSolverStatus ] = None ) -> str :
3233
+ def StatusName (self , status : Optional [Any ] = None ) -> str :
3249
3234
return self .status_name (status )
3250
3235
3251
3236
def StopSearch (self ) -> None :
0 commit comments