Created
February 20, 2024 10:19
-
-
Save willu47/24aca5c92b6d09ad31d434c7ff31f4dc to your computer and use it in GitHub Desktop.
Extract the dual values for the E8_AnnualEmissionsLimit constraint from the CPLEX solution of an OSeMOSYS model
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
import pandas as pd | |
import sys | |
from logging import getLogger | |
logger = getLogger(__name__) | |
def main(file_path: str) -> pd.DataFrame: | |
"""Read CPLEX solution file and return dual values for E8_AnnualEmissionsLimit constraint. | |
""" | |
df = pd.read_xml(file_path, xpath=".//constraint", parser="etree") | |
logger.info(f"Reading CPLEX solution file from {file_path}") | |
df[["Variable", "Index"]] = df["name"].str.split("(", expand=True) | |
df["Index"] = df["Index"].str.replace(")", "", regex=False) | |
df[['Variable', 'Index', 'index', 'status', 'slack', 'dual']][df['Variable'] == 'E8_AnnualEmissionsLimit'] | |
return df | |
if __name__ == "__main__": | |
args = sys.argv[1:] | |
if len(args) != 1: | |
logger.error("Please provide the path to the CPLEX solution file e.g. python get_cplex_dual.py my_solution_file.sol") | |
sys.exit(1) | |
file_path = args[0] | |
df = main(file_path) | |
logger.info("Writing CPLEX solution file to E8_AnnualEmissionsLimit.csv") | |
df.to_csv('E8_AnnualEmissionsLimit.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment