Created
February 15, 2022 10:04
-
-
Save 270ajay/63bda92329ee891eeb07e65f53d77e1e to your computer and use it in GitHub Desktop.
Lp sol file generator for debugging mathematical models
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
"""Creates a file which contains info from lp and sol file. | |
---------------- | |
Sample sol file: | |
---------------- | |
# Solution for model Mathematical Model | |
# Objective value = 140 | |
x1 10 | |
x2 20 | |
x3 30 | |
--------------- | |
Sample lp file: | |
--------------- | |
\ Model Mathematical Model | |
\ LP format - for model browsing. Use MPS format to capture full model detail. | |
Minimize | |
x1 + 2 x2 + 3 x3 | |
Subject To | |
ct1: x1 + x2 >= 25 | |
ct2: x1 + x3 >= 25 | |
Bounds | |
10 <= x1 <= 50 | |
20 <= x2 <= 45 | |
30 <= x3 <= 35 | |
End | |
-------------------------------------- | |
Output file generated by this program: | |
-------------------------------------- | |
\ Model Mathematical Model | |
\ LP format - for model browsing. Use MPS format to capture full model detail. | |
Minimize | |
x1 [10.0] + 2 x2 [20.0] + 3 x3 [30.0] | |
Subject To | |
ct1: x1 [10.0] + x2 [20.0] >= 25 | |
ct2: x1 [10.0] + x3 [30.0] >= 25 | |
Bounds | |
10 <= x1 [10.0] <= 50 | |
20 <= x2 [20.0] <= 45 | |
30 <= x3 [30.0] <= 35 | |
End | |
""" | |
# ------------------------------------------------------------------------------ | |
# CONSTANTS | |
# ------------------------------------------------------------------------------ | |
# Enter the sol file name here | |
_SOL_FILE_NAME = "sol_file.sol" | |
# Enter the first n lines that should not be read | |
_SKIP_FIRST_N_LINES_IN_SOL_FILE = 2 | |
# Enter the lp file name here | |
_LP_FILE_NAME = "lp_file.lp" | |
# Enter the output file name here | |
_OUTPUT_FILE_NAME = "lp_sol_file.txt" | |
# Symbols enclosing the solution in the output file | |
_LEFT_SYMBOL_TO_SOL = "[" | |
_RIGHT_SYMBOL_TO_SOL = "]" | |
def main(): | |
"""Entry point of the program.""" | |
print("Running...") | |
val_for_var = _create_var_val_dict(_SOL_FILE_NAME, | |
_SKIP_FIRST_N_LINES_IN_SOL_FILE) | |
_create_txt_file_from_lp_sol_info(val_for_var, _OUTPUT_FILE_NAME, | |
_LP_FILE_NAME) | |
print(f"{_OUTPUT_FILE_NAME} is created with lp expressions and values") | |
def _create_var_val_dict(file_name, skip_first_n_lines): | |
"""Returns a dictionary with key: variable name, value: value assigned.""" | |
sol_file_info_List = _get_list_of_info_from_file(file_name, | |
skip_first_n_lines) | |
val_for_var = {} | |
for sol_info in sol_file_info_List: | |
var, val = sol_info.split(" ") | |
val_for_var[var] = float(val) | |
return val_for_var | |
def _create_txt_file_from_lp_sol_info(val_for_var, txt_file_name, lp_file_name): | |
"""Creates txt file from lp file.""" | |
with open(f"{txt_file_name}", 'w') as file: | |
lp_file_list = _get_list_of_info_from_file(lp_file_name) | |
for lp_info in lp_file_list: | |
symbols_list = lp_info.split(" ") | |
new_symbols_list = [] | |
for symbol in symbols_list: | |
if symbol in val_for_var: | |
new_symbols_list.append(f"{symbol} {_LEFT_SYMBOL_TO_SOL}" | |
f"{val_for_var[symbol]}" | |
f"{_RIGHT_SYMBOL_TO_SOL}") | |
else: | |
new_symbols_list.append(f"{symbol}") | |
new_symbols_list.append("\n") | |
file.write(" ".join(new_symbols_list)) | |
def _get_list_of_info_from_file(file_name, skip_first_n_lines=0): | |
"""Reads and returns list of information from file.""" | |
with open(file_name) as f: | |
if skip_first_n_lines < 1: | |
return f.read().splitlines() | |
return f.read().splitlines()[skip_first_n_lines:] | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment