def __init__(self, safety_margin, vessel, quay, **kwargs): """ initialization function for Instance class :param safety_margin: prefix:``safety margin between two consecutive QCs``,type:``int``,range:``[0, inf)`` :param vessel: prefix:``vessel for an instance``, type:``Vessel`` :param quay: prefix:``quay for an instance``, type:``Quay`` :param kwargs: other options, e.g., 'fixed', which means that the initial location of QCs are given by kwargs['fixed'] :return: None """ super(Instance, self).__init__() self.__safety_margin = safety_margin self.__vessel = vessel self.__quay = quay if "fixed" in kwargs: if isinstance(kwargs["fixed"], (list, tuple)) and len( kwargs["fixed"]) == len(self.quay.size): previous_position = -1 * float("inf") for i, q in enumerate(self.quay.size): if kwargs["fixed"][ i] - previous_position > self.__safety_margin: q.initial_location = kwargs["fixed"][i] previous_position = q.initial_location else: raise QCSPGenException( "- initial location data error: safety margin=%d" % self.__safety_margin) else: raise QCSPGenException( "- kwargs['fixed'] should be list/tuple with length=%d" % self.quay.size) else: self._set_qcs_l0_randomly()
def __init__(self, n, **kwargs): """ initialization function of a quay :param n: prefix:``number of QCs``, type:``int``, range:``(0, inf)`` :param kwargs: properties of quay cranes :return: None """ super(Quay, self).__init__(QC) for i in range(n): super(Quay, self).append(QC()) for p in QC.PROPERTY: if p in kwargs.keys(): if isinstance(kwargs[p], (float, int)): for q in self.qcs: setattr(q, p, kwargs[p]) elif len(kwargs[p]) == self.size: for i, q in enumerate(self.qcs): setattr(q, p, kwargs[p][i]) else: raise QCSPGenException( "- {0}=n or =[{0}_1,...{0}_n] where n is the number of QCs!" .format(p))
def remove(self, item): """ to remove an item from an aggregator :param item: the item to be removed :return: None """ try: self._aggregation.remove(item) except ValueError: raise QCSPGenException("- item is not in {}, cannot remove".format( self.__class__.__name__))
def compulsory_kwargs(kwargs, compulsory): """ function to check if `kwargs` contains `compulsory` or not. If no, raise a QCSPGenException. :param kwargs: key word arguments to be checked :param compulsory: compulsory key word dicts :exception: QCSPGenException """ for k in compulsory: if k not in kwargs: raise QCSPGenException("- Compulsory kwargs: %r. %s is missing!" % (compulsory, k))
def verify_is_in(data, data_range): """ function to check if `data` is in `data_range`. If no, raise a QCSPGenException. :param data: a string to be checked :param data_range: legitimate data range :exception: QCSPGenException :return: data """ if data not in data_range: raise QCSPGenException("- %s is not in %r\n" % (data, data_range)) return data
def verify_type(data, t, prefix=""): """ function to check if `data` is type `t`. If no, raise a QCSPGenException. :param data: data to be checked :param t: expected legitimate data type :param prefix: a prefix for the raised QCSPGenException message :exception: QCSPGenException :return: data """ if not isinstance(data, t): raise QCSPGenException("- %s should be %r\n" % (prefix, t)) return data
def wrapper(*args, **kwargs): message = "" argspect = inspect.getargspec(func) for a in arg_elements.iterkeys(): try: data = args[argspect.args.index(a)] except (ValueError, IndexError): try: data = kwargs[a] except KeyError: try: # it's value may appear as default used_defaults = argspect.defaults[len(argspect.defaults ) - len(argspect.args) + len(args):] data = list(itertools.chain( args, used_defaults))[argspect.args.index(a)] except: raise QCSPGenException( "- Compulsory kwarg %s is missing!" % a) if arg_elements[a].type: if not ('float' in arg_elements[a].type and type(data).__name__ in ('float', 'int') or type(data).__name__ in arg_elements[a].type): message += "- data {} should be {}\n".format( arg_elements[a].prefix, arg_elements[a].type) if arg_elements[a].range: if (arg_elements[a].is_numeric() and not arg_elements[a].range.check_range(data)) or \ (not arg_elements[a].is_numeric() and data not in arg_elements[a].range): message += "- %s not in range %s\n" % ( arg_elements[a].prefix, arg_elements[a].range) if message is not "": raise QCSPGenException(message) return func(*args, **kwargs)
def append(self, *args): """ overridden method for Aggregator ``append``, to append a list of tasks into a bay :param args: a list of tasks :return: None """ for t in args: super(Bay, self).append(t) t.location = self.__index self.__remaining_capacity -= t.processing_time self.__aggregate_task_processing_time += t.processing_time if self.__remaining_capacity < 0: raise QCSPGenException( "- the remaining capacity of a bay must be non-negative!")
def verify_numerical_type(data, t=(int, float), lb=0.0, ub=float('inf'), prefix="", left_tight=True, right_tight=True): """ function to check the input `data` is numerical and besides, it is in the range of (lb, ub) or [lb, ub] or (lb, ub], or [lb, ub). :param data: the input to be checked :param t: int or float :param lb: lower bound :param ub: upper bound :param prefix: a prefix for the raised QCSPGenException message :param left_tight: if True, then lb can be reached :param right_tight: if True, then ub can be reached :exception: QCSPGenException :return: data """ message = "" if not isinstance(data, t): message += "- data %s should be %r\n" % (prefix, t) if left_tight and right_tight and not lb <= data <= ub: message += "- %s not in range [%.1f, %.1f]" % (prefix, lb, ub) elif left_tight and not right_tight and not lb <= data < ub: message += "- %s not in range [%.1f, %.1f)" % (prefix, lb, ub) elif not left_tight and right_tight and not lb < data <= ub: message += "- %s not in range (%.1f, %.1f]" % (prefix, lb, ub) elif not left_tight and not right_tight and not lb < data < ub: message += "- %s not in range (%.1f, %.1f)" % (prefix, lb, ub) if message is not "": raise QCSPGenException(message) return data
def wrapper(self, *item): if not isinstance(item[-1], self.item_type): raise QCSPGenException("- typeError: item is not type {}".format( self.item_type)) return func(self, *item)