Skip to content

Commit

Permalink
reopt_jl : added solver tolerance for HiGHS
Browse files Browse the repository at this point in the history
  • Loading branch information
lilycatolson committed Mar 5, 2024
1 parent ac7262a commit 61f4d65
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
3 changes: 2 additions & 1 deletion omf/models/microgridDesign.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 5 additions & 4 deletions omf/solvers/reopt_jl/REoptSolver/src/REoptSolver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Expand Down
6 changes: 3 additions & 3 deletions omf/solvers/reopt_jl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -290,15 +290,15 @@ 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"))
os.system(f'''julia --project="{project_path}" -e '
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)
Expand Down

0 comments on commit 61f4d65

Please sign in to comment.