422cb49b31
- Create Dockerfile and entrypoint script for application setup - Implement pattern loading from Excel and weight caching - Develop core functions for Hopfield and Hamming networks - Add Sinatra web server for user interaction - Create HTML interface for displaying patterns and results - Include necessary gems in Gemfile and lockfile - Add .dockerignore and .gitignore files
49 lines
1.1 KiB
Ruby
49 lines
1.1 KiB
Ruby
WEIGHTS_CACHE_FILE = 'weights.cache'
|
|
|
|
def load_weights_cache(patterns_file)
|
|
return nil unless File.exist?(WEIGHTS_CACHE_FILE)
|
|
cached = Marshal.load(File.binread(WEIGHTS_CACHE_FILE))
|
|
cached[:mtime] == File.mtime(patterns_file) ? cached[:weights] : nil
|
|
rescue
|
|
nil
|
|
end
|
|
|
|
def save_weights_cache(weights, patterns_file)
|
|
File.binwrite(WEIGHTS_CACHE_FILE, Marshal.dump({ mtime: File.mtime(patterns_file), weights: weights }))
|
|
end
|
|
|
|
def train_hopfield(vectors)
|
|
n = vectors[0].length
|
|
weights = Array.new(n) { Array.new(n, 0.0) }
|
|
|
|
vectors.each do |v|
|
|
n.times do |i|
|
|
n.times do |j|
|
|
weights[i][j] += v[i] * v[j] unless i == j
|
|
end
|
|
end
|
|
end
|
|
|
|
weights
|
|
end
|
|
|
|
def step_hopfield(weights, vector)
|
|
n = vector.length
|
|
Array.new(n) do |i|
|
|
sum = 0.0
|
|
n.times { |j| sum += weights[i][j] * vector[j] }
|
|
sum >= 0 ? 1 : -1
|
|
end
|
|
end
|
|
|
|
MAX_HOPFIELD_ITER = 200
|
|
|
|
def recall_hopfield(weights, input_vec)
|
|
current = input_vec.dup
|
|
MAX_HOPFIELD_ITER.times do
|
|
next_vec = step_hopfield(weights, current)
|
|
return current if vectors_equal?(current, next_vec)
|
|
current = next_vec
|
|
end
|
|
current
|
|
end |