aboutsummaryrefslogtreecommitdiff
path: root/hw9/12-12.jl
blob: e08eadcaf495082b673252e8167977d8c64c27ba (plain)
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
# my neural net from 12.11

# first, store the memory of A as a lattice of 1, -1, where 1 is the memory and -1 is the absence of memory
A = [
	-1 -1 -1 -1 1 1 -1 -1 -1 -1;
	-1 -1 -1 1 1 1 1 -1 -1 -1;
	-1 -1 1 1 -1 -1 1 1 -1 -1;
	-1 1 1 -1 -1 -1 -1 1 1 -1;
	1 1 -1 -1 -1 -1 -1 -1 1 1;
	1 1 -1 -1 -1 -1 -1 -1 1 1;
	-1 1 1 -1 -1 -1 -1 1 1 -1;
	-1 -1 1 1 -1 -1 1 1 -1 -1;
	-1 -1 -1 1 1 1 1 -1 -1 -1;
	-1 -1 -1 -1 1 1 -1 -1 -1 -1
]

B = [
	1 1 1 1 1 -1 -1 -1 -1 -1;
	1 1 -1 -1 1 1 -1 -1 -1 -1;
	1 1 -1 -1 1 1 -1 -1 -1 -1;
	1 1 -1 -1 1 1 -1 -1 -1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1 1 1 1 1 1 1 1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 1 1 1 1 1 1 1 1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1
]

G = [
	1 1 1 1 1 1 1 1 1 1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 -1 -1 -1 -1 -1 -1 -1 -1;
	1 1 -1 -1 -1 -1 -1 1 1 1;
	1 1 -1 -1 -1 -1 -1 -1 1 1;
	1 1 -1 -1 -1 -1 -1 -1 1 1;
	1 1 -1 -1 -1 -1 -1 -1 1 1;
	-1 1 1 1 1 1 1 1 1 1;
	-1 -1 1 1 1 1 1 1 1 1
]

encodings = [A, B, C, D, E_letter, F, G]

spin_numbers(row, col) = (row - 1) * 10 + col

# create strength of interactions between ith and jth spinds in Ji,j
J = zeros(100, 100)
for m in 1:100 # row
	for n in 1:100 # col
		i = (m - 1) % 10 + 1
		j = (n - 1) % 10 + 1
		k = (m - 1) ÷ 10 + 1
		l = (n - 1) ÷ 10 + 1

		J[m, n] = 0
		for encoding in encodings
			J[m, n] += encoding[i, k] * encoding[j, l]
		end
		J[m, n] /= length(encodings)
	end
end

# the energy function
function energy(s, J)
	E = 0
	for i in 1:100
		for j in 1:100
			E += J[i, j] * s[i] * s[j]
		end
	end
	return -E
end

# the update function (uses monte carlo steps)
function monte_carlo(s, J)
	for i in 1:100 # systematically go through each point in the lattice
		# calculate the energy of the system
		E = energy(s, J)
		# randomly flip a spin
		s[i] = -s[i]
		# calculate the new energy of the system
		E_new = energy(s, J)
		# calculate the change in energy
		dE = E_new - E
		# if the change in energy is positive, flip the spin back
		if dE > 0
			s[i] = -s[i]
		end
	end
	return s
end

# create the main function
function main(s, J, nsteps)
	E = energy(s, J)
	for i in 1:nsteps
		s = monte_carlo(s, J)
		E = energy(s, J)
	end
	return s, E
end

# run the main function

# randomly change some values in A to see if NN works
function produce_test_arr(enc, prob_change = 0.1)
	tmp = zeros(10, 10)
	for i in 1:10
		for j in 1:10
			if rand() < prob_change
				tmp[i, j] = rand([-1, 1]) # set some random values
			else
				tmp[i, j] = enc[i, j]
			end
		end
	end
	return tmp
end

function check_if_same(s, enc)
	for i in 1:10
		for j in 1:10
			if s[i, j] != enc[i, j]
				println("The neural net did not work")
				return false
			end
		end
	end
	# println("The neural net worked")
	return true
end

function apply_damage_to_J(J, prob_damage = 0.8)
	println("Applying damage to J with probability ", prob_damage)
	ret = copy(J)
	for i in 1:100
		for j in 1:100
			if rand() < prob_damage
				ret[i, j] = 0
			else
				ret[i, j] = J[i, j]
			end
		end
	end
	return ret
end

function run_tests(num_times, Js, MC_steps = 2)
	num_correct = 0
	num_total = 0

	for i in 1:num_times
		# randomly select a memory
		enc = encodings[rand(1:length(encodings))]
		# produce a test array
		s = produce_test_arr(enc)
		# run theNN 
		s, E = main(s, Js, MC_steps)
		# check if the NN worked
		if check_if_same(s, enc)
			num_correct += 1
		end
		num_total += 1
	end

	println("Number of correct tests: ", num_correct)

	return num_correct / num_total
end

function run_tests_for_damage_range(damages, num_times, MC_steps = 2, init_J = J)
	res = []

	for damage in damages
		damaged_J = apply_damage_to_J(init_J, damage)
		p = run_tests(num_times, damaged_J, MC_steps)

		push!(res, p)
	end

	return res
end

damage_range = collect(0.0:0.1:0.15)
res = run_tests_for_damage_range(damage_range, 10)

#plot the results
using Plots
plot(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 2")

res = run_tests_for_damage_range(damage_range, 10, 10)
plot!(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 10")

res = run_tests_for_damage_range(damage_range, 10, 100)
plot!(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 100")

res = run_tests_for_damage_range(damage_range, 10, 1000)
plot!(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 1000")

savefig("12-12-all.png")