-
Notifications
You must be signed in to change notification settings - Fork 12
/
exstracs_prediction.py
122 lines (104 loc) · 6.67 KB
/
exstracs_prediction.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
"""
Name: ExSTraCS_Prediction.py
Authors: Ryan Urbanowicz - Written at Dartmouth College, Hanover, NH, USA
Contact: ryan.j.urbanowicz@darmouth.edu
Created: April 25, 2014
Modified: August 25,2014
Description: Based on a given match set, this module uses a voting scheme to select the phenotype prediction for ExSTraCS.
---------------------------------------------------------------------------------------------------------------------------------------------------------
ExSTraCS V2.0: Extended Supervised Tracking and Classifying System - An advanced LCS designed specifically for complex, noisy classification/data mining tasks,
such as biomedical/bioinformatics/epidemiological problem domains. This algorithm should be well suited to any supervised learning problem involving
classification, prediction, data mining, and knowledge discovery. This algorithm would NOT be suited to function approximation, behavioral modeling,
or other multi-step problems. This LCS algorithm is most closely based on the "UCS" algorithm, an LCS introduced by Ester Bernado-Mansilla and
Josep Garrell-Guiu (2003) which in turn is based heavily on "XCS", an LCS introduced by Stewart Wilson (1995).
Copyright (C) 2014 Ryan Urbanowicz
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABLILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
---------------------------------------------------------------------------------------------------------------------------------------------------------
"""
#Import Required Modules-------------------------------
from exstracs_constants import *
import random
#------------------------------------------------------
class Prediction:
def __init__(self, population): #now takes in population ( have to reference the match set to do prediction) pop.matchSet
""" Constructs the voting array and determines the prediction decision. """
self.decision = None
#-------------------------------------------------------
# DISCRETE PHENOTYPE
#-------------------------------------------------------
if cons.env.formatData.discretePhenotype:
self.vote = {}
self.tieBreak_Numerosity = {}
self.tieBreak_TimeStamp = {}
for eachClass in cons.env.formatData.phenotypeList:
self.vote[eachClass] = 0.0
self.tieBreak_Numerosity[eachClass] = 0.0
self.tieBreak_TimeStamp[eachClass] = 0.0
for ref in population.matchSet:
cl = population.popSet[ref]
self.vote[cl.phenotype] += cl.fitness * cl.numerosity * cons.env.formatData.classPredictionWeights[cl.phenotype]
self.tieBreak_Numerosity[cl.phenotype] += cl.numerosity
self.tieBreak_TimeStamp[cl.phenotype] += cl.initTimeStamp
highVal = 0.0
bestClass = [] #Prediction is set up to handle best class ties for problems with more than 2 classes
for thisClass in cons.env.formatData.phenotypeList:
if self.vote[thisClass] >= highVal:
highVal = self.vote[thisClass]
for thisClass in cons.env.formatData.phenotypeList:
if self.vote[thisClass] == highVal: #Tie for best class
bestClass.append(thisClass)
#---------------------------
if highVal == 0.0:
self.decision = None
#-----------------------------------------------------------------------
elif len(bestClass) > 1: #Randomly choose between the best tied classes
bestNum = 0
newBestClass = []
for thisClass in bestClass:
if self.tieBreak_Numerosity[thisClass] >= bestNum:
bestNum = self.tieBreak_Numerosity[thisClass]
for thisClass in bestClass:
if self.tieBreak_Numerosity[thisClass] == bestNum:
newBestClass.append(thisClass)
#-----------------------------------------------------------------------
if len(newBestClass) > 1: #still a tie
bestStamp = 0
newestBestClass = []
for thisClass in newBestClass:
if self.tieBreak_TimeStamp[thisClass] >= bestStamp:
bestStamp = self.tieBreak_TimeStamp[thisClass]
for thisClass in newBestClass:
if self.tieBreak_TimeStamp[thisClass] == bestStamp:
newestBestClass.append(thisClass)
#-----------------------------------------------------------------------
if len(newestBestClass) > 1: # Prediction is completely tied - ExSTraCS has no useful information for making a prediction
self.decision = 'Tie'
else:
self.decision = newBestClass[0]
#----------------------------------------------------------------------
else: #One best class determined by fitness vote
self.decision = bestClass[0]
#-------------------------------------------------------
# CONTINUOUS PHENOTYPE
#-------------------------------------------------------
else:
print("Prediction - Error: ExSTraCS 2.0 can not handle continuous endpoints.")
def getFitnessSum(self,population,low,high):
""" Get the fitness Sum of rules in the rule-set. For continuous phenotype prediction. """
fitSum = 0
for ref in population.matchSet:
cl = population.popSet[ref]
if cl.phenotype[0] <= low and cl.phenotype[1] >= high: #if classifier range subsumes segment range.
fitSum += cl.fitness
return fitSum
def getDecision(self):
""" Returns prediction decision. """
return self.decision
def getSet(self):
""" Returns prediction decision. """
return self.vote