From 40270b0030e89467ab02d03827a4c1e3ad20edeb Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Fri, 19 May 2023 15:27:54 -0500 Subject: [PATCH 1/5] Make test/ a standalone project --- .github/workflows/test.yml | 33 ++-- Makefile | 14 +- deps/formatter/Project.toml | 5 - deps/formatter/format.jl | 9 - juliaw | 75 -------- test/Project.toml | 22 +-- test/import/egret_test.jl | 21 --- test/instance/migrate_test.jl | 22 --- test/instance/read_test.jl | 166 ----------------- test/lmp/aelmp_test.jl | 35 ---- test/lmp/conventional_test.jl | 51 ------ test/model/formulations_test.jl | 84 --------- test/runtests.jl | 46 ----- .../methods/XavQiuWanThi19/filter_test.jl | 83 --------- .../methods/XavQiuWanThi19/find_test.jl | 37 ---- .../XavQiuWanThi19/sensitivity_test.jl | 147 --------------- test/src/UnitCommitmentT.jl | 58 ++++++ test/src/import/egret_test.jl | 23 +++ test/src/instance/migrate_test.jl | 24 +++ test/src/instance/read_test.jl | 168 ++++++++++++++++++ test/src/lmp/aelmp_test.jl | 40 +++++ test/src/lmp/conventional_test.jl | 53 ++++++ test/src/model/formulations_test.jl | 86 +++++++++ .../methods/XavQiuWanThi19/filter_test.jl | 86 +++++++++ .../methods/XavQiuWanThi19/find_test.jl | 39 ++++ .../XavQiuWanThi19/sensitivity_test.jl | 149 ++++++++++++++++ test/src/transform/initcond_test.jl | 30 ++++ .../transform/randomize/XavQiuAhm2021_test.jl | 106 +++++++++++ test/src/transform/slice_test.jl | 68 +++++++ test/src/usage.jl | 66 +++++++ test/src/validation/repair_test.jl | 43 +++++ test/transform/initcond_test.jl | 28 --- .../transform/randomize/XavQiuAhm2021_test.jl | 83 --------- test/transform/slice_test.jl | 66 ------- test/usage.jl | 62 ------- test/validation/repair_test.jl | 39 ---- 36 files changed, 1070 insertions(+), 1097 deletions(-) delete mode 100644 deps/formatter/Project.toml delete mode 100644 deps/formatter/format.jl delete mode 100755 juliaw delete mode 100644 test/import/egret_test.jl delete mode 100644 test/instance/migrate_test.jl delete mode 100644 test/instance/read_test.jl delete mode 100644 test/lmp/aelmp_test.jl delete mode 100644 test/lmp/conventional_test.jl delete mode 100644 test/model/formulations_test.jl delete mode 100644 test/runtests.jl delete mode 100644 test/solution/methods/XavQiuWanThi19/filter_test.jl delete mode 100644 test/solution/methods/XavQiuWanThi19/find_test.jl delete mode 100644 test/solution/methods/XavQiuWanThi19/sensitivity_test.jl create mode 100644 test/src/UnitCommitmentT.jl create mode 100644 test/src/import/egret_test.jl create mode 100644 test/src/instance/migrate_test.jl create mode 100644 test/src/instance/read_test.jl create mode 100644 test/src/lmp/aelmp_test.jl create mode 100644 test/src/lmp/conventional_test.jl create mode 100644 test/src/model/formulations_test.jl create mode 100644 test/src/solution/methods/XavQiuWanThi19/filter_test.jl create mode 100644 test/src/solution/methods/XavQiuWanThi19/find_test.jl create mode 100644 test/src/solution/methods/XavQiuWanThi19/sensitivity_test.jl create mode 100644 test/src/transform/initcond_test.jl create mode 100644 test/src/transform/randomize/XavQiuAhm2021_test.jl create mode 100644 test/src/transform/slice_test.jl create mode 100644 test/src/usage.jl create mode 100644 test/src/validation/repair_test.jl delete mode 100644 test/transform/initcond_test.jl delete mode 100644 test/transform/randomize/XavQiuAhm2021_test.jl delete mode 100644 test/transform/slice_test.jl delete mode 100644 test/usage.jl delete mode 100644 test/validation/repair_test.jl diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a217247..094770a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: Tests +name: Build & Test on: push: pull_request: @@ -6,19 +6,30 @@ on: - cron: '45 10 * * *' jobs: test: + name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} runs-on: ${{ matrix.os }} strategy: matrix: - julia-version: ['1.6', '1.7'] - julia-arch: [x64] - os: [ubuntu-latest, windows-latest, macOS-latest] - exclude: - - os: macOS-latest - julia-arch: x86 + version: ['1.6', '1.7', '1.8', '1.9'] + os: + - ubuntu-latest + arch: + - x64 steps: - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@latest + - uses: julia-actions/setup-julia@v1 with: - version: ${{ matrix.julia-version }} - - uses: julia-actions/julia-buildpkg@latest - - uses: julia-actions/julia-runtest@latest + version: ${{ matrix.version }} + arch: ${{ matrix.arch }} + - name: Run tests + shell: julia --color=yes --project=test {0} + run: | + using Pkg + Pkg.develop(path=".") + Pkg.update() + using UnitCommitmentT + try + runtests() + catch + exit(1) + end \ No newline at end of file diff --git a/Makefile b/Makefile index 41f1275..d689bad 100644 --- a/Makefile +++ b/Makefile @@ -4,20 +4,8 @@ VERSION := 0.3 -clean: - rm -rfv build Manifest.toml test/Manifest.toml deps/formatter/build deps/formatter/Manifest.toml - docs: cd docs; julia --project=. make.jl; cd .. rsync -avP --delete-after docs/build/ ../docs/$(VERSION)/ -format: - cd deps/formatter; ../../juliaw format.jl - -test: test/Manifest.toml - ./juliaw test/runtests.jl - -test/Manifest.toml: test/Project.toml - julia --project=test -e "using Pkg; Pkg.instantiate()" - -.PHONY: docs test format install-deps +.PHONY: docs diff --git a/deps/formatter/Project.toml b/deps/formatter/Project.toml deleted file mode 100644 index 4bc5f25..0000000 --- a/deps/formatter/Project.toml +++ /dev/null @@ -1,5 +0,0 @@ -[deps] -JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" - -[compat] -JuliaFormatter = "0.14.4" diff --git a/deps/formatter/format.jl b/deps/formatter/format.jl deleted file mode 100644 index 59ebd54..0000000 --- a/deps/formatter/format.jl +++ /dev/null @@ -1,9 +0,0 @@ -using JuliaFormatter -format( - [ - "../../src", - "../../test", - "../../benchmark/run.jl", - ], - verbose=true, -) diff --git a/juliaw b/juliaw deleted file mode 100755 index b78bc72..0000000 --- a/juliaw +++ /dev/null @@ -1,75 +0,0 @@ -#!/bin/bash -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -if [ ! -e Project.toml ]; then - echo "juliaw: Project.toml not found" - exit 1 -fi - -if [ ! -e Manifest.toml ]; then - julia --project=. -e 'using Pkg; Pkg.instantiate()' || exit 1 -fi - -if [ ! -e build/sysimage.so -o Project.toml -nt build/sysimage.so ]; then - echo "juliaw: rebuilding system image..." - - # Generate temporary project folder - rm -rf $HOME/.juliaw - mkdir -p $HOME/.juliaw/src - cp Project.toml Manifest.toml $HOME/.juliaw - NAME=$(julia -e 'using TOML; toml = TOML.parsefile("Project.toml"); "name" in keys(toml) && print(toml["name"])') - if [ ! -z $NAME ]; then - cat > $HOME/.juliaw/src/$NAME.jl << EOF -module $NAME -end -EOF - fi - - # Add PackageCompiler dependencies to temporary project - julia --project=$HOME/.juliaw -e 'using Pkg; Pkg.add(["PackageCompiler", "TOML", "Logging"])' - - # Generate system image scripts - cat > $HOME/.juliaw/sysimage.jl << EOF -using PackageCompiler -using TOML -using Logging - -Logging.disable_logging(Logging.Info) -mkpath("$PWD/build") - -println("juliaw: generating precompilation statements...") -run(\`julia --project="$PWD" --trace-compile="$PWD"/build/precompile.jl \$(ARGS)\`) - -println("juliaw: finding dependencies...") -project = TOML.parsefile("Project.toml") -manifest = TOML.parsefile("Manifest.toml") -deps = Symbol[] -for dep in keys(project["deps"]) - if dep in keys(manifest) - # Up to Julia 1.6 - dep_entry = manifest[dep][1] - else - # Julia 1.7+ - dep_entry = manifest["deps"][dep][1] - end - if "path" in keys(dep_entry) - println(" - \$(dep) [skip]") - else - println(" - \$(dep)") - push!(deps, Symbol(dep)) - end -end - -println("juliaw: building system image...") -create_sysimage( - deps, - precompile_statements_file = "$PWD/build/precompile.jl", - sysimage_path = "$PWD/build/sysimage.so", -) -EOF - julia --project=$HOME/.juliaw $HOME/.juliaw/sysimage.jl $* -else - julia --project=. --sysimage build/sysimage.so $* -fi diff --git a/test/Project.toml b/test/Project.toml index 5a175be..7b68ed9 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,3 +1,8 @@ +name = "UnitCommitmentT" +uuid = "a3b7a17a-ab64-45e4-a924-cd5ae7dc644e" +authors = ["Alinson S. Xavier "] +version = "0.1.0" + [deps] Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" @@ -6,21 +11,10 @@ GZip = "92fee26a-97fe-5a0c-ad85-20a5f3185b63" HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" JuMP = "4076af6c-e467-56ae-b986-b466b2749572" +JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" -Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" -PackageCompiler = "9b87118b-4619-50d2-8e1e-99f35a4d4d9d" -Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" -SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[compat] -DataStructures = "0.18" -Distributions = "0.25" -GZip = "0.5" -JSON = "0.21" -JuMP = "1" -MathOptInterface = "1" -PackageCompiler = "1" -julia = "1" +UnitCommitment = "64606440-39ea-11e9-0f29-3303a1d3d877" diff --git a/test/import/egret_test.jl b/test/import/egret_test.jl deleted file mode 100644 index a0a61bf..0000000 --- a/test/import/egret_test.jl +++ /dev/null @@ -1,21 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment - -@testset "read_egret_solution" begin - solution = - UnitCommitment.read_egret_solution("$FIXTURES/egret_output.json.gz") - for attr in - ["Is on", "Thermal production (MW)", "Thermal production cost (\$)"] - @test attr in keys(solution) - @test "115_STEAM_1" in keys(solution[attr]) - @test length(solution[attr]["115_STEAM_1"]) == 48 - end - @test solution["Thermal production cost (\$)"]["315_CT_6"][15:20] == - [0.0, 0.0, 884.44, 1470.71, 1470.71, 884.44] - @test solution["Startup cost (\$)"]["315_CT_6"][15:20] == - [0.0, 0.0, 5665.23, 0.0, 0.0, 0.0] - @test length(keys(solution["Is on"])) == 154 -end diff --git a/test/instance/migrate_test.jl b/test/instance/migrate_test.jl deleted file mode 100644 index 1176a01..0000000 --- a/test/instance/migrate_test.jl +++ /dev/null @@ -1,22 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, LinearAlgebra, Cbc, JuMP, JSON, GZip - -@testset "read v0.2" begin - instance = UnitCommitment.read("$FIXTURES/ucjl-0.2.json.gz") - @test length(instance.scenarios) == 1 - sc = instance.scenarios[1] - @test length(sc.reserves_by_name["r1"].amount) == 4 - @test sc.thermal_units_by_name["g2"].reserves[1].name == "r1" -end - -@testset "read v0.3" begin - instance = UnitCommitment.read("$FIXTURES/ucjl-0.3.json.gz") - @test length(instance.scenarios) == 1 - sc = instance.scenarios[1] - @test length(sc.thermal_units) == 6 - @test length(sc.buses) == 14 - @test length(sc.lines) == 20 -end diff --git a/test/instance/read_test.jl b/test/instance/read_test.jl deleted file mode 100644 index 1ad347b..0000000 --- a/test/instance/read_test.jl +++ /dev/null @@ -1,166 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, LinearAlgebra, Cbc, JuMP, JSON, GZip - -@testset "read_benchmark" begin - instance = UnitCommitment.read("$FIXTURES/case14.json.gz") - - @test repr(instance) == ( - "UnitCommitmentInstance(1 scenarios, 6 thermal units, 0 profiled units, 14 buses, " * - "20 lines, 19 contingencies, 1 price sensitive loads, 4 time steps)" - ) - - @test length(instance.scenarios) == 1 - sc = instance.scenarios[1] - @test length(sc.lines) == 20 - @test length(sc.buses) == 14 - @test length(sc.thermal_units) == 6 - @test length(sc.contingencies) == 19 - @test length(sc.price_sensitive_loads) == 1 - @test instance.time == 4 - - @test sc.lines[5].name == "l5" - @test sc.lines[5].source.name == "b2" - @test sc.lines[5].target.name == "b5" - @test sc.lines[5].reactance ≈ 0.17388 - @test sc.lines[5].susceptance ≈ 10.037550333 - @test sc.lines[5].normal_flow_limit == [1e8 for t in 1:4] - @test sc.lines[5].emergency_flow_limit == [1e8 for t in 1:4] - @test sc.lines[5].flow_limit_penalty == [5e3 for t in 1:4] - @test sc.lines_by_name["l5"].name == "l5" - - @test sc.lines[1].name == "l1" - @test sc.lines[1].source.name == "b1" - @test sc.lines[1].target.name == "b2" - @test sc.lines[1].reactance ≈ 0.059170 - @test sc.lines[1].susceptance ≈ 29.496860773945 - @test sc.lines[1].normal_flow_limit == [300.0 for t in 1:4] - @test sc.lines[1].emergency_flow_limit == [400.0 for t in 1:4] - @test sc.lines[1].flow_limit_penalty == [1e3 for t in 1:4] - - @test sc.buses[9].name == "b9" - @test sc.buses[9].load == [35.36638, 33.25495, 31.67138, 31.14353] - @test sc.buses_by_name["b9"].name == "b9" - - @test sc.reserves[1].name == "r1" - @test sc.reserves[1].type == "spinning" - @test sc.reserves[1].amount == [100.0, 100.0, 100.0, 100.0] - @test sc.reserves_by_name["r1"].name == "r1" - - unit = sc.thermal_units[1] - @test unit.name == "g1" - @test unit.bus.name == "b1" - @test unit.ramp_up_limit == 1e6 - @test unit.ramp_down_limit == 1e6 - @test unit.startup_limit == 1e6 - @test unit.shutdown_limit == 1e6 - @test unit.must_run == [false for t in 1:4] - @test unit.min_power_cost == [1400.0 for t in 1:4] - @test unit.min_uptime == 1 - @test unit.min_downtime == 1 - for t in 1:1 - @test unit.cost_segments[1].mw[t] == 10.0 - @test unit.cost_segments[2].mw[t] == 20.0 - @test unit.cost_segments[3].mw[t] == 5.0 - @test unit.cost_segments[1].cost[t] ≈ 20.0 - @test unit.cost_segments[2].cost[t] ≈ 30.0 - @test unit.cost_segments[3].cost[t] ≈ 40.0 - end - @test length(unit.startup_categories) == 3 - @test unit.startup_categories[1].delay == 1 - @test unit.startup_categories[2].delay == 2 - @test unit.startup_categories[3].delay == 3 - @test unit.startup_categories[1].cost == 1000.0 - @test unit.startup_categories[2].cost == 1500.0 - @test unit.startup_categories[3].cost == 2000.0 - @test length(unit.reserves) == 0 - @test sc.thermal_units_by_name["g1"].name == "g1" - - unit = sc.thermal_units[2] - @test unit.name == "g2" - @test unit.must_run == [false for t in 1:4] - @test length(unit.reserves) == 1 - - unit = sc.thermal_units[3] - @test unit.name == "g3" - @test unit.bus.name == "b3" - @test unit.ramp_up_limit == 70.0 - @test unit.ramp_down_limit == 70.0 - @test unit.startup_limit == 70.0 - @test unit.shutdown_limit == 70.0 - @test unit.must_run == [true for t in 1:4] - @test unit.min_power_cost == [0.0 for t in 1:4] - @test unit.min_uptime == 1 - @test unit.min_downtime == 1 - for t in 1:4 - @test unit.cost_segments[1].mw[t] ≈ 33 - @test unit.cost_segments[2].mw[t] ≈ 33 - @test unit.cost_segments[3].mw[t] ≈ 34 - @test unit.cost_segments[1].cost[t] ≈ 33.75 - @test unit.cost_segments[2].cost[t] ≈ 38.04 - @test unit.cost_segments[3].cost[t] ≈ 44.77853 - end - @test length(unit.reserves) == 1 - @test unit.reserves[1].name == "r1" - - @test sc.contingencies[1].lines == [sc.lines[1]] - @test sc.contingencies[1].thermal_units == [] - @test sc.contingencies[1].name == "c1" - @test sc.contingencies_by_name["c1"].name == "c1" - - load = sc.price_sensitive_loads[1] - @test load.name == "ps1" - @test load.bus.name == "b3" - @test load.revenue == [100.0 for t in 1:4] - @test load.demand == [50.0 for t in 1:4] - @test sc.price_sensitive_loads_by_name["ps1"].name == "ps1" -end - -@testset "read_benchmark sub-hourly" begin - instance = UnitCommitment.read("$FIXTURES/case14-sub-hourly.json.gz") - @test instance.time == 4 - unit = instance.scenarios[1].thermal_units[1] - @test unit.name == "g1" - @test unit.min_uptime == 2 - @test unit.min_downtime == 2 - @test length(unit.startup_categories) == 3 - @test unit.startup_categories[1].delay == 2 - @test unit.startup_categories[2].delay == 4 - @test unit.startup_categories[3].delay == 6 - @test unit.initial_status == -200 -end - -@testset "read_benchmark profiled-units" begin - instance = UnitCommitment.read("$FIXTURES/case14-profiled.json.gz") - sc = instance.scenarios[1] - @test length(sc.profiled_units) == 2 - - first_pu = sc.profiled_units[1] - @test first_pu.name == "g7" - @test first_pu.bus.name == "b4" - @test first_pu.cost == [100.0 for t in 1:4] - @test first_pu.min_power == [60.0 for t in 1:4] - @test first_pu.max_power == [100.0 for t in 1:4] - @test sc.profiled_units_by_name["g7"].name == "g7" - - second_pu = sc.profiled_units[2] - @test second_pu.name == "g8" - @test second_pu.bus.name == "b5" - @test second_pu.cost == [50.0 for t in 1:4] - @test second_pu.min_power == [0.0 for t in 1:4] - @test second_pu.max_power == [120.0 for t in 1:4] - @test sc.profiled_units_by_name["g8"].name == "g8" -end - -@testset "read_benchmark commitmemt-status" begin - instance = UnitCommitment.read("$FIXTURES/case14-fixed-status.json.gz") - sc = instance.scenarios[1] - - @test sc.thermal_units[1].commitment_status == [nothing for t in 1:4] - @test sc.thermal_units[2].commitment_status == [true for t in 1:4] - @test sc.thermal_units[4].commitment_status == [false for t in 1:4] - @test sc.thermal_units[6].commitment_status == - [false, nothing, true, nothing] -end diff --git a/test/lmp/aelmp_test.jl b/test/lmp/aelmp_test.jl deleted file mode 100644 index 484275c..0000000 --- a/test/lmp/aelmp_test.jl +++ /dev/null @@ -1,35 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, Cbc, HiGHS, JuMP -import UnitCommitment: AELMP - -@testset "aelmp" begin - path = "$FIXTURES/aelmp_simple.json.gz" - # model has to be solved first - instance = UnitCommitment.read(path) - model = UnitCommitment.build_model( - instance = instance, - optimizer = Cbc.Optimizer, - variable_names = true, - ) - JuMP.set_silent(model) - UnitCommitment.optimize!(model) - - # policy 1: allow offlines; consider startups - aelmp_1 = - UnitCommitment.compute_lmp(model, AELMP(), optimizer = HiGHS.Optimizer) - @test aelmp_1["s1", "B1", 1] ≈ 231.7 atol = 0.1 - - # policy 2: do not allow offlines; but consider startups - aelmp_2 = UnitCommitment.compute_lmp( - model, - AELMP( - allow_offline_participation = false, - consider_startup_costs = true, - ), - optimizer = HiGHS.Optimizer, - ) - @test aelmp_2["s1", "B1", 1] ≈ 274.3 atol = 0.1 -end diff --git a/test/lmp/conventional_test.jl b/test/lmp/conventional_test.jl deleted file mode 100644 index 80a4ce5..0000000 --- a/test/lmp/conventional_test.jl +++ /dev/null @@ -1,51 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, Cbc, HiGHS, JuMP -import UnitCommitment: ConventionalLMP - -function solve_conventional_testcase(path::String) - instance = UnitCommitment.read(path) - model = UnitCommitment.build_model( - instance = instance, - optimizer = Cbc.Optimizer, - variable_names = true, - ) - JuMP.set_silent(model) - UnitCommitment.optimize!(model) - lmp = UnitCommitment.compute_lmp( - model, - ConventionalLMP(), - optimizer = HiGHS.Optimizer, - ) - return lmp -end - -@testset "conventional" begin - # instance 1 - path = "$FIXTURES/lmp_simple_test_1.json.gz" - lmp = solve_conventional_testcase(path) - @test lmp["s1", "A", 1] == 50.0 - @test lmp["s1", "B", 1] == 50.0 - - # instance 2 - path = "$FIXTURES/lmp_simple_test_2.json.gz" - lmp = solve_conventional_testcase(path) - @test lmp["s1", "A", 1] == 50.0 - @test lmp["s1", "B", 1] == 60.0 - - # instance 3 - path = "$FIXTURES/lmp_simple_test_3.json.gz" - lmp = solve_conventional_testcase(path) - @test lmp["s1", "A", 1] == 50.0 - @test lmp["s1", "B", 1] == 70.0 - @test lmp["s1", "C", 1] == 100.0 - - # instance 4 - path = "$FIXTURES/lmp_simple_test_4.json.gz" - lmp = solve_conventional_testcase(path) - @test lmp["s1", "A", 1] == 50.0 - @test lmp["s1", "B", 1] == 70.0 - @test lmp["s1", "C", 1] == 90.0 -end diff --git a/test/model/formulations_test.jl b/test/model/formulations_test.jl deleted file mode 100644 index 09cfeca..0000000 --- a/test/model/formulations_test.jl +++ /dev/null @@ -1,84 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment -using JuMP -using Cbc -using JSON -import UnitCommitment: - ArrCon2000, - CarArr2006, - DamKucRajAta2016, - Formulation, - Gar1962, - KnuOstWat2018, - MorLatRam2013, - PanGua2016, - XavQiuWanThi2019, - WanHob2016 - -function _test( - formulation::Formulation; - instances = ["case14"], - dump::Bool = false, -)::Nothing - for instance_name in instances - instance = UnitCommitment.read("$(FIXTURES)/$(instance_name).json.gz") - model = UnitCommitment.build_model( - instance = instance, - formulation = formulation, - optimizer = Cbc.Optimizer, - variable_names = true, - ) - set_silent(model) - UnitCommitment.optimize!(model) - solution = UnitCommitment.solution(model) - if dump - open("/tmp/ucjl.json", "w") do f - return write(f, JSON.json(solution, 2)) - end - write_to_file(model, "/tmp/ucjl.lp") - end - @test UnitCommitment.validate(instance, solution) - end - return -end - -@testset "formulations" begin - @testset "default" begin - _test(Formulation()) - end - @testset "ArrCon2000" begin - _test(Formulation(ramping = ArrCon2000.Ramping())) - end - @testset "DamKucRajAta2016" begin - _test(Formulation(ramping = DamKucRajAta2016.Ramping())) - end - @testset "MorLatRam2013" begin - _test( - Formulation( - ramping = MorLatRam2013.Ramping(), - startup_costs = MorLatRam2013.StartupCosts(), - ), - ) - end - @testset "PanGua2016" begin - _test(Formulation(ramping = PanGua2016.Ramping())) - end - @testset "Gar1962" begin - _test(Formulation(pwl_costs = Gar1962.PwlCosts())) - end - @testset "CarArr2006" begin - _test(Formulation(pwl_costs = CarArr2006.PwlCosts())) - end - @testset "KnuOstWat2018" begin - _test(Formulation(pwl_costs = KnuOstWat2018.PwlCosts())) - end - @testset "WanHob2016" begin - _test( - Formulation(ramping = WanHob2016.Ramping()), - instances = ["case14-flex"], - ) - end -end diff --git a/test/runtests.jl b/test/runtests.jl deleted file mode 100644 index 08ade97..0000000 --- a/test/runtests.jl +++ /dev/null @@ -1,46 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using Test -using UnitCommitment - -push!(Base.LOAD_PATH, @__DIR__) -UnitCommitment._setup_logger(level = Base.CoreLogging.Error) - -FIXTURES = "$(@__DIR__)/fixtures" - -@testset "UnitCommitment" begin - include("usage.jl") - @testset "import" begin - include("import/egret_test.jl") - end - @testset "instance" begin - include("instance/read_test.jl") - include("instance/migrate_test.jl") - end - @testset "model" begin - include("model/formulations_test.jl") - end - @testset "solution" begin - @testset "XavQiuWanThi19" begin - include("solution/methods/XavQiuWanThi19/filter_test.jl") - include("solution/methods/XavQiuWanThi19/find_test.jl") - include("solution/methods/XavQiuWanThi19/sensitivity_test.jl") - end - end - @testset "transform" begin - include("transform/initcond_test.jl") - include("transform/slice_test.jl") - @testset "randomize" begin - include("transform/randomize/XavQiuAhm2021_test.jl") - end - end - @testset "validation" begin - include("validation/repair_test.jl") - end - @testset "lmp" begin - include("lmp/conventional_test.jl") - include("lmp/aelmp_test.jl") - end -end diff --git a/test/solution/methods/XavQiuWanThi19/filter_test.jl b/test/solution/methods/XavQiuWanThi19/filter_test.jl deleted file mode 100644 index 4ed69e7..0000000 --- a/test/solution/methods/XavQiuWanThi19/filter_test.jl +++ /dev/null @@ -1,83 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, Test, LinearAlgebra -import UnitCommitment: _Violation, _offer, _query - -@testset "_ViolationFilter" begin - instance = UnitCommitment.read("$FIXTURES/case14.json.gz") - sc = instance.scenarios[1] - filter = UnitCommitment._ViolationFilter(max_per_line = 1, max_total = 2) - _offer( - filter, - _Violation( - time = 1, - monitored_line = sc.lines[1], - outage_line = nothing, - amount = 100.0, - ), - ) - _offer( - filter, - _Violation( - time = 1, - monitored_line = sc.lines[1], - outage_line = sc.lines[1], - amount = 300.0, - ), - ) - _offer( - filter, - _Violation( - time = 1, - monitored_line = sc.lines[1], - outage_line = sc.lines[5], - amount = 500.0, - ), - ) - _offer( - filter, - _Violation( - time = 1, - monitored_line = sc.lines[1], - outage_line = sc.lines[4], - amount = 400.0, - ), - ) - _offer( - filter, - _Violation( - time = 1, - monitored_line = sc.lines[2], - outage_line = sc.lines[1], - amount = 200.0, - ), - ) - _offer( - filter, - _Violation( - time = 1, - monitored_line = sc.lines[2], - outage_line = sc.lines[8], - amount = 100.0, - ), - ) - - actual = _query(filter) - expected = [ - _Violation( - time = 1, - monitored_line = sc.lines[2], - outage_line = sc.lines[1], - amount = 200.0, - ), - _Violation( - time = 1, - monitored_line = sc.lines[1], - outage_line = sc.lines[5], - amount = 500.0, - ), - ] - @test actual == expected -end diff --git a/test/solution/methods/XavQiuWanThi19/find_test.jl b/test/solution/methods/XavQiuWanThi19/find_test.jl deleted file mode 100644 index d3d3452..0000000 --- a/test/solution/methods/XavQiuWanThi19/find_test.jl +++ /dev/null @@ -1,37 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, Test, LinearAlgebra -import UnitCommitment: _Violation, _offer, _query - -@testset "find_violations" begin - instance = UnitCommitment.read("$FIXTURES/case14.json.gz") - sc = instance.scenarios[1] - for line in sc.lines, t in 1:instance.time - line.normal_flow_limit[t] = 1.0 - line.emergency_flow_limit[t] = 1.0 - end - isf = UnitCommitment._injection_shift_factors( - lines = sc.lines, - buses = sc.buses, - ) - lodf = UnitCommitment._line_outage_factors( - lines = sc.lines, - buses = sc.buses, - isf = isf, - ) - inj = [1000.0 for b in 1:13, t in 1:instance.time] - overflow = [0.0 for l in sc.lines, t in 1:instance.time] - violations = UnitCommitment._find_violations( - instance = instance, - sc = sc, - net_injections = inj, - overflow = overflow, - isf = isf, - lodf = lodf, - max_per_line = 1, - max_per_period = 5, - ) - @test length(violations) == 20 -end diff --git a/test/solution/methods/XavQiuWanThi19/sensitivity_test.jl b/test/solution/methods/XavQiuWanThi19/sensitivity_test.jl deleted file mode 100644 index 1ab5950..0000000 --- a/test/solution/methods/XavQiuWanThi19/sensitivity_test.jl +++ /dev/null @@ -1,147 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, Test, LinearAlgebra - -@testset "_susceptance_matrix" begin - instance = UnitCommitment.read("$FIXTURES/case14.json.gz") - sc = instance.scenarios[1] - actual = UnitCommitment._susceptance_matrix(sc.lines) - @test size(actual) == (20, 20) - expected = Diagonal([ - 29.5, - 7.83, - 8.82, - 9.9, - 10.04, - 10.2, - 41.45, - 8.35, - 3.14, - 6.93, - 8.77, - 6.82, - 13.4, - 9.91, - 15.87, - 20.65, - 6.46, - 9.09, - 8.73, - 5.02, - ]) - @test round.(actual, digits = 2) == expected -end - -@testset "_reduced_incidence_matrix" begin - instance = UnitCommitment.read("$FIXTURES/case14.json.gz") - sc = instance.scenarios[1] - actual = UnitCommitment._reduced_incidence_matrix( - lines = sc.lines, - buses = sc.buses, - ) - @test size(actual) == (20, 13) - @test actual[1, 1] == -1.0 - @test actual[3, 1] == 1.0 - @test actual[4, 1] == 1.0 - @test actual[5, 1] == 1.0 - @test actual[3, 2] == -1.0 - @test actual[6, 2] == 1.0 - @test actual[4, 3] == -1.0 - @test actual[6, 3] == -1.0 - @test actual[7, 3] == 1.0 - @test actual[8, 3] == 1.0 - @test actual[9, 3] == 1.0 - @test actual[2, 4] == -1.0 - @test actual[5, 4] == -1.0 - @test actual[7, 4] == -1.0 - @test actual[10, 4] == 1.0 - @test actual[10, 5] == -1.0 - @test actual[11, 5] == 1.0 - @test actual[12, 5] == 1.0 - @test actual[13, 5] == 1.0 - @test actual[8, 6] == -1.0 - @test actual[14, 6] == 1.0 - @test actual[15, 6] == 1.0 - @test actual[14, 7] == -1.0 - @test actual[9, 8] == -1.0 - @test actual[15, 8] == -1.0 - @test actual[16, 8] == 1.0 - @test actual[17, 8] == 1.0 - @test actual[16, 9] == -1.0 - @test actual[18, 9] == 1.0 - @test actual[11, 10] == -1.0 - @test actual[18, 10] == -1.0 - @test actual[12, 11] == -1.0 - @test actual[19, 11] == 1.0 - @test actual[13, 12] == -1.0 - @test actual[19, 12] == -1.0 - @test actual[20, 12] == 1.0 - @test actual[17, 13] == -1.0 - @test actual[20, 13] == -1.0 -end - -@testset "_injection_shift_factors" begin - instance = UnitCommitment.read("$FIXTURES/case14.json.gz") - sc = instance.scenarios[1] - actual = UnitCommitment._injection_shift_factors( - lines = sc.lines, - buses = sc.buses, - ) - @test size(actual) == (20, 13) - @test round.(actual, digits = 2) == [ - -0.84 -0.75 -0.67 -0.61 -0.63 -0.66 -0.66 -0.65 -0.65 -0.64 -0.63 -0.63 -0.64 - -0.16 -0.25 -0.33 -0.39 -0.37 -0.34 -0.34 -0.35 -0.35 -0.36 -0.37 -0.37 -0.36 - 0.03 -0.53 -0.15 -0.1 -0.12 -0.14 -0.14 -0.14 -0.13 -0.13 -0.12 -0.12 -0.13 - 0.06 -0.14 -0.32 -0.22 -0.25 -0.3 -0.3 -0.29 -0.28 -0.27 -0.25 -0.26 -0.27 - 0.08 -0.07 -0.2 -0.29 -0.26 -0.22 -0.22 -0.22 -0.23 -0.25 -0.26 -0.26 -0.24 - 0.03 0.47 -0.15 -0.1 -0.12 -0.14 -0.14 -0.14 -0.13 -0.13 -0.12 -0.12 -0.13 - 0.08 0.31 0.5 -0.3 -0.03 0.36 0.36 0.28 0.23 0.1 -0.0 0.02 0.17 - 0.0 0.01 0.02 -0.01 -0.22 -0.63 -0.63 -0.45 -0.41 -0.32 -0.24 -0.25 -0.36 - 0.0 0.01 0.01 -0.01 -0.12 -0.17 -0.17 -0.26 -0.24 -0.18 -0.14 -0.14 -0.21 - -0.0 -0.02 -0.03 0.02 -0.66 -0.2 -0.2 -0.29 -0.36 -0.5 -0.63 -0.61 -0.43 - -0.0 -0.01 -0.02 0.01 0.21 -0.12 -0.12 -0.17 -0.28 -0.53 0.18 0.15 -0.03 - -0.0 -0.0 -0.0 0.0 0.03 -0.02 -0.02 -0.03 -0.02 0.01 -0.52 -0.17 -0.09 - -0.0 -0.01 -0.01 0.01 0.11 -0.06 -0.06 -0.09 -0.05 0.02 -0.28 -0.59 -0.31 - -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -1.0 -0.0 -0.0 -0.0 -0.0 -0.0 0.0 - 0.0 0.01 0.02 -0.01 -0.22 0.37 0.37 -0.45 -0.41 -0.32 -0.24 -0.25 -0.36 - 0.0 0.01 0.02 -0.01 -0.21 0.12 0.12 0.17 -0.72 -0.47 -0.18 -0.15 0.03 - 0.0 0.01 0.01 -0.01 -0.14 0.08 0.08 0.12 0.07 -0.03 -0.2 -0.24 -0.6 - 0.0 0.01 0.02 -0.01 -0.21 0.12 0.12 0.17 0.28 -0.47 -0.18 -0.15 0.03 - -0.0 -0.0 -0.0 0.0 0.03 -0.02 -0.02 -0.03 -0.02 0.01 0.48 -0.17 -0.09 - -0.0 -0.01 -0.01 0.01 0.14 -0.08 -0.08 -0.12 -0.07 0.03 0.2 0.24 -0.4 - ] -end - -@testset "_line_outage_factors" begin - instance = UnitCommitment.read("$FIXTURES/case14.json.gz") - sc = instance.scenarios[1] - isf_before = UnitCommitment._injection_shift_factors( - lines = sc.lines, - buses = sc.buses, - ) - lodf = UnitCommitment._line_outage_factors( - lines = sc.lines, - buses = sc.buses, - isf = isf_before, - ) - for contingency in sc.contingencies - for lc in contingency.lines - prev_susceptance = lc.susceptance - lc.susceptance = 0.0 - isf_after = UnitCommitment._injection_shift_factors( - lines = sc.lines, - buses = sc.buses, - ) - lc.susceptance = prev_susceptance - for lm in sc.lines - expected = isf_after[lm.offset, :] - actual = - isf_before[lm.offset, :] + - lodf[lm.offset, lc.offset] * isf_before[lc.offset, :] - @test norm(expected - actual) < 1e-6 - end - end - end -end diff --git a/test/src/UnitCommitmentT.jl b/test/src/UnitCommitmentT.jl new file mode 100644 index 0000000..c07f547 --- /dev/null +++ b/test/src/UnitCommitmentT.jl @@ -0,0 +1,58 @@ +module UnitCommitmentT + +using JuliaFormatter +using UnitCommitment +using Test + +include("usage.jl") +include("import/egret_test.jl") +include("instance/read_test.jl") +include("instance/migrate_test.jl") +include("model/formulations_test.jl") +include("solution/methods/XavQiuWanThi19/filter_test.jl") +include("solution/methods/XavQiuWanThi19/find_test.jl") +include("solution/methods/XavQiuWanThi19/sensitivity_test.jl") +include("transform/initcond_test.jl") +include("transform/slice_test.jl") +include("transform/randomize/XavQiuAhm2021_test.jl") +include("validation/repair_test.jl") +include("lmp/conventional_test.jl") +include("lmp/aelmp_test.jl") + +basedir = dirname(@__FILE__) + +function fixture(path::String)::String + return "$basedir/../fixtures/$path" +end + +function runtests() + println("Running tests...") + UnitCommitment._setup_logger(level = Base.CoreLogging.Error) + @testset "UnitCommitment" begin + usage_test() + import_egret_test() + instance_read_test() + instance_migrate_test() + model_formulations_test() + solution_methods_XavQiuWanThi19_filter_test() + solution_methods_XavQiuWanThi19_find_test() + solution_methods_XavQiuWanThi19_sensitivity_test() + transform_initcond_test() + transform_slice_test() + transform_randomize_XavQiuAhm2021_test() + validation_repair_test() + lmp_conventional_test() + lmp_aelmp_test() + end + return +end + +function format() + JuliaFormatter.format(basedir, verbose = true) + JuliaFormatter.format("$basedir/../../src", verbose = true) + return +end + +export runtests, format + +end # module UnitCommitmentT diff --git a/test/src/import/egret_test.jl b/test/src/import/egret_test.jl new file mode 100644 index 0000000..fb05780 --- /dev/null +++ b/test/src/import/egret_test.jl @@ -0,0 +1,23 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment + +function import_egret_test() + @testset "read_egret_solution" begin + solution = + UnitCommitment.read_egret_solution(fixture("egret_output.json.gz")) + for attr in + ["Is on", "Thermal production (MW)", "Thermal production cost (\$)"] + @test attr in keys(solution) + @test "115_STEAM_1" in keys(solution[attr]) + @test length(solution[attr]["115_STEAM_1"]) == 48 + end + @test solution["Thermal production cost (\$)"]["315_CT_6"][15:20] == + [0.0, 0.0, 884.44, 1470.71, 1470.71, 884.44] + @test solution["Startup cost (\$)"]["315_CT_6"][15:20] == + [0.0, 0.0, 5665.23, 0.0, 0.0, 0.0] + @test length(keys(solution["Is on"])) == 154 + end +end diff --git a/test/src/instance/migrate_test.jl b/test/src/instance/migrate_test.jl new file mode 100644 index 0000000..8328e53 --- /dev/null +++ b/test/src/instance/migrate_test.jl @@ -0,0 +1,24 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, LinearAlgebra, Cbc, JuMP, JSON, GZip + +function instance_migrate_test() + @testset "read v0.2" begin + instance = UnitCommitment.read(fixture("/ucjl-0.2.json.gz")) + @test length(instance.scenarios) == 1 + sc = instance.scenarios[1] + @test length(sc.reserves_by_name["r1"].amount) == 4 + @test sc.thermal_units_by_name["g2"].reserves[1].name == "r1" + end + + @testset "read v0.3" begin + instance = UnitCommitment.read(fixture("/ucjl-0.3.json.gz")) + @test length(instance.scenarios) == 1 + sc = instance.scenarios[1] + @test length(sc.thermal_units) == 6 + @test length(sc.buses) == 14 + @test length(sc.lines) == 20 + end +end diff --git a/test/src/instance/read_test.jl b/test/src/instance/read_test.jl new file mode 100644 index 0000000..85f44ff --- /dev/null +++ b/test/src/instance/read_test.jl @@ -0,0 +1,168 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, LinearAlgebra, Cbc, JuMP, JSON, GZip + +function instance_read_test() + @testset "read_benchmark" begin + instance = UnitCommitment.read(fixture("case14.json.gz")) + + @test repr(instance) == ( + "UnitCommitmentInstance(1 scenarios, 6 thermal units, 0 profiled units, 14 buses, " * + "20 lines, 19 contingencies, 1 price sensitive loads, 4 time steps)" + ) + + @test length(instance.scenarios) == 1 + sc = instance.scenarios[1] + @test length(sc.lines) == 20 + @test length(sc.buses) == 14 + @test length(sc.thermal_units) == 6 + @test length(sc.contingencies) == 19 + @test length(sc.price_sensitive_loads) == 1 + @test instance.time == 4 + + @test sc.lines[5].name == "l5" + @test sc.lines[5].source.name == "b2" + @test sc.lines[5].target.name == "b5" + @test sc.lines[5].reactance ≈ 0.17388 + @test sc.lines[5].susceptance ≈ 10.037550333 + @test sc.lines[5].normal_flow_limit == [1e8 for t in 1:4] + @test sc.lines[5].emergency_flow_limit == [1e8 for t in 1:4] + @test sc.lines[5].flow_limit_penalty == [5e3 for t in 1:4] + @test sc.lines_by_name["l5"].name == "l5" + + @test sc.lines[1].name == "l1" + @test sc.lines[1].source.name == "b1" + @test sc.lines[1].target.name == "b2" + @test sc.lines[1].reactance ≈ 0.059170 + @test sc.lines[1].susceptance ≈ 29.496860773945 + @test sc.lines[1].normal_flow_limit == [300.0 for t in 1:4] + @test sc.lines[1].emergency_flow_limit == [400.0 for t in 1:4] + @test sc.lines[1].flow_limit_penalty == [1e3 for t in 1:4] + + @test sc.buses[9].name == "b9" + @test sc.buses[9].load == [35.36638, 33.25495, 31.67138, 31.14353] + @test sc.buses_by_name["b9"].name == "b9" + + @test sc.reserves[1].name == "r1" + @test sc.reserves[1].type == "spinning" + @test sc.reserves[1].amount == [100.0, 100.0, 100.0, 100.0] + @test sc.reserves_by_name["r1"].name == "r1" + + unit = sc.thermal_units[1] + @test unit.name == "g1" + @test unit.bus.name == "b1" + @test unit.ramp_up_limit == 1e6 + @test unit.ramp_down_limit == 1e6 + @test unit.startup_limit == 1e6 + @test unit.shutdown_limit == 1e6 + @test unit.must_run == [false for t in 1:4] + @test unit.min_power_cost == [1400.0 for t in 1:4] + @test unit.min_uptime == 1 + @test unit.min_downtime == 1 + for t in 1:1 + @test unit.cost_segments[1].mw[t] == 10.0 + @test unit.cost_segments[2].mw[t] == 20.0 + @test unit.cost_segments[3].mw[t] == 5.0 + @test unit.cost_segments[1].cost[t] ≈ 20.0 + @test unit.cost_segments[2].cost[t] ≈ 30.0 + @test unit.cost_segments[3].cost[t] ≈ 40.0 + end + @test length(unit.startup_categories) == 3 + @test unit.startup_categories[1].delay == 1 + @test unit.startup_categories[2].delay == 2 + @test unit.startup_categories[3].delay == 3 + @test unit.startup_categories[1].cost == 1000.0 + @test unit.startup_categories[2].cost == 1500.0 + @test unit.startup_categories[3].cost == 2000.0 + @test length(unit.reserves) == 0 + @test sc.thermal_units_by_name["g1"].name == "g1" + + unit = sc.thermal_units[2] + @test unit.name == "g2" + @test unit.must_run == [false for t in 1:4] + @test length(unit.reserves) == 1 + + unit = sc.thermal_units[3] + @test unit.name == "g3" + @test unit.bus.name == "b3" + @test unit.ramp_up_limit == 70.0 + @test unit.ramp_down_limit == 70.0 + @test unit.startup_limit == 70.0 + @test unit.shutdown_limit == 70.0 + @test unit.must_run == [true for t in 1:4] + @test unit.min_power_cost == [0.0 for t in 1:4] + @test unit.min_uptime == 1 + @test unit.min_downtime == 1 + for t in 1:4 + @test unit.cost_segments[1].mw[t] ≈ 33 + @test unit.cost_segments[2].mw[t] ≈ 33 + @test unit.cost_segments[3].mw[t] ≈ 34 + @test unit.cost_segments[1].cost[t] ≈ 33.75 + @test unit.cost_segments[2].cost[t] ≈ 38.04 + @test unit.cost_segments[3].cost[t] ≈ 44.77853 + end + @test length(unit.reserves) == 1 + @test unit.reserves[1].name == "r1" + + @test sc.contingencies[1].lines == [sc.lines[1]] + @test sc.contingencies[1].thermal_units == [] + @test sc.contingencies[1].name == "c1" + @test sc.contingencies_by_name["c1"].name == "c1" + + load = sc.price_sensitive_loads[1] + @test load.name == "ps1" + @test load.bus.name == "b3" + @test load.revenue == [100.0 for t in 1:4] + @test load.demand == [50.0 for t in 1:4] + @test sc.price_sensitive_loads_by_name["ps1"].name == "ps1" + end + + @testset "read_benchmark sub-hourly" begin + instance = UnitCommitment.read(fixture("case14-sub-hourly.json.gz")) + @test instance.time == 4 + unit = instance.scenarios[1].thermal_units[1] + @test unit.name == "g1" + @test unit.min_uptime == 2 + @test unit.min_downtime == 2 + @test length(unit.startup_categories) == 3 + @test unit.startup_categories[1].delay == 2 + @test unit.startup_categories[2].delay == 4 + @test unit.startup_categories[3].delay == 6 + @test unit.initial_status == -200 + end + + @testset "read_benchmark profiled-units" begin + instance = UnitCommitment.read(fixture("case14-profiled.json.gz")) + sc = instance.scenarios[1] + @test length(sc.profiled_units) == 2 + + first_pu = sc.profiled_units[1] + @test first_pu.name == "g7" + @test first_pu.bus.name == "b4" + @test first_pu.cost == [100.0 for t in 1:4] + @test first_pu.min_power == [60.0 for t in 1:4] + @test first_pu.max_power == [100.0 for t in 1:4] + @test sc.profiled_units_by_name["g7"].name == "g7" + + second_pu = sc.profiled_units[2] + @test second_pu.name == "g8" + @test second_pu.bus.name == "b5" + @test second_pu.cost == [50.0 for t in 1:4] + @test second_pu.min_power == [0.0 for t in 1:4] + @test second_pu.max_power == [120.0 for t in 1:4] + @test sc.profiled_units_by_name["g8"].name == "g8" + end + + @testset "read_benchmark commitmemt-status" begin + instance = UnitCommitment.read(fixture("case14-fixed-status.json.gz")) + sc = instance.scenarios[1] + + @test sc.thermal_units[1].commitment_status == [nothing for t in 1:4] + @test sc.thermal_units[2].commitment_status == [true for t in 1:4] + @test sc.thermal_units[4].commitment_status == [false for t in 1:4] + @test sc.thermal_units[6].commitment_status == + [false, nothing, true, nothing] + end +end diff --git a/test/src/lmp/aelmp_test.jl b/test/src/lmp/aelmp_test.jl new file mode 100644 index 0000000..a7494bf --- /dev/null +++ b/test/src/lmp/aelmp_test.jl @@ -0,0 +1,40 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, Cbc, HiGHS, JuMP +import UnitCommitment: AELMP + +function lmp_aelmp_test() + @testset "aelmp" begin + path = fixture("aelmp_simple.json.gz") + # model has to be solved first + instance = UnitCommitment.read(path) + model = UnitCommitment.build_model( + instance = instance, + optimizer = Cbc.Optimizer, + variable_names = true, + ) + JuMP.set_silent(model) + UnitCommitment.optimize!(model) + + # policy 1: allow offlines; consider startups + aelmp_1 = UnitCommitment.compute_lmp( + model, + AELMP(), + optimizer = HiGHS.Optimizer, + ) + @test aelmp_1["s1", "B1", 1] ≈ 231.7 atol = 0.1 + + # policy 2: do not allow offlines; but consider startups + aelmp_2 = UnitCommitment.compute_lmp( + model, + AELMP( + allow_offline_participation = false, + consider_startup_costs = true, + ), + optimizer = HiGHS.Optimizer, + ) + @test aelmp_2["s1", "B1", 1] ≈ 274.3 atol = 0.1 + end +end diff --git a/test/src/lmp/conventional_test.jl b/test/src/lmp/conventional_test.jl new file mode 100644 index 0000000..b1135c5 --- /dev/null +++ b/test/src/lmp/conventional_test.jl @@ -0,0 +1,53 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, Cbc, HiGHS, JuMP +import UnitCommitment: ConventionalLMP + +function solve_conventional_testcase(path::String) + instance = UnitCommitment.read(path) + model = UnitCommitment.build_model( + instance = instance, + optimizer = Cbc.Optimizer, + variable_names = true, + ) + JuMP.set_silent(model) + UnitCommitment.optimize!(model) + lmp = UnitCommitment.compute_lmp( + model, + ConventionalLMP(), + optimizer = HiGHS.Optimizer, + ) + return lmp +end + +function lmp_conventional_test() + @testset "conventional" begin + # instance 1 + path = fixture("lmp_simple_test_1.json.gz") + lmp = solve_conventional_testcase(path) + @test lmp["s1", "A", 1] == 50.0 + @test lmp["s1", "B", 1] == 50.0 + + # instance 2 + path = fixture("lmp_simple_test_2.json.gz") + lmp = solve_conventional_testcase(path) + @test lmp["s1", "A", 1] == 50.0 + @test lmp["s1", "B", 1] == 60.0 + + # instance 3 + path = fixture("lmp_simple_test_3.json.gz") + lmp = solve_conventional_testcase(path) + @test lmp["s1", "A", 1] == 50.0 + @test lmp["s1", "B", 1] == 70.0 + @test lmp["s1", "C", 1] == 100.0 + + # instance 4 + path = fixture("lmp_simple_test_4.json.gz") + lmp = solve_conventional_testcase(path) + @test lmp["s1", "A", 1] == 50.0 + @test lmp["s1", "B", 1] == 70.0 + @test lmp["s1", "C", 1] == 90.0 + end +end diff --git a/test/src/model/formulations_test.jl b/test/src/model/formulations_test.jl new file mode 100644 index 0000000..2b9307d --- /dev/null +++ b/test/src/model/formulations_test.jl @@ -0,0 +1,86 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment +using JuMP +using Cbc +using JSON +import UnitCommitment: + ArrCon2000, + CarArr2006, + DamKucRajAta2016, + Formulation, + Gar1962, + KnuOstWat2018, + MorLatRam2013, + PanGua2016, + XavQiuWanThi2019, + WanHob2016 + +function _test( + formulation::Formulation; + instances = ["case14"], + dump::Bool = false, +)::Nothing + for instance_name in instances + instance = UnitCommitment.read(fixture("$(instance_name).json.gz")) + model = UnitCommitment.build_model( + instance = instance, + formulation = formulation, + optimizer = Cbc.Optimizer, + variable_names = true, + ) + set_silent(model) + UnitCommitment.optimize!(model) + solution = UnitCommitment.solution(model) + if dump + open("/tmp/ucjl.json", "w") do f + return write(f, JSON.json(solution, 2)) + end + write_to_file(model, "/tmp/ucjl.lp") + end + @test UnitCommitment.validate(instance, solution) + end + return +end + +function model_formulations_test() + @testset "formulations" begin + @testset "default" begin + _test(Formulation()) + end + @testset "ArrCon2000" begin + _test(Formulation(ramping = ArrCon2000.Ramping())) + end + @testset "DamKucRajAta2016" begin + _test(Formulation(ramping = DamKucRajAta2016.Ramping())) + end + @testset "MorLatRam2013" begin + _test( + Formulation( + ramping = MorLatRam2013.Ramping(), + startup_costs = MorLatRam2013.StartupCosts(), + ), + ) + end + @testset "PanGua2016" begin + _test(Formulation(ramping = PanGua2016.Ramping())) + end + @testset "Gar1962" begin + _test(Formulation(pwl_costs = Gar1962.PwlCosts())) + end + @testset "CarArr2006" begin + _test(Formulation(pwl_costs = CarArr2006.PwlCosts())) + end + @testset "KnuOstWat2018" begin + _test(Formulation(pwl_costs = KnuOstWat2018.PwlCosts())) + end + @testset "WanHob2016" begin + _test( + Formulation(ramping = WanHob2016.Ramping()), + instances = ["case14-flex"], + ) + end + end +end diff --git a/test/src/solution/methods/XavQiuWanThi19/filter_test.jl b/test/src/solution/methods/XavQiuWanThi19/filter_test.jl new file mode 100644 index 0000000..7e37ca9 --- /dev/null +++ b/test/src/solution/methods/XavQiuWanThi19/filter_test.jl @@ -0,0 +1,86 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, Test, LinearAlgebra +import UnitCommitment: _Violation, _offer, _query + +function solution_methods_XavQiuWanThi19_filter_test() + @testset "_ViolationFilter" begin + instance = UnitCommitment.read(fixture("case14.json.gz")) + sc = instance.scenarios[1] + filter = + UnitCommitment._ViolationFilter(max_per_line = 1, max_total = 2) + _offer( + filter, + _Violation( + time = 1, + monitored_line = sc.lines[1], + outage_line = nothing, + amount = 100.0, + ), + ) + _offer( + filter, + _Violation( + time = 1, + monitored_line = sc.lines[1], + outage_line = sc.lines[1], + amount = 300.0, + ), + ) + _offer( + filter, + _Violation( + time = 1, + monitored_line = sc.lines[1], + outage_line = sc.lines[5], + amount = 500.0, + ), + ) + _offer( + filter, + _Violation( + time = 1, + monitored_line = sc.lines[1], + outage_line = sc.lines[4], + amount = 400.0, + ), + ) + _offer( + filter, + _Violation( + time = 1, + monitored_line = sc.lines[2], + outage_line = sc.lines[1], + amount = 200.0, + ), + ) + _offer( + filter, + _Violation( + time = 1, + monitored_line = sc.lines[2], + outage_line = sc.lines[8], + amount = 100.0, + ), + ) + + actual = _query(filter) + expected = [ + _Violation( + time = 1, + monitored_line = sc.lines[2], + outage_line = sc.lines[1], + amount = 200.0, + ), + _Violation( + time = 1, + monitored_line = sc.lines[1], + outage_line = sc.lines[5], + amount = 500.0, + ), + ] + @test actual == expected + end +end diff --git a/test/src/solution/methods/XavQiuWanThi19/find_test.jl b/test/src/solution/methods/XavQiuWanThi19/find_test.jl new file mode 100644 index 0000000..a4a67bf --- /dev/null +++ b/test/src/solution/methods/XavQiuWanThi19/find_test.jl @@ -0,0 +1,39 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, Test, LinearAlgebra +import UnitCommitment: _Violation, _offer, _query + +function solution_methods_XavQiuWanThi19_find_test() + @testset "find_violations" begin + instance = UnitCommitment.read(fixture("case14.json.gz")) + sc = instance.scenarios[1] + for line in sc.lines, t in 1:instance.time + line.normal_flow_limit[t] = 1.0 + line.emergency_flow_limit[t] = 1.0 + end + isf = UnitCommitment._injection_shift_factors( + lines = sc.lines, + buses = sc.buses, + ) + lodf = UnitCommitment._line_outage_factors( + lines = sc.lines, + buses = sc.buses, + isf = isf, + ) + inj = [1000.0 for b in 1:13, t in 1:instance.time] + overflow = [0.0 for l in sc.lines, t in 1:instance.time] + violations = UnitCommitment._find_violations( + instance = instance, + sc = sc, + net_injections = inj, + overflow = overflow, + isf = isf, + lodf = lodf, + max_per_line = 1, + max_per_period = 5, + ) + @test length(violations) == 20 + end +end diff --git a/test/src/solution/methods/XavQiuWanThi19/sensitivity_test.jl b/test/src/solution/methods/XavQiuWanThi19/sensitivity_test.jl new file mode 100644 index 0000000..0eef038 --- /dev/null +++ b/test/src/solution/methods/XavQiuWanThi19/sensitivity_test.jl @@ -0,0 +1,149 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, Test, LinearAlgebra + +function solution_methods_XavQiuWanThi19_sensitivity_test() + @testset "_susceptance_matrix" begin + instance = UnitCommitment.read(fixture("/case14.json.gz")) + sc = instance.scenarios[1] + actual = UnitCommitment._susceptance_matrix(sc.lines) + @test size(actual) == (20, 20) + expected = Diagonal([ + 29.5, + 7.83, + 8.82, + 9.9, + 10.04, + 10.2, + 41.45, + 8.35, + 3.14, + 6.93, + 8.77, + 6.82, + 13.4, + 9.91, + 15.87, + 20.65, + 6.46, + 9.09, + 8.73, + 5.02, + ]) + @test round.(actual, digits = 2) == expected + end + + @testset "_reduced_incidence_matrix" begin + instance = UnitCommitment.read(fixture("/case14.json.gz")) + sc = instance.scenarios[1] + actual = UnitCommitment._reduced_incidence_matrix( + lines = sc.lines, + buses = sc.buses, + ) + @test size(actual) == (20, 13) + @test actual[1, 1] == -1.0 + @test actual[3, 1] == 1.0 + @test actual[4, 1] == 1.0 + @test actual[5, 1] == 1.0 + @test actual[3, 2] == -1.0 + @test actual[6, 2] == 1.0 + @test actual[4, 3] == -1.0 + @test actual[6, 3] == -1.0 + @test actual[7, 3] == 1.0 + @test actual[8, 3] == 1.0 + @test actual[9, 3] == 1.0 + @test actual[2, 4] == -1.0 + @test actual[5, 4] == -1.0 + @test actual[7, 4] == -1.0 + @test actual[10, 4] == 1.0 + @test actual[10, 5] == -1.0 + @test actual[11, 5] == 1.0 + @test actual[12, 5] == 1.0 + @test actual[13, 5] == 1.0 + @test actual[8, 6] == -1.0 + @test actual[14, 6] == 1.0 + @test actual[15, 6] == 1.0 + @test actual[14, 7] == -1.0 + @test actual[9, 8] == -1.0 + @test actual[15, 8] == -1.0 + @test actual[16, 8] == 1.0 + @test actual[17, 8] == 1.0 + @test actual[16, 9] == -1.0 + @test actual[18, 9] == 1.0 + @test actual[11, 10] == -1.0 + @test actual[18, 10] == -1.0 + @test actual[12, 11] == -1.0 + @test actual[19, 11] == 1.0 + @test actual[13, 12] == -1.0 + @test actual[19, 12] == -1.0 + @test actual[20, 12] == 1.0 + @test actual[17, 13] == -1.0 + @test actual[20, 13] == -1.0 + end + + @testset "_injection_shift_factors" begin + instance = UnitCommitment.read(fixture("/case14.json.gz")) + sc = instance.scenarios[1] + actual = UnitCommitment._injection_shift_factors( + lines = sc.lines, + buses = sc.buses, + ) + @test size(actual) == (20, 13) + @test round.(actual, digits = 2) == [ + -0.84 -0.75 -0.67 -0.61 -0.63 -0.66 -0.66 -0.65 -0.65 -0.64 -0.63 -0.63 -0.64 + -0.16 -0.25 -0.33 -0.39 -0.37 -0.34 -0.34 -0.35 -0.35 -0.36 -0.37 -0.37 -0.36 + 0.03 -0.53 -0.15 -0.1 -0.12 -0.14 -0.14 -0.14 -0.13 -0.13 -0.12 -0.12 -0.13 + 0.06 -0.14 -0.32 -0.22 -0.25 -0.3 -0.3 -0.29 -0.28 -0.27 -0.25 -0.26 -0.27 + 0.08 -0.07 -0.2 -0.29 -0.26 -0.22 -0.22 -0.22 -0.23 -0.25 -0.26 -0.26 -0.24 + 0.03 0.47 -0.15 -0.1 -0.12 -0.14 -0.14 -0.14 -0.13 -0.13 -0.12 -0.12 -0.13 + 0.08 0.31 0.5 -0.3 -0.03 0.36 0.36 0.28 0.23 0.1 -0.0 0.02 0.17 + 0.0 0.01 0.02 -0.01 -0.22 -0.63 -0.63 -0.45 -0.41 -0.32 -0.24 -0.25 -0.36 + 0.0 0.01 0.01 -0.01 -0.12 -0.17 -0.17 -0.26 -0.24 -0.18 -0.14 -0.14 -0.21 + -0.0 -0.02 -0.03 0.02 -0.66 -0.2 -0.2 -0.29 -0.36 -0.5 -0.63 -0.61 -0.43 + -0.0 -0.01 -0.02 0.01 0.21 -0.12 -0.12 -0.17 -0.28 -0.53 0.18 0.15 -0.03 + -0.0 -0.0 -0.0 0.0 0.03 -0.02 -0.02 -0.03 -0.02 0.01 -0.52 -0.17 -0.09 + -0.0 -0.01 -0.01 0.01 0.11 -0.06 -0.06 -0.09 -0.05 0.02 -0.28 -0.59 -0.31 + -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -1.0 -0.0 -0.0 -0.0 -0.0 -0.0 0.0 + 0.0 0.01 0.02 -0.01 -0.22 0.37 0.37 -0.45 -0.41 -0.32 -0.24 -0.25 -0.36 + 0.0 0.01 0.02 -0.01 -0.21 0.12 0.12 0.17 -0.72 -0.47 -0.18 -0.15 0.03 + 0.0 0.01 0.01 -0.01 -0.14 0.08 0.08 0.12 0.07 -0.03 -0.2 -0.24 -0.6 + 0.0 0.01 0.02 -0.01 -0.21 0.12 0.12 0.17 0.28 -0.47 -0.18 -0.15 0.03 + -0.0 -0.0 -0.0 0.0 0.03 -0.02 -0.02 -0.03 -0.02 0.01 0.48 -0.17 -0.09 + -0.0 -0.01 -0.01 0.01 0.14 -0.08 -0.08 -0.12 -0.07 0.03 0.2 0.24 -0.4 + ] + end + + @testset "_line_outage_factors" begin + instance = UnitCommitment.read(fixture("/case14.json.gz")) + sc = instance.scenarios[1] + isf_before = UnitCommitment._injection_shift_factors( + lines = sc.lines, + buses = sc.buses, + ) + lodf = UnitCommitment._line_outage_factors( + lines = sc.lines, + buses = sc.buses, + isf = isf_before, + ) + for contingency in sc.contingencies + for lc in contingency.lines + prev_susceptance = lc.susceptance + lc.susceptance = 0.0 + isf_after = UnitCommitment._injection_shift_factors( + lines = sc.lines, + buses = sc.buses, + ) + lc.susceptance = prev_susceptance + for lm in sc.lines + expected = isf_after[lm.offset, :] + actual = + isf_before[lm.offset, :] + + lodf[lm.offset, lc.offset] * isf_before[lc.offset, :] + @test norm(expected - actual) < 1e-6 + end + end + end + end +end diff --git a/test/src/transform/initcond_test.jl b/test/src/transform/initcond_test.jl new file mode 100644 index 0000000..24c23dd --- /dev/null +++ b/test/src/transform/initcond_test.jl @@ -0,0 +1,30 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, Cbc, JuMP + +function transform_initcond_test() + @testset "generate_initial_conditions!" begin + # Load instance + instance = UnitCommitment.read(fixture("case118-initcond.json.gz")) + optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) + sc = instance.scenarios[1] + # All units should have unknown initial conditions + for g in sc.thermal_units + @test g.initial_power === nothing + @test g.initial_status === nothing + end + + # Generate initial conditions + UnitCommitment.generate_initial_conditions!(sc, optimizer) + + # All units should now have known initial conditions + for g in sc.thermal_units + @test g.initial_power !== nothing + @test g.initial_status !== nothing + end + + # TODO: Check that initial conditions are feasible + end +end diff --git a/test/src/transform/randomize/XavQiuAhm2021_test.jl b/test/src/transform/randomize/XavQiuAhm2021_test.jl new file mode 100644 index 0000000..7819529 --- /dev/null +++ b/test/src/transform/randomize/XavQiuAhm2021_test.jl @@ -0,0 +1,106 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +import Random +import UnitCommitment: XavQiuAhm2021 + +using Distributions +using Random +using UnitCommitment, Cbc, JuMP + +function get_scenario() + return UnitCommitment.read_benchmark( + "matpower/case118/2017-02-01", + ).scenarios[1] +end +system_load(sc) = sum(b.load for b in sc.buses) +test_approx(x, y) = @test isapprox(x, y, atol = 1e-3) + +function transform_randomize_XavQiuAhm2021_test() + @testset "XavQiuAhm2021" begin + @testset "cost and load share" begin + sc = get_scenario() + # Check original costs + unit = sc.thermal_units[10] + test_approx(unit.min_power_cost[1], 825.023) + test_approx(unit.cost_segments[1].cost[1], 36.659) + test_approx(unit.startup_categories[1].cost[1], 7570.42) + + # Check original load share + bus = sc.buses[1] + prev_system_load = system_load(sc) + test_approx(bus.load[1] / prev_system_load[1], 0.012) + + randomize!( + sc, + XavQiuAhm2021.Randomization(randomize_load_profile = false), + rng = MersenneTwister(42), + ) + + # Check randomized costs + test_approx(unit.min_power_cost[1], 831.977) + test_approx(unit.cost_segments[1].cost[1], 36.968) + test_approx(unit.startup_categories[1].cost[1], 7634.226) + + # Check randomized load share + curr_system_load = system_load(sc) + test_approx(bus.load[1] / curr_system_load[1], 0.013) + + # System load should not change + @test prev_system_load ≈ curr_system_load + end + + @testset "load profile" begin + sc = get_scenario() + # Check original load profile + @test round.(system_load(sc), digits = 1)[1:8] ≈ [ + 3059.5, + 2983.2, + 2937.5, + 2953.9, + 3073.1, + 3356.4, + 4068.5, + 4018.8, + ] + + randomize!( + sc, + XavQiuAhm2021.Randomization(); + rng = MersenneTwister(42), + ) + + # Check randomized load profile + @test round.(system_load(sc), digits = 1)[1:8] ≈ [ + 4854.7, + 4849.2, + 4732.7, + 4848.2, + 4948.4, + 5231.1, + 5874.8, + 5934.8, + ] + end + + @testset "profiled unit cost" begin + sc = UnitCommitment.read( + fixture("case14-profiled.json.gz"), + ).scenarios[1] + # Check original costs + pu1 = sc.profiled_units[1] + pu2 = sc.profiled_units[2] + test_approx(pu1.cost[1], 100.0) + test_approx(pu2.cost[1], 50.0) + randomize!( + sc, + XavQiuAhm2021.Randomization(randomize_load_profile = false), + rng = MersenneTwister(42), + ) + # Check randomized costs + test_approx(pu1.cost[1], 98.039) + test_approx(pu2.cost[1], 48.385) + end + end +end diff --git a/test/src/transform/slice_test.jl b/test/src/transform/slice_test.jl new file mode 100644 index 0000000..affe864 --- /dev/null +++ b/test/src/transform/slice_test.jl @@ -0,0 +1,68 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, LinearAlgebra, Cbc, JuMP, JSON, GZip + +function transform_slice_test() + @testset "slice" begin + instance = UnitCommitment.read(fixture("case14.json.gz")) + modified = UnitCommitment.slice(instance, 1:2) + sc = modified.scenarios[1] + + # Should update all time-dependent fields + @test modified.time == 2 + @test length(sc.power_balance_penalty) == 2 + @test length(sc.reserves_by_name["r1"].amount) == 2 + for u in sc.thermal_units + @test length(u.max_power) == 2 + @test length(u.min_power) == 2 + @test length(u.must_run) == 2 + @test length(u.min_power_cost) == 2 + for s in u.cost_segments + @test length(s.mw) == 2 + @test length(s.cost) == 2 + end + end + for b in sc.buses + @test length(b.load) == 2 + end + for l in sc.lines + @test length(l.normal_flow_limit) == 2 + @test length(l.emergency_flow_limit) == 2 + @test length(l.flow_limit_penalty) == 2 + end + for ps in sc.price_sensitive_loads + @test length(ps.demand) == 2 + @test length(ps.revenue) == 2 + end + + # Should be able to build model without errors + optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) + model = UnitCommitment.build_model( + instance = modified, + optimizer = optimizer, + variable_names = true, + ) + end + + @testset "slice profiled units" begin + instance = UnitCommitment.read(fixture("case14-profiled.json.gz")) + modified = UnitCommitment.slice(instance, 1:2) + sc = modified.scenarios[1] + + # Should update all time-dependent fields + for pu in sc.profiled_units + @test length(pu.max_power) == 2 + @test length(pu.min_power) == 2 + end + + # Should be able to build model without errors + optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) + model = UnitCommitment.build_model( + instance = modified, + optimizer = optimizer, + variable_names = true, + ) + end +end diff --git a/test/src/usage.jl b/test/src/usage.jl new file mode 100644 index 0000000..ea4eb20 --- /dev/null +++ b/test/src/usage.jl @@ -0,0 +1,66 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, LinearAlgebra, Cbc, JuMP, JSON + +function _set_flow_limits!(instance) + for sc in instance.scenarios + sc.power_balance_penalty = [100_000 for _ in 1:instance.time] + for line in sc.lines, t in 1:4 + line.normal_flow_limit[t] = 10.0 + end + end +end + +function usage_test() + @testset "usage" begin + @testset "deterministic" begin + instance = UnitCommitment.read(fixture("case14.json.gz")) + _set_flow_limits!(instance) + optimizer = + optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) + model = UnitCommitment.build_model( + instance = instance, + optimizer = optimizer, + variable_names = true, + ) + @test name(model[:is_on]["g1", 1]) == "is_on[g1,1]" + + # Optimize and retrieve solution + UnitCommitment.optimize!(model) + solution = UnitCommitment.solution(model) + + # Write solution to a file + filename = tempname() + UnitCommitment.write(filename, solution) + loaded = JSON.parsefile(filename) + @test length(loaded["Is on"]) == 6 + + # Verify solution + @test UnitCommitment.validate(instance, solution) + + # Reoptimize with fixed solution + UnitCommitment.fix!(model, solution) + UnitCommitment.optimize!(model) + @test UnitCommitment.validate(instance, solution) + end + + @testset "stochastic" begin + instance = UnitCommitment.read([ + fixture("case14.json.gz"), + fixture("case14.json.gz"), + ]) + _set_flow_limits!(instance) + @test length(instance.scenarios) == 2 + optimizer = + optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) + model = UnitCommitment.build_model( + instance = instance, + optimizer = optimizer, + ) + UnitCommitment.optimize!(model) + solution = UnitCommitment.solution(model) + end + end +end diff --git a/test/src/validation/repair_test.jl b/test/src/validation/repair_test.jl new file mode 100644 index 0000000..9d34aeb --- /dev/null +++ b/test/src/validation/repair_test.jl @@ -0,0 +1,43 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using UnitCommitment, JSON, GZip, DataStructures + +function parse_case14() + return JSON.parse( + GZip.gzopen(fixture("case14.json.gz")), + dicttype = () -> DefaultOrderedDict(nothing), + ) +end + +function validation_repair_test() + @testset "repair!" begin + @testset "Cost curve should be convex" begin + json = parse_case14() + json["Generators"]["g1"]["Production cost curve (MW)"] = + [100, 150, 200] + json["Generators"]["g1"]["Production cost curve (\$)"] = + [10, 25, 30] + sc = UnitCommitment._from_json(json, repair = false) + @test UnitCommitment.repair!(sc) == 4 + end + + @testset "Startup limit must be greater than Pmin" begin + json = parse_case14() + json["Generators"]["g1"]["Production cost curve (MW)"] = [100, 150] + json["Generators"]["g1"]["Production cost curve (\$)"] = [100, 150] + json["Generators"]["g1"]["Startup limit (MW)"] = 80 + sc = UnitCommitment._from_json(json, repair = false) + @test UnitCommitment.repair!(sc) == 1 + end + + @testset "Startup costs and delays must be increasing" begin + json = parse_case14() + json["Generators"]["g1"]["Startup costs (\$)"] = [300, 200, 100] + json["Generators"]["g1"]["Startup delays (h)"] = [8, 4, 2] + sc = UnitCommitment._from_json(json, repair = false) + @test UnitCommitment.repair!(sc) == 4 + end + end +end diff --git a/test/transform/initcond_test.jl b/test/transform/initcond_test.jl deleted file mode 100644 index 744eaf7..0000000 --- a/test/transform/initcond_test.jl +++ /dev/null @@ -1,28 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, Cbc, JuMP - -@testset "generate_initial_conditions!" begin - # Load instance - instance = UnitCommitment.read("$FIXTURES/case118-initcond.json.gz") - optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) - sc = instance.scenarios[1] - # All units should have unknown initial conditions - for g in sc.thermal_units - @test g.initial_power === nothing - @test g.initial_status === nothing - end - - # Generate initial conditions - UnitCommitment.generate_initial_conditions!(sc, optimizer) - - # All units should now have known initial conditions - for g in sc.thermal_units - @test g.initial_power !== nothing - @test g.initial_status !== nothing - end - - # TODO: Check that initial conditions are feasible -end diff --git a/test/transform/randomize/XavQiuAhm2021_test.jl b/test/transform/randomize/XavQiuAhm2021_test.jl deleted file mode 100644 index ea85000..0000000 --- a/test/transform/randomize/XavQiuAhm2021_test.jl +++ /dev/null @@ -1,83 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -import Random -import UnitCommitment: XavQiuAhm2021 - -using Distributions -using Random -using UnitCommitment, Cbc, JuMP - -function get_scenario() - return UnitCommitment.read_benchmark( - "matpower/case118/2017-02-01", - ).scenarios[1] -end -system_load(sc) = sum(b.load for b in sc.buses) -test_approx(x, y) = @test isapprox(x, y, atol = 1e-3) - -@testset "XavQiuAhm2021" begin - @testset "cost and load share" begin - sc = get_scenario() - # Check original costs - unit = sc.thermal_units[10] - test_approx(unit.min_power_cost[1], 825.023) - test_approx(unit.cost_segments[1].cost[1], 36.659) - test_approx(unit.startup_categories[1].cost[1], 7570.42) - - # Check original load share - bus = sc.buses[1] - prev_system_load = system_load(sc) - test_approx(bus.load[1] / prev_system_load[1], 0.012) - - randomize!( - sc, - XavQiuAhm2021.Randomization(randomize_load_profile = false), - rng = MersenneTwister(42), - ) - - # Check randomized costs - test_approx(unit.min_power_cost[1], 831.977) - test_approx(unit.cost_segments[1].cost[1], 36.968) - test_approx(unit.startup_categories[1].cost[1], 7634.226) - - # Check randomized load share - curr_system_load = system_load(sc) - test_approx(bus.load[1] / curr_system_load[1], 0.013) - - # System load should not change - @test prev_system_load ≈ curr_system_load - end - - @testset "load profile" begin - sc = get_scenario() - # Check original load profile - @test round.(system_load(sc), digits = 1)[1:8] ≈ - [3059.5, 2983.2, 2937.5, 2953.9, 3073.1, 3356.4, 4068.5, 4018.8] - - randomize!(sc, XavQiuAhm2021.Randomization(); rng = MersenneTwister(42)) - - # Check randomized load profile - @test round.(system_load(sc), digits = 1)[1:8] ≈ - [4854.7, 4849.2, 4732.7, 4848.2, 4948.4, 5231.1, 5874.8, 5934.8] - end - - @testset "profiled unit cost" begin - sc = - UnitCommitment.read("$FIXTURES/case14-profiled.json.gz").scenarios[1] - # Check original costs - pu1 = sc.profiled_units[1] - pu2 = sc.profiled_units[2] - test_approx(pu1.cost[1], 100.0) - test_approx(pu2.cost[1], 50.0) - randomize!( - sc, - XavQiuAhm2021.Randomization(randomize_load_profile = false), - rng = MersenneTwister(42), - ) - # Check randomized costs - test_approx(pu1.cost[1], 98.039) - test_approx(pu2.cost[1], 48.385) - end -end diff --git a/test/transform/slice_test.jl b/test/transform/slice_test.jl deleted file mode 100644 index 6519cbd..0000000 --- a/test/transform/slice_test.jl +++ /dev/null @@ -1,66 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, LinearAlgebra, Cbc, JuMP, JSON, GZip - -@testset "slice" begin - instance = UnitCommitment.read("$FIXTURES/case14.json.gz") - modified = UnitCommitment.slice(instance, 1:2) - sc = modified.scenarios[1] - - # Should update all time-dependent fields - @test modified.time == 2 - @test length(sc.power_balance_penalty) == 2 - @test length(sc.reserves_by_name["r1"].amount) == 2 - for u in sc.thermal_units - @test length(u.max_power) == 2 - @test length(u.min_power) == 2 - @test length(u.must_run) == 2 - @test length(u.min_power_cost) == 2 - for s in u.cost_segments - @test length(s.mw) == 2 - @test length(s.cost) == 2 - end - end - for b in sc.buses - @test length(b.load) == 2 - end - for l in sc.lines - @test length(l.normal_flow_limit) == 2 - @test length(l.emergency_flow_limit) == 2 - @test length(l.flow_limit_penalty) == 2 - end - for ps in sc.price_sensitive_loads - @test length(ps.demand) == 2 - @test length(ps.revenue) == 2 - end - - # Should be able to build model without errors - optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) - model = UnitCommitment.build_model( - instance = modified, - optimizer = optimizer, - variable_names = true, - ) -end - -@testset "slice profiled units" begin - instance = UnitCommitment.read("$FIXTURES/case14-profiled.json.gz") - modified = UnitCommitment.slice(instance, 1:2) - sc = modified.scenarios[1] - - # Should update all time-dependent fields - for pu in sc.profiled_units - @test length(pu.max_power) == 2 - @test length(pu.min_power) == 2 - end - - # Should be able to build model without errors - optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) - model = UnitCommitment.build_model( - instance = modified, - optimizer = optimizer, - variable_names = true, - ) -end diff --git a/test/usage.jl b/test/usage.jl deleted file mode 100644 index 63b3f36..0000000 --- a/test/usage.jl +++ /dev/null @@ -1,62 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, LinearAlgebra, Cbc, JuMP, JSON - -function _set_flow_limits!(instance) - for sc in instance.scenarios - sc.power_balance_penalty = [100_000 for _ in 1:instance.time] - for line in sc.lines, t in 1:4 - line.normal_flow_limit[t] = 10.0 - end - end -end - -@testset "usage" begin - @testset "deterministic" begin - instance = UnitCommitment.read("$FIXTURES/case14.json.gz") - _set_flow_limits!(instance) - optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) - model = UnitCommitment.build_model( - instance = instance, - optimizer = optimizer, - variable_names = true, - ) - @test name(model[:is_on]["g1", 1]) == "is_on[g1,1]" - - # Optimize and retrieve solution - UnitCommitment.optimize!(model) - solution = UnitCommitment.solution(model) - - # Write solution to a file - filename = tempname() - UnitCommitment.write(filename, solution) - loaded = JSON.parsefile(filename) - @test length(loaded["Is on"]) == 6 - - # Verify solution - @test UnitCommitment.validate(instance, solution) - - # Reoptimize with fixed solution - UnitCommitment.fix!(model, solution) - UnitCommitment.optimize!(model) - @test UnitCommitment.validate(instance, solution) - end - - @testset "stochastic" begin - instance = UnitCommitment.read([ - "$FIXTURES/case14.json.gz", - "$FIXTURES/case14.json.gz", - ]) - _set_flow_limits!(instance) - @test length(instance.scenarios) == 2 - optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0) - model = UnitCommitment.build_model( - instance = instance, - optimizer = optimizer, - ) - UnitCommitment.optimize!(model) - solution = UnitCommitment.solution(model) - end -end diff --git a/test/validation/repair_test.jl b/test/validation/repair_test.jl deleted file mode 100644 index 060ca9f..0000000 --- a/test/validation/repair_test.jl +++ /dev/null @@ -1,39 +0,0 @@ -# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment -# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. -# Released under the modified BSD license. See COPYING.md for more details. - -using UnitCommitment, JSON, GZip, DataStructures - -function parse_case14() - return JSON.parse( - GZip.gzopen("$FIXTURES/case14.json.gz"), - dicttype = () -> DefaultOrderedDict(nothing), - ) -end - -@testset "repair!" begin - @testset "Cost curve should be convex" begin - json = parse_case14() - json["Generators"]["g1"]["Production cost curve (MW)"] = [100, 150, 200] - json["Generators"]["g1"]["Production cost curve (\$)"] = [10, 25, 30] - sc = UnitCommitment._from_json(json, repair = false) - @test UnitCommitment.repair!(sc) == 4 - end - - @testset "Startup limit must be greater than Pmin" begin - json = parse_case14() - json["Generators"]["g1"]["Production cost curve (MW)"] = [100, 150] - json["Generators"]["g1"]["Production cost curve (\$)"] = [100, 150] - json["Generators"]["g1"]["Startup limit (MW)"] = 80 - sc = UnitCommitment._from_json(json, repair = false) - @test UnitCommitment.repair!(sc) == 1 - end - - @testset "Startup costs and delays must be increasing" begin - json = parse_case14() - json["Generators"]["g1"]["Startup costs (\$)"] = [300, 200, 100] - json["Generators"]["g1"]["Startup delays (h)"] = [8, 4, 2] - sc = UnitCommitment._from_json(json, repair = false) - @test UnitCommitment.repair!(sc) == 4 - end -end From 9dc3607c56e822ecf3b3a9d09d0cadb876853c0e Mon Sep 17 00:00:00 2001 From: oyurdakul Date: Mon, 22 May 2023 16:41:00 -0500 Subject: [PATCH 2/5] progressive hedging --- Project.toml | 4 + docs/src/format.md | 8 +- docs/src/index.md | 2 +- docs/src/model.md | 60 +++- docs/src/usage.md | 87 +++++- src/UnitCommitment.jl | 4 + .../methods/ProgressiveHedging/optimize.jl | 265 ++++++++++++++++++ .../methods/ProgressiveHedging/read.jl | 18 ++ .../methods/ProgressiveHedging/solution.jl | 86 ++++++ .../methods/ProgressiveHedging/structs.jl | 130 +++++++++ 10 files changed, 646 insertions(+), 18 deletions(-) create mode 100644 src/solution/methods/ProgressiveHedging/optimize.jl create mode 100644 src/solution/methods/ProgressiveHedging/read.jl create mode 100644 src/solution/methods/ProgressiveHedging/solution.jl create mode 100644 src/solution/methods/ProgressiveHedging/structs.jl diff --git a/Project.toml b/Project.toml index 9c3832e..6aa6aeb 100644 --- a/Project.toml +++ b/Project.toml @@ -18,6 +18,8 @@ PackageCompiler = "9b87118b-4619-50d2-8e1e-99f35a4d4d9d" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" +MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195" [compat] DataStructures = "0.18" @@ -26,5 +28,7 @@ GZip = "0.5" JSON = "0.21" JuMP = "1" MathOptInterface = "1" +MPI = "0.20.8" PackageCompiler = "1" julia = "1" +TimerOutputs = "0.5.23" \ No newline at end of file diff --git a/docs/src/format.md b/docs/src/format.md index 267da58..24b8de3 100644 --- a/docs/src/format.md +++ b/docs/src/format.md @@ -4,7 +4,7 @@ Data Format Input Data Format ----------------- -Instances are specified by JSON files containing the following main sections: +To create the JuMP model for a two-stage security constrained unit commitment (SUC) problem, UC.jl creates an instance that is composed of one or multiple scenarios. For each scenario, UC.jl expects a JSON file that describes the parameters of the problem, as well as the weight and the name of each respective scenario. Note that the input data format required for modeling the deterministic security-constrained unit commitment (SCUC) problem also follows the format mapped out below, as SCUC is treated by the package as the special case of the SUC problem with one scenario. The requisite input data format contains the following fields: * [Parameters](#Parameters) * [Buses](#Buses) @@ -26,6 +26,8 @@ This section describes system-wide parameters, such as power balance penalty, an | `Time horizon (h)` | Length of the planning horizon (in hours). | Required | N | `Time step (min)` | Length of each time step (in minutes). Must be a divisor of 60 (e.g. 60, 30, 20, 15, etc). | `60` | N | `Power balance penalty ($/MW)` | Penalty for system-wide shortage or surplus in production (in $/MW). This is charged per time step. For example, if there is a shortage of 1 MW for three time steps, three times this amount will be charged. | `1000.0` | Y +| `Scenario name` | Name of the scenario. | `s1` | N +| `Scenario weight` | Weight of the scenario. The scenario weight can be any positive real number, that is, it does not have to be between zero and one. The package normalizes the weights to ensure that the probability of all scenarios sum up to one. | one divided by the total number of scenarios | N #### Example @@ -34,7 +36,9 @@ This section describes system-wide parameters, such as power balance penalty, an "Parameters": { "Version": "0.3", "Time horizon (h)": 4, - "Power balance penalty ($/MW)": 1000.0 + "Power balance penalty ($/MW)": 1000.0, + "Scenario name": "s1", + "Scenario weight": 0.5 } } ``` diff --git a/docs/src/index.md b/docs/src/index.md index 9a8a013..752fb64 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -1,6 +1,6 @@ # UnitCommitment.jl -**UnitCommitment.jl** (UC.jl) is a Julia/JuMP optimization package for the Security-Constrained Unit Commitment Problem (SCUC), a fundamental optimization problem in power systems used, for example, to clear the day-ahead electricity markets. The package provides benchmark instances for the problem and Julia/JuMP implementations of state-of-the-art mixed-integer programming formulations. +**UnitCommitment.jl** (UC.jl) is a Julia/JuMP optimization package for modeling and solving the two-stage Stochastic Unit Commitment (SUC) problem, which permits the representation of uncertainty in a broad range of problem parameters. As a special case of the SUC problem, UC.jl allows for modeling and solving the Security-Constrained Unit Commitment Problem (SCUC), a fundamental optimization problem in power systems used, for example, to clear the day-ahead electricity markets. The package provides benchmark instances for the problem and Julia/JuMP implementations of state-of-the-art mixed-integer programming formulations. ## Package Components diff --git a/docs/src/model.md b/docs/src/model.md index d944b07..f57648d 100644 --- a/docs/src/model.md +++ b/docs/src/model.md @@ -5,9 +5,16 @@ In this page, we describe the JuMP optimization model produced by the function ` Decision variables ------------------ +In this section, we describe the decision variables of the JuMP model produced by the function `UnitCommitment.build_model`. + +Recall that the UC.jl package allows for modeling and solving the SUC problem, wherein a broad range problem parameters can vary across user-defined scenarios. To facilitate the modeling of the SUC problem, some of the decision variables of the JuMP model are modeled as first-stage decisions, which are here-and-now decisions taken before the uncertainty is realized and thus are the same across all scenarios. As laid out below, the first-stage decisions relate primarily to the binary decisions of the generators. + +In contrast, most of the decision variables of the JuMP model are second-stage decisions that can attain a different value in each scenario. Accordingly, the second-stage decision variables described below are indexed by the term `sn`. The term `sn` is preserved even if the package is used to model and solve the deterministic security-constrained unit commitment (SCUC) problem, which is currently treated by the package as the special case of the SUC model with only one scenario, having the default scenario index `"s1"`. ### Generators +In this section, we describe the decision variables associated with the generators, which include both thermal units (e.g., natural gas-fired power plant) and profiled units (e.g., wind turbine). + #### Thermal Units Name | Symbol | Description | Unit @@ -15,11 +22,15 @@ Name | Symbol | Description | Unit `is_on[g,t]` | $u_{g}(t)$ | True if generator `g` is on at time `t`. | Binary `switch_on[g,t]` | $v_{g}(t)$ | True is generator `g` switches on at time `t`. | Binary `switch_off[g,t]` | $w_{g}(t)$ | True if generator `g` switches off at time `t`. | Binary -`prod_above[g,t]` |$p'_{g}(t)$ | Amount of power produced by generator `g` above its minimum power output at time `t`. For example, if the minimum power of generator `g` is 100 MW and `g` is producing 115 MW of power at time `t`, then `prod_above[g,t]` equals `15.0`. | MW -`segprod[g,t,k]` | $p^k_g(t)$ | Amount of power from piecewise linear segment `k` produced by generator `g` at time `t`. For example, if cost curve for generator `g` is defined by the points `(100, 1400)`, `(110, 1600)`, `(130, 2200)` and `(135, 2400)`, and if the generator is producing 115 MW of power at time `t`, then `segprod[g,t,:]` equals `[10.0, 5.0, 0.0]`.| MW -`reserve[r,g,t]` | $r_g(t)$ | Amount of reserve `r` provided by unit `g` at time `t`. | MW `startup[g,t,s]` | $\delta^s_g(t)$ | True if generator `g` switches on at time `t` incurring start-up costs from start-up category `s`. | Binary +`prod_above[sn,g,t]` |$p'_{g}(t)$ | Amount of power produced by generator `g` above its minimum power output at time `t` in scenario `sn`. For example, if the minimum power of generator `g` is 100 MW and `g` is producing 115 MW of power at time `t` in scenario `sn`, then `prod_above[sn,g,t]` equals `15.0`. | MW +`segprod[sn,g,t,k]` | $p^k_g(t)$ | Amount of power from piecewise linear segment `k` produced by generator `g` at time `t` in scenario `sn`. For example, if cost curve for generator `g` is defined by the points `(100, 1400)`, `(110, 1600)`, `(130, 2200)` and `(135, 2400)`, and if the generator is producing 115 MW of power at time `t` in scenario `sn`, then `segprod[sn,g,t,:]` equals `[10.0, 5.0, 0.0]`.| MW +`reserve[sn,r,g,t]` | $r_g(t)$ | Amount of reserve `r` provided by unit `g` at time `t` in scenario `sn`. | MW + +!!! warning + The first-stage decision variables of the JuMP model are `is_on[g,t]`, `switch_on[g,t]`, `switch_off[g,t]`, and `startup[g,t,s]`. As such, the dictionaries corresponding to these variables do not include the scenario index in their keys. In contrast, all other variables of the created JuMP model are allowed to obtain a different value in each scenario and are thus modeled as second-stage decision variables. Accordingly, the dictionaries of all second-stage decision variables have the scenario index in their keys. This is true even if the model is created to solve the deterministic SCUC, in which case the default scenario index `s1` is included in the dictionary key. + #### Profiled Units @@ -32,26 +43,26 @@ Name | Symbol | Description | Unit Name | Symbol | Description | Unit :-----|:------:|:-------------|:------: -`net_injection[b,t]` | $n_b(t)$ | Net injection at bus `b` at time `t`. | MW -`curtail[b,t]` | $s^+_b(t)$ | Amount of load curtailed at bus `b` at time `t` | MW +`net_injection[sn,b,t]` | $n_b(t)$ | Net injection at bus `b` at time `t` in scenario `sn`. | MW +`curtail[sn,b,t]` | $s^+_b(t)$ | Amount of load curtailed at bus `b` at time `t` in scenario `sn`. | MW ### Price-sensitive loads Name | Symbol | Description | Unit :-----|:------:|:-------------|:------: -`loads[s,t]` | $d_{s}(t)$ | Amount of power served to price-sensitive load `s` at time `t`. | MW +`loads[sn,s,t]` | $d_{s}(t)$ | Amount of power served to price-sensitive load `s` at time `t` in scenario `sn`. | MW ### Transmission lines Name | Symbol | Description | Unit :-----|:------:|:-------------|:------: -`flow[l,t]` | $f_l(t)$ | Power flow on line `l` at time `t`. | MW -`overflow[l,t]` | $f^+_l(t)$ | Amount of flow above the limit for line `l` at time `t`. | MW +`flow[sn,l,t]` | $f_l(t)$ | Power flow on line `l` at time `t` in scenario `sn`. | MW +`overflow[sn,l,t]` | $f^+_l(t)$ | Amount of flow above the limit for line `l` at time `t` in scenario `sn`. | MW !!! warning - Since transmission and N-1 security constraints are enforced in a lazy way, most of the `flow[l,t]` variables are never added to the model. Accessing `model[:flow][l,t]` without first checking that the variable exists will likely generate an error. + Since transmission and N-1 security constraints are enforced in a lazy way, most of the `flow[l,t]` variables are never added to the model. Accessing `model[:flow][sn,l,t]` without first checking that the variable exists will likely generate an error. Objective function ------------------ @@ -106,7 +117,12 @@ end ### Fixing variables, modifying objective function and adding constraints -Since we now have a direct reference to the JuMP decision variables, it is possible to fix variables, change the coefficients in the objective function, or even add new constraints to the model before solving it. The script below shows how can this be accomplished. For more information on modifying an existing model, [see the JuMP documentation](https://jump.dev/JuMP.jl/stable/manual/variables/). +Since we now have a direct reference to the JuMP decision variables, it is possible to fix variables, change the coefficients in the objective function, or even add new constraints to the model before solving it. +!!! warning + + It is important to take into account the stage of the decision variables in modifying the optimization model. In changing a deterministic SCUC model, modifying the second-stage decision variables requires adding the term `s1`, which is the default scenario name assigned to the second-stage decision variables in the SCUC model. For an SUC model, the package permits the modification of the second-stage decision variables individually for each scenario. + +The script below shows how the JuMP model can be modified after it is created. For more information on modifying an existing model, [see the JuMP documentation](https://jump.dev/JuMP.jl/stable/manual/variables/). ```julia using Cbc @@ -122,13 +138,31 @@ model = UnitCommitment.build_model( optimizer=Cbc.Optimizer, ) -# Fix a decision variable to 1.0 +# Fix the commitment status of the generator "g1" in time period 1 to 1.0 JuMP.fix( model[:is_on]["g1",1], 1.0, force=true, ) +# Fix the production level of the generator "g1" above its minimum level in time period 1 and +# in scenario "s1" to 20.0 MW. Observe that the three-tuple dictionary key involves the scenario +# index "s1", as production above minimum is a second-stage decision variable. + +JuMP.fix( + model[:prod_above]["s1", "g1", 1], + 20.0, + force=true, +) + +# Enforce the curtailment of 20.0 MW of load at bus "b2" in time period 4 in scenario "s10". +JuMP.fix( + curtail["s10", "b2", 4] = + 20.0, + force=true, +) + + # Change the objective function JuMP.set_objective_coefficient( model, @@ -178,10 +212,10 @@ for t in 1:T # In this example, we assume a cost of $5/MW. set_objective_coefficient(model, x[t], 5.0) - # Attach the new component to bus b1, by modifying the + # Attach the new component to bus b1 in scenario s1, by modifying the # constraint `eq_net_injection`. set_normalized_coefficient( - model[:eq_net_injection]["b1", t], + model[:eq_net_injection]["s1", "b1", t], x[t], 1.0, ) diff --git a/docs/src/usage.md b/docs/src/usage.md index 26d1779..cc0281a 100644 --- a/docs/src/usage.md +++ b/docs/src/usage.md @@ -25,7 +25,7 @@ Typical Usage ### Solving user-provided instances -The first step to use UC.jl is to construct a JSON file describing your unit commitment instance. See [Data Format](format.md) for a complete description of the data format UC.jl expects. The next steps, as shown below, are to: (1) read the instance from file; (2) construct the optimization model; (3) run the optimization; and (4) extract the optimal solution. +The first step to use UC.jl is to construct a JSON file that describes each scenario of your unit commitment instance. See [Data Format](format.md) for a complete description of the data format UC.jl expects. The next steps, as shown below, are to: (1) construct the instance using scenario files; (2) build the optimization model; (3) run the optimization; and (4) extract the optimal solution. ```julia using Cbc @@ -33,7 +33,7 @@ using JSON using UnitCommitment # 1. Read instance -instance = UnitCommitment.read("/path/to/input.json") +instance = UnitCommitment.read(["/path/to/s1.json", "/path/to/s2.json"]) # 2. Construct optimization model model = UnitCommitment.build_model( @@ -49,6 +49,15 @@ solution = UnitCommitment.solution(model) UnitCommitment.write("/path/to/output.json", solution) ``` +The above lines of code can also be used for solving the deterministic Security-Constrained Unit Commitment (SCUC) problem, which will create an instance based on a single scenario. The unit commitment instance for SCUC can alternatively be constructed as + +```julia +# 1. Read instance +instance = UnitCommitment.read("/path/to/input.json") +``` + + + ### Solving benchmark instances UnitCommitment.jl contains a large number of benchmark instances collected from the literature and converted into a common data format. To solve one of these instances individually, instead of constructing your own, the function `read_benchmark` can be used, as shown below. See [Instances](instances.md) for the complete list of available instances. @@ -136,6 +145,80 @@ solution = JSON.parsefile("solution.json") # Validate solution and print validation errors UnitCommitment.validate(instance, solution) ``` +## Modeling and Solving the SUC Problem + +To model the SUC problem, UC.jl supports reading scenario files at a specified directory using the `Glob` package. For instance, in order to construct a SUC instance using all JSON files at a given directory, where each JSON file describes one scenario, the following line of code can be used: + +```julia +instance = UnitCommitment.read(glob("*.json", "/path/to/scenarios/")) +``` + +Alternatively, the specific vector of scenario files can also be passed as follows: +```julia +instance = UnitCommitment.read(["/path/to/s1.json", "/path/to/s2.json"])) +``` + +## Solving the SUC Problem + +We next lay out the alternative methods supported by UC.jl for solving the SUC problem. + +## Solving the Extensive Form of the SUC Problem + +By default, UC.jl solves the extensive form of the SUC problem. + +```julia +UnitCommitment.optimize!(model) +solution = UnitCommitment.solution(model) +UnitCommitment.write("/path/to/output.json", solution) +``` + +Note that the created `solution` dictionary will include both the optimal first-stage decisions, as well as the optimal second-stage decisions under all scenarios. + +## Solving the SUC Problem Using Progressive Hedging + +Importantly, UC.jl further provides the option of solving the SUC problem using the progressive hedging (PH) algorithm, which is an algorithm closely related to the alternating direction method of multipliers (ADMM). To that end, the package supports solving the PH subproblem associated with each scenario in parallel in a separate Julia process, where the communication among the Julia processes is provided using the Message Passing Interface or MPI. + +The solve the SUC problem using Progressive Hedging, you may run the following line of code, which will create `NUM_OF_PROCS` processes where each process executes the `ph.jl` file. + +```julia +using MPI +const FILENAME = "ph.jl" +const NUM_OF_PROCS = 5 + +mpiexec(exe -> run(`$exe -n $NUM_OF_PROCS $(Base.julia_cmd()) $FILENAME`)) +``` + +#### **`ph.jl`** +```julia +using MPI: MPI_Info +using Gurobi, MPI, UnitCommitment, Glob +import UnitCommitment: ProgressiveHedging + +MPI.Init() +ph = ProgressiveHedging.Method() +instance = UnitCommitment.read( + glob("*.json", "/path/to/scenarios/"), + ph +) +model = UnitCommitment.build_model( + instance = instance, + optimizer = Gurobi.Optimizer, + ) +UnitCommitment.optimize!(model, ph) +solution = UnitCommitment.solution(model, ph) +MPI.Finalize() +``` + +Observe that the `read`, `build_model`, and `solution` methods take the `ph` object as an argument, which is of type `Progressive Hedging`. + +The subproblem solved within each Julia process deduces the number of scenarios it needs to model using the total number of scenarios and the total number of processes. For instance, if `glob("*.json", "/path/to/scenarios/")` returns a vector of 15 scenario file paths and `NUM_OF_PROCS = 5`, then each subproblem will model and solve 3 scenarios. If the total number of scenarios is not divisible by `NUM_OF_PROCS`, then the read method will throw an error. + +The `solution(model, ph)` method gathers the optimal solution of all processes and returns a dictionary that contains all optimal first-stage decisions as well as all optimal second-stage decisions evaluated for each scenario. + +!!! warning + + Currently, PH can handle only equiprobable scenarios. Further, `solution(model, ph)` can only handle cases where only one scenario is modeled in each process. + ## Computing Locational Marginal Prices diff --git a/src/UnitCommitment.jl b/src/UnitCommitment.jl index 3191395..40016f7 100644 --- a/src/UnitCommitment.jl +++ b/src/UnitCommitment.jl @@ -19,6 +19,7 @@ include("model/formulations/KnuOstWat2018/structs.jl") include("model/formulations/MorLatRam2013/structs.jl") include("model/formulations/PanGua2016/structs.jl") include("solution/methods/XavQiuWanThi2019/structs.jl") +include("solution/methods/ProgressiveHedging/structs.jl") include("model/formulations/WanHob2016/structs.jl") include("import/egret.jl") @@ -49,6 +50,9 @@ include("solution/methods/XavQiuWanThi2019/enforce.jl") include("solution/methods/XavQiuWanThi2019/filter.jl") include("solution/methods/XavQiuWanThi2019/find.jl") include("solution/methods/XavQiuWanThi2019/optimize.jl") +include("solution/methods/ProgressiveHedging/optimize.jl") +include("solution/methods/ProgressiveHedging/read.jl") +include("solution/methods/ProgressiveHedging/solution.jl") include("solution/optimize.jl") include("solution/solution.jl") include("solution/warmstart.jl") diff --git a/src/solution/methods/ProgressiveHedging/optimize.jl b/src/solution/methods/ProgressiveHedging/optimize.jl new file mode 100644 index 0000000..c6831e5 --- /dev/null +++ b/src/solution/methods/ProgressiveHedging/optimize.jl @@ -0,0 +1,265 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. +using MPI, Printf +using TimerOutputs +import JuMP +const to = TimerOutput() + +function optimize!( + model::JuMP.Model, + method::ProgressiveHedging.Method, +)::ProgressiveHedging.FinalResult + mpi = ProgressiveHedging.MpiInfo(MPI.COMM_WORLD) + iterations = Array{ProgressiveHedging.IterationInfo,1}(undef, 0) + if method.consensus_vars === nothing + method.consensus_vars = + [var for var in all_variables(model) if is_binary(var)] + end + nvars = length(method.consensus_vars) + if method.weights === nothing + method.weights = [1.0 for _ in 1:nvars] + end + if method.initial_global_consensus_vals === nothing + method.initial_global_consensus_vals = [0.0 for _ in 1:nvars] + end + + ph_sp_params = ProgressiveHedging.SpParams( + ρ = method.ρ, + λ = [method.λ_default for _ in 1:nvars], + global_consensus_vals = method.initial_global_consensus_vals, + ) + ph_subproblem = ProgressiveHedging.SubProblem( + model, + model[:obj], + method.consensus_vars, + method.weights, + ) + set_optimizer_attribute(model, "Threads", method.num_of_threads) + while true + it_time = @elapsed begin + solution = solve_subproblem(ph_subproblem, ph_sp_params) + MPI.Barrier(mpi.comm) + global_obj = compute_global_objective(mpi, solution) + global_consensus_vals = compute_global_consensus(mpi, solution) + update_λ_and_residuals!( + solution, + ph_sp_params, + global_consensus_vals, + ) + global_infeas = compute_global_infeasibility(solution, mpi) + global_residual = compute_global_residual(mpi, solution) + if has_numerical_issues(global_consensus_vals) + break + end + end + total_elapsed_time = compute_total_elapsed_time(it_time, iterations) + it = ProgressiveHedging.IterationInfo( + it_num = length(iterations) + 1, + sp_consensus_vals = solution.consensus_vals, + global_consensus_vals = global_consensus_vals, + sp_obj = solution.obj, + global_obj = global_obj, + it_time = it_time, + total_elapsed_time = total_elapsed_time, + global_residual = global_residual, + global_infeas = global_infeas, + ) + iterations = [iterations; it] + print_progress(mpi, it, method.print_interval) + if should_stop(mpi, iterations, method.termination_criteria) + break + end + end + + return ProgressiveHedging.FinalResult( + last(iterations).global_obj, + last(iterations).sp_consensus_vals, + last(iterations).global_infeas, + last(iterations).it_num, + last(iterations).total_elapsed_time, + ) +end + +function compute_total_elapsed_time( + it_time::Float64, + iterations::Array{ProgressiveHedging.IterationInfo,1}, +)::Float64 + length(iterations) > 0 ? + current_total_time = last(iterations).total_elapsed_time : + current_total_time = 0 + return current_total_time + it_time +end + +function compute_global_objective( + mpi::ProgressiveHedging.MpiInfo, + s::ProgressiveHedging.SpSolution, +)::Float64 + global_obj = MPI.Allreduce(s.obj, MPI.SUM, mpi.comm) + global_obj /= mpi.nprocs + return global_obj +end + +function compute_global_consensus( + mpi::ProgressiveHedging.MpiInfo, + s::ProgressiveHedging.SpSolution, +)::Array{Float64,1} + sp_consensus_vals = s.consensus_vals + global_consensus_vals = MPI.Allreduce(sp_consensus_vals, MPI.SUM, mpi.comm) + global_consensus_vals = global_consensus_vals / mpi.nprocs + return global_consensus_vals +end + +function compute_global_residual( + mpi::ProgressiveHedging.MpiInfo, + s::ProgressiveHedging.SpSolution, +)::Float64 + n_vars = length(s.consensus_vals) + local_residual_sum = abs.(s.residuals) + global_residual_sum = MPI.Allreduce(local_residual_sum, MPI.SUM, mpi.comm) + return sum(global_residual_sum) / n_vars +end + +function compute_global_infeasibility( + solution::ProgressiveHedging.SpSolution, + mpi::ProgressiveHedging.MpiInfo, +)::Float64 + local_infeasibility = norm(solution.residuals) + global_infeas = MPI.Allreduce(local_infeasibility, MPI.SUM, mpi.comm) + return global_infeas +end + +function solve_subproblem( + sp::ProgressiveHedging.SubProblem, + ph_sp_params::ProgressiveHedging.SpParams, +)::ProgressiveHedging.SpSolution + G = length(sp.consensus_vars) + if norm(ph_sp_params.λ) < 1e-3 + @objective(sp.mip, Min, sp.obj) + else + @objective( + sp.mip, + Min, + sp.obj + + sum( + sp.weights[g] * + ph_sp_params.λ[g] * + (sp.consensus_vars[g] - ph_sp_params.global_consensus_vals[g]) + for g in 1:G + ) + + (ph_sp_params.ρ / 2) * sum( + sp.weights[g] * + ( + sp.consensus_vars[g] - + ph_sp_params.global_consensus_vals[g] + )^2 for g in 1:G + ) + ) + end + optimize!(sp.mip, XavQiuWanThi2019.Method()) + obj = objective_value(sp.mip) + sp_consensus_vals = value.(sp.consensus_vars) + return ProgressiveHedging.SpSolution( + obj = obj, + consensus_vals = sp_consensus_vals, + residuals = zeros(G), + ) +end + +function update_λ_and_residuals!( + solution::ProgressiveHedging.SpSolution, + ph_sp_params::ProgressiveHedging.SpParams, + global_consensus_vals::Array{Float64,1}, +)::Nothing + n_vars = length(solution.consensus_vals) + ph_sp_params.global_consensus_vals = global_consensus_vals + for n in 1:n_vars + solution.residuals[n] = + solution.consensus_vals[n] - ph_sp_params.global_consensus_vals[n] + ph_sp_params.λ[n] += ph_sp_params.ρ * solution.residuals[n] + end +end + +function print_header(mpi::ProgressiveHedging.MpiInfo)::Nothing + if !mpi.root + return + end + @info "Solving via Progressive Hedging:" + @info @sprintf( + "%8s %20s %20s %14s %8s %8s", + "iter", + "obj", + "infeas", + "consensus", + "time-it", + "time" + ) +end + +function print_progress( + mpi::ProgressiveHedging.MpiInfo, + iteration::ProgressiveHedging.IterationInfo, + print_interval, +)::Nothing + if !mpi.root + return + end + if iteration.it_num % print_interval != 0 + return + end + @info @sprintf( + "Current iteration %8d %20.6e %20.6e %12.2f %% %8.2f %8.2f", + iteration.it_num, + iteration.global_obj, + iteration.global_infeas, + iteration.global_residual * 100, + iteration.it_time, + iteration.total_elapsed_time + ) +end + +function has_numerical_issues(target::Array{Float64,1})::Bool + if target == NaN + @warn "Numerical issues detected. Stopping." + return true + end + return false +end + +function should_stop( + mpi::ProgressiveHedging.MpiInfo, + iterations::Array{ProgressiveHedging.IterationInfo,1}, + criteria::ProgressiveHedging.TerminationCriteria, +)::Bool + if length(iterations) >= criteria.max_iterations + if mpi.root + @info "Iteration limit reached. Stopping." + end + return true + end + + if length(iterations) < criteria.min_iterations + return false + end + + if last(iterations).total_elapsed_time > criteria.max_time + if mpi.root + @info "Time limit reached. Stopping." + end + return true + end + + curr_it = last(iterations) + prev_it = iterations[length(iterations)-1] + + if curr_it.global_infeas < criteria.min_feasibility + obj_change = abs(prev_it.global_obj - curr_it.global_obj) + if obj_change < criteria.min_improvement + if mpi.root + @info "Feasibility limit reached. Stopping." + end + return true + end + end + return false +end diff --git a/src/solution/methods/ProgressiveHedging/read.jl b/src/solution/methods/ProgressiveHedging/read.jl new file mode 100644 index 0000000..476b8b7 --- /dev/null +++ b/src/solution/methods/ProgressiveHedging/read.jl @@ -0,0 +1,18 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +function read( + paths::Vector{String}, + method::ProgressiveHedging.Method, +)::UnitCommitmentInstance + comm = MPI.COMM_WORLD + mpi = ProgressiveHedging.MpiInfo(comm) + (length(paths) % mpi.nprocs == 0) || error( + "Number of processes $(mpi.nprocs) is not a divisor of $(length(paths))", + ) + bundled_scenarios = length(paths) ÷ mpi.nprocs + sc_num_start = (mpi.rank - 1) * bundled_scenarios + 1 + sc_num_end = mpi.rank * bundled_scenarios + return read(paths[sc_num_start:sc_num_end]) +end diff --git a/src/solution/methods/ProgressiveHedging/solution.jl b/src/solution/methods/ProgressiveHedging/solution.jl new file mode 100644 index 0000000..b06ad89 --- /dev/null +++ b/src/solution/methods/ProgressiveHedging/solution.jl @@ -0,0 +1,86 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using MPI, DataStructures +const FIRST_STAGE_VARS = ["Is on", "Switch on", "Switch off"] + +function solution( + model::JuMP.Model, + method::ProgressiveHedging.Method, +)::OrderedDict + comm = MPI.COMM_WORLD + mpi = ProgressiveHedging.MpiInfo(comm) + sp_solution = UnitCommitment.solution(model) + gather_solution = OrderedDict() + for (solution_key, dict) in sp_solution + if solution_key !== "Spinning reserve (MW)" && + solution_key ∉ FIRST_STAGE_VARS + push!(gather_solution, solution_key => OrderedDict()) + for (gen_bus_key, values) in dict + global T = length(values) + receive_values = + MPI.UBuffer(Vector{Float64}(undef, T * mpi.nprocs), T) + MPI.Gather!(float.(values), receive_values, comm) + if mpi.root + push!( + gather_solution[solution_key], + gen_bus_key => receive_values.data, + ) + end + end + end + end + push!(gather_solution, "Spinning reserve (MW)" => OrderedDict()) + for (reserve_type, dict) in sp_solution["Spinning reserve (MW)"] + push!( + gather_solution["Spinning reserve (MW)"], + reserve_type => OrderedDict(), + ) + for (gen_key, values) in dict + receive_values = + MPI.UBuffer(Vector{Float64}(undef, T * mpi.nprocs), T) + MPI.Gather!(float.(values), receive_values, comm) + if mpi.root + push!( + gather_solution["Spinning reserve (MW)"][reserve_type], + gen_key => receive_values.data, + ) + end + end + end + aggregate_solution = OrderedDict() + if mpi.root + for first_stage_var in FIRST_STAGE_VARS + aggregate_solution[first_stage_var] = OrderedDict() + for gen_key in keys(sp_solution[first_stage_var]) + aggregate_solution[first_stage_var][gen_key] = + sp_solution[first_stage_var][gen_key] + end + end + for i in 1:mpi.nprocs + push!(aggregate_solution, "s$i" => OrderedDict()) + for (solution_key, solution_dict) in gather_solution + push!(aggregate_solution["s$i"], solution_key => OrderedDict()) + if solution_key !== "Spinning reserve (MW)" + for (gen_bus_key, values) in solution_dict + aggregate_solution["s$i"][solution_key][gen_bus_key] = + gather_solution[solution_key][gen_bus_key][(i-1)*T+1:i*T] + end + else + for (reserve_name, reserve_dict) in solution_dict + push!( + aggregate_solution["s$i"][solution_key], + reserve_name => OrderedDict(), + ) + for (gen_key, values) in reserve_dict + aggregate_solution["s$i"][solution_key][reserve_name][gen_key] = + gather_solution[solution_key][reserve_name][gen_key][(i-1)*T+1:i*T] + end + end + end + end + end + end + return aggregate_solution +end diff --git a/src/solution/methods/ProgressiveHedging/structs.jl b/src/solution/methods/ProgressiveHedging/structs.jl new file mode 100644 index 0000000..b339813 --- /dev/null +++ b/src/solution/methods/ProgressiveHedging/structs.jl @@ -0,0 +1,130 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +module ProgressiveHedging +using JuMP, MPI, TimerOutputs +import ..SolutionMethod + +mutable struct TerminationCriteria + max_iterations::Int + max_time::Float64 + min_feasibility::Float64 + min_improvement::Float64 + min_iterations::Int + + function TerminationCriteria(; + max_iterations::Int = 1000, + max_time::Float64 = 14400.0, + min_feasibility::Float64 = 1e-3, + min_improvement::Float64 = 1e-3, + min_iterations::Int = 2, + ) + return new( + max_iterations, + max_time, + min_feasibility, + min_improvement, + min_iterations, + ) + end +end + +Base.@kwdef mutable struct IterationInfo + it_num::Int + sp_consensus_vals::Array{Float64,1} + global_consensus_vals::Array{Float64,1} + sp_obj::Float64 + global_obj::Float64 + it_time::Float64 + total_elapsed_time::Float64 + global_residual::Float64 + global_infeas::Float64 +end + +mutable struct Method <: SolutionMethod + consensus_vars::Union{Array{VariableRef,1},Nothing} + weights::Union{Array{Float64,1},Nothing} + initial_global_consensus_vals::Union{Array{Float64,1},Nothing} + num_of_threads::Int + ρ::Float64 + λ_default::Float64 + print_interval::Int + termination_criteria::TerminationCriteria + + function Method(; + consensus_vars::Union{Array{VariableRef,1},Nothing} = nothing, + weights::Union{Array{Float64,1},Nothing} = nothing, + initial_global_consensus_vals::Union{Array{Float64,1},Nothing} = nothing, + num_of_threads::Int = 1, + ρ::Float64 = 1.0, + λ_default::Float64 = 0.0, + print_interval::Int = 1, + termination_criteria::TerminationCriteria = TerminationCriteria(), + ) + return new( + consensus_vars, + weights, + initial_global_consensus_vals, + num_of_threads, + ρ, + λ_default, + print_interval, + termination_criteria, + ) + end +end + +struct FinalResult + obj::Float64 + vals::Any + infeasibility::Float64 + total_iteration_num::Int + wallclock_time::Float64 +end + +struct SpResult + obj::Float64 + vals::Array{Float64,1} +end + +Base.@kwdef mutable struct SubProblem + mip::JuMP.Model + obj::AffExpr + consensus_vars::Array{VariableRef,1} + weights::Array{Float64,1} +end + +Base.@kwdef struct SpSolution + obj::Float64 + consensus_vals::Array{Float64,1} + residuals::Array{Float64,1} +end + +Base.@kwdef mutable struct SpParams + ρ::Float64 + λ::Array{Float64,1} + global_consensus_vals::Array{Float64,1} +end + +struct MpiInfo + comm::Any + rank::Int + root::Bool + nprocs::Int + + function MpiInfo(comm) + rank = MPI.Comm_rank(comm) + 1 + is_root = (rank == 1) + nprocs = MPI.Comm_size(comm) + return new(comm, rank, is_root, nprocs) + end +end + +Base.@kwdef struct Callbacks + before_solve_subproblem::Any + after_solve_subproblem::Any + after_iteration::Any +end + +end From 3961aedaf551ac2f8de621bb5c6c39048579ccd7 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Fri, 26 May 2023 10:52:23 -0500 Subject: [PATCH 3/5] Revise docs and struct name; add basic MPI test --- Project.toml | 4 +- docs/src/format.md | 122 +++++++++--------- docs/src/index.md | 8 +- docs/src/model.md | 61 +++++---- docs/src/usage.md | 109 ++++++---------- .../methods/ProgressiveHedging/optimize.jl | 67 ++++------ .../methods/ProgressiveHedging/read.jl | 4 +- .../methods/ProgressiveHedging/solution.jl | 7 +- .../methods/ProgressiveHedging/structs.jl | 8 +- test/Project.toml | 1 + test/src/UnitCommitmentT.jl | 2 + .../solution/methods/ProgressiveHedging/ph.jl | 37 ++++++ .../methods/ProgressiveHedging/usage_test.jl | 16 +++ 13 files changed, 225 insertions(+), 221 deletions(-) create mode 100644 test/src/solution/methods/ProgressiveHedging/ph.jl create mode 100644 test/src/solution/methods/ProgressiveHedging/usage_test.jl diff --git a/Project.toml b/Project.toml index 6aa6aeb..2a45202 100644 --- a/Project.toml +++ b/Project.toml @@ -28,7 +28,7 @@ GZip = "0.5" JSON = "0.21" JuMP = "1" MathOptInterface = "1" -MPI = "0.20.8" +MPI = "0.20" PackageCompiler = "1" julia = "1" -TimerOutputs = "0.5.23" \ No newline at end of file +TimerOutputs = "0.5" \ No newline at end of file diff --git a/docs/src/format.md b/docs/src/format.md index 24b8de3..cf146ea 100644 --- a/docs/src/format.md +++ b/docs/src/format.md @@ -4,7 +4,7 @@ Data Format Input Data Format ----------------- -To create the JuMP model for a two-stage security constrained unit commitment (SUC) problem, UC.jl creates an instance that is composed of one or multiple scenarios. For each scenario, UC.jl expects a JSON file that describes the parameters of the problem, as well as the weight and the name of each respective scenario. Note that the input data format required for modeling the deterministic security-constrained unit commitment (SCUC) problem also follows the format mapped out below, as SCUC is treated by the package as the special case of the SUC problem with one scenario. The requisite input data format contains the following fields: +An instance of the stochastic security-constrained unit commitment (SCUC) problem is composed multiple scenarios. Each scenario should be described in an individual JSON file containing the main section belows. For deterministic instances, a single scenario file, following the same format below, may also be provided. Fields that are allowed to differ among scenarios are marked as "uncertain". Fields that are allowed to be time-dependent are marked as "time series". * [Parameters](#Parameters) * [Buses](#Buses) @@ -20,14 +20,14 @@ Each section is described in detail below. See [case118/2017-01-01.json.gz](http This section describes system-wide parameters, such as power balance penalty, and optimization parameters, such as the length of the planning horizon and the time. -| Key | Description | Default | Time series? -| :----------------------------- | :------------------------------------------------ | :------: | :------------: -| `Version` | Version of UnitCommitment.jl this file was written for. Required to ensure that the file remains readable in future versions of the package. If you are following this page to construct the file, this field should equal `0.3`. | Required | N -| `Time horizon (h)` | Length of the planning horizon (in hours). | Required | N -| `Time step (min)` | Length of each time step (in minutes). Must be a divisor of 60 (e.g. 60, 30, 20, 15, etc). | `60` | N -| `Power balance penalty ($/MW)` | Penalty for system-wide shortage or surplus in production (in $/MW). This is charged per time step. For example, if there is a shortage of 1 MW for three time steps, three times this amount will be charged. | `1000.0` | Y -| `Scenario name` | Name of the scenario. | `s1` | N -| `Scenario weight` | Weight of the scenario. The scenario weight can be any positive real number, that is, it does not have to be between zero and one. The package normalizes the weights to ensure that the probability of all scenarios sum up to one. | one divided by the total number of scenarios | N +| Key | Description | Default | Time series? | Uncertain? +| :----------------------------- | :------------------------------------------------ | :------: | :------------:| :----------: +| `Version` | Version of UnitCommitment.jl this file was written for. Required to ensure that the file remains readable in future versions of the package. If you are following this page to construct the file, this field should equal `0.3`. | Required | No | No +| `Time horizon (h)` | Length of the planning horizon (in hours). | Required | No | No +| `Time step (min)` | Length of each time step (in minutes). Must be a divisor of 60 (e.g. 60, 30, 20, 15, etc). | `60` | No | No +| `Power balance penalty ($/MW)` | Penalty for system-wide shortage or surplus in production (in $/MW). This is charged per time step. For example, if there is a shortage of 1 MW for three time steps, three times this amount will be charged. | `1000.0` | No | Yes +| `Scenario name` | Name of the scenario. | `"s1"` | No | --- +| `Scenario weight` | Weight of the scenario. The scenario weight can be any positive real number, that is, it does not have to be between zero and one. The package normalizes the weights to ensure that the probability of all scenarios sum up to one. | 1.0 | No | --- #### Example @@ -47,9 +47,9 @@ This section describes system-wide parameters, such as power balance penalty, an This section describes the characteristics of each bus in the system. -| Key | Description | Default | Time series? -| :----------------- | :------------------------------------------------------------ | ------- | :-------------: -| `Load (MW)` | Fixed load connected to the bus (in MW). | Required | Y +| Key | Description | Default | Time series? | Uncertain? +| :----------------- | :------------------------------------------------------------ | ------- | :-----------: | :---: +| `Load (MW)` | Fixed load connected to the bus (in MW). | Required | Yes | Yes #### Example @@ -81,33 +81,33 @@ This section describes all generators in the system. Two types of units can be s #### Thermal Units -| Key | Description | Default | Time series? -| :------------------------ | :------------------------------------------------| ------- | :-----------: -| `Bus` | Identifier of the bus where this generator is located (string). | Required | N -| `Type` | Type of the generator (string). For thermal generators, this must be `Thermal`. | Required | N -| `Production cost curve (MW)` and `Production cost curve ($)` | Parameters describing the piecewise-linear production costs. See below for more details. | Required | Y -| `Startup costs ($)` and `Startup delays (h)` | Parameters describing how much it costs to start the generator after it has been shut down for a certain amount of time. If `Startup costs ($)` and `Startup delays (h)` are set to `[300.0, 400.0]` and `[1, 4]`, for example, and the generator is shut down at time `00:00` (h:min), then it costs \$300 to start up the generator at any time between `01:00` and `03:59`, and \$400 to start the generator at time `04:00` or any time after that. The number of startup cost points is unlimited, and may be different for each generator. Startup delays must be strictly increasing and the first entry must equal `Minimum downtime (h)`. | `[0.0]` and `[1]` | N -| `Minimum uptime (h)` | Minimum amount of time the generator must stay operational after starting up (in hours). For example, if the generator starts up at time `00:00` (h:min) and `Minimum uptime (h)` is set to 4, then the generator can only shut down at time `04:00`. | `1` | N -| `Minimum downtime (h)` | Minimum amount of time the generator must stay offline after shutting down (in hours). For example, if the generator shuts down at time `00:00` (h:min) and `Minimum downtime (h)` is set to 4, then the generator can only start producing power again at time `04:00`. | `1` | N -| `Ramp up limit (MW)` | Maximum increase in production from one time step to the next (in MW). For example, if the generator is producing 100 MW at time step 1 and if this parameter is set to 40 MW, then the generator will produce at most 140 MW at time step 2. | `+inf` | N -| `Ramp down limit (MW)` | Maximum decrease in production from one time step to the next (in MW). For example, if the generator is producing 100 MW at time step 1 and this parameter is set to 40 MW, then the generator will produce at least 60 MW at time step 2. | `+inf` | N -| `Startup limit (MW)` | Maximum amount of power a generator can produce immediately after starting up (in MW). For example, if `Startup limit (MW)` is set to 100 MW and the unit is off at time step 1, then it may produce at most 100 MW at time step 2.| `+inf` | N -| `Shutdown limit (MW)` | Maximum amount of power a generator can produce immediately before shutting down (in MW). Specifically, the generator can only shut down at time step `t+1` if its production at time step `t` is below this limit. | `+inf` | N -| `Initial status (h)` | If set to a positive number, indicates the amount of time (in hours) the generator has been on at the beginning of the simulation, and if set to a negative number, the amount of time the generator has been off. For example, if `Initial status (h)` is `-2`, this means that the generator was off since `-02:00` (h:min). The simulation starts at time `00:00`. If `Initial status (h)` is `3`, this means that the generator was on since `-03:00`. A value of zero is not acceptable. | Required | N -| `Initial power (MW)` | Amount of power the generator at time step `-1`, immediately before the planning horizon starts. | Required | N -| `Must run?` | If `true`, the generator should be committed, even if that is not economical (Boolean). | `false` | Y -| `Reserve eligibility` | List of reserve products this generator is eligibe to provide. By default, the generator is not eligible to provide any reserves. | `[]` | N -| `Commitment status` | List of commitment status over the time horizon. At time `t`, if `true`, the generator must be commited at that time period; if `false`, the generator must not be commited at that time period. If `null` at time `t`, the generator's commitment status is then decided by the model. By default, the status is a list of `null` values. | `null` | Y +| Key | Description | Default | Time series? | Uncertain? +| :------------------------ | :------------------------------------------------| ------- | :-----------: | :---: +| `Bus` | Identifier of the bus where this generator is located (string). | Required | No | Yes +| `Type` | Type of the generator (string). For thermal generators, this must be `Thermal`. | Required | No | No +| `Production cost curve (MW)` and `Production cost curve ($)` | Parameters describing the piecewise-linear production costs. See below for more details. | Required | Yes | Yes +| `Startup costs ($)` and `Startup delays (h)` | Parameters describing how much it costs to start the generator after it has been shut down for a certain amount of time. If `Startup costs ($)` and `Startup delays (h)` are set to `[300.0, 400.0]` and `[1, 4]`, for example, and the generator is shut down at time `00:00` (h:min), then it costs \$300 to start up the generator at any time between `01:00` and `03:59`, and \$400 to start the generator at time `04:00` or any time after that. The number of startup cost points is unlimited, and may be different for each generator. Startup delays must be strictly increasing and the first entry must equal `Minimum downtime (h)`. | `[0.0]` and `[1]` | No | Yes +| `Minimum uptime (h)` | Minimum amount of time the generator must stay operational after starting up (in hours). For example, if the generator starts up at time `00:00` (h:min) and `Minimum uptime (h)` is set to 4, then the generator can only shut down at time `04:00`. | `1` | No | Yes +| `Minimum downtime (h)` | Minimum amount of time the generator must stay offline after shutting down (in hours). For example, if the generator shuts down at time `00:00` (h:min) and `Minimum downtime (h)` is set to 4, then the generator can only start producing power again at time `04:00`. | `1` | No | Yes +| `Ramp up limit (MW)` | Maximum increase in production from one time step to the next (in MW). For example, if the generator is producing 100 MW at time step 1 and if this parameter is set to 40 MW, then the generator will produce at most 140 MW at time step 2. | `+inf` | No | Yes +| `Ramp down limit (MW)` | Maximum decrease in production from one time step to the next (in MW). For example, if the generator is producing 100 MW at time step 1 and this parameter is set to 40 MW, then the generator will produce at least 60 MW at time step 2. | `+inf` | No | Yes +| `Startup limit (MW)` | Maximum amount of power a generator can produce immediately after starting up (in MW). For example, if `Startup limit (MW)` is set to 100 MW and the unit is off at time step 1, then it may produce at most 100 MW at time step 2.| `+inf` | No | Yes +| `Shutdown limit (MW)` | Maximum amount of power a generator can produce immediately before shutting down (in MW). Specifically, the generator can only shut down at time step `t+1` if its production at time step `t` is below this limit. | `+inf` | No | Yes +| `Initial status (h)` | If set to a positive number, indicates the amount of time (in hours) the generator has been on at the beginning of the simulation, and if set to a negative number, the amount of time the generator has been off. For example, if `Initial status (h)` is `-2`, this means that the generator was off since `-02:00` (h:min). The simulation starts at time `00:00`. If `Initial status (h)` is `3`, this means that the generator was on since `-03:00`. A value of zero is not acceptable. | Required | No | No +| `Initial power (MW)` | Amount of power the generator at time step `-1`, immediately before the planning horizon starts. | Required | No | No +| `Must run?` | If `true`, the generator should be committed, even if that is not economical (Boolean). | `false` | Yes | Yes +| `Reserve eligibility` | List of reserve products this generator is eligibe to provide. By default, the generator is not eligible to provide any reserves. | `[]` | No | Yes +| `Commitment status` | List of commitment status over the time horizon. At time `t`, if `true`, the generator must be commited at that time period; if `false`, the generator must not be commited at that time period. If `null` at time `t`, the generator's commitment status is then decided by the model. By default, the status is a list of `null` values. | `null` | Yes | Yes #### Profiled Units -| Key | Description | Default | Time series? -| :---------------- | :------------------------------------------------ | :------: | :------------: -| `Bus` | Identifier of the bus where this generator is located (string). | Required | N -| `Type` | Type of the generator (string). For profiled generators, this must be `Profiled`. | Required | N -| `Cost ($/MW)` | Cost incurred for serving each MW of power by this generator. | Required | Y -| `Minimum power (MW)` | Minimum amount of power this generator may supply. | `0.0` | Y -| `Maximum power (MW)` | Maximum amount of power this generator may supply. | Required | Y +| Key | Description | Default | Time series? | Uncertain? +| :---------------- | :------------------------------------------------ | :------: | :------------: | :---: +| `Bus` | Identifier of the bus where this generator is located (string). | Required | No | Yes +| `Type` | Type of the generator (string). For profiled generators, this must be `Profiled`. | Required | No | No +| `Cost ($/MW)` | Cost incurred for serving each MW of power by this generator. | Required | Yes | Yes +| `Minimum power (MW)` | Minimum amount of power this generator may supply. | `0.0` | Yes | Yes +| `Maximum power (MW)` | Maximum amount of power this generator may supply. | Required | Yes | Yes #### Production costs and limits @@ -177,11 +177,11 @@ Note that this curve also specifies the production limits. Specifically, the fir This section describes components in the system which may increase or reduce their energy consumption according to the energy prices. Fixed loads (as described in the `buses` section) are always served, regardless of the price, unless there is significant congestion in the system or insufficient production capacity. Price-sensitive loads, on the other hand, are only served if it is economical to do so. -| Key | Description | Default | Time series? -| :---------------- | :------------------------------------------------ | :------: | :------------: -| `Bus` | Bus where the load is located. Multiple price-sensitive loads may be placed at the same bus. | Required | N -| `Revenue ($/MW)` | Revenue obtained for serving each MW of power to this load. | Required | Y -| `Demand (MW)` | Maximum amount of power required by this load. Any amount lower than this may be served. | Required | Y +| Key | Description | Default | Time series? | Uncertain? +| :---------------- | :------------------------------------------------ | :------: | :------------: | :----: +| `Bus` | Bus where the load is located. Multiple price-sensitive loads may be placed at the same bus. | Required | No | Yes +| `Revenue ($/MW)` | Revenue obtained for serving each MW of power to this load. | Required | Yes | Yes +| `Demand (MW)` | Maximum amount of power required by this load. Any amount lower than this may be served. | Required | Yes | Yes #### Example @@ -201,15 +201,15 @@ This section describes components in the system which may increase or reduce the This section describes the characteristics of transmission system, such as its topology and the susceptance of each transmission line. -| Key | Description | Default | Time series? -| :--------------------- | :----------------------------------------------- | ------- | :------------: -| `Source bus` | Identifier of the bus where the transmission line originates. | Required | N -| `Target bus` | Identifier of the bus where the transmission line reaches. | Required | N -| `Reactance (ohms)` | Reactance of the transmission line (in ohms). | Required | N -| `Susceptance (S)` | Susceptance of the transmission line (in siemens). | Required | N -| `Normal flow limit (MW)` | Maximum amount of power (in MW) allowed to flow through the line when the system is in its regular, fully-operational state. | `+inf` | Y -| `Emergency flow limit (MW)` | Maximum amount of power (in MW) allowed to flow through the line when the system is in degraded state (for example, after the failure of another transmission line). | `+inf` | Y -| `Flow limit penalty ($/MW)` | Penalty for violating the flow limits of the transmission line (in $/MW). This is charged per time step. For example, if there is a thermal violation of 1 MW for three time steps, then three times this amount will be charged. | `5000.0` | Y +| Key | Description | Default | Time series? | Uncertain? +| :--------------------- | :----------------------------------------------- | ------- | :------------: | :---: +| `Source bus` | Identifier of the bus where the transmission line originates. | Required | No | Yes +| `Target bus` | Identifier of the bus where the transmission line reaches. | Required | No | Yes +| `Reactance (ohms)` | Reactance of the transmission line (in ohms). | Required | No | Yes +| `Susceptance (S)` | Susceptance of the transmission line (in siemens). | Required | No | Yes +| `Normal flow limit (MW)` | Maximum amount of power (in MW) allowed to flow through the line when the system is in its regular, fully-operational state. | `+inf` | Yes | Yes +| `Emergency flow limit (MW)` | Maximum amount of power (in MW) allowed to flow through the line when the system is in degraded state (for example, after the failure of another transmission line). | `+inf` | Y | Yes +| `Flow limit penalty ($/MW)` | Penalty for violating the flow limits of the transmission line (in $/MW). This is charged per time step. For example, if there is a thermal violation of 1 MW for three time steps, then three times this amount will be charged. | `5000.0` | Yes | Yes #### Example @@ -235,11 +235,11 @@ This section describes the characteristics of transmission system, such as its t This section describes the hourly amount of reserves required. -| Key | Description | Default | Time series? -| :-------------------- | :------------------------------------------------- | --------- | :----: -| `Type` | Type of reserve product. Must be either "spinning" or "flexiramp". | Required | N -| `Amount (MW)` | Amount of reserves required. | Required | Y -| `Shortfall penalty ($/MW)` | Penalty for shortage in meeting the reserve requirements (in $/MW). This is charged per time step. Negative value implies reserve constraints must always be satisfied. | `-1` | Y +| Key | Description | Default | Time series? | Uncertain? +| :-------------------- | :------------------------------------------------- | --------- | :----: | :---: +| `Type` | Type of reserve product. Must be either "spinning" or "flexiramp". | Required | No | No +| `Amount (MW)` | Amount of reserves required. | Required | Yes | Yes +| `Shortfall penalty ($/MW)` | Penalty for shortage in meeting the reserve requirements (in $/MW). This is charged per time step. Negative value implies reserve constraints must always be satisfied. | `-1` | Yes | Yes #### Example 1 @@ -273,10 +273,10 @@ This section describes the hourly amount of reserves required. This section describes credible contingency scenarios in the optimization, such as the loss of a transmission line or generator. -| Key | Description | Default -| :-------------------- | :----------------------------------------------- | ---------- -| `Affected generators` | List of generators affected by this contingency. May be omitted if no generators are affected. | `[]` -| `Affected lines` | List of transmission lines affected by this contingency. May be omitted if no lines are affected. | `[]` +| Key | Description | Default | Uncertain? +| :-------------------- | :----------------------------------------------- | :--------: | :---: +| `Affected generators` | List of generators affected by this contingency. May be omitted if no generators are affected. | `[]` | Yes +| `Affected lines` | List of transmission lines affected by this contingency. May be omitted if no lines are affected. | `[]` | Yes #### Example @@ -327,8 +327,8 @@ The output data format is also JSON-based, but it is not currently documented si Current limitations ------------------- -* Network topology remains the same for all time periods +* Network topology must remain the same for all time periods. * Only N-1 transmission contingencies are supported. Generator contingencies are not currently supported. * Time-varying minimum production amounts are not currently compatible with ramp/startup/shutdown limits. * Flexible ramping products can only be acquired under the `WanHob2016` formulation, which does not support spinning reserves. - +* The set of generators must be the same in all scenarios. diff --git a/docs/src/index.md b/docs/src/index.md index 752fb64..7c7f5a5 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -1,12 +1,12 @@ # UnitCommitment.jl -**UnitCommitment.jl** (UC.jl) is a Julia/JuMP optimization package for modeling and solving the two-stage Stochastic Unit Commitment (SUC) problem, which permits the representation of uncertainty in a broad range of problem parameters. As a special case of the SUC problem, UC.jl allows for modeling and solving the Security-Constrained Unit Commitment Problem (SCUC), a fundamental optimization problem in power systems used, for example, to clear the day-ahead electricity markets. The package provides benchmark instances for the problem and Julia/JuMP implementations of state-of-the-art mixed-integer programming formulations. +**UnitCommitment.jl** (UC.jl) is an optimization package for the Security-Constrained Unit Commitment Problem (SCUC), a fundamental optimization problem in power systems used, for example, to clear the day-ahead electricity markets. Both deterministic and two-stage stochastic versions of the problem are supported. The package provides benchmark instances for the problem, a flexible and well-documented data format for the problem, as well as Julia/JuMP implementations of state-of-the-art mixed-integer programming formulations and solution methods. ## Package Components * **Data Format:** The package proposes an extensible and fully-documented JSON-based data specification format for SCUC, developed in collaboration with Independent System Operators (ISOs), which describes the most important aspects of the problem. The format supports all the most common generator characteristics (including ramping, piecewise-linear production cost curves and time-dependent startup costs), as well as operating reserves, price-sensitive loads, transmission networks and contingencies. * **Benchmark Instances:** The package provides a diverse collection of large-scale benchmark instances collected from the literature, converted into a common data format, and extended using data-driven methods to make them more challenging and realistic. -* **Model Implementation**: The package provides a Julia/JuMP implementations of state-of-the-art formulations and solution methods for SCUC, including multiple ramping formulations ([ArrCon2000](https://doi.org/10.1109/59.871739), [MorLatRam2013](https://doi.org/10.1109/TPWRS.2013.2251373), [DamKucRajAta2016](https://doi.org/10.1007/s10107-015-0919-9), [PanGua2016](https://doi.org/10.1287/opre.2016.1520)), multiple piecewise-linear costs formulations ([Gar1962](https://doi.org/10.1109/AIEEPAS.1962.4501405), [CarArr2006](https://doi.org/10.1109/TPWRS.2006.876672), [KnuOstWat2018](https://doi.org/10.1109/TPWRS.2017.2783850)) and contingency screening methods ([XavQiuWanThi2019](https://doi.org/10.1109/TPWRS.2019.2892620)). Our goal is to keep these implementations up-to-date as new methods are proposed in the literature. +* **Model Implementation**: The package provides a Julia/JuMP implementations of state-of-the-art formulations and solution methods for the deterministic and stochastic SCUC, including multiple ramping formulations ([ArrCon2000](https://doi.org/10.1109/59.871739), [MorLatRam2013](https://doi.org/10.1109/TPWRS.2013.2251373), [DamKucRajAta2016](https://doi.org/10.1007/s10107-015-0919-9), [PanGua2016](https://doi.org/10.1287/opre.2016.1520)), multiple piecewise-linear costs formulations ([Gar1962](https://doi.org/10.1109/AIEEPAS.1962.4501405), [CarArr2006](https://doi.org/10.1109/TPWRS.2006.876672), [KnuOstWat2018](https://doi.org/10.1109/TPWRS.2017.2783850)) and contingency screening methods ([XavQiuWanThi2019](https://doi.org/10.1109/TPWRS.2019.2892620)). Our goal is to keep these implementations up-to-date as new methods are proposed in the literature. * **Benchmark Tools:** The package provides automated benchmark scripts to accurately evaluate the performance impact of proposed code changes. ## Table of Contents @@ -35,7 +35,7 @@ Depth = 3 If you use UnitCommitment.jl in your research (instances, models or algorithms), we kindly request that you cite the package as follows: -* **Alinson S. Xavier, Aleksandr M. Kazachkov, Ogün Yurdakul, Feng Qiu**, "UnitCommitment.jl: A Julia/JuMP Optimization Package for Security-Constrained Unit Commitment (Version 0.3)". Zenodo (2022). [DOI: 10.5281/zenodo.4269874](https://doi.org/10.5281/zenodo.4269874). +* **Alinson S. Xavier, Aleksandr M. Kazachkov, Ogün Yurdakul, Feng Qiu**, "UnitCommitment.jl: A Julia/JuMP Optimization Package for Security-Constrained Unit Commitment (Version 0.4)". Zenodo (2022). [DOI: 10.5281/zenodo.4269874](https://doi.org/10.5281/zenodo.4269874). If you use the instances, we additionally request that you cite the original sources, as described in the [instances page](instances.md). @@ -43,7 +43,7 @@ If you use the instances, we additionally request that you cite the original sou ```text UnitCommitment.jl: A Julia/JuMP Optimization Package for Security-Constrained Unit Commitment -Copyright © 2020-2022, UChicago Argonne, LLC. All Rights Reserved. +Copyright © 2020-2023, UChicago Argonne, LLC. All Rights Reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/docs/src/model.md b/docs/src/model.md index f57648d..17ea389 100644 --- a/docs/src/model.md +++ b/docs/src/model.md @@ -1,15 +1,16 @@ JuMP Model ========== -In this page, we describe the JuMP optimization model produced by the function `UnitCommitment.build_model`. A detailed understanding of this model is not necessary if you are just interested in using the package to solve some standard unit commitment cases, but it may be useful, for example, if you need to solve a slightly different problem, with additional variables and constraints. The notation in this page generally follows [KnOsWa20]. +In this page, we describe the JuMP optimization model produced by the function `build_model`. A detailed understanding of this model is not necessary if you are just interested in using the package to solve some standard unit commitment cases, but it may be useful, for example, if you need to solve a slightly different problem, with additional variables and constraints. The notation in this page generally follows [KnOsWa20]. Decision variables ------------------ -In this section, we describe the decision variables of the JuMP model produced by the function `UnitCommitment.build_model`. -Recall that the UC.jl package allows for modeling and solving the SUC problem, wherein a broad range problem parameters can vary across user-defined scenarios. To facilitate the modeling of the SUC problem, some of the decision variables of the JuMP model are modeled as first-stage decisions, which are here-and-now decisions taken before the uncertainty is realized and thus are the same across all scenarios. As laid out below, the first-stage decisions relate primarily to the binary decisions of the generators. +UC.jl models the security-constrained unit commitment problem as a two-stage stochastic program. In this approach, some of the decision variables are *first-stage decisions*, which are taken before the uncertainty is realized and must therefore be the same across all scenarios, while the remaining variables are *second-stage decisions*, which can attain a different values in each scenario. In the current version of the package, all binary variables (which model commitment decisions of thermal units) are first-stage decisions and all continuous variables are second-stage decisions. -In contrast, most of the decision variables of the JuMP model are second-stage decisions that can attain a different value in each scenario. Accordingly, the second-stage decision variables described below are indexed by the term `sn`. The term `sn` is preserved even if the package is used to model and solve the deterministic security-constrained unit commitment (SCUC) problem, which is currently treated by the package as the special case of the SUC model with only one scenario, having the default scenario index `"s1"`. +!!! note + + UC.jl treats deterministic SCUC instances as a special case of the stochastic problem in which there is only one scenario, named `"s1"` by default. To access second-stage decisions, therefore, you must provide this scenario name as the value for `sn`. For example, `model[:prod_above]["s1", g, t]`. ### Generators @@ -17,15 +18,15 @@ In this section, we describe the decision variables associated with the generato #### Thermal Units -Name | Symbol | Description | Unit -:-----|:--------:|:-------------|:------: -`is_on[g,t]` | $u_{g}(t)$ | True if generator `g` is on at time `t`. | Binary -`switch_on[g,t]` | $v_{g}(t)$ | True is generator `g` switches on at time `t`. | Binary -`switch_off[g,t]` | $w_{g}(t)$ | True if generator `g` switches off at time `t`. | Binary -`startup[g,t,s]` | $\delta^s_g(t)$ | True if generator `g` switches on at time `t` incurring start-up costs from start-up category `s`. | Binary -`prod_above[sn,g,t]` |$p'_{g}(t)$ | Amount of power produced by generator `g` above its minimum power output at time `t` in scenario `sn`. For example, if the minimum power of generator `g` is 100 MW and `g` is producing 115 MW of power at time `t` in scenario `sn`, then `prod_above[sn,g,t]` equals `15.0`. | MW -`segprod[sn,g,t,k]` | $p^k_g(t)$ | Amount of power from piecewise linear segment `k` produced by generator `g` at time `t` in scenario `sn`. For example, if cost curve for generator `g` is defined by the points `(100, 1400)`, `(110, 1600)`, `(130, 2200)` and `(135, 2400)`, and if the generator is producing 115 MW of power at time `t` in scenario `sn`, then `segprod[sn,g,t,:]` equals `[10.0, 5.0, 0.0]`.| MW -`reserve[sn,r,g,t]` | $r_g(t)$ | Amount of reserve `r` provided by unit `g` at time `t` in scenario `sn`. | MW +Name | Description | Unit | Stage +:-----|:-------------|:------: | :------: +`is_on[g,t]` | True if generator `g` is on at time `t`. | Binary | 1 +`switch_on[g,t]` | True is generator `g` switches on at time `t`. | Binary| 1 +`switch_off[g,t]` | True if generator `g` switches off at time `t`. | Binary| 1 +`startup[g,t,s]` | True if generator `g` switches on at time `t` incurring start-up costs from start-up category `s`. | Binary| 1 +`prod_above[sn,g,t]` | Amount of power produced by generator `g` above its minimum power output at time `t` in scenario `sn`. For example, if the minimum power of generator `g` is 100 MW and `g` is producing 115 MW of power at time `t` in scenario `sn`, then `prod_above[sn,g,t]` equals `15.0`. | MW | 2 +`segprod[sn,g,t,k]` | Amount of power from piecewise linear segment `k` produced by generator `g` at time `t` in scenario `sn`. For example, if cost curve for generator `g` is defined by the points `(100, 1400)`, `(110, 1600)`, `(130, 2200)` and `(135, 2400)`, and if the generator is producing 115 MW of power at time `t` in scenario `sn`, then `segprod[sn,g,t,:]` equals `[10.0, 5.0, 0.0]`.| MW | 2 +`reserve[sn,r,g,t]` | Amount of reserve `r` provided by unit `g` at time `t` in scenario `sn`. | MW | 2 !!! warning @@ -34,31 +35,31 @@ Name | Symbol | Description | Unit #### Profiled Units -Name | Symbol | Description | Unit -:-----|:------:|:-------------|:------: -`prod_profiled[s,t]` | $p^{\dagger}_{g}(t)$ | Amount of power produced by profiled unit `g` at time `t`. | MW +Name | Description | Unit | Stage +:-----|:-------------|:------: | :------: +`prod_profiled[s,t]` | Amount of power produced by profiled unit `g` at time `t`. | MW | 2 ### Buses -Name | Symbol | Description | Unit -:-----|:------:|:-------------|:------: -`net_injection[sn,b,t]` | $n_b(t)$ | Net injection at bus `b` at time `t` in scenario `sn`. | MW -`curtail[sn,b,t]` | $s^+_b(t)$ | Amount of load curtailed at bus `b` at time `t` in scenario `sn`. | MW +Name | Description | Unit | Stage +:-----|:-------------|:------:| :------: +`net_injection[sn,b,t]` | Net injection at bus `b` at time `t` in scenario `sn`. | MW | 2 +`curtail[sn,b,t]` | Amount of load curtailed at bus `b` at time `t` in scenario `sn`. | MW | 2 ### Price-sensitive loads -Name | Symbol | Description | Unit -:-----|:------:|:-------------|:------: -`loads[sn,s,t]` | $d_{s}(t)$ | Amount of power served to price-sensitive load `s` at time `t` in scenario `sn`. | MW +Name | Description | Unit | Stage +:-----|:-------------|:------:| :------: +`loads[sn,s,t]` | Amount of power served to price-sensitive load `s` at time `t` in scenario `sn`. | MW | 2 ### Transmission lines -Name | Symbol | Description | Unit -:-----|:------:|:-------------|:------: -`flow[sn,l,t]` | $f_l(t)$ | Power flow on line `l` at time `t` in scenario `sn`. | MW -`overflow[sn,l,t]` | $f^+_l(t)$ | Amount of flow above the limit for line `l` at time `t` in scenario `sn`. | MW +Name | Description | Unit | Stage +:-----|:-------------|:------:| :------: +`flow[sn,l,t]` | Power flow on line `l` at time `t` in scenario `sn`. | MW | 2 +`overflow[sn,l,t]` | Amount of flow above the limit for line `l` at time `t` in scenario `sn`. | MW | 2 !!! warning @@ -148,21 +149,19 @@ JuMP.fix( # Fix the production level of the generator "g1" above its minimum level in time period 1 and # in scenario "s1" to 20.0 MW. Observe that the three-tuple dictionary key involves the scenario # index "s1", as production above minimum is a second-stage decision variable. - JuMP.fix( model[:prod_above]["s1", "g1", 1], 20.0, force=true, ) -# Enforce the curtailment of 20.0 MW of load at bus "b2" in time period 4 in scenario "s10". +# Enforce the curtailment of 20.0 MW of load at bus "b2" in time period 4 in scenario "s1". JuMP.fix( - curtail["s10", "b2", 4] = + curtail["s1", "b2", 4] = 20.0, force=true, ) - # Change the objective function JuMP.set_objective_coefficient( model, diff --git a/docs/src/usage.md b/docs/src/usage.md index cc0281a..d141edc 100644 --- a/docs/src/usage.md +++ b/docs/src/usage.md @@ -4,28 +4,24 @@ Usage Installation ------------ -UnitCommitment.jl was tested and developed with [Julia 1.7](https://julialang.org/). To install Julia, please follow the [installation guide on the official Julia website](https://julialang.org/downloads/). To install UnitCommitment.jl, run the Julia interpreter, type `]` to open the package manager, then type: +UnitCommitment.jl was tested and developed with [Julia 1.9](https://julialang.org/). To install Julia, please follow the [installation guide on the official Julia website](https://julialang.org/downloads/). To install UnitCommitment.jl, run the Julia interpreter, type `]` to open the package manager, then type: ```text -pkg> add UnitCommitment@0.3 +pkg> add UnitCommitment@0.4 ``` -To test that the package has been correctly installed, run: - -```text -pkg> test UnitCommitment -``` - -If all tests pass, the package should now be ready to be used by any Julia script on the machine. - -To solve the optimization models, a mixed-integer linear programming (MILP) solver is also required. Please see the [JuMP installation guide](https://jump.dev/JuMP.jl/stable/installation/) for more instructions on installing a solver. Typical open-source choices are [Cbc](https://github.com/JuliaOpt/Cbc.jl) and [GLPK](https://github.com/JuliaOpt/GLPK.jl). In the instructions below, Cbc will be used, but any other MILP solver listed in JuMP installation guide should also be compatible. +To solve the optimization models, a mixed-integer linear programming (MILP) solver is also required. Please see the [JuMP installation guide](https://jump.dev/JuMP.jl/stable/installation/) for more instructions on installing a solver. Typical open-source choices are [HiGHS](https://github.com/jump-dev/HiGHS.jl), [Cbc](https://github.com/JuliaOpt/Cbc.jl) and [GLPK](https://github.com/JuliaOpt/GLPK.jl). In the instructions below, Cbc will be used, but any other MILP solver listed in JuMP installation guide should also be compatible. Typical Usage ------------- ### Solving user-provided instances -The first step to use UC.jl is to construct a JSON file that describes each scenario of your unit commitment instance. See [Data Format](format.md) for a complete description of the data format UC.jl expects. The next steps, as shown below, are to: (1) construct the instance using scenario files; (2) build the optimization model; (3) run the optimization; and (4) extract the optimal solution. +The first step to use UC.jl is to construct JSON files that describe each scenario of your stochastic unit commitment instance. See [Data Format](format.md) for a complete description of the data format UC.jl expects. The next steps, as shown below, are to: (1) read the scenario files; (2) build the optimization model; (3) run the optimization; and (4) extract the optimal solution. + +!!! note + +> By default, UC.jl uses the extensive form to solve the problem. For a more advanced solution method, see below. ```julia using Cbc @@ -49,21 +45,24 @@ solution = UnitCommitment.solution(model) UnitCommitment.write("/path/to/output.json", solution) ``` -The above lines of code can also be used for solving the deterministic Security-Constrained Unit Commitment (SCUC) problem, which will create an instance based on a single scenario. The unit commitment instance for SCUC can alternatively be constructed as +To read all files in a given folder, the [Glob](https://github.com/vtjnash/Glob.jl) package can be used: ```julia -# 1. Read instance -instance = UnitCommitment.read("/path/to/input.json") +using Glob +instance = UnitCommitment.read(glob("*.json", "/path/to/scenarios/")) ``` +To solve deterministic instances, a single scenario file may be provided. +```julia +instance = UnitCommitment.read("/path/to/s1.json") +``` ### Solving benchmark instances -UnitCommitment.jl contains a large number of benchmark instances collected from the literature and converted into a common data format. To solve one of these instances individually, instead of constructing your own, the function `read_benchmark` can be used, as shown below. See [Instances](instances.md) for the complete list of available instances. +UnitCommitment.jl contains a large number of deterministic benchmark instances collected from the literature and converted into a common data format. To solve one of these instances individually, instead of constructing your own, the function `read_benchmark` can be used, as shown below. See [Instances](instances.md) for the complete list of available instances. ```julia -using UnitCommitment instance = UnitCommitment.read_benchmark("matpower/case3375wp/2017-02-01") ``` @@ -145,75 +144,51 @@ solution = JSON.parsefile("solution.json") # Validate solution and print validation errors UnitCommitment.validate(instance, solution) ``` -## Modeling and Solving the SUC Problem -To model the SUC problem, UC.jl supports reading scenario files at a specified directory using the `Glob` package. For instance, in order to construct a SUC instance using all JSON files at a given directory, where each JSON file describes one scenario, the following line of code can be used: +## Progressive Hedging -```julia -instance = UnitCommitment.read(glob("*.json", "/path/to/scenarios/")) -``` +By default, UC.jl uses the Extensive Form (EF) when solving stochastic instances. This approach involves constructing a single JuMP model that contains data and decision variables for all scenarios. Although EF has optimality guarantees and performs well with small test cases, it can become computationally intractable for large instances or substantial number of scenarios. -Alternatively, the specific vector of scenario files can also be passed as follows: -```julia -instance = UnitCommitment.read(["/path/to/s1.json", "/path/to/s2.json"])) -``` +Progressive Hedging (PH) is an alternative (heuristic) solution method provided by UC.jl in which the problem is decomposed into smaller scenario-based subproblems, which are then solved in parallel in separate Julia processes, potentially across multiple machines. Quadratic penalty terms are used to enforce convergence of first-stage decision variables. The method is closely related to the Alternative Direction Method of Multipliers (ADMM) and can handle larger instances, although it is not guaranteed to converge to the optimal solution. Our implementation of PH relies on Message Passing Interface (MPI) for communication. We refer to [MPI.jl Documentation](https://github.com/JuliaParallel/MPI.jl) for more details on installing MPI. -## Solving the SUC Problem +The following example shows how to solve SCUC instances using progressive hedging. The script should be saved in a file, say `ph.jl`, and executed using `mpiexec -n julia ph.jl`. -We next lay out the alternative methods supported by UC.jl for solving the SUC problem. - -## Solving the Extensive Form of the SUC Problem - -By default, UC.jl solves the extensive form of the SUC problem. - -```julia -UnitCommitment.optimize!(model) -solution = UnitCommitment.solution(model) -UnitCommitment.write("/path/to/output.json", solution) -``` - -Note that the created `solution` dictionary will include both the optimal first-stage decisions, as well as the optimal second-stage decisions under all scenarios. - -## Solving the SUC Problem Using Progressive Hedging - -Importantly, UC.jl further provides the option of solving the SUC problem using the progressive hedging (PH) algorithm, which is an algorithm closely related to the alternating direction method of multipliers (ADMM). To that end, the package supports solving the PH subproblem associated with each scenario in parallel in a separate Julia process, where the communication among the Julia processes is provided using the Message Passing Interface or MPI. - -The solve the SUC problem using Progressive Hedging, you may run the following line of code, which will create `NUM_OF_PROCS` processes where each process executes the `ph.jl` file. ```julia +using Cbc using MPI -const FILENAME = "ph.jl" -const NUM_OF_PROCS = 5 +using UnitCommitment +using Glob -mpiexec(exe -> run(`$exe -n $NUM_OF_PROCS $(Base.julia_cmd()) $FILENAME`)) -``` +# 1. Initialize MPI +MPI.Init() -#### **`ph.jl`** -```julia -using MPI: MPI_Info -using Gurobi, MPI, UnitCommitment, Glob -import UnitCommitment: ProgressiveHedging +# 2. Configure progressive hedging method +ph = UnitCommitment.ProgressiveHedging() -MPI.Init() -ph = ProgressiveHedging.Method() -instance = UnitCommitment.read( - glob("*.json", "/path/to/scenarios/"), - ph -) +# 3. Read problem instance +instance = UnitCommitment.read(["s1.json", "s2.json"], ph) + +# 4. Build JuMP model model = UnitCommitment.build_model( - instance = instance, - optimizer = Gurobi.Optimizer, - ) + instance = instance, + optimizer = Cbc.Optimizer, +) + +# 5. Run the decentralized optimization algorithm UnitCommitment.optimize!(model, ph) + +# 6. Fetch the solution solution = UnitCommitment.solution(model, ph) + +# 7. Close MPI MPI.Finalize() ``` -Observe that the `read`, `build_model`, and `solution` methods take the `ph` object as an argument, which is of type `Progressive Hedging`. +When using PH, the model can be customized as usual, with a different formulations or additional user-provided constraints. Note that `read`, in this case, takes `ph` as an argument. This allows each Julia process to read only the instance files that are relevant to it. Similarly, the `solution` function gathers the optimal solution of each processes and returns a combined dictionary. -The subproblem solved within each Julia process deduces the number of scenarios it needs to model using the total number of scenarios and the total number of processes. For instance, if `glob("*.json", "/path/to/scenarios/")` returns a vector of 15 scenario file paths and `NUM_OF_PROCS = 5`, then each subproblem will model and solve 3 scenarios. If the total number of scenarios is not divisible by `NUM_OF_PROCS`, then the read method will throw an error. +Each process solves a sub-problem with $\frac{s}{p}$ scenarios, where $s$ is the total number of scenarios and $p$ is the number of MPI processes. For instance, if we have 15 scenario files and 5 processes, then each process will solve a JuMP model that contains data for 3 scenarios. If the total number of scenarios is not divisible by the number of processes, then an error will be thrown. -The `solution(model, ph)` method gathers the optimal solution of all processes and returns a dictionary that contains all optimal first-stage decisions as well as all optimal second-stage decisions evaluated for each scenario. !!! warning diff --git a/src/solution/methods/ProgressiveHedging/optimize.jl b/src/solution/methods/ProgressiveHedging/optimize.jl index c6831e5..c08e391 100644 --- a/src/solution/methods/ProgressiveHedging/optimize.jl +++ b/src/solution/methods/ProgressiveHedging/optimize.jl @@ -6,12 +6,9 @@ using TimerOutputs import JuMP const to = TimerOutput() -function optimize!( - model::JuMP.Model, - method::ProgressiveHedging.Method, -)::ProgressiveHedging.FinalResult - mpi = ProgressiveHedging.MpiInfo(MPI.COMM_WORLD) - iterations = Array{ProgressiveHedging.IterationInfo,1}(undef, 0) +function optimize!(model::JuMP.Model, method::ProgressiveHedging)::FinalResult + mpi = MpiInfo(MPI.COMM_WORLD) + iterations = Array{IterationInfo,1}(undef, 0) if method.consensus_vars === nothing method.consensus_vars = [var for var in all_variables(model) if is_binary(var)] @@ -24,17 +21,13 @@ function optimize!( method.initial_global_consensus_vals = [0.0 for _ in 1:nvars] end - ph_sp_params = ProgressiveHedging.SpParams( + ph_sp_params = SpParams( ρ = method.ρ, λ = [method.λ_default for _ in 1:nvars], global_consensus_vals = method.initial_global_consensus_vals, ) - ph_subproblem = ProgressiveHedging.SubProblem( - model, - model[:obj], - method.consensus_vars, - method.weights, - ) + ph_subproblem = + SubProblem(model, model[:obj], method.consensus_vars, method.weights) set_optimizer_attribute(model, "Threads", method.num_of_threads) while true it_time = @elapsed begin @@ -54,7 +47,7 @@ function optimize!( end end total_elapsed_time = compute_total_elapsed_time(it_time, iterations) - it = ProgressiveHedging.IterationInfo( + it = IterationInfo( it_num = length(iterations) + 1, sp_consensus_vals = solution.consensus_vals, global_consensus_vals = global_consensus_vals, @@ -72,7 +65,7 @@ function optimize!( end end - return ProgressiveHedging.FinalResult( + return FinalResult( last(iterations).global_obj, last(iterations).sp_consensus_vals, last(iterations).global_infeas, @@ -83,7 +76,7 @@ end function compute_total_elapsed_time( it_time::Float64, - iterations::Array{ProgressiveHedging.IterationInfo,1}, + iterations::Array{IterationInfo,1}, )::Float64 length(iterations) > 0 ? current_total_time = last(iterations).total_elapsed_time : @@ -91,29 +84,20 @@ function compute_total_elapsed_time( return current_total_time + it_time end -function compute_global_objective( - mpi::ProgressiveHedging.MpiInfo, - s::ProgressiveHedging.SpSolution, -)::Float64 +function compute_global_objective(mpi::MpiInfo, s::SpSolution)::Float64 global_obj = MPI.Allreduce(s.obj, MPI.SUM, mpi.comm) global_obj /= mpi.nprocs return global_obj end -function compute_global_consensus( - mpi::ProgressiveHedging.MpiInfo, - s::ProgressiveHedging.SpSolution, -)::Array{Float64,1} +function compute_global_consensus(mpi::MpiInfo, s::SpSolution)::Array{Float64,1} sp_consensus_vals = s.consensus_vals global_consensus_vals = MPI.Allreduce(sp_consensus_vals, MPI.SUM, mpi.comm) global_consensus_vals = global_consensus_vals / mpi.nprocs return global_consensus_vals end -function compute_global_residual( - mpi::ProgressiveHedging.MpiInfo, - s::ProgressiveHedging.SpSolution, -)::Float64 +function compute_global_residual(mpi::MpiInfo, s::SpSolution)::Float64 n_vars = length(s.consensus_vals) local_residual_sum = abs.(s.residuals) global_residual_sum = MPI.Allreduce(local_residual_sum, MPI.SUM, mpi.comm) @@ -121,18 +105,15 @@ function compute_global_residual( end function compute_global_infeasibility( - solution::ProgressiveHedging.SpSolution, - mpi::ProgressiveHedging.MpiInfo, + solution::SpSolution, + mpi::MpiInfo, )::Float64 local_infeasibility = norm(solution.residuals) global_infeas = MPI.Allreduce(local_infeasibility, MPI.SUM, mpi.comm) return global_infeas end -function solve_subproblem( - sp::ProgressiveHedging.SubProblem, - ph_sp_params::ProgressiveHedging.SpParams, -)::ProgressiveHedging.SpSolution +function solve_subproblem(sp::SubProblem, ph_sp_params::SpParams)::SpSolution G = length(sp.consensus_vars) if norm(ph_sp_params.λ) < 1e-3 @objective(sp.mip, Min, sp.obj) @@ -159,7 +140,7 @@ function solve_subproblem( optimize!(sp.mip, XavQiuWanThi2019.Method()) obj = objective_value(sp.mip) sp_consensus_vals = value.(sp.consensus_vars) - return ProgressiveHedging.SpSolution( + return SpSolution( obj = obj, consensus_vals = sp_consensus_vals, residuals = zeros(G), @@ -167,8 +148,8 @@ function solve_subproblem( end function update_λ_and_residuals!( - solution::ProgressiveHedging.SpSolution, - ph_sp_params::ProgressiveHedging.SpParams, + solution::SpSolution, + ph_sp_params::SpParams, global_consensus_vals::Array{Float64,1}, )::Nothing n_vars = length(solution.consensus_vals) @@ -180,7 +161,7 @@ function update_λ_and_residuals!( end end -function print_header(mpi::ProgressiveHedging.MpiInfo)::Nothing +function print_header(mpi::MpiInfo)::Nothing if !mpi.root return end @@ -197,8 +178,8 @@ function print_header(mpi::ProgressiveHedging.MpiInfo)::Nothing end function print_progress( - mpi::ProgressiveHedging.MpiInfo, - iteration::ProgressiveHedging.IterationInfo, + mpi::MpiInfo, + iteration::IterationInfo, print_interval, )::Nothing if !mpi.root @@ -227,9 +208,9 @@ function has_numerical_issues(target::Array{Float64,1})::Bool end function should_stop( - mpi::ProgressiveHedging.MpiInfo, - iterations::Array{ProgressiveHedging.IterationInfo,1}, - criteria::ProgressiveHedging.TerminationCriteria, + mpi::MpiInfo, + iterations::Array{IterationInfo,1}, + criteria::TerminationCriteria, )::Bool if length(iterations) >= criteria.max_iterations if mpi.root diff --git a/src/solution/methods/ProgressiveHedging/read.jl b/src/solution/methods/ProgressiveHedging/read.jl index 476b8b7..6247cd7 100644 --- a/src/solution/methods/ProgressiveHedging/read.jl +++ b/src/solution/methods/ProgressiveHedging/read.jl @@ -4,10 +4,10 @@ function read( paths::Vector{String}, - method::ProgressiveHedging.Method, + ::ProgressiveHedging, )::UnitCommitmentInstance comm = MPI.COMM_WORLD - mpi = ProgressiveHedging.MpiInfo(comm) + mpi = MpiInfo(comm) (length(paths) % mpi.nprocs == 0) || error( "Number of processes $(mpi.nprocs) is not a divisor of $(length(paths))", ) diff --git a/src/solution/methods/ProgressiveHedging/solution.jl b/src/solution/methods/ProgressiveHedging/solution.jl index b06ad89..8d672b6 100644 --- a/src/solution/methods/ProgressiveHedging/solution.jl +++ b/src/solution/methods/ProgressiveHedging/solution.jl @@ -5,12 +5,9 @@ using MPI, DataStructures const FIRST_STAGE_VARS = ["Is on", "Switch on", "Switch off"] -function solution( - model::JuMP.Model, - method::ProgressiveHedging.Method, -)::OrderedDict +function solution(model::JuMP.Model, method::ProgressiveHedging)::OrderedDict comm = MPI.COMM_WORLD - mpi = ProgressiveHedging.MpiInfo(comm) + mpi = MpiInfo(comm) sp_solution = UnitCommitment.solution(model) gather_solution = OrderedDict() for (solution_key, dict) in sp_solution diff --git a/src/solution/methods/ProgressiveHedging/structs.jl b/src/solution/methods/ProgressiveHedging/structs.jl index b339813..39d68a4 100644 --- a/src/solution/methods/ProgressiveHedging/structs.jl +++ b/src/solution/methods/ProgressiveHedging/structs.jl @@ -2,9 +2,7 @@ # Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. # Released under the modified BSD license. See COPYING.md for more details. -module ProgressiveHedging using JuMP, MPI, TimerOutputs -import ..SolutionMethod mutable struct TerminationCriteria max_iterations::Int @@ -42,7 +40,7 @@ Base.@kwdef mutable struct IterationInfo global_infeas::Float64 end -mutable struct Method <: SolutionMethod +mutable struct ProgressiveHedging <: SolutionMethod consensus_vars::Union{Array{VariableRef,1},Nothing} weights::Union{Array{Float64,1},Nothing} initial_global_consensus_vals::Union{Array{Float64,1},Nothing} @@ -52,7 +50,7 @@ mutable struct Method <: SolutionMethod print_interval::Int termination_criteria::TerminationCriteria - function Method(; + function ProgressiveHedging(; consensus_vars::Union{Array{VariableRef,1},Nothing} = nothing, weights::Union{Array{Float64,1},Nothing} = nothing, initial_global_consensus_vals::Union{Array{Float64,1},Nothing} = nothing, @@ -126,5 +124,3 @@ Base.@kwdef struct Callbacks after_solve_subproblem::Any after_iteration::Any end - -end diff --git a/test/Project.toml b/test/Project.toml index 7b68ed9..167e679 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -13,6 +13,7 @@ JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" JuMP = "4076af6c-e467-56ae-b986-b466b2749572" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195" MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" diff --git a/test/src/UnitCommitmentT.jl b/test/src/UnitCommitmentT.jl index c07f547..f529c42 100644 --- a/test/src/UnitCommitmentT.jl +++ b/test/src/UnitCommitmentT.jl @@ -12,6 +12,7 @@ include("model/formulations_test.jl") include("solution/methods/XavQiuWanThi19/filter_test.jl") include("solution/methods/XavQiuWanThi19/find_test.jl") include("solution/methods/XavQiuWanThi19/sensitivity_test.jl") +include("solution/methods/ProgressiveHedging/usage_test.jl") include("transform/initcond_test.jl") include("transform/slice_test.jl") include("transform/randomize/XavQiuAhm2021_test.jl") @@ -37,6 +38,7 @@ function runtests() solution_methods_XavQiuWanThi19_filter_test() solution_methods_XavQiuWanThi19_find_test() solution_methods_XavQiuWanThi19_sensitivity_test() + solution_methods_ProgressiveHedging_usage_test() transform_initcond_test() transform_slice_test() transform_randomize_XavQiuAhm2021_test() diff --git a/test/src/solution/methods/ProgressiveHedging/ph.jl b/test/src/solution/methods/ProgressiveHedging/ph.jl new file mode 100644 index 0000000..1e6acd0 --- /dev/null +++ b/test/src/solution/methods/ProgressiveHedging/ph.jl @@ -0,0 +1,37 @@ +using Cbc +using MPI +using JuMP +using UnitCommitment + +UnitCommitment._setup_logger(level = Base.CoreLogging.Error) +function fixture(path::String)::String + basedir = dirname(@__FILE__) + return "$basedir/../../../../fixtures/$path" +end + +# 1. Initialize MPI +MPI.Init() + +# 2. Configure progressive hedging method +ph = UnitCommitment.ProgressiveHedging() + +# 3. Read problem instance +instance = UnitCommitment.read( + [fixture("case14.json.gz"), fixture("case14.json.gz")], + ph, +) + +# 4. Build JuMP model +model = UnitCommitment.build_model( + instance = instance, + optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), +) + +# 5. Run the decentralized optimization algorithm +UnitCommitment.optimize!(model, ph) + +# 6. Fetch the solution +solution = UnitCommitment.solution(model, ph) + +# 7. Close MPI +MPI.Finalize() diff --git a/test/src/solution/methods/ProgressiveHedging/usage_test.jl b/test/src/solution/methods/ProgressiveHedging/usage_test.jl new file mode 100644 index 0000000..6e4342f --- /dev/null +++ b/test/src/solution/methods/ProgressiveHedging/usage_test.jl @@ -0,0 +1,16 @@ +# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment +# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved. +# Released under the modified BSD license. See COPYING.md for more details. + +using MPI + +function solution_methods_ProgressiveHedging_usage_test() + basedir = dirname(@__FILE__) + @testset "ProgressiveHedging" begin + mpiexec() do exe + return run( + `$exe -n 2 $(Base.julia_cmd()) --project=test $basedir/ph.jl`, + ) + end + end +end \ No newline at end of file From 03bf1c4c04e3c67989f21fe75e83bf559bf7a0a0 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Fri, 26 May 2023 11:47:34 -0500 Subject: [PATCH 4/5] PH: Rename vars, remove return value --- .../methods/ProgressiveHedging/optimize.jl | 178 ++++++++---------- .../methods/ProgressiveHedging/structs.jl | 107 +++-------- .../solution/methods/ProgressiveHedging/ph.jl | 21 ++- 3 files changed, 120 insertions(+), 186 deletions(-) diff --git a/src/solution/methods/ProgressiveHedging/optimize.jl b/src/solution/methods/ProgressiveHedging/optimize.jl index c08e391..03831d8 100644 --- a/src/solution/methods/ProgressiveHedging/optimize.jl +++ b/src/solution/methods/ProgressiveHedging/optimize.jl @@ -6,106 +6,95 @@ using TimerOutputs import JuMP const to = TimerOutput() -function optimize!(model::JuMP.Model, method::ProgressiveHedging)::FinalResult +function optimize!(model::JuMP.Model, method::ProgressiveHedging)::Nothing mpi = MpiInfo(MPI.COMM_WORLD) - iterations = Array{IterationInfo,1}(undef, 0) - if method.consensus_vars === nothing - method.consensus_vars = - [var for var in all_variables(model) if is_binary(var)] + iterations = PHIterationInfo[] + consensus_vars = [var for var in all_variables(model) if is_binary(var)] + nvars = length(consensus_vars) + weights = ones(nvars) + if method.initial_weights !== nothing + weights = copy(method.initial_weights) end - nvars = length(method.consensus_vars) - if method.weights === nothing - method.weights = [1.0 for _ in 1:nvars] + target = zeros(nvars) + if method.initial_target !== nothing + target = copy(method.initial_target) end - if method.initial_global_consensus_vals === nothing - method.initial_global_consensus_vals = [0.0 for _ in 1:nvars] - end - - ph_sp_params = SpParams( + params = PHSubProblemParams( ρ = method.ρ, - λ = [method.λ_default for _ in 1:nvars], - global_consensus_vals = method.initial_global_consensus_vals, + λ = [method.λ for _ in 1:nvars], + target = target, ) - ph_subproblem = - SubProblem(model, model[:obj], method.consensus_vars, method.weights) - set_optimizer_attribute(model, "Threads", method.num_of_threads) + sp = PHSubProblem(model, model[:obj], consensus_vars, weights) while true - it_time = @elapsed begin - solution = solve_subproblem(ph_subproblem, ph_sp_params) + iteration_time = @elapsed begin + solution = solve_subproblem(sp, params, method.inner_method) MPI.Barrier(mpi.comm) global_obj = compute_global_objective(mpi, solution) - global_consensus_vals = compute_global_consensus(mpi, solution) - update_λ_and_residuals!( - solution, - ph_sp_params, - global_consensus_vals, - ) + target = compute_target(mpi, solution) + update_λ_and_residuals!(solution, params, target) global_infeas = compute_global_infeasibility(solution, mpi) global_residual = compute_global_residual(mpi, solution) - if has_numerical_issues(global_consensus_vals) + if has_numerical_issues(target) break end end - total_elapsed_time = compute_total_elapsed_time(it_time, iterations) - it = IterationInfo( - it_num = length(iterations) + 1, - sp_consensus_vals = solution.consensus_vals, - global_consensus_vals = global_consensus_vals, - sp_obj = solution.obj, + total_elapsed_time = + compute_total_elapsed_time(iteration_time, iterations) + current_iteration = PHIterationInfo( + global_infeas = global_infeas, global_obj = global_obj, - it_time = it_time, - total_elapsed_time = total_elapsed_time, global_residual = global_residual, - global_infeas = global_infeas, + iteration_number = length(iterations) + 1, + iteration_time = iteration_time, + sp_vals = solution.vals, + sp_obj = solution.obj, + target = target, + total_elapsed_time = total_elapsed_time, ) - iterations = [iterations; it] - print_progress(mpi, it, method.print_interval) - if should_stop(mpi, iterations, method.termination_criteria) + push!(iterations, current_iteration) + print_progress(mpi, current_iteration, method.print_interval) + if should_stop(mpi, iterations, method.termination) break end end - - return FinalResult( - last(iterations).global_obj, - last(iterations).sp_consensus_vals, - last(iterations).global_infeas, - last(iterations).it_num, - last(iterations).total_elapsed_time, - ) + return end function compute_total_elapsed_time( - it_time::Float64, - iterations::Array{IterationInfo,1}, + iteration_time::Float64, + iterations::Array{PHIterationInfo,1}, )::Float64 length(iterations) > 0 ? current_total_time = last(iterations).total_elapsed_time : current_total_time = 0 - return current_total_time + it_time + return current_total_time + iteration_time end -function compute_global_objective(mpi::MpiInfo, s::SpSolution)::Float64 +function compute_global_objective( + mpi::MpiInfo, + s::PhSubProblemSolution, +)::Float64 global_obj = MPI.Allreduce(s.obj, MPI.SUM, mpi.comm) global_obj /= mpi.nprocs return global_obj end -function compute_global_consensus(mpi::MpiInfo, s::SpSolution)::Array{Float64,1} - sp_consensus_vals = s.consensus_vals - global_consensus_vals = MPI.Allreduce(sp_consensus_vals, MPI.SUM, mpi.comm) - global_consensus_vals = global_consensus_vals / mpi.nprocs - return global_consensus_vals +function compute_target(mpi::MpiInfo, s::PhSubProblemSolution)::Array{Float64,1} + sp_vals = s.vals + target = MPI.Allreduce(sp_vals, MPI.SUM, mpi.comm) + target = target / mpi.nprocs + return target end -function compute_global_residual(mpi::MpiInfo, s::SpSolution)::Float64 - n_vars = length(s.consensus_vals) +function compute_global_residual(mpi::MpiInfo, s::PhSubProblemSolution)::Float64 + n_vars = length(s.vals) local_residual_sum = abs.(s.residuals) global_residual_sum = MPI.Allreduce(local_residual_sum, MPI.SUM, mpi.comm) return sum(global_residual_sum) / n_vars end function compute_global_infeasibility( - solution::SpSolution, + solution::PhSubProblemSolution, mpi::MpiInfo, )::Float64 local_infeasibility = norm(solution.residuals) @@ -113,9 +102,13 @@ function compute_global_infeasibility( return global_infeas end -function solve_subproblem(sp::SubProblem, ph_sp_params::SpParams)::SpSolution +function solve_subproblem( + sp::PHSubProblem, + params::PHSubProblemParams, + method::SolutionMethod, +)::PhSubProblemSolution G = length(sp.consensus_vars) - if norm(ph_sp_params.λ) < 1e-3 + if norm(params.λ) < 1e-3 @objective(sp.mip, Min, sp.obj) else @objective( @@ -124,40 +117,31 @@ function solve_subproblem(sp::SubProblem, ph_sp_params::SpParams)::SpSolution sp.obj + sum( sp.weights[g] * - ph_sp_params.λ[g] * - (sp.consensus_vars[g] - ph_sp_params.global_consensus_vals[g]) - for g in 1:G + params.λ[g] * + (sp.consensus_vars[g] - params.target[g]) for g in 1:G ) + - (ph_sp_params.ρ / 2) * sum( - sp.weights[g] * - ( - sp.consensus_vars[g] - - ph_sp_params.global_consensus_vals[g] - )^2 for g in 1:G + (params.ρ / 2) * sum( + sp.weights[g] * (sp.consensus_vars[g] - params.target[g])^2 for + g in 1:G ) ) end - optimize!(sp.mip, XavQiuWanThi2019.Method()) + optimize!(sp.mip, method) obj = objective_value(sp.mip) - sp_consensus_vals = value.(sp.consensus_vars) - return SpSolution( - obj = obj, - consensus_vals = sp_consensus_vals, - residuals = zeros(G), - ) + sp_vals = value.(sp.consensus_vars) + return PhSubProblemSolution(obj = obj, vals = sp_vals, residuals = zeros(G)) end function update_λ_and_residuals!( - solution::SpSolution, - ph_sp_params::SpParams, - global_consensus_vals::Array{Float64,1}, + solution::PhSubProblemSolution, + params::PHSubProblemParams, + target::Array{Float64,1}, )::Nothing - n_vars = length(solution.consensus_vals) - ph_sp_params.global_consensus_vals = global_consensus_vals + n_vars = length(solution.vals) + params.target = target for n in 1:n_vars - solution.residuals[n] = - solution.consensus_vals[n] - ph_sp_params.global_consensus_vals[n] - ph_sp_params.λ[n] += ph_sp_params.ρ * solution.residuals[n] + solution.residuals[n] = solution.vals[n] - params.target[n] + params.λ[n] += params.ρ * solution.residuals[n] end end @@ -179,22 +163,22 @@ end function print_progress( mpi::MpiInfo, - iteration::IterationInfo, + iteration::PHIterationInfo, print_interval, )::Nothing if !mpi.root return end - if iteration.it_num % print_interval != 0 + if iteration.iteration_number % print_interval != 0 return end @info @sprintf( - "Current iteration %8d %20.6e %20.6e %12.2f %% %8.2f %8.2f", - iteration.it_num, + "%8d %20.6e %20.6e %12.2f %% %8.2f %8.2f", + iteration.iteration_number, iteration.global_obj, iteration.global_infeas, iteration.global_residual * 100, - iteration.it_time, + iteration.iteration_time, iteration.total_elapsed_time ) end @@ -209,21 +193,21 @@ end function should_stop( mpi::MpiInfo, - iterations::Array{IterationInfo,1}, - criteria::TerminationCriteria, + iterations::Array{PHIterationInfo,1}, + termination::PHTermination, )::Bool - if length(iterations) >= criteria.max_iterations + if length(iterations) >= termination.max_iterations if mpi.root @info "Iteration limit reached. Stopping." end return true end - if length(iterations) < criteria.min_iterations + if length(iterations) < termination.min_iterations return false end - if last(iterations).total_elapsed_time > criteria.max_time + if last(iterations).total_elapsed_time > termination.max_time if mpi.root @info "Time limit reached. Stopping." end @@ -233,9 +217,9 @@ function should_stop( curr_it = last(iterations) prev_it = iterations[length(iterations)-1] - if curr_it.global_infeas < criteria.min_feasibility + if curr_it.global_infeas < termination.min_feasibility obj_change = abs(prev_it.global_obj - curr_it.global_obj) - if obj_change < criteria.min_improvement + if obj_change < termination.min_improvement if mpi.root @info "Feasibility limit reached. Stopping." end diff --git a/src/solution/methods/ProgressiveHedging/structs.jl b/src/solution/methods/ProgressiveHedging/structs.jl index 39d68a4..46f68f2 100644 --- a/src/solution/methods/ProgressiveHedging/structs.jl +++ b/src/solution/methods/ProgressiveHedging/structs.jl @@ -4,81 +4,34 @@ using JuMP, MPI, TimerOutputs -mutable struct TerminationCriteria - max_iterations::Int - max_time::Float64 - min_feasibility::Float64 - min_improvement::Float64 - min_iterations::Int - - function TerminationCriteria(; - max_iterations::Int = 1000, - max_time::Float64 = 14400.0, - min_feasibility::Float64 = 1e-3, - min_improvement::Float64 = 1e-3, - min_iterations::Int = 2, - ) - return new( - max_iterations, - max_time, - min_feasibility, - min_improvement, - min_iterations, - ) - end +Base.@kwdef mutable struct PHTermination + max_iterations::Int = 1000 + max_time::Float64 = 14400.0 + min_feasibility::Float64 = 1e-3 + min_improvement::Float64 = 1e-3 + min_iterations::Int = 2 end -Base.@kwdef mutable struct IterationInfo - it_num::Int - sp_consensus_vals::Array{Float64,1} - global_consensus_vals::Array{Float64,1} - sp_obj::Float64 +Base.@kwdef mutable struct PHIterationInfo + global_infeas::Float64 global_obj::Float64 - it_time::Float64 - total_elapsed_time::Float64 global_residual::Float64 - global_infeas::Float64 -end - -mutable struct ProgressiveHedging <: SolutionMethod - consensus_vars::Union{Array{VariableRef,1},Nothing} - weights::Union{Array{Float64,1},Nothing} - initial_global_consensus_vals::Union{Array{Float64,1},Nothing} - num_of_threads::Int - ρ::Float64 - λ_default::Float64 - print_interval::Int - termination_criteria::TerminationCriteria - - function ProgressiveHedging(; - consensus_vars::Union{Array{VariableRef,1},Nothing} = nothing, - weights::Union{Array{Float64,1},Nothing} = nothing, - initial_global_consensus_vals::Union{Array{Float64,1},Nothing} = nothing, - num_of_threads::Int = 1, - ρ::Float64 = 1.0, - λ_default::Float64 = 0.0, - print_interval::Int = 1, - termination_criteria::TerminationCriteria = TerminationCriteria(), - ) - return new( - consensus_vars, - weights, - initial_global_consensus_vals, - num_of_threads, - ρ, - λ_default, - print_interval, - termination_criteria, - ) - end + iteration_number::Int + iteration_time::Float64 + sp_vals::Array{Float64,1} + sp_obj::Float64 + target::Array{Float64,1} + total_elapsed_time::Float64 end -struct FinalResult - obj::Float64 - vals::Any - infeasibility::Float64 - total_iteration_num::Int - wallclock_time::Float64 +Base.@kwdef mutable struct ProgressiveHedging <: SolutionMethod + initial_weights::Union{Vector{Float64},Nothing} = nothing + initial_target::Union{Vector{Float64},Nothing} = nothing + ρ::Float64 = 1.0 + λ::Float64 = 0.0 + print_interval::Int = 1 + termination::PHTermination = PHTermination() + inner_method::SolutionMethod = XavQiuWanThi2019.Method() end struct SpResult @@ -86,23 +39,23 @@ struct SpResult vals::Array{Float64,1} end -Base.@kwdef mutable struct SubProblem +Base.@kwdef mutable struct PHSubProblem mip::JuMP.Model obj::AffExpr consensus_vars::Array{VariableRef,1} weights::Array{Float64,1} end -Base.@kwdef struct SpSolution +Base.@kwdef struct PhSubProblemSolution obj::Float64 - consensus_vals::Array{Float64,1} + vals::Array{Float64,1} residuals::Array{Float64,1} end -Base.@kwdef mutable struct SpParams +Base.@kwdef mutable struct PHSubProblemParams ρ::Float64 λ::Array{Float64,1} - global_consensus_vals::Array{Float64,1} + target::Array{Float64,1} end struct MpiInfo @@ -118,9 +71,3 @@ struct MpiInfo return new(comm, rank, is_root, nprocs) end end - -Base.@kwdef struct Callbacks - before_solve_subproblem::Any - after_solve_subproblem::Any - after_iteration::Any -end diff --git a/test/src/solution/methods/ProgressiveHedging/ph.jl b/test/src/solution/methods/ProgressiveHedging/ph.jl index 1e6acd0..0cc1f49 100644 --- a/test/src/solution/methods/ProgressiveHedging/ph.jl +++ b/test/src/solution/methods/ProgressiveHedging/ph.jl @@ -1,4 +1,4 @@ -using Cbc +using HiGHS using MPI using JuMP using UnitCommitment @@ -9,29 +9,32 @@ function fixture(path::String)::String return "$basedir/../../../../fixtures/$path" end -# 1. Initialize MPI +# Initialize MPI MPI.Init() -# 2. Configure progressive hedging method +# Configure progressive hedging method ph = UnitCommitment.ProgressiveHedging() -# 3. Read problem instance +# Read problem instance instance = UnitCommitment.read( [fixture("case14.json.gz"), fixture("case14.json.gz")], ph, ) -# 4. Build JuMP model +# Build JuMP model model = UnitCommitment.build_model( instance = instance, - optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), + optimizer = optimizer_with_attributes( + HiGHS.Optimizer, + MOI.Silent() => true, + ), ) -# 5. Run the decentralized optimization algorithm +# Run the decentralized optimization algorithm UnitCommitment.optimize!(model, ph) -# 6. Fetch the solution +# Fetch the solution solution = UnitCommitment.solution(model, ph) -# 7. Close MPI +# Close MPI MPI.Finalize() From b8ada6432ad78ccf5489b69a3a5be5d255c6e565 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Fri, 26 May 2023 11:50:41 -0500 Subject: [PATCH 5/5] Format source code --- test/src/solution/methods/ProgressiveHedging/usage_test.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/solution/methods/ProgressiveHedging/usage_test.jl b/test/src/solution/methods/ProgressiveHedging/usage_test.jl index 6e4342f..418dda9 100644 --- a/test/src/solution/methods/ProgressiveHedging/usage_test.jl +++ b/test/src/solution/methods/ProgressiveHedging/usage_test.jl @@ -13,4 +13,4 @@ function solution_methods_ProgressiveHedging_usage_test() ) end end -end \ No newline at end of file +end