simulator 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +16 -0
- data/LICENSE +22 -0
- data/README.md +67 -0
- data/RELEASE.md +31 -0
- data/Rakefile +2 -0
- data/examples/drop/Gemfile +5 -0
- data/examples/drop/drop.png +0 -0
- data/examples/drop/drop.rb +70 -0
- data/examples/mortage/Gemfile +5 -0
- data/examples/mortage/mortgage.png +0 -0
- data/examples/mortage/mortgage.rb +91 -0
- data/features/equations.feature +17 -0
- data/features/physics.feature +22 -0
- data/features/savings_simulation.feature +17 -0
- data/features/step_definitions/equations_steps.rb +52 -0
- data/features/step_definitions/savings_simulation_steps.rb +40 -0
- data/lib/simulator.rb +14 -0
- data/lib/simulator/bound_variable.rb +18 -0
- data/lib/simulator/data.rb +23 -0
- data/lib/simulator/equation.rb +16 -0
- data/lib/simulator/model.rb +43 -0
- data/lib/simulator/period.rb +28 -0
- data/lib/simulator/run.rb +95 -0
- data/lib/simulator/sandbox.rb +31 -0
- data/lib/simulator/variable.rb +11 -0
- data/lib/simulator/variable_context.rb +66 -0
- data/lib/simulator/version.rb +3 -0
- data/simulator.gemspec +20 -0
- data/spec/beer_spec.rb +91 -0
- data/spec/context_spec.rb +38 -0
- data/spec/equation_spec.rb +24 -0
- data/spec/interest_spec.rb +79 -0
- data/spec/model_spec.rb +33 -0
- data/spec/period_spec.rb +28 -0
- data/spec/run_spec.rb +44 -0
- data/spec/sandbox_spec.rb +24 -0
- metadata +128 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'simulator'
|
2
|
+
include Simulator
|
3
|
+
|
4
|
+
describe "a variable context" do
|
5
|
+
before :each do
|
6
|
+
@context = VariableContext.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "can contain variables explicitly" do
|
10
|
+
@context.add_variables Variable.new(:x), Variable.new(:y)
|
11
|
+
@context.set(x: 3, y: 4).should eq x: 3, y:4
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can bind variables implicitly" do
|
15
|
+
@context.add x:3, y: 4
|
16
|
+
@context.get(:x).value.should eq 3
|
17
|
+
@context.get(:y).value.should eq 4
|
18
|
+
end
|
19
|
+
|
20
|
+
it "binds variables to values using BoundVariables" do
|
21
|
+
@context.add_variables Variable.new(:x), Variable.new(:y)
|
22
|
+
@context.set x: 3, y: 4
|
23
|
+
|
24
|
+
@context.get(:x).value.should eq 3
|
25
|
+
@context.get(:y).value.should eq 4
|
26
|
+
end
|
27
|
+
|
28
|
+
it "can be cloned while maintaining variable instances" do
|
29
|
+
@context.add_variables Variable.new(:x), Variable.new(:y)
|
30
|
+
@context.set x: 3, y: 4
|
31
|
+
copy = @context.clone
|
32
|
+
copy.set x: 10
|
33
|
+
|
34
|
+
@context.get(:x).value.should be 3
|
35
|
+
copy.get(:x).value.should be 10
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'simulator'
|
2
|
+
include Simulator
|
3
|
+
|
4
|
+
describe "an equation" do
|
5
|
+
before :each do
|
6
|
+
@context = VariableContext.new
|
7
|
+
@var_x = Variable.new :x
|
8
|
+
@var_z = Variable.new :z
|
9
|
+
|
10
|
+
# add new variables to the context
|
11
|
+
@context.add_variables @var_x, @var_z
|
12
|
+
|
13
|
+
# set the values of the variables in this context
|
14
|
+
@context.set x: 3, z: 4
|
15
|
+
end
|
16
|
+
it "is bound to a variable" do
|
17
|
+
@context.get(:x).value.should be 3
|
18
|
+
eqtn = Equation.new @var_x do
|
19
|
+
99
|
20
|
+
end
|
21
|
+
eqtn.evaluate_in(@context).should be 99
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'simulator'
|
2
|
+
include Simulator
|
3
|
+
|
4
|
+
describe "a simple interest model" do
|
5
|
+
before :each do
|
6
|
+
@model = Model.new do
|
7
|
+
name = "Interest Model"
|
8
|
+
|
9
|
+
var :balance
|
10
|
+
var :interest
|
11
|
+
|
12
|
+
eqtn :balance do
|
13
|
+
balance + balance * interest
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
@run = Run.new @model
|
18
|
+
end
|
19
|
+
|
20
|
+
it "can step a run" do
|
21
|
+
@run.period_count.should eq 1
|
22
|
+
@run.set balance: 100, interest: 0.1
|
23
|
+
|
24
|
+
@run.step
|
25
|
+
@run.period_count.should eq 2
|
26
|
+
@run.variable_value(:balance).should eq 110
|
27
|
+
@run.step
|
28
|
+
|
29
|
+
@run.period_count.should eq 3
|
30
|
+
@run.variable_value(:balance).should eq 121
|
31
|
+
|
32
|
+
@run.set interest: 0.5
|
33
|
+
@run.step
|
34
|
+
|
35
|
+
@run.period_count.should eq 4
|
36
|
+
@run.variable_value(:balance).should eq (181.5)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
describe "a complex interest model" do
|
42
|
+
before :each do
|
43
|
+
@model = Model.new do
|
44
|
+
name = "Interest Model"
|
45
|
+
|
46
|
+
var :balance
|
47
|
+
var :interest
|
48
|
+
|
49
|
+
eqtn :balance do
|
50
|
+
balance + balance * interest
|
51
|
+
end
|
52
|
+
|
53
|
+
eqtn :interest do
|
54
|
+
# Add 1% each evaluation
|
55
|
+
interest + 0.01
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
@run = Run.new @model
|
60
|
+
end
|
61
|
+
|
62
|
+
it "can step a run" do
|
63
|
+
@run.period_count.should eq 1
|
64
|
+
@run.set balance: 100, interest: 0.1
|
65
|
+
|
66
|
+
@run.step
|
67
|
+
@run.period_count.should eq 2
|
68
|
+
@run.variable_value(:balance).should eq 110
|
69
|
+
@run.variable_value(:interest).should eq 0.11
|
70
|
+
@run.step
|
71
|
+
|
72
|
+
@run.period_count.should eq 3
|
73
|
+
@run.variable_value(:balance).should eq 122.1
|
74
|
+
@run.variable_value(:interest).should eq 0.12
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
|
data/spec/model_spec.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'simulator'
|
2
|
+
include Simulator
|
3
|
+
|
4
|
+
describe "a model" do
|
5
|
+
before :each do
|
6
|
+
@model = Model.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "has equations" do
|
10
|
+
@model.equations.nil?.should be false
|
11
|
+
end
|
12
|
+
|
13
|
+
it "has variables" do
|
14
|
+
@model.variables.nil?.should be false
|
15
|
+
end
|
16
|
+
|
17
|
+
it "accepts new variables" do
|
18
|
+
@model.variables.length.should be 0
|
19
|
+
x = Variable.new :x
|
20
|
+
@model.add_variable x
|
21
|
+
@model.variables.length.should be 1
|
22
|
+
end
|
23
|
+
|
24
|
+
it "accepts new equations" do
|
25
|
+
@model.equations.length.should be 0
|
26
|
+
e = Equation.new nil do
|
27
|
+
1
|
28
|
+
end
|
29
|
+
@model.add_equation e
|
30
|
+
@model.equations.length.should be 1
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
data/spec/period_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'simulator'
|
2
|
+
include Simulator
|
3
|
+
|
4
|
+
describe "a period" do
|
5
|
+
before :each do
|
6
|
+
#model = Model.new
|
7
|
+
#[:x, :z].each do |v|
|
8
|
+
#model.add_variable Variable.new v
|
9
|
+
#end
|
10
|
+
#run = Run.new model
|
11
|
+
#@period = run.periods.first
|
12
|
+
|
13
|
+
periods = []
|
14
|
+
3.times do
|
15
|
+
periods << Period.new(nil, periods.clone)
|
16
|
+
end
|
17
|
+
@period = Period.new nil, periods
|
18
|
+
end
|
19
|
+
it "has a variable context" do
|
20
|
+
@period.context.should_not be nil
|
21
|
+
end
|
22
|
+
it "knows about previous periods" do
|
23
|
+
@period.previous_periods.should_not be_empty
|
24
|
+
@period.previous.previous_periods.length.should be (@period.previous_periods.length - 1)
|
25
|
+
@period.first.should be @period.previous_periods.first
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
data/spec/run_spec.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'simulator'
|
2
|
+
include Simulator
|
3
|
+
|
4
|
+
describe "a run" do
|
5
|
+
before :each do
|
6
|
+
@model = Model.new
|
7
|
+
var_x = Variable.new :x
|
8
|
+
eqtn = Equation.new var_x do
|
9
|
+
x + 1
|
10
|
+
end
|
11
|
+
@model.add_equation eqtn
|
12
|
+
@model.add_variable var_x
|
13
|
+
@run = Run.new @model
|
14
|
+
end
|
15
|
+
|
16
|
+
it "has bound variables" do
|
17
|
+
@run.variables.first.class.should be BoundVariable
|
18
|
+
end
|
19
|
+
|
20
|
+
it "is not affected by setting variables in a different run" do
|
21
|
+
run2 = Run.new @model
|
22
|
+
|
23
|
+
@run.set x: 1
|
24
|
+
run2.set x: 2
|
25
|
+
|
26
|
+
@run.variable_value(:x).should be 1
|
27
|
+
run2.variable_value(:x).should be 2
|
28
|
+
end
|
29
|
+
|
30
|
+
it "can evaluate model equations in its current context" do
|
31
|
+
@run.set x: 3
|
32
|
+
@run.evaluate
|
33
|
+
@run.variable_value(:x).should be 4
|
34
|
+
@run.evaluate
|
35
|
+
@run.variable_value(:x).should be 5
|
36
|
+
end
|
37
|
+
|
38
|
+
it "can increment periods" do
|
39
|
+
@run.periods.length.should be 1
|
40
|
+
@run.increment_period
|
41
|
+
@run.periods.length.should be 2
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'simulator'
|
2
|
+
include Simulator
|
3
|
+
|
4
|
+
describe "an evaluation sandbox" do
|
5
|
+
before :each do
|
6
|
+
@context = VariableContext.new
|
7
|
+
@context.add x: 3, z: 4
|
8
|
+
@sandbox = Sandbox.new @context
|
9
|
+
end
|
10
|
+
it "can evaluate variables" do
|
11
|
+
@sandbox.instance_eval do
|
12
|
+
x
|
13
|
+
end.should eq 3
|
14
|
+
@sandbox.instance_eval do
|
15
|
+
z
|
16
|
+
end.should eq 4
|
17
|
+
end
|
18
|
+
it "is used to evaluate expressions" do
|
19
|
+
@sandbox.instance_eval do
|
20
|
+
x * z
|
21
|
+
end.should eq 12
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simulator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jamie Ly
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: cucumber
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Use to simulate discrete time models. See the examples directory for
|
47
|
+
examples.
|
48
|
+
email:
|
49
|
+
- me@jamie.ly
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE
|
57
|
+
- README.md
|
58
|
+
- RELEASE.md
|
59
|
+
- Rakefile
|
60
|
+
- examples/drop/Gemfile
|
61
|
+
- examples/drop/drop.png
|
62
|
+
- examples/drop/drop.rb
|
63
|
+
- examples/mortage/Gemfile
|
64
|
+
- examples/mortage/mortgage.png
|
65
|
+
- examples/mortage/mortgage.rb
|
66
|
+
- features/equations.feature
|
67
|
+
- features/physics.feature
|
68
|
+
- features/savings_simulation.feature
|
69
|
+
- features/step_definitions/equations_steps.rb
|
70
|
+
- features/step_definitions/savings_simulation_steps.rb
|
71
|
+
- lib/simulator.rb
|
72
|
+
- lib/simulator/bound_variable.rb
|
73
|
+
- lib/simulator/data.rb
|
74
|
+
- lib/simulator/equation.rb
|
75
|
+
- lib/simulator/model.rb
|
76
|
+
- lib/simulator/period.rb
|
77
|
+
- lib/simulator/run.rb
|
78
|
+
- lib/simulator/sandbox.rb
|
79
|
+
- lib/simulator/variable.rb
|
80
|
+
- lib/simulator/variable_context.rb
|
81
|
+
- lib/simulator/version.rb
|
82
|
+
- simulator.gemspec
|
83
|
+
- spec/beer_spec.rb
|
84
|
+
- spec/context_spec.rb
|
85
|
+
- spec/equation_spec.rb
|
86
|
+
- spec/interest_spec.rb
|
87
|
+
- spec/model_spec.rb
|
88
|
+
- spec/period_spec.rb
|
89
|
+
- spec/run_spec.rb
|
90
|
+
- spec/sandbox_spec.rb
|
91
|
+
homepage: http://github.com/jamiely/simulator
|
92
|
+
licenses: []
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 1.8.23
|
112
|
+
signing_key:
|
113
|
+
specification_version: 3
|
114
|
+
summary: Use to simulate discrete time models.
|
115
|
+
test_files:
|
116
|
+
- features/equations.feature
|
117
|
+
- features/physics.feature
|
118
|
+
- features/savings_simulation.feature
|
119
|
+
- features/step_definitions/equations_steps.rb
|
120
|
+
- features/step_definitions/savings_simulation_steps.rb
|
121
|
+
- spec/beer_spec.rb
|
122
|
+
- spec/context_spec.rb
|
123
|
+
- spec/equation_spec.rb
|
124
|
+
- spec/interest_spec.rb
|
125
|
+
- spec/model_spec.rb
|
126
|
+
- spec/period_spec.rb
|
127
|
+
- spec/run_spec.rb
|
128
|
+
- spec/sandbox_spec.rb
|