Coverage for o2/actions/batching_actions/modify_size_rule_by_allocation_action.py: 47%
38 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-16 11:18 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-16 11:18 +0000
1from o2.actions.base_actions.add_size_rule_base_action import (
2 AddSizeRuleAction,
3 AddSizeRuleBaseActionParamsType,
4)
5from o2.actions.base_actions.base_action import (
6 RateSelfReturnType,
7)
8from o2.actions.base_actions.modify_size_rule_base_action import (
9 ModifySizeRuleBaseAction,
10 ModifySizeRuleBaseActionParamsType,
11)
12from o2.models.self_rating import RATING
13from o2.models.solution import Solution
14from o2.models.timetable import RULE_TYPE
15from o2.store import Store
16from o2.util.helper import select_variants
19class ModifySizeRuleByAllocationActionParamsType(ModifySizeRuleBaseActionParamsType):
20 """Parameter for ModifySizeRuleByAllocationAction."""
22 pass
25class ModifySizeRuleByLowAllocationAction(ModifySizeRuleBaseAction):
26 """An Action to modify size batching rules based on allocation.
28 1. Gets tasks with high allocation ratio
29 Allocation ratio = no. of resources assigned to the task / total no. of resources
30 2. Looks at all resources with allocation high allocation ratio
31 3. Gets a task (with batching enabled) that uses the resource
32 4. Increases the size rule by 1
33 """
35 params: ModifySizeRuleByAllocationActionParamsType
37 @staticmethod
38 def rate_self(
39 store: "Store", input: "Solution"
40 ) -> RateSelfReturnType["ModifySizeRuleByLowAllocationAction | AddSizeRuleAction"]:
41 """Generate a best set of parameters & self-evaluates this action."""
42 timetable = input.state.timetable
43 evaluation = input.evaluation
45 resource_allocations = evaluation.resource_allocation_ratio_task
46 tasks_by_allocation = sorted(resource_allocations.items(), key=lambda x: x[1], reverse=True)
48 for task_id, _ in tasks_by_allocation:
49 duration_fn = store.constraints.get_duration_fn_for_task(task_id)
50 selectors = timetable.get_firing_rule_selectors_for_task(task_id, rule_type=RULE_TYPE.SIZE)
51 # If no size rule exists, add one
52 if not selectors:
53 yield (
54 RATING.LOW,
55 AddSizeRuleAction(
56 AddSizeRuleBaseActionParamsType(task_id=task_id, size=2, duration_fn=duration_fn)
57 ),
58 )
59 for selector in select_variants(store, selectors):
60 yield (
61 ModifySizeRuleBaseAction.get_default_rating(),
62 ModifySizeRuleByLowAllocationAction(
63 ModifySizeRuleByAllocationActionParamsType(
64 rule=selector,
65 size_increment=1,
66 duration_fn=duration_fn,
67 )
68 ),
69 )
72class ModifySizeRuleByHighAllocationAction(ModifySizeRuleBaseAction):
73 """An Action to modify size batching rules based on allocation.
75 1. Gets tasks with low allocation ratio
76 Allocation ratio = no. of resources assigned to the task / total no. of resources
77 2. Looks at all resources with allocation low allocation ratio
78 3. Gets a task (with batching enabled) that uses the resource
79 4. Decreases the size rule by 1
80 """
82 params: ModifySizeRuleByAllocationActionParamsType
84 @staticmethod
85 def rate_self(
86 store: "Store", input: "Solution"
87 ) -> RateSelfReturnType["ModifySizeRuleByHighAllocationAction | AddSizeRuleAction"]:
88 """Generate a best set of parameters & self-evaluates this action."""
89 timetable = store.current_timetable
90 evaluation = store.current_evaluation
92 resource_allocations = evaluation.resource_allocation_ratio_task
93 tasks_by_allocation = sorted(resource_allocations.items(), key=lambda x: x[1])
95 for task_id, _ in tasks_by_allocation:
96 selectors = timetable.get_firing_rule_selectors_for_task(task_id, rule_type=RULE_TYPE.SIZE)
97 # NOTE: We do NOT try to add a new size rule here,
98 # because in this action we try to reduce batching
99 for selector in select_variants(store, selectors):
100 duration_fn = store.constraints.get_duration_fn_for_task(task_id)
101 yield (
102 ModifySizeRuleBaseAction.get_default_rating(),
103 ModifySizeRuleByHighAllocationAction(
104 ModifySizeRuleByAllocationActionParamsType(
105 rule=selector,
106 size_increment=-1,
107 duration_fn=duration_fn,
108 )
109 ),
110 )