Module: YPetri::Simulation::Timed

Defined in:
lib/y_petri/simulation/timed.rb,
lib/y_petri/simulation/timed/recorder.rb

Overview

A mixin for timed simulations, used by an #extend call during init.

Defined Under Namespace

Classes: Recorder

Constant Summary collapse

DEFAULT_SETTINGS =
-> do { step: 0.1, sampling: 5, time: 0..60 } end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_samplingObject (readonly)

Returns the value of attribute default_sampling.



16
17
18
# File 'lib/y_petri/simulation/timed.rb', line 16

def default_sampling
  @default_sampling
end

#initial_timeObject (readonly) Also known as: starting_time

Returns the value of attribute initial_time.



16
17
18
# File 'lib/y_petri/simulation/timed.rb', line 16

def initial_time
  @initial_time
end

#stepObject

Returns the value of attribute step.



16
17
18
# File 'lib/y_petri/simulation/timed.rb', line 16

def step
  @step
end

#target_timeObject Also known as: ending_time

Returns the value of attribute target_time.



16
17
18
# File 'lib/y_petri/simulation/timed.rb', line 16

def target_time
  @target_time
end

#timeObject (readonly)

Returns the value of attribute time.



16
17
18
# File 'lib/y_petri/simulation/timed.rb', line 16

def time
  @time
end

#time_unitObject (readonly)

Returns the value of attribute time_unit.



16
17
18
# File 'lib/y_petri/simulation/timed.rb', line 16

def time_unit
  @time_unit
end

Instance Method Details

#at(*args) ⇒ Object

Alias for #dup for timed simulations.



199
200
201
# File 'lib/y_petri/simulation/timed.rb', line 199

def at *args
  dup *args
end

#dup(time: time(), **named_args) ⇒ Object

Customized dup method that allows to modify the attributes of the duplicate upon creation.



193
194
195
# File 'lib/y_petri/simulation/timed.rb', line 193

def dup time: time(), **named_args
  super( **named_args ).tap { |instance| instance.reset_time! time }
end

#fluxes(*transitions) ⇒ Object Also known as: flux

Expects an arbitrary number of arguments identifying TS transitions, and retuns an array of their fluxes. Returns fluxes of all the TS transitions if no argument is given.



79
80
81
82
# File 'lib/y_petri/simulation/timed.rb', line 79

def fluxes( *transitions )
  return Fluxes TS_transitions() if transitions.empty?
  Fluxes( transitions )
end

#Fluxes(array) ⇒ Object

Expects a single array of TS transitions or transition ids and returns an array of their fluxes under current marking.



69
70
71
72
73
# File 'lib/y_petri/simulation/timed.rb', line 69

def Fluxes( array )
  tt = TS_transitions()
  TS_Transitions( array )
    .map { |t| flux_vector.column_to_a.fetch tt.index( t ) }
end

#increment_time!(Δt = step) ⇒ Object

Increments the simulation’s time and alerts the recorder.



178
179
180
181
# File 'lib/y_petri/simulation/timed.rb', line 178

def increment_time! Δt=step
  @time += Δt
  recorder.alert!
end

#pflux(*transitions, gap: 0, precision: 4) ⇒ Object Also known as: pfluxes

Pretty prints flux of the indicated TS transitions as a hash with transition names as keys. Takes optional list of transition ids (first ordered arg.), and optional 2 named arguments (:gap and :precision), as in #pretty_print_numeric_values.



107
108
109
110
# File 'lib/y_petri/simulation/timed.rb', line 107

def pflux( *transitions, gap: 0, precision: 4 )
  t_flux( *transitions )
    .pretty_print_numeric_values( gap: gap, precision: precision )
end

#reset!(**nn) ⇒ Object

Resets the timed simulation.



185
186
187
188
# File 'lib/y_petri/simulation/timed.rb', line 185

def reset! **nn
  @time = initial_time || time_unit * 0
  super
end

#run(upto: target_time, final_step: :exact) ⇒ Object

Same as #run!, but guards against run upto infinity.



130
131
132
133
# File 'lib/y_petri/simulation/timed.rb', line 130

def run( upto: target_time, final_step: :exact )
  fail "Upto time equals infinity!" if upto == Float::INFINITY
  run!( upto: upto, final_step: final_step )
end

#run!(upto: target_time, final_step: :exact) ⇒ Object

Near alias for #run_upto. Accepts :upto named argument, using :final_step, has the same options as in #run_upto method.



139
140
141
# File 'lib/y_petri/simulation/timed.rb', line 139

def run!( upto: target_time, final_step: :exact )
  run_upto( upto, final_step: final_step )
end

#run_upto(target_time, final_step: :exact) ⇒ Object

Runs the simulation until the target time. Named argument :final_step has options :just_before, :just_after and :exact, and tunes the simulation behavior towards the end of the run.

stop_before: last step has normal size, simulation stops before or just

on the target time

stop_after: last step has normal size, simulation stops after or just

on the target time_step

exact: simulation stops exactly on the prescribed time, last step

is shortened if necessary


154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/y_petri/simulation/timed.rb', line 154

def run_upto( target_time, final_step: :exact )
  case final_step
  when :stop_before then
    step! while time + step <= target_time
  when :exact then
    step! while time + step < target_time
    step!( target_time - time )
    @time = target_time
  when :stop_after then
    step! while time < target_time
  else
    fail ArgumentError, "Unrecognized :final_step option: #{final_step}"
  end
end

#set_sampling(sampling) ⇒ Object

Sets sampling of the simulation’s data recorder.



48
49
50
# File 'lib/y_petri/simulation/timed.rb', line 48

def set_sampling sampling
  recorder.sampling = sampling
end

#set_simulation_methodObject

Changing the simulation method on the fly not supported.



54
55
56
57
# File 'lib/y_petri/simulation/timed.rb', line 54

def set_simulation_method
  fail NotImplementedError,
       "Changing simulation method on the fly not supported!"
end

#set_step(n) ⇒ Object Also known as: set_step_size

Explicit alias for #step= method. Deprecated, use #step= instead.



30
31
32
# File 'lib/y_petri/simulation/timed.rb', line 30

def set_step n
  step=( n )
end

#set_time(target_time) ⇒ Object Also known as: set_target_time

Explicit alias for #target_time= method. Deprecated, use #target_time= instead.



38
39
40
# File 'lib/y_petri/simulation/timed.rb', line 38

def set_time target_time
  target_time=( target_time )
end

#settings(all = false) ⇒ Object

Returns the settings pertaining to the Timed aspect of the simulation, that is, :step, :sampling and :time.



122
123
124
125
126
# File 'lib/y_petri/simulation/timed.rb', line 122

def settings all=false
  super.update( step: step,
                sampling: sampling,
                time: time_range )
end

#T_fluxes(array) ⇒ Object Also known as: t_Fluxes

Fluxes of the indicated TS transitions. Expects a single array argument, and returns a hash with transition names as keys.



88
89
90
# File 'lib/y_petri/simulation/timed.rb', line 88

def T_fluxes( array )
  TS_Transitions( array ).names( true ) >> Fluxes( array )
end

#t_fluxes(*transitions) ⇒ Object Also known as: t_flux

Fluxes of the indicated TS transitions. Expects an arbitrary number of TS transitions or their ids, returns a hash with transition names as keys.



96
97
98
99
# File 'lib/y_petri/simulation/timed.rb', line 96

def t_fluxes( *transitions )
  return T_fluxes TS_transitions() if transitions.empty?
  T_fluxes( transitions )
end

#time_rangeObject

Reads the time range (initial_time .. target_time) of the simulation.



115
116
117
# File 'lib/y_petri/simulation/timed.rb', line 115

def time_range
  initial_time .. target_time
end

#timed?Boolean

True for timed simulations.

Returns:

  • (Boolean)


12
13
14
# File 'lib/y_petri/simulation/timed.rb', line 12

def timed?
  true
end

#to_sObject

String representation of this timed simulation.



171
172
173
174
# File 'lib/y_petri/simulation/timed.rb', line 171

def to_s
  "#<Simulation: time: %s, pp: %s, tt: %s, oid: %s>" %
    [ time, pp.size, tt.size, object_id ]
end

#zero_gradient(places: nil) ⇒ Object Also known as: zero_∇

Returns the zero gradient. Optionally, places can be specified, for which the zero vector is returned.



206
207
208
209
210
211
212
# File 'lib/y_petri/simulation/timed.rb', line 206

def zero_gradient places: nil
  return zero_gradient places: places() if places.nil?
  places.map { |id|
    p = place( id )
    ( p.free? ? p.initial_marking : p.clamp ) * 0 / time_unit
  }.to_column_vector
end