Skip to content

Commit

Permalink
Working on #6
Browse files Browse the repository at this point in the history
  • Loading branch information
mperhez committed Oct 27, 2022
1 parent 0d16b75 commit 7baa358
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 167 deletions.
82 changes: 82 additions & 0 deletions examples/example_1 copy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using NAssets
using Plots
using GraphRecipes


ntw_size = 9
ntw_topo = 4
n_steps = 100
# Prepare base simulation configuration
bcfg = (
seed = 123,
ntw_topo = ntw_topo,
size = ntw_size,
ctl_model = 7,
ctl_k = 3,
ctl_B = 0.7,
# prob_random_walks = 0.8,
n_steps = n_steps,
deterioration = [ (rul,t,a) -> rul - a 1 ],
traffic_dist_params = [1.0, 0.07],
#max_queue_ne = 4000,
#capacity_factor = 1.5,
# link_capacity = 1000,
#clear_cache_graph_freq = 5,
#pkt_per_tick = 5000,
# max_msg_live = 5,
init_sne_params = (ids=[5,9],capacity_factor=[2,2]),
init_link_params = (ids=[(2,5),(4,5),(5,8),(5,6)],capacities=Int.(ones(4)*1000)),
ntw_services = [
(3, 6),
#(1, 4),
(9,1),
(8,4)
]
)

#obtain the configuration object. This method will create the control and underlying graphs as well as other objects required to run the simulation.
cfg = NAssets.config(bcfg)


# run simulation
ctl_ags, vnes, modbin = NAssets.single_run(cfg,log_to_file=false)

# Three objects are returned with the state trajectory of the control agents, network elements (assets) and the entire model


# Condition deterioration

prul = plot(title="Network Assets RUL")

for ne in 1:size(vnes,1)
plot!(prul,[ ne_st.rul for ne_st in vnes[ne] ], linestyle= ne == 2 ? :dash : :dot )
end
prul
# hline!(p,[10], linestyle=:dash, c=:red, label="")
# hline!(p,[0.1], linestyle=:dash, c=:red, label="")

# print(modbin)



#rings
#gp = NAssets.get_graph(-1,40,NAssets.GraphModel(7);k=2,B=0)

#grid?

gp = NAssets.get_graph(-1,ntw_size,NAssets.GraphModel(5))
graphplot(gp)

# tpt_p = plot(
# ylabel!(NAssets.plot_tpt_step(rd[1].snes_ts,rd[4].snes_ts,rd[1].model_ts,t),"Throughput\n(MB/time)",guidefontsize=6)
# ,plot_tpt_step(rd[2].snes_ts,rd[4].snes_ts,rd[2].model_ts,t)
# ,plot_tpt_step(rd[3].snes_ts,rd[4].snes_ts,rd[3].model_ts,t)
# ,layout=(1,3))

p = plot(title="End-to-end Throughput")
for ne in 1:size(vnes,1)
plot!(p,NAssets.get_throughput_trj(vnes[ne],n_steps), linestyle= :solid , label = "Asset_$ne", legend = :outerright
)
end
p

103 changes: 91 additions & 12 deletions examples/example_1.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,57 @@
using NAssets
using Plots
using GraphRecipes


tst = (rul,t,a) -> begin
println("rul: $rul --- a: $a")
return last(rul) - ( a * t )
end

ntw_size = 9
ntw_topo = 4
n_steps = 100
# Prepare base simulation configuration
bcfg = (
ntw_topo = 2,
size = 5,
ctl_model = 1,
n_steps = 80,
traffic_dist_params = [1.0, 0.05],
ntw_services = [(3, 5), (1, 4)]
seed = 123,
ntw_topo = ntw_topo,
size = ntw_size,
ctl_model = 7,
ctl_k = 3,
ctl_B = 0.7,
# prob_random_walks = 0.8,
n_steps = n_steps,
deterioration = [ (rul,t,a) -> rul - a 1 ],
prediction = [ tst 1 ],
traffic_dist_params = [1.0, 0.07],
traffic_packets = 100,
#max_queue_ne = 100,
#capacity_factor = 5,
link_capacity = 2000,
max_msg_live = 10,
#clear_cache_graph_freq = 5,
pkt_per_tick = 3100,
# max_msg_live = 5,
mnt_bc_duration = 1,
mnt_bc_cost = 0.5,
mnt_wc_duration = 5,
mnt_wc_cost = 5,
mnt_policy = 0,
predictive_freq = 10,
prediction_window = 10,
init_sne_params =
#(ids=[9],capacity_factor=[4]),
(ids=[9],mnt_policy=[1]),
# init_link_params = (
# # # ids=[(2,5),(4,5),(5,8),(5,6),(1,2),(3,6),(6,9),(8,9)],capacities=[2000,4000,4000,500,4000,5000,500,500]
# ids = [(7,8)],
# capacities = [2000]
# ),
ntw_services = [
(3, 7),
(9,1),
(8,4)
]
)

#obtain the configuration object. This method will create the control and underlying graphs as well as other objects required to run the simulation.
Expand All @@ -22,13 +63,51 @@ ctl_ags, vnes, modbin = NAssets.single_run(cfg,log_to_file=false)

# Three objects are returned with the state trajectory of the control agents, network elements (assets) and the entire model

#
p = plot(title="Network Assets RUL")

# Condition deterioration

prul = plot(title="Network Assets RUL")

for ne in 1:size(vnes,1)
plot!(p,[ ne_st.rul for ne_st in vnes[ne] ], linestyle= ne == 2 ? :dash : :dot )
plot!(prul,[ ne_st.rul for ne_st in vnes[ne] ], linestyle= ne == 2 ? :dash : :dot )
end
hline!(p,[10], linestyle=:dash, c=:red, label="")
hline!(p,[0.1], linestyle=:dash, c=:red, label="")
p
prul
hline!(prul,[10], linestyle=:dash, c=:red, label="")
prul
# hline!(p,[0.1], linestyle=:dash, c=:red, label="")

# print(modbin)



#rings
#gp = NAssets.get_graph(-1,40,NAssets.GraphModel(7);k=2,B=0)

#grid?

# gp = NAssets.get_graph(-1,ntw_size,NAssets.GraphModel(5))
# graphplot(gp)

# tpt_p = plot(
# ylabel!(NAssets.plot_tpt_step(rd[1].snes_ts,rd[4].snes_ts,rd[1].model_ts,t),"Throughput\n(MB/time)",guidefontsize=6)
# ,plot_tpt_step(rd[2].snes_ts,rd[4].snes_ts,rd[2].model_ts,t)
# ,plot_tpt_step(rd[3].snes_ts,rd[4].snes_ts,rd[3].model_ts,t)
# ,layout=(1,3))

# p = plot(title="End-to-end Throughput")
# for ne in 1:size(vnes,1)
# plot!(p,NAssets.get_throughput_trj(vnes[ne],n_steps), linestyle= :solid , label = "Asset_$ne", legend = :outerright
# )
# end
# p



# pls = plot(title="Packet loss")
# for ne in 1:size(vnes,1)
# plot!(pls,[ vnes[ne][i].drop_pkt for i=1:n_steps ], linestyle= :solid , label = "Asset_$ne", legend = :outerright
# )
# end
# pls


4 changes: 2 additions & 2 deletions src/ctl/queries_multiple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function do_query!(msg::OFMessage,a::Agent,model)

path = do_query(query_time,query,query_graph,query_paths)

# log_info(model.ticks,a.id,"query: $(query) ----path found: $path ----> in precalc paths: $(query_paths)")
# log_info(model.ticks,a.id,"query: $(query) ----path found: $path ----> in precalc paths: $(query_paths) -- query graph: $(nv(query_graph)))")

# For no centralised control topo
if model.ctrl_model != GraphModel(1) # ¬ centralised
Expand All @@ -33,7 +33,7 @@ function do_query!(msg::OFMessage,a::Agent,model)

#valid path not found?, need to send queries to nbs
if isempty(path)
#should always query nbs even if has already queried with no reply?
#TODO should always query nbs even if has already queried with no reply?
sent_to = query_nbs!(a,msg,query_graph,query,[a.id],model)
record_pending_query!(a,model.ticks,sent_to,query)
push!(a.pending,(model.ticks,msg,false)) #false:not reprocess next time
Expand Down
33 changes: 20 additions & 13 deletions src/model/netManModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function create_sim_asset_agents!(model)

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

log_info(model.ticks,"Prediction in init: $(last(prediction))")
mnt = @match mnt_policy begin
1 => MaintenanceInfoPreventive(deterioration,prediction,model)
2 => MaintenanceInfoPredictive(deterioration,prediction,model)
Expand Down Expand Up @@ -260,10 +261,9 @@ end
Initialise control agents
"""
function init_agent!(a::Agent,model)

nodes = sort([get_controlled_assets(a.id,model)...])
if model.ctrl_model != GraphModel(1)
#Calculate sub graphs and init msg channels among agents
nodes = [get_controlled_assets(a.id,model)...]
sub_g = get_subgraph(model.ntw_graph,nodes,:eid)

nodes = [a.id]
Expand All @@ -284,17 +284,24 @@ function init_agent!(a::Agent,model)
end

##
if a.maintenance.policy != CorrectiveM
schedule_event!(a,CTL_Event(4),a.maintenance.predictive_freq,Array{Int64,1}())
if a.maintenance.policy == PredictiveM
if Symbol("py_integration") in keys(model.properties)
#conversion to py
ajm_py = model.py_integration.np.matrix(adjacency_matrix(a.ntw_graph))
model.py_integration.opt_init.optimisation_initialisation( ajm_py,
model.traffic_dist_params
#[1,0.05]
, model.mnt_bc_cost, model.mnt_bc_duration, model.mnt_wc_cost, model.mnt_wc_duration)
end

snes = [ getindex(model,ca) for ca in nodes ]

with_predictions = [ sne.id for sne in snes if sne.maintenance.policy != CorrectiveM ]

log_info(model.ticks,a.id,"with_predictions: $(with_predictions)")

with_py_plan = [ sne.id for sne in snes if sne.maintenance.policy == PredictiveM ]

schedule_event!(a,CTL_Event(4),a.maintenance.predictive_freq,with_predictions)

if !isempty(with_py_plan)
if Symbol("py_integration") in keys(model.properties)
#conversion to py
ajm_py = model.py_integration.np.matrix(adjacency_matrix(a.ntw_graph))
model.py_integration.opt_init.optimisation_initialisation( ajm_py,
model.traffic_dist_params
, model.mnt_bc_cost, model.mnt_bc_duration, model.mnt_wc_cost, model.mnt_wc_duration)
end
end
end
Expand Down
Loading

0 comments on commit 7baa358

Please sign in to comment.