-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplex.rb
141 lines (113 loc) · 2.29 KB
/
simplex.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
require 'compsci/simplex'
include CompSci
BENCH_IPS = true
BENCH_OBJECT_SPACE = true
SIMPLEX_PARAMS = [
# 1 (index 0)
[[1, 1],
[[2, 1],
[1, 2]],
[4, 3]],
[[3, 4],
[[1, 1],
[2, 1]],
[4, 5]],
[[2, -1],
[[1, 2],
[3, 2],],
[6, 12]],
[[60, 90, 300],
[[1, 1, 1],
[1, 3, 0],
[2, 0, 1]],
[600, 600, 900]],
# 5
[[70, 210, 140],
[[1, 1, 1],
[5, 4, 4],
[40, 20, 30]],
[100, 480, 3200]],
[[2, -1, 2],
[[2, 1, 0],
[1, 2, -2],
[0, 1, 2]],
[10, 20, 5]],
[[11, 16, 15],
[[1, 2, Rational(3, 2)],
[Rational(2, 3), Rational(2, 3), 1],
[Rational(1, 2), Rational(1, 3), Rational(1, 2)]],
[12_000, 4_600, 2_400]],
[[5, 4, 3],
[[2, 3, 1],
[4, 1, 2],
[3, 4, 2]],
[5, 11, 8]],
[[3, 2, -4],
[[1, 4, 0],
[2, 4,-2],
[1, 1,-2]],
[5, 6, 2]],
# 10
[[2, -1, 8],
[[2, -4, 6],
[-1, 3, 4],
[0, 0, 2]],
[3, 2, 1]],
[[100_000, 40_000, 18_000],
[[20, 6, 3],
[0, 1, 0],
[-1,-1, 1],
[-9, 1, 1]],
[182, 10, 0, 0]],
[[1, 2, 1, 2],
[[1, 0, 1, 0],
[0, 1, 0, 1],
[1, 1, 0, 0],
[0, 0, 1, 1]],
[1, 4, 2, 2]],
[[10, -57, -9, -24],
[[0.5, -5.5, -2.5, 9],
[0.5, -1.5, -0.5, 1],
[ 1, 0, 0, 0]],
[0, 0, 1]],
# 14 (index 13)
[[25, 20],
[[20, 12],
[1, 1]],
[1800, 8*15]],
]
def new_simplices
SIMPLEX_PARAMS.map { |c, a, b| Simplex.new(c, a, b) }
end
if BENCH_IPS
require 'benchmark/ips'
Benchmark.ips do |b|
b.config time: 3, warmup: 0.5
b.report("Simplex init") {
new_simplices
}
b.report("init, solve") {
new_simplices.each { |s| s.solution }
}
#b.report("Simplex Matrix") {
#}
b.compare!
end
end
if BENCH_OBJECT_SPACE
require 'compsci/timer'
require 'objspace'
def disp_memsize(var, label = '')
"memsize(%s): %i" % [label, ObjectSpace.memsize_of(var)]
end
simplices = SIMPLEX_PARAMS.map { |c, a, b|
Simplex.new(c, a, b)
}
puts "SIMPLEX_PARAMS.size = #{SIMPLEX_PARAMS.size}"
puts "simplices.size = #{simplices.size}"
puts disp_memsize SIMPLEX_PARAMS, 'SIMPLEX_PARAMS'
puts disp_memsize simplices, 'simplices'
results = simplices.map { |s| s.solution }
puts disp_memsize simplices, 'simplices after solving'
puts disp_memsize results, 'results'
end