Spaces are allocated according to an order in the file

This commit is contained in:
Evgeniy L 2015-12-25 16:55:55 +03:00
parent d2451f816b
commit 0f05006e01
2 changed files with 28 additions and 15 deletions

View File

@ -26,6 +26,9 @@ from oslo_log import log
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
def format_x_vector(coefficients):
return ' + '.join(['({0:+} * x{1})'.format(c, i) for i, c in enumerate(coefficients)])
def shift(arr, steps, val=0): def shift(arr, steps, val=0):
res_arr = np.roll(arr, steps) res_arr = np.roll(arr, steps)
np.put(res_arr, range(steps), val) np.put(res_arr, range(steps), val)
@ -120,12 +123,15 @@ class DynamicAllocationLinearProgram(object):
self.x_amount = len(self.disks) * len(self.spaces) self.x_amount = len(self.disks) * len(self.spaces)
self._init_equation(self.disks, self.spaces) self._init_equation(self.disks, self.spaces)
self._init_objective_function_coefficient()
self._init_min_max() self._init_min_max()
def solve(self): def solve(self):
upper_bound_matrix = self._make_upper_bound_constraint_matrix() or None upper_bound_matrix = self._make_upper_bound_constraint_matrix() or None
upper_bound_vector = self._make_upper_bound_constraint_vector() or None upper_bound_vector = self._make_upper_bound_constraint_vector() or None
LOG.debug('Objective function coefficients human-readable: %s', format_x_vector(self.objective_function_coefficients))
solution = linprog( solution = linprog(
self.objective_function_coefficients, self.objective_function_coefficients,
A_eq=self.equality_constraint_matrix, A_eq=self.equality_constraint_matrix,
@ -139,7 +145,6 @@ class DynamicAllocationLinearProgram(object):
return self._convert_solution(solution) return self._convert_solution(solution)
def _init_min_max(self): def _init_min_max(self):
for space_idx, space in enumerate(self.spaces): for space_idx, space in enumerate(self.spaces):
row = self._make_matrix_row() row = self._make_matrix_row()
@ -211,12 +216,8 @@ class DynamicAllocationLinearProgram(object):
# 0, - x4 multiplier, size of space 2 on 2nd disk, 0 for the first # 0, - x4 multiplier, size of space 2 on 2nd disk, 0 for the first
# 0, - x5 multiplier, size of space 1 on 3rd disk, 0 for the first # 0, - x5 multiplier, size of space 1 on 3rd disk, 0 for the first
# 0] - x6 multiplier, size of space 2 on 3rd disk, 0 for the first # 0] - x6 multiplier, size of space 2 on 3rd disk, 0 for the first
equality_matrix_row = self._make_matrix_row() equality_matrix_row = self._make_matrix_row()
# Amount of coefficients is equal to amount of columns in the matrix
self._init_objective_function_coefficient(len(spaces) * len(disks))
# Set first len(spaces) elements to 1 # Set first len(spaces) elements to 1
equality_matrix_row = shift(equality_matrix_row, len(spaces), val=1) equality_matrix_row = shift(equality_matrix_row, len(spaces), val=1)
@ -228,16 +229,27 @@ class DynamicAllocationLinearProgram(object):
for _ in range(self.x_amount): for _ in range(self.x_amount):
self._add_bound(0, None) self._add_bound(0, None)
def _add_objective_function_coefficient(self): def _init_objective_function_coefficient(self):
# By default the algorithm tries to minimize the solution # Amount of coefficients is equal to amount of x
# we should invert sign, in order to make it as a maximization c_amount = self.x_amount
# function, we want disks to be maximally allocated.
# Coefficient for space per disk is 1, because all spaces
# are equal and should not be adjusted.
self.objective_function_coefficients.append(-1)
def _init_objective_function_coefficient(self, size): coefficients = [1 for _ in range(c_amount)]
self.objective_function_coefficients = [-1] * size
# We want spaces to be allocated on disks
# in order which user specified them in the schema.
# In order to do that, we set coefficients
# higher for those spaces which defined earlier
# in the list
for s_i in range(len(self.spaces)):
for d_i in range(len(self.disks)):
c_i = len(self.spaces) * d_i + s_i
coefficients[c_i] = (len(self.spaces) - s_i) * (len(self.disks) - d_i)
# By default the algorithm tries to minimize the solution
# we should invert sign, in order to make it a maximization
# function, because we want disks to be maximally allocated.
self.objective_function_coefficients = [-c for c in coefficients]
def _add_bound(self, min_, max_): def _add_bound(self, min_, max_):
np.append(self.bounds, (min_, max_)) np.append(self.bounds, (min_, max_))

View File

@ -28,7 +28,8 @@ class StdoutViewer(object):
class SVGViewer(object): class SVGViewer(object):
PALETTE = ['#666547', '#fb2e01', '#6fcb9f', '#ffe28a', '#fffeb3'] PALETTE = ['#666547', '#fb2e01', '#6fcb9f', '#ffe28a', '#b9feb3',
'#404040', '#4cc3d9', '#7bc8a4', '#ffc65d', '#f16745']
DISK_HEIGHT = 3 * cm DISK_HEIGHT = 3 * cm
SPACE_HEIGHT = 3 * cm SPACE_HEIGHT = 3 * cm
WIDTH_MULTIPLIER = 10 WIDTH_MULTIPLIER = 10