| 1185 | @pytest.mark.parametrize("n", [5, 10, 15, 25, 40]) # type: ignore |
| 1186 | @pytest.mark.parametrize("b_per_dim", [10, 20]) # type: ignore |
| 1187 | def test_voronoide(n, b_per_dim) -> None: |
| 1188 | if n < 25 or b_per_dim < 1 and not os.environ.get("CIRCLECI", False): # Outside CircleCI, only the big. |
| 1189 | raise SkipTest("Only big things outside CircleCI.") |
| 1190 | |
| 1191 | list_optims = ["CMA", "DE", "PSO", "RandomSearch", "TwoPointsDE", "OnePlusOne"] |
| 1192 | if os.environ.get("CIRCLECI", False) and (n > 10 or n * b_per_dim > 100): # In CircleCI, only the small. |
| 1193 | raise SkipTest("Topology optimization too slow in CircleCI") |
| 1194 | if os.environ.get("CIRCLECI", False) or (n < 10 or b_per_dim < 20): |
| 1195 | list_optims = ["CMA", "PSO", "OnePlusOne"] |
| 1196 | if n > 20: |
| 1197 | list_optims = ["DE", "TwoPointsDE"] |
| 1198 | fails = {} |
| 1199 | for o in list_optims: |
| 1200 | fails[o] = 0 |
| 1201 | size = n * n |
| 1202 | sqrtsize = n |
| 1203 | b = b_per_dim * size # budget |
| 1204 | nw = 20 # parallel workers |
| 1205 | |
| 1206 | num_tests = 20 |
| 1207 | array = ng.p.Array(shape=(n, n), lower=-1.0, upper=1.0) |
| 1208 | for idx in range(num_tests): |
| 1209 | xa = idx % 3 |
| 1210 | xb = 2 - xa |
| 1211 | xs = 1.5 * ( |
| 1212 | np.array([float(np.cos(xa * i + xb * j) < 0.0) for i in range(n) for j in range(n)]).reshape(n, n) |
| 1213 | - 0.5 |
| 1214 | ) |
| 1215 | if (idx // 3) % 2 > 0: |
| 1216 | xs = np.transpose(xs) |
| 1217 | if (idx // 6) % 2 > 0: |
| 1218 | xs = -xs |
| 1219 | |
| 1220 | def f(x): |
| 1221 | # return np.linalg.norm(x - xs) + np.linalg.norm(x - gaussian_filter(x, sigma=1)) |
| 1222 | return ( |
| 1223 | 5.0 * np.sum(np.abs(x - xs) > 0.3) / size |
| 1224 | + 13.0 * np.linalg.norm(x - gaussian_filter(x, sigma=3)) / sqrtsize |
| 1225 | ) |
| 1226 | |
| 1227 | VoronoiDE = ng.optimizers.VoronoiDE(array, budget=b, num_workers=nw) |
| 1228 | vde = f(VoronoiDE.minimize(f).value) |
| 1229 | for o in list_optims: |
| 1230 | try: |
| 1231 | other = ng.optimizers.registry[o](array, budget=b, num_workers=nw) |
| 1232 | val = f(other.minimize(f).value) |
| 1233 | except: |
| 1234 | print(f"crash in {o}") |
| 1235 | val = float(1.0e7) |
| 1236 | # print(o, val / vde) |
| 1237 | if val < vde: |
| 1238 | fails[o] += 1 |
| 1239 | # Remove both lines below. TODO |
| 1240 | # ratio = min([(idx + 1 - fails[o]) / (0.001 + fails[o]) for o in list_optims]) |
| 1241 | # print(f"temporary: {ratio}", idx + 1, fails, f"({n}-{b_per_dim})") |
| 1242 | ratio = min([(num_tests - fails[o]) / (0.001 + fails[o]) for o in list_optims]) |
| 1243 | print(f"VoronoiDE for DO: {ratio}", num_tests, fails, f"({n}-{b_per_dim})") |
| 1244 | |