Files
lr_miad_nn/app.rb
T
Dmitry 422cb49b31 Add initial implementation of Hopfield and Hamming networks with web interface
- 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
2026-05-16 22:13:26 +03:00

79 lines
2.1 KiB
Ruby

require 'sinatra'
require 'json'
require_relative 'loader'
require_relative 'functions'
require_relative 'hopfield'
require_relative 'hamming'
EPS = 0.01
set :bind, '0.0.0.0'
set :port, 4567
set :server, 'webrick'
disable :protection
set :host_authorization, { allow_if: ->(_env) { true } }
PATTERNS = load_patterns('patterns.xlsx')
PATTERN_VECTORS = PATTERNS.map { |_, m| to_vector(m) }
PATTERN_NAMES = PATTERNS.keys.map(&:to_s)
WEIGHTS = load_weights_cache('patterns.xlsx') || begin
puts 'Обучение модели...'
w = train_hopfield(PATTERN_VECTORS)
save_weights_cache(w, 'patterns.xlsx')
puts 'Веса сохранены в кэш.'
w
end
get '/' do
@patterns = PATTERNS.transform_values { |m| to_vector(m) }
erb :index
end
post '/recall_steps' do
content_type :json
noisy = JSON.parse(request.body.read)['vector'].map(&:to_f)
# Хопфилд: собираем каждый шаг
hopfield_steps = [noisy.map { |v| v.to_i }]
current = noisy.dup
MAX_HOPFIELD_ITER.times do
nxt = step_hopfield(WEIGHTS, current)
hopfield_steps << nxt
break if vectors_equal?(current, nxt)
current = nxt
end
# Хэмминг: собираем состояния MAXNET
eps = 0.9 / PATTERN_VECTORS.length
outputs = hamming_layer(PATTERN_VECTORS, noisy)
hamming_steps = [outputs.map { |v| v.round(2) }]
MAX_MAXNET_ITER.times do
nxt = maxnet_step(outputs, eps)
hamming_steps << nxt.map { |v| v.round(2) }
break if nxt.count { |v| v > 0 } <= 1
outputs = nxt
end
winner_idx = outputs.each_with_index.max_by { |v, _| v }[1]
{
hopfield_steps: hopfield_steps,
hamming_steps: hamming_steps,
pattern_names: PATTERN_NAMES,
hamming_winner: PATTERN_NAMES[winner_idx]
}.to_json
end
post '/recall' do
content_type :json
noisy = JSON.parse(request.body.read)['vector'].map(&:to_f)
hopfield_result = recall_hopfield(WEIGHTS, noisy)
hamming_idx = recall_hamming(PATTERN_VECTORS, noisy)
{
hopfield: hopfield_result,
hamming: PATTERN_NAMES[hamming_idx]
}.to_json
end