From 61f4d6558cd7cacec119245104a1594cd63456b0 Mon Sep 17 00:00:00 2001 From: Lily Olson Date: Tue, 5 Mar 2024 08:38:20 -0500 Subject: [PATCH] reopt_jl : added solver tolerance for HiGHS --- omf/models/microgridDesign.py | 3 ++- omf/solvers/reopt_jl/REoptSolver/src/REoptSolver.jl | 9 +++++---- omf/solvers/reopt_jl/__init__.py | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/omf/models/microgridDesign.py b/omf/models/microgridDesign.py index 72b325fba..fd0f1c923 100644 --- a/omf/models/microgridDesign.py +++ b/omf/models/microgridDesign.py @@ -112,6 +112,7 @@ def work(modelDir, inputDict): dieselOMCostKw = float(inputDict['dieselOMCostKw']) dieselOMCostKwh = float(inputDict['dieselOMCostKwh']) dieselOnlyRunsDuringOutage = bool(inputDict['dieselOnlyRunsDuringOutage']) + tolerance = float(inputDict['solverTolerance']) loadShape = np.array(load_df) criticalLoadShape = np.array(critical_load_df) numRows = loadShape.shape[0] @@ -281,7 +282,7 @@ def work(modelDir, inputDict): json.dump(scenario, jsonFile) # Run REopt API script *** => switched to REopt.jl - reopt_jl.run_reopt_jl(modelDir, "Scenario_test_POST.json", outages=run_outages, max_runtime_s = max_runtime ) + reopt_jl.run_reopt_jl(modelDir, "Scenario_test_POST.json", outages=run_outages, max_runtime_s = max_runtime, tolerance = tolerance) with open(pJoin(modelDir, 'results.json')) as jsonFile: results = json.load(jsonFile) diff --git a/omf/solvers/reopt_jl/REoptSolver/src/REoptSolver.jl b/omf/solvers/reopt_jl/REoptSolver/src/REoptSolver.jl index b142a6ff3..41f3314c7 100644 --- a/omf/solvers/reopt_jl/REoptSolver/src/REoptSolver.jl +++ b/omf/solvers/reopt_jl/REoptSolver/src/REoptSolver.jl @@ -7,9 +7,10 @@ using Base.Filesystem using REopt, JuMP, JSON, HiGHS #SCIP #, Cbc #Ipopt, ECOS, Clp, GLPK -function get_model(path::String, max_runtime_s::Union{Nothing,Int}) +function get_model(path::String, max_runtime_s::Union{Nothing,Int}, tolerance::Float64) m = Model(HiGHS.Optimizer) set_attribute(m,"threads",20) + set_attribute(m,"mip_rel_gap",tolerance) if max_runtime_s != nothing set_attribute(m,"time_limit", float(max_runtime_s)) end @@ -25,12 +26,12 @@ function results_to_json(results, output_path) end function run(path::String, outages::Bool=false, microgrid_only::Bool=false, max_runtime_s::Union{Nothing,Int}=nothing, - api_key::String="WhEzm6QQQrks1hcsdN0Vrd56ZJmUyXJxTJFg6pn9") + api_key::String="WhEzm6QQQrks1hcsdN0Vrd56ZJmUyXJxTJFg6pn9", tolerance::Float64=0.05) ENV["NREL_DEVELOPER_API_KEY"]=api_key - m = get_model(path, max_runtime_s) - m2 = get_model(path, max_runtime_s) + m = get_model(path, max_runtime_s, tolerance) + m2 = get_model(path, max_runtime_s, tolerance) input_path = normpath(joinpath(path, "Scenario_test_POST.json")) reopt_inputs_path = normpath(joinpath(path,"REoptInputs.json")) output_path = normpath(joinpath(path,"results.json")) diff --git a/omf/solvers/reopt_jl/__init__.py b/omf/solvers/reopt_jl/__init__.py index 5fe467552..10b7325cc 100644 --- a/omf/solvers/reopt_jl/__init__.py +++ b/omf/solvers/reopt_jl/__init__.py @@ -253,7 +253,7 @@ def get_randomized_api_key(): #potential optional inputs (for solver): ratio_gap, threads, max_solutions, verbosity #potential other inputs (ease of use): load csv file path ("path_to_csv") => check if "loads_kw" already exists def run_reopt_jl(path, inputFile="", loadFile="", default=False, outages=False, microgrid_only=False, max_runtime_s=None, - run_with_sysimage=True ): + run_with_sysimage=True, tolerance=0.05 ): ''' calls 'run' function through run_reopt.jl (Julia file) ''' if inputFile == "" and not default: @@ -290,7 +290,7 @@ def run_reopt_jl(path, inputFile="", loadFile="", default=False, outages=False, os.system(f'''julia --sysimage="{sysimage_path}" -e ' using .REoptSolver; ENV["NREL_DEVELOPER_API_KEY"]="{api_key}"; - REoptSolver.run("{path}", {outages_jl}, {microgrid_only_jl}, {max_runtime_s_jl}, "{api_key}") + REoptSolver.run("{path}", {outages_jl}, {microgrid_only_jl}, {max_runtime_s_jl}, "{api_key}", {tolerance}) ' ''') else: project_path = os.path.normpath(os.path.join(thisDir,"REoptSolver")) @@ -298,7 +298,7 @@ def run_reopt_jl(path, inputFile="", loadFile="", default=False, outages=False, using Pkg; Pkg.instantiate(); import REoptSolver; ENV["NREL_DEVELOPER_API_KEY"]="{api_key}"; - REoptSolver.run("{path}", {outages_jl}, {microgrid_only_jl}, {max_runtime_s_jl}, "{api_key}") + REoptSolver.run("{path}", {outages_jl}, {microgrid_only_jl}, {max_runtime_s_jl}, "{api_key}", {tolerance}) ' ''') except Exception as e: print(e)