Debian Codegraph @

tests/test_optimizers/test_knapsack_facility_location.py

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