|
| 1 | +%--------------------------------------------------------------------------------------------------- |
| 2 | +% Copyright (c) Institute of Control Systems, Hamburg University of Technology. All rights reserved. |
| 3 | +% Licensed under the GPLv3. See LICENSE in the project root for license information. |
| 4 | +% Author(s): Christian Hespe |
| 5 | +%--------------------------------------------------------------------------------------------------- |
| 6 | + |
| 7 | +classdef DelayNetwork < MatlabNetwork |
| 8 | + %DELAYNETWORK Network implementation that simulates a constant and uniform delay during |
| 9 | + %transmission |
| 10 | + % This implementation of BaseNetwork does not consider any stochastic |
| 11 | + % effects during transmission, but is subject to a constant and |
| 12 | + % uniform delay for all informaton transmitted through the net |
| 13 | + |
| 14 | + properties(GetAccess = public, SetAccess = immutable) |
| 15 | + range % Range of the communication |
| 16 | + delay % Transmission delay |
| 17 | + end |
| 18 | + |
| 19 | + properties(GetAccess = private, SetAccess = private) |
| 20 | + buffer % Delay buffer |
| 21 | + idx % buffer index |
| 22 | + end |
| 23 | + |
| 24 | + methods |
| 25 | + function obj = DelayNetwork(agentCount, cycleTime, dim, range, delay) |
| 26 | + %DELAYNETWORK Construct an instance of this class |
| 27 | + % The network needs several parameter to be correctly |
| 28 | + % initialized. |
| 29 | + % |
| 30 | + % agentCount Number of agents in the network |
| 31 | + % dim Dimension of the underlying space |
| 32 | + % range Communication range |
| 33 | + % delay Communication delay in steps |
| 34 | + |
| 35 | + % Initialize MatlabNetwork properties |
| 36 | + obj@MatlabNetwork(agentCount, cycleTime, dim); |
| 37 | + |
| 38 | + obj.range = range; |
| 39 | + obj.delay = delay; |
| 40 | + |
| 41 | + obj.idx = 1; |
| 42 | + obj.buffer = cell(agentCount, delay+1); |
| 43 | + end |
| 44 | + |
| 45 | + function recvMessages = process(obj) |
| 46 | + %PROCESS Processes all messages that were send by the agents |
| 47 | + %since the last call. |
| 48 | + % The messages get broadcasted to all agents in the receiving |
| 49 | + % range. |
| 50 | + |
| 51 | + sentMessages = obj.sentMessages.takeAll(); |
| 52 | + |
| 53 | + % Initialize every message as received by everyone |
| 54 | + filter = ones([length(sentMessages), obj.agentCount], 'logical'); |
| 55 | + |
| 56 | + % Compute the recipients of each message |
| 57 | + for i = 1:length(sentMessages) |
| 58 | + pos_sender = obj.positions(:, sentMessages(i).sender); |
| 59 | + |
| 60 | + % Exclude the possibility of agents sending to themselves |
| 61 | + filter(i, sentMessages(i).sender) = false; |
| 62 | + |
| 63 | + % Calculate the distance from the sender to all agents |
| 64 | + dist = vecnorm(pos_sender - obj.positions); |
| 65 | + |
| 66 | + % Remove receivers that are outside the transmission range |
| 67 | + filter(i,:) = filter(i,:) & (dist <= obj.range); |
| 68 | + end |
| 69 | + |
| 70 | + % Copy all received messages in the delay buffers |
| 71 | + for i = 1:obj.agentCount |
| 72 | + obj.buffer{i,obj.idx} = sentMessages(filter(:,i)); |
| 73 | + end |
| 74 | + |
| 75 | + % Prepare receive buffer and advance the index |
| 76 | + recvMessages = obj.buffer(:, obj.mask(obj.idx-1)); |
| 77 | + obj.idx = obj.mask(obj.idx+1); |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + methods(Access = private) |
| 82 | + function idx = mask(obj, idx) |
| 83 | + idx = mod(idx-1, obj.delay+1) + 1; |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments