Skip to content

Commit

Permalink
ucjl add profile
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquimg committed Dec 9, 2024
1 parent 228faca commit 0c3a097
Showing 1 changed file with 46 additions and 61 deletions.
107 changes: 46 additions & 61 deletions UnitCommitment/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import UnitCommitment
import SHA
import Dates

# helper functions
include("../utils/utils.jl")
include("../utils/profile.jl")
# !!! TYPE PIRACY TO INTERCEPT ALL HIGHS SOLVES AND WRITE THEM TO FILES !!!
include("../utils/highs_write.jl")

function print_help()
valid_cases = uc_cases()
print(
Expand All @@ -41,48 +47,6 @@ function print_help()
return
end

function _parse_args(args)
ret = Dict{String,String}()
for arg in args
if (m = match(r"--([a-z]+)=(.+?)($|\s)", arg)) !== nothing
ret[m[1]] = m[2]
elseif (m = match(r"--([a-z]+?)($|\s)", arg)) !== nothing
ret[m[1]] = "true"
else
error("unsupported argument $arg")
end
end
return ret
end

const HIGHS_WRITE_FILE_PREFIX = Ref{String}("UNNAMED")

function _write_highs_model(highs)
prefix = HIGHS_WRITE_FILE_PREFIX[]::String
if isempty(prefix)
return
end
instances = joinpath(dirname(@__DIR__), "instances")
tmp_filename = joinpath(instances, "tmp.mps")
HiGHS.Highs_writeModel(highs, tmp_filename)
# We SHA the raw file so that potential gzip differences across
# platforms don't matter.
hex = bytes2hex(open(SHA.sha256, tmp_filename))
run(`gzip $tmp_filename`)
mv(
"$(tmp_filename).gz",
joinpath(instances, "$prefix-$hex.mps.gz");
force = true,
)
return
end

# !!! TYPE PIRACY TO INTERCEPT ALL HIGHS SOLVES AND WRITE THEM TO FILES !!!
function HiGHS.Highs_run(highs)
_write_highs_model(highs)
return ccall((:Highs_run, HiGHS.libhighs), Cint, (Ptr{Cvoid},), highs)
end

function uc_cases()
return [
"pglib-uc/ferc/2015-01-01_hw",
Expand All @@ -98,6 +62,25 @@ function uc_cases()
]
end

function build_and_solve(instance; time_limit = 600.0)
model = UnitCommitment.build_model(;
instance = instance,
# optimizer = HiGHS.Optimizer,
optimizer = JuMP.optimizer_with_attributes(
HiGHS.Optimizer,
"mip_rel_gap" => 0.01,
"time_limit" => time_limit,
),
)
# iterative model, so we do no write files
HIGHS_WRITE_FILE_PREFIX[] = ""
UnitCommitment.optimize!(
model,
UnitCommitment.XavQiuWanThi2019.Method(; time_limit = time_limit),
)
return
end

function main(args)
parsed_args = _parse_args(args)
if get(parsed_args, "help", "false") == "true"
Expand All @@ -113,30 +96,32 @@ function main(args)
for case in cases
@info("Running $case")
if get(parsed_args, "run", "false") == "true"
model_name = "UnitCommitment_$(replace(case, "/" => "_"))"
try
# Read benchmark instance
instance = UnitCommitment.read_benchmark(case)
# Construct model (using state-of-the-art defaults)
model = UnitCommitment.build_model(;
instance = instance,
# optimizer = HiGHS.Optimizer,
optimizer = JuMP.optimizer_with_attributes(
HiGHS.Optimizer,
"mip_rel_gap" => 0.01,
"time_limit" => 600.0,
),
)
# iterative model, so we do no write files
HIGHS_WRITE_FILE_PREFIX[] = ""
UnitCommitment.optimize!(
model,
UnitCommitment.XavQiuWanThi2019.Method(;
time_limit = 600.0,
),
)
if get(parsed_args, "profile", "false") == "true"
build_and_solve(instance; time_limit = 0.01)
data =
@proflist build_and_solve(instance; time_limit = 0.01) [
JuMP,
HiGHS,
:Highs_run,
]
save_proflist(
data;
output_filename = joinpath(
dirname(@__DIR__),
"profile.jsonl",
),
label = model_name,
)
else
build_and_solve(instance; time_limit = 600.0)
end
# we only write files for the last model
if write_files
HIGHS_WRITE_FILE_PREFIX[] = "UnitCommitment_$(replace(case, "/" => "_"))"
HIGHS_WRITE_FILE_PREFIX[] = model_name
JuMP.set_time_limit_sec(model, 0.0)
JuMP.optimize!(model)
end
Expand Down

0 comments on commit 0c3a097

Please sign in to comment.