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
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
|
#!/usr/bin/env python3
"""
Utilities for analytic description of parameter-dependent model attributes.
This module provides classes and helper functions useful for least-squares
regression and general handling of model functions.
"""
from itertools import chain, combinations
import logging
import numpy as np
import os
import re
from scipy import optimize
from .utils import is_numeric, param_to_ndarray
logger = logging.getLogger(__name__)
def powerset(iterable):
"""
Return powerset of `iterable` elements.
Example: `powerset([1, 2])` -> `[(), (1), (2), (1, 2)]`
"""
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
def gplearn_to_function(function_str: str):
"""
Convert gplearn-style function string to Python function.
Takes a function string like "mul(add(X0, X1), X2)" and returns
a Python function implementing the specified behaviour,
e.g. "lambda x, y, z: (x + y) * z".
Supported functions:
add -- x + y
sub -- x - y
mul -- x * y
div -- x / y if |y| > 0.001, otherwise 1
sqrt -- sqrt(|x|)
log -- log(|x|) if |x| > 0.001, otherwise 0
inv -- 1 / x if |x| > 0.001, otherwise 0
"""
eval_globals = {
"add": lambda x, y: x + y,
"sub": lambda x, y: x - y,
"mul": lambda x, y: x * y,
"div": lambda x, y: np.divide(x, y) if np.abs(y) > 0.001 else 1.0,
"sqrt": lambda x: np.sqrt(np.abs(x)),
"log": lambda x: np.log(np.abs(x)) if np.abs(x) > 0.001 else 0.0,
"inv": lambda x: 1.0 / x if np.abs(x) > 0.001 else 0.0,
}
last_arg_index = 0
for i in range(0, 100):
if function_str.find("X{:d}".format(i)) >= 0:
last_arg_index = i
arg_list = []
for i in range(0, last_arg_index + 1):
arg_list.append("X{:d}".format(i))
eval_str = "lambda {}, *whatever: {}".format(",".join(arg_list), function_str)
logger.debug(eval_str)
return eval(eval_str, eval_globals)
class ParamFunction:
"""
A one-dimensional model function, ready for least squares optimization and similar.
Supports validity checks (e.g. if it is undefined for x <= 0) and an
error measure.
"""
def __init__(self, param_function, validation_function, num_vars, repr_str=None):
"""
Create function object suitable for regression analysis.
This documentation assumes that 1-dimensional functions
(-> single float as model input) are used. However, n-dimensional
functions (-> list of float as model input) are also supported.
:param param_function: regression function (reg_param, model_param) -> float.
reg_param is a list of regression variable values,
model_param is the model input value (float).
Example: `lambda rp, mp: rp[0] + rp[1] * mp`
:param validation_function: function used to check whether param_function
is defined for a given model_param. Signature:
model_param -> bool
Example: `lambda mp: mp > 0`
:param num_vars: How many regression variables are used by this function,
i.e., the length of param_function's reg_param argument.
"""
self._param_function = param_function
self._validation_function = validation_function
self._num_variables = num_vars
self.repr_str = repr_str
def __repr__(self) -> str:
if self.repr_str:
return f"ParamFunction<{self.repr_str}>"
return f"ParamFunction<{self._param_function}, {self.validation_function}, {self._num_variables}>"
def is_valid(self, arg: float) -> bool:
"""
Check whether the regression function is defined for the given argument.
:param arg: argument (e.g. model parameter) to check for
:returns: True iff the function is defined for `arg`
"""
return self._validation_function(arg)
def eval(self, param: list, arg: float) -> float:
"""
Evaluate regression function.
:param param: regression variable values (list of float)
:param arg: model input (float)
:returns: regression function output (float)
"""
return self._param_function(param, arg)
def error_function(self, P: list, X: float, y: float) -> float:
"""
Calculate model error.
:param P: regression variables as returned by optimization (list of float)
:param X: model input (float)
:param y: expected model output / ground truth for model input (float)
:returns: Deviation between model output and ground truth (float)
"""
return self._param_function(P, X) - y
class NormalizationFunction:
"""
Wrapper for parameter normalization functions used in YAML PTA/DFA models.
"""
def __init__(self, function_str: str):
"""
Create a new normalization function from `function_str`.
:param function_str: Function string. Must use the single argument
`param` and return a float.
"""
self._function_str = function_str
self._function = eval("lambda param: " + function_str)
def eval(self, param_value: float) -> float:
"""
Evaluate the normalization function and return its output.
:param param_value: Parameter value
"""
return self._function(param_value)
class ModelFunction:
always_predictable = False
has_eval_arr = False
"""
Encapsulates the behaviour of a single model attribute, e.g. TX power or write duration.
The behaviour may be constant or depend on a number of factors. Modelfunction is a virtual base class,
individuel decendents describe actual behaviour.
Common attributes:
:param value: median data value
:type value: float
:param value_error: static model value error
:type value_error: dict, optional
:param function_error: model error
:type value_error: dict, optional
"""
def __init__(self, value, n_samples=None):
# a model always has a static (median/mean) value. For StaticFunction, it's the only data point.
# For more complex models, it's usede both as fallback in case the model cannot predict the current
# parameter combination, and for use cases requiring static models
self.value = value
self.n_samples = n_samples
# A ModelFunction may track its own accuracy, both of the static value and of the eval() method.
# However, it does not specify how the accuracy was calculated (e.g. which data was used and whether cross-validation was performed)
self.value_error = None
self.function_error = None
def is_predictable(self, param_list):
raise NotImplementedError
def eval(self, param_list):
raise NotImplementedError
def eval_arr(self, params):
raise NotImplementedError
def get_complexity_score(self):
raise NotImplementedError
def eval_mae(self, param_list):
"""Return model Mean Absolute Error (MAE) for `param_list`."""
if self.is_predictable(param_list):
return self.function_error["mae"]
return self.value_error["mae"]
def webconf_function_map(self):
return list()
def to_json(self, **kwargs):
"""Convert model to JSON."""
ret = {
"value": self.value,
"n_samples": self.n_samples,
}
if self.value_error is not None:
ret["valueError"] = self.value_error
if self.function_error is not None:
ret["functionError"] = self.function_error
return ret
def hyper_to_dref(self):
return dict()
@classmethod
def from_json(cls, data):
"""
Create ModelFunction instance from JSON.
Delegates to StaticFunction, SplitFunction, etc. as appropriate.
"""
if data["type"] == "static":
mf = StaticFunction.from_json(data)
elif data["type"] == "split":
mf = SplitFunction.from_json(data)
elif data["type"] == "scalarSplit":
mf = ScalarSplitFunction.from_json(data)
elif data["type"] == "analytic":
mf = AnalyticFunction.from_json(data)
else:
raise ValueError("Unknown ModelFunction type: " + data["type"])
if "valueError" in data:
mf.value_error = data["valueError"]
if "functionError" in data:
mf.function_error = data["functionError"]
return mf
@classmethod
def from_json_maybe(cls, json_wrapped: dict, attribute: str):
# Legacy Code for PTA / tests. Do not use.
if type(json_wrapped) is dict and attribute in json_wrapped:
# benchmark data obtained before 2021-03-04 uses {"attr": {"static": 0}}
# benchmark data obtained after 2021-03-04 uses {"attr": {"type": "static", "value": 0}} or {"attr": None}
# from_json expects the latter.
if json_wrapped[attribute] is None:
return None
if (
"static" in json_wrapped[attribute]
and "type" not in json_wrapped[attribute]
):
json_wrapped[attribute]["type"] = "static"
json_wrapped[attribute]["value"] = json_wrapped[attribute]["static"]
json_wrapped[attribute].pop("static")
return cls.from_json(json_wrapped[attribute])
return StaticFunction(0)
class StaticFunction(ModelFunction):
always_predictable = True
has_eval_arr = True
def is_predictable(self, param_list=None):
"""
Return whether the model function can be evaluated on the given parameter values.
For a StaticFunction, this is always the case (i.e., this function always returns true).
"""
return True
def eval(self, param_list=None):
"""
Evaluate model function with specified param/arg values.
Far a Staticfunction, this is just the static value
"""
return self.value
def eval_arr(self, params):
return [self.value for p in params]
def get_complexity_score(self):
return 1
def to_json(self, **kwargs):
ret = super().to_json(**kwargs)
ret.update({"type": "static", "value": self.value})
return ret
def to_dot(self, pydot, graph, feature_names, parent=None):
graph.add_node(
pydot.Node(str(id(self)), label=f"{self.value:.2f}", shape="rectangle")
)
@classmethod
def from_json(cls, data):
assert data["type"] == "static"
return cls(data["value"])
def __repr__(self):
return f"StaticFunction({self.value})"
class SplitFunction(ModelFunction):
def __init__(self, value, param_index, param_name, child, **kwargs):
super().__init__(value, **kwargs)
self.param_name = param_name
self.param_index = param_index
self.child = child
self.use_weighted_avg = bool(int(os.getenv("DFATOOL_RMT_WEIGHTED_AVG", "0")))
def is_predictable(self, param_list):
"""
Return whether the model function can be evaluated on the given parameter values.
The first value corresponds to the lexically first model parameter, etc.
All parameters must be set, not just the ones this function depends on.
Returns False iff a parameter the function depends on is not numeric
(e.g. None).
"""
param_value = param_list[self.param_index]
if param_value in self.child:
return self.child[param_value].is_predictable(param_list)
return all(
map(lambda child: child.is_predictable(param_list), self.child.values())
)
def eval(self, param_list):
param_value = param_list[self.param_index]
if param_value in self.child:
return self.child[param_value].eval(param_list)
if self.use_weighted_avg:
return np.average(
list(map(lambda child: child.eval(param_list), self.child.values())),
weights=list(map(lambda child: child.n_samples, self.child.values())),
)
return np.mean(
list(map(lambda child: child.eval(param_list), self.child.values()))
)
def webconf_function_map(self):
ret = list()
for child in self.child.values():
ret.extend(child.webconf_function_map())
return ret
def to_json(self, **kwargs):
ret = super().to_json(**kwargs)
update = {
"type": "split",
"paramIndex": self.param_index,
"paramName": self.param_name,
"child": dict([[k, v.to_json(**kwargs)] for k, v in self.child.items()]),
}
ret.update(update)
return ret
def get_number_of_nodes(self):
ret = 1
for v in self.child.values():
if type(v) in (SplitFunction, ScalarSplitFunction):
ret += v.get_number_of_nodes()
else:
ret += 1
return ret
def get_max_depth(self):
ret = [0]
for v in self.child.values():
if type(v) is SplitFunction:
ret.append(v.get_max_depth())
return 1 + max(ret)
def get_number_of_leaves(self):
ret = 0
for v in self.child.values():
if type(v) is SplitFunction:
ret += v.get_number_of_leaves()
else:
ret += 1
return ret
def get_complexity_score(self):
if not self.child:
return 1
ret = 1
for v in self.child.values():
ret += v.get_complexity_score()
return ret
def to_dot(self, pydot, graph, feature_names, parent=None):
try:
label = feature_names[self.param_index]
except IndexError:
label = f"param{self.param_index}"
graph.add_node(pydot.Node(str(id(self)), label=label))
for key, child in self.child.items():
child.to_dot(pydot, graph, feature_names, str(id(self)))
graph.add_edge(pydot.Edge(str(id(self)), str(id(child)), label=key))
@classmethod
def from_json(cls, data):
assert data["type"] == "split"
self = cls(data["value"], data["paramIndex"], data["paramName"], dict())
for k, v in data["child"].items():
self.child[k] = ModelFunction.from_json(v)
return self
def __repr__(self):
return f"SplitFunction<{self.value}, param_index={self.param_index}>"
class ScalarSplitFunction(ModelFunction):
def __init__(
self, value, param_index, param_name, threshold, child_le, child_gt, **kwargs
):
super().__init__(value, **kwargs)
self.param_index = param_index
self.param_name = param_name
self.threshold = threshold
self.child_le = child_le
self.child_gt = child_gt
def is_predictable(self, param_list):
"""
Return whether the model function can be evaluated on the given parameter values.
"""
return is_numeric(param_list[self.param_index])
def eval(self, param_list):
param_value = param_list[self.param_index]
if param_value <= self.threshold:
return self.child_le.eval(param_list)
return self.child_gt.eval(param_list)
def webconf_function_map(self):
return (
self.child_le.webconf_function_map() + self.child_gt.webconf_function_map()
)
def to_json(self, **kwargs):
ret = super().to_json(**kwargs)
update = {
"type": "scalarSplit",
"paramIndex": self.param_index,
"paramName": self.param_name,
"threshold": self.threshold,
"left": self.child_le.to_json(**kwargs),
"right": self.child_gt.to_json(**kwargs),
}
ret.update(update)
return ret
def get_number_of_nodes(self):
ret = 1
for v in (self.child_le, self.child_gt):
if type(v) in (SplitFunction, ScalarSplitFunction):
ret += v.get_number_of_nodes()
else:
ret += 1
return ret
def get_max_depth(self):
ret = [0]
for v in (self.child_le, self.child_gt):
if type(v) in (SplitFunction, ScalarSplitFunction):
ret.append(v.get_max_depth())
return 1 + max(ret)
def get_number_of_leaves(self):
ret = 0
for v in (self.child_le, self.child_gt):
if type(v) in (SplitFunction, ScalarSplitFunction):
ret += v.get_number_of_leaves()
else:
ret += 1
return ret
def get_complexity_score(self):
ret = 1
for v in (self.child_le, self.child_gt):
ret += v.get_complexity_score()
return ret
def to_dot(self, pydot, graph, feature_names, parent=None):
try:
label = feature_names[self.param_index]
except IndexError:
label = f"param{self.param_index}"
graph.add_node(pydot.Node(str(id(self)), label=label))
for key, child in self.child.items():
child.to_dot(pydot, graph, feature_names, str(id(self)))
graph.add_edge(pydot.Edge(str(id(self)), str(id(child)), label=key))
@classmethod
def from_json(cls, data):
assert data["type"] == "scalarSplit"
left = ModelFunction.from_json(data["left"])
right = ModelFunction.from_json(data["right"])
self = cls(
data.get("value", 0),
data["paramIndex"],
data["paramName"],
data["threshold"],
left,
right,
)
return self
def __repr__(self):
return f"ScalarSplitFunction<{self.value}, param_index={self.param_index}>"
class SubstateFunction(ModelFunction):
def __init__(self, value, sequence_by_count, count_model, sub_model, **kwargs):
super().__init__(value, **kwargs)
self.sequence_by_count = sequence_by_count
self.count_model = count_model
self.sub_model = sub_model
# only used by analyze-archive model quality evaluation. Not serialized.
self.static_duration = None
def is_predictable(self, param_list):
substate_count = round(self.count_model.eval(param_list))
return substate_count in self.sequence_by_count
def eval(self, param_list, duration=None):
substate_count = round(self.count_model.eval(param_list))
cumulative_energy = 0
total_duration = 0
substate_model, _ = self.sub_model.get_fitted()
substate_sequence = self.sequence_by_count[substate_count]
for i, sub_name in enumerate(substate_sequence):
sub_duration = substate_model(sub_name, "duration", param=param_list)
sub_power = substate_model(sub_name, "power", param=param_list)
if i == substate_count - 1:
if duration is not None:
sub_duration = duration - total_duration
elif self.static_duration is not None:
sub_duration = self.static_duration - total_duration
cumulative_energy += sub_power * sub_duration
total_duration += sub_duration
return cumulative_energy / total_duration
def to_json(self, **kwargs):
ret = super().to_json(**kwargs)
ret.update(
{
"type": "substate",
"sequence": self.sequence_by_count,
"countModel": self.count_model.to_json(**kwargs),
"subModel": self.sub_model.to_json(**kwargs),
}
)
return ret
@classmethod
def from_json(cls, data):
assert data["type"] == "substate"
raise NotImplementedError
def __repr__(self):
return "SubstateFunction"
class SKLearnRegressionFunction(ModelFunction):
always_predictable = True
has_eval_arr = True
def __init__(self, value, **kwargs):
# Needed for JSON export
self.param_names = kwargs.pop("param_names")
self.arg_count = kwargs.pop("arg_count")
self.param_names_and_args = self.param_names + list(
map(lambda i: f"arg{i}", range(self.arg_count))
)
super().__init__(value, **kwargs)
self.categorical_to_scalar = bool(
int(os.getenv("DFATOOL_PARAM_CATEGORICAL_TO_SCALAR", "0"))
)
self.fit_success = None
def _build_feature_names(self):
# SKLearnRegressionFunction descendants use self.param_names \ self.ignore_index as features.
# Thus, model feature indexes ≠ self.param_names indexes.
# self.feature_names accounts for this and allows mapping feature indexes back to parameter names / parameter indexes.
self.feature_names = list(
map(
lambda i: self.param_names[i],
filter(
lambda i: not self.ignore_index[i],
range(len(self.param_names)),
),
)
)
self.feature_names += list(
map(
lambda i: f"arg{i-len(self.param_names)}",
filter(
lambda i: not self.ignore_index[i],
range(
len(self.param_names),
len(self.param_names) + self.arg_count,
),
),
)
)
def fit(self, param_values, data, ignore_param_indexes=None):
raise NotImplementedError
def is_predictable(self, param_list=None):
return self.fit_success
def eval(self, param_list=None):
"""
Evaluate model function with specified param/arg values.
Far a Staticfunction, this is just the static value
"""
if param_list is None:
return self.value
actual_param_list = list()
for i, param in enumerate(param_list):
if not self.ignore_index[i]:
if i in self.categorical_to_index:
try:
actual_param_list.append(self.categorical_to_index[i][param])
except KeyError:
# param was not part of training data. substitute an unused scalar.
# Note that all param values which were not part of training data map to the same scalar this way.
# This should be harmless.
actual_param_list.append(
max(self.categorical_to_index[i].values()) + 1
)
else:
actual_param_list.append(int(param))
predictions = self.regressor.predict(np.array([actual_param_list]))
if predictions.shape == (1,):
return predictions[0]
return predictions
def eval_arr(self, params):
actual_params = list()
for param_tuple in params:
actual_param_list = list()
for i, param in enumerate(param_tuple):
if not self.ignore_index[i]:
if i in self.categorical_to_index:
try:
actual_param_list.append(
self.categorical_to_index[i][param]
)
except KeyError:
# param was not part of training data. substitute an unused scalar.
# Note that all param values which were not part of training data map to the same scalar this way.
# This should be harmless.
actual_param_list.append(
max(self.categorical_to_index[i].values()) + 1
)
else:
actual_param_list.append(int(param))
actual_params.append(actual_param_list)
predictions = self.regressor.predict(np.array(actual_params))
return predictions
def to_json(self, **kwargs):
ret = super().to_json(**kwargs)
# Note: categorical_to_index uses param_names, not feature_names
param_names = self.param_names + list(
map(
lambda i: f"arg{i-len(self.param_names)}",
range(
len(self.param_names),
len(self.param_names) + self.arg_count,
),
)
)
ret["paramValueToIndex"] = dict(
map(
lambda kv: (param_names[kv[0]], kv[1]),
self.categorical_to_index.items(),
)
)
return ret
class CARTFunction(SKLearnRegressionFunction):
def __init__(self, value, decart=False, **kwargs):
self.decart = decart
super().__init__(value, **kwargs)
def fit(self, param_values, data, scalar_param_indexes=None):
max_depth = int(os.getenv("DFATOOL_CART_MAX_DEPTH", "0"))
if max_depth == 0:
max_depth = None
if self.decart:
fit_parameters, self.categorical_to_index, self.ignore_index = (
param_to_ndarray(
param_values,
with_nan=False,
categorical_to_scalar=self.categorical_to_scalar,
ignore_indexes=scalar_param_indexes,
)
)
else:
fit_parameters, self.categorical_to_index, self.ignore_index = (
param_to_ndarray(
param_values,
with_nan=False,
categorical_to_scalar=self.categorical_to_scalar,
)
)
if fit_parameters.shape[1] == 0:
logger.warning(
f"Cannot generate CART due to lack of parameters: parameter shape is {np.array(param_values).shape}, fit_parameter shape is {fit_parameters.shape}"
)
self.fit_success = False
return self
logger.debug("Fitting sklearn CART ...")
from sklearn.tree import DecisionTreeRegressor
self.regressor = DecisionTreeRegressor(max_depth=max_depth)
self.regressor.fit(fit_parameters, data)
logger.debug("Fitted sklearn CART")
self.fit_success = True
self._build_feature_names()
return self
def get_number_of_nodes(self):
return self.regressor.tree_.node_count
def get_number_of_leaves(self):
return self.regressor.tree_.n_leaves
def get_max_depth(self):
return self.regressor.get_depth()
def get_complexity_score(self):
return self.get_number_of_nodes()
def to_json(self, **kwargs):
import sklearn.tree
self.leaf_id = sklearn.tree._tree.TREE_LEAF
ret = super().to_json(**kwargs)
ret.update(self.recurse_(self.regressor.tree_, 0))
return ret
def hyper_to_dref(self):
return {
"cart/max depth": self.regressor.max_depth or "infty",
"cart/min samples split": self.regressor.min_samples_split,
"cart/min samples leaf": self.regressor.min_samples_leaf,
"cart/min impurity decrease": self.regressor.min_impurity_decrease,
"cart/max leaf nodes": self.regressor.max_leaf_nodes or "infty",
"cart/criterion": self.regressor.criterion,
"cart/splitter": self.regressor.splitter,
}
# recursive function for all nodes:
def recurse_(self, tree, node_id, depth=0):
left_child = tree.children_left[node_id]
right_child = tree.children_right[node_id]
# basic leaf with standard values
# conversion because of numpy
sub_data = {
"type": "static",
"value": float(tree.value[node_id]),
"valueError": float(tree.impurity[node_id]),
# "samples": int(tree.n_node_samples[node_id])
}
# if has childs / not a leaf:
if left_child != self.leaf_id or right_child != self.leaf_id:
# sub_data["paramName"] = "X[" + str(self.regressor.tree_.feature[left_child_id]) + "]"
# sub_data["paramIndex"] = int(self.regressor.tree_.feature[left_child_id])
try:
sub_data["paramName"] = self.feature_names[
self.regressor.tree_.feature[node_id]
]
sub_data["paramIndex"] = self.param_names_and_args.index(
sub_data["paramName"]
)
except IndexError:
sub_data["paramName"] = "arg" + str(
self.regressor.tree_.feature[node_id] - len(self.feature_names)
)
sub_data["paramIndex"] = (
len(self.param_names)
+ self.regressor.tree_.feature[node_id]
- len(self.feature_names)
)
except ValueError:
sub_data["paramIndex"] = (
len(self.param_names)
+ self.regressor.tree_.feature[node_id]
- len(self.feature_names)
)
sub_data["threshold"] = tree.threshold[node_id]
sub_data["type"] = "scalarSplit"
# child value
if left_child != self.leaf_id:
sub_data["left"] = self.recurse_(tree, left_child, depth=depth + 1)
if right_child != self.leaf_id:
sub_data["right"] = self.recurse_(tree, right_child, depth=depth + 1)
return sub_data
class LMTFunction(SKLearnRegressionFunction):
def fit(self, param_values, data):
# max_depth : int, default=5
# The maximum depth of the tree considering only the splitting nodes.
# A higher value implies a higher training time.
max_depth = int(os.getenv("DFATOOL_LMT_MAX_DEPTH", "5"))
# min_samples_split : int or float, default=6
# The minimum number of samples required to split an internal node.
# The minimum valid number of samples in each node is 6.
# A lower value implies a higher training time.
# - If int, then consider `min_samples_split` as the minimum number.
# - If float, then `min_samples_split` is a fraction and
# `ceil(min_samples_split * n_samples)` are the minimum
# number of samples for each split.
if "." in os.getenv("DFATOOL_LMT_MIN_SAMPLES_SPLIT", ""):
min_samples_split = float(os.getenv("DFATOOL_LMT_MIN_SAMPLES_SPLIT"))
else:
min_samples_split = int(os.getenv("DFATOOL_LMT_MIN_SAMPLES_SPLIT", "6"))
# min_samples_leaf : int or float, default=0.1
# The minimum number of samples required to be at a leaf node.
# A split point at any depth will only be considered if it leaves at
# least `min_samples_leaf` training samples in each of the left and
# right branches.
# The minimum valid number of samples in each leaf is 3.
# A lower value implies a higher training time.
# - If int, then consider `min_samples_leaf` as the minimum number.
# - If float, then `min_samples_leaf` is a fraction and
# `ceil(min_samples_leaf * n_samples)` are the minimum
# number of samples for each node.
if "." in os.getenv("DFATOOL_LMT_MIN_SAMPLES_LEAF", "0.1"):
min_samples_leaf = float(os.getenv("DFATOOL_LMT_MIN_SAMPLES_LEAF", "0.1"))
else:
min_samples_leaf = int(os.getenv("DFATOOL_LMT_MIN_SAMPLES_LEAF"))
# max_bins : int, default=25
# The maximum number of bins to use to search the optimal split in each
# feature. Features with a small number of unique values may use less than
# ``max_bins`` bins. Must be lower than 120 and larger than 10.
# A higher value implies a higher training time.
max_bins = int(os.getenv("DFATOOL_LMT_MAX_BINS", "120"))
# criterion : {"mse", "rmse", "mae", "poisson"}, default="mse"
# The function to measure the quality of a split. "poisson"
# requires ``y >= 0``.
criterion = os.getenv("DFATOOL_LMT_CRITERION", "mse")
from sklearn.linear_model import LinearRegression
from dfatool.lineartree import LinearTreeRegressor
lmt = LinearTreeRegressor(
base_estimator=LinearRegression(),
max_depth=max_depth,
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
max_bins=max_bins,
criterion=criterion,
)
fit_parameters, self.categorical_to_index, self.ignore_index = param_to_ndarray(
param_values,
with_nan=False,
categorical_to_scalar=self.categorical_to_scalar,
)
if fit_parameters.shape[1] == 0:
logger.warning(
f"Cannot generate LMT due to lack of parameters: parameter shape is {np.array(param_values).shape}, fit_parameter shape is {fit_parameters.shape}"
)
self.fit_success = False
return self
logger.debug("Fitting LMT ...")
try:
lmt.fit(fit_parameters, data)
except np.linalg.LinAlgError as e:
logger.error(f"LMT generation failed: {e}")
self.fit_success = False
return self
logger.debug("Fitted LMT")
self.regressor = lmt
self.fit_success = True
self._build_feature_names()
return self
def get_number_of_nodes(self):
return self.regressor.node_count
def get_number_of_leaves(self):
return len(self.regressor._leaves.keys())
def get_complexity_score(self):
ret = self.get_number_of_nodes() - self.get_number_of_leaves()
for leaf in self.regressor._leaves.values():
ret += len(
list(
filter(lambda x: x > 0, leaf.model.coef_ + [leaf.model.intercept_])
)
)
return ret
def get_max_depth(self):
return max(map(len, self.regressor._leaves.keys())) + 1
def to_json(self, **kwargs):
ret = super().to_json(**kwargs)
ret.update(self.recurse_(self.regressor.summary(), 0))
return ret
def hyper_to_dref(self):
return {
"lmt/max depth": self.regressor.max_depth,
"lmt/max bins": self.regressor.max_bins,
"lmt/min samples split": self.regressor.min_samples_split,
"lmt/min samples leaf": self.regressor.min_samples_leaf,
"lmt/criterion": self.regressor.criterion,
}
def recurse_(self, node_hash, node_index):
node = node_hash[node_index]
sub_data = dict()
if "th" in node:
return {
"type": "scalarSplit",
"paramName": self.feature_names[node["col"]],
"paramIndex": self.param_names_and_args.index(
self.feature_names[node["col"]]
),
"threshold": node["th"],
"left": self.recurse_(node_hash, node["children"][0]),
"right": self.recurse_(node_hash, node["children"][1]),
}
model = node["models"]
fs = "0 + regression_arg(0)"
for i, coef in enumerate(model.coef_):
if coef:
fs += f" + regression_arg({i+1}) * parameter({self.feature_names[i]})"
return {
"type": "analytic",
"functionStr": fs,
"parameterNames": self.param_names,
"regressionModel": [model.intercept_] + list(model.coef_),
}
class XGBoostFunction(SKLearnRegressionFunction):
def fit(self, param_values, data):
# <https://xgboost.readthedocs.io/en/stable/python/python_api.html#module-xgboost.sklearn>
# <https://xgboost.readthedocs.io/en/stable/parameter.html#parameters-for-tree-booster>
# n_estimators := number of trees in forest
# max_depth := maximum tree depth
# eta <=> learning_rate
# n_estimators : Optional[int]
# Number of gradient boosted trees. Equivalent to number of boosting
# rounds.
# xgboost/sklearn.py: DEFAULT_N_ESTIMATORS = 100
n_estimators = int(os.getenv("DFATOOL_XGB_N_ESTIMATORS", "100"))
# max_depth : Optional[int] [default=6]
# Maximum tree depth for base learners.
# Maximum depth of a tree. Increasing this value will make the model more complex and more likely to overfit. 0 indicates no limit on depth. Beware
# that XGBoost aggressively consumes memory when training a deep tree. exact tree method requires non-zero value.
# range: [0,∞]
max_depth = int(os.getenv("DFATOOL_XGB_MAX_DEPTH", "6"))
# max_leaves : [default=0]
# Maximum number of leaves; 0 indicates no limit.
# Maximum number of nodes to be added. Not used by exact tree method.
max_leaves = int(os.getenv("DFATOOL_XGB_MAX_LEAVES", "0"))
# learning_rate : Optional[float] [default=0.3]
# Boosting learning rate (xgb's "eta")
# Step size shrinkage used in update to prevents overfitting. After each boosting step, we can directly get the weights of new features, and eta
# shrinks the feature weights to make the boosting process more conservative.
# range: [0,1]
learning_rate = float(os.getenv("DFATOOL_XGB_ETA", "0.3"))
# gamma : Optional[float] [default=0]
# (min_split_loss) Minimum loss reduction required to make a further partition on a
# leaf node of the tree.
# Minimum loss reduction required to make a further partition on a leaf node of the tree. The larger gamma is, the more conservative the algorithm will be.
# range: [0,∞]
gamma = float(os.getenv("DFATOOL_XGB_GAMMA", "0"))
# subsample : Optional[float] [default=1]
# Subsample ratio of the training instance.
# Subsample ratio of the training instances. Setting it to 0.5 means that XGBoost would randomly sample half of the training data prior to growing
# trees. and this will prevent overfitting. Subsampling will occur once in every boosting iteration.
# range: (0,1]
subsample = float(os.getenv("DFATOOL_XGB_SUBSAMPLE", "1"))
# reg_alpha : Optional[float] [default=0]
# L1 regularization term on weights (xgb's alpha).
# L1 regularization term on weights. Increasing this value will make model more conservative.
# range: [0, ∞]
reg_alpha = float(os.getenv("DFATOOL_XGB_REG_ALPHA", "0"))
# reg_lambda : Optional[float] [default=1]
# L2 regularization term on weights (xgb's lambda).
# L2 regularization term on weights. Increasing this value will make model more conservative.
# range: [0, ∞]
reg_lambda = float(os.getenv("DFATOOL_XGB_REG_LAMBDA", "1"))
fit_parameters, self.categorical_to_index, self.ignore_index = param_to_ndarray(
param_values,
with_nan=False,
categorical_to_scalar=self.categorical_to_scalar,
)
if fit_parameters.shape[1] == 0:
logger.warning(
f"Cannot run XGBoost due to lack of parameters: parameter shape is {np.array(param_values).shape}, fit_parameter shape is {fit_parameters.shape}"
)
self.fit_success = False
return self
import xgboost
xgb = xgboost.XGBRegressor(
n_estimators=n_estimators,
max_depth=max_depth,
max_leaves=max_leaves,
subsample=subsample,
learning_rate=learning_rate,
gamma=gamma,
reg_alpha=reg_alpha,
reg_lambda=reg_lambda,
)
xgb.fit(fit_parameters, np.reshape(data, (-1, 1)))
self.fit_success = True
self.regressor = xgb
self._build_feature_names()
if output_filename := os.getenv("DFATOOL_XGB_DUMP_MODEL", None):
xgb.get_booster().dump_model(
output_filename, dump_format="json", with_stats=True
)
return self
def to_json(self, internal=False, **kwargs):
import json
tempfile = f"/tmp/xgb{os.getpid()}.json"
self.regressor.get_booster().dump_model(
tempfile, dump_format="json", with_stats=True
)
with open(tempfile, "r") as f:
data = json.load(f)
os.remove(tempfile)
if internal:
return data
return list(
map(
lambda tree: self.tree_to_webconf_json(tree, **kwargs),
data,
)
)
def tree_to_webconf_json(self, tree, **kwargs):
ret = dict()
if "children" in tree:
return {
"type": "scalarSplit",
"paramName": self.feature_names[int(tree["split"][1:])],
"threshold": tree["split_condition"],
"value": None,
"left": self.tree_to_webconf_json(tree["children"][0], **kwargs),
"right": self.tree_to_webconf_json(tree["children"][1], **kwargs),
}
else:
return {
"type": "static",
"value": tree["leaf"],
}
def get_number_of_nodes(self):
return sum(map(self._get_number_of_nodes, self.to_json(internal=True)))
def _get_number_of_nodes(self, data):
ret = 1
for child in data.get("children", list()):
ret += self._get_number_of_nodes(child)
return ret
def get_number_of_leaves(self):
return sum(map(self._get_number_of_leaves, self.to_json(internal=True)))
def _get_number_of_leaves(self, data):
if "leaf" in data:
return 1
ret = 0
for child in data.get("children", list()):
ret += self._get_number_of_leaves(child)
return ret
def get_max_depth(self):
return max(map(self._get_max_depth, self.to_json(internal=True)))
def _get_max_depth(self, data):
ret = [0]
for child in data.get("children", list()):
ret.append(self._get_max_depth(child))
return 1 + max(ret)
def get_complexity_score(self):
return self.get_number_of_nodes()
def hyper_to_dref(self):
return {
"xgb/n estimators": self.regressor.n_estimators,
"xgb/max depth": self.regressor.max_depth == 0
and "infty"
or self.regressor.max_depth,
"xgb/max leaves": self.regressor.max_leaves == 0
and "infty"
or self.regressor.max_leaves,
"xgb/subsample": self.regressor.subsample,
"xgb/eta": self.regressor.learning_rate,
"xgb/gamma": self.regressor.gamma,
"xgb/alpha": self.regressor.reg_alpha,
"xgb/lambda": self.regressor.reg_lambda,
}
class SymbolicRegressionFunction(SKLearnRegressionFunction):
def fit(self, param_values, data, ignore_param_indexes=None):
# population_size : integer, optional (default=1000)
# The number of programs in each generation.
population_size = int(os.getenv("DFATOOL_SYMREG_POPULATION_SIZE", "1000"))
# generations : integer, optional (default=20)
# The number of generations to evolve.
generations = int(os.getenv("DFATOOL_SYMREG_GENERATIONS", "20"))
# tournament_size : integer, optional (default=20)
# The number of programs that will compete to become part of the next
# generation.
tournament_size = int(os.getenv("DFATOOL_SYMREG_TOURNAMENT_SIZE", "20"))
# const_range : tuple of two floats, or None, optional (default=(-1., 1.))
# The range of constants to include in the formulas. If None then no
# constants will be included in the candidate programs.
if cr := os.getenv("DFATOOL_SYMREG_CONST_RANGE", None):
if cr == "none":
const_range = None
else:
const_range = tuple(map(float, cr.split(",")))
else:
const_range = (-1.0, 1.0)
# function_set : iterable, optional (default=('add', 'sub', 'mul', 'div'))
# The functions to use when building and evolving programs. This iterable
# can include strings to indicate either individual functions as outlined
# below, or you can also include your own functions as built using the
# ``make_function`` factory from the ``functions`` module.
function_set = tuple(
os.getenv("DFATOOL_SYMREG_FUNCTION_SET", "add sub mul div").split()
)
# metric : str, optional (default='mean absolute error')
# The name of the raw fitness metric. Available options include:
metric = os.getenv("DFATOOL_SYMREG_METRIC", "mse")
# parsimony_coefficient : float or "auto", optional (default=0.001)
# This constant penalizes large programs by adjusting their fitness to
# be less favorable for selection. Larger values penalize the program
# more which can control the phenomenon known as 'bloat'. Bloat is when
# evolution is increasing the size of programs without a significant
# increase in fitness, which is costly for computation time and makes for
# a less understandable final result. This parameter may need to be tuned
# over successive runs.
#
# If "auto" the parsimony coefficient is recalculated for each generation
# using c = Cov(l,f)/Var( l), where Cov(l,f) is the covariance between
# program size l and program fitness f in the population, and Var(l) is
# the variance of program sizes.
parsimony_coefficient = float(
os.getenv("DFATOOL_SYMREG_PARSIMONY_COEFFICIENT", "0.001")
)
# n_jobs : integer, optional (default=1)
# The number of jobs to run in parallel for `fit`. If -1, then the number
# of jobs is set to the number of cores.
n_jobs = int(os.getenv("DFATOOL_SYMREG_N_JOBS", "1"))
# verbose : int, optional (default=0)
# Controls the verbosity of the evolution building process.
verbose = int(os.getenv("DFATOOL_SYMREG_VERBOSE", "0"))
fit_parameters, self.categorical_to_index, self.ignore_index = param_to_ndarray(
param_values,
with_nan=False,
categorical_to_scalar=self.categorical_to_scalar,
ignore_indexes=ignore_param_indexes,
)
if fit_parameters.shape[1] == 0:
logger.debug(
f"Cannot use Symbolic Regression due to lack of parameters: parameter shape is {np.array(param_values).shape}, fit_parameter shape is {fit_parameters.shape}"
)
self.fit_success = False
return self
from dfatool.gplearn.genetic import SymbolicRegressor
self._build_feature_names()
self.regressor = SymbolicRegressor(
population_size=population_size,
generations=generations,
tournament_size=tournament_size,
const_range=const_range,
function_set=function_set,
metric=metric,
parsimony_coefficient=parsimony_coefficient,
n_jobs=n_jobs,
verbose=verbose,
feature_names=self.feature_names,
)
self.regressor.fit(fit_parameters, data)
self.fit_success = True
return self
def get_complexity_score(self):
rstr = str(self.regressor)
return rstr.count(",") * 2 + 1
def hyper_to_dref(self):
return {
"symreg/population size": self.regressor.population_size,
"symreg/generations": self.regressor.generations,
"symreg/tournament size": self.regressor.tournament_size,
"symreg/const range/min": self.regressor.const_range[0],
"symreg/const range/max": self.regressor.const_range[1],
"symreg/function set": " ".join(self.regressor.function_set),
"symreg/metric": self.regressor.metric,
"symreg/parsimony coefficient": self.regressor.parsimony_coefficient,
"symreg/n jobs": self.regressor.n_jobs,
}
# first-order linear function (no feature interaction)
class FOLFunction(ModelFunction):
always_predictable = True
def __init__(self, value, parameters, num_args=0, **kwargs):
super().__init__(value, **kwargs)
self.parameter_names = parameters
self._num_args = num_args
self.fit_success = False
def fit(self, param_values, data, ignore_param_indexes=None):
self.categorical_to_scalar = bool(
int(os.getenv("DFATOOL_PARAM_CATEGORICAL_TO_SCALAR", "0"))
)
second_order = int(os.getenv("DFATOOL_FOL_SECOND_ORDER", "0"))
fit_parameters, categorical_to_index, ignore_index = param_to_ndarray(
param_values,
with_nan=False,
categorical_to_scalar=self.categorical_to_scalar,
ignore_indexes=ignore_param_indexes,
)
self.categorical_to_index = categorical_to_index
self.ignore_index = ignore_index
fit_parameters = fit_parameters.swapaxes(0, 1)
if second_order:
num_param = fit_parameters.shape[0]
rawbuf = "reg_param[0]"
num_vars = 1
for i in range(num_param):
if second_order == 2:
rawbuf += f" + reg_param[{num_vars}] * model_param[{i}]"
num_vars += 1
for j in range(i + 1, num_param):
rawbuf += f" + reg_param[{num_vars}] * model_param[{i}] * model_param[{j}]"
num_vars += 1
funbuf = "regression_arg(0)"
num_vars = 1
for j, param_name in enumerate(self.parameter_names):
if ignore_index[j]:
continue
else:
if second_order == 2:
funbuf += (
f" + regression_arg({num_vars}) * parameter({param_name})"
)
num_vars += 1
for k in range(j + 1, len(self.parameter_names)):
if ignore_index[j]:
continue
funbuf += f" + regression_arg({num_vars}) * parameter({param_name}) * parameter({self.parameter_names[k]})"
num_vars += 1
else:
num_vars = fit_parameters.shape[0] + 1
rawbuf = "reg_param[0]"
for i in range(1, num_vars):
rawbuf += f" + reg_param[{i}] * model_param[{i-1}]"
funbuf = "regression_arg(0)"
i = 1
for j, param_name in enumerate(self.parameter_names):
if ignore_index[j]:
continue
else:
funbuf += f" + regression_arg({i}) * parameter({param_name})"
i += 1
self.model_function = funbuf
self._function_str = "lambda reg_param, model_param:" + rawbuf
self._function = eval(self._function_str)
error_function = lambda P, X, y: self._function(P, X) - y
self.model_args = list(np.ones((num_vars)))
try:
res = optimize.least_squares(
error_function, self.model_args, args=(fit_parameters, data), xtol=2e-15
)
except ValueError as err:
logger.warning(f"Fit failed: {err} (function: {self.model_function})")
return self
if res.status > 0:
self.model_args = res.x
self.fit_success = True
else:
logger.warning(
f"Fit failed: {res.message} (function: {self.model_function})"
)
return self
def is_predictable(self, param_list=None):
"""
Return whether the model function can be evaluated on the given parameter values.
"""
return True
def eval(self, param_list=None):
"""
Evaluate model function with specified param/arg values.
Far a Staticfunction, this is just the static value
"""
if param_list is None:
return self.value
actual_param_list = list()
for i, param in enumerate(param_list):
if not self.ignore_index[i]:
if i in self.categorical_to_index:
try:
actual_param_list.append(self.categorical_to_index[i][param])
except KeyError:
# param was not part of training data. substitute an unused scalar.
# Note that all param values which were not part of training data map to the same scalar this way.
# This should be harmless.
actual_param_list.append(
max(self.categorical_to_index[i].values()) + 1
)
else:
actual_param_list.append(int(param))
try:
return self._function(self.model_args, actual_param_list)
except FloatingPointError as e:
logger.error(
f"{e} when predicting {self._function_str}({self.model_args}, {actual_param_list}) for {param_list}, returning static value"
)
return self.value
except TypeError as e:
logger.error(
f"{e} when predicting {self._function_str}({self.model_args}, {actual_param_list}) for {param_list}"
)
raise
def get_complexity_score(self):
return len(self.model_args)
def to_dot(self, pydot, graph, feature_names, parent=None):
model_function = self.model_function
for i, arg in enumerate(self.model_args):
model_function = model_function.replace(
f"regression_arg({i})", f"{arg:.2f}"
)
graph.add_node(
pydot.Node(str(id(self)), label=model_function, shape="rectangle")
)
def to_json(self, **kwargs):
ret = super().to_json(**kwargs)
ret.update(
{
"type": "analytic",
"functionStr": self.model_function,
"argCount": self._num_args,
"parameterNames": self.parameter_names,
"regressionModel": list(self.model_args),
}
)
return ret
def hyper_to_dref(self):
return {
"fol/categorical to scalar": int(self.categorical_to_scalar),
}
class AnalyticFunction(ModelFunction):
"""
A multi-dimensional model function, generated from a string, which can be optimized using regression.
The function describes a single model attribute (e.g. TX duration or send(...) energy)
and how it is influenced by model parameters such as configured bit rate or
packet length.
"""
def __init__(
self,
value,
function_str,
parameters,
num_args=0,
regression_args=None,
fit_by_param=None,
**kwargs,
):
"""
Create a new AnalyticFunction object from a function string.
:param function_str: the function.
Refer to regression variables using regression_arg(123),
to parameters using parameter(name),
and to function arguments (if any) using function_arg(123).
Example: "regression_arg(0) + regression_arg(1) * parameter(txbytes)"
:param parameters: list containing the names of all model parameters,
including those not used in function_str, sorted lexically.
Sorting is mandatory, as parameter indexes (and not names) are used internally.
:param num_args: number of local function arguments, if any. Set to 0 if
the model attribute does not belong to a function or if function
arguments are not included in the model.
:param regression_args: Initial regression variable values,
both for function usage and least squares optimization.
If unset, defaults to [1, 1, 1, ...]
"""
super().__init__(value, **kwargs)
self._parameter_names = parameters
self._num_args = num_args
self.model_function = function_str
rawfunction = function_str
self._dependson = [False] * (len(parameters) + num_args)
self.fit_success = False
self.fit_by_param = fit_by_param
if type(function_str) == str:
num_vars_re = re.compile(r"regression_arg\(([0-9]+)\)")
num_vars = max(map(int, num_vars_re.findall(function_str))) + 1
for i in range(len(parameters)):
if rawfunction.find("parameter({})".format(parameters[i])) >= 0:
self._dependson[i] = True
rawfunction = rawfunction.replace(
"parameter({})".format(parameters[i]),
"model_param[{:d}]".format(i),
)
for i in range(0, num_args):
if rawfunction.find("function_arg({:d})".format(i)) >= 0:
self._dependson[len(parameters) + i] = True
rawfunction = rawfunction.replace(
"function_arg({:d})".format(i),
"model_param[{:d}]".format(len(parameters) + i),
)
for i in range(num_vars):
rawfunction = rawfunction.replace(
"regression_arg({:d})".format(i), "reg_param[{:d}]".format(i)
)
self._function_str = rawfunction
self._function = eval("lambda reg_param, model_param: " + rawfunction)
else:
self._function_str = "raise ValueError"
self._function = function_str
if regression_args:
self.model_args = regression_args.copy()
self._fit_success = True
elif type(function_str) == str:
self.model_args = list(np.ones((num_vars)))
else:
self.model_args = []
def get_fit_data(self, by_param):
"""
Return training data suitable for scipy.optimize.least_squares.
:param by_param: measurement data, partitioned by parameter/arg values.
by_param[*] must be a list or 1-D NumPy array containing the ground truth.
The parameter values (dict keys) must be numeric for
all parameters this function depends on -- otherwise, the
corresponding data will be left out. Parameter values must be
ordered according to the order of parameter names used in
the ParamFunction constructor. Argument values (if any) always come after
parameters, in the order of their index in the function signature.
:return: (X, Y, num_valid, num_total):
X -- 2-D NumPy array of parameter combinations (model input).
First dimension is the parameter/argument index, the second
dimension contains its values.
Example: X[0] contains the first parameter's values.
Y -- 1-D NumPy array of training data (desired model output).
num_valid -- amount of distinct parameter values suitable for optimization
num_total -- total amount of distinct parameter values
"""
dimension = len(self._parameter_names) + self._num_args
X = [[] for i in range(dimension)]
Y = []
num_valid = 0
num_total = 0
for key, val in by_param.items():
if len(key) == dimension:
valid = True
num_total += 1
for i in range(dimension):
if self._dependson[i] and not is_numeric(key[i]):
valid = False
if valid:
num_valid += 1
Y.extend(val)
for i in range(dimension):
if self._dependson[i]:
X[i].extend([float(key[i])] * len(val))
else:
X[i].extend([np.nan] * len(val))
else:
logger.warning(
"Invalid parameter key length while gathering fit data. is {}, want {}.".format(
len(key), dimension
)
)
X = np.array(X)
Y = np.array(Y)
return X, Y, num_valid, num_total
def fit(self, by_param):
"""
Fit the function on measurements via least squares regression.
:param by_param: measurement data, partitioned by parameter/arg values
The ground truth is read from by_param[*],
which must be a list or 1-D NumPy array. Parameter values must be
ordered according to the parameter names in the constructor. If
argument values are present, they must come after parameter values
in the order of their appearance in the function signature.
"""
X, Y, num_valid, num_total = self.get_fit_data(by_param)
if num_valid > 2:
error_function = lambda P, X, y: self._function(P, X) - y
try:
res = optimize.least_squares(
error_function, self.model_args, args=(X, Y), xtol=2e-15
)
except ValueError as err:
logger.warning(f"Fit failed: {err} (function: {self.model_function})")
return
if res.status > 0:
self.model_args = res.x
self.fit_success = True
else:
logger.warning(
f"Fit failed: {res.message} (function: {self.model_function})"
)
else:
logger.debug("Insufficient amount of valid parameter keys, cannot fit")
def is_predictable(self, param_list):
"""
Return whether the model function can be evaluated on the given parameter values.
The first value corresponds to the lexically first model parameter, etc.
All parameters must be set, not just the ones this function depends on.
Returns False iff a parameter the function depends on is not numeric
(e.g. None).
"""
for i, param in enumerate(param_list):
if self._dependson[i] and not is_numeric(param):
return False
return True
def eval(self, param_list):
"""
Evaluate model function with specified param/arg values.
:param param_list: parameter values (list of float). First item
corresponds to lexically first parameter, etc.
:param arg_list: argument values (list of float), if arguments are used.
"""
try:
return self._function(self.model_args, param_list)
except FloatingPointError as e:
logger.error(
f"{e} when predicting {self._function_str}({param_list}), returning static value"
)
return self.value
def get_complexity_score(self):
return len(self.model_args)
def webconf_function_map(self):
js_buf = self.model_function
for i in range(len(self.model_args)):
js_buf = js_buf.replace(f"regression_arg({i})", str(self.model_args[i]))
for parameter_name in self._parameter_names:
js_buf = js_buf.replace(
f"parameter({parameter_name})", f"""param["{parameter_name}"]"""
)
for arg_num in range(self._num_args):
js_buf = js_buf.replace(f"function_arg({arg_num})", f"args[{arg_num}]")
js_buf = "(param, args) => " + js_buf.replace("np.", "Math.")
return [(f'"{self.model_function}"', js_buf)]
def to_json(self, **kwargs):
ret = super().to_json(**kwargs)
ret.update(
{
"type": "analytic",
"functionStr": self.model_function,
"argCount": self._num_args,
"parameterNames": self._parameter_names,
"regressionModel": list(self.model_args),
}
)
return ret
def to_dot(self, pydot, graph, feature_names, parent=None):
model_function = self.model_function
for i, arg in enumerate(self.model_args):
model_function = model_function.replace(
f"regression_arg({i})", f"{arg:.2f}"
)
graph.add_node(
pydot.Node(str(id(self)), label=model_function, shape="rectangle")
)
@classmethod
def from_json(cls, data):
assert data["type"] == "analytic"
return cls(
data.get("value", 0),
data["functionStr"],
data["parameterNames"],
data.get("argCount", 0),
data["regressionModel"],
)
def __repr__(self):
return f"AnalyticFunction<{self.value}, {self.model_function}>"
class analytic:
"""
Utilities for analytic description of parameter-dependent model attributes and regression analysis.
provided functions:
functions -- retrieve pre-defined set of regression function candidates
function_powerset -- combine several per-parameter functions into a single AnalyticFunction
"""
_num0_8 = np.vectorize(lambda x: 8 - bin(int(x)).count("1"))
_num0_16 = np.vectorize(lambda x: 16 - bin(int(x)).count("1"))
_num1 = np.vectorize(lambda x: bin(int(x)).count("1"))
_safe_log = np.vectorize(lambda x: np.log(np.abs(x)) if np.abs(x) > 0.001 else 1.0)
_safe_inv = np.vectorize(lambda x: 1 / x if np.abs(x) > 0.001 else 1.0)
_safe_sqrt = np.vectorize(lambda x: np.sqrt(np.abs(x)))
_function_map = {
"linear": lambda x: x,
"logarithmic": np.log,
"logarithmic1": lambda x: np.log(x + 1),
"exponential": np.exp,
"square": lambda x: x**2,
"inverse": lambda x: 1 / x,
"sqrt": lambda x: np.sqrt(np.abs(x)),
"num0_8": _num0_8,
"num0_16": _num0_16,
"num1": _num1,
"safe_log": lambda x: np.log(np.abs(x)) if np.abs(x) > 0.001 else 1.0,
"safe_inv": lambda x: 1 / x if np.abs(x) > 0.001 else 1.0,
"safe_sqrt": lambda x: np.sqrt(np.abs(x)),
}
@staticmethod
def functions(safe_functions_enabled=False):
"""
Retrieve pre-defined set of regression function candidates.
:param safe_functions_enabled: Include "safe" variants of functions with
limited argument range, e.g. a safe
inverse which returns 1 when dividing by 0.
Returns a dict of functions which are typical for energy/timing
behaviour of embedded hardware, e.g. linear, exponential or inverse
dependency on a configuration setting/runtime variable.
Each function is a ParamFunction object. In most cases, two regression
variables are expected.
"""
functions = {
"linear": ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] * model_param,
lambda model_param: True,
2,
repr_str="β₀ + β₁ * x",
),
"logarithmic": ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] * np.log(model_param),
lambda model_param: model_param > 0,
2,
repr_str="β₀ + β₁ * np.log(x)",
),
"logarithmic1": ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] * np.log(model_param + 1),
lambda model_param: model_param > -1,
2,
repr_str="β₀ + β₁ * np.log(x+1)",
),
"exponential": ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] * np.exp(model_param),
lambda model_param: model_param <= 64,
2,
repr_str="β₀ + β₁ * np.exp(x)",
),
#'polynomial' : lambda reg_param, model_param: reg_param[0] + reg_param[1] * model_param + reg_param[2] * model_param ** 2,
"square": ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] * model_param**2,
lambda model_param: True,
2,
repr_str="β₀ + β₁ * x²",
),
"inverse": ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] / model_param,
lambda model_param: model_param != 0,
2,
repr_str="β₀ + β₁ * 1/x",
),
"sqrt": ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] * np.sqrt(model_param),
lambda model_param: model_param >= 0,
2,
repr_str="β₀ + β₁ * np.sqrt(x)",
),
# "num0_8": ParamFunction(
# lambda reg_param, model_param: reg_param[0]
# + reg_param[1] * analytic._num0_8(model_param),
# lambda model_param: True,
# 2,
# ),
# "num0_16": ParamFunction(
# lambda reg_param, model_param: reg_param[0]
# + reg_param[1] * analytic._num0_16(model_param),
# lambda model_param: True,
# 2,
# ),
# "num1": ParamFunction(
# lambda reg_param, model_param: reg_param[0]
# + reg_param[1] * analytic._num1(model_param),
# lambda model_param: True,
# 2,
# ),
}
if safe_functions_enabled or bool(
int(os.getenv("DFATOOL_REGRESSION_SAFE_FUNCTIONS", "0"))
):
functions.pop("logarithmic1")
functions.pop("logarithmic")
functions["safe_log"] = ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] * analytic._safe_log(model_param),
lambda model_param: True,
2,
repr_str="β₀ + β₁ * safe_log(x)",
)
functions.pop("inverse")
functions["safe_inv"] = ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] * analytic._safe_inv(model_param),
lambda model_param: True,
2,
repr_str="β₀ + β₁ * safe(1/x)",
)
functions.pop("sqrt")
functions["safe_sqrt"] = ParamFunction(
lambda reg_param, model_param: reg_param[0]
+ reg_param[1] * analytic._safe_sqrt(model_param),
lambda model_param: True,
2,
repr_str="β₀ + β₁ * safe_sqrt(x)",
)
if os.getenv("DFATOOL_RMT_SUBMODEL", "uls") == "fol":
functions = {"linear": functions["linear"]}
return functions
@staticmethod
def _fmap(reference_type, reference_name, function_type):
"""Map arg/parameter name and best-fit function name to function text suitable for AnalyticFunction."""
ref_str = "{}({})".format(reference_type, reference_name)
if function_type == "linear":
return ref_str
if function_type == "logarithmic":
return "np.log({})".format(ref_str)
if function_type == "logarithmic1":
return "np.log({} + 1)".format(ref_str)
if function_type == "exponential":
return "np.exp({})".format(ref_str)
if function_type == "exponential":
return "np.exp({})".format(ref_str)
if function_type == "square":
return "({})**2".format(ref_str)
if function_type == "inverse":
return "1/({})".format(ref_str)
if function_type == "sqrt":
return "np.sqrt({})".format(ref_str)
return "analytic._{}({})".format(function_type, ref_str)
@staticmethod
def function_powerset(fit_results, parameter_names, num_args=0, **kwargs):
"""
Combine per-parameter regression results into a single multi-dimensional function.
:param fit_results: results dict. One element per parameter, each containing
a dict of the form {'best' : name of function with best fit}.
Must not include parameters which do not influence the model attribute.
Example: {'txpower' : {'best': 'exponential'}}
:param parameter_names: Parameter names, including those left
out in fit_results because they do not influence the model attribute.
Must be sorted lexically.
Example: ['bitrate', 'txpower']
:param num_args: number of local function arguments, if any. Set to 0 if
the model attribute does not belong to a function or if function
arguments are not included in the model.
Returns an AnalyticFunction instantce corresponding to the combined
function.
"""
buf = "0"
arg_idx = 0
for combination in powerset(fit_results.items()):
buf += " + regression_arg({:d})".format(arg_idx)
arg_idx += 1
for function_item in combination:
if is_numeric(function_item[0]):
buf += " * {}".format(
analytic._fmap(
"function_arg", function_item[0], function_item[1]["best"]
)
)
else:
buf += " * {}".format(
analytic._fmap(
"parameter", function_item[0], function_item[1]["best"]
)
)
return AnalyticFunction(
None, buf, parameter_names, num_args, fit_by_param=fit_results, **kwargs
)
|