Class: Simulator::Run
- Inherits:
-
Object
- Object
- Simulator::Run
- Defined in:
- lib/simulator/run.rb
Overview
A run of a model. It has its own context
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#periods ⇒ Object
readonly
Returns the value of attribute periods.
Instance Method Summary collapse
-
#current_context ⇒ Object
Returns the current_context, that is the variable context of the last period.
- #data ⇒ Object
- #evaluate ⇒ Object
- #increment_period ⇒ Object
-
#initialize(model) ⇒ Run
constructor
A new instance of Run.
-
#period_count ⇒ Object
The number of periods so far.
- #set(var_hash) ⇒ Object
- #step(times = 1) ⇒ Object
- #summary ⇒ Object
- #variable_value(var_name) ⇒ Object
- #variables ⇒ Object
- #variables_csv ⇒ Object
Constructor Details
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
4 5 6 |
# File 'lib/simulator/run.rb', line 4 def model @model end |
#periods ⇒ Object (readonly)
Returns the value of attribute periods.
5 6 7 |
# File 'lib/simulator/run.rb', line 5 def periods @periods end |
Instance Method Details
#current_context ⇒ Object
Returns the current_context, that is the variable context of the last period.
43 44 45 |
# File 'lib/simulator/run.rb', line 43 def current_context @periods.last.context end |
#data ⇒ Object
90 91 92 93 94 |
# File 'lib/simulator/run.rb', line 90 def data data = Data.new data.periods = @periods data end |
#evaluate ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/simulator/run.rb', line 13 def evaluate # evaluate the equations in the current context @model.equations.each do | eqtn | result = eqtn.evaluate_in current_context, @periods var = eqtn.variable bound_var = current_context.get(var.name) bound_var.value = result end end |
#increment_period ⇒ Object
23 24 25 26 27 |
# File 'lib/simulator/run.rb', line 23 def increment_period p = Period.new self, @periods.dup @periods << p p end |
#period_count ⇒ Object
The number of periods so far. All runs have at least 1 period.
37 38 39 |
# File 'lib/simulator/run.rb', line 37 def period_count @periods.length end |
#set(var_hash) ⇒ Object
51 52 53 |
# File 'lib/simulator/run.rb', line 51 def set(var_hash) current_context.set var_hash end |
#step(times = 1) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/simulator/run.rb', line 29 def step(times=1) times.times do evaluate increment_period end end |
#summary ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/simulator/run.rb', line 59 def summary var_list = variables.sort_by(&:name).collect do |v| "\t#{v.name} = #{v.value}" end.join("\n") model_summary = %Q{Model "#{@model.name}"} period_summary = "Period #{period_count}" "#{model_summary}\n#{period_summary}\nVariables:\n#{var_list}" end |
#variable_value(var_name) ⇒ Object
55 56 57 |
# File 'lib/simulator/run.rb', line 55 def variable_value(var_name) current_context.get(var_name).value end |
#variables ⇒ Object
47 48 49 |
# File 'lib/simulator/run.rb', line 47 def variables current_context.variables end |
#variables_csv ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/simulator/run.rb', line 69 def variables_csv vars = variables.sort_by(&:name) var_names = vars.collect(&:name) header = "'Period',#{var_names.collect{|n| "'#{n}'"}.join(',')}" rows = [] @periods.each_index do |index| period = @periods[index] context = period.context var_values = var_names.collect do | var_name | var = context.get var_name var.value end.join(',') rows << "#{index},#{var_values}" end "#{header}\n#{rows.join("\n")}" end |