-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomialTests.jl
More file actions
223 lines (168 loc) · 4.65 KB
/
PolynomialTests.jl
File metadata and controls
223 lines (168 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using Test
include("Polynomial.jl")
include("DifferentialEquations.jl")
include("GaussianAssemblySpace.jl")
# Example temperature schedule (you may customize this based on your problem)
function example_temperature_schedule(iteration)
return 1.0 / log(1 + iteration)
end
# Example usage
initial_solution = randn(3 ) # Replace with the initial solution for your problem
max_iterations = 100
x1 = randn!(zeros(100))
x2 = randn!(zeros(100))
x3 = randn!(zeros(100))
o = ones(100)
y = (x1.^2 + x2.^2 + (x3.^2).*2)
model = :((x1.^2 + x2.^2 + (x3.^2).*β))
result_solution, result_energy = simulated_annealing(model)
println("Result Solution: ", result_solution)
println("Result Energy: ", result_energy)
@test result_energy < 0.1
y = x1 .+ x2
model = :(x1 .- β)
β = 5.0
@test compute_predictions(:(β+0.0)) == (ones(100) .* 5.0)
@test compute_predictions(:(x1 .+ 0.0)) == x1
β = 0.0
@test compute_MSE_instantaneous(:(β+0.0)) > 0.5
@test compute_MSE_instantaneous(:(x1 .+ x2)) < 0.5
# Ensure that if a basic building block is the true model, then the
# first model off the priority queue is that model.
y = x1
init_population()
@test length(P) == 0 && length(Q) == 4
for i in 1:4
add_to_population(dequeue!(Q))
end
@test length(P) == 4 && length(Q) == 0
add_descendants_to_queue(10000,1000000)
@test length(Q) == 32
x1 = rand(100)
x2 = rand(100)
y = 2 .* x1 .+ 4 .* x2
model = create_descendant(operations[1], (:x1, :x2))
@test isapprox(model.args[2].args[2], 2, atol=1e-2) && isapprox(model.args[3].args[2], 4, atol=1e-2)
# compute assembly index of expresssions
e1 = building_blocks[1]
e2 = create_descendant(operations[2], (e1, building_blocks[2]))
e3 = create_descendant(operations[2], (e2, e2))
e4 = create_descendant(operations[2], (e2, create_descendant(operations[2], (building_blocks[2], building_blocks[3]))))
@test compute_assembly_index(e1) == 0
@test compute_assembly_index(e2) == 1
@test compute_assembly_index(e3) == 2
@test compute_assembly_index(e4) == 3
e1 = building_blocks[1]
e2 = create_descendant(operations[1], (e1, building_blocks[2]))
e3 = create_descendant(operations[1], (e2, e2))
e4 = create_descendant(operations[1], (e2, create_descendant(operations[1], (building_blocks[2], building_blocks[3]))))
@test compute_assembly_index(e1) == 0
@test compute_assembly_index(e2) == 1
@test compute_assembly_index(e3) == 2
@test compute_assembly_index(e4) == 3
# @time P2 = generate_next_generation(P, k)
#=
n1 = AssemblyNode(quote x end, Number, 0, nothing)
n2 = AssemblyNode(:(x+y), Number, 0, n1)
blocks_ = Expr[]
block_types_ = Type[]
get_ancestors(n1, blocks_, block_types_)
@test length(blocks_) == 3
@test length(block_types_) == 3
blocks = Expr[]
block_types = Type[]
get_ancestors(n2, blocks, block_types)
@test length(blocks) == 4
@test length(block_types) == 4
@test blocks[1] == :(x+y)
@test block_types[1] == Number
=#
x1 = randn(100)
x2 = randn(100)
x3 = randn(100)
o = ones(100)
y = x1
@time final_model = assemble(λ)
print("Final Model: ")
print_poly(final_model)
println("")
#=
model = :(1.0 .* x1)
@test compute_MI(model) == Inf
model = :(1.0 .* x2)
@test compute_MI(model) < 0.1
model = :(1.0 .* x3)
@test compute_MI(model) < 0.1
y = ones(100)
@test compute_MI(:(5.0+0.0)) == 0.0
=#
#=
# Simplest example
x1 = randn!(zeros(100))
x2 = randn!(zeros(100))
# x3 = randn!(zeros(100))
y = (x1 .* x2)
λ = var(y) / 2
# It finds the correct model here.
@time assemble(λ)
x1 = u1
x2 = u2
y = dx1
λ = var(y .- mean(y)) / 3
@time assemble(λ)
=#
# true_model = :($(building_blocks[1]) .* ($(building_blocks[4]) .- $(building_blocks[3])) .- $(building_blocks[2]))
x1 = u1
x2 = u2
x3 = u3
o = ones(length(x1))
y = dx1
#=
λ = var(y .- mean(y)) / 4
@time final_model = assemble(λ)
print("Final Model: ")
print_poly(final_model)
println("")
y = dx2
λ = var(y) / 4
@time final_model = assemble(λ)
print("Final Model: ")
print_poly(final_model)
println("")
y = dx3
λ = var(y) / 4
@time final_model = assemble(λ)
print("Final Model: ")
print_poly(final_model)
println("")
=#
#=
# Test the nonlinear mutual information implementation
x = randn(100) .* 2.0
y = sin.(x.^2)
m1 = :(x .+ 0.0)
m2 = :(x .^ 2)
m3 = :(sin.(x.^2))
MI1 = compute_MI_nonlinear(m1)
MI2 = compute_MI_nonlinear(m2)
MI3 = compute_MI_nonlinear(m3)
@test MI1 < MI2
@test MI2 < MI3
# Test the nonlinear mutual information implementation on the second
# component of the Lorenz63 model
x1 = u1
x2 = u2
x3 = u3
y = dx2
m1 = :(x3 .+ 0.0)
m2 = :(β .- x3)
m3 = :(x1 .* (β .- x3))
m4 = :((x1 .* (β .- x3)) .- x2)
MI1 = compute_MI_nonlinear(m1)
MI2 = compute_MI_nonlinear(m2)
MI3 = compute_MI_nonlinear(m3)
MI4 = compute_MI_nonlinear(m4)
@test MI1 - MI2 <= 10e-5
@test MI2 - MI3 <= 10e-5
@test MI3 - MI4 <= 10e-5
=#