Example #1
0
 def typecheck_ct_added_to_model(self, mdl, ct, caller=None):
     if not ct.is_added():
         s_caller = resolve_caller_as_string(caller, sep=' ')
         mdl.fatal("{0}Constraint: {1!s} has not been added to any model".format(s_caller, ct))
     elif mdl is not ct.model:
         s_caller = resolve_caller_as_string(caller, sep=' ')
         mdl.fatal("{0}Constraint: {1!s} belongs to a different model".format(s_caller, ct))
Example #2
0
 def typecheck_int(cls,
                   logger,
                   arg,
                   check_math,
                   accept_negative=True,
                   caller=None):
     if not is_number(arg):
         caller_string = resolve_caller_as_string(caller)
         logger.fatal("{0}Expecting number, got: {1!r}",
                      (caller_string, arg))
     if check_math:
         if math.isnan(arg):
             caller_string = resolve_caller_as_string(caller)
             logger.fatal("{0}NaN value detected", (caller_string, ))
         elif math.isinf(arg):
             caller_string = resolve_caller_as_string(caller)
             logger.fatal("{0}Infinite value detected", (caller_string, ))
     if not is_int(arg):
         caller_string = resolve_caller_as_string(caller)
         logger.fatal("{0}Expecting integer, got: {1!r}",
                      (caller_string, arg))
     elif not accept_negative and arg < 0:
         caller_string = resolve_caller_as_string(caller)
         logger.fatal("{0}Expecting positive integer, got: {1!r}",
                      (caller_string, arg))
Example #3
0
 def typecheck_string(self, arg, accept_empty=False, accept_none=False, caller=''):
     if is_string(arg):
         if not accept_empty and 0 == len(arg):
             s_caller = resolve_caller_as_string(caller)
             self.fatal("{0}Expecting a non-empty string", s_caller)
     elif not (arg is None and accept_none):
         s_caller = resolve_caller_as_string(caller)
         self.fatal("{0}Expecting string, got: {1!r}", s_caller, arg)
Example #4
0
 def typecheck_num(cls, logger, arg, check_math, caller=None):
     if not is_number(arg):
         caller_string = resolve_caller_as_string(caller)
         logger.fatal("{0}Expecting number, got: {1!r}", (caller_string, arg))
     elif check_math:
         if math.isnan(arg):
             caller_string = resolve_caller_as_string(caller)
             logger.fatal("{0}NaN value detected", (caller_string,))
         elif math.isinf(arg):
             caller_string = resolve_caller_as_string(caller)
             logger.fatal("{0}Infinite value detected", (caller_string,))
Example #5
0
    def typecheck_var_seq(self, seq, vtype=None, caller=None):
        # build a list to avoid consuming an iterator
        checked_var_list = list(seq)
        for i, x in enumerate(checked_var_list):
            if not isinstance(x, Var):
                caller_s = resolve_caller_as_string(caller)
                self.fatal("{2}Expecting an iterable returning variables, {0!r} was passed at position {1}", x, i, caller_s)
            if vtype and x.vartype.get_cplex_typecode() != vtype:
                caller_s = resolve_caller_as_string(caller)
                self.fatal("{3}Expecting an iterable returning variables of type {0}, {1!r} was passed at position {2}",
                           vtype.short_name, x, i, caller_s)

        return checked_var_list
Example #6
0
 def typecheck_logical_op(self, arg, caller):
     if not hasattr(
             arg, 'as_logical_operand') or arg.as_logical_operand() is None:
         caller_s = resolve_caller_as_string(caller)
         self.fatal(
             '{1}Not a logical operand: {0!r}. Expecting binary variable, logical expression',
             arg, caller_s)
Example #7
0
 def typecheck_logical_op(cls, logger, arg, caller):
     if not hasattr(
             arg, 'as_logical_operand') or arg.as_logical_operand() is None:
         caller_s = resolve_caller_as_string(caller)
         logger.fatal(
             '{1}Not a logical operand: {0!s} - Expecting binary variable, logical expression or constraint',
             arg, caller_s)
Example #8
0
 def typecheck_string_seq(self, arg, accept_empty=False, accept_none=False, caller=''):
     checked_strings = list(arg)
     # do not accept a string
     if is_string(arg):
         s_caller = resolve_caller_as_string(caller)
         self.fatal("{0}Expecting list of strings, a string was passed: '{1}'", s_caller, arg)
     for s in checked_strings:
         self.typecheck_string(s, accept_empty=accept_empty, accept_none=accept_none, caller=caller)
     return checked_strings
Example #9
0
 def check_for_duplicate_keys(self, keys, caller=None):
     key_set = set(keys)
     if len(key_set) < len(keys):
         # some key is duplicated:
         inc_set = set()
         for k in keys:
             if k in inc_set:
                 s_caller = resolve_caller_as_string(caller, sep=' ')
                 self.fatal("{0}Duplicated key: {1!s}".format(s_caller, k))
             else:
                 inc_set.add(k)
Example #10
0
 def typecheck_ct_not_added(self, ct, do_raise=False, caller=None):
     if ct.is_added():
         s_caller = resolve_caller_as_string(caller, sep=' ')
         if do_raise:
             self.fatal(
                 '{0}expects a non-added constraint, {1} is added (index={2})',
                 s_caller, ct, ct.get_index())
         else:
             self.warning(
                 '{0}expects a non-added constraint, {1} is added (index={2})',
                 s_caller, ct, ct.get_index())
Example #11
0
 def typecheck_initial_lp_stats(cls, logger, stats, stat_type, caller=None):
     caller_s = resolve_caller_as_string(caller)
     if not is_ordered_sequence(stats):
         logger.fatal(
             '{0}expects ordered sequence of {2} basis statuses, {1!r} was passed',
             caller_s, stats, stat_type)
     l_stats = list(stats)
     for s, stat in enumerate(l_stats):
         if not isinstance(stat, BasisStatus):
             logger.fatal(
                 '{0}expects a sequence of {3} basis status, {1} was passed at pos {2}',
                 caller_s, stat, s, stat_type)
     return l_stats
Example #12
0
 def loop_caller():
     return '%s, arg#%d' % (resolve_caller_as_string(caller,
                                                     sep=''), i)