Skip to content

Instantly share code, notes, and snippets.

View Poincare's full-sized avatar

Dhaivat Pandya Poincare

View GitHub Profile
require 'eventmachine'
EM.run do
EM.add_periodic_timer(1) do
puts "time elapsed"
end
end
require 'eventmachine'
EM.run do
EM.add_timer(10) do
puts "STOP"
EM.stop_event_loop
end
EM.add_periodic_timer(1) do
require 'eventmachine'
EM.run do
EM.add_periodic_timer(1) do
puts "time elapsed"
end
EM.defer do
logs = IO.readlines('/var/log/kernel.log')
end
end
require 'eventmachine'
EM.run do
spawned_process = EM.spawn do |did_what|
puts "I just " + did_what
end
EM.add_periodic_timer(1) do
spawned_process.notify "ate"
end
def fitness(n):
return n^0b1010
end
class Chromosome
attr_accessor :bitstring
attr_accessor :fitness
def generate_random(length)
i = 0
@bitstring = ""
while i < length
@bitstring += rand(2).to_s
i += 1
class Chromosome
attr_accessor :bitstring
def fitness_func(n)
return n^0b1010
end
...
class Chromosome
...
def self.set_roulette(chrs)
@@roulette = []
chrs.each do |f|
i = 0
while i < f.fitness
@@roulette.push(f)
i += 1
class Chromosome
...
def self.produce_new(chrs)
roulette(chrs)
newchrs = []
chrs.each do |c|
newchrs.push(roulette(chrs))
end
fitnesses(newchrs)
class Chromosome
...
def self.get_mates(chrs)
i = 0
res = {}
#again, the counting is very important (and mistakes are very hard to catch)
#so, we are writing it out in full
while i < chrs.length
res[i] = i+1