Skip to content

Commit 5ab49f9

Browse files
committed
Fixing issues on averaging consensus.
1 parent a644e2a commit 5ab49f9

4 files changed

Lines changed: 25 additions & 10 deletions

File tree

src/algorithm/consensus/averaging.jl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ end
1010
mutable struct NoConsensusActor <: ConsensusActor
1111
end
1212

13-
struct AveragingConsensusMessage
13+
struct AveragingConsensusMessage <: OptimizationMessage
1414
λ::Vector{Float64}
1515
k::Int
1616
data::Any
17+
initial::Bool
18+
AveragingConsensusMessage(λ, k, data, initial=false) = new(λ, k, data, initial)
1719
end
1820

1921
struct ConsensusFinishedMessage
@@ -27,7 +29,7 @@ end
2729
first_message = true
2830
k::Int = 0
2931
max_iter::Int = 50
30-
λ::Vector{Float64} = Vector{Real}()
32+
λ::Vector{Float64} = [1]
3133

3234
initial_λ::Real
3335
α::Real
@@ -37,14 +39,22 @@ end
3739
end
3840

3941
function on_exchange_message(algorithm_data::AveragingConsensusAlgorithm, carrier::Carrier, message::AveragingConsensusMessage, meta::Any)
42+
#@info "Doing something" algorithm_data.first_message message.k algorithm_data.k
4043
if message.k >= algorithm_data.max_iter
41-
# abort if iteration count is reached
44+
if algorithm_data.first_message
45+
# negotiation is over, only new with k=0 is expected
46+
return
47+
end
48+
# finish if iteration count is reached, reset all state data
4249
algorithm_data.finish_callback(algorithm_data, carrier)
50+
algorithm_data.first_message = true
51+
empty!(algorithm_data.message_queue)
4352
return
4453
end
45-
46-
if algorithm_data.first_message
54+
55+
if algorithm_data.first_message || message.initial
4756
algorithm_data.first_message = false
57+
algorithm_data.k = 0
4858
algorithm_data.λ = ones(length(message.λ)) .* algorithm_data.initial_λ
4959

5060
for addr in others(carrier, "")
@@ -55,11 +65,11 @@ function on_exchange_message(algorithm_data::AveragingConsensusAlgorithm, carrie
5565

5666
push!(queue, message)
5767

58-
if length(queue) == length(others(carrier, ""))
68+
if length(queue) == length(others(carrier, "")) || algorithm_data.k < message.k
5969
avgλ = sum(m.λ for m in queue) ./ length(queue)
6070
algorithm_data.λ .+= algorithm_data.α .* (avgλ .- algorithm_data.λ) .+ gradient_term(algorithm_data.actor, algorithm_data.λ, message.data)
71+
algorithm_data.k = message.k + 1
6172

62-
algorithm_data.k += message.k + 1
6373
delete!(algorithm_data.message_queue, message.k)
6474

6575
for addr in others(carrier, "")

src/algorithm/consensus/economic_dispatch.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ export LinearCostEconomicDispatchConsensusActor
33
@kwdef mutable struct LinearCostEconomicDispatchConsensusActor <: ConsensusActor
44
cost::Real
55
P_max::Real
6-
ρ::Real = 0.01
6+
ρ::Real = 0.05
77
ϵ::Real = 0.1
88
P_min::Real = 0
99
N_guess::Int = 10
10-
P::Vector{Float64} = Vector{Real}()
10+
P::Vector{Float64} = [0]
1111
end
1212

1313
function DistributedResourceOptimization.gradient_term(actor::LinearCostEconomicDispatchConsensusActor, λ::Vector{<:Real}, P_target::Vector{<:Real})

src/algorithm/core.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Subtypes should implement specific distributed algorithm logic for resource opti
88
"""
99
abstract type DistributedAlgorithm end
1010

11+
"""
12+
Supertype for any optimization message. Used to detect whether the message is part of an optimization
13+
"""
14+
abstract type OptimizationMessage end
1115

1216
"""
1317
on_exchange_message(algorithm::DistributedAlgorithm, carrier::Carrier, message_data::Any, meta::Any)
@@ -27,6 +31,7 @@ Processes the received exchange message, updating the algorithm's state or trigg
2731
May return a result or perform side effects depending on the implementation.
2832
"""
2933
function on_exchange_message(algorithm::DistributedAlgorithm, carrier::Carrier, message_data::Any, meta::Any)
34+
@error algorithm carrier message_data meta
3035
throw("NotImplementedOrWrongArgumentTypes")
3136
end
3237

src/carrier/mango.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function DistributedOptimizationRole(algorithm::DistributedAlgorithm; tid::Symbo
103103
end
104104

105105
function Mango.handle_message(role::DistributedOptimizationRole, message::Any, meta::Any)
106-
if haskey(meta, "optimization_message") && meta["optimization_message"]
106+
if haskey(meta, "optimization_message") && meta["optimization_message"] || message isa OptimizationMessage
107107
on_exchange_message(role.algorithm, role.carrier, message, meta)
108108
end
109109
end

0 commit comments

Comments
 (0)