Move helper methods into utils file

This commit is contained in:
Evgeniy L 2016-02-18 20:27:06 +03:00
parent c67aa04291
commit 1afe1a411e
2 changed files with 64 additions and 55 deletions

View File

@ -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)

View File

@ -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.