-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrating m3sa changes with opendc
- Loading branch information
1 parent
52f3738
commit be9011f
Showing
17 changed files
with
3,193 additions
and
1,128 deletions.
There are no files selected for viewing
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
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
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,41 @@ | ||
/* | ||
* Copyright (c) 2020 AtLarge Research | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
description = "Multi-Meta-Model Simulation Analysis (M3SA) used across OpenDC modules" | ||
|
||
// Build configuration | ||
plugins { | ||
`kotlin-library-conventions` | ||
} | ||
|
||
dependencies { | ||
api(libs.kotlinx.coroutines) | ||
|
||
testImplementation(projects.opendcSimulator.opendcSimulatorCore) | ||
} | ||
|
||
sourceSets { | ||
main { | ||
kotlin.srcDirs("src/main/kotlin", "src/main/python") | ||
resources.srcDir("src/main/resources") | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
opendc-experiments/opendc-m3sa/src/main/kotlin/M3saRunner.kt
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,56 @@ | ||
/* | ||
* Copyright (c) 2024 AtLarge Research | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
import kotlin.io.path.Path | ||
|
||
/** | ||
* This constant variable should be changed depending on the root folder that is being run. | ||
* PATH_TO_PYTHON_MAIN should point to the main python file, ran when the analysis starts. | ||
*/ | ||
|
||
public val ANALYSIS_SCRIPTS_DIRECTORY: String = "./opendc-experiments/opendc-m3sa/src/main/python" | ||
public val ABSOLUTE_SCRIPT_PATH: String = | ||
Path("$ANALYSIS_SCRIPTS_DIRECTORY/main.py").toAbsolutePath().normalize().toString() | ||
public val SCRIPT_LANGUAGE: String = "python3" | ||
|
||
public fun m3saRun( | ||
outputFolderPath: String, | ||
m3saSetupPath: String, | ||
) { | ||
val process = | ||
ProcessBuilder( | ||
SCRIPT_LANGUAGE, | ||
ABSOLUTE_SCRIPT_PATH, | ||
outputFolderPath, | ||
m3saSetupPath, | ||
) | ||
.directory(Path(ANALYSIS_SCRIPTS_DIRECTORY).toFile()) | ||
.start() | ||
|
||
val exitCode = process.waitFor() | ||
if (exitCode == 0) { | ||
println("[M3SA says] M3SA operation(s) completed successfully.") | ||
} else { | ||
val errors = process.errorStream.bufferedReader().readText() | ||
println("[M3SA says] Exit code $exitCode; Error(s): $errors") | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
opendc-experiments/opendc-m3sa/src/main/python/accuracy_evaluator.py
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,114 @@ | ||
import numpy as np | ||
|
||
from models.MetaModel import MetaModel | ||
|
||
|
||
def accuracy_evaluator( | ||
real_data, | ||
multi_model, | ||
compute_mape=True, | ||
compute_nad=True, | ||
compute_rmsle=True, | ||
rmsle_hyperparameter=0.5, | ||
only_metamodel=False | ||
): | ||
""" | ||
:param real_data: the real-world data of the simulation | ||
:param multi_model: the Multi-Model, containing individual models (possibly also a Meta-Model, with id=101) | ||
:param MAPE: whether to calculate Mean Absolute Percentage Error (MAPE) | ||
:param NAD: whether to calculate Normalized Absolute Differences (NAD) | ||
:param RMSLE: whether to calculate Root Mean Square Logarithmic Error (RMSLE) | ||
:param rmsle_hyperparameter: the hyperparameter that balances the ration underestimations:overestimations | ||
- default is 0.5 (balanced penalty) | ||
- < 0.5: more penalty for overestimations | ||
- > 0.5: more penalty for underestimations | ||
e.g., RMSLE_hyperparameter=0.3 -> 30% penalty for overestimations, 70% penalty for underestimations (3:7 ratio) | ||
:return: None, but prints the accuracy metrics | ||
""" | ||
|
||
meta_model = MetaModel(multimodel=multi_model) | ||
multi_model.models.append(meta_model.meta_model) # metamodel | ||
# multi_model.models.append(Model(raw_host_data=real_data, id=-1, path=None)) # real-world data | ||
|
||
with open(multi_model.output_folder_path + "/accuracy_report.txt", "a") as f: | ||
f.write("====================================\n") | ||
f.write("Accuracy Report, against ground truth\n") | ||
|
||
for model in multi_model.models: | ||
if only_metamodel and model.id != 101: | ||
continue | ||
|
||
if model.id == -1: | ||
f.write("Real-World data") | ||
elif model.id == 101: | ||
f.write( | ||
f"Meta-Model, meta-function: {multi_model.user_input['meta_function']}, window_size: {meta_model.multi_model.window_size}") | ||
else: | ||
f.write(f"Model {model.id}") | ||
|
||
simulation_data = model.raw_sim_data | ||
min_len = min(len(real_data), len(simulation_data)) | ||
real_data = real_data[:min_len] | ||
simulation_data = simulation_data[:min_len] | ||
if compute_mape: | ||
accuracy_mape = mape( | ||
real_data=real_data, | ||
simulation_data=simulation_data | ||
) | ||
f.write(f"\nMean Absolute Percentage Error (MAPE): {accuracy_mape}%") | ||
|
||
if compute_nad: | ||
accuracy_nad = nad( | ||
real_data=real_data, | ||
simulation_data=simulation_data | ||
) | ||
f.write(f"\nNormalized Absolute Differences (NAD): {accuracy_nad}%") | ||
|
||
if compute_rmsle: | ||
accuracy_rmsle = rmsle( | ||
real_data=real_data, | ||
simulation_data=simulation_data, | ||
alpha=rmsle_hyperparameter | ||
) | ||
f.write( | ||
f"\nRoot Mean Square Logarithmic Error (RMSLE), alpha={rmsle_hyperparameter}:{accuracy_rmsle}\n\n") | ||
|
||
f.write("====================================\n") | ||
|
||
|
||
def mape(real_data, simulation_data): | ||
""" | ||
Calculate Mean Absolute Percentage Error (MAPE) | ||
:param real_data: Array of real values | ||
:param simulation_data: Array of simulated values | ||
:return: MAPE value | ||
""" | ||
real_data = np.array(real_data) | ||
simulation_data = np.array(simulation_data) | ||
return round(np.mean(np.abs((real_data - simulation_data) / real_data)) * 100, 3) | ||
|
||
|
||
def nad(real_data, simulation_data): | ||
""" | ||
Calculate Normalized Absolute Differences (NAD) | ||
:param real_data: Array of real values | ||
:param simulation_data: Array of simulated values | ||
:return: NAD value | ||
""" | ||
real_data = np.array(real_data) | ||
simulation_data = np.array(simulation_data) | ||
return round(np.sum(np.abs(real_data - simulation_data)) / np.sum(real_data) * 100, 3) | ||
|
||
|
||
def rmsle(real_data, simulation_data, alpha=0.5): | ||
""" | ||
Calculate Root Mean Square Logarithmic Error (RMSLE) with an adjustable alpha parameter | ||
:param real_data: Array of real values | ||
:param simulation_data: Array of simulated values | ||
:param alpha: Hyperparameter that balances the penalty between underestimations and overestimations | ||
:return: RMSLE value | ||
""" | ||
real_data = np.array(real_data) | ||
simulation_data = np.array(simulation_data) | ||
log_diff = alpha * np.log(real_data) - (1 - alpha) * np.log(simulation_data) | ||
return round(np.sqrt(np.mean(log_diff ** 2)) * 100, 3) |
Oops, something went wrong.