Coverage for o2/actions/legacy_optimos_actions/remove_resource_by_cost_action.py: 100%
16 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 dataclasses import dataclass
3from o2.actions.base_actions.base_action import RateSelfReturnType
4from o2.actions.base_actions.modify_resource_base_action import (
5 ModifyResourceBaseAction,
6 ModifyResourceBaseActionParamsType,
7)
8from o2.models.solution import Solution
9from o2.store import Store
12class RemoveResourceByCostActionParamsType(ModifyResourceBaseActionParamsType):
13 """Parameter for `RemoveResourceByCostAction`."""
16@dataclass(frozen=True)
17class RemoveResourceByCostAction(ModifyResourceBaseAction, str=False):
18 """`RemoveResourceByCostAction` will remove the resource with the highest cost.
20 This action is based on the original optimos implementation,
21 see `resolve_remove_resources_in_process` in that project.
23 It first gets all resources sorted by their cost (cost/hour * available_time),
24 then it removes the resource with the highest cost, if that resource's tasks
25 can be done by other resources.
26 """
28 @staticmethod
29 def rate_self(store: Store, input: "Solution") -> RateSelfReturnType:
30 """Generate a best set of parameters & self-evaluates this action."""
31 resources = store.solution.state.timetable.get_resources_with_cost()
32 for resource, _cost in resources:
33 if not resource.can_safely_be_removed(store.solution.state.timetable):
34 continue
35 yield (
36 RemoveResourceByCostAction.get_default_rating(store),
37 RemoveResourceByCostAction(
38 RemoveResourceByCostActionParamsType(
39 resource_id=resource.id,
40 remove_resource=True,
41 )
42 ),
43 )
45 return