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 (readonly)

Returns the value of attribute step.



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

def step
  @step
end

#target_timeObject (readonly) 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

#dup(time: time, **nn) ⇒ Object Also known as: at

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



141
142
143
# File 'lib/y_petri/simulation/timed.rb', line 141

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

#flux(ids_of_TS_transitions = nil) ⇒ Object

Returns the flux of the indicated TS transitions (all TS transitions, if no argument is given).



38
39
40
41
42
43
44
# File 'lib/y_petri/simulation/timed.rb', line 38

def flux ids_of_TS_transitions=nil
  tt = TS_transitions()
  return flux tt if ids_of_TS_transitions.nil?
  TS_transitions( ids_of_TS_transitions ).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.



126
127
128
129
# File 'lib/y_petri/simulation/timed.rb', line 126

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

#pflux(ids = nil, gap: 0, precision: 4) ⇒ Object

Pretty prints flux of the indicated TS transitions as 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.



57
58
59
# File 'lib/y_petri/simulation/timed.rb', line 57

def pflux ids=nil, gap: 0, precision: 4
  t_flux( ids ).pretty_print_numeric_values( gap: gap, precision: precision )
end

#reset!(**nn) ⇒ Object

Resets the timed simulation.



133
134
135
136
# File 'lib/y_petri/simulation/timed.rb', line 133

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.



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

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.



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

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.

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

on the target time

just_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


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/y_petri/simulation/timed.rb', line 102

def run_upto( target_time, final_step: :exact )
  case final_step
  when :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 :after then
    step! while time < target_time
  else
    fail ArgumentError, "Unrecognized :final_step option: #{final_step}"
  end
end

#settings(all = false) ⇒ Object

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



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

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

#t_flux(ids = nil) ⇒ Object

Flux of the indicated TS transitions (as hash with transition names as keys).



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

def t_flux ids=nil
  TS_transitions( ids ).names( true ) >> flux( ids )
end

#time_rangeObject

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



63
64
65
# File 'lib/y_petri/simulation/timed.rb', line 63

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.



119
120
121
122
# File 'lib/y_petri/simulation/timed.rb', line 119

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.



149
150
151
152
153
154
155
# File 'lib/y_petri/simulation/timed.rb', line 149

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