-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexstracs_tree.py
1526 lines (1316 loc) · 79.3 KB
/
exstracs_tree.py
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
# START GP INTEGRATION CODE*************************************************************************************************************************************
import deap
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
from deap import gp
from deap.tools import crossover
import operator
import math
import random
import string
import copy
import ast
from collections import defaultdict
from functools import partial, wraps
from exstracs_classifier import Classifier
from exstracs_constants import *
from exstracs_pareto import *
# -----------------------------------------------------------------------
# Set up DEAP stuff
# -----------------------------------------------------------------------
def protectedDiv(left, right):
try:
return left / right
except ZeroDivisionError:
return 1
# Change the evaluate function as needed.
# This takes in one parameter.
def evalSymbReg(individual, points):
# Transform the tree expression in a callable function
func = self.toolbox.compile(expr=individual)
# Evaluate the mean squared error between the expression
# and the real function : x**4 + x**3 + x**2 + x
sqerrors = ((func(x) - x ** 4 - x ** 3 - x ** 2 - x) ** 2 for x in points)
return math.fsum(sqerrors) / len(points),
# STOP GP INTEGRATION CODE*************************************************************************************************************************************
# START GP INTEGRATION CODE*************************************************************************************************************************************
class Tree:
def __init__(self, tree=None, exploreIter=None):
# Initialize DEAP GP environment for generating a single tree---------------------------------------------------------------
# addPrimitive adds different operators (operator, num. of arguments)
# Written to approximate x**4 - x**3 - x**2 - x)**2
# PrimitiveSet constructor takes in num. arguments - default named ARGx where x is index
pset = gp.PrimitiveSet("MAIN", int(cons.env.formatData.numAttributes))
# pset = gp.PrimitiveSet("MAIN", 11)
pset.addPrimitive(operator.add, 2)
pset.addPrimitive(max, 2)
pset.addPrimitive(operator.lt, 2)
pset.addPrimitive(operator.gt, 2)
pset.addPrimitive(operator.sub, 2)
pset.addPrimitive(operator.mul, 2)
pset.addPrimitive(protectedDiv, 2)
pset.addPrimitive(operator.neg, 1)
pset.addPrimitive(math.cos, 1)
pset.addPrimitive(math.sin, 1)
# addEphemeralConstant(name, function)
randomName = str(exploreIter) + str(time.time()) + str(random.randint(-10000, 10000))
# print('New Tree: '+str(randomName))
pset.addEphemeralConstant(randomName, lambda: random.randint(-5, 5))
# pset.addEphemeralConstant(randomName, lambda: random.randint(-5,5))
# pset.addEphemeralConstant("rand101", lambda: random.randint(-5,5))
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin)
self.toolbox = base.Toolbox()
# an expression that generates either full trees (all leaves on the same level between 2 and 5)
# or grown trees with leaves on different levels.
self.toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=2, max_=3)
self.toolbox.register("individual", tools.initIterate, creator.Individual, self.toolbox.expr)
self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual)
self.toolbox.register("compile", gp.compile, pset=pset)
# generating test points of values from -1 to 1, with 0.1 intervals
self.toolbox.register("evaluate", evalSymbReg, points=[x / 10. for x in range(-10, 10)])
self.toolbox.register("select", tools.selTournament, tournsize=3)
self.toolbox.register("mate", gp.cxOnePoint)
self.toolbox.register("expr_mut", gp.genFull, min_=2, max_=4)
self.toolbox.register("mutate", gp.mutNodeReplacement, pset=pset)
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin)
# methods used to reference terminal and primitive objects
term_list = list(pset.terminals.values())[0]
self.terminals = {}
for terminal in term_list:
if isinstance(terminal, deap.gp.Terminal):
self.terminals[terminal.name] = terminal
primitives = list(pset.primitives.values())[0]
self.pset = pset
# ---------------------------------------------------------------------------------------------------------------------
# STOP GP INTEGRATION CODE*************************************************************************************************************************************
self.condition = None
self.errorSum = 0
self.errorCount = 0
self.phenCount = 0 # number of times phenProb added to count for testing reasons
self.phenSum = 0 # sum of phenotype probability calculation values for continuous variables
self.totalFreq = 1
self.id = ''.join(random.choice(string.ascii_lowercase) for i in range(7))
self.one_count = 0
self.zero_count = 0
self.isTree = True
# Major Parameters --------------------------------------------------
self.phenotype = None # Class if the endpoint is discrete, and a continuous phenotype if the endpoint is continuous
self.phenotype_RP = None # NEW - probability of this phenotype occurring by chance.
# Fitness Metrics ---------------------------------------------------
self.accuracy = 0.0 # Classifier accuracy - Accuracy calculated using only instances in the dataset which this rule matched.
self.accuracyComponent = None # Accuracy adjusted based on accuracy by random chance, and transformed to give more weight to early changes in accuracy.
self.coverDiff = 1 # Number of instance correctly covered by rule beyond what would be expected by chance.
self.indFitness = 0.0 # Fitness from the perspective of an individual rule (strength/accuracy based)
self.relativeIndFitness = None
self.fitness = 1 # CHANGED: Original self.fitness = cons.init_fit
# Classifier fitness - initialized to a constant initial fitness value
self.numerosity = 1 # The number of rule copies stored in the population. (Indirectly stored as incremented numerosity)
self.aveMatchSetSize = None # A parameter used in deletion which reflects the size of match sets within this rule has been included.
self.deletionVote = None # The current deletion weight for this classifier.
# Experience Management ---------------------------------------------
self.epochComplete = False # Has this rule existed for a complete epoch (i.e. a cycle through training set).
# Fitness Metrics ---------------------------------------------------
# self.freqComponent = None
# Fitness Sharing
# self.adjustedAccuracy = 0.0
# self.relativeAccuracy = 0.0
self.lastMatch = 0
self.lastFitness = 0.0
self.sumIndFitness = 1.0 # experimental
self.partOfCorrect = True
self.lastMatchFitness = 1.0
# self.isMatchSetFitness = False #For Debugging
self.aveRelativeIndFitness = None
self.matchedAndFrontEstablished = False
self.totalFreq = 1
# START GP INTEGRATION CODE*************************************************************************************************************************************
if tree == None:
self.timeStampGA = 0 # Time since rule last in a correct set.
self.initTimeStamp = 0 # Iteration in which the rule first appeared.
self.aveMatchSetSize = 1
self.lastMatch = 0 # Experimental - for brief fitness update
# Classifier Accuracy Tracking --------------------------------------
self.matchCount = 0 # Known in many LCS implementations as experience i.e. the total number of times this classifier was in a match set
self.correctCount = 0 # The total number of times this classifier was in a correct set
self.matchCover = 0 # The total number of times this classifier was in a match set within a single epoch. (value fixed after epochComplete)
self.correctCover = 0 # The total number of times this classifier was in a correct set within a single epoch. (value fixed after epochComplete)
# Covering sets initially overly optimistic prediction values - this takes care of error with prediction which previously had only zero value fitness and indFitness scores for covered rules.
self.indFitness = 1.0
self.fitness = 1.0
tree = creator.Individual(self.toolbox.population(n=1)[0]) # tree actually created
self.form = tree
elif exploreIter != None:
# print "New Tree Created"
self.treeClone(tree, exploreIter)
# print self.initTimeStamp
else:
print("Error with initializing")
# get specified tree args
tree_args = set()
for idx, node in enumerate(self.form):
if isinstance(node, deap.gp.Terminal):
if node.name.split('G')[0] == 'AR':
nodeAtt = int(float(node.name.split('G')[1]))
tree_args.add(nodeAtt)
self.specifiedAttList = list(tree_args)
# STOP GP INTEGRATION CODE*************************************************************************************************************************************
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# CLASSIFIER CONSTRUCTION METHODS
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------`
# START GP INTEGRATION CODE*************************************************************************************************************************************
# clone tree
def treeClone(self, clOld, exploreIter):
""" Constructs an identical Classifier. However, the experience of the copy is set to 0 and the numerosity
is set to 1 since this is indeed a new individual in a population. Used by the genetic algorithm to generate
offspring based on parent classifiers."""
self.form = copy.deepcopy(clOld.form)
self.phenotype = None
self.phenotype_RP = None
self.timeStampGA = exploreIter # consider starting at 0 instead???
self.initTimeStamp = exploreIter
self.lastMatch = exploreIter
self.aveMatchSetSize = copy.deepcopy(clOld.aveMatchSetSize)
self.fitness = clOld.fitness # Test removal
self.accuracy = clOld.accuracy
self.relativeIndFitness = clOld.relativeIndFitness
self.indFitness = clOld.indFitness
self.sumIndFitness = clOld.sumIndFitness
# self.sumPreFitnessNEC = 1.0
# self.sumPreFitnessEC = 1.0
self.accuracyComponent = clOld.accuracyComponent
self.matchCount = 1 # Known in many LCS implementations as experience i.e. the total number of times this classifier was in a match set
self.correctCount = None # The total number of times this classifier was in a correct set
self.matchCover = 1 # The total number of times this classifier was in a match set within a single epoch. (value fixed after epochComplete)
self.correctCover = None # The total number of times this classifier was in a correct set within a single epoch. (value fixed after epochComplete)
def updateClonePhenotype(self, phenotype):
if phenotype == self.phenotype:
self.correctCount = 1
self.correctCover = 1
else:
self.correctCount = 0
self.correctCover = 0
# STOP GP INTEGRATION CODE*************************************************************************************************************************************
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# MATCHING
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def match(self, state):
return True
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Set Phenotype - GP TREE!!
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def setPhenotype(self,
args): # Ensure that for every instance we recalculate the tree phenotype prediction. It never stays fixed.
args = [int(i) for i in args]
try:
func = self.toolbox.compile(expr=self.form)
except:
print("Self form: " + str(self.form))
print("String: " + str(self.tree_string()))
print("Len String: " + str(len(self.form)))
print("Args: " + str(args))
raise NameError("Problem with args")
dataInfo = cons.env.formatData
# -------------------------------------------------------
# BINARY PHENOTYPE - ONLY
# -------------------------------------------------------
if dataInfo.discretePhenotype and len(dataInfo.phenotypeList) == 2:
if func(*args) > 0:
self.phenotype = '1'
if not self.epochComplete:
self.one_count += 1
else:
self.phenotype = '0'
if not self.epochComplete:
self.zero_count += 1
if not self.epochComplete: # Not sure where Ben came up with this but it seems to makes reasonable sense. May want to examie more carefully.
self.phenotype_RP = ((cons.env.formatData.classProportions['1'] * self.one_count) + (
cons.env.formatData.classProportions['0'] * self.zero_count)) / (
self.one_count + self.zero_count)
# For trees there could be one uniform random chance. Ultimately we want to use balanced accuracy for trees (do we do better than by chance) but for now we will just use 0.5 for simplicity.
# -------------------------------------------------------
# MULTICLASS PHENOTYPE
# -------------------------------------------------------
elif dataInfo.discretePhenotype and not len(dataInfo.phenotypeList) == 2:
if func(*args) < 0: # lowest class
self.phenotype = dataInfo.phenotypeList[0]
elif func(*args) >= len(dataInfo.phenotypeList) - 1: # lowest class
self.phenotype = dataInfo.phenotypeList[len(dataInfo.phenotypeList) - 1]
else: # one of the middle classes
count = 1
notfoundClass = True
while notfoundClass:
if func(*args) < count and func(*args) >= count - 1:
self.phenotype = dataInfo.phenotypeList[count]
notfoundClass = False
if count > len(dataInfo.phenotypeList):
notfoundClass = False
print("ERROR:setPhenotype in tree: Failed to find class")
count += 1
if not self.epochComplete: # Not sure where Ben came up with this but it seems to makes reasonable sense. May want to examie more carefully.
self.phenotype_RP = 0.5
# -------------------------------------------------------
# CONTINUOUS PHENOTYPE
# -------------------------------------------------------
else: # ContinuousCode #########################
self.phenotype = func(*args)
if not self.epochComplete: # Not sure where Ben came up with this but it seems to makes reasonable sense. May want to examie more carefully.
self.phenotype_RP = 0.5
def setPhenProb(self):
pass
def calcPhenProb(self, error):
if self.epochComplete:
return
phenRange = [self.phenotype - error, self.phenotype + error]
count = 0
ref = 0
# print cons.env.formatData.phenotypeRanked
# print self.phenotype
while ref < len(cons.env.formatData.phenotypeRanked) and cons.env.formatData.phenotypeRanked[ref] <= phenRange[
1]:
if cons.env.formatData.phenotypeRanked[ref] >= phenRange[0]:
count += 1
ref += 1
self.phenSum += count / float(cons.env.formatData.numTrainInstances)
self.phenCount += 1
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# GENETIC ALGORITHM MECHANISMS
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# START GP INTEGRATION CODE*************************************************************************************************************************************
def cxOnePoint(ind1, ind2):
"""Randomly select in each individual and exchange each subtree with the
point as root between each individual.
:param ind1: First tree participating in the crossover.
:param ind2: Second tree participating in the crossover.
:returns: A tuple of two trees.
"""
if len(ind1) < 2 or len(ind2) < 2:
# No crossover on single node tree
return ind1, ind2
# List all available primitive types in each individual
types1 = defaultdict(list)
types2 = defaultdict(list)
if ind1.root.ret == __type__:
# Not STGP optimization
types1[__type__] = range(1, len(ind1))
types2[__type__] = range(1, len(ind2))
common_types = [__type__]
else:
for idx, node in enumerate(ind1[1:], 1):
types1[node.ret].append(idx)
for idx, node in enumerate(ind2[1:], 1):
types2[node.ret].append(idx)
common_types = set(types1.keys()).intersection(set(types2.keys()))
if len(common_types) > 0:
type_ = random.choice(list(common_types))
index1 = random.choice(types1[type_])
index2 = random.choice(types2[type_])
slice1 = ind1.searchSubtree(index1)
slice2 = ind2.searchSubtree(index2)
ind1[slice1], ind2[slice2] = ind2[slice2], ind1[slice1]
return ind1, ind2
# does not return clone
# get this to return boolean
def uniformCrossover(self, cl, state, phenotype): # If called, assumes self is a tree!!
if cl.isTree: # BOTH TREES - use build in DEAP crossover mechanism
# print self.form
# print "------------------------------------------------------"
# print cl.form
# print "------------------------------------------------------"
orig_form = copy.deepcopy(self.form)
other = copy.deepcopy(cl.form)
try:
gp.cxOnePoint(self.form, cl.form) # method defined just above!
except:
print('Single point crossover failure')
print("Self form: " + str(self.form))
print("Self String: " + str(self.tree_string()))
print("Other form: " + str(cl.form))
print("Other string: " + str(cl.tree_string()))
print(5 / 0)
try:
self.test_arity()
except:
print("Original Form: " + str(orig_form))
print("Original String: " + str(self.tree_string_form(orig_form)))
print("Original Other: " + str(other))
print("Original Other String: " + str(self.tree_string_form(other)))
print("New Form: " + str(self.form))
print("New String: " + self.tree_string())
print("New Other: " + str(cl.form))
print("New Other String: " + str(cl.tree_string()))
raise NameError("Crossover Returning invalid")
tree_args = set()
for idx, node in enumerate(self.form):
if isinstance(node, deap.gp.Terminal):
if node.name.split('G')[0] == 'AR':
nodeAtt = int(float(node.name.split('G')[1]))
tree_args.add(nodeAtt)
self.specifiedAttList = list(tree_args)
tree_args = set()
for idx, node in enumerate(cl.form):
if isinstance(node, deap.gp.Terminal):
if node.name.split('G')[0] == 'AR':
nodeAtt = int(float(node.name.split('G')[1]))
tree_args.add(nodeAtt)
cl.specifiedAttList = list(tree_args)
# print "*********************************************************************"
# print self.form
# print "------------------------------------------------------"
# print cl.form
# print "------------------------------------------------------"
# raise NameError("crossover with two trees")
# self.form = new_self
# cl.form = new_t
else: # SELF is a tree by the other entity is a Rule - Utilize special custom crossover proceedure.
orig_form = copy.deepcopy(self.form)
orig_rule = copy.deepcopy(cl)
self.rule_crossover(cl, state) # PERFORM THE RULE/TREE Crossover!!!!!!
# Checking that a rule does not go beyond specLimit
if len(cl.specifiedAttList) > cons.env.formatData.specLimit:
cl.specLimitFix(cl)
# fix other crossover mistakes in rules (like interval range swaps)
v = 0
while v < len(cl.specifiedAttList) - 1:
attributeInfo = cons.env.formatData.attributeInfo[cl.specifiedAttList[v]]
if attributeInfo[0]: # If continuous feature
if cl.condition[v][0] > cl.condition[v][1]:
print('crossover error: cl')
print(cl.condition)
temp = cl.condition[v][0]
cl.condition[v][0] = cl.condition[v][1]
cl.condition[v][1] = temp
# cl.origin += '_fix' #temporary code
if state[cl.specifiedAttList[
v]] == cons.labelMissingData: # If example is missing, don't attempt range, instead generalize attribute in rule.
# print 'removed '+str(cl.specifiedAttList[v])
cl.specifiedAttList.pop(v)
cl.condition.pop(v) # buildMatch handles both discrete and continuous attributes
v -= 1
else:
if not cl.condition[v][0] < state[cl.specifiedAttList[v]] or not cl.condition[v][1] > state[
cl.specifiedAttList[v]]:
# print 'crossover range error'
attRange = attributeInfo[1][1] - attributeInfo[1][0]
rangeRadius = random.randint(25,
75) * 0.01 * attRange / 2.0 # Continuous initialization domain radius.
Low = state[cl.specifiedAttList[v]] - rangeRadius
High = state[cl.specifiedAttList[v]] + rangeRadius
condList = [Low,
High] # ALKR Representation, Initialization centered around training instance with a range between 25 and 75% of the domain size.
cl.condition[v] = condList
# self.origin += '_RangeError' #temporary code
v += 1
if len(self.form) == 0:
print("Original Form: " + str(orig_form))
print("Original Rule Spec: " + str(orig_rule.specifiedAttList))
print("Original Rule Cond: " + str(orig_rule.condition))
raise NameError("Crossover Returning empty tree")
try:
self.test_arity()
except:
print("Original Form: " + str(orig_form))
print("Original String: " + str(self.tree_string_form(orig_form)))
print("Original Rule Spec: " + str(orig_rule.specifiedAttList))
print("New Form: " + str(self.form))
print("New String: " + self.tree_string())
print("New Rule: " + str(cl.specifiedAttList))
raise NameError("Crossover Returning invalid")
if orig_form == self.form and (cl.isTree and other == cl.form):
print('No change after crossover')
return False
else:
return True
def return_constants(self):
terminal_idx = []
index = 0
for node in self.form:
if isinstance(node, deap.gp.Terminal):
if not node.name.split('G')[0] == 'AR':
terminal_idx.append(index)
# if isinstance(node, deap.gp.rand101):
# terminal_idx.append(index)
index += 1
for idx in terminal_idx:
print(self.form[idx].value)
pass
def replace(self, idx, argument):
# hack that works for now. Ask randy about this
terminal = self.terminals["ARG" + str(argument)]
self.form[idx] = terminal
# does not return clone
def rule_crossover(self, rule, state): # 'Mates' a rule with a tree. #self is the tree, and rule is the rule.
# (1)remove unique features from rule (2)
# Handles the different scenarios of rule/tree crossover. (1) Equal swap, more tree than rule
tree_cross = [] # tree unique features information - index and attNumber
rule_cross = []
tree_args = set() # unique set of attributes found in tree but not rule
rule_orig = copy.deepcopy(rule.specifiedAttList)
tree_orig = copy.deepcopy(self.form)
# Identify all attributes specified in tree that are not in the rule, we do this instead of grab from self.specifiedAttList because we also store idx for each.
for idx, node in enumerate(self.form):
if isinstance(node, deap.gp.Terminal):
if node.name.split('G')[0] == 'AR':
nodeAtt = int(float(node.name.split('G')[1]))
if nodeAtt not in rule.specifiedAttList:
tree_cross.append((idx, nodeAtt))
tree_args.add(nodeAtt)
# print('tree/cross')
# print(tree_cross)
# Pick a random subset of unique tree attributes to exchange
tree_cross[:] = [elt for elt in tree_cross if random.choice([0, 1]) == 0]
# Select attributes to remove from rule and put in tree. They are removed from rule.
for idx, val in enumerate(rule.specifiedAttList):
if val not in tree_args:
if random.choice([0, 1]) == 0:
rule_cross.append(rule.specifiedAttList.pop(
idx)) # what is idx here? should be the position in the list (may be an error here)
rule.condition.pop(idx) # also remove
tree_cross_orig = copy.deepcopy(tree_cross) # for debugging
rule_cross_orig = copy.deepcopy(rule_cross) # for debugging
while len(tree_cross) != 0 or len(rule_cross) != 0:
if len(tree_cross) == 0: # Add an extra feature to tree
try:
rule_arg = rule_cross.pop(0)
if random.choice([0, 1]) == 0: # pick random subset of unique rule attributes to exchange
if not self.replace_constant(rule_arg):
self.add_terminal(rule_arg)
else:
if not self.add_terminal(rule_arg):
self.replace_constant(rule_arg)
except Exception as exc:
print("Orig: " + str(tree_orig))
print("Form: " + str(self.form))
print("String: " + str(self.tree_string()))
print("Rule Cross: " + str(rule_cross_orig))
print(exc)
raise NameError("Problem with crossing")
elif len(rule_cross) == 0: # Add an extra feature to rule
tree_arg = tree_cross.pop(0)
# print "Removing: " + str(tree_arg[0]) + " Arg: " + str(tree_arg[1])
# print "Tree cross" + str(tree_cross)
# print self.form
# print self.tree_string()
removed = self.remove_from_tree(tree_arg[0], [])
# print self.form
# print self.tree_string()
if tree_arg[1] not in rule.specifiedAttList:
rule.specifiedAttList.append(tree_arg[1])
rule.condition.append(self.buildMatch(tree_arg[1], state))
# update indices of removed
for index, arg in enumerate(tree_cross):
number = arg[0]
for idx in removed:
if idx < number:
number -= 1
new_arg = (number, arg[1])
tree_cross[index] = new_arg
else: # EVEN exhange between tree and rule!!!
tree_arg = tree_cross.pop(0)
rule_arg = rule_cross.pop(0)
self.replace(tree_arg[0], rule_arg)
if tree_arg[1] not in rule.specifiedAttList:
rule.specifiedAttList.append(tree_arg[1])
# have this append 1 for now, figure out how to do later
rule.condition.append(self.buildMatch(tree_arg[1], state))
# temporary fix to get tree specAttlist correct
tree_args = set()
for idx, node in enumerate(self.form):
if isinstance(node, deap.gp.Terminal):
if node.name.split('G')[0] == 'AR':
nodeAtt = int(float(node.name.split('G')[1]))
tree_args.add(nodeAtt)
self.specifiedAttList = list(tree_args)
if len(rule.specifiedAttList) != len(set(rule.specifiedAttList)):
print("Orig Tree: " + str(tree_orig))
print("Orig Rule: " + str(rule_orig))
print("Tree Args: " + str(tree_args))
print("Tree Cross: " + str(tree_cross_orig))
print("Rule Cross: " + str(rule_cross_orig))
print("New Rule: " + str(rule.specifiedAttList))
raise NameError("Duplicated in specAtt")
"""
def remove_from_tree(self, idx, removed):
if type(self.form[idx - 1]) == deap.gp.Primitive and self.form[idx-1].arity == 1:
#print "recurse"
del self.form[idx]
removed.append(idx)
return self.remove_from_tree(idx - 1, removed)
else:
count = 0
curr = idx - 1
while (curr + 1) != 0:
#print "Curr: " + str(curr)
count += 1
#print "Count: " + str(count)
if type(self.form[curr]) == deap.gp.Primitive:
count -= self.form[curr].arity
if count < 1:
del self.form[idx]
removed.append(idx)
del self.form[curr]
removed.append(curr)
break
curr -= 1
return removed
"""
def remove_from_tree(self, idx, removed):
removed.append(idx)
count = 0
curr = idx - 1
while (curr + 1) != 0:
# print "Curr: " + str(curr)
count += 1
# print "Count: " + str(count)
if isinstance(self.form[curr], deap.gp.Primitive):
count -= self.form[curr].arity
if count < 1:
if self.form[curr].arity == 1:
removed.append(curr)
else:
removed.append(curr)
break
curr -= 1
if len(removed) < len(self.form):
for item in removed:
try:
del self.form[item]
except:
print(item)
raise NameError("Problem with deletion")
return removed
def replace_constant(self, ruleAtt):
terminal_idx = []
for idx, node in enumerate(self.form):
if isinstance(node, deap.gp.Terminal):
if not node.name.split('G')[0] == 'AR':
terminal_idx.append(idx)
# if isinstance(node, deap.gp.rand101):
# terminal_idx.append(idx)
if isinstance(node, deap.gp.Terminal):
# print node.value
# print node.name
# rint getattr(node.ret)
pass
if len(terminal_idx) != 0:
# select a rand101 terminal index at random
idx = random.choice(terminal_idx)
# make the value of the terminal at switch index the given rule
self.replace(idx, ruleAtt)
return True
print("Problem in replace constant") # is there a problem here?
return False
"""
def add_terminal(self, attr, max_depth):
#get all terminals not at max depth
#print "Problem in add terminal"
#get terminal object of attr
terminal = terminals["ARG" + str(attr)]
#get arity 2 primitives
prim_list = []
for prim in primitives:
if prim.arity == 2:
prim_list.append(prim)
#choose random 2-arity primitive
ran = random.choice(range(len(prim_list)))
primitive = prim_list[ran]
branches = self.search_branch(max_depth)
if len(branches) == 0:
return False
idx = random.choice(range(len(branches)))
branch = branches[idx]
#insert attributes to right place
try:
self.form.insert(branch, terminal)
self.form.insert(branch, primitive)
except:
raise NameError("Problem in insertion")
return True
"""
def searchSubtree(form, begin):
"""Return a slice object that corresponds to the
range of values that defines the subtree which has the
element with index *begin* as its root.
"""
end = begin + 1
total = form[begin].arity
while total > 0:
total += form[end].arity - 1
end += 1
return slice(begin, end)
def add_terminal(self, attr):
"""Inserts a new branch at a random position in *individual*. The subtree
at the chosen position is used as child node of the created subtree, in
that way, it is really an insertion rather than a replacement. Note that
the original subtree will become one of the children of the new primitive
inserted, but not perforce the first (its position is randomly selected if
the new primitive has more than one child).
:param individual: The normal or typed tree to be mutated.
:returns: A tuple of one tree.
"""
# get terminal object of attr
terminal = self.terminals["ARG" + str(attr)]
# get arity 2 primitives
individual = self.form
index = random.randrange(len(individual))
node = individual[index]
slice_ = individual.searchSubtree(index)
choice = random.choice
# As we want to keep the current node as children of the new one,
# it must accept the return value of the current node
primitives = [p for p in self.pset.primitives[node.ret] if node.ret in p.args and p.arity == 2]
if len(primitives) == 0:
return individual,
new_node = choice(primitives)
new_subtree = [None] * len(new_node.args)
position = choice([i for i, a in enumerate(new_node.args) if a == node.ret])
for i, arg_type in enumerate(new_node.args):
if i != position:
new_subtree[i] = terminal
new_subtree[position:position + 1] = individual[slice_]
new_subtree.insert(0, new_node)
individual[slice_] = new_subtree
self.form = individual
return True
# find all branches at less than max depth
def search_branch(self, max_depth):
tree = self.form
stack = []
idx = 0
begin = tree[idx]
ter_list = []
depth = 1
stack.insert(0, begin.arity)
while len(stack) != 0:
stack[0] -= 1
idx += 1
node = tree[idx]
if isinstance(node, deap.gp.Primitive):
# print "adding primitive"
stack.insert(0, node.arity)
depth += 1
else:
# print "adding terminal"
if depth < max_depth:
ter_list.append(idx)
# print stack
while (len(stack) != 0 and stack[0] == 0):
stack.pop(0)
depth -= 1
# print stack
return ter_list
# STOP GP INTEGRATION CODE*************************************************************************************************************************************
def buildMatch(self, attRef, state):
""" Builds a matching condition element given an attribute to be specified for the classifierCovering method. """
attributeInfo = cons.env.formatData.attributeInfo[attRef]
# -------------------------------------------------------
# CONTINUOUS ATTRIBUTE
# -------------------------------------------------------
if attributeInfo[0]:
attRange = attributeInfo[1][1] - attributeInfo[1][0]
rangeRadius = random.randint(25, 75) * 0.01 * attRange / 2.0 # Continuous initialization domain radius.
Low = state[attRef] - rangeRadius
High = state[attRef] + rangeRadius
condList = [Low,
High] # ALKR Representation, Initialization centered around training instance with a range between 25 and 75% of the domain size.
# -------------------------------------------------------
# DISCRETE ATTRIBUTE
# -------------------------------------------------------
else:
condList = state[attRef] # State already formatted like GABIL in DataManagement
return condList
# -------------------------------------------------
# Mutation
# -------------------------------------------------
# START GP INTEGRATION CODE*************************************************************************************************************************************
# get this to return boolean
def Mutation(self, state, phenotype):
origform = copy.deepcopy(self.form)
# print "Original: " + str(self.form)
self.form = self.toolbox.mutate(self.form)[0]
self.form = self.toolbox.mutate(self.form)[0]
self.form = self.toolbox.mutate(self.form)[0]
# print "New: " + str(self.form)
# print "Same: " + str(self.form == origform)
# print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
return self.form != origform
# I guess for now just consider mutation failed?
# STOP GP INTEGRATION CODE*************************************************************************************************************************************
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# SUBSUMPTION METHODS
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# figure out which booleans these should return
def subsumes(self, cl):
""" Returns if the classifier (self) subsumes cl """
return False
def isSubsumer(self):
""" Returns if the classifier (self) is a possible subsumer. A classifier must have sufficient experience (one epoch) and it must also be as or more accurate than the classifier it is trying to subsume. """
return False
def isMoreGeneral(self, cl):
""" Returns if the classifier (self) is more general than cl. Check that all attributes specified in self are also specified in cl. """
return False
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# DELETION METHOD
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def getDelProp(self, meanFitness):
""" Returns the vote for deletion of the classifier. """
if self.fitness / self.numerosity >= cons.delta * meanFitness or self.matchCount < cons.theta_del:
self.deletionVote = self.aveMatchSetSize * self.numerosity
elif self.fitness == 0.0 or self.fitness / self.numerosity == 0.0:
self.deletionVote = self.aveMatchSetSize * self.numerosity * meanFitness / (cons.init_fit / self.numerosity)
else:
# print self.fitness
self.deletionVote = self.aveMatchSetSize * self.numerosity * meanFitness / (
self.fitness / self.numerosity) # note, numerosity seems redundant (look into theory of deletion in LCS.
return self.deletionVote
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# OTHER METHODS
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def equals(self, cl):
if not cl.isTree:
return False
else:
return self.form == cl.form
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# PARAMETER UPDATES
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def updateEpochStatus(self, exploreIter):
""" Determines when a learning epoch has completed (one cycle through training data). """
# if not self.epochComplete and (exploreIter - self.initTimeStamp-1) >= cons.env.formatData.numTrainInstances and cons.offlineData:
if not self.epochComplete and (
exploreIter - self.initTimeStamp) >= cons.env.formatData.numTrainInstances and cons.offlineData:
self.epochComplete = True
cons.firstEpochComplete = True
# randomProbClass = cons.env.formatData.classProportions[self.phenotype] #Put this in as a fix - for rules that become epoch complete after having been extrapolated on a previous run.
# self.usefulDiff = self.correctCover - randomProbClass*self.matchCover
self.usefulDiff = (self.correctCover - self.phenotype_RP * self.matchCover) # *len(self.specifiedAttList)
# Pareto Fitness - Epoch Complete Front Construction
if self.accuracyComponent > 0 and self.usefulDiff > 0:
objectivePair = [self.accuracyComponent, self.usefulDiff]
changedme = cons.env.formatData.ecFront.updateFront(objectivePair)
# if changedme:
# print 'new'
# print self.accuracyComponent
# print self.usefulDiff
# print self.initTimeStamp
# if self.accuracyComponent == 0.692582281546 and self.usefulDiff == 204.723:
# print'I was born and now am complete'
# print self.initTimeStamp
return True
return False
def updateExperience(self):
""" Increases the experience of the classifier by one. Once an epoch has completed, rule accuracy can't change."""
self.matchCount += 1
if self.epochComplete: # Once epoch Completed, number of matches for a unique rule will not change, so do repeat calculation
pass
else:
self.matchCover += 1
######MOVE TO NEW PLACE EVENTUALLY
if not cons.env.formatData.discretePhenotype:
self.phenotype_RP = 0.5
# print "gets here"
"""
self.phenotype_RP = float(self.phenSum) / float(self.phenCount)
if self.phenotype_RP > 1:
print("phenSum: " + str(self.phenSum))
print("matchCover: " + str(self.matchCover))
print("RP: " + str(self.phenotype_RP))
print("Count: " + str(self.phenCount))
raise NameError("Problem with phenotype_RP")
"""
def updateCorrect(self):
""" Increases the correct phenotype tracking by one. Once an epoch has completed, rule accuracy can't change."""
self.correctCount += 1
if self.epochComplete: # Once epoch Completed, number of correct for a unique rule will not change, so do repeat calculation
pass
else:
self.correctCover += 1
def updateError(self, trueEndpoint):
if not self.epochComplete:
# Error caclulations are limited to extremes of observed training data phenotypes in calculating the range centroid.
if self.phenotype > cons.env.formatData.phenotypeList[1]:
adjustedError = 1
elif self.phenotype < cons.env.formatData.phenotypeList[0]:
adjustedError = 1
else:
error = abs(self.phenotype - trueEndpoint)
adjustedError = error / (cons.env.formatData.phenotypeList[1] - cons.env.formatData.phenotypeList[0])
self.errorSum += adjustedError # Error is fraction of total phenotype range (i.e. maximum possible error)
# if adjustedError == 0:
# print("#########################################")
self.errorCount += 1
def updateIncorrectError(self):
if not self.epochComplete:
self.errorSum += 1.0
self.errorCount += 1
# NEW
def updateAccuracy(self, exploreIter):
""" Update the accuracy tracker """
nonUsefulDiscount = 0.001
coverOpportunity = 1000
adjAccuracy = 0
# -----------------------------------------------------------------------------------
# CALCULATE ACCURACY
# -----------------------------------------------------------------------------------
if cons.env.formatData.discretePhenotype:
self.accuracy = self.correctCover / float(self.matchCover)
else: # ContinuousCode #########################