File size: 6,326 Bytes
a8b2d62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Returns the MSE between the predictions and the truth provided targets for the given dataset
function truthScore(member::PopMember, cX::Array{Float32, 2}, cy::Array{Float32}, truth::Truth)::Float32
    transformed = transform(cX, truth)
    targets = truthPrediction(transformed, cy, truth)
    preds = evalTreeArray(member.tree, transformed)
    return MSE(preds, targets)
end

# Assumes a dataset X, y for a given truth
function truthScore(member::PopMember, truth::Truth)::Float32
    return truthScore(member, X, y, truth)
end

# Assumes a list of Truths TRUTHS is defined. Performs the truthScore function for each of them and returns the average
function truthScore(member::PopMember, cX::Array{Float32, 2}, cy::Array{Float32})::Float32
    s = 0
    for truth in TRUTHS
        s += (truthScore(member, cX, cy, truth))/size(TRUTHS)[1]
    end
    return s
end

# Assumes list of Truths TRUTHS and dataset X, y are defined
function truthScore(member::PopMember)::Float32
    return truthScore(member, X, y)
end
# Returns the MSE between the predictions and the truth provided targets for the given dataset
function truthScore(tree::Node, cX::Array{Float32, 2}, cy::Array{Float32}, truth::Truth)::Float32
    transformed = transform(cX, truth)
    targets = truthPrediction(transformed, cy, truth)
    preds = evalTreeArray(tree, transformed)
    return MSE(preds, targets)
end

# Assumes a dataset X, y for a given truth
function truthScore(tree::Node, truth::Truth)::Float32
    return truthScore(tree, X, y, truth)
end

# Assumes a list of Truths TRUTHS is defined. Performs the truthScore function for each of them and returns the average
function truthScore(tree::Node, cX::Array{Float32, 2}, cy::Array{Float32})::Float32
    s = 0
    for truth in TRUTHS
        s += (truthScore(tree, cX, cy, truth))/size(TRUTHS)[1]
    end
    return s
end

# Assumes list of Truths TRUTHS and dataset X, y are defined
function truthScore(tree::Node)::Float32
    return truthScore(tree, X, y)
end

# Returns true iff Truth Score is below a given threshold i.e truth is satisfied
function testTruth(member::PopMember, truth::Truth, threshold::Float32=Float32(1.0e-8))::Bool
    truthError = truthScore(member, truth)
    #print(stringTree(member.tree), "\n")
    #print(truth, ": ")
    #print(truthError, "\n")
    if truthError > threshold
        #print("Returns False \n ----\n")
        return false
    else
        #print("Returns True \n ----\n")
        return true
    end
end

# Returns a list of violating functions from assumed list TRUTHS
function violatingTruths(member::PopMember)::Array{Truth}
    return violatingTruths(member.tree)
end

# Returns true iff Truth Score is below a given threshold i.e truth is satisfied
function testTruth(tree::Node, truth::Truth, threshold::Float32=Float32(1.0e-3))::Bool
    truthError = truthScore(tree, truth)
    if truthError > threshold
        return false
    else
        return true
    end
end

# Returns a list of violating functions from assumed list TRUTHS
function violatingTruths(tree::Node)::Array{Truth}
    toReturn = []
    #print("\n Checking Equation ", stringTree(tree), "\n")
    for truth in TRUTHS
        test_truth = testTruth(tree, truth)
        #print("Truth: ", truth, ": " , test_truth, "\n-----\n")
        if !test_truth
            append!(toReturn, [truth])
        end
    end
    return toReturn
end

function randomIndex(cX::Array{Float32, 2}, k::Integer=10)::Array{Int32, 1}
    indxs = sample([Int32(i) for i in 1:size(cX)[1]], k)
    return indxs
end

function randomIndex(leng::Integer, k::Integer=10)::Array{Int32, 1}
    indxs = sample([Int32(i) for i in 1:leng], k)
    return indxs
end

function extendedX(cX::Array{Float32, 2}, truth::Truth, indx::Array{Int32, 1})::Array{Float32, 2}
    workingcX = copy(cX)
    X_slice = workingcX[indx, :]
    X_transformed = transform(X_slice, truth)
    return X_transformed
end
function extendedX(truth::Truth, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
    return extendedX(OGX, truth, indx)
end
function extendedX(cX::Array{Float32, 2}, violatedTruths::Array{Truth}, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
    if length(violatedTruths) == 0
        return nothing
    end
    workingX = extendedX(cX, violatedTruths[1], indx)
    for truth in violatedTruths[2:length(violatedTruths)]
        workingX = vcat(workingX, extendedX(cX, truth, indx))
    end
    return workingX
end
function extendedX(violatedTruths::Array{Truth}, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
    return extendedX(OGX, violatedTruths, indx)
end
function extendedX(tree::Node, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
    violatedTruths = violatingTruths(tree)
    return extendedX(violatedTruths, indx)
end
function extendedX(member::PopMember, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
    return extendedX(member.tree, indx)
end


function extendedy(cX::Array{Float32, 2}, cy::Array{Float32}, truth::Truth, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
    cy = copy(cy)
    cX = copy(cX)
    X_slice = cX[indx, :]
    y_slice = cy[indx]
    X_transformed = transform(X_slice, truth)
    y_transformed = truthPrediction(X_transformed, y_slice, truth)
    return y_transformed
end
function extendedy(truth::Truth, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
    return extendedy(OGX, OGy, truth, indx)
end
function extendedy(cX::Array{Float32, 2}, cy::Array{Float32}, violatedTruths::Array{Truth}, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
    if length(violatedTruths) == 0
        return nothing
    end
    workingy = extendedy(cX, cy, violatedTruths[1], indx)
    for truth in violatedTruths[2:length(violatedTruths)]
        workingy = vcat(workingy, extendedy(cX, cy, truth, indx))
    end
    return workingy
end
function extendedy(violatedTruths::Array{Truth}, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
    return extendedy(OGX,OGy, violatedTruths, indx)
end
function extendedy(tree::Node, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
    violatedTruths = violatingTruths(tree)
    return extendedy(violatedTruths, indx)
end
function extendedy(member::PopMember, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
    return extendedy(member.tree, indx)
end