Debian Codegraph @

tests/test_optimizers/test_knapsack_facility_location.py

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 import scipy
2 import numpy
3
4 try:
5 import cupy
6 except:
7 import numpy as cupy
8
9 from apricot import FacilityLocationSelection
10 from apricot.optimizers import *
11
12 from sklearn.datasets import load_digits
13 from sklearn.metrics import pairwise_distances
14
15
16 from nose.tools import assert_less_equal
17 from numpy.testing import assert_almost_equal
18 from numpy.testing import assert_array_equal
19 from numpy.testing import assert_array_almost_equal
20
21 # print("[" + ", ".join(map(str, model.ranking)) + "]")
22 # print("[" + ", ".join([str(round(gain, 4)) for gain in model.gains]) + "]")
23
24 numpy.random.seed(0)
25
26 digits_data = load_digits()
27 X_digits = digits_data.data[:250]
28 X_digits_costs = numpy.abs(numpy.random.randn(250))
29
30 X_digits_sparse = scipy.sparse.csr_matrix(X_digits)
31 X_digits_cupy = cupy.array(X_digits)
32
33 digits_ranking = [96, 179, 219, 216, 26, 56, 206, 54, 225, 187, 163, 79, 192,
34 17, 35, 8, 169, 64, 242, 69, 133, 195, 117, 7, 227, 87, 125, 142, 150, 92,
35 154, 137, 27, 71, 10, 233, 114, 98, 201, 209, 30, 13, 226, 243, 57, 67, 1,
36 141, 211, 222, 14, 208, 31, 238, 138, 51, 136, 9, 74, 122, 45, 152, 61,
37 18, 34, 191, 215, 49, 39, 160, 38, 221, 143, 180, 12, 131, 124, 52, 99,
38 78, 129, 59, 106, 231, 94, 203, 90, 120, 126]
39
40 digits_gains = [132.3769, 19.3468, 14.247, 4.3593, 7.0206, 8.4383, 2.6395,
41 2.6087, 3.4037, 2.2681, 2.8624, 1.3601, 0.5692, 2.7581, 2.0511, 1.3346,
42 3.6155, 1.3742, 1.0221, 0.3618, 0.1726, 0.8898, 0.7893, 0.7392, 0.9948,
43 0.6893, 0.51, 0.5096, 0.1732, 0.506, 0.2326, 0.4857, 0.4344, 0.2623,
44 0.2858, 0.7386, 0.4207, 0.1898, 0.3478, 0.1395, 0.2196, 0.168, 0.2155,
45 0.3933, 0.3766, 0.5458, 0.4164, 0.4137, 0.266, 0.37, 0.3328, 0.1958,
46 0.2645, 0.2956, 0.2661, 0.2467, 0.3663, 0.252, 0.2453, 0.1533, 0.2195,
47 0.371, 0.1653, 0.1424, 0.1558, 0.1183, 0.1771, 0.0917, 0.13, 0.2125,
48 0.1625, 0.1436, 0.263, 0.1454, 0.3037, 0.2128, 0.2727, 0.2004, 0.1544,
49 0.1161, 0.1492, 0.1326, 0.1472, 0.1741, 0.1245, 0.2288, 0.1378, 0.1216,
50 0.1267]
51
52 digits_greedi_ranking = [96, 179, 216, 219, 54, 26, 56, 206, 225, 187, 163,
53 79, 192, 69, 92, 35, 7, 133, 17, 242, 195, 227, 64, 125, 117, 8, 169, 142,
54 150, 27, 87, 10, 137, 243, 30, 71, 154, 226, 13, 209, 233, 211, 98, 114,
55 57, 9, 1, 74, 131, 67, 51, 122, 31, 222, 18, 191, 215, 49, 39, 208, 201,
56 14, 244, 12, 65, 99, 78, 38, 129, 59, 231, 203, 238, 138, 106, 45, 120,
57 34, 152, 15, 52, 94, 134, 221, 77, 61, 217, 232, 241, 141]
58
59 digits_greedi_gains = [132.3769, 19.3468, 5.5078, 13.0984, 3.052, 7.0012,
60 8.0181, 2.6359, 3.4037, 2.2681, 2.8624, 1.3601, 0.5692, 0.4066, 2.6452,
61 2.0193, 1.2513, 0.1726, 2.233, 1.6882, 0.8898, 1.0965, 1.2725, 0.4306,
62 0.7893, 0.363, 1.8682, 0.5096, 0.1663, 0.4344, 0.6893, 0.2858, 0.4857,
63 0.3933, 0.2196, 0.2623, 0.2326, 0.2215, 0.2098, 0.1395, 0.709, 0.266,
64 0.1715, 0.4207, 0.3766, 0.252, 0.4164, 0.2799, 0.3581, 0.5458, 0.2467,
65 0.1533, 0.2645, 0.37, 0.1424, 0.1183, 0.1771, 0.0917, 0.13, 0.1958,
66 0.3132, 0.3328, 0.13, 0.3037, 0.2199, 0.1544, 0.1161, 0.1625, 0.1492,
67 0.1326, 0.1741, 0.2288, 0.2956, 0.2661, 0.1472, 0.2195, 0.1216, 0.1558,
68 0.367, 0.0949, 0.231, 0.1245, 0.162, 0.1436, 0.1452, 0.1347, 0.0929,
69 0.1611, 0.0928, 0.2684]
70
71 digits_approx_ranking = [96, 179, 219, 216, 26, 56, 206, 54, 225, 187, 163,
72 79, 192, 17, 92, 35, 242, 64, 69, 169, 195, 133, 117, 7, 227, 87, 8, 142,
73 125, 154, 150, 137, 27, 71, 10, 233, 114, 98, 13, 201, 209, 30, 226, 243,
74 57, 67, 1, 141, 211, 222, 14, 31, 208, 238, 138, 51, 136, 9, 74, 122, 45,
75 152, 61, 18, 180, 34, 191, 215, 49, 39, 160, 38, 221, 143, 12, 131, 124,
76 52, 99, 203, 78, 129, 59, 106, 231, 94, 90, 120, 126]
77
78 digits_approx_gains = [132.3769, 19.3468, 14.247, 4.3593, 7.0206, 8.4383,
79 2.6395, 2.6087, 3.4037, 2.2681, 2.8624, 1.3601, 0.5692, 2.7581, 2.6452,
80 2.0511, 1.6882, 1.3742, 0.3618, 1.9058, 0.8898, 0.1726, 0.7893, 0.7392,
81 0.9948, 0.6893, 0.363, 0.5096, 0.393, 0.2326, 0.1663, 0.4857, 0.4344,
82 0.2623, 0.2858, 0.7386, 0.4207, 0.1898, 0.168, 0.3478, 0.1395, 0.2196,
83 0.2155, 0.3933, 0.3766, 0.5458, 0.4164, 0.4137, 0.266, 0.37, 0.3328,
84 0.2645, 0.1958, 0.2956, 0.2661, 0.2467, 0.3663, 0.252, 0.2453, 0.1533,
85 0.2195, 0.371, 0.1653, 0.1424, 0.1454, 0.1558, 0.1183, 0.1771, 0.0917,
86 0.13, 0.2125, 0.1625, 0.1436, 0.263, 0.3037, 0.2128, 0.2727, 0.2004,
87 0.1544, 0.2288, 0.1161, 0.1492, 0.1326, 0.1472, 0.1741, 0.1245, 0.1378,
88 0.1216, 0.1267]
89
90 digits_stochastic_ranking = [225, 104, 60, 12, 213, 142, 45, 236, 53, 144,
91 30, 226, 202, 232, 140, 95, 2, 150, 240, 75, 62, 185, 135, 37, 168, 61,
92 241, 212, 121, 124]
93
94 digits_stochastic_gains = [2023.6035, 12.477, 28.4395, 2.1767, 10.596, 31.2631,
95 1.8466, 3.2723, 1.4403, 0.266, 33.6989, 3.1455, 2.548, 2.5224, 0.1445,
96 0.5862, 2.4432, 2.1424, 0.4515, 0.4854, 0.8598, 0.1719, 8.4095, 0.8081,
97 1.938, 3.427, 2.0227, 1.1632, 0.7072, 1.5839]
98
99 digits_sample_ranking = [179, 216, 206, 219, 26, 56, 54, 163, 187, 79, 242, 17,
100 35, 64, 192, 169, 69, 133, 195, 117, 7, 87, 227, 171, 8, 125, 142, 209,
101 137, 27, 149, 71, 10, 18, 114, 13, 201, 30, 226, 98, 243, 57, 67, 1, 141,
102 211, 222, 39, 14, 208, 31, 138, 38, 228, 51, 136, 74, 9, 129, 143, 61,
103 180, 34, 191, 215, 49, 160, 221, 233, 238, 12, 131, 124, 52, 78, 59, 106,
104 99, 94, 90, 120, 196, 50, 126]
105
106 digits_sample_gains = [135.1415, 12.9734, 9.23, 12.6779, 6.9931, 8.4351, 2.624,
107 4.6758, 2.4125, 1.3601, 2.2744, 3.2146, 2.0511, 2.3048, 0.4903, 3.921,
108 0.4243, 0.1726, 0.8898, 0.7893, 0.7392, 0.8592, 1.0594, 1.368, 0.363,
109 0.51, 0.5096, 0.2371, 0.4857, 0.4344, 0.9881, 0.2623, 0.2858, 0.5983,
110 0.4506, 0.1896, 0.3478, 0.2196, 0.2215, 0.1715, 0.3933, 0.3766, 0.5458,
111 0.4227, 0.4137, 0.266, 0.37, 0.2294, 0.3328, 0.1958, 0.2645, 0.2661,
112 0.259, 0.3979, 0.2467, 0.3663, 0.2453, 0.2369, 0.2239, 0.3055, 0.1653,
113 0.1608, 0.1558, 0.1183, 0.1771, 0.0917, 0.2125, 0.1436, 0.1724, 0.1718,
114 0.3037, 0.2128, 0.2727, 0.2004, 0.1161, 0.1326, 0.1472, 0.141, 0.1245,
115 0.1378, 0.1216, 0.2449, 0.2774, 0.1281]
116
117 digits_modular_ranking = [96, 179, 216, 206, 54, 133, 219, 26, 192, 187, 69,
118 79, 225, 150, 56, 209, 8, 154, 242, 163, 13, 98, 71, 10, 125, 35, 226,
119 195, 117, 142, 30, 87, 64, 17, 7, 27, 92, 137, 49, 114, 227, 229, 208,
120 201, 244, 122, 39, 191, 243, 171, 221, 18, 138, 61, 180, 57, 211, 78,
121 169, 94, 59, 120, 34, 74, 15, 1, 217, 200, 55, 99, 129, 126, 38, 9, 233,
122 90, 65, 51, 14, 238, 178, 159, 149, 106, 241, 213, 82, 45, 31, 160, 222,
123 215, 141, 52]
124
125 digits_modular_gains = [132.3769, 19.3468, 5.5078, 3.8928, 3.7407, 7.4877,
126 4.418, 6.5995, 0.5051, 2.4398, 0.4647, 1.3601, 4.9198, 0.2969, 6.3063,
127 0.3112, 1.2141, 0.6595, 1.0254, 2.7387, 0.5686, 0.1775, 1.3718, 0.2858,
128 0.8841, 0.9211, 0.5179, 0.8666, 0.782, 0.5096, 0.2196, 0.9902, 1.045,
129 2.0233, 0.7392, 0.4986, 1.3647, 0.4857, 0.0917, 0.4207, 0.923, 0.1395,
130 0.1498, 0.3478, 0.13, 0.1533, 0.4446, 0.1681, 0.3933, 0.1928, 0.1442,
131 0.1424, 0.2661, 0.1653, 0.0949, 0.3766, 0.2162, 0.1161, 1.5536, 0.1605,
132 0.1326, 0.1961, 0.1558, 0.2493, 0.0949, 0.4164, 0.0929, 0.0948, 0.0844,
133 0.141, 0.1492, 0.1259, 0.1625, 0.2601, 0.6224, 0.1378, 0.3119, 0.2467,
134 0.3328, 0.1718, 0.064, 0.0777, 0.1232, 0.1472, 0.0928, 0.1178, 0.0607,
135 0.2195, 0.2645, 0.1358, 0.334, 0.1326, 0.4131, 0.2004]
136
137 # Test all optimizers
138
139 def test_digits_naive():
140 model = FacilityLocationSelection(25, 'cosine', optimizer='naive')
141 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
142 assert_array_equal(model.ranking, digits_ranking)
143 assert_array_almost_equal(model.gains, digits_gains, 4)
144 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
145
146 def test_digits_lazy():
147 model = FacilityLocationSelection(25, 'cosine', optimizer='lazy')
148 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
149 assert_array_equal(model.ranking, digits_ranking)
150 assert_array_almost_equal(model.gains, digits_gains, 4)
151 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
152
153 def test_digits_two_stage():
154 model = FacilityLocationSelection(25, 'cosine', optimizer='two-stage')
155 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
156 assert_array_equal(model.ranking[:50], digits_ranking[:50])
157 assert_array_almost_equal(model.gains[:50], digits_gains[:50], 4)
158 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
159
160 def test_digits_greedi_nn():
161 model = FacilityLocationSelection(25, 'cosine', optimizer='greedi',
162 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'naive'},
163 random_state=0)
164 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
165 assert_array_equal(model.ranking, digits_greedi_ranking)
166 assert_array_almost_equal(model.gains, digits_greedi_gains, 4)
167 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
168
169 def test_digits_greedi_ll():
170 model = FacilityLocationSelection(25, 'cosine', optimizer='greedi',
171 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'lazy'},
172 random_state=0)
173 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
174 assert_array_equal(model.ranking, digits_greedi_ranking)
175 assert_array_almost_equal(model.gains, digits_greedi_gains, 4)
176 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
177
178 def test_digits_greedi_ln():
179 model = FacilityLocationSelection(25, 'cosine', optimizer='greedi',
180 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'naive'},
181 random_state=0)
182 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
183 assert_array_equal(model.ranking, digits_greedi_ranking)
184 assert_array_almost_equal(model.gains, digits_greedi_gains, 4)
185 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
186
187 def test_digits_greedi_nl():
188 model = FacilityLocationSelection(25, 'cosine', optimizer='greedi',
189 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'lazy'},
190 random_state=0)
191 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
192 assert_array_equal(model.ranking, digits_greedi_ranking)
193 assert_array_almost_equal(model.gains, digits_greedi_gains, 4)
194 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
195
196 def test_digits_approximate():
197 model = FacilityLocationSelection(25, 'cosine', optimizer='approximate-lazy')
198 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
199 assert_array_equal(model.ranking, digits_approx_ranking)
200 assert_array_almost_equal(model.gains, digits_approx_gains, 4)
201 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
202
203 def test_digits_stochastic():
204 model = FacilityLocationSelection(25, 'cosine', optimizer='stochastic',
205 random_state=0)
206 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
207 assert_array_equal(model.ranking, digits_stochastic_ranking)
208 assert_array_almost_equal(model.gains, digits_stochastic_gains, 4)
209 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
210
211 def test_digits_sample():
212 model = FacilityLocationSelection(25, 'cosine', optimizer='sample',
213 random_state=0)
214 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
215 assert_array_equal(model.ranking, digits_sample_ranking)
216 assert_array_almost_equal(model.gains, digits_sample_gains, 4)
217 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)
218
219 def test_digits_modular():
220 model = FacilityLocationSelection(25, 'cosine', optimizer='modular',
221 random_state=0)
222 model.fit(X_digits_cupy, sample_cost=X_digits_costs)
223 assert_array_equal(model.ranking, digits_modular_ranking)
224 assert_array_almost_equal(model.gains, digits_modular_gains, 4)
225 assert_less_equal(sum(X_digits_costs[model.ranking]), 25)