From f9650b6e58aec1b2e9c78c3505ddef092f2c3126 Mon Sep 17 00:00:00 2001 From: Yuriy Taraday Date: Fri, 4 May 2012 17:04:13 +0400 Subject: [PATCH] Clean up weighted_sum logic. Change-Id: Ie8ea6af8e94149e06d1096baac4519e132d1f44d --- nova/scheduler/least_cost.py | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/nova/scheduler/least_cost.py b/nova/scheduler/least_cost.py index 70dc180e..f10e2c4b 100644 --- a/nova/scheduler/least_cost.py +++ b/nova/scheduler/least_cost.py @@ -97,30 +97,11 @@ def weighted_sum(weighted_fns, host_states, weighing_properties): candidate. """ - # Make a grid of functions results. - # One row per host. One column per function. - scores = [] - for weight, fn in weighted_fns: - scores.append([fn(host_state, weighing_properties) - for host_state in host_states]) + min_score, best_host = None, None + for host_state in host_states: + score = sum(weight * fn(host_state, weighing_properties) + for weight, fn in weighted_fns) + if min_score is None or score < min_score: + min_score, best_host = score, host_state - # Adjust the weights in the grid by the functions weight adjustment - # and sum them up to get a final list of weights. - adjusted_scores = [] - for (weight, fn), row in zip(weighted_fns, scores): - adjusted_scores.append([weight * score for score in row]) - - # Now, sum down the columns to get the final score. Column per host. - final_scores = [0.0] * len(host_states) - for row in adjusted_scores: - for idx, col in enumerate(row): - final_scores[idx] += col - - # Super-impose the host_state into the scores so - # we don't lose it when we sort. - final_scores = [(final_scores[idx], host_state) - for idx, host_state in enumerate(host_states)] - - final_scores = sorted(final_scores) - weight, host_state = final_scores[0] # Lowest score is the winner! - return WeightedHost(weight, host_state=host_state) + return WeightedHost(min_score, host_state=best_host)