atpe.py 66.2 KB
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
"""
    Implements the ATPE algorithm. See
    https://www.electricbrain.io/blog/learning-to-optimize
    and
    https://www.electricbrain.io/blog/optimizing-optimization to learn more
"""

__authors__ = "Bradley Arsenault"
__license__ = "3-clause BSD License"
__contact__ = "github.com/hyperopt/hyperopt"

from hyperopt import hp
from contextlib import contextmanager
import re
import functools
import random
import numpy
import numpy.random
import pkg_resources
import tempfile
import scipy.stats
import os
import math
import hyperopt
import datetime
import json
import copy

# Windows doesn't support opening a NamedTemporaryFile.
# Solution inspired in https://stackoverflow.com/a/46501017/147507
@contextmanager
def ClosedNamedTempFile(contents):
    try:
        with tempfile.NamedTemporaryFile(delete=False) as f:
            file_name = f.name
            f.write(contents)
        yield file_name
    finally:
        os.unlink(file_name)


class Hyperparameter:
    """ This class represents a hyperparameter."""

    def __init__(self, config, parent=None, root="root"):
        self.config = config
        self.root = root
        self.name = root[5:]
        self.parent = parent
        self.resultVariableName = re.sub("\\.\\d+\\.", ".", self.name)

        self.hyperoptVariableName = self.root
        if "name" in config:
            self.hyperoptVariableName = config["name"]

    def createHyperoptSpace(self, lockedValues=None):
        name = self.root

        if lockedValues is None:
            lockedValues = {}

        if "anyOf" in self.config or "oneOf" in self.config:
            data = []
            if "anyOf" in self.config:
                data = self.config["anyOf"]
            else:
                data = self.config["oneOf"]

            subSpaces = [
                Hyperparameter(
                    param, self, name + "." + str(index)
                ).createHyperoptSpace(lockedValues)
                for index, param in enumerate(data)
            ]
            for index, space in enumerate(subSpaces):
                space["$index"] = index

            choices = hp.choice(self.hyperoptVariableName, subSpaces)

            return choices
        elif "enum" in self.config:
            if self.name in lockedValues:
                return lockedValues[self.name]

            choices = hp.choice(self.hyperoptVariableName, self.config["enum"])
            return choices
        elif "constant" in self.config:
            if self.name in lockedValues:
                return lockedValues[self.name]

            return self.config["constant"]
        elif self.config["type"] == "object":
            space = {}
            for key in self.config["properties"].keys():
                config = self.config["properties"][key]
                space[key] = Hyperparameter(
                    config, self, name + "." + key
                ).createHyperoptSpace(lockedValues)
            return space
        elif self.config["type"] == "number":
            if self.name in lockedValues:
                return lockedValues[self.name]

            mode = self.config.get("mode", "uniform")
            scaling = self.config.get("scaling", "linear")

            if mode == "uniform":
                min = self.config.get("min", 0)
                max = self.config.get("max", 1)
                rounding = self.config.get("rounding", None)

                if scaling == "linear":
                    if rounding is not None:
                        return hp.quniform(
                            self.hyperoptVariableName, min, max, rounding
                        )
                    else:
                        return hp.uniform(self.hyperoptVariableName, min, max)
                elif scaling == "logarithmic":
                    if rounding is not None:
                        return hp.qloguniform(
                            self.hyperoptVariableName,
                            math.log(min),
                            math.log(max),
                            rounding,
                        )
                    else:
                        return hp.loguniform(
                            self.hyperoptVariableName, math.log(min), math.log(max)
                        )
            if mode == "randint":
                min = self.config.get("min")
                max = self.config.get("max")
                return hp.randint(self.hyperoptVariableName, min, max)

            if mode == "normal":
                mean = self.config.get("mean", 0)
                stddev = self.config.get("stddev", 1)
                rounding = self.config.get("rounding", None)

                if scaling == "linear":
                    if rounding is not None:
                        return hp.qnormal(
                            self.hyperoptVariableName, mean, stddev, rounding
                        )
                    else:
                        return hp.normal(self.hyperoptVariableName, mean, stddev)
                elif scaling == "logarithmic":
                    if rounding is not None:
                        return hp.qlognormal(
                            self.hyperoptVariableName,
                            math.log(mean),
                            math.log(stddev),
                            rounding,
                        )
                    else:
                        return hp.lognormal(
                            self.hyperoptVariableName, math.log(mean), math.log(stddev)
                        )

    def getFlatParameterNames(self):
        name = self.root

        if "anyOf" in self.config or "oneOf" in self.config:
            keys = set()
            if "anyOf" in self.config:
                data = self.config["anyOf"]
            else:
                data = self.config["oneOf"]

            for index, param in enumerate(data):
                subKeys = Hyperparameter(
                    param, self, name + "." + str(index)
                ).getFlatParameterNames()
                for key in subKeys:
                    keys.add(key)

            return keys
        elif "enum" in self.config or "constant" in self.config:
            return [name]
        elif self.config["type"] == "object":
            keys = set()
            for key in self.config["properties"].keys():
                config = self.config["properties"][key]
                subKeys = Hyperparameter(
                    config, self, name + "." + key
                ).getFlatParameterNames()
                for key in subKeys:
                    keys.add(key)

            return keys
        elif self.config["type"] == "number":
            return [name]

    def getFlatParameters(self):
        name = self.root
        if "anyOf" in self.config or "oneOf" in self.config:
            parameters = []
            if "anyOf" in self.config:
                data = self.config["anyOf"]
            else:
                data = self.config["oneOf"]

            for index, param in enumerate(data):
                subParameters = Hyperparameter(
                    param, self, name + "." + str(index)
                ).getFlatParameters()
                parameters = parameters + subParameters
            return parameters
        elif "enum" in self.config or "constant" in self.config:
            return [self]
        elif self.config["type"] == "object":
            parameters = []
            for key in self.config["properties"].keys():
                config = self.config["properties"][key]
                subParameters = Hyperparameter(
                    config, self, name + "." + key
                ).getFlatParameters()
                parameters = parameters + subParameters
            return parameters
        elif self.config["type"] == "number":
            return [self]

    def getLog10Cardinality(self):
        if "anyOf" in self.config or "oneOf" in self.config:
            if "anyOf" in self.config:
                data = self.config["anyOf"]
            else:
                data = self.config["oneOf"]

            log10_cardinality = Hyperparameter(
                data[0], self, self.root + ".0"
            ).getLog10Cardinality()
            for index, subParam in enumerate(data[1:]):
                # We used logarithm identities to create this reduction formula
                other_log10_cardinality = Hyperparameter(
                    subParam, self, self.root + "." + str(index)
                ).getLog10Cardinality()

                # Revert to linear at high and low values, for numerical stability. Check here: https://www.desmos.com/calculator/efkbbftd18 to observe
                if (log10_cardinality - other_log10_cardinality) > 3:
                    log10_cardinality = log10_cardinality + 1
                elif (other_log10_cardinality - log10_cardinality) > 3:
                    log10_cardinality = other_log10_cardinality + 1
                else:
                    log10_cardinality = other_log10_cardinality + math.log10(
                        1 + math.pow(10, log10_cardinality - other_log10_cardinality)
                    )
            return log10_cardinality + math.log10(len(data))
        elif "enum" in self.config:
            return math.log10(len(self.config["enum"]))
        elif "constant" in self.config:
            return math.log10(1)
        elif self.config["type"] == "object":
            log10_cardinality = 0
            for index, subParam in enumerate(self.config["properties"].values()):
                subParameter = Hyperparameter(
                    subParam, self, self.root + "." + str(index)
                )
                log10_cardinality += subParameter.getLog10Cardinality()
            return log10_cardinality
        elif self.config["type"] == "number":
            if "rounding" in self.config:
                return math.log10(
                    min(
                        20,
                        (self.config["max"] - self.config["min"])
                        / self.config["rounding"]
                        + 1,
                    )
                )
            else:
                return math.log10(20)  # Default of 20 for fully uniform numbers.

    def convertToFlatValues(self, params):
        flatParams = {}

        def recurse(key, value, root):
            result_key = root + "." + key
            if isinstance(value, str):
                flatParams[result_key[1:]] = value
            elif (
                isinstance(value, float)
                or isinstance(value, bool)
                or isinstance(value, int)
                or numpy.issubdtype(value, numpy.integer)
                or numpy.issubdtype(value, numpy.floating)
            ):
                flatParams[result_key[1:]] = value
            elif isinstance(value, dict):
                for subkey, subvalue in value.items():
                    recurse(subkey, subvalue, result_key)

        for key in params.keys():
            value = params[key]
            recurse(key, value, "")

        flatValues = {}

        if "anyOf" in self.config or "oneOf" in self.config:
            if "anyOf" in self.config:
                data = self.config["anyOf"]
            else:
                data = self.config["oneOf"]

            subParameterIndex = flatParams[self.resultVariableName + ".$index"]
            flatValues[self.name] = subParameterIndex

            for index, param in enumerate(data):
                subParameter = Hyperparameter(param, self, self.root + "." + str(index))

                if index == subParameterIndex:
                    subFlatValues = subParameter.convertToFlatValues(flatParams)
                    for key in subFlatValues:
                        flatValues[key] = subFlatValues[key]
                else:
                    for flatParam in subParameter.getFlatParameters():
                        flatValues[flatParam.name] = ""

            return flatValues
        elif "constant" in self.config:
            flatValues[self.name] = flatParams[self.resultVariableName]
            return flatValues
        elif "enum" in self.config:
            flatValues[self.name] = flatParams[self.resultVariableName]
            return flatValues
        elif self.config["type"] == "object":
            for key in self.config["properties"].keys():
                config = self.config["properties"][key]

                subFlatValues = Hyperparameter(
                    config, self, self.root + "." + key
                ).convertToFlatValues(flatParams)

                for key in subFlatValues:
                    flatValues[key] = subFlatValues[key]

                if self.name == "":
                    for key in params.keys():
                        if key.startswith("$"):
                            flatValues[key] = params[key]

            return flatValues
        elif self.config["type"] == "number":
            flatValues[self.name] = flatParams[self.resultVariableName]
            return flatValues

    def convertToStructuredValues(self, flatValues):
        if "anyOf" in self.config or "oneOf" in self.config:
            if "anyOf" in self.config:
                data = self.config["anyOf"]
            else:
                data = self.config["oneOf"]

            subParameterIndex = flatValues[self.name]
            subParam = Hyperparameter(
                data[subParameterIndex], self, self.root + "." + str(subParameterIndex)
            )

            structured = subParam.convertToStructuredValues(flatValues)
            structured["$index"] = subParameterIndex

            return structured
        elif "constant" in self.config:
            return flatValues[self.name]
        elif "enum" in self.config:
            return flatValues[self.name]
        elif self.config["type"] == "object":
            result = {}
            for key in self.config["properties"].keys():
                config = self.config["properties"][key]

                subStructuredValue = Hyperparameter(
                    config, self, self.root + "." + key
                ).convertToStructuredValues(flatValues)

                result[key] = subStructuredValue

                if self.name == "":
                    for key in flatValues.keys():
                        if key.startswith("$"):
                            result[key] = flatValues[key]
            return result
        elif self.config["type"] == "number":
            return flatValues[self.name]

    @staticmethod
    def createHyperparameterConfigForHyperoptDomain(domain):
        if domain.name is None:
            data = {"type": "object", "properties": {}}

            for key in domain.params:
                data["properties"][
                    key
                ] = Hyperparameter.createHyperparameterConfigForHyperoptDomain(
                    domain.params[key]
                )

                if "name" not in data["properties"][key]:
                    data["properties"][key]["name"] = key

            return data
        elif domain.name == "dict":
            data = {"type": "object", "properties": {}}

            for item in domain.named_args:
                data["properties"][
                    item[0]
                ] = Hyperparameter.createHyperparameterConfigForHyperoptDomain(item[1])

            return data
        elif domain.name == "switch":
            data = {"oneOf": []}
            data["name"] = domain.pos_args[0].pos_args

            for item in domain.pos_args[1:]:
                data["oneOf"].append(
                    Hyperparameter.createHyperparameterConfigForHyperoptDomain(item)
                )
            return data
        elif domain.name == "hyperopt_param":
            data = Hyperparameter.createHyperparameterConfigForHyperoptDomain(
                domain.pos_args[1]
            )
            data["name"] = domain.pos_args[0]._obj
            return data
        elif domain.name == "uniform":
            data = {"type": "number"}
            data["scaling"] = "linear"
            data["mode"] = "uniform"
            data["min"] = domain.pos_args[0]._obj
            data["max"] = domain.pos_args[1]._obj
            return data
        elif domain.name == "quniform":
            data = {"type": "number"}
            data["scaling"] = "linear"
            data["mode"] = "uniform"
            data["min"] = domain.pos_args[0]._obj
            data["max"] = domain.pos_args[1]._obj
            data["rounding"] = domain.pos_args[2]._obj
            return data
        elif domain.name == "loguniform":
            data = {"type": "number"}
            data["scaling"] = "logarithmic"
            data["mode"] = "uniform"
            data["min"] = math.exp(domain.pos_args[0]._obj)
            data["max"] = math.exp(domain.pos_args[1]._obj)
            return data
        elif domain.name == "qloguniform":
            data = {"type": "number"}
            data["scaling"] = "logarithmic"
            data["mode"] = "uniform"
            data["min"] = math.exp(domain.pos_args[0]._obj)
            data["max"] = math.exp(domain.pos_args[1]._obj)
            data["rounding"] = domain.pos_args[2]._obj
            return data
        elif domain.name == "normal":
            data = {"type": "number"}
            data["scaling"] = "linear"
            data["mode"] = "normal"
            data["mean"] = domain.pos_args[0]._obj
            data["stddev"] = domain.pos_args[1]._obj
            return data
        elif domain.name == "qnormal":
            data = {"type": "number"}
            data["scaling"] = "linear"
            data["mode"] = "normal"
            data["mean"] = domain.pos_args[0]._obj
            data["stddev"] = domain.pos_args[1]._obj
            data["rounding"] = domain.pos_args[2]._obj
            return data
        elif domain.name == "lognormal":
            data = {"type": "number"}
            data["scaling"] = "logarithmic"
            data["mode"] = "normal"
            data["mean"] = math.exp(domain.pos_args[0]._obj)
            data["stddev"] = math.exp(domain.pos_args[1]._obj)
            return data
        elif domain.name == "qlognormal":
            data = {"type": "number"}
            data["scaling"] = "logarithmic"
            data["mode"] = "normal"
            data["mean"] = math.exp(domain.pos_args[0]._obj)
            data["stddev"] = math.exp(domain.pos_args[1]._obj)
            data["rounding"] = domain.pos_args[2]._obj
            return data
        elif domain.name == "literal":
            data = {"type": "string", "constant": domain._obj}
            return data
        elif domain.name == "randint":
            data = {"type": "number"}
            low = domain.pos_args[0]._obj
            high = domain.pos_args[1]._obj if len(domain.pos_args) > 1 else None
            data["min"] = 0 if high is None else low
            data["max"] = high or low
            data["mode"] = "randint"
            return data
        else:
            raise ValueError("Unsupported hyperopt domain type " + str(domain))


class ATPEOptimizer:
    resultInformationKeys = ["trial", "status", "loss", "time", "log", "error"]

    atpeParameters = [
        "gamma",
        "nEICandidates",
        "resultFilteringAgeMultiplier",
        "resultFilteringLossRankMultiplier",
        "resultFilteringMode",
        "resultFilteringRandomProbability",
        "secondaryCorrelationExponent",
        "secondaryCorrelationMultiplier",
        "secondaryCutoff",
        "secondaryFixedProbability",
        "secondaryLockingMode",
        "secondaryProbabilityMode",
        "secondaryTopLockingPercentile",
    ]

    atpeParameterCascadeOrdering = [
        "resultFilteringMode",
        "secondaryProbabilityMode",
        "secondaryLockingMode",
        "resultFilteringAgeMultiplier",
        "resultFilteringLossRankMultiplier",
        "resultFilteringRandomProbability",
        "secondaryTopLockingPercentile",
        "secondaryCorrelationExponent",
        "secondaryCorrelationMultiplier",
        "secondaryFixedProbability",
        "secondaryCutoff",
        "gamma",
        "nEICandidates",
    ]

    atpeParameterValues = {
        "resultFilteringMode": ["age", "loss_rank", "none", "random"],
        "secondaryLockingMode": ["random", "top"],
        "secondaryProbabilityMode": ["correlation", "fixed"],
    }

    atpeModelFeatureKeys = [
        "all_correlation_best_percentile25_ratio",
        "all_correlation_best_percentile50_ratio",
        "all_correlation_best_percentile75_ratio",
        "all_correlation_kurtosis",
        "all_correlation_percentile5_percentile25_ratio",
        "all_correlation_skew",
        "all_correlation_stddev_best_ratio",
        "all_correlation_stddev_median_ratio",
        "all_loss_best_percentile25_ratio",
        "all_loss_best_percentile50_ratio",
        "all_loss_best_percentile75_ratio",
        "all_loss_kurtosis",
        "all_loss_percentile5_percentile25_ratio",
        "all_loss_skew",
        "all_loss_stddev_best_ratio",
        "all_loss_stddev_median_ratio",
        "log10_cardinality",
        "recent_10_correlation_best_percentile25_ratio",
        "recent_10_correlation_best_percentile50_ratio",
        "recent_10_correlation_best_percentile75_ratio",
        "recent_10_correlation_kurtosis",
        "recent_10_correlation_percentile5_percentile25_ratio",
        "recent_10_correlation_skew",
        "recent_10_correlation_stddev_best_ratio",
        "recent_10_correlation_stddev_median_ratio",
        "recent_10_loss_best_percentile25_ratio",
        "recent_10_loss_best_percentile50_ratio",
        "recent_10_loss_best_percentile75_ratio",
        "recent_10_loss_kurtosis",
        "recent_10_loss_percentile5_percentile25_ratio",
        "recent_10_loss_skew",
        "recent_10_loss_stddev_best_ratio",
        "recent_10_loss_stddev_median_ratio",
        "recent_15%_correlation_best_percentile25_ratio",
        "recent_15%_correlation_best_percentile50_ratio",
        "recent_15%_correlation_best_percentile75_ratio",
        "recent_15%_correlation_kurtosis",
        "recent_15%_correlation_percentile5_percentile25_ratio",
        "recent_15%_correlation_skew",
        "recent_15%_correlation_stddev_best_ratio",
        "recent_15%_correlation_stddev_median_ratio",
        "recent_15%_loss_best_percentile25_ratio",
        "recent_15%_loss_best_percentile50_ratio",
        "recent_15%_loss_best_percentile75_ratio",
        "recent_15%_loss_kurtosis",
        "recent_15%_loss_percentile5_percentile25_ratio",
        "recent_15%_loss_skew",
        "recent_15%_loss_stddev_best_ratio",
        "recent_15%_loss_stddev_median_ratio",
        "recent_25_correlation_best_percentile25_ratio",
        "recent_25_correlation_best_percentile50_ratio",
        "recent_25_correlation_best_percentile75_ratio",
        "recent_25_correlation_kurtosis",
        "recent_25_correlation_percentile5_percentile25_ratio",
        "recent_25_correlation_skew",
        "recent_25_correlation_stddev_best_ratio",
        "recent_25_correlation_stddev_median_ratio",
        "recent_25_loss_best_percentile25_ratio",
        "recent_25_loss_best_percentile50_ratio",
        "recent_25_loss_best_percentile75_ratio",
        "recent_25_loss_kurtosis",
        "recent_25_loss_percentile5_percentile25_ratio",
        "recent_25_loss_skew",
        "recent_25_loss_stddev_best_ratio",
        "recent_25_loss_stddev_median_ratio",
        "top_10%_correlation_best_percentile25_ratio",
        "top_10%_correlation_best_percentile50_ratio",
        "top_10%_correlation_best_percentile75_ratio",
        "top_10%_correlation_kurtosis",
        "top_10%_correlation_percentile5_percentile25_ratio",
        "top_10%_correlation_skew",
        "top_10%_correlation_stddev_best_ratio",
        "top_10%_correlation_stddev_median_ratio",
        "top_10%_loss_best_percentile25_ratio",
        "top_10%_loss_best_percentile50_ratio",
        "top_10%_loss_best_percentile75_ratio",
        "top_10%_loss_kurtosis",
        "top_10%_loss_percentile5_percentile25_ratio",
        "top_10%_loss_skew",
        "top_10%_loss_stddev_best_ratio",
        "top_10%_loss_stddev_median_ratio",
        "top_20%_correlation_best_percentile25_ratio",
        "top_20%_correlation_best_percentile50_ratio",
        "top_20%_correlation_best_percentile75_ratio",
        "top_20%_correlation_kurtosis",
        "top_20%_correlation_percentile5_percentile25_ratio",
        "top_20%_correlation_skew",
        "top_20%_correlation_stddev_best_ratio",
        "top_20%_correlation_stddev_median_ratio",
        "top_20%_loss_best_percentile25_ratio",
        "top_20%_loss_best_percentile50_ratio",
        "top_20%_loss_best_percentile75_ratio",
        "top_20%_loss_kurtosis",
        "top_20%_loss_percentile5_percentile25_ratio",
        "top_20%_loss_skew",
        "top_20%_loss_stddev_best_ratio",
        "top_20%_loss_stddev_median_ratio",
        "top_30%_correlation_best_percentile25_ratio",
        "top_30%_correlation_best_percentile50_ratio",
        "top_30%_correlation_best_percentile75_ratio",
        "top_30%_correlation_kurtosis",
        "top_30%_correlation_percentile5_percentile25_ratio",
        "top_30%_correlation_skew",
        "top_30%_correlation_stddev_best_ratio",
        "top_30%_correlation_stddev_median_ratio",
        "top_30%_loss_best_percentile25_ratio",
        "top_30%_loss_best_percentile50_ratio",
        "top_30%_loss_best_percentile75_ratio",
        "top_30%_loss_kurtosis",
        "top_30%_loss_percentile5_percentile25_ratio",
        "top_30%_loss_skew",
        "top_30%_loss_stddev_best_ratio",
        "top_30%_loss_stddev_median_ratio",
    ]

    def __init__(self):
        try:
            import lightgbm
            import sklearn
        except ImportError:
            raise ImportError(
                "You must install lightgbm and sklearn in order to use the ATPE algorithm. Please run `pip install lightgbm scikit-learn` and try again. These are not built in dependencies of hyperopt."
            )

        scalingModelData = json.loads(
            pkg_resources.resource_string(
                __name__, "atpe_models/scaling_model.json"
            ).decode("utf-8")
        )
        self.featureScalingModels = {}
        for key in self.atpeModelFeatureKeys:
            self.featureScalingModels[key] = sklearn.preprocessing.StandardScaler()
            self.featureScalingModels[key].scale_ = numpy.array(
                scalingModelData[key]["scales"]
            )
            self.featureScalingModels[key].mean_ = numpy.array(
                scalingModelData[key]["means"]
            )
            self.featureScalingModels[key].var_ = numpy.array(
                scalingModelData[key]["variances"]
            )

        self.parameterModels = {}
        self.parameterModelConfigurations = {}
        for param in self.atpeParameters:
            modelData = pkg_resources.resource_string(
                __name__, "atpe_models/model-" + param + ".txt"
            )
            with ClosedNamedTempFile(modelData) as model_file_name:
                self.parameterModels[param] = lightgbm.Booster(
                    model_file=model_file_name
                )

            configString = pkg_resources.resource_string(
                __name__, "atpe_models/model-" + param + "-configuration.json"
            )
            data = json.loads(configString.decode("utf-8"))
            self.parameterModelConfigurations[param] = data

        self.lastATPEParameters = None
        self.lastLockedParameters = []
        self.atpeParamDetails = None

    def recommendNextParameters(
        self, hyperparameterSpace, results, currentTrials, lockedValues=None
    ):
        rstate = numpy.random.RandomState(seed=int(random.randint(1, 2 ** 32 - 1)))

        params = {"param": {}}

        def sample(parameters):
            params["param"] = parameters
            return {"loss": 0.5, "status": "ok"}

        parameters = Hyperparameter(hyperparameterSpace).getFlatParameters()

        if lockedValues is not None:
            # Remove any locked values from ones the optimizer will examine
            parameters = list(
                filter(lambda param: param.name not in lockedValues.keys(), parameters)
            )

        log10_cardinality = Hyperparameter(hyperparameterSpace).getLog10Cardinality()
        initializationRounds = max(10, int(log10_cardinality))

        atpeParams = {}
        atpeParamDetails = {}
        if (
            len(list(result for result in results if result["loss"]))
            < initializationRounds
        ):
            atpeParams = {
                "gamma": 1.0,
                "nEICandidates": 24,
                "resultFilteringAgeMultiplier": None,
                "resultFilteringLossRankMultiplier": None,
                "resultFilteringMode": "none",
                "resultFilteringRandomProbability": None,
                "secondaryCorrelationExponent": 1.0,
                "secondaryCorrelationMultiplier": None,
                "secondaryCutoff": 0,
                "secondarySorting": 0,
                "secondaryFixedProbability": 0.5,
                "secondaryLockingMode": "top",
                "secondaryProbabilityMode": "fixed",
                "secondaryTopLockingPercentile": 0,
            }
        else:
            # Calculate the statistics for the distribution
            stats = self.computeAllResultStatistics(hyperparameterSpace, results)
            stats["num_parameters"] = len(parameters)
            stats["log10_cardinality"] = Hyperparameter(
                hyperparameterSpace
            ).getLog10Cardinality()
            stats["log10_trial"] = math.log10(len(results))
            baseVector = []

            for feature in self.atpeModelFeatureKeys:
                scalingModel = self.featureScalingModels[feature]
                transformed = scalingModel.transform([[stats[feature]]])[0][0]
                baseVector.append(transformed)

            baseVector = numpy.array([baseVector])

            for atpeParamIndex, atpeParameter in enumerate(
                self.atpeParameterCascadeOrdering
            ):
                vector = copy.copy(baseVector)[0].tolist()
                atpeParamFeatures = self.atpeParameterCascadeOrdering[:atpeParamIndex]
                for atpeParamFeature in atpeParamFeatures:
                    # We have to insert a special value of -3 for any conditional parameters.
                    if (
                        atpeParamFeature == "resultFilteringAgeMultiplier"
                        and atpeParams["resultFilteringMode"] != "age"
                    ):
                        vector.append(
                            -3
                        )  # This is the default value inserted when parameters aren't relevant
                    elif (
                        atpeParamFeature == "resultFilteringLossRankMultiplier"
                        and atpeParams["resultFilteringMode"] != "loss_rank"
                    ):
                        vector.append(
                            -3
                        )  # This is the default value inserted when parameters aren't relevant
                    elif (
                        atpeParamFeature == "resultFilteringRandomProbability"
                        and atpeParams["resultFilteringMode"] != "random"
                    ):
                        vector.append(
                            -3
                        )  # This is the default value inserted when parameters aren't relevant
                    elif (
                        atpeParamFeature == "secondaryCorrelationMultiplier"
                        and atpeParams["secondaryProbabilityMode"] != "correlation"
                    ):
                        vector.append(
                            -3
                        )  # This is the default value inserted when parameters aren't relevant
                    elif (
                        atpeParamFeature == "secondaryFixedProbability"
                        and atpeParams["secondaryProbabilityMode"] != "fixed"
                    ):
                        vector.append(
                            -3
                        )  # This is the default value inserted when parameters aren't relevant
                    elif (
                        atpeParamFeature == "secondaryTopLockingPercentile"
                        and atpeParams["secondaryLockingMode"] != "top"
                    ):
                        vector.append(
                            -3
                        )  # This is the default value inserted when parameters aren't relevant
                    elif atpeParamFeature in self.atpeParameterValues:
                        for value in self.atpeParameterValues[atpeParamFeature]:
                            vector.append(
                                1.0 if atpeParams[atpeParamFeature] == value else 0
                            )
                    else:
                        vector.append(float(atpeParams[atpeParamFeature]))

                allFeatureKeysForATPEParamModel = copy.copy(self.atpeModelFeatureKeys)
                for atpeParamFeature in atpeParamFeatures:
                    if atpeParamFeature in self.atpeParameterValues:
                        for value in self.atpeParameterValues[atpeParamFeature]:
                            allFeatureKeysForATPEParamModel.append(
                                atpeParamFeature + "_" + value
                            )
                    else:
                        allFeatureKeysForATPEParamModel.append(atpeParamFeature)

                value = self.parameterModels[atpeParameter].predict([vector])[0]
                featureContributions = self.parameterModels[atpeParameter].predict(
                    [vector], pred_contrib=True
                )[0]

                atpeParamDetails[atpeParameter] = {"value": None, "reason": None}

                # Set the value
                if atpeParameter in self.atpeParameterValues:
                    # Renormalize the predicted probabilities
                    config = self.parameterModelConfigurations[atpeParameter]
                    for atpeParamValueIndex, atpeParamValue in enumerate(
                        self.atpeParameterValues[atpeParameter]
                    ):
                        value[atpeParamValueIndex] = (
                            (
                                (
                                    value[atpeParamValueIndex]
                                    - config["predMeans"][atpeParamValue]
                                )
                                / config["predStddevs"][atpeParamValue]
                            )
                            * config["origStddevs"][atpeParamValue]
                        ) + config["origMeans"][atpeParamValue]
                        value[atpeParamValueIndex] = max(
                            0.0, min(1.0, value[atpeParamValueIndex])
                        )

                    maxVal = numpy.max(value)
                    for atpeParamValueIndex, atpeParamValue in enumerate(
                        self.atpeParameterValues[atpeParameter]
                    ):
                        value[atpeParamValueIndex] = max(
                            value[atpeParamValueIndex], maxVal * 0.15
                        )  # We still allow the non reccomended modes to get chosen 15% of the time

                    # Make a random weighted choice based on the normalized probabilities
                    probabilities = value / numpy.sum(value)
                    chosen = numpy.random.choice(
                        a=self.atpeParameterValues[atpeParameter], p=probabilities
                    )
                    atpeParams[atpeParameter] = str(chosen)
                else:
                    # Renormalize the predictions
                    config = self.parameterModelConfigurations[atpeParameter]
                    value = (
                        ((value - config["predMean"]) / config["predStddev"])
                        * config["origStddev"]
                    ) + config["origMean"]
                    atpeParams[atpeParameter] = float(value)

                atpeParamDetails[atpeParameter]["reason"] = {}
                # If we are predicting a class, we get separate feature contributions for each class. Take the average
                if atpeParameter in self.atpeParameterValues:
                    featureContributions = numpy.mean(
                        numpy.reshape(
                            featureContributions,
                            newshape=(
                                len(allFeatureKeysForATPEParamModel) + 1,
                                len(self.atpeParameterValues[atpeParameter]),
                            ),
                        ),
                        axis=1,
                    )

                contributions = [
                    (
                        self.atpeModelFeatureKeys[index],
                        float(featureContributions[index]),
                    )
                    for index in range(len(self.atpeModelFeatureKeys))
                ]
                contributions = sorted(contributions, key=lambda r: -r[1])
                # Only focus on the top 10% of features, since it gives more useful information. Otherwise the total gets really squashed out over many features,
                # because our model is highly regularized.
                contributions = contributions[: int(len(contributions) / 10)]
                total = numpy.sum([contrib[1] for contrib in contributions])

                for contributionIndex, contribution in enumerate(contributions[:3]):
                    atpeParamDetails[atpeParameter]["reason"][contribution[0]] = (
                        str(int(float(contribution[1]) * 100.0 / total)) + "%"
                    )

                # Apply bounds to all the parameters
                if atpeParameter == "gamma":
                    atpeParams["gamma"] = max(0.2, min(2.0, atpeParams["gamma"]))
                if atpeParameter == "nEICandidates":
                    atpeParams["nEICandidates"] = int(
                        max(2.0, min(48, atpeParams["nEICandidates"]))
                    )
                if atpeParameter == "resultFilteringAgeMultiplier":
                    atpeParams["resultFilteringAgeMultiplier"] = max(
                        1.0, min(4.0, atpeParams["resultFilteringAgeMultiplier"])
                    )
                if atpeParameter == "resultFilteringLossRankMultiplier":
                    atpeParams["resultFilteringLossRankMultiplier"] = max(
                        1.0, min(4.0, atpeParams["resultFilteringLossRankMultiplier"])
                    )
                if atpeParameter == "resultFilteringRandomProbability":
                    atpeParams["resultFilteringRandomProbability"] = max(
                        0.7, min(0.9, atpeParams["resultFilteringRandomProbability"])
                    )
                if atpeParameter == "secondaryCorrelationExponent":
                    atpeParams["secondaryCorrelationExponent"] = max(
                        1.0, min(3.0, atpeParams["secondaryCorrelationExponent"])
                    )
                if atpeParameter == "secondaryCorrelationMultiplier":
                    atpeParams["secondaryCorrelationMultiplier"] = max(
                        0.2, min(1.8, atpeParams["secondaryCorrelationMultiplier"])
                    )
                if atpeParameter == "secondaryCutoff":
                    atpeParams["secondaryCutoff"] = max(
                        -1.0, min(1.0, atpeParams["secondaryCutoff"])
                    )
                if atpeParameter == "secondaryFixedProbability":
                    atpeParams["secondaryFixedProbability"] = max(
                        0.2, min(0.8, atpeParams["secondaryFixedProbability"])
                    )
                if atpeParameter == "secondaryTopLockingPercentile":
                    atpeParams["secondaryTopLockingPercentile"] = max(
                        0, min(10.0, atpeParams["secondaryTopLockingPercentile"])
                    )

            # Now blank out unneeded params so they don't confuse us
            if atpeParams["secondaryLockingMode"] == "random":
                atpeParams["secondaryTopLockingPercentile"] = None

            if atpeParams["secondaryProbabilityMode"] == "fixed":
                atpeParams["secondaryCorrelationMultiplier"] = None
            else:
                atpeParams["secondaryFixedProbability"] = None

            if atpeParams["resultFilteringMode"] == "none":
                atpeParams["resultFilteringAgeMultiplier"] = None
                atpeParams["resultFilteringLossRankMultiplier"] = None
                atpeParams["resultFilteringRandomProbability"] = None
            elif atpeParams["resultFilteringMode"] == "age":
                atpeParams["resultFilteringLossRankMultiplier"] = None
                atpeParams["resultFilteringRandomProbability"] = None
            elif atpeParams["resultFilteringMode"] == "loss_rank":
                atpeParams["resultFilteringAgeMultiplier"] = None
                atpeParams["resultFilteringRandomProbability"] = None
            elif atpeParams["resultFilteringMode"] == "random":
                atpeParams["resultFilteringAgeMultiplier"] = None
                atpeParams["resultFilteringLossRankMultiplier"] = None

            for atpeParameter in self.atpeParameters:
                if atpeParams[atpeParameter] is None:
                    del atpeParamDetails[atpeParameter]
                else:
                    atpeParamDetails[atpeParameter]["value"] = atpeParams[atpeParameter]

        self.lastATPEParameters = atpeParams
        self.atpeParamDetails = atpeParamDetails

        def computePrimarySecondary():
            if len(results) < initializationRounds:
                return (
                    parameters,
                    [],
                    [0.5] * len(parameters),
                )  # Put all parameters as primary

            if len(set(result["loss"] for result in results)) < 5:
                return (
                    parameters,
                    [],
                    [0.5] * len(parameters),
                )  # Put all parameters as primary

            numberParameters = [
                parameter
                for parameter in parameters
                if parameter.config["type"] == "number"
            ]
            otherParameters = [
                parameter
                for parameter in parameters
                if parameter.config["type"] != "number"
            ]

            totalWeight = 0
            correlations = {}
            for parameter in numberParameters:
                if (
                    len(
                        set(
                            result[parameter.name]
                            for result in results
                            if result[parameter.name] is not None
                        )
                    )
                    < 2
                ):
                    correlations[parameter.name] = 0
                else:
                    values = []
                    valueLosses = []
                    for result in results:
                        if (
                            result[parameter.name] is not None
                            and result["loss"] is not None
                        ):
                            values.append(result[parameter.name])
                            valueLosses.append(result["loss"])

                    correlation = math.pow(
                        abs(scipy.stats.spearmanr(values, valueLosses)[0]),
                        atpeParams["secondaryCorrelationExponent"],
                    )
                    correlations[parameter.name] = correlation
                    totalWeight += correlation

            threshold = totalWeight * abs(atpeParams["secondaryCutoff"])

            if atpeParams["secondaryCutoff"] < 0:
                # Reverse order - we lock in the highest correlated parameters
                sortedParameters = sorted(
                    numberParameters, key=lambda parameter: correlations[parameter.name]
                )
            else:
                # Normal order - sort properties by their correlation to lock in lowest correlated parameters
                sortedParameters = sorted(
                    numberParameters,
                    key=lambda parameter: -correlations[parameter.name],
                )

            primaryParameters = []
            secondaryParameters = []
            cumulative = totalWeight
            for parameter in sortedParameters:
                if cumulative < threshold:
                    secondaryParameters.append(parameter)
                else:
                    primaryParameters.append(parameter)

                cumulative -= correlations[parameter.name]

            return (
                primaryParameters + otherParameters,
                secondaryParameters,
                correlations,
            )

        if (
            len([result["loss"] for result in results if result["loss"] is not None])
            == 0
        ):
            maxLoss = 1
        else:
            maxLoss = numpy.max(
                [result["loss"] for result in results if result["loss"] is not None]
            )

        # We create a copy of lockedValues so we don't modify the object that was passed in as an argument - treat it as immutable.
        # The ATPE algorithm will lock additional values in a stochastic manner
        if lockedValues is None:
            lockedValues = {}
        else:
            lockedValues = copy.copy(lockedValues)

        filteredResults = []
        removedResults = []
        if len(results) > initializationRounds:
            (
                primaryParameters,
                secondaryParameters,
                correlations,
            ) = computePrimarySecondary()

            self.lastLockedParameters = []

            sortedResults = list(
                sorted(
                    list(results),
                    key=lambda result: (
                        result["loss"] if result["loss"] is not None else (maxLoss + 1)
                    ),
                )
            )
            topResults = sortedResults
            if atpeParams["secondaryLockingMode"] == "top":
                topResultsN = max(
                    1,
                    int(
                        math.ceil(
                            len(sortedResults)
                            * atpeParams["secondaryTopLockingPercentile"]
                            / 100.0
                        )
                    ),
                )
                topResults = sortedResults[:topResultsN]

            # Any secondary parameters have may be locked to either the current best
            # value or any value within the result pool.
            for secondary in secondaryParameters:
                if atpeParams["secondaryProbabilityMode"] == "fixed":
                    if random.uniform(0, 1) < atpeParams["secondaryFixedProbability"]:
                        self.lastLockedParameters.append(secondary.name)
                        if atpeParams["secondaryLockingMode"] == "top":
                            lockResult = random.choice(topResults)
                            if (
                                lockResult[secondary.name] is not None
                                and lockResult[secondary.name] != ""
                            ):
                                lockedValues[secondary.name] = lockResult[
                                    secondary.name
                                ]
                        elif atpeParams["secondaryLockingMode"] == "random":
                            lockedValues[
                                secondary.name
                            ] = self.chooseRandomValueForParameter(secondary)

                elif atpeParams["secondaryProbabilityMode"] == "correlation":
                    probability = max(
                        0,
                        min(
                            1,
                            abs(correlations[secondary.name])
                            * atpeParams["secondaryCorrelationMultiplier"],
                        ),
                    )
                    if random.uniform(0, 1) < probability:
                        self.lastLockedParameters.append(secondary.name)
                        if atpeParams["secondaryLockingMode"] == "top":
                            lockResult = random.choice(topResults)
                            if (
                                lockResult[secondary.name] is not None
                                and lockResult[secondary.name] != ""
                            ):
                                lockedValues[secondary.name] = lockResult[
                                    secondary.name
                                ]
                        elif atpeParams["secondaryLockingMode"] == "random":
                            lockedValues[
                                secondary.name
                            ] = self.chooseRandomValueForParameter(secondary)

            # Now last step, we filter results prior to sending them into ATPE
            for resultIndex, result in enumerate(results):
                if atpeParams["resultFilteringMode"] == "none":
                    filteredResults.append(result)
                elif atpeParams["resultFilteringMode"] == "random":
                    if (
                        random.uniform(0, 1)
                        < atpeParams["resultFilteringRandomProbability"]
                    ):
                        filteredResults.append(result)
                    else:
                        removedResults.append(result)
                elif atpeParams["resultFilteringMode"] == "age":
                    age = float(resultIndex) / float(len(results))
                    if random.uniform(0, 1) < (
                        atpeParams["resultFilteringAgeMultiplier"] * age
                    ):
                        filteredResults.append(result)
                    else:
                        removedResults.append(result)
                elif atpeParams["resultFilteringMode"] == "loss_rank":
                    rank = 1.0 - (
                        float(sortedResults.index(result)) / float(len(results))
                    )
                    if random.uniform(0, 1) < (
                        atpeParams["resultFilteringLossRankMultiplier"] * rank
                    ):
                        filteredResults.append(result)
                    else:
                        removedResults.append(result)

        # If we are in initialization, or by some other fluke of random nature that we
        # end up with no results after filtering, then just use all the results
        if len(filteredResults) == 0:
            filteredResults = results

        hyperopt.fmin(
            fn=sample,
            space=Hyperparameter(hyperparameterSpace).createHyperoptSpace(lockedValues),
            algo=functools.partial(
                hyperopt.tpe.suggest,
                n_startup_jobs=initializationRounds,
                gamma=atpeParams["gamma"],
                n_EI_candidates=int(atpeParams["nEICandidates"]),
            ),
            max_evals=1,
            trials=self.convertResultsToTrials(hyperparameterSpace, filteredResults),
            rstate=rstate,
            show_progressbar=False,
        )

        return params.get("param")

    def chooseRandomValueForParameter(self, parameter):
        if parameter.config.get("mode", "uniform") == "uniform":
            minVal = parameter.config["min"]
            maxVal = parameter.config["max"]

            if parameter.config.get("scaling", "linear") == "logarithmic":
                minVal = math.log(minVal)
                maxVal = math.log(maxVal)

            value = random.uniform(minVal, maxVal)

            if parameter.config.get("scaling", "linear") == "logarithmic":
                value = math.exp(value)

            if "rounding" in parameter.config:
                value = (
                    round(value / parameter.config["rounding"])
                    * parameter.config["rounding"]
                )
        elif parameter.config.get("mode", "uniform") == "normal":
            meanVal = parameter.config["mean"]
            stddevVal = parameter.config["stddev"]

            if parameter.config.get("scaling", "linear") == "logarithmic":
                meanVal = math.log(meanVal)
                stddevVal = math.log(stddevVal)

            value = random.gauss(meanVal, stddevVal)

            if parameter.config.get("scaling", "linear") == "logarithmic":
                value = math.exp(value)

            if "rounding" in parameter.config:
                value = (
                    round(value / parameter.config["rounding"])
                    * parameter.config["rounding"]
                )
        elif parameter.config.get("mode", "uniform") == "randint":
            min = parameter.config["min"]
            max = parameter.config["max"]
            value = random.randint(min, max)

        return value

    def computePartialResultStatistics(self, hyperparameterSpace, results):
        losses = numpy.array(
            sorted([result["loss"] for result in results if result["loss"] is not None])
        )

        bestLoss = 0
        percentile5Loss = 0
        percentile25Loss = 0
        percentile50Loss = 0
        percentile75Loss = 0
        statistics = {}

        numpy.warnings.filterwarnings("ignore")

        if len(set(losses)) > 1:
            bestLoss = numpy.percentile(losses, 0)
            percentile5Loss = numpy.percentile(losses, 5)
            percentile25Loss = numpy.percentile(losses, 25)
            percentile50Loss = numpy.percentile(losses, 50)
            percentile75Loss = numpy.percentile(losses, 75)

            statistics["loss_skew"] = scipy.stats.skew(losses)
            statistics["loss_kurtosis"] = scipy.stats.kurtosis(losses)
        else:
            statistics["loss_skew"] = 0
            statistics["loss_kurtosis"] = 0

        if percentile50Loss == 0:
            statistics["loss_stddev_median_ratio"] = 0
            statistics["loss_best_percentile50_ratio"] = 0
        else:
            statistics["loss_stddev_median_ratio"] = (
                numpy.std(losses) / percentile50Loss
            )
            statistics["loss_best_percentile50_ratio"] = bestLoss / percentile50Loss

        if bestLoss == 0:
            statistics["loss_stddev_best_ratio"] = 0
        else:
            statistics["loss_stddev_best_ratio"] = numpy.std(losses) / bestLoss

        if percentile25Loss == 0:
            statistics["loss_best_percentile25_ratio"] = 0
            statistics["loss_percentile5_percentile25_ratio"] = 0
        else:
            statistics["loss_best_percentile25_ratio"] = bestLoss / percentile25Loss
            statistics["loss_percentile5_percentile25_ratio"] = (
                percentile5Loss / percentile25Loss
            )

        if percentile75Loss == 0:
            statistics["loss_best_percentile75_ratio"] = 0
        else:
            statistics["loss_best_percentile75_ratio"] = bestLoss / percentile75Loss

        def getValue(result, parameter):
            return result[parameter.name]

        # Now we compute correlations between each parameter and the loss
        parameters = Hyperparameter(hyperparameterSpace).getFlatParameters()
        correlations = []
        for parameter in parameters:
            if parameter.config["type"] == "number":
                if (
                    len(
                        set(
                            getValue(result, parameter)
                            for result in results
                            if (
                                getValue(result, parameter) is not None
                                and result["loss"] is not None
                            )
                        )
                    )
                    < 2
                ):
                    correlations.append(0)
                else:
                    values = []
                    valueLosses = []
                    for result in results:
                        if result["loss"] is not None and (
                            isinstance(getValue(result, parameter), float)
                            or isinstance(getValue(result, parameter), int)
                        ):
                            values.append(getValue(result, parameter))
                            valueLosses.append(result["loss"])

                    correlation = abs(scipy.stats.spearmanr(values, valueLosses)[0])
                    if math.isnan(correlation) or math.isinf(correlation):
                        correlations.append(0)
                    else:
                        correlations.append(correlation)

        correlations = numpy.array(correlations)

        if len(set(correlations)) == 1:
            statistics["correlation_skew"] = 0
            statistics["correlation_kurtosis"] = 0
            statistics["correlation_stddev_median_ratio"] = 0
            statistics["correlation_stddev_best_ratio"] = 0

            statistics["correlation_best_percentile25_ratio"] = 0
            statistics["correlation_best_percentile50_ratio"] = 0
            statistics["correlation_best_percentile75_ratio"] = 0
            statistics["correlation_percentile5_percentile25_ratio"] = 0
        else:
            bestCorrelation = numpy.percentile(
                correlations, 100
            )  # Correlations are in the opposite order of losses, higher correlation is considered "best"
            percentile5Correlation = numpy.percentile(correlations, 95)
            percentile25Correlation = numpy.percentile(correlations, 75)
            percentile50Correlation = numpy.percentile(correlations, 50)
            percentile75Correlation = numpy.percentile(correlations, 25)

            statistics["correlation_skew"] = scipy.stats.skew(correlations)
            statistics["correlation_kurtosis"] = scipy.stats.kurtosis(correlations)

            if percentile50Correlation == 0:
                statistics["correlation_stddev_median_ratio"] = 0
                statistics["correlation_best_percentile50_ratio"] = 0
            else:
                statistics["correlation_stddev_median_ratio"] = (
                    numpy.std(correlations) / percentile50Correlation
                )
                statistics["correlation_best_percentile50_ratio"] = (
                    bestCorrelation / percentile50Correlation
                )

            if bestCorrelation == 0:
                statistics["correlation_stddev_best_ratio"] = 0
            else:
                statistics["correlation_stddev_best_ratio"] = (
                    numpy.std(correlations) / bestCorrelation
                )

            if percentile25Correlation == 0:
                statistics["correlation_best_percentile25_ratio"] = 0
                statistics["correlation_percentile5_percentile25_ratio"] = 0
            else:
                statistics["correlation_best_percentile25_ratio"] = (
                    bestCorrelation / percentile25Correlation
                )
                statistics["correlation_percentile5_percentile25_ratio"] = (
                    percentile5Correlation / percentile25Correlation
                )

            if percentile75Correlation == 0:
                statistics["correlation_best_percentile75_ratio"] = 0
            else:
                statistics["correlation_best_percentile75_ratio"] = (
                    bestCorrelation / percentile75Correlation
                )

        return statistics

    def computeAllResultStatistics(self, hyperparameterSpace, results):
        losses = numpy.array(
            sorted([result["loss"] for result in results if result["loss"] is not None])
        )

        if len(set(losses)) > 1:
            percentile10Loss = numpy.percentile(losses, 10)
            percentile20Loss = numpy.percentile(losses, 20)
            percentile30Loss = numpy.percentile(losses, 30)
        else:
            percentile10Loss = losses[0]
            percentile20Loss = losses[0]
            percentile30Loss = losses[0]

        allResults = list(results)
        percentile10Results = [
            result
            for result in results
            if result["loss"] is not None and result["loss"] <= percentile10Loss
        ]
        percentile20Results = [
            result
            for result in results
            if result["loss"] is not None and result["loss"] <= percentile20Loss
        ]
        percentile30Results = [
            result
            for result in results
            if result["loss"] is not None and result["loss"] <= percentile30Loss
        ]

        recent10Count = min(len(results), 10)
        recent10Results = results[-recent10Count:]

        recent25Count = min(len(results), 25)
        recent25Results = results[-recent25Count:]

        recent15PercentCount = max(math.ceil(len(results) * 0.15), 5)
        recent15PercentResults = results[-recent15PercentCount:]

        statistics = {}
        allResultStatistics = self.computePartialResultStatistics(
            hyperparameterSpace, allResults
        )
        for stat, value in allResultStatistics.items():
            statistics["all_" + stat] = value

        percentile10Statistics = self.computePartialResultStatistics(
            hyperparameterSpace, percentile10Results
        )
        for stat, value in percentile10Statistics.items():
            statistics["top_10%_" + stat] = value

        percentile20Statistics = self.computePartialResultStatistics(
            hyperparameterSpace, percentile20Results
        )
        for stat, value in percentile20Statistics.items():
            statistics["top_20%_" + stat] = value

        percentile30Statistics = self.computePartialResultStatistics(
            hyperparameterSpace, percentile30Results
        )
        for stat, value in percentile30Statistics.items():
            statistics["top_30%_" + stat] = value

        recent10Statistics = self.computePartialResultStatistics(
            hyperparameterSpace, recent10Results
        )
        for stat, value in recent10Statistics.items():
            statistics["recent_10_" + stat] = value

        recent25Statistics = self.computePartialResultStatistics(
            hyperparameterSpace, recent25Results
        )
        for stat, value in recent25Statistics.items():
            statistics["recent_25_" + stat] = value

        recent15PercentResult = self.computePartialResultStatistics(
            hyperparameterSpace, recent15PercentResults
        )
        for stat, value in recent15PercentResult.items():
            statistics["recent_15%_" + stat] = value

        # Although we have added lots of protection in the computePartialResultStatistics code, one last hedge against any NaN or infinity values coming up
        # in our statistics
        for key in statistics.keys():
            if math.isnan(statistics[key]) or math.isinf(statistics[key]):
                statistics[key] = 0

        return statistics

    def convertResultsToTrials(self, hyperparameterSpace, results):
        trials = hyperopt.Trials()

        for resultIndex, result in enumerate(results):
            data = {
                "book_time": datetime.datetime.now(),
                "exp_key": None,
                "misc": {
                    "cmd": ("domain_attachment", "FMinIter_Domain"),
                    "idxs": {},
                    "tid": resultIndex,
                    "vals": {},
                    "workdir": None,
                },
                "owner": None,
                "refresh_time": datetime.datetime.now(),
                "result": {"loss": result["loss"], "status": result["status"]},
                "spec": None,
                "state": 2,
                "tid": resultIndex,
                "version": 0,
            }

            for param in Hyperparameter(hyperparameterSpace).getFlatParameters():
                value = result[param.name]
                if value is not "" and value is not None:
                    if "enum" in param.config:
                        value = param.config["enum"].index(value)

                    data["misc"]["idxs"][param.hyperoptVariableName] = [resultIndex]
                    data["misc"]["vals"][param.hyperoptVariableName] = [value]
                else:
                    data["misc"]["idxs"][param.hyperoptVariableName] = []
                    data["misc"]["vals"][param.hyperoptVariableName] = []

            trials.insert_trial_doc(data)
        return trials

    def convertTrialsToResults(self, hyperparameterSpace, trials):
        results = []
        for trialIndex, trial in enumerate(trials.trials):
            data = {
                "trial": trialIndex,
                "status": trial["result"]["status"],
                "loss": trial["result"]["loss"],
                "log": "",
                "time": abs(
                    (trial["book_time"] - trial["refresh_time"]).total_seconds()
                ),
            }

            params = trial["misc"]["vals"]
            for param in Hyperparameter(hyperparameterSpace).getFlatParameters():
                key = param.hyperoptVariableName

                if len(params[key]) == 1:
                    value = params[key][0]
                    if "enum" in param.config:
                        value = param.config["enum"][value]

                    data[param.name] = value
                else:
                    data[param.name] = ""

            results.append(data)
        return results


def suggest(new_ids, domain, trials, seed):
    optimizer = ATPEOptimizer()

    # Convert the PyLL domain back into a descriptive form of hyperparameter space
    hyperparameterConfig = Hyperparameter.createHyperparameterConfigForHyperoptDomain(
        domain
    )

    results = optimizer.convertTrialsToResults(hyperparameterConfig, trials)

    # If there is a loss value that is negative, then we must increment the values so
    # they are all positive. This is because ATPE has been optimized only for positive
    # loss value
    if len(results) > 0:
        minVal = min(
            [result["loss"] for result in results if result["loss"] is not None]
        )
        if minVal < 0:
            for result in results:
                if result["loss"] is not None:
                    result["loss"] = result["loss"] - minVal + 0.1

    hyperparameters = Hyperparameter(hyperparameterConfig)

    rval = []
    for new_id in new_ids:
        parameters = optimizer.recommendNextParameters(
            hyperparameterConfig, results, currentTrials=[]
        )
        flatParameters = hyperparameters.convertToFlatValues(parameters)

        rval_results = [domain.new_result()]
        rval_miscs = [
            dict(
                tid=new_id,
                cmd=domain.cmd,
                workdir=domain.workdir,
                idxs={key: [0] for key in flatParameters},
                vals={key: [flatParameters[key]] for key in flatParameters},
            )
        ]

        rval.extend(trials.new_trial_docs([new_id], [None], rval_results, rval_miscs))

    return rval