-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateSpaceModel.java
215 lines (183 loc) · 9.58 KB
/
StateSpaceModel.java
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Below is the copyright agreement for the Ptolemy II system.
Copyright (c) 2014-2015 The Regents of the University of California.
All rights reserved.
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the above
copyright notice and the following two paragraphs appear in all copies
of this software.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.
Ptolemy II includes the work of others, to see those copyrights, follow
the copyright link on the splash page or see copyright.htm.
*/
package org.ptolemy.ssm;
import java.util.ArrayList;
import java.util.List;
import org.ptolemy.ssm.MirrorDecoratorListener.DecoratorEvent;
import ptolemy.actor.gui.ColorAttribute;
import ptolemy.data.ArrayToken;
import ptolemy.data.StringToken;
import ptolemy.data.expr.Parameter;
import ptolemy.data.type.ArrayType;
import ptolemy.data.type.BaseType;
import ptolemy.kernel.CompositeEntity;
import ptolemy.kernel.util.Attribute;
import ptolemy.kernel.util.IllegalActionException;
import ptolemy.kernel.util.InternalErrorException;
import ptolemy.kernel.util.NameDuplicationException;
import ptolemy.kernel.util.Settable;
import ptolemy.kernel.util.Workspace;
public class StateSpaceModel extends MirrorDecorator {
/** Construct a StateSpaceModel with a name and a container.
* The container argument must not be null, or a
* NullPointerException will be thrown. This actor will use the
* workspace of the container for synchronization and version counts.
* If the name argument is null, then the name is set to the empty string.
* Increment the version of the workspace.
* @param container The container.
* @param name The name of this actor.
* @exception IllegalActionException If the container is incompatible
* with this actor.
* @exception NameDuplicationException If the name coincides with
* an actor already in the container.
*/
public StateSpaceModel(CompositeEntity container, String name)
throws IllegalActionException, NameDuplicationException {
super(container, name);
_init();
}
@Override
public Object clone(Workspace workspace) throws CloneNotSupportedException {
StateSpaceModel newObject = (StateSpaceModel) super
.clone(workspace);
newObject._cachedStateVariableNames = new ArrayList<>();
return newObject;
}
@Override
public void attributeChanged(Attribute attribute)
throws IllegalActionException {
if (attribute == stateVariableNames) {
sendParameterEvent(DecoratorEvent.CHANGED_PARAMETER, stateVariableNames);
// create a hidden parameter that corresponds to the specified state variable, if not already present
ArrayToken names = (ArrayToken) stateVariableNames.getToken();
String stateName = ((StringToken) names.getElement(0))
.stringValue();
List<String> temp = new ArrayList<>();
if (stateName.length() > 0) {
// Set the output type according to the state variables
try {
// create missing parameters for the newly added state variables.
for (int i = 0; i < names.length(); i++) {
stateName = ((StringToken) names.getElement(i))
.stringValue();
temp.add(stateName);
// check if this state name already existed before
if (!_cachedStateVariableNames.contains(stateName)) {
Parameter y = (Parameter) this.getAttribute(stateName);
if ( y == null
&& stateName.length() != 0) {
y = new Parameter(this, stateName);
y.setExpression("0.0");
sendParameterEvent(DecoratorEvent.ADDED_PARAMETER, y);
}
// FindBugs: Possible null pointer dereference of y.
if (y != null) {
y.setVisibility(Settable.NONE);
}
if (this.getAttribute(stateName+"_update") == null) {
Parameter yUpdate = new Parameter(this, stateName+"_update");
yUpdate.setExpression(stateName);
sendParameterEvent(DecoratorEvent.ADDED_PARAMETER, yUpdate);
}
_cachedStateVariableNames.add(stateName);
}
}
// remove parameters corresponding to obsolete state variables.
for (String old : _cachedStateVariableNames) {
if (! temp.contains(old)) {
Parameter yUpdate = (Parameter) this.getAttribute(old+"_update");
sendParameterEvent(DecoratorEvent.REMOVED_PARAMETER,yUpdate);
if (yUpdate != null) {
yUpdate.setContainer(null);
}
Parameter y = (Parameter) this.getAttribute(old);
sendParameterEvent(DecoratorEvent.REMOVED_PARAMETER,y);
if (y != null) {
y.setContainer(null);
}
_cachedStateVariableNames.remove(old);
}
}
} catch (NameDuplicationException e) {
// should not happen
throw new InternalErrorException("Duplicate field in " + this.getName());
}
}
} else {
// FIXME: If the attribute is changed in the SSM, this needs to be propagated to the
// container StateSpaceActor b/c we likely would like to change the expressions accordingly
super.attributeChanged(attribute);
}
}
/** An expression for the prior distribution from which the samples are drawn.
*/
public Parameter prior;
/** The process noise. If the system contains multiple state variables, the process noise
* should be an expression that returns an ArrayToken. See multivariateGaussian for one such function.
*/
public Parameter processNoise;
/** An expression for a prior distribution from which the initial particles are sampled
*/
public Parameter priorDistribution;
/** The names of the state variables, in an array of strings.
* The default is an ArrayToken of an empty String.
*/
public Parameter stateVariableNames;
/** The value of current time. This parameter is not visible in
* the expression screen except in expert mode. Its value initially
* is just 0.0, a double, but upon each firing, it is given a
* value equal to the current time as reported by the director.
*/
public Parameter t;
/** Initialize the class. */
private void _init() throws IllegalActionException,
NameDuplicationException {
//create parameters for the initial state variable names here.
String[] names = {"x", "y"};
for (int i=0; i < names.length; i++) {
String stateName = names[i];
Parameter y = new Parameter(this, stateName);
y.setExpression("0.0");
Parameter yUpdate = new Parameter(this, stateName.concat("_update"));
yUpdate.setExpression(stateName);
}
stateVariableNames = new Parameter(this, "stateVariableNames");
stateVariableNames.setExpression("{\"x\",\"y\"}");
stateVariableNames.setTypeEquals(new ArrayType(BaseType.STRING));
prior = new Parameter(this, "prior");
prior.setExpression("{random()*200-100,random()*200-100}");
prior.setTypeEquals(new ArrayType(BaseType.DOUBLE));
processNoise = new Parameter(this, "processNoise");
processNoise.setExpression("multivariateGaussian({0.0,0.0},[1.0,0.4;0.4,1.2])");
t = new Parameter(this, "t");
t.setTypeEquals(BaseType.DOUBLE);
t.setVisibility(Settable.EXPERT);
t.setExpression("0.0");
ColorAttribute color = new ColorAttribute(this,
"decoratorHighlightColor");
color.setExpression("{1.0,0.4,0.0,1.0}");
_cachedStateVariableNames = new ArrayList<>();
}
private List<String> _cachedStateVariableNames;
}