This repository has been archived by the owner on Apr 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
214 lines (157 loc) · 5.83 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require 'date'
class DemandRepository
@@next_id = 0
@@demands = []
def self.create(project, started_at, lead_time)
demand = Demand.new(project, started_at, lead_time)
@@demands << demand
return demand
end
def list
@@demands
end
def self.next_id
@@next_id += 1
end
def self.demand_sample
demands = []
1000.times do
demands << @@demands.sample
end
return demands
end
end
class Demand
attr_reader :id, :finished_at, :lead_time, :project
attr_accessor :started_at
def initialize(project, started_at, lead_time)
@project = project
@id = DemandRepository.next_id
@started_at = started_at
@lead_time = lead_time
#puts "#{@id}: #{@started_at} | #{@lead_time}"
end
def finish! day
@finished_at = day
@project.finish! self, day
end
end
class Kanban
attr_reader :backlog, :wip, :finished, :projects
def initialize(projects, lts, queue_discipline)
@projects = projects
@backlog = []
@wip = []
@finished = []
@projects.each do |project|
project.started.each {|demand| @wip << demand}
project.backlog.each {|demand| @backlog << demand}
end
@lts = lts
@wip_limit = @wip.count #I'm assuming initial wip == wip limits
@queue_discipline = queue_discipline;
# puts "Kanban Initialized:" + @backlog.collect {|x| x.project.id + x.id.to_s }.to_s + "" + @wip.collect {|x| x.project.id + x.id.to_s}.to_s + "" + @finished.collect {|x| x.project.id + x.id.to_s}.to_s
end
def finished?
@backlog.count == 0 && @wip.count == 0
end
def finish!(demand, day)
@wip.delete demand
@finished << demand
demand.finish! day
end
def start! day
started_at = day
while !self.finished?
unless day.saturday? || day.sunday?
#puts "Day #{day} Kanban:" + self.backlog.collect {|x| x.project.id + x.id.to_s }.to_s + "" + self.wip.collect {|x| x.project.id + x.id.to_s}.to_s + "" + self.finished.collect {|x| x.project.id + x.id.to_s}.to_s
self.run! day
end
day += 1
raise "600 limit days reached! Problem with your sim?" if (day - started_at).to_i > 599
end
end
def run!(day)
# run the board
@wip.each do |demand|
self.finish!(demand, day) if demand.started_at.mjd + demand.lead_time <= day.mjd
end
#report
#@projects.each {|project| puts "Day #{day} #{project.id}: [#{project.backlog.count} / #{project.started.count} / #{project.finished.count}]"}
#replenish
(@wip_limit - @wip.count).times {self.replenish! day}
end
protected
def replenish!(day)
project = @queue_discipline.next
if project
demand = project.pull!
demand.started_at = day
@backlog.delete demand
@wip << demand
end
end
end
class Project
attr_reader :id, :backlog, :started, :finished, :delivered_at
def initialize(project_spec, lead_time_sample, day)
@id = project_spec[0]
@backlog_size = project_spec[1].sample #simulate backlog size from range
@backlog = []
@started = []
@finished = []
# for demand on backlog use lead time sample
@backlog_size.times { @backlog << DemandRepository.create(self, nil, lead_time_sample.sample) }
# for started demand use the lead time sample "tail" (or current time + 1 day)
project_spec[2].each do |started_at|
days_on_cycle = day.mjd - started_at.mjd
lead_time_tail_sample = lead_time_sample.select {|i| i >= days_on_cycle}
lead_time = lead_time_tail_sample.sample || days_on_cycle + 1
@started << DemandRepository.create(self, started_at, lead_time)
end
#puts "Project #{@id}:" + self.backlog.collect {|x| x.project.id + x.id.to_s }.to_s + " | " + self.started.collect {|x| x.project.id + x.id.to_s}.to_s + " | " + self.finished.collect {|x| x.project.id + x.id.to_s}.to_s
end
def finish!(demand, day)
@started.delete demand
@finished << demand
@delivered_at = day if @backlog.count == 0 && @started.count == 0
end
def pull!
demand = @backlog.first
raise "Backlog is empty" unless demand
@backlog.delete demand
@started << demand
return demand
end
end
class MonteCarlo
def initialize(lead_time_sample, project_specs, queue_discipline_class)
@today = Date.parse(Time.now.strftime('%Y/%m/%d'))
@lead_time_sample = lead_time_sample.sort {|a,b| a <=> b}
@deliver_distribution = {}
@project_specs = project_specs
@queue_discipline_class = queue_discipline_class
end
def run!
1000.times do
projects = []
@project_specs.each {|project_spec| projects << Project.new(project_spec, @lead_time_sample, @today)}
kanban = Kanban.new(projects, @lead_time_sample, @queue_discipline_class.new(projects))
kanban.start! @today
kanban.projects.each do |project|
(@deliver_distribution[project.id] ||= []) << project.delivered_at
end
end
@deliver_distribution.each do |k, v|
str = "#{k};"
v.sort.each {|day| str += "#{day};"}
puts str
end
str = "GLTS;"
@lead_time_sample.each {|lt| str += "#{lt};"}
puts str
str = "OLTS;"
DemandRepository.demand_sample.each {|demand| str += "#{demand.lead_time};"}
puts str
end
end