class Rbrain

Public Class Methods

accum_all_neurons(brain, wt) click to toggle source
# File lib/rbrain.rb, line 51
def self.accum_all_neurons(brain, wt)
  neurons = brain
  neurons.each do |n|
    n[4] += wt
  end
end
accum_rand_neuron(brain, wt) click to toggle source
# File lib/rbrain.rb, line 34
def self.accum_rand_neuron(brain, wt)
  n = rand(brain.size)
  brain[n][4] += wt
  return n
end
csv_init_brain(file) click to toggle source
# File lib/rbrain.rb, line 85
def self.csv_init_brain(file)
  data ||= CSV.read(file)
  neuron_data = {}
  data.each do |n|
    k = n[0]
    v = n[1].to_i
    neuron_data[k] = v
  end

  brain = self.init_brain(neuron_data.size - 1)
  return neuron_data, brain
end
csv_make_connections(brain, data, file) click to toggle source
# File lib/rbrain.rb, line 98
def self.csv_make_connections(brain, data, file)
  no_conn = 0
  conn_file_data ||= CSV.read(file)
  conn_file_data.each do |connection|
    from_id = connection[0]
    to_id   = connection[1]
    weight  = connection[2]
    brain[data[from_id].to_i][1] << data[to_id].to_i
    brain[data[from_id].to_i][2] << weight.to_i
    no_conn += 1
  end
  return no_conn
end
fire_neuron(neuron, brain) click to toggle source
# File lib/rbrain.rb, line 40
def self.fire_neuron(neuron, brain)
  fired = false
  neuron[1].each do |n_link|
    brain[n_link][4] += neuron[2][n_link] if not neuron[2][n_link].nil?
    neuron[3] = 0
    neuron[4] = 0
    fired = true
  end
  return fired
end
get_threshold() click to toggle source
# File lib/rbrain.rb, line 81
def self.get_threshold
  @threshold
end
init_brain(n_count) click to toggle source
# File lib/rbrain.rb, line 10
def self.init_brain(n_count)
  neurons = []
  for i in 0..(n_count-1) do
    neurons << init_neuron(i)
  end
  return neurons
end
init_neuron(id) click to toggle source
# File lib/rbrain.rb, line 4
def self.init_neuron(id)
  #neuron[id, links, weights, thisstate, nextstate, fired?]
  neuron = [id, [], [], 0, 0, false]
  return neuron
end
make_connections(brain) click to toggle source
# File lib/rbrain.rb, line 18
def self.make_connections(brain)
  num_conn = 0
  brain.each do |neuron|
    0..(rand(1..10)).times do  
      link = rand(1..brain.size-1)
      weight = rand(1..5)
      if not neuron[1].include?(link)
        neuron[1] << link
        neuron[2] << weight
        num_conn += 1
      end
    end
  end
  return num_conn
end
print_fired_neurons(neurons) click to toggle source
set_threshold(value) click to toggle source
# File lib/rbrain.rb, line 77
def self.set_threshold(value)
  @threshold = value
end
update_neuron(neuron, brain) click to toggle source
# File lib/rbrain.rb, line 58
def self.update_neuron(neuron, brain)
  fired = false
  if neuron[3] >= @threshold
    self.fire_neuron(neuron, brain)
    fired = true
  end
  neuron[3] += neuron[4]
  neuron[4] = 0
  neuron[5] = true if fired
  return fired
end