Skip to content

Instantly share code, notes, and snippets.

@hongo35
Last active December 20, 2015 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongo35/6171048 to your computer and use it in GitHub Desktop.
Save hongo35/6171048 to your computer and use it in GitHub Desktop.
require 'json'
class CNN
#parameter
P = 0.75
REPEAT = 200
def initialize
@nodes = [{"name" => 0}]
@links = []
@potential_links = []
end
def create
node_no = 0
REPEAT.times do |i|
rand = rand()
if rand <= P
connect_node = (@nodes.size == 1) ? 0 : rand(@nodes.size - 1)
@nodes.push({"name" => node_no + 1})
if @links.size > 1
@links.each do |link|
if link['source'] == connect_node
@potential_links.push({"source" => node_no + 1, "target" => link['target']})
elsif link['target'] == connect_node
@potential_links.push({"source" => node_no + 1, "target" => link['source']})
end
end
end
@links.push({"source" => node_no + 1, "target" => connect_node})
node_no += 1
else
if @potential_links.size > 0
link = (@potential_links.size == 1) ? @potential_links[0] : @potential_links[rand(@potential_links.size - 1)]
@links.push(link)
@potential_links.delete(link)
end
end
end
puts JSON.generate({"nodes" => @nodes, "links" => @links})
end
end
c = CNN.new
c.create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment