File size: 2,218 Bytes
43c68a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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 JuMP

"""
    function compute_lmp(
        model::JuMP.Model,
        method::ConventionalLMP;
        optimizer,
    )::OrderedDict{Tuple{String,String,Int},Float64}

Calculates conventional locational marginal prices of the given unit commitment
instance. Returns a dictionary mapping `(bus_name, time)` to the marginal price.

Arguments
---------

- `model`:
    the UnitCommitment model, must be solved before calling this function.

- `method`:
    the LMP method.

- `optimizer`:
    the optimizer for solving the LP problem.

Examples
--------

```julia
using UnitCommitment
using HiGHS

import UnitCommitment: ConventionalLMP

# Read benchmark instance
instance = UnitCommitment.read_benchmark("matpower/case118/2018-01-01")

# Build the model
model = UnitCommitment.build_model(
    instance = instance,
    optimizer = HiGHS.Optimizer,
)

# Optimize the model
UnitCommitment.optimize!(model)

# Compute the LMPs using the conventional method
lmp = UnitCommitment.compute_lmp(
    model,
    ConventionalLMP(),
    optimizer = HiGHS.Optimizer,
)

# Access the LMPs
# Example: "s1" is the scenario name, "b1" is the bus name, 1 is the first time slot
@show lmp["s1", "b1", 1]
```
"""
function compute_lmp(
    model::JuMP.Model,
    ::ConventionalLMP;
    optimizer,
)::OrderedDict{Tuple{String,String,Int},Float64}
    if !has_values(model)
        error("The UC model must be solved before calculating the LMPs.")
    end
    lmp = OrderedDict()

    @info "Fixing binary variables and relaxing integrality..."
    vals = Dict(v => value(v) for v in all_variables(model))
    for v in all_variables(model)
        if is_binary(v)
            unset_binary(v)
            fix(v, vals[v])
        end
    end
    relax_integrality(model)
    set_optimizer(model, optimizer)

    @info "Solving the LP..."
    JuMP.optimize!(model)

    @info "Getting dual values (LMPs)..."
    for (key, val) in model[:eq_net_injection]
        lmp[key] = dual(val)
    end

    return lmp
end