Sfoglia il codice sorgente

Agents.jl benchmarks working

Snej69420 6 mesi fa
parent
commit
a7bf17fc5b

+ 9 - 9
benchmarks/Agents.jl/DelayedAtomic.jl

@@ -1,8 +1,8 @@
-using Agents
+import DEVS2Agents: time_advance, internal_transition!, external_transition!, confluent_transition!, output!, setup_ports!, format_state, format_message
 
-@enum State Passive=1 Active=2
+@enum State DA_Passive=1 DA_Active=2
 
-struct Message <: DEVSMessage
+struct A2A <: DEVSMessage
     activate::Int
 end
 
@@ -15,7 +15,7 @@ function julia_stones(delay::Float64)
 end
 
 @agent struct DelayedAtomic(AtomicDEVS)
-    state::State = Passive
+    state::State = DA_Passive
     int_delay::Float64 = 0.0
     ext_delay::Float64 = 0.0
     prep_time::Float64 = 0.0
@@ -27,7 +27,7 @@ end
 end
 
 format_state(agent::DelayedAtomic) = "State.$(agent.state)"
-format_message(msg::Message) = "$(msg.activate)"
+format_message(msg::A2A) = "$(msg.activate)"
 
 function setup_ports!(agent::DelayedAtomic)
     add_in_port!(agent, :i_in)
@@ -37,19 +37,19 @@ function setup_ports!(agent::DelayedAtomic)
 end
 
 function time_advance(agent::DelayedAtomic, model)
-    return agent.state == Active ? agent.prep_time : Inf
+    return agent.state == DA_Active ? agent.prep_time : Inf
 end
 
 function internal_transition!(agent::DelayedAtomic, model)
     agent.stats && (agent.int_count += 1)
     agent.int_delay > 0.0 && julia_stones(agent.int_delay)
-    agent.state = Passive
+    agent.state = DA_Passive
 end
 
 function external_transition!(agent::DelayedAtomic, model, e, bag)
     agent.stats && (agent.ext_count += 1)
     agent.ext_delay > 0.0 && julia_stones(agent.ext_delay)
-    agent.state = Active
+    agent.state = DA_Active
 end
 
 function confluent_transition!(agent::DelayedAtomic, model, bag)
@@ -61,7 +61,7 @@ function output!(agent::DelayedAtomic, model)
     if agent.has_out_port
         return (
             (
-                :o_out, (Message(0), )
+                :o_out, (A2A(0), )
             ),
         )
     end

+ 56 - 67
benchmarks/Agents.jl/Generator.jl

@@ -1,67 +1,56 @@
-module GeneratorModel
-    using Agents
-    using ..Atomic
-
-    @enum GenState Waiting=1 Active=2 Passive=3
-
-    struct Message <: DEVSMessage
-        activate::UnitRange{Int}
-    end
-
-    @agent struct Generator(AtomicDEVS)
-        num_outputs::Int = 1
-        period::Float64 = 1.0
-        cycles::Int = 1
-        current_cycle::Int = 0
-        state::GenState = Waiting
-    end
-
-    Atomic.format_state(gen::Generator) = "State.$(gen.state)_Cycle.$(gen.current_cycle)"
-    Atomic.format_message(msg::Message) = "$(msg.activate)"
-
-    function Atomic.setup_ports!(gen::Generator)
-        Atomic.add_out_port!(gen, :o_out)
-    end
-
-    function Atomic.time_advance(gen::Generator, model)
-        if gen.state == Active
-            return 0.0
-        elseif gen.state == Waiting
-            return gen.period
-        else
-            return Inf
-        end
-    end
-
-    function Atomic.internal_transition!(gen::Generator, model)
-        if gen.current_cycle >= gen.cycles
-            gen.state = Passive
-        elseif gen.state == Active
-            gen.state = Waiting
-            gen.current_cycle += 1
-        elseif gen.state == Waiting
-            gen.state = Active
-        else
-            gen.state = Passive
-        end
-    end
-
-    function Atomic.external_transition!(gen::Generator, model, e, bag)
-        return nothing
-    end
-
-    function Atomic.confluent_transition!(gen::Generator, model, bag)
-        Atomic.internal_transition!(gen, model)
-    end
-
-    function Atomic.output!(gen::Generator, model)
-        if gen.state == Active
-            return (
-                (
-                    :o_out, (Message(0:(gen.num_outputs - 1)),
-                )),
-            )
-        end
-        return nothing
-    end
-end
+import DEVS2Agents: time_advance, internal_transition!, external_transition!, confluent_transition!, output!, setup_ports!, format_state, format_message
+
+@enum GenState G_Waiting=1 G_Active=2 G_Passive=3
+
+struct Generate <: DEVSMessage
+    activate::UnitRange{Int}
+end
+
+@agent struct Generator(AtomicDEVS)
+    num_outputs::Int = 1
+    period::Float64 = 1.0
+    cycles::Int = 1
+    current_cycle::Int = 0
+    state::GenState = G_Waiting
+end
+
+format_state(gen::Generator) = "State.$(gen.state)_Cycle.$(gen.current_cycle)"
+format_message(msg::Generate) = "$(msg.activate)"
+
+function setup_ports!(gen::Generator)
+    add_out_port!(gen, :o_out)
+end
+
+function time_advance(gen::Generator, model)
+    if gen.state == G_Active
+        return 0.0
+    elseif gen.state == G_Waiting
+        return gen.period
+    else
+        return Inf
+    end
+end
+
+function internal_transition!(gen::Generator, model)
+    if gen.current_cycle >= gen.cycles
+        gen.state = G_Passive
+    elseif gen.state == G_Active
+        gen.state = G_Waiting
+        gen.current_cycle += 1
+    elseif gen.state == G_Waiting
+        gen.state = G_Active
+    else
+        gen.state = Passive
+    end
+end
+
+function output!(gen::Generator, model)
+    if gen.state == G_Active
+        return (
+            (
+                :o_out, (Generate(0:(gen.num_outputs - 1)),
+            )),
+        )
+    end
+    return nothing
+end

+ 1 - 1
benchmarks/Agents.jl/Manifest.toml

@@ -2,7 +2,7 @@
 
 julia_version = "1.12.5"
 manifest_format = "2.0"
-project_hash = "5605752f0f769648bb26fa38943a1c13045159b0"
+project_hash = "5d298123c5a26569afa491caa1b3184db6cc5195"
 
 [[deps.AbstractTrees]]
 git-tree-sha1 = "2d9c9a55f9c93e8887ad391fbae72f8ef55e1177"

+ 1 - 0
benchmarks/Agents.jl/Project.toml

@@ -1,2 +1,3 @@
 [deps]
+ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
 DEVS2Agents = "073a0784-fe80-4c04-b3c3-fb0686668e26"

+ 71 - 78
benchmarks/Agents.jl/Topologies.jl

@@ -1,101 +1,94 @@
-module DEVStoneTopologies
-    using ..Atomic
-    using ..Coupled
-    using ..BaseAtomic
 
-    export build_devstone, Structure, LI, HI, HO
+@enum Structure LI HI HO
 
-    @enum Structure LI HI HO
+function build_devstone(type::Structure, name::String, depth::Int, width::Int, int_delay::Float64, ext_delay::Float64, stats::Bool=false)
+    if depth < 1 || width < 1
+        error("Invalid depth or width")
+    end
 
-    function build_devstone(type::Structure, name::String, depth::Int, width::Int, int_delay::Float64, ext_delay::Float64, stats::Bool=false)
-        if depth < 1 || width < 1
-            error("Invalid depth or width")
-        end
+    top_model = CoupledDEVS(name)
+    has_out_port = type == HI || type == HO
+
+    add_in_port!(top_model, :i_in)
+    if has_out_port
+        add_out_port!(top_model, :o_out)
+    end
+    if type == HO
+        add_in_port!(top_model, :i_in2)
+        add_out_port!(top_model, :o_out2)
+    end
 
-        top_model = CoupledDEVS(name)
-        has_out_port = type == HI || type == HO
+    # Leaf
+    if depth == 1
+        p_time = round(rand() + 1.0, digits=3)
+        atom = add_submodel!(top_model, "Atomic_0_0", DelayedAtomic;
+            int_delay=int_delay, ext_delay=ext_delay, prep_time=p_time,
+            has_out_port, stats=stats)
 
-        add_in_port!(top_model, :i_in)
+        connect!(top_model, self.i_in, atom.i_in)
         if has_out_port
-            add_out_port!(top_model, :o_out)
-        end
-        if type == HO
-            add_in_port!(top_model, :i_in2)
-            add_out_port!(top_model, :o_out2)
+            connect!(top_model, atom.o_out, self.o_out)
         end
 
-        # Leaf
-        if depth == 1
-            p_time = round(rand() + 1.0, digits=3)
-            atom = add_submodel!(top_model, "Atomic_0_0", BaseAtomic.DelayedAtomic;
-                int_delay=int_delay, ext_delay=ext_delay, prep_time=p_time,
-                has_out_port, stats=stats)
+        return top_model
+    end
 
-            connect!(top_model, self.i_in, atom.i_in)
-            if has_out_port
-                connect!(top_model, atom.o_out, self.o_out)
-            end
+    child_name = "Coupled_$(depth - 1)"
+    child_coupled = build_devstone(type, child_name, depth - 1, width, int_delay, ext_delay, stats)
+    child = add_submodel!(top_model, child_name, child_coupled)
 
-            return top_model
-        end
+    connect!(top_model, self.i_in, child.i_in)
+    if type == HO
+        connect!(top_model, self.i_in, child.i_in2)
+    end
 
-        child_name = "Coupled_$(depth - 1)"
-        child_coupled = build_devstone(type, child_name, depth - 1, width, int_delay, ext_delay, stats)
-        child = add_submodel!(top_model, child_name, child_coupled)
+    if has_out_port
+        connect!(top_model, child.o_out, self.o_out)
+    end
 
-        connect!(top_model, self.i_in, child.i_in)
-        if type == HO
-            connect!(top_model, self.i_in, child.i_in2)
+    width_models = ModelRef[]
+    for idx in 0:(width - 2)
+        p_time = 1.0 - round(rand() * (0.5 / width) + (idx / width), digits=3)
+        atomic_name = "Atomic_$(depth - 1)_$(idx)"
+
+        atom = add_submodel!(top_model, atomic_name, DelayedAtomic;
+            int_delay=int_delay, ext_delay=ext_delay, prep_time=p_time,
+            has_out_port=has_out_port, stats=stats)
+
+        push!(width_models, atom)
+    end
+
+    if type == LI
+        for atom in width_models
+            connect!(top_model, self.i_in, atom.i_in)
         end
 
-        if has_out_port
-            connect!(top_model, child.o_out, self.o_out)
+    elseif type == HI
+        for atom in width_models
+            connect!(top_model, self.i_in, atom.i_in)
         end
 
-        width_models = ModelRef[]
-        for idx in 0:(width - 2)
-            p_time = 1.0 - round(rand() * (0.5 / width) + (idx / width), digits=3)
-            atomic_name = "Atomic_$(depth - 1)_$(idx)"
+        # Chain atomics
+        for i in 1:(length(width_models) - 1)
+            connect!(top_model, width_models[i].o_out, width_models[i + 1].i_in)
+        end
 
-            atom = add_submodel!(top_model, atomic_name, BaseAtomic.DelayedAtomic;
-                int_delay=int_delay, ext_delay=ext_delay, prep_time=p_time,
-                has_out_port=has_out_port, stats=stats)
+    elseif type == HO
+        for i in 1:(length(width_models) - 1)
+            curr_atom = width_models[i]
+            next_atom = width_models[i + 1]
 
-            push!(width_models, atom)
+            connect!(top_model, curr_atom.o_out, next_atom.i_in)
+            connect!(top_model, self.i_in2, curr_atom.i_in)
+            connect!(top_model, curr_atom.o_out, self.o_out2)
         end
 
-        if type == LI
-            for atom in width_models
-                connect!(top_model, self.i_in, atom.i_in)
-            end
-
-        elseif type == HI
-            for atom in width_models
-                connect!(top_model, self.i_in, atom.i_in)
-            end
-
-            # Chain atomics
-            for i in 1:(length(width_models) - 1)
-                connect!(top_model, width_models[i].o_out, width_models[i + 1].i_in)
-            end
-
-        elseif type == HO
-            for i in 1:(length(width_models) - 1)
-                curr_atom = width_models[i]
-                next_atom = width_models[i + 1]
-
-                connect!(top_model, curr_atom.o_out, next_atom.i_in)
-                connect!(top_model, self.i_in2, curr_atom.i_in)
-                connect!(top_model, curr_atom.o_out, self.o_out2)
-            end
-
-            if length(width_models) > 0
-                last_atom = width_models[end]
-                connect!(top_model, self.i_in2, last_atom.i_in)
-                connect!(top_model, last_atom.o_out, self.o_out2)
-            end
+        if length(width_models) > 0
+            last_atom = width_models[end]
+            connect!(top_model, self.i_in2, last_atom.i_in)
+            connect!(top_model, last_atom.o_out, self.o_out2)
         end
-
-        return top_model
     end
+
+    return top_model
 end

+ 11 - 14
benchmarks/Agents.jl/main.jl

@@ -1,18 +1,15 @@
-include("../Framework/AtomicDEVS.jl")
-include("../Framework/CoupledDEVS.jl")
+using ArgParse
+
+using Agents
+using DEVS2Agents
+
 include("DelayedAtomic.jl")
 include("Generator.jl")
 include("Topologies.jl")
 
-using Agents
-using ArgParse
-using Profile
-using ProfileView
-using .Atomic, .Coupled, .BaseAtomic, .GeneratorModel, .DEVStoneTopologies
-
 function build_environment(type::Structure, devstone_model::CoupledDEVS, gen_period::Float64, num_outputs::Int)
     env = CoupledDEVS("DEVStoneEnvironment")
-    add_submodel!(env, :generator, GeneratorModel.Generator;
+    add_submodel!(env, :generator, Generator;
         num_outputs = num_outputs, period = gen_period, cycles = 1)
     add_submodel!(env, :devstone, devstone_model)
 
@@ -29,7 +26,7 @@ function parse_commandline()
     @add_arg_table! s begin
         "--path", "-p"
             help = "Path to the results directory"
-            default = "./Results-Julia"
+            default = "../Results/Agents"
         "--filename", "-f"
             help = "Name of the output file suffix"
             default = ""
@@ -86,12 +83,12 @@ function run_single_simulation(topology_type::Structure, type_str::String, depth
     model_created_time = time()
 
     # Setup
-    AgentUnion = Union{BaseAtomic.DelayedAtomic, GeneratorModel.Generator}
-    model = compile_devs(env, AgentUnion, (Atomic.UniversalEvent, ); logging)
+    AgentUnion = Union{DelayedAtomic, Generator}
+    model = compile_devs(env, AgentUnion, (UniversalEvent, ); logging)
     engine_setup_time = time()
 
 #     simulate_devs!(warmup)
-#     model = compile_devs(env, AgentUnion, (Atomic.UniversalEvent, ); logging=false)
+#     model = compile_devs(env, AgentUnion, (UniversalEvent, ); logging=false)
 
 #     Profile.clear()
 
@@ -171,7 +168,7 @@ function main()
                             total_int = 0
                             total_ext = 0
                             for agent in allagents(model)
-                                if agent isa BaseAtomic.DelayedAtomic
+                                if agent isa DelayedAtomic
                                     total_int += agent.int_count
                                     total_ext += agent.ext_count
                                 end