def _check_exact_dimension(*args): """Wraps the dimension checking in order to provide additional logic. Returns: The wrapped function output. """ # Retrieving the object and the input from arguments obj, x = args[0], args[1] # Tries to squeeze the last dimension of `x` as it might be an array of (dim, 1) try: # Squeezes the array x = np.squeeze(x, axis=1) # If squeeze could not be performed, it means that there is no extra dimension except ValueError: pass # If the function's number of dimensions is equal to `-1` (n-dimensional) if obj.dims == -1: # Checks if the input array is bigger than zero if x.shape[0] == 0: # If not, raises an error raise e.SizeError(f'{obj.name} input should be n-dimensional') return f(obj, x) # If the input dimensions is different from function's allowed dimensions if x.shape[0] != obj.dims: # Raises an error raise e.SizeError(f'{obj.name} input should be {obj.dims}-dimensional') return f(obj, x)
def _check_less_equal_dimension(*args): """Wraps the dimension checking in order to provide additional logic. Returns: The wrapped function output. """ # Retrieving the object and the input from arguments obj, x = args[0], args[1] # Tries to squeeze the last dimension of `x` as it might be an array of (dim, 1) try: # Squeezes the array x = np.squeeze(x, axis=1) # If squeeze could not be performed, it means that there is no extra dimension except ValueError: pass # If the input dimensions is different from function's allowed dimensions if x.shape[0] > obj.dims: # Raises an error raise e.SizeError(f'{obj.name} input should be less or equal to {obj.dims}-dimensional') return f(obj, x)
def test_size_error(): new_exception = exception.SizeError('error') try: raise new_exception except exception.SizeError: pass
def __init__(self, name='Knapsack', dims=-1, continuous=False, convex=False, differentiable=False, multimodal=False, separable=False, values=(0, ), weights=(0, ), max_capacity=0.0): """Initialization method. Args: name (str): Name of the function. dims (int): Number of allowed dimensions. continuous (bool): Whether the function is continuous. convex (bool): Whether the function is convex. differentiable (bool): Whether the function is differentiable. multimodal (bool): Whether the function is multimodal. separable (bool): Whether the function is separable. values (tuple): Tuple of items values. weights (tuple): Tuple of items weights. max_capacity: Maximum capacity of the knapsack. """ # Override its parent class super(Knapsack, self).__init__(name, dims, continuous, convex, differentiable, multimodal, separable) # Checking if values and weights have the same length if len(values) != len(weights): raise e.SizeError( '`values` and `weights` needs to have the same size') # Items values self.values = values # Items weights self.weights = weights # Maximum capacity of the knapsack self.max_capacity = max_capacity # Re-writes the correct number of dimensions self.dims = len(values)
def _check_exact_dimension_and_auxiliary_matrix(*args): """Wraps the dimension checking in order to provide additional logic. Returns: The wrapped function output. """ # Retrieving the object and the input from arguments obj, x = args[0], args[1] # Tries to squeeze the last dimension of `x` as it might be an array of (dim, 1) try: # Squeezes the array x = np.squeeze(x, axis=1) # If squeeze could not be performed, it means that there is no extra dimension except ValueError: pass # If the input dimensions differs from function's allowed dimensions if x.shape[0] not in [2, 10, 30, 50]: # Raises an error raise e.SizeError(f'{obj.name} input should be 2-, 10-, 30- or 50-dimensional') # If the input dimension is equal to 2 if x.shape[0] == 2: # Defines the auxiliary matrix `M` as `M2` setattr(obj, 'M', obj.M2) # If the input dimension is equal to 10 elif x.shape[0] == 10: # Defines the auxiliary matrix `M` as `M10` setattr(obj, 'M', obj.M10) # If the input dimension is equal to 30 elif x.shape[0] == 30: # Defines the auxiliary matrix `M` as `M30` setattr(obj, 'M', obj.M30) # If the input dimension is equal to 50 elif x.shape[0] == 50: # Defines the auxiliary matrix `M` as `M50` setattr(obj, 'M', obj.M50) return f(obj, x)