-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
195 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# This source code is licensed under the CC-BY-NC license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import pandas as pd | ||
|
||
# Carbon Aware Scheduling Algorithm | ||
# takes a dataframe that contains renewable and dc power, dc_all | ||
# applies cas within the flexible_workload_ratio, and max_capacity constraints | ||
# returns the carbon balanced version of the input dataframe, balanced_df | ||
def cas(df_all, flexible_workload_ratio, max_capacity): | ||
# work on 24 hour basis | ||
# sort the df in terms of ascending renewable en | ||
# take flexible_workload_ratio from the highest carbon intensity hours | ||
# to lowest ones if there is not enough renewables until max_capacity is hit | ||
balanced_df = [] | ||
for i in range(0, df_all.shape[0], 24): | ||
sorted_df = df_all[i : i + 24].sort_values( | ||
by=["tot_renewable", "avg_dc_power_mw"] | ||
) | ||
start = 0 | ||
end = 23 | ||
if sorted_df.shape[0] < 23: | ||
break | ||
work_to_move = 0 | ||
while start < end: | ||
renewable_surplus = ( | ||
sorted_df["tot_renewable"].iloc[end] | ||
- sorted_df["avg_dc_power_mw"].iloc[end] | ||
) | ||
renewable_gap = ( | ||
sorted_df["avg_dc_power_mw"].iloc[start] | ||
- sorted_df["tot_renewable"].iloc[start] | ||
) | ||
available_space = min( | ||
renewable_surplus, | ||
(max_capacity - sorted_df["avg_dc_power_mw"].iloc[end]), | ||
) | ||
if renewable_surplus <= 0: | ||
end = end - 1 | ||
continue | ||
if renewable_gap <= 0: | ||
start = start + 1 | ||
continue | ||
if work_to_move <= 0 and renewable_gap > 0: | ||
work_to_move = min( | ||
renewable_gap, | ||
( | ||
flexible_workload_ratio | ||
/ 100 | ||
* sorted_df["avg_dc_power_mw"].iloc[start] | ||
), | ||
) | ||
|
||
if available_space > work_to_move: | ||
sorted_df["avg_dc_power_mw"].iloc[end] = ( | ||
sorted_df["avg_dc_power_mw"].iloc[end] | ||
+ work_to_move | ||
) | ||
sorted_df["avg_dc_power_mw"].iloc[start] = ( | ||
sorted_df["avg_dc_power_mw"].iloc[start] | ||
- work_to_move | ||
) | ||
start = start + 1 | ||
work_to_move = 0 | ||
else: | ||
sorted_df["avg_dc_power_mw"].iloc[end] = ( | ||
sorted_df["avg_dc_power_mw"].iloc[end] | ||
+ available_space | ||
) | ||
sorted_df["avg_dc_power_mw"].iloc[start] = ( | ||
sorted_df["avg_dc_power_mw"].iloc[start] | ||
- available_space | ||
) | ||
work_to_move = work_to_move - available_space | ||
end = end - 1 | ||
balanced_df.append(sorted_df) | ||
|
||
final_balanced_df = pd.concat(balanced_df).sort_values(by=["index"]) | ||
return final_balanced_df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters