Skip to content

Commit

Permalink
added reopt_jl functionality to run without sysimage on build errors;…
Browse files Browse the repository at this point in the history
… added additional error handling for der-cam solver
  • Loading branch information
lilycatolson committed Jul 1, 2024
1 parent 7a0da03 commit 99bd134
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 deletions.
76 changes: 41 additions & 35 deletions omf/solvers/der_cam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,50 +335,56 @@ def solve_model(path, modelFile, apiKey="", timeout=0):
lock_path = os.path.normpath(os.path.join(thisDir,lock_file))
check_lock(lock_path,timeout)

if not os.path.exists(lock_path):
with open(lock_path, 'w') as f:
f.write(str(os.getpid()))
print(f"lock acquired at: {lock_path}")
#posts model to API
response = req.post(url=urlRequest, data=data, files=files)
start_time = time.time()
#print(f"response.status_code: {response.status_code}, response.reason: {response.reason}")
modelResponse = response.json()['model']
#acquires key for given model
modelKey = modelResponse['model_key']

modelHasResults, modelStatus, modelMsg = check_model_status( modelKey, userKey )
if modelStatus == "failed":
release_lock(start_time, lock_path)
print(f"error: {modelMsg}")
return
start_time = time.time()
modelKey = None
try:
if not os.path.exists(lock_path):
with open(lock_path, 'w') as f:
f.write(str(os.getpid()))
print(f"lock acquired at: {lock_path}")
#posts model to API
response = req.post(url=urlRequest, data=data, files=files)

modelResponse = response.json()['model']
#acquires key for given model
modelKey = modelResponse['model_key']

#waits for model to solve
while modelHasResults != 1:
#add waiting period between checks? (doesn't seem to have limits on requests, only posting models)
modelHasResults, modelStatus, modelMsg = check_model_status( modelKey, userKey )
if modelStatus == "failed":
release_lock(start_time, lock_path)
print(f"error: {modelMsg}")
return
if check_timeout(start_time, timeout):
release_lock(start_time, lock_path)
print(f"error: timeout of {timeout} seconds reached while waiting for model results")
print(f"results available: {modelHasResults}, status: {modelStatus}, msg: {modelMsg}")
return

solvedModel = get_model_results( modelKey, userKey, modelHasResults )

#todo: add optional function inputs for output file names
with open(os.path.normpath(os.path.join(path,"results.csv")), 'w') as f:
f.write(solvedModel['results'])
#waits for model to solve
while modelHasResults != 1:
#add waiting period between checks? (doesn't seem to have limits on requests, only posting models)
modelHasResults, modelStatus, modelMsg = check_model_status( modelKey, userKey )
if modelStatus == "failed":
release_lock(start_time, lock_path)
print(f"error: {modelMsg}")
return
if check_timeout(start_time, timeout):
release_lock(start_time, lock_path)
print(f"error: timeout of {timeout} seconds reached while waiting for model results")
print(f"results available: {modelHasResults}, status: {modelStatus}, msg: {modelMsg}")
return

solvedModel = get_model_results( modelKey, userKey, modelHasResults )

#with open(os.path.normpath(os.path.join(path,"results_nodes.csv")), 'w') as f:
# f.write(solvedModel['resultsNodes'])

print(f'model competed: results saved to {path}/results.csv') #and {path}/resultsNodes.csv')
#todo: add optional function inputs for output file names
with open(os.path.normpath(os.path.join(path,"results.csv")), 'w') as f:
f.write(solvedModel['results'])

#with open(os.path.normpath(os.path.join(path,"results_nodes.csv")), 'w') as f:
# f.write(solvedModel['resultsNodes'])

print(f'model competed: results saved to {path}/results.csv') #and {path}/resultsNodes.csv')

release_lock(start_time, lock_path)
except Exception as e:
print(f'Exception occured during der_cam solve_model : {e}')
release_lock(start_time,lock_path)

release_lock(start_time, lock_path)
return modelKey

def print_existing_models():
Expand Down
25 changes: 19 additions & 6 deletions omf/solvers/reopt_jl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ def install_reopt_jl(system : list = platform.system(), build_sysimage=True):
precompile_path = str(os.path.normpath(os.path.join(thisDir,"precompile_reopt.jl")))

if os.path.isfile(instantiated_path):
print("reopt_jl dependencies installed - to reinstall remove file: instantiated.txt")
if not os.path.isfile(sysimage_path) and build_sysimage:
print("error: reopt_jl sysimage not found - remove instantiated.txt to build")
return
print("error: reopt_jl.so not found - remove instantiated.txt to build \n attempting to run without sysimage... ")
return False
else:
print("reopt_jl dependencies installed - to reinstall remove instantiated.txt")
return build_sysimage

try:
install_pyjulia = [
Expand Down Expand Up @@ -76,10 +78,20 @@ def install_reopt_jl(system : list = platform.system(), build_sysimage=True):
commands += [ f'copy nul {instantiated_path}' ]
else:
raise ValueError(f'No installation script available yet for {system}')

for command in commands:
os.system(command)

return build_sysimage

except (ImportError, OSError) as e:
if build_sysimage:
print(f"error while building reopt_jl.so: {e} \n attempting install again without building sysimage... ")
return install_reopt_jl(build_sysimage=False)
else:
print(e)
return

except Exception as e:
print(e)
return
Expand Down Expand Up @@ -260,7 +272,8 @@ def run_reopt_jl(path, inputFile="", loadFile="", default=False, outages=False,
print("Invalid inputs: inputFile needed if default=False")
return

install_reopt_jl(build_sysimage=run_with_sysimage)
#runs reopt differently based on success of sysimage build
built_sysimage = install_reopt_jl(build_sysimage=run_with_sysimage)

constant_file = "Scenario_test_POST.json"
constant_path = os.path.normpath(os.path.join(path,constant_file))
Expand All @@ -285,7 +298,7 @@ def run_reopt_jl(path, inputFile="", loadFile="", default=False, outages=False,

api_key = get_randomized_api_key()

if run_with_sysimage:
if built_sysimage:
sysimage_path = os.path.normpath(os.path.join(thisDir,"reopt_jl.so"))
command = f'''julia --sysimage="{sysimage_path}" -e '
using .REoptSolver;
Expand Down

0 comments on commit 99bd134

Please sign in to comment.