|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding:UTF-8 |
| 3 | +__author__ = 'shenshijun' |
| 4 | +""" |
| 5 | +动态规划实现活动调度的问题,每个活动都有开始和结束时间,并且有一个权,要求得使得权和最大的活动集合 |
| 6 | +""" |
| 7 | + |
| 8 | + |
| 9 | +class Action(object): |
| 10 | + """ |
| 11 | + 活动对象 |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self, start_time, end_time, weight): |
| 15 | + self.start_time = start_time |
| 16 | + self.end_time = end_time |
| 17 | + self.weight = weight |
| 18 | + |
| 19 | + def __cmp__(self, other): |
| 20 | + pass |
| 21 | + |
| 22 | + def __eq__(self, other): |
| 23 | + return self.start_time == other.start_time and self.end_time == other.end_time |
| 24 | + |
| 25 | + def __hash__(self): |
| 26 | + return hash(hash(self.start_time) * hash(self.end_time)) |
| 27 | + |
| 28 | + def __str__(self): |
| 29 | + return "".join(["Action(start_time=", str(self.start_time), ",ent_time=", str(self.end_time), ",weight=", |
| 30 | + str(self.weight), ")"]) |
| 31 | + |
| 32 | + |
| 33 | +__value_dict = {} |
| 34 | +__actions_dict = {} |
| 35 | + |
| 36 | + |
| 37 | +def max_weighted_actions(action_list): |
| 38 | + """ |
| 39 | + 假设Action是按照end_time排序的。因此在算法里面就不排序了。 |
| 40 | + :param action_list: |
| 41 | + :return: |
| 42 | + """ |
| 43 | + global __actions_dict |
| 44 | + global __value_dict |
| 45 | + action_len = len(action_list) |
| 46 | + # 循环退出条件 |
| 47 | + if action_len is 0: |
| 48 | + return 0 |
| 49 | + |
| 50 | + cur_max_weight = 0 |
| 51 | + for k in xrange(action_len): |
| 52 | + left_max_weight = 0 |
| 53 | + if k > 0: |
| 54 | + if (action_list[0], action_list[k]) not in __value_dict: |
| 55 | + __value_dict[(action_list[0], action_list[k])] = max_weighted_actions( |
| 56 | + filter(lambda action: action.end_time <= action_list[k].start_time, action_list[:k])) |
| 57 | + left_max_weight = __value_dict[(action_list[0], action_list[k])] |
| 58 | + |
| 59 | + right_max_weight = 0 |
| 60 | + if k < action_len - 1: |
| 61 | + if (action_list[k], action_list[-1]) not in __value_dict: |
| 62 | + __value_dict[(action_list[k], action_list[-1])] = max_weighted_actions( |
| 63 | + filter(lambda action: action.start_time >= action_list[k].end_time, action_list[k + 1:])) |
| 64 | + right_max_weight = __value_dict[(action_list[k], action_list[-1])] |
| 65 | + |
| 66 | + cur_weight = left_max_weight + right_max_weight + action_list[k].weight |
| 67 | + if cur_max_weight < cur_weight: |
| 68 | + cur_max_weight = cur_weight |
| 69 | + |
| 70 | + __value_dict[(action_list[0], action_list[-1])] = cur_max_weight |
| 71 | + return __value_dict[(action_list[0], action_list[-1])] |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + action_list = [Action(1, 4, 2), Action(3, 5, 1), Action(0, 6, 1), Action(5, 7, 2), Action(3, 9, 1), Action(5, 9, 1), |
| 76 | + Action(6, 10, 1), Action(8, 11, 2), Action(8, 12, 1), Action(2, 14, 1), Action(2, 16, 2)] |
| 77 | + print(max_weighted_actions(action_list)) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + main() |
| 82 | + |
0 commit comments