JaviCru
JaviCru

Reputation: 51

How to implement 'time' to call functions in Matlab?

I'm trying to simulate in Matlab the traffic of a network of 14 nodes and 21 links. I have a function called "New_Connection" and another called " Close_connection " among others.

For now I have implemented traffic using a 'for' loop. In each iteration is called "New_Connection" that randomly chooses a source node , a destination node and a random duration (now this value is an integer) is executed. This connection may or may not be set (lock).

After, is called the "Close_connection" function, which checks all connection times (stored in an array) and if they have a value of 0 closes the connection.

Finally, before the end of the loop, is subtracted a temporary unit to all connections established least the last one.

What I would like is to perform this simulation using a system that implements a time (eg 1 minute) and at any time, any node establishes a new connection. For example:

t=0.000134 s ---- Node1 to Node8
t=0.003024 s ---- Node12 to Node11
t=0.003799 s ---- Node6 to Node3
.
.
.
t=59.341432 s ---- Node1 to Node4

And the "Close_Connection" function considers these time to close connections.

I have searched for information on Simulink, SimEvents, Parallel computing, Discrete event simulation... but I can not really understand the functioning.

Thank you very much in advance and apologies for my English.

Upvotes: 1

Views: 218

Answers (1)

Daniel
Daniel

Reputation: 36710

You don't need to to use a complex framework like SIMEVENTS. For simple tasks you can write your own event queue. The following code implements a simple scenario. Create new Connections ever T=Uniform(0,10) seconds, delete the connection after 10s

%max duration
SIMTIME=60;
T=0;
NODES=[1:20];
%Constructor for new events. 'Command' is a string, 'data' gives the parameters
MAKEEVENT=@(t,c,d)(struct('time',t,'command',c,'data',{d}));
%create event to end simulation
QUEUE(1)=MAKEEVENT(SIMTIME,'ENDSIM',[]);
%create initial event to create the first connection
QUEUE(end+1)=MAKEEVENT(0,'PRODUCECONNECTION',[]);
RUN=true;
while RUN
    [nT,cevent]=min([QUEUE.time]);
    assert(nT>=T,'event was created for the past')
    T=nT;
    EVENT=QUEUE(cevent);
    QUEUE(cevent)=[];
    fprintf('T=%f\n',T)
    switch (EVENT.command)
        case 'ENDSIM'
            %maybe collect data here
            RUN=false;
        case 'PRODUCECONNECTION'
            %standard producer pattern
            %Create a connection between two random nodes every 10s
            next=rand*10;
            QUEUE(end+1)=MAKEEVENT(T+next,'PRODUCECONNECTION',[]);
            R=randperm(size(NODES,2));
            first=NODES(R(1));
            second=NODES(R(2));
            fprintf('CONNECT NODE %d and %d\n',first,second)
            %connection will last for 20s
            QUEUE(end+1)=MAKEEVENT(T+next,'RELEASECONNECTION',{first,second});
        case 'RELEASECONNECTION'
            first=EVENT.data{1};
            second=EVENT.data{2};
            fprintf('DISCONNNECT NODE %d and %d\n',first,second)
    end
end

Upvotes: 2

Related Questions