Class: Eventsims::Simulate

Inherits:
Randomsim show all
Defined in:
lib/eventsims/simevent.rb

Instance Method Summary collapse

Methods inherited from Randomsim

#arrival, #custspend, #idle, #intarrival, #queuewait, #servbegin, #servend, #service

Constructor Details

#initialize(*args) ⇒ Simulate

Returns a new instance of Simulate.



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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/eventsims/simevent.rb', line 160

def initialize(*args)
	''' Initialisation '''

	stop, amount = 10, (2..20).step(1).to_a.sample
	@_intarrival, @_service = [], []

	if args.size == 1
		@_intarrival = args[0]
		raise "Argument must be a list" unless args[0].is_a?(Array)	
		(args[0].size).times{@_service << (1..stop).step(1).to_a.sample}	

		args[0].each{|y| 
			raise "Only numbers allowed in Arrays" if y.is_a?(String) } 
		
	elsif args.size == 2
		#checks if both arguments are arrays
		unless args.all?{|x| x.is_a?(Array)	}
			raise "Argument one and two must be Arrays"  
		end
		@_intarrival = args[0]
		@_service = args[1]
	
		args[0].each{|y| 
			raise "Only numbers allowed in Arrays" if y.is_a?(String) } 
		args[1].each{|y| 
			raise "Only numbers allowed in Arrays" if y.is_a?(String) } 
		
		#Check length of both lists and throw error if not equal
		if args[0].size != args[1].size
			raise "List arguments must be of equal length"
		end

	else
		raise "You must supply one or two Array arguments"
	end

	# If first value less than 0 set it to zero
	@_intarrival[0] = 0 if @_intarrival[0] < 0
		
	# Required variables and input error handling
	@_preservstart = []; @_preservstart << args[0][0]
	@_preservstart[0] = 0 if @_preservstart[0] < 0

	# Other variable declaration
	@_arrival, @_servstart, @_queue = [], [], []
	@_servend, @_custspend, @_idle = [], [], [0]

	# Makes display less annoying
	def makenice(thelist)
		temp = []
		thelist.each{|i| if i.is_a?(Integer)
			temp << i
			else
				temp << i.round(4) 
			end  }
		thelist = temp
		return thelist
	end

	def getarrive_()
		'''Returns arrival time'''
		increase = 0
		@_intarrival.each{|i| increase +=i
			@_arrival << increase  }
		return @_arrival
	end

	def servbegins_()
		'''Returns time when service begin'''
		increase, i = 0, 0
		while i < @_service.size
			increase += @_service[i]
			@_preservstart << increase
			i+=1
		end
		@_preservstart.pop()

		return @_preservstart
	end

	#populate @_servend with values. just to get same size
	@_intarrival.each{|i| @_servend << i}

	# #Please maintain order if you are editing the code!
	# # Calling functions
	getarrive_()	#Returns arrival time
	servbegins_()	#Returns time when servce begin

	def get_servend_()
		'''Retuns time when service ends'''
		x = 0; while x < ((@_preservstart).size)
			@_servend[x] = @_preservstart[x] + @_service[x]
			s = 1; while s < (@_preservstart).size
				if @_preservstart[s] < (maximum = @_arrival[s] > @_servend[x]? @_arrival[s] : @_servend[x])
					@_preservstart[s] = (maximum)
				end
				s+=1 
			end
			x+=1
		end

		return @_servend
	end

	# Method used to calculate the rest of the data like
	#time when service begins
	#wait time in queue, (_queue)
	#time customer spent in the system (_custspend)
	def otherresults(list1, list2, list3)
		''' Stores and return the value of (list2 - list3) in list1 '''
		x = 0; while x < list2.size
			list1 << (list2[x] - list3[x]) 
			x+=1
		end
		list1[0] = 0 if list1[0] < 0	
	end

	def idletime_()
		'''Returns the idle time of server'''
		x,y = 0,1
		while y < @_servend.size
			(@_idle) << (@_servstart[y] - @_servend[x])
			x+=1; y+=1
		end
		return @_idle
	end

	# Calling other methods
	get_servend_()
	otherresults(@_servstart, @_servend, @_service) 
	otherresults(@_queue, @_servstart, @_arrival) 
	otherresults(@_custspend, @_servend, @_arrival)
	idletime_()
end

Instance Method Details

#get_servend_Object

Returns time when servce begin



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/eventsims/simevent.rb', line 248

def get_servend_()
	'''Retuns time when service ends'''
	x = 0; while x < ((@_preservstart).size)
		@_servend[x] = @_preservstart[x] + @_service[x]
		s = 1; while s < (@_preservstart).size
			if @_preservstart[s] < (maximum = @_arrival[s] > @_servend[x]? @_arrival[s] : @_servend[x])
				@_preservstart[s] = (maximum)
			end
			s+=1 
		end
		x+=1
	end

	return @_servend
end

#getarrive_Object



219
220
221
222
223
224
225
# File 'lib/eventsims/simevent.rb', line 219

def getarrive_()
	'''Returns arrival time'''
	increase = 0
	@_intarrival.each{|i| increase +=i
		@_arrival << increase  }
	return @_arrival
end

#idletime_Object



277
278
279
280
281
282
283
284
285
# File 'lib/eventsims/simevent.rb', line 277

def idletime_()
	'''Returns the idle time of server'''
	x,y = 0,1
	while y < @_servend.size
		(@_idle) << (@_servstart[y] - @_servend[x])
		x+=1; y+=1
	end
	return @_idle
end

#makenice(thelist) ⇒ Object

Makes display less annoying



208
209
210
211
212
213
214
215
216
217
# File 'lib/eventsims/simevent.rb', line 208

def makenice(thelist)
	temp = []
	thelist.each{|i| if i.is_a?(Integer)
		temp << i
		else
			temp << i.round(4) 
		end  }
	thelist = temp
	return thelist
end

#otherresults(list1, list2, list3) ⇒ Object

Method used to calculate the rest of the data like time when service begins wait time in queue, (_queue) time customer spent in the system (_custspend)



268
269
270
271
272
273
274
275
# File 'lib/eventsims/simevent.rb', line 268

def otherresults(list1, list2, list3)
	''' Stores and return the value of (list2 - list3) in list1 '''
	x = 0; while x < list2.size
		list1 << (list2[x] - list3[x]) 
		x+=1
	end
	list1[0] = 0 if list1[0] < 0	
end

#servbegins_Object



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/eventsims/simevent.rb', line 227

def servbegins_()
	'''Returns time when service begin'''
	increase, i = 0, 0
	while i < @_service.size
		increase += @_service[i]
		@_preservstart << increase
		i+=1
	end
	@_preservstart.pop()

	return @_preservstart
end