-
Notifications
You must be signed in to change notification settings - Fork 2
/
BaseAgent.m
124 lines (104 loc) · 4.89 KB
/
BaseAgent.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
%---------------------------------------------------------------------------------------------------
% Copyright (c) Institute of Control Systems, Hamburg University of Technology. All rights reserved.
% Licensed under the GPLv3. See LICENSE in the project root for license information.
% Author(s): Christian Hespe
%---------------------------------------------------------------------------------------------------
classdef(Abstract) BaseAgent < handle & matlab.mixin.Heterogeneous
%BASEAGENT Defines the basic properties of an agent in the simulation
%framework.
% All specific agent classes must inherit from this abstract class,
% as it defines the basis properties, that are required during
% simulation.
properties(GetAccess = public, SetAccess = immutable)
id % Number that uniquely identifies the agent in the network
dT % Time between to calls to the step function
end
properties(GetAccess = private, SetAccess = private)
dataToSend % Saves the data the agent wants to transmitt until the networks fetches it
messages % Buffer for received messages of this agent
end
properties(Dependent, GetAccess = public, SetAccess = immutable)
order % Dynamic order, dimension of the state space of the agent
end
properties(Abstract, Dependent, GetAccess = public, SetAccess = private)
% These dependent properties are only there for convenience. Because
% the position and velocity can be encoded in arbitrary states, this is
% used to easy working with the agents without increasing the storage
% cost.
position % Current position of the agent
velocity % Current velocity of the agent
% The state is also saved as a dependent property. In this way, the
% agent implementation can choose how to implement the dynamics.
% Either, the predefined dynamics objects can be used, which
% contain a state themselves, or it can choose to declare a local
% variable and manually implement the dynamics
state % Dynamic state of the agent
end
methods
function obj = BaseAgent(id, dT)
%BASEAGENT Construct an instance of this class
% network is a reference to the network object.
% dT is the time between two simulation steps
obj.id = id;
obj.dT = dT;
end
function value = get.order(obj)
%GET.ORDER Implementation of the dependent property order
value = size(obj.state, 1);
end
function msgs = receive(obj, ~, msgs)
%RECEIVE Function that agent implementation can call to receive
%messages from other agents in the network.
% This default implementation is meant to be used if the
% agent only processes messages in its own step function. If
% processing between steps is required, you must override
% this function.
if isempty(obj.messages)
obj.messages = MessageBuffer();
end
if nargin > 1 && nargout == 0
obj.messages.put(msgs);
elseif nargin <= 1 && nargout > 0
msgs = obj.messages.takeAll();
else
error('You must have either inputs or an output!')
end
end
end
methods(Abstract)
%STEP Function that gets called at each simulation step
% All agent implementations should override this method to
% implement agent specific behaviour, such as the dynamics
% and the network interactions.
step(obj)
end
methods(Access = protected)
function send(obj, data)
%SEND Function that the agent implementations can call to send
%data over the network
% The data is not send immediately, but stored until the
% network requests it.
obj.dataToSend = data;
end
end
methods(Access = ?BaseNetwork)
function message = getMessage(obj)
%GETMESSAGE Function that returns the message that this agent
%wants to send.
% If no data is to be sent, the reponse is empty.
if isempty(obj.dataToSend)
message = [];
else
message = Message(obj.id, obj.dataToSend);
obj.dataToSend = [];
end
end
end
methods (Static, Sealed, Access = protected)
function default_object = getDefaultScalarElement()
%GETDEFAULTSCALARELEMENT Provides a default element for Matlab
%to place in newly created arrays of BaseAgents.
default_object = StationaryAgent;
end
end
end