-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch15.rb
82 lines (59 loc) · 1.39 KB
/
ch15.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
require 'distribution'
require_relative './feedback'
include Distribution::Shorthand
class AbstractServerPool < Component
def initialize(n, server, incoming_load)
@n = n
@queue = 0
@server = server
@incoming_load = incoming_load
end
def work(u)
@n = [0, u.to_i]
completed = 0
@n.times do |_|
completed += @server.call
completed = @queue if completed >= @queue
end
@queue -= completed
completed
end
def monitoring
"#{@n} #{@queue}"
end
end
class ServerPool < AbstractServerPool
def work(u)
incoming_load = @incoming_load.to_f # Additions to the queue.
@queue = incoming_load
return 1 if load == 0 # No work: 100% percent completion rate
completed = super.to_f
completed / incoming_load # completion rate
end
end
load_queue = -> { norm_rng(mean: 1000, stddev: 5) }
consume_queue = -> { 100 * beta_rng(a: 20, b: 2) }
class SpecialController < Component
def initialize(period1, period2)
@period1 = period1
@period2 = period2
@t = 0
end
def work(u)
if u > 0
@t = @period1
return 1
end
@t -= 1 # At this point: u <= 0 guaranteed!
if @t == 0
@t = @period2
return -1
end
0
end
end
setpoint = -> t { 1.0 }
DT = 1
p = ServerPool(0, consume_queue, load_queue)
c = SpecialController(100, 10)
Feedback.closed_loop(setpoint, c, p, actuator = Integrator.new)