From 1afe1a411eb3f1f8327789cdb1600680b3e48641 Mon Sep 17 00:00:00 2001 From: Evgeniy L Date: Thu, 18 Feb 2016 20:27:06 +0300 Subject: [PATCH] Move helper methods into utils file --- bareon_dynamic_allocator/allocators.py | 67 +++++--------------------- bareon_dynamic_allocator/utils.py | 52 ++++++++++++++++++++ 2 files changed, 64 insertions(+), 55 deletions(-) diff --git a/bareon_dynamic_allocator/allocators.py b/bareon_dynamic_allocator/allocators.py index a16c06f..a28e0a4 100644 --- a/bareon_dynamic_allocator/allocators.py +++ b/bareon_dynamic_allocator/allocators.py @@ -21,9 +21,10 @@ import numpy as np from oslo_log import log from scipy.optimize import linprog -from termcolor import colored from bareon_dynamic_allocator import errors +from bareon_dynamic_allocator import utils + from bareon_dynamic_allocator.objects import Disk from bareon_dynamic_allocator.objects import Space from bareon_dynamic_allocator.parser import Parser @@ -33,53 +34,6 @@ from bareon_dynamic_allocator.sequences import CrossSumInequalitySequence LOG = log.getLogger(__name__) -def shift(arr, steps, val=0): - res_arr = np.roll(arr, steps) - np.put(res_arr, range(steps), val) - - return res_arr - - -def grouper(iterable, n, fillvalue=None): - """Collect data into fixed-length chunks or blocks. - - Source: https://docs.python.org/2/library/itertools.html#recipes - """ - args = [iter(iterable)] * n - return itertools.izip_longest(fillvalue=fillvalue, *args) - - -def format_x_vector(coefficients, num=0): - return '\n{0}\n'.format('\n'.join( - [' + '.join(group) - for group in grouper(['({0:+.5f} * x{1})'.format(c, i) - for i, c in enumerate(coefficients)], num)])) - - -def format_equation(matrix, vector, row_len): - equation = [] - - for idx, m_row in enumerate(matrix): - line = [] - - for i, c in enumerate(m_row): - x = '({0:+} * x{1})'.format(c, i) - if c > 0: - colored_x = colored(x, 'green') - elif c < 0: - colored_x = colored(x, 'red') - else: - colored_x = colored(x, 'white') - - line.append(colored_x) - - line = ' + '.join(line) + ' = {0}'.format(vector[idx]) - - equation.append(line) - - return '\n'.join(equation) - - class DynamicAllocator(object): def __init__(self, hw_info, schema): @@ -209,16 +163,16 @@ class DynamicAllocationLinearProgram(object): upper_bound_vector = self._make_upper_bound_constraint_vector() or None LOG.debug('Objective function coefficients human-readable:\n%s\n', - format_x_vector(self.objective_function_coefficients, - len(self.spaces))) + utils.format_x_vector(self.objective_function_coefficients, + len(self.spaces))) LOG.debug('Equality equation:\n%s\n', - format_equation( + utils.format_equation( self.equality_constraint_matrix, self.equality_constraint_vector, len(self.spaces))) LOG.debug('Inequality equation:\n%s\n', - format_equation( + utils.format_equation( upper_bound_matrix, upper_bound_vector, len(self.spaces))) @@ -383,7 +337,7 @@ class DynamicAllocationLinearProgram(object): def _convert_solution(self, solution_vector): result = [] - spaces_grouped_by_disk = list(grouper( + spaces_grouped_by_disk = list(utils.grouper( solution_vector, len(self.spaces))) for disk_i in range(len(self.disks)): @@ -426,11 +380,14 @@ class DynamicAllocationLinearProgram(object): equality_matrix_row = self._make_matrix_row() # Set first len(spaces) elements to 1 - equality_matrix_row = shift(equality_matrix_row, len(spaces), val=1) + equality_matrix_row = utils.shift( + equality_matrix_row, + len(spaces), + val=1) for _ in range(len(disks)): self.equality_constraint_matrix.append(equality_matrix_row) - equality_matrix_row = shift( + equality_matrix_row = utils.shift( equality_matrix_row, len(spaces), val=0) diff --git a/bareon_dynamic_allocator/utils.py b/bareon_dynamic_allocator/utils.py index cffe87e..2b16e21 100644 --- a/bareon_dynamic_allocator/utils.py +++ b/bareon_dynamic_allocator/utils.py @@ -12,8 +12,60 @@ # License for the specific language governing permissions and limitations # under the License. +import itertools + +import numpy as np import yaml +from termcolor import colored + + +def shift(arr, steps, val=0): + res_arr = np.roll(arr, steps) + np.put(res_arr, range(steps), val) + + return res_arr + + +def grouper(iterable, n, fillvalue=None): + """Collect data into fixed-length chunks or blocks. + + Source: https://docs.python.org/2/library/itertools.html#recipes + """ + args = [iter(iterable)] * n + return itertools.izip_longest(fillvalue=fillvalue, *args) + + +def format_x_vector(coefficients, num=0): + return '\n{0}\n'.format('\n'.join( + [' + '.join(group) + for group in grouper(['({0:+.5f} * x{1})'.format(c, i) + for i, c in enumerate(coefficients)], num)])) + + +def format_equation(matrix, vector, row_len): + equation = [] + + for idx, m_row in enumerate(matrix): + line = [] + + for i, c in enumerate(m_row): + x = '({0:+} * x{1})'.format(c, i) + if c > 0: + colored_x = colored(x, 'green') + elif c < 0: + colored_x = colored(x, 'red') + else: + colored_x = colored(x, 'white') + + line.append(colored_x) + + line = ' + '.join(line) + ' = {0}'.format(vector[idx]) + + equation.append(line) + + return '\n'.join(equation) + def parse_yaml(path): """Parses yaml file.