Browse Source

removed profiling, added shorthand notations for functions and simulate devs now accepts ints as well

Snej69420 4 months ago
parent
commit
8f016ea42a
3 changed files with 72 additions and 54 deletions
  1. 49 30
      src/AtomicDEVS.jl
  2. 19 19
      src/CoupledDEVS.jl
  3. 4 5
      src/DEVS2Agents.jl

+ 49 - 30
src/AtomicDEVS.jl

@@ -4,6 +4,8 @@ using InteractiveUtils
 using TimerOutputs
 const Timer = TimerOutput()
 
+PRECISION::Float64 = 1e-6
+
 @enum Transitions NONE INTERNAL EXTERNAL CONFLUENT
 
 @agent struct AtomicDEVS{I, O}(NoSpaceAgent)
@@ -11,13 +13,28 @@ const Timer = TimerOutput()
     out_ports::O = NamedTuple()
 
     last_event_time::Float64 = 0.0
-    next_ta::Float64 = -1.0 # -1, initialization needed
+    next_ta::Float64 = -1.0 # -1, initialisation needed
 
     is_imminent::Bool = false
     is_receiving::Bool = false
 end
 
 # DEVS Interface (To be extended by the modeler)
+function confluent_transition! end
+const conf_transition! = confluent_transition!
+
+function external_transition! end
+const ext_transition! = external_transition!
+
+function internal_transition! end
+const int_transition! = internal_transition!
+
+function time_advance end
+const ta = time_advance
+
+function output! end
+
+
 function confluent_transition!(agent, model, bag)
     internal_transition!(agent, model)
     external_transition!(agent, model, 0.0, bag)
@@ -28,7 +45,7 @@ time_advance(agent, model) = error("time_advance not implemented for $(typeof(ag
 output!(agent, model) = nothing
 
 function simulate_devs!(model, duration::Float64=Inf)
-    @timeit Timer "Simulation Loop" begin
+#     @timeit Timer "Simulation Loop" begin
         if model.logging !== nothing && isempty(model.event_logs)
             if model.logging == ""
                 print_initial_conditions(model)
@@ -58,14 +75,16 @@ function simulate_devs!(model, duration::Float64=Inf)
                 export_trace_to_csv(model, model.logging)
             end
         end
-    end
-    show(Timer)
-    println()
-    reset_timer!(Timer)
+#     end
+#     show(Timer)
+#     println()
+#     reset_timer!(Timer)
 end
+const simulate_devs!(model, duration::Integer) = simulate_devs!(model, Float64(duration))
+
 
 function send_message!(agent, model, port::Symbol, msg)
-    @timeit Timer "Send Message" begin
+#     @timeit Timer "Send Message" begin
         if model.logging !== nothing
             if !haskey(model.out_log, agent.id)
                 model.out_log[agent.id] = Dict{Symbol, Vector{Any}}()
@@ -90,7 +109,7 @@ function send_message!(agent, model, port::Symbol, msg)
                 end
             end
         end
-    end
+#     end
 end
 
 function route_outputs!(model)
@@ -171,33 +190,33 @@ function transition!(model, current_time, triggering_agent_id)
     empty!(model.requeue_buffer)
 
     for id in model.imminent
-        @timeit Timer "Process Imminent" process_imminent!(
-            model[id],
-            model,
-            current_time,
-            triggering_agent_id
-        )
-#         process_imminent!(
+#         @timeit Timer "Process Imminent" process_imminent!(
 #             model[id],
 #             model,
 #             current_time,
 #             triggering_agent_id
 #         )
-    end
-
-    for id in model.active_receivers
-        @timeit Timer "Process Receivers" process_receiver!(
+        process_imminent!(
             model[id],
             model,
             current_time,
             triggering_agent_id
         )
-#         process_receiver!(
+    end
+
+    for id in model.active_receivers
+#         @timeit Timer "Process Receivers" process_receiver!(
 #             model[id],
 #             model,
 #             current_time,
 #             triggering_agent_id
 #         )
+        process_receiver!(
+            model[id],
+            model,
+            current_time,
+            triggering_agent_id
+        )
     end
 
     for id in model.imminent
@@ -211,13 +230,13 @@ function transition!(model, current_time, triggering_agent_id)
     empty!(model.imminent)
     empty!(model.active_receivers)
 
-    @timeit Timer "Requeue Events" begin
+#     @timeit Timer "Requeue Events" begin
         for req_id in model.requeue_buffer
             agent = model[req_id]
-#             Agents.add_event!(agent, model)
-            @timeit Timer "Agents.jl Add Event" Agents.add_event!(agent, model)
+            Agents.add_event!(agent, model)
+#             @timeit Timer "Agents.jl Add Event" Agents.add_event!(agent, model)
         end
-    end
+#     end
 end
 
 function devs_step!(agent, model)
@@ -229,7 +248,7 @@ function devs_step!(agent, model)
 
     target_time = agent.last_event_time + agent.next_ta
 
-    if abs(current_time - target_time) <= 1e-9
+    if abs(current_time - target_time) <= PRECISION
         if !agent.is_imminent
             agent.is_imminent = true
             push!(model.imminent, agent.id)
@@ -241,16 +260,16 @@ function devs_step!(agent, model)
     is_last_in_batch = true
     if !isempty(queue)
         next_time = first(queue)[2]
-        if abs(next_time - current_time) <= 1e-9
+        if abs(next_time - current_time) <= PRECISION
             is_last_in_batch = false
         end
     end
 
     if is_last_in_batch && !isempty(model.imminent)
-        @timeit Timer "Route Outputs" route_outputs!(model)
-        @timeit Timer "Transitions" transition!(model, current_time, agent.id)
-#         route_outputs!(model)
-#         transition!(model, current_time, agent.id)
+#         @timeit Timer "Route Outputs" route_outputs!(model)
+#         @timeit Timer "Transitions" transition!(model, current_time, agent.id)
+        route_outputs!(model)
+        transition!(model, current_time, agent.id)
     end
 end
 

+ 19 - 19
src/CoupledDEVS.jl

@@ -185,8 +185,8 @@ function build_standalone_agents(top_model::CoupledDEVS; routing_tables=Dict{Int
     path_to_id = Dict{String, Int}()
     current_id = Ref(1)
 
-#     instantiate_tree!(top_model, string(top_model.name), path_to_id, agent_list, current_id, routing_tables)
-    @timeit Builder "Instantiate Tree" instantiate_tree!(top_model, string(top_model.name), path_to_id, agent_list, current_id, routing_tables)
+    instantiate_tree!(top_model, string(top_model.name), path_to_id, agent_list, current_id, routing_tables)
+#     @timeit Builder "Instantiate Tree" instantiate_tree!(top_model, string(top_model.name), path_to_id, agent_list, current_id, routing_tables)
 
     return agent_list, path_to_id
 end
@@ -314,17 +314,17 @@ function build_routing_tables(couplings::Dict, agent_list)
 end
 
 function compile_devs(top_model::CoupledDEVS; logging::Union{Nothing, String}=nothing)
-    @timeit Builder "Compile DEVS" begin
-#         dummy_agents, path_to_id = build_standalone_agents(top_model)
-        @timeit Builder "Build Dummy Agents" dummy_agents, path_to_id = build_standalone_agents(top_model)
+#     @timeit Builder "Compile DEVS" begin
+        dummy_agents, path_to_id = build_standalone_agents(top_model)
+#         @timeit Builder "Build Dummy Agents" dummy_agents, path_to_id = build_standalone_agents(top_model)
 
         # Resolve direct couplings using raw data (NO EventQueueABM creation!)$
-#         couplings = resolve_direct_couplings(top_model, path_to_id, dummy_agents)
-#         routing_tables = build_routing_tables(couplings, dummy_agents)
-#         final_agents, final_paths = build_standalone_agents(top_model; routing_tables=routing_tables)
-        @timeit Builder "Resolve Couplings" couplings = resolve_direct_couplings(top_model, path_to_id, dummy_agents)
-        @timeit Builder "Build Routing Tables" routing_tables = build_routing_tables(couplings, dummy_agents)
-        @timeit Builder "Build Final Agents" final_agents, final_paths = build_standalone_agents(top_model; routing_tables=routing_tables)
+        couplings = resolve_direct_couplings(top_model, path_to_id, dummy_agents)
+        routing_tables = build_routing_tables(couplings, dummy_agents)
+        final_agents, final_paths = build_standalone_agents(top_model; routing_tables=routing_tables)
+#         @timeit Builder "Resolve Couplings" couplings = resolve_direct_couplings(top_model, path_to_id, dummy_agents)
+#         @timeit Builder "Build Routing Tables" routing_tables = build_routing_tables(couplings, dummy_agents)
+#         @timeit Builder "Build Final Agents" final_agents, final_paths = build_standalone_agents(top_model; routing_tables=routing_tables)
 
         unique_types = unique(typeof(a) for a in final_agents)
         AgentUnion = Union{unique_types...}
@@ -334,17 +334,17 @@ function compile_devs(top_model::CoupledDEVS; logging::Union{Nothing, String}=no
             logging = logging,
             path_to_id = final_paths
         )
-#         final_model = EventQueueABM(AgentUnion, (UniversalEvent, ); properties = final_props, autogenerate_after_action = false)
-        @timeit Builder "Build Model" final_model = EventQueueABM(AgentUnion, (UniversalEvent, ); properties = final_props)
+        final_model = EventQueueABM(AgentUnion, (UniversalEvent, ); properties = final_props, autogenerate_after_action = false)
+#         @timeit Builder "Build Model" final_model = EventQueueABM(AgentUnion, (UniversalEvent, ); properties = final_props)
 
         typed_agents = Vector{AgentUnion}(final_agents)
         for agent in typed_agents
-#             add_agent!(agent, final_model)
-            @timeit Builder "Agents.jl Add Agent" add_agent!(agent, final_model)
+            add_agent!(agent, final_model)
+#             @timeit Builder "Agents.jl Add Agent" add_agent!(agent, final_model)
         end
-    end
-    show(Builder)
-    println()
-    reset_timer!(Builder)
+#     end
+#     show(Builder)
+#     println()
+#     reset_timer!(Builder)
     return final_model
 end

+ 4 - 5
src/DEVS2Agents.jl

@@ -9,12 +9,11 @@ module DEVS2Agents
 
     export CouplingError, MissingPortError, IllegalCouplingError
 
-    export AtomicDEVS, UniversalEvent, add_in_port!, add_out_port!, simulate_devs!
-    export internal_transition!, external_transition!, confluent_transition!, output!, send_message!, time_advance
+    export AtomicDEVS, UniversalEvent, simulate_devs!
+    export internal_transition!, int_transition!, external_transition!, ext_transition!, confluent_transition!, conf_transition!
+    export output!, send_message!, time_advance, ta
     export format_state, format_message, INTERNAL, EXTERNAL, CONFLUENT
 
-    export CoupledDEVS, add_submodel!, connect!, compile_devs, ModelRef, PortRef, self
+    export CoupledDEVS, add_submodel!, add_in_port!, add_out_port!, connect!, compile_devs, ModelRef, PortRef, self
     export print_trace, print_initial_conditions, export_trace_to_csv
-
-    export DelayedAtomic, State, Message, Passive, Active
 end