Skip to content

Commit

Permalink
Solving #2 and #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
mperhez committed Aug 27, 2022
1 parent 548a40d commit a8f9fc0
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 120 deletions.
9 changes: 6 additions & 3 deletions src/core/core_structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,16 @@ mutable struct MaintenanceInfo
duration::Int64
# Cost of this type of maintenance
cost::Float64
#To trigger preventive maintenance ahead of breakdown
threshold::Int64
#Threshold for triggering maintenance.
threshold::Float64
# prediction function and parameters in an array
prediction::Array{Any}
#How often rul predictions are run
predictive_freq::Int64
# how many steps ahead the prediction is going to be for
prediction_window::Int64
deterioration_parameter::Float64
# deterioration function and parameters in an array
deterioration::Array{Any}
#Duration of the corrective maintenance
reference_duration::Int64
#Cost of the corrective maintenance
Expand Down
9 changes: 6 additions & 3 deletions src/core/graph_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,14 @@ Given the graph `g`, this function creates a deep copy of `g` and removes all th
function soft_remove_vertex(g::AbstractGraph,dpn_id::Int)

new_g = deepcopy(g)
nbs₀ = deepcopy(all_neighbors(new_g,dpn_id))
l_dpn_id = to_local_vertex(new_g,dpn_id) # local id of dpn_id

# @show [ get_prop(new_g,j,:eid) for j=1:nv(new_g) ]
nbs₀ = deepcopy(all_neighbors(new_g,l_dpn_id))

for nb in nbs₀
rem_edge!(new_g,dpn_id,nb)
rem_edge!(new_g,nb,dpn_id)
rem_edge!(new_g,l_dpn_id,nb)
rem_edge!(new_g,nb,l_dpn_id)
end
return new_g#
end
Expand Down
77 changes: 57 additions & 20 deletions src/ctl/agent_control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ function controlled_sne_down!(a::Agent,dpn_id::Int,model)

lvb = to_local_vertex(a.base_ntw_graph,dpn_id)
lvc = to_local_vertex(a.ntw_graph,dpn_id)
a.base_ntw_graph = soft_remove_vertex(a.base_ntw_graph,lvb)
a.ntw_graph = soft_remove_vertex(a.ntw_graph,lvc)
a.base_ntw_graph = lvb in [ get_prop(a.base_ntw_graph,j,:eid) for j=1:nv(a.base_ntw_graph) ] ? soft_remove_vertex(a.base_ntw_graph,lvb) : a.base_ntw_graph
a.ntw_graph = lvc in [ get_prop(a.ntw_graph,j,:eid) for j=1:nv(a.ntw_graph) ] ? soft_remove_vertex(a.ntw_graph,lvc) : a.ntw_graph

#TODO implement when a control agent is down too
# do_drop!(msg,a,model)
Expand Down Expand Up @@ -339,13 +339,13 @@ function remove_drop_sne!(a::Agent,dpid::Int64,drop_time::Int64)
lvb = to_local_vertex(a.base_ntw_graph,dpid)
lvc = to_local_vertex(a.ntw_graph,dpid)

log_info(drop_time,a.id," Removing dpid: $dpid from local base: $lvb --- local curr: $lvc")
# log_info(drop_time,a.id," Removing dpid: $dpid from local base: $lvb --- local curr: $lvc")

if lvb != 0
a.base_ntw_graph = soft_remove_vertex(a.base_ntw_graph,lvb)
a.base_ntw_graph = lvb in [ get_prop(a.base_ntw_graph,j,:eid) for j=1:nv(a.base_ntw_graph) ] ? soft_remove_vertex(a.base_ntw_graph,lvb) : a.base_ntw_graph
end
if lvc != 0
a.ntw_graph = soft_remove_vertex(a.ntw_graph,lvc)
a.ntw_graph = lvc in [ get_prop(a.ntw_graph,j,:eid) for j=1:nv(a.ntw_graph) ] ? soft_remove_vertex(a.ntw_graph,lvc) : a.ntw_graph
end

state = get_state(a)
Expand All @@ -366,45 +366,82 @@ It deals with prediction of unavailability (for a given time window) of a set of
"""
function do_update_flows_from_changes!(a::Agent,ntw_changes::Vector{Int64},model::ABM)
#TODO operation for other than centralised agent

if get_state(a).up

log_info(model.ticks,a.id,"Pred_Down: ntw_changes!!!!!! $(ntw_changes)")
# log_info(model.ticks,a.id,"Pred_Down: ntw_changes!!!!!! $(ntw_changes)")

joining_nodes = filter(x->x>0,ntw_changes)
dropping_nodes = -1 * filter(x->x<0,ntw_changes)

base_g = deepcopy(a.base_ntw_graph)
query_graph = deepcopy(a.ntw_graph)

#check graphs consistency
## for each node in G
## check if there is node is up in model
## if it is not then soft_remove from graph and update

lsnes = get_live_snes(model)

bg_vs = [ get_prop(base_g,v,:eid) for v in 1:nv(base_g) ]
qg_vs = [ get_prop(query_graph,v,:eid) for v in 1:nv(query_graph) ]

for gid in bg_vs
if ~(gid in lsnes)
base_g = soft_remove_vertex(base_g,gid)
end
end

for gid in qg_vs
if ~(gid in lsnes)
query_graph = soft_remove_vertex(query_graph,gid)
end
end

# log_info(model.ticks,a.id,"[A] Base graph: $([ get_prop(base_g,v,:eid) for v in 1:nv(base_g)]) -- edges: $([e for e in edges(base_g)])")
# log_info(model.ticks,a.id,"[A] query graph: $([ get_prop(query_graph,v,:eid) for v in 1:nv(query_graph)]) -- edges: $([e for e in edges(query_graph)])")
# log_info(model.ticks,a.id," jnodes -> $joining_nodes")
#add nodes
for jng_id in joining_nodes
query_graph = soft_remove_vertex(query_graph,jng_id)

base_g = a.base_ntw_graph

#remove node from existing query graph if exists, to refresh with base graph
lvq = to_local_vertex(query_graph,jng_id)
query_graph = lvq in [ get_prop(query_graph,j,:eid) for j=1:nv(query_graph) ] ? soft_remove_vertex(query_graph,lvq) : query_graph

lv = to_local_vertex(base_g,jng_id)
nbs = neighbors(base_g,lv)

nbs = lv != 0 ? neighbors(base_g,lv) : []
# log_info(model.ticks,a.id," jnode_id -> $jng_id -- lv: $lv")
#TODO check if nbs is up?

#restore links in query_grpah from base ntw_graph
# log_info(model.ticks,a.id,"nbs: $nbs")
for nb_id in nbs
add_edge!(query_graph,nb_id,jng_id)
add_edge!(query_graph,jng_id,nb_id)
if is_up(getindex(model,nb_id))
add_edge!(query_graph,nb_id,jng_id)
add_edge!(query_graph,jng_id,nb_id)
end

end
end

#remove nodes
#remove nodes if present in query graph
for dpn_id in dropping_nodes
query_graph = soft_remove_vertex(query_graph,dpn_id)
# log_info(model.ticks,a.id," dnode_id -> $dpn_id")
lv = to_local_vertex(query_graph,dpn_id)
query_graph = lv in [ get_prop(query_graph,j,:eid) for j=1:nv(query_graph) ] ? soft_remove_vertex(query_graph,dpn_id) : query_graph
end

query_time = model.ticks

#Only trigger queries if maintenance policy is not predictive (where routes/flows come from the optimisation algorithm)
queries = a.maintenance.policy != PredictiveM ? model.ntw_services : []


#TODO Why query graph has nodes that are down??
# log_info(model.ticks,a.id,"[B] Base graph: $([ get_prop(base_g,v,:eid) for v in 1:nv(base_g)])")
# log_info(model.ticks,a.id,"[B] query graph: $([ get_prop(query_graph,v,:eid) for v in 1:nv(query_graph)])")

for query in queries
query_paths = Dict{Tuple{Int64,Int64},Array{Tuple{Int64,Float64,Float64,Array{Int64}}}}()

query_paths = Dict{Tuple{Int64,Int64},Array{Tuple{Int64,Float64,Float64,Array{Int64}}}}()
path = do_query(query_time,query,query_graph,query_paths)

if isempty(path)
Expand Down Expand Up @@ -432,7 +469,7 @@ Update flows of the snes controlled by the agent a and for the path given,
function do_update_flows_from_path!(a::Agent,path::Array{Int64,1},model::ABM)
msg = OFMessage(-1, model.ticks,-1,0,OFPR_ADD_FLOW,[])
# log_info(model.ticks,a.id,"active path: $path")
install_flow!(a,path,model,msg)
# install_flow!(a,path,model,msg)
if length(path) > 1
k = (first(path),last(path))
# log_info(model.ticks,a.id,"key: $(k) ==> Ag Path: $spath")
Expand Down
17 changes: 11 additions & 6 deletions src/model/netManModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,20 @@ function create_sim_asset_agents!(model)
#next_fire = rand(0:0.2:model.:Τ)
id = nextid(model)

deterioration = id in model.init_sne_params.ids ? model.init_sne_params.deterioration[first(indexin(id,model.init_sne_params.ids))] : model.deterioration
deterioration = typeof(model.init_sne_params) <: NamedTuple && id in model.init_sne_params.ids && Symbol("deterioration") in keys(model.init_sne_params) ? model.init_sne_params.deterioration[first(indexin(id,model.init_sne_params.ids))] : model.deterioration

mnt = @match model.mnt_policy begin
1 => MaintenanceInfoPreventive(deterioration,model)
2 => MaintenanceInfoPredictive(deterioration,model)
_ => MaintenanceInfoCorrective(deterioration,model)
prediction = typeof(model.init_sne_params) <: NamedTuple && id in model.init_sne_params.ids && Symbol("prediction") in keys(model.init_sne_params) ? model.init_sne_params.prediction[first(indexin(id,model.init_sne_params.ids))] : model.prediction

mnt_policy = typeof(model.init_sne_params) <: NamedTuple && id in model.init_sne_params.ids && Symbol("mnt_policy") in keys(model.init_sne_params) ? model.init_sne_params.mnt_policy[first(indexin(id,model.init_sne_params.ids))] : model.mnt_policy

mnt = @match mnt_policy begin
1 => MaintenanceInfoPreventive(deterioration,prediction,model)
2 => MaintenanceInfoPredictive(deterioration,prediction,model)
_ => MaintenanceInfoCorrective(deterioration,prediction,model)
end

capacity_factor = typeof(model.init_sne_params) <: NamedTuple && id in model.init_sne_params.ids && Symbol("capacity_factor") in keys(model.init_sne_params) ? model.init_sne_params.capacity_factor[first(indexin(id,model.init_sne_params.ids))] : model.capacity_factor

capacity_factor = id in model.init_sne_params.ids ? model.init_sne_params.capacity_factor[first(indexin(id,model.init_sne_params.ids))] : model.capacity_factor
max_q_capacity = capacity_factor * model.pkt_per_tick
a_params[:pkt_per_tick] = max_q_capacity
a = add_agent_pos!(
Expand Down
Loading

0 comments on commit a8f9fc0

Please sign in to comment.