y_petri 2.2.4 → 2.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +675 -0
- data/README.md +6 -3
- data/Rakefile +1 -1
- data/lib/y_petri/agent/{petri_net_related.rb → petri_net_aspect.rb} +34 -10
- data/lib/y_petri/agent/{simulation_related.rb → simulation_aspect.rb} +49 -34
- data/lib/y_petri/agent.rb +5 -5
- data/lib/y_petri/core/guarded.rb +24 -0
- data/lib/y_petri/core/timed/euler.rb +4 -8
- data/lib/y_petri/core/timed/gillespie.rb +11 -17
- data/lib/y_petri/core/timed/methods.rb +23 -0
- data/lib/y_petri/core/timed/pseudo_euler.rb +10 -13
- data/lib/y_petri/core/timed/quasi_euler.rb +9 -8
- data/lib/y_petri/core/timed/runge_kutta.rb +10 -18
- data/lib/y_petri/core/timed.rb +6 -14
- data/lib/y_petri/core/timeless/methods.rb +15 -0
- data/lib/y_petri/core/timeless/pseudo_euler.rb +4 -8
- data/lib/y_petri/core/timeless.rb +9 -4
- data/lib/y_petri/core.rb +44 -42
- data/lib/y_petri/net/data_set.rb +246 -142
- data/lib/y_petri/net/node_access.rb +282 -0
- data/lib/y_petri/net/own_state.rb +14 -4
- data/lib/y_petri/net/state/feature/assignment.rb +123 -0
- data/lib/y_petri/net/state/feature/delta.rb +55 -35
- data/lib/y_petri/net/state/feature/firing.rb +68 -25
- data/lib/y_petri/net/state/feature/flux.rb +9 -2
- data/lib/y_petri/net/state/feature/gradient.rb +36 -19
- data/lib/y_petri/net/state/feature/marking.rb +10 -5
- data/lib/y_petri/net/state/feature.rb +105 -11
- data/lib/y_petri/net/state/features/record.rb +144 -99
- data/lib/y_petri/net/state/features.rb +327 -200
- data/lib/y_petri/net/state.rb +48 -82
- data/lib/y_petri/net/visualization.rb +1 -1
- data/lib/y_petri/net.rb +62 -47
- data/lib/y_petri/place/arcs.rb +44 -0
- data/lib/y_petri/place/features.rb +115 -0
- data/lib/y_petri/place.rb +62 -29
- data/lib/y_petri/simulation/dependency.rb +31 -67
- data/lib/y_petri/simulation/feature_set.rb +1 -1
- data/lib/y_petri/simulation/initial_marking/access.rb +42 -26
- data/lib/y_petri/simulation/marking_clamps/access.rb +22 -17
- data/lib/y_petri/simulation/marking_clamps.rb +0 -2
- data/lib/y_petri/simulation/marking_vector/access.rb +102 -40
- data/lib/y_petri/simulation/marking_vector.rb +35 -37
- data/lib/y_petri/simulation/matrix.rb +1 -1
- data/lib/y_petri/simulation/node_representation.rb +25 -0
- data/lib/y_petri/simulation/nodes/access.rb +78 -0
- data/lib/y_petri/simulation/{elements.rb → nodes.rb} +14 -13
- data/lib/y_petri/simulation/place_mapping.rb +2 -2
- data/lib/y_petri/simulation/place_representation.rb +8 -7
- data/lib/y_petri/simulation/places/access.rb +89 -70
- data/lib/y_petri/simulation/places/free.rb +1 -1
- data/lib/y_petri/simulation/places/types.rb +20 -22
- data/lib/y_petri/simulation/places.rb +23 -18
- data/lib/y_petri/simulation/recorder.rb +23 -18
- data/lib/y_petri/simulation/timed/recorder.rb +19 -11
- data/lib/y_petri/simulation/timed.rb +93 -29
- data/lib/y_petri/simulation/timeless/recorder.rb +11 -6
- data/lib/y_petri/simulation/timeless.rb +13 -3
- data/lib/y_petri/simulation/transition_representation/A.rb +24 -4
- data/lib/y_petri/simulation/transition_representation/S.rb +11 -1
- data/lib/y_petri/simulation/transition_representation/T.rb +1 -1
- data/lib/y_petri/simulation/transition_representation/Ts.rb +1 -1
- data/lib/y_petri/simulation/transition_representation/a.rb +1 -1
- data/lib/y_petri/simulation/transition_representation/s.rb +12 -1
- data/lib/y_petri/simulation/transition_representation/t.rb +1 -1
- data/lib/y_petri/simulation/transition_representation/tS.rb +1 -1
- data/lib/y_petri/simulation/transition_representation/ts.rb +1 -1
- data/lib/y_petri/simulation/transition_representation/types.rb +1 -1
- data/lib/y_petri/simulation/transition_representation.rb +4 -11
- data/lib/y_petri/simulation/transitions/A.rb +17 -2
- data/lib/y_petri/simulation/transitions/S.rb +1 -1
- data/lib/y_petri/simulation/transitions/T.rb +1 -1
- data/lib/y_petri/simulation/transitions/Ts.rb +6 -5
- data/lib/y_petri/simulation/transitions/a.rb +1 -1
- data/lib/y_petri/simulation/transitions/access.rb +195 -168
- data/lib/y_petri/simulation/transitions/s.rb +1 -1
- data/lib/y_petri/simulation/transitions/t.rb +1 -1
- data/lib/y_petri/simulation/transitions/tS.rb +1 -1
- data/lib/y_petri/simulation/transitions/ts.rb +1 -1
- data/lib/y_petri/simulation/transitions/types.rb +1 -1
- data/lib/y_petri/simulation/transitions.rb +5 -7
- data/lib/y_petri/simulation.rb +84 -90
- data/lib/y_petri/transition/A.rb +8 -2
- data/lib/y_petri/transition/T.rb +25 -2
- data/lib/y_petri/transition/arcs.rb +19 -3
- data/lib/y_petri/transition/construction_convenience.rb +11 -10
- data/lib/y_petri/transition/t.rb +14 -1
- data/lib/y_petri/transition/types.rb +6 -1
- data/lib/y_petri/transition.rb +9 -12
- data/lib/y_petri/version.rb +1 -1
- data/lib/y_petri/world/dependency.rb +3 -3
- data/lib/y_petri/world/{petri_net_related.rb → petri_net_aspect.rb} +4 -4
- data/lib/y_petri/world/simulation_aspect.rb +352 -0
- data/lib/y_petri/world.rb +4 -4
- data/lib/y_petri.rb +1 -1
- data/test/agent_test.rb +2 -1
- data/test/examples/demonstrator.rb +4 -1
- data/test/examples/demonstrator_2.rb +5 -0
- data/test/examples/demonstrator_4.rb +6 -5
- data/test/examples/example_2.rb +2 -0
- data/test/examples/manual_examples.rb +4 -4
- data/test/net_test.rb +457 -54
- data/test/place_test.rb +11 -7
- data/test/simulation_test.rb +358 -331
- data/test/transition_test.rb +11 -10
- data/test/world_test.rb +2 -0
- data/test/y_petri_test.rb +2 -1
- data/y_petri.gemspec +24 -18
- metadata +71 -17
- data/LICENSE +0 -22
- data/lib/y_petri/net/element_access.rb +0 -239
- data/lib/y_petri/simulation/element_representation.rb +0 -20
- data/lib/y_petri/simulation/elements/access.rb +0 -57
- data/lib/y_petri/transition/type.rb +0 -103
- data/lib/y_petri/transition/type_information.rb +0 -103
- data/lib/y_petri/world/simulation_related.rb +0 -176
data/test/simulation_test.rb
CHANGED
@@ -1,303 +1,325 @@
|
|
1
1
|
#! /usr/bin/ruby
|
2
2
|
# encoding: utf-8
|
3
3
|
|
4
|
+
gem 'minitest'
|
4
5
|
require 'minitest/autorun'
|
5
6
|
require_relative '../lib/y_petri' # tested component itself
|
6
7
|
# require 'y_petri'
|
7
8
|
# require 'sy'
|
8
9
|
|
9
|
-
describe YPetri::Simulation do
|
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
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
10
|
+
# describe YPetri::Simulation do
|
11
|
+
# before do
|
12
|
+
# @w = YPetri::World.new
|
13
|
+
# end
|
14
|
+
|
15
|
+
# it "should allow for creation of an empty simulation" do
|
16
|
+
# net = @w.Net.send :new
|
17
|
+
# sim = net.simulation
|
18
|
+
# skip
|
19
|
+
# sim.pp.must_equal []
|
20
|
+
# sim.pp( *[] ).must_equal []
|
21
|
+
# sim.tt.must_equal( [] )
|
22
|
+
# sim.tt( *[] ).must_equal []
|
23
|
+
# end
|
24
|
+
|
25
|
+
# describe "simulation setup" do
|
26
|
+
# before do
|
27
|
+
# @p = @w.Place.send :new, name: :A, default_marking: 1
|
28
|
+
# @q = @w.Place.send :new, name: :B, default_marking: 2
|
29
|
+
# @net = @w.Net.of [ @p, @q ]
|
30
|
+
# end
|
31
|
+
|
32
|
+
# it "should allow to set up a simplistic simulation instance" do
|
33
|
+
# @net.simulation
|
34
|
+
# @net.simulation marking_clamps: { @q => 42 } # one clamp
|
35
|
+
# @net.simulation initial_marking: { @p => 42, @q => 43 }
|
36
|
+
# @net.simulation marking_clamps: { @p => 42 }, initial_marking: { @q => 43 }
|
37
|
+
# @net.simulation initial_marking: { A: 42 }
|
38
|
+
# end
|
39
|
+
|
40
|
+
# it "should fail with malformed arguments" do
|
41
|
+
# -> { @net.simulation use_default_marking: false }.must_raise TypeError
|
42
|
+
# -> { @net.simulation initial_marking: { Foo: 1 } }.must_raise NameError
|
43
|
+
# end
|
44
|
+
|
45
|
+
# describe "place representation aspects" do
|
46
|
+
# before do
|
47
|
+
# @s = YPetri::Simulation.new( net: @net,
|
48
|
+
# initial_marking: { A: 42 },
|
49
|
+
# marking_clamps: { B: 43 } )
|
50
|
+
# end
|
51
|
+
|
52
|
+
# it "should have elements/access" do
|
53
|
+
# @s.send( :place, :A )
|
54
|
+
# .must_be_kind_of YPetri::Simulation::PlaceRepresentation
|
55
|
+
# @s.send( :place, :B )
|
56
|
+
# .must_be_kind_of YPetri::Simulation::PlaceRepresentation
|
57
|
+
# @s.net.places.names.must_equal [:A, :B]
|
58
|
+
# @s.pn.must_equal [:A, :B]
|
59
|
+
# @s.send( :places ).free.size.must_equal 1
|
60
|
+
# @s.send( :places ).first.quantum.must_equal 1
|
61
|
+
# @s.send( :free_places ).names.must_equal [:A]
|
62
|
+
# @s.send( :places ).clamped.size.must_equal 1
|
63
|
+
# @s.send( :clamped_places ).names.must_equal [:B]
|
64
|
+
# @s.send( :Places, [:A] ).map( &:source ).must_equal [@p]
|
65
|
+
# @s.send( :places, :A ).map( &:source ).must_equal [@p]
|
66
|
+
# @s.send( :Transitions, [] ).must_equal []
|
67
|
+
# @s.send( :Places, [] ).must_equal []
|
68
|
+
# end
|
69
|
+
|
70
|
+
# describe "marking vector representation" do
|
71
|
+
# it "should work" do
|
72
|
+
# @s.instance_variable_get( :@m_vector ).must_equal @s.m_vector
|
73
|
+
# @s.m_vector.must_be_kind_of YPetri::Simulation::MarkingVector
|
74
|
+
# @s.m_vector.size.must_equal 2
|
75
|
+
# @s.m_vector.to_a.must_equal [42, 43]
|
76
|
+
# @s.m.must_equal [42, 43]
|
77
|
+
# @s.marking.must_equal [42]
|
78
|
+
# @s.marking_clamps.keys_to_names.must_equal( { B: 43 } )
|
79
|
+
# end
|
80
|
+
# end
|
81
|
+
# end # describe simulation step
|
82
|
+
|
83
|
+
# describe "transition representation aspects" do
|
84
|
+
# before do
|
85
|
+
# @ts = @w.Transition.send :new, name: "T_ts", codomain: :A, action: -> { 1 }
|
86
|
+
# @tS = @w.Transition.send :new, name: "T_tS", s: { B: -1, A: 1 }, action: proc { 1 }
|
87
|
+
# @Ts = @w.Transition.send :new, name: "T_Ts", codomain: :A, rate: -> { 1 }
|
88
|
+
# @TS = @w.Transition.send :new, name: "T_TS", s: { B: -1, A: 1 }, rate: proc { 1 }
|
89
|
+
# end
|
90
|
+
|
91
|
+
# it "should be what intended" do
|
92
|
+
# @ts.type.must_equal :ts
|
93
|
+
# @ts.domain.must_equal []
|
94
|
+
# @ts.codomain.must_equal [@p]
|
95
|
+
# @tS.type.must_equal :tS
|
96
|
+
# @tS.domain.must_equal [@q] # inferred
|
97
|
+
# @tS.codomain.must_equal [@q, @p]
|
98
|
+
# @Ts.type.must_equal :Ts
|
99
|
+
# @Ts.domain.must_equal []
|
100
|
+
# @Ts.codomain.must_equal [@p]
|
101
|
+
# @TS.type.must_equal :TS
|
102
|
+
# @TS.domain.must_equal [@q] # inferred
|
103
|
+
# @TS.codomain.must_equal [@q, @p]
|
104
|
+
# end
|
105
|
+
|
106
|
+
# describe "ts transition" do
|
107
|
+
# before do
|
108
|
+
# @net = @w.Net.of [ @p, @q, @ts ]
|
109
|
+
# end
|
110
|
+
|
111
|
+
# describe "no clamps" do
|
112
|
+
# before do
|
113
|
+
# @sim = @net.simulation net: @net
|
114
|
+
# end
|
115
|
+
|
116
|
+
# it "should behave" do
|
117
|
+
# @sim.tt.size.must_equal 1
|
118
|
+
# @ts.codomain.names.must_equal [:A]
|
119
|
+
# @sim.ts_tt.first.codomain.names.must_equal [:A]
|
120
|
+
# @ts.domain.names.must_equal []
|
121
|
+
# @sim.ts_tt.first.domain.names.must_equal []
|
122
|
+
# @sim.timed?.must_equal false
|
123
|
+
# @sim.m.must_equal [1, 2]
|
124
|
+
# @sim.p_m.must_equal( { A: 1, B: 2 } )
|
125
|
+
# @sim.recording.must_equal( { 0 => [1, 2]} )
|
126
|
+
# @sim.simulation_method.must_equal :pseudo_euler
|
127
|
+
# @sim.core.must_be_kind_of YPetri::Core
|
128
|
+
# @sim.ts_tt.first.domain.must_equal []
|
129
|
+
# @sim.send( :ts_transitions ).first.domain_access_code.must_equal ''
|
130
|
+
# λ = @sim.send( :transitions ).ts.first.delta_closure
|
131
|
+
# λ.arity.must_equal 0
|
132
|
+
# λ.call.must_equal [1]
|
133
|
+
# cc = @sim.send( :transitions ).ts.delta_closures
|
134
|
+
# cc.map( &:call ).map( &:first ).must_equal [1]
|
135
|
+
# cl = @sim.send( :transitions ).ts.delta_closure
|
136
|
+
# cl.call.must_equal Matrix[ [1], [0] ]
|
137
|
+
# @sim.step!
|
138
|
+
# @sim.p_m.must_equal( { A: 2, B: 2 } ) # marking of A goes up by 1
|
139
|
+
# @sim.recording.must_equal( { 0 => [1, 2], 1 => [2, 2] } )
|
140
|
+
# end
|
141
|
+
# end
|
142
|
+
|
143
|
+
# describe "with clamps" do
|
144
|
+
# before do
|
145
|
+
# @sim = @net.simulation marking_clamps: { B: 42 }
|
146
|
+
# end
|
147
|
+
|
148
|
+
# it "should behave" do
|
149
|
+
# @sim.recording.must_equal( { 0 => [1] } )
|
150
|
+
# @sim.step!
|
151
|
+
# @sim.recording.must_equal( { 0 => [1], 1 => [2] } )
|
152
|
+
# end
|
153
|
+
# end
|
154
|
+
# end # ts transition
|
155
|
+
|
156
|
+
# describe "tS transition" do
|
157
|
+
# before do
|
158
|
+
# @net = @w.Net.of [ @p, @q, @tS ]
|
159
|
+
# end
|
160
|
+
|
161
|
+
# describe "no clamps" do
|
162
|
+
# before do
|
163
|
+
# @sim = @net.simulation net: @net
|
164
|
+
# end
|
165
|
+
|
166
|
+
# it "should behave" do
|
167
|
+
# @sim.recording.must_equal( { 0 => [1, 2] } )
|
168
|
+
# @sim.step!
|
169
|
+
# @sim.recording.must_equal( { 0 => [1, 2], 1 => [2, 1] } )
|
170
|
+
# end
|
171
|
+
# end
|
172
|
+
|
173
|
+
# describe "with clamps" do
|
174
|
+
# before do
|
175
|
+
# @sim = @net.simulation marking_clamps: { B: 43 }
|
176
|
+
# end
|
177
|
+
|
178
|
+
# it "should behave" do
|
179
|
+
# @sim.recording.must_equal( { 0 => [1] } )
|
180
|
+
# 3.times do @sim.step! end
|
181
|
+
# @sim.recording.must_equal( { 0 => [1], 1 => [2], 2 => [3], 3 => [4] } )
|
182
|
+
# end
|
183
|
+
# end
|
184
|
+
# end # tS transition
|
185
|
+
|
186
|
+
# describe "Ts transition" do
|
187
|
+
# before do
|
188
|
+
# @net = @w.Net.of [ @p, @q, @Ts ]
|
189
|
+
# end
|
190
|
+
|
191
|
+
# describe "no clamps" do
|
192
|
+
# before do
|
193
|
+
# @sim = @net.simulation sampling: 1
|
194
|
+
# end
|
195
|
+
|
196
|
+
# it "should behave" do
|
197
|
+
# @sim.timed?.must_equal true
|
198
|
+
# @sim.simulation_method.must_equal :pseudo_euler
|
199
|
+
# @sim.Ts_tt.size.must_equal 1
|
200
|
+
# @sim.send( :transitions ).Ts.first.gradient_closure.call.must_equal [1]
|
201
|
+
# @sim.Ts_tt.first.codomain.names.must_equal [:A]
|
202
|
+
# @sim.recording.must_equal( { 0.0 => [1, 2] } )
|
203
|
+
# @sim.step! 1
|
204
|
+
# @sim.recording.must_equal( { 0.0 => [1, 2], 1.0 => [2, 2] } )
|
205
|
+
# end
|
206
|
+
# end
|
207
|
+
|
208
|
+
# describe "with clamps" do
|
209
|
+
# before do
|
210
|
+
# @sim = @net.simulation sampling: 1, marking_clamps: { B: 43 }
|
211
|
+
# end
|
212
|
+
|
213
|
+
# it "should behave" do
|
214
|
+
# @sim.send( :transitions ).Ts.first.codomain.names.must_equal [:A]
|
215
|
+
# @sim.simulation_method.must_equal :pseudo_euler
|
216
|
+
# @sim.timed?.must_equal true
|
217
|
+
# @sim.core.timed?.must_equal true
|
218
|
+
# @sim.reset!
|
219
|
+
# @sim.time.must_equal 0
|
220
|
+
# @sim.p_m.must_equal( { A: 1, B: 43 } )
|
221
|
+
# @sim.recording.must_equal( { 0.0 => [1] } )
|
222
|
+
# @sim.send( :transitions ).Ts.first.gradient_closure.call.must_equal [1]
|
223
|
+
# @sim.step! 1
|
224
|
+
# @sim.time.must_equal 1
|
225
|
+
# @sim.p_m.must_equal( { A: 2, B: 43 } )
|
226
|
+
# @sim.recording.to_h.must_equal( { 0.0 => [1], 1.0 => [2] } )
|
227
|
+
# @sim.send( :transitions ).Ts.first.gradient_closure.call.must_equal [1]
|
228
|
+
# @sim.step! 1
|
229
|
+
# @sim.time.must_equal 2
|
230
|
+
# @sim.p_m.must_equal( { A: 3, B: 43 } )
|
231
|
+
# @sim.recording.to_h.must_equal( { 0.0 => [1], 1.0 => [2], 2.0 => [3] } )
|
232
|
+
# @sim.send( :transitions ).Ts.first.gradient_closure.call.must_equal [1]
|
233
|
+
|
234
|
+
# 3.times do @sim.step! 1 end
|
235
|
+
# @sim.recording.to_h
|
236
|
+
# .must_equal( { 0.0 => [1], 1.0 => [2], 2.0 => [3], 3.0 => [4] } )
|
237
|
+
# end
|
238
|
+
# end
|
239
|
+
# end # Ts transition
|
240
|
+
|
241
|
+
# describe "TS transition" do
|
242
|
+
# before do
|
243
|
+
# @net = @w.Net.of [ @p, @q, @TS ]
|
244
|
+
# end
|
245
|
+
|
246
|
+
# describe "no clamps" do
|
247
|
+
# before do
|
248
|
+
# @sim = @net.simulation sampling: 1
|
249
|
+
# end
|
250
|
+
|
251
|
+
# it "should behave" do
|
252
|
+
# @sim.recording.must_be_kind_of YPetri::Net::DataSet
|
253
|
+
# @sim.recording
|
254
|
+
# .must_equal @net.State.Features.marking.DataSet.new.update( 0.0 => [1, 2] )
|
255
|
+
# @sim.recording.must_equal( { 0.0 => [1, 2] } )
|
256
|
+
# @sim.step! 1
|
257
|
+
# @sim.recording.must_equal( { 0.0 => [1, 2], 1.0 => [2, 1] } )
|
258
|
+
# end
|
259
|
+
# end
|
260
|
+
|
261
|
+
# describe "with clamps" do
|
262
|
+
# before do
|
263
|
+
# @sim = @net.simulation sampling: 1, marking_clamps: { B: 43 }
|
264
|
+
# end
|
265
|
+
|
266
|
+
# it "should behave" do
|
267
|
+
# @sim.recording.must_equal( { 0.0 => [1] } )
|
268
|
+
# 3.times do @sim.step! end
|
269
|
+
# @sim.recording.must_equal( { 0.0 => [1], 1.0 => [2], 2.0 => [3], 3.0 => [4] } )
|
270
|
+
# end
|
271
|
+
# end
|
272
|
+
# end # TS transition
|
273
|
+
# end # transition representation aspects
|
274
|
+
# end
|
275
|
+
# end
|
276
|
+
|
277
|
+
# describe "timeless simulation" do
|
278
|
+
# before do
|
279
|
+
# self.class.class_exec { include YPetri }
|
280
|
+
# U = Place m!: 2.5
|
281
|
+
# V = Place m!: 2.5
|
282
|
+
# Uplus = Transition codomain: :U do 1 end # s transition
|
283
|
+
# U2V = Transition s: { U: -1, V: 1 } # S transition
|
284
|
+
# set_ssc :Timeless, YPetri::Simulation::DEFAULT_SETTINGS.call
|
285
|
+
# new_simulation ssc: :Timeless
|
286
|
+
# 5.times do simulation.step! end
|
287
|
+
# end
|
288
|
+
|
289
|
+
# it "should behave" do
|
290
|
+
# s = simulation
|
291
|
+
# assert ! s.timed?
|
292
|
+
# s.core.must_be_kind_of YPetri::Core::Timeless::PseudoEuler
|
293
|
+
# ds = s.recording
|
294
|
+
# ds.size.must_equal 6
|
295
|
+
# ds.events.must_equal [0, 1, 2, 3, 4, 5]
|
296
|
+
# ds.interpolate( 1 )
|
297
|
+
# .must_equal [2.5, 3.5]
|
298
|
+
# ds.interpolate( 2 )
|
299
|
+
# .must_equal [2.5, 4.5]
|
300
|
+
# -> { ds.interpolate( 1.5 ) }.must_raise TypeError
|
301
|
+
# ds.reconstruct( at: 2 )
|
302
|
+
# .p_m.must_equal( { U: 2.5, V: 4.5 } )
|
303
|
+
# ds.reconstruct( at: 2 ).must_respond_to( :pm )
|
304
|
+
# ds.marking.slice( 2..4 ).series
|
305
|
+
# .must_equal [[2.5, 2.5, 2.5], [4.5, 5.5, 6.5]]
|
306
|
+
# ds.marking.slice( 2..4 )
|
307
|
+
# .must_equal( { 2 => [2.5, 4.5],
|
308
|
+
# 3 => [2.5, 5.5],
|
309
|
+
# 4 => [2.5, 6.5] } )
|
310
|
+
# ds.firing.slice( 1..2 ).series
|
311
|
+
# .must_equal [[1, 1]]
|
312
|
+
# ds.series( firing: [:U2V] )
|
313
|
+
# .must_equal [ [ 1, 1, 1, 1, 1, 1 ] ]
|
314
|
+
# ds.Delta( [:U], transitions: [:Uplus] ).series
|
315
|
+
# .must_equal [ [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ]
|
316
|
+
# tmp = ds.reduce_features( marking: [:U], firing: [:U2V] )
|
317
|
+
# tmp.features
|
318
|
+
# .must_equal( ds.net.State.Features marking: [:U], firing: [:U2V] )
|
319
|
+
# tmp.must_equal( { 0 => [2.5, 1], 1 => [2.5, 1], 2 => [2.5, 1],
|
320
|
+
# 3 => [2.5, 1], 4 => [2.5, 1], 5 => [2.5, 1] } )
|
321
|
+
# end
|
322
|
+
# end
|
301
323
|
|
302
324
|
describe "timed simulation" do
|
303
325
|
before do
|
@@ -311,44 +333,49 @@ describe "timed simulation" do
|
|
311
333
|
|
312
334
|
it "should behave" do
|
313
335
|
places.map( &:marking ).must_equal [0.5, 0.5] # marking unaffected
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
net.State.Features.
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
336
|
+
s = simulation
|
337
|
+
s.settings.must_equal( { method: :pseudo_euler, guarded: false,
|
338
|
+
step: 0.1, sampling: 5, time: 0..60 } )
|
339
|
+
assert s.recording.to_csv.start_with?( ":event,:A,:B\n" +
|
340
|
+
"0.0,0.5,0.5\n" +
|
341
|
+
"5.0,0.475,0.38916\n" +
|
342
|
+
"10.0,0.45,0.30289\n" +
|
343
|
+
"15.0,0.425,0.23574\n" +
|
344
|
+
"20.0,0.4,0.18348\n" +
|
345
|
+
"25.0,0.375,0.1428\n" )
|
346
|
+
assert s.recording.to_csv.end_with?( "60.0,0.2,0.02471" )
|
347
|
+
s.recording.events.must_equal [ 0.0, 5.0, 10.0, 15.0, 20.0,
|
348
|
+
25.0, 30.0, 35.0, 40.0, 45.0,
|
349
|
+
50.0, 55.0, 60.0 ]
|
350
|
+
s.recording.values_at( 5, 10 )
|
351
|
+
.must_equal [ [0.475, 0.38916], [0.45, 0.30289] ]
|
352
|
+
s.recording.slice( 2..12 )
|
353
|
+
.must_equal( { 5.0 => [0.475, 0.38916], 10.0=>[0.45, 0.30289] } )
|
354
|
+
s.recording.net
|
355
|
+
.must_equal net
|
356
|
+
s.recording.features
|
357
|
+
.must_equal net.State.Features.marking( :A, :B )
|
358
|
+
net.State.Features.State
|
359
|
+
.must_equal net.State
|
360
|
+
s.recording.net.State
|
361
|
+
.must_equal net.State
|
362
|
+
s.recording.series( marking: [:A] )
|
363
|
+
.must_equal [ [ 0.5, 0.475, 0.45, 0.425, 0.4, 0.375, 0.35, 0.325,
|
364
|
+
0.3, 0.275, 0.25, 0.225, 0.2 ] ]
|
365
|
+
s.net.State.Features.firing.map( &:transition ).names
|
366
|
+
.must_equal [ :A_pump, :B_decay ]
|
367
|
+
s.recording.reduce_features( s.net.State.Features.firing, Δt: 1 )
|
368
|
+
.to_h.take( 2 ).map( &:flatten! ).map { |a| a.map &[:round, 6 ] }
|
369
|
+
.must_equal [ [ 0.0, 0.005, 0.025 ], [ 5.0, 0.005, 0.019458 ] ]
|
370
|
+
s.recording.firing( Δt: 0.1 ).series.map( &:first ).map( &[ :round, 6 ] )
|
371
|
+
.must_equal [ 0.0005, 0.0025 ]
|
372
|
+
s.recording.Firing( [] )
|
373
|
+
.must_equal( [*0..12].map { |n| n * 5.0 } >> [[]] * 13 )
|
374
|
+
s.recording
|
375
|
+
.delta( :A, transitions: [:A_pump], delta_time: 0.1 )
|
376
|
+
.series
|
377
|
+
.must_equal [ [ -0.0005 ] * 13 ]
|
378
|
+
plot_state
|
379
|
+
sleep 5
|
353
380
|
end
|
354
381
|
end
|