y_petri 2.0.15 → 2.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/lib/y_petri/{manipulator → agent}/hash_key_pointer.rb +2 -2
  3. data/lib/y_petri/agent/petri_net_related.rb +115 -0
  4. data/lib/y_petri/{manipulator → agent}/selection.rb +2 -1
  5. data/lib/y_petri/{manipulator/simulation_related_methods.rb → agent/simulation_related.rb} +93 -110
  6. data/lib/y_petri/agent.rb +22 -0
  7. data/lib/y_petri/core/timed/euler.rb +20 -0
  8. data/lib/y_petri/core/timed/pseudo_euler.rb +31 -0
  9. data/lib/y_petri/core/timed/quasi_euler.rb +23 -0
  10. data/lib/y_petri/core/timed.rb +70 -0
  11. data/lib/y_petri/core/timeless/pseudo_euler.rb +20 -0
  12. data/lib/y_petri/core/timeless.rb +12 -0
  13. data/lib/y_petri/core.rb +100 -0
  14. data/lib/y_petri/dsl.rb +66 -0
  15. data/lib/y_petri/fixed_assets.rb +7 -0
  16. data/lib/y_petri/net/element_access.rb +239 -0
  17. data/lib/y_petri/net/state/feature/delta.rb +88 -0
  18. data/lib/y_petri/net/state/feature/firing.rb +57 -0
  19. data/lib/y_petri/net/state/feature/flux.rb +58 -0
  20. data/lib/y_petri/net/state/feature/gradient.rb +75 -0
  21. data/lib/y_petri/net/state/feature/marking.rb +62 -0
  22. data/lib/y_petri/net/state/feature.rb +79 -0
  23. data/lib/y_petri/net/state/features/dataset.rb +135 -0
  24. data/lib/y_petri/net/state/features/record.rb +50 -0
  25. data/lib/y_petri/net/state/features.rb +126 -0
  26. data/lib/y_petri/net/state.rb +121 -0
  27. data/lib/y_petri/net/timed.rb +8 -0
  28. data/lib/y_petri/net/visualization.rb +3 -3
  29. data/lib/y_petri/net.rb +73 -77
  30. data/lib/y_petri/place.rb +8 -3
  31. data/lib/y_petri/simulation/dependency.rb +107 -0
  32. data/lib/y_petri/simulation/element_representation.rb +20 -0
  33. data/lib/y_petri/simulation/elements/access.rb +57 -0
  34. data/lib/y_petri/simulation/elements.rb +45 -0
  35. data/lib/y_petri/simulation/feature_set.rb +21 -0
  36. data/lib/y_petri/simulation/initial_marking/access.rb +55 -0
  37. data/lib/y_petri/simulation/initial_marking.rb +15 -0
  38. data/lib/y_petri/simulation/marking_clamps/access.rb +34 -0
  39. data/lib/y_petri/simulation/marking_clamps.rb +18 -0
  40. data/lib/y_petri/simulation/marking_vector/access.rb +106 -0
  41. data/lib/y_petri/simulation/marking_vector.rb +156 -0
  42. data/lib/y_petri/simulation/matrix.rb +64 -0
  43. data/lib/y_petri/simulation/place_mapping.rb +62 -0
  44. data/lib/y_petri/simulation/place_representation.rb +74 -0
  45. data/lib/y_petri/simulation/places/access.rb +121 -0
  46. data/lib/y_petri/simulation/places/clamped.rb +8 -0
  47. data/lib/y_petri/simulation/places/free.rb +8 -0
  48. data/lib/y_petri/simulation/places/types.rb +25 -0
  49. data/lib/y_petri/simulation/places.rb +41 -0
  50. data/lib/y_petri/simulation/recorder.rb +54 -0
  51. data/lib/y_petri/simulation/timed/recorder.rb +53 -0
  52. data/lib/y_petri/simulation/timed.rb +161 -261
  53. data/lib/y_petri/simulation/timeless/recorder.rb +25 -0
  54. data/lib/y_petri/simulation/timeless.rb +35 -0
  55. data/lib/y_petri/simulation/transition_representation/A.rb +58 -0
  56. data/lib/y_petri/simulation/transition_representation/S.rb +45 -0
  57. data/lib/y_petri/simulation/transition_representation/T.rb +80 -0
  58. data/lib/y_petri/simulation/transition_representation/TS.rb +46 -0
  59. data/lib/y_petri/simulation/transition_representation/Ts.rb +32 -0
  60. data/lib/y_petri/simulation/transition_representation/a.rb +30 -0
  61. data/lib/y_petri/simulation/transition_representation/s.rb +29 -0
  62. data/lib/y_petri/simulation/transition_representation/t.rb +37 -0
  63. data/lib/y_petri/simulation/transition_representation/tS.rb +38 -0
  64. data/lib/y_petri/simulation/transition_representation/ts.rb +32 -0
  65. data/lib/y_petri/simulation/transition_representation/types.rb +62 -0
  66. data/lib/y_petri/simulation/transition_representation.rb +79 -0
  67. data/lib/y_petri/simulation/transitions/A.rb +40 -0
  68. data/lib/y_petri/simulation/transitions/S.rb +24 -0
  69. data/lib/y_petri/simulation/transitions/T.rb +34 -0
  70. data/lib/y_petri/simulation/transitions/TS.rb +57 -0
  71. data/lib/y_petri/simulation/transitions/Ts.rb +60 -0
  72. data/lib/y_petri/simulation/transitions/a.rb +8 -0
  73. data/lib/y_petri/simulation/transitions/access.rb +186 -0
  74. data/lib/y_petri/simulation/transitions/s.rb +9 -0
  75. data/lib/y_petri/simulation/transitions/t.rb +22 -0
  76. data/lib/y_petri/simulation/transitions/tS.rb +55 -0
  77. data/lib/y_petri/simulation/transitions/ts.rb +58 -0
  78. data/lib/y_petri/simulation/transitions/types.rb +98 -0
  79. data/lib/y_petri/simulation/transitions.rb +21 -0
  80. data/lib/y_petri/simulation.rb +176 -781
  81. data/lib/y_petri/transition/assignment.rb +7 -5
  82. data/lib/y_petri/transition/construction.rb +119 -187
  83. data/lib/y_petri/transition/init.rb +311 -0
  84. data/lib/y_petri/transition/ordinary_timeless.rb +8 -6
  85. data/lib/y_petri/transition/timed.rb +11 -18
  86. data/lib/y_petri/transition.rb +104 -132
  87. data/lib/y_petri/version.rb +1 -1
  88. data/lib/y_petri/world/dependency.rb +40 -0
  89. data/lib/y_petri/world/petri_net_related.rb +61 -0
  90. data/lib/y_petri/{workspace/simulation_related_methods.rb → world/simulation_related.rb} +42 -49
  91. data/lib/y_petri/world.rb +27 -0
  92. data/lib/y_petri.rb +47 -99
  93. data/test/{manipulator_test.rb → agent_test.rb} +19 -17
  94. data/{lib/y_petri → test/examples}/demonstrator.rb +0 -0
  95. data/{lib/y_petri → test/examples}/demonstrator_2.rb +1 -0
  96. data/{lib/y_petri → test/examples}/demonstrator_3.rb +0 -0
  97. data/{lib/y_petri → test/examples}/demonstrator_4.rb +0 -0
  98. data/test/examples/example_2.rb +16 -0
  99. data/test/{manual_examples.rb → examples/manual_examples.rb} +0 -0
  100. data/test/net_test.rb +126 -121
  101. data/test/place_test.rb +1 -1
  102. data/test/sim_test +565 -0
  103. data/test/simulation_test.rb +338 -264
  104. data/test/transition_test.rb +77 -174
  105. data/test/world_mock.rb +12 -0
  106. data/test/{workspace_test.rb → world_test.rb} +19 -20
  107. data/test/y_petri_test.rb +4 -5
  108. metadata +101 -26
  109. data/lib/y_petri/dependency_injection.rb +0 -45
  110. data/lib/y_petri/manipulator/petri_net_related_methods.rb +0 -74
  111. data/lib/y_petri/manipulator.rb +0 -20
  112. data/lib/y_petri/net/selections.rb +0 -209
  113. data/lib/y_petri/simulation/collections.rb +0 -460
  114. data/lib/y_petri/workspace/parametrized_subclassing.rb +0 -22
  115. data/lib/y_petri/workspace/petri_net_related_methods.rb +0 -88
  116. data/lib/y_petri/workspace.rb +0 -16
  117. data/test/timed_simulation_test.rb +0 -153
@@ -0,0 +1,45 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for S transition representations.
4
+ #
5
+ class YPetri::Simulation::TransitionRepresentation
6
+ module Type_S
7
+ include Type_a
8
+
9
+ attr_reader :stoichiometry
10
+
11
+ # Sparse stoichiometry vector corresponding to the free places.
12
+ #
13
+ attr_reader :sparse_stoichiometry_vector
14
+
15
+ # Sparse stoichiometry vector corresponding to all the places.
16
+ #
17
+ attr_reader :sparse_sv
18
+
19
+ # True for stoichiometric transitions.
20
+ #
21
+ def S?
22
+ true
23
+ end
24
+ alias stoichiometric? S?
25
+
26
+ # False for stoichiometric transitions.
27
+ #
28
+ def s?
29
+ false
30
+ end
31
+ alias nonstoichiometric? s?
32
+
33
+ # Initialization subroutine.
34
+ #
35
+ def init
36
+ super
37
+ @stoichiometry = source.stoichiometry
38
+ @sparse_stoichiometry_vector =
39
+ Matrix.correspondence_matrix( codomain, free_places ) *
40
+ stoichiometry.to_column_vector
41
+ @sparse_sv = Matrix.correspondence_matrix( codomain, places ) *
42
+ stoichiometry.to_column_vector
43
+ end
44
+ end # module Type_S
45
+ end # class YPetri::Simulation::TransitionRepresentation
@@ -0,0 +1,80 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for timed transition representations.
4
+ #
5
+ class YPetri::Simulation::TransitionRepresentation
6
+ module Type_T
7
+ include Type_a
8
+
9
+ # True for timed transitions.
10
+ #
11
+ def T?
12
+ true
13
+ end
14
+ alias timed? T?
15
+
16
+ # False for timed transitions.
17
+ #
18
+ def t?
19
+ false
20
+ end
21
+ alias timeless? t?
22
+
23
+ # Initialization subroutine.
24
+ #
25
+ def init
26
+ simulation.extend YPetri::Simulation::Timed unless simulation.timed?
27
+ super
28
+ @function = source.rate_closure
29
+ end
30
+
31
+ # Gradient contribution of the transition to the free places.
32
+ #
33
+ def gradient
34
+ ∇.select { |p, v| p.free? }
35
+ end
36
+
37
+ # Returns the gradient contribution to the free places, as hash
38
+ # place names >> gradient contribution.
39
+ #
40
+ def g
41
+ gradient.with_keys do |p| p.name || p end
42
+ end
43
+
44
+ # Returns the gradient contribution to all the places, as hash
45
+ # place names >> gradient contribution.
46
+ #
47
+ def g_all
48
+ ∇.with_keys do |p| p.name || p end
49
+ end
50
+
51
+ # Change, for free places, as it would happen if the transition fired for
52
+ # time Δt, returned as hash codomain places >> change.
53
+ #
54
+ def delta Δt
55
+ gradient.with_values { |v| v * Δt }
56
+ end
57
+
58
+ # Change, for free places, as it would happen if the transition fired for
59
+ # time Δt, returned as hash codomain place names >> change.
60
+ #
61
+ def d Δt
62
+ delta( Δt ).with_keys do |p| p.name || p end
63
+ end
64
+
65
+ # Change, for all places, as it would happen if the transition fired for
66
+ # time Δt, returned as hash codomain places >> change.
67
+ #
68
+ def Δ( Δt )
69
+ gradient.with_values { |v| v * Δt }
70
+ end
71
+ alias delta_all Δ
72
+
73
+ # Change, for all places, as it would happen if the transition fired for
74
+ # time Δt, returned as hash codomain place names >> change.
75
+ #
76
+ def d_all( Δt )
77
+ delta( Δt ).with_keys do |p| p.name || p end
78
+ end
79
+ end # Type_T
80
+ end # class YPetri::Simulation::TransitionRepresentation
@@ -0,0 +1,46 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for TS transition representations.
4
+ #
5
+ class YPetri::Simulation::TransitionRepresentation
6
+ module Type_TS
7
+ include Type_T
8
+ include Type_S
9
+
10
+ attr_reader :rate_closure
11
+
12
+ # Initialization subroutine.
13
+ #
14
+ def init
15
+ super
16
+ @rate_closure = to_rate_closure
17
+ end
18
+
19
+ # Transition's rate, given the current system state.
20
+ #
21
+ def rate
22
+ rate_closure.call
23
+ end
24
+ alias flux rate
25
+ alias propensity rate
26
+
27
+ # Firing of the transition (rate * Δtime).
28
+ #
29
+ def firing Δt
30
+ rate * Δt
31
+ end
32
+
33
+ # Gradient contribution of the transition to all places.
34
+ #
35
+ def ∇
36
+ codomain >> stoichiometry.map { |coeff| rate * coeff }
37
+ end
38
+ alias gradient_all ∇
39
+
40
+ # Builds a flux closure.
41
+ #
42
+ def to_rate_closure
43
+ build_closure
44
+ end
45
+ end # module Type_TS
46
+ end # class YPetri::Simulation::TransitionRepresentation
@@ -0,0 +1,32 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for Ts transition representations.
4
+ #
5
+ class YPetri::Simulation::TransitionRepresentation
6
+ module Type_Ts
7
+ include Type_T
8
+ include Type_s
9
+
10
+ attr_reader :gradient_closure
11
+
12
+ # Initialization subroutine.
13
+ #
14
+ def init
15
+ super
16
+ @gradient_closure = to_gradient_closure
17
+ end
18
+
19
+ # Gradient contribution of the transition to all places.
20
+ #
21
+ def ∇
22
+ codomain >> gradient_closure.call
23
+ end
24
+ alias gradient_all ∇
25
+
26
+ # Builds a gradient closure.
27
+ #
28
+ def to_gradient_closure
29
+ build_closure
30
+ end
31
+ end # Type_Ts
32
+ end # class YPetri::Simulation::TransitionRepresentation
@@ -0,0 +1,30 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for non-assignment transition representations.
4
+ #
5
+ module YPetri::Simulation::TransitionRepresentation::Type_a
6
+ # Assignment action -- false for non-assignment transitions.
7
+ #
8
+ def A?
9
+ false
10
+ end
11
+ alias assignment_action? A?
12
+ alias assignment? A?
13
+
14
+ # Normal (non-assignment) action -- true for A transitions
15
+ #
16
+ def a?
17
+ true
18
+ end
19
+
20
+ # Initialization subroutine.
21
+ #
22
+ def init
23
+ end
24
+
25
+ # Change, for free places, as it would happen if the transition fired.
26
+ #
27
+ def delta
28
+ Δ.select { |p, v| p.free? }
29
+ end
30
+ end # class YPetri::Simulation::TransitionRepresentation::Type_a
@@ -0,0 +1,29 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for s (nonstoichiometric) transition representations.
4
+ #
5
+ class YPetri::Simulation::TransitionRepresentation
6
+ module Type_s
7
+ include Type_a
8
+
9
+ # False for non-stoichiometric transitions.
10
+ #
11
+ def S?
12
+ false
13
+ end
14
+ alias stoichiometric? S?
15
+
16
+ # True for stoichiometric transitions.
17
+ #
18
+ def s?
19
+ true
20
+ end
21
+ alias nonstoichiometric? s?
22
+
23
+ # Initialization subroutine.
24
+ #
25
+ def init
26
+ super
27
+ end
28
+ end # module Type_s
29
+ end # class YPetri::Simulation::TransitionRepresentation
@@ -0,0 +1,37 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for timed transition representations.
4
+ #
5
+ class YPetri::Simulation::TransitionRepresentation
6
+ module Type_t
7
+ include Type_a
8
+
9
+ # False for timed transitions.
10
+ #
11
+ def T?
12
+ false
13
+ end
14
+ alias timed? T?
15
+
16
+ # True for timed transitions.
17
+ #
18
+ def t?
19
+ true
20
+ end
21
+ alias timeless? t?
22
+
23
+ # Initialization subroutine.
24
+ #
25
+ def init
26
+ super
27
+ @function = source.action_closure
28
+ end
29
+
30
+ # Change, as it would happen if the transition fired, returned as hash
31
+ # codomain places >> change.
32
+ #
33
+ def d
34
+ delta.with_keys do |p| p.name || p end
35
+ end
36
+ end # Type_t
37
+ end # class YPetri::Simulation::TransitionRepresentation
@@ -0,0 +1,38 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for tS transition representations.
4
+ #
5
+ class YPetri::Simulation::TransitionRepresentation
6
+ module Type_tS
7
+ include Type_t
8
+ include Type_S
9
+
10
+ attr_reader :firing_closure
11
+
12
+ # Initialization subroutine.
13
+ #
14
+ def init
15
+ super
16
+ @firing_closure = to_firing_closure
17
+ end
18
+
19
+ # Transition's firing, given the current system state.
20
+ #
21
+ def firing
22
+ firing_closure.call
23
+ end
24
+
25
+ # Change, to all places, as it would happen if the transition fired.
26
+ #
27
+ def Δ
28
+ codomain >> stoichiometry.map { |coeff| firing * coeff }
29
+ end
30
+ alias delta_all Δ
31
+
32
+ # Builds a firing closure.
33
+ #
34
+ def to_firing_closure
35
+ build_closure
36
+ end
37
+ end # module Type_tS
38
+ end # class YPetri::Simulation::TransitionRepresentation
@@ -0,0 +1,32 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for ts transition representations.
4
+ #
5
+ class YPetri::Simulation::TransitionRepresentation
6
+ module Type_ts
7
+ include Type_t
8
+ include Type_s
9
+
10
+ attr_reader :delta_closure
11
+
12
+ # Initialization subroutine.
13
+ #
14
+ def init
15
+ super
16
+ @delta_closure = to_delta_closure
17
+ end
18
+
19
+ # Delta state closure.
20
+ #
21
+ def to_delta_closure
22
+ build_closure
23
+ end
24
+
25
+ # Change, to all places, as it would happen if the transition fired.
26
+ #
27
+ def Δ
28
+ codomain >> delta_closure.call
29
+ end
30
+ alias delta_all Δ
31
+ end # module Type_ts
32
+ end # class YPetri::Simulation::TransitionRepresentation
@@ -0,0 +1,62 @@
1
+ #encoding: utf-8
2
+
3
+ require_relative 'a'
4
+ require_relative 'A'
5
+ require_relative 't'
6
+ require_relative 'T'
7
+ require_relative 's'
8
+ require_relative 'S'
9
+ require_relative 'ts'
10
+ require_relative 'Ts'
11
+ require_relative 'tS'
12
+ require_relative 'TS'
13
+
14
+ # A mixin with transition types.
15
+ #
16
+ class YPetri::Simulation::TransitionRepresentation
17
+ module Types
18
+ attr_reader :type
19
+
20
+ def type_init *args
21
+ extend case source.type
22
+ when :A then Type_A
23
+ when :TS then Type_TS
24
+ when :Ts then Type_Ts
25
+ when :tS then Type_tS
26
+ when :ts then Type_ts
27
+ else fail TypeError, "Unknown tr. type #{source.type}!" end
28
+ init
29
+ end
30
+
31
+ # The transition's type.
32
+ #
33
+ def type
34
+ return :A if A?
35
+ if T? then S? ? :TS : :Ts else S? ? :tS : :ts end
36
+ end
37
+
38
+ # Is this a TS transition?
39
+ #
40
+ def TS?
41
+ type == :TS
42
+ end
43
+
44
+ # Is this a Ts transition?
45
+ #
46
+ def Ts?
47
+ type == :Ts
48
+ end
49
+
50
+ # Is this a tS transition?
51
+ #
52
+ def tS?
53
+ type == :tS
54
+ end
55
+
56
+ # Is this a ts transition?
57
+ #
58
+ def ts?
59
+ type == :ts
60
+ end
61
+ end # module Types
62
+ end # class YPetri::Simulation::TransitionRepresentation
@@ -0,0 +1,79 @@
1
+ #encoding: utf-8
2
+
3
+ # Representation of a YPetri::Transition inside a YPetri::Simulation instance.
4
+ #
5
+ class YPetri::Simulation
6
+ class TransitionRepresentation < ElementRepresentation
7
+ require_relative 'transition_representation/types'
8
+ include Types
9
+
10
+ attr_reader :domain, :codomain
11
+ attr_reader :function # source transition function
12
+
13
+ # Expect a single YPetri place as an argument.
14
+ #
15
+ def initialize net_transition
16
+ super
17
+ @domain, @codomain = places( source.domain ), places( source.codomain )
18
+ type_init
19
+ end
20
+
21
+ # Returns the indices of this transition's domain in the marking vector.
22
+ #
23
+ def domain_indices
24
+ domain.map { |p| places.index p }
25
+ end
26
+
27
+ # Returns the indices of this transition's codomain in the marking vector.
28
+ #
29
+ def codomain_indices
30
+ codomain.map { |p| places.index p }
31
+ end
32
+
33
+ # Returns the indices of this transition's domain among the free places.
34
+ #
35
+ def free_domain_indices
36
+ domain.map { |p| free_places.index p }
37
+ end
38
+
39
+ # Returns the indices of this transition's codomain among the free places.
40
+ #
41
+ def free_codomain_indices
42
+ codomain.map { |p| free_places.index p }
43
+ end
44
+
45
+ # Builds the transition's function into a closure.
46
+ #
47
+ def build_closure
48
+ mv, f = simulation.m_vector, function
49
+ λ = "-> { f.( %s ) }" % domain_access_code( vector: :mv )
50
+ eval λ
51
+ end
52
+
53
+ # Builds a code string for accessing the domain directly from a marking
54
+ # vector, given as argument.
55
+ #
56
+ def domain_access_code( vector: :m_vector )
57
+ Matrix.column_vector_access_code( vector: vector,
58
+ indices: domain_indices )
59
+ end
60
+
61
+ # Builds a code string that assigns to the free places of the codomain.
62
+ #
63
+ def codomain_assignment_code vector: (fail ArgumentError, "No vector!"),
64
+ source: (fail ArgumentError, "No source array!")
65
+ Matrix.column_vector_assignment_code( vector: vector,
66
+ indices: free_codomain_indices,
67
+ source: source )
68
+ end
69
+
70
+ # Builds a closure that increments a vector with this transition's codomain.
71
+ #
72
+ def increment_by_codomain_code vector: (fail ArgumentError, "No vector!"),
73
+ source: (fail ArgumentError, "No source array!")
74
+ Matrix.column_vector_increment_by_array_code vector: vector,
75
+ indices: free_codomain_indices,
76
+ source: source
77
+ end
78
+ end # class TransitionRepresentation
79
+ end # class YPetri::Simulation
@@ -0,0 +1,40 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for collections of A transitions.
4
+ #
5
+ class YPetri::Simulation::Transitions
6
+ module Type_A
7
+ attr_reader :assignment_closure
8
+
9
+ def initialize
10
+ @assignment_closure = to_assignment_closure
11
+ end
12
+
13
+ # Assignment closures that directly affect the marking when called.
14
+ #
15
+ def assignment_closures
16
+ map &:assignment_closure
17
+ end
18
+
19
+ # Combined assignment action, as it would occur if these A transitions fired
20
+ # in order, as hash place >> action.
21
+ #
22
+ def action
23
+ each_with_object Hash.new do |t, hsh| hsh.update( t.action ) end
24
+ end
25
+
26
+ # Returns the assignments to all places, as they would happen if A transition
27
+ # could change their values.
28
+ #
29
+ def act
30
+ each_with_object Hash.new do |t, hsh| hsh.update( t.act ) end
31
+ end
32
+
33
+ # Builds a joint assignment closure.
34
+ #
35
+ def to_assignment_closure
36
+ closures = assignment_closures
37
+ -> { closures.each &:call }
38
+ end
39
+ end # Type_A
40
+ end # class YPetri::Simulation::Transitions
@@ -0,0 +1,24 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for collections of S transitions.
4
+ #
5
+ class YPetri::Simulation::Transitions
6
+ module Type_S
7
+ include Type_a
8
+
9
+ # Returns the collection's stoichiometry matrix versus free places.
10
+ #
11
+ def stoichiometry_matrix
12
+ map( &:sparse_stoichiometry_vector )
13
+ .reduce( Matrix.empty( places.free.size, 0 ), :join_right )
14
+ end
15
+
16
+ # Returns the collection's stoichiometry matrix versus all places.
17
+ #
18
+ def SM
19
+ map( &:sparse_sv )
20
+ .reduce( Matrix.empty( places.size, 0 ), :join_right )
21
+ end
22
+ alias stoichiometry_matrix_all SM
23
+ end # module Type_S
24
+ end # class YPetri::Simulation::Transitions
@@ -0,0 +1,34 @@
1
+ #encoding: utf-8
2
+
3
+ # A mixin for collections of T transitions.
4
+ #
5
+ class YPetri::Simulation::Transitions
6
+ module Type_T
7
+ include Type_a
8
+
9
+ # T transitions have gradient closures.
10
+ #
11
+ def gradient_closures
12
+ map &:gradient_closure
13
+ end
14
+
15
+ # Gradient by the T transitions.
16
+ #
17
+ def gradient
18
+ Ts().gradient + TS().gradient
19
+ end
20
+
21
+ # State change of free places if the timed transitions fire for given time.
22
+ #
23
+ def delta Δt
24
+ gradient * Δt
25
+ end
26
+
27
+ # State change of all places if the timed transitions fire for given time.
28
+ #
29
+ def Δ Δt
30
+ ∇ * Δt
31
+ end
32
+ alias delta_all Δ
33
+ end # module Type_T
34
+ end # class YPetri::Simulation::Transitions
@@ -0,0 +1,57 @@
1
+ #encoding: utf-8
2
+
3
+ # Mixin for collections of TS transitions.
4
+ #
5
+ class YPetri::Simulation::Transitions
6
+ module Type_TS
7
+ include Type_T
8
+ include Type_S
9
+
10
+ # Rate vector closure accessor.
11
+ #
12
+ def rate_closure
13
+ @rate_closure ||= to_rate_closure
14
+ end
15
+
16
+ # Rate (flux, propensity) closures.
17
+ #
18
+ def rate_closures
19
+ map &:rate_closure
20
+ end
21
+
22
+ # Rate (flux/propensity) vector.
23
+ #
24
+ def rate_vector
25
+ to_rate_closure.call
26
+ end
27
+ alias flux_vector rate_vector
28
+ alias propensity_vector rate_vector
29
+
30
+ # Firing vector (rate vector * Δtime).
31
+ #
32
+ def firing_vector Δt
33
+ rate_vector * Δt
34
+ end
35
+
36
+ # Gradient contribution to free places.
37
+ #
38
+ def gradient
39
+ stoichiometry_matrix * rate_vector
40
+ end
41
+
42
+ # Gradient contribution to all places.
43
+ #
44
+ def ∇
45
+ SM() * rate_vector
46
+ end
47
+ alias gradient_all ∇
48
+
49
+ # Builds the rate vector closure, that outputs the rate vector based on
50
+ # the system state when called.
51
+ #
52
+ def to_rate_closure
53
+ rc = rate_closures
54
+ -> { rc.map( &:call ).to_column_vector }
55
+ end
56
+ end # module Type_TS
57
+ end # class YPetri::Simulation::Transitions