Skip to content

Commit

Permalink
* Version 0.9
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/bn4r/HEAD@8 a06254ae-0921-0410-a5a0-b56d72fd7be6
  • Loading branch information
spejman committed Jan 22, 2007
1 parent 8045404 commit 9b2b97c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
11 changes: 10 additions & 1 deletion lib/bn4r/bn.rb
Expand Up @@ -54,7 +54,11 @@ def remove_vertex(v)

# Returns an array with childs of given node ( or vertice )
def childs(v)
self.adjacent_vertices(v)
begin
self.adjacent_vertices(v)
rescue RGL::NoVertexError
[]
end
end

# Iterates all the childs of given node ( or vertice )
Expand Down Expand Up @@ -173,6 +177,11 @@ def initialize ( name , outcomes = [true, false], extra_info = nil)
@relations = []
end

# Renames a node
def rename(new_name)
@name = new_name
end

# Returns a copy of the node itself
def copy
tmp = BayesNetNode.new(@name, @outcomes, @extra_info)
Expand Down
16 changes: 13 additions & 3 deletions lib/bn4r/bn_algorithms.rb
Expand Up @@ -45,8 +45,13 @@ def enumeration_ask(x,e, bn_vertices = vertices)
# The input are the nodes of the bn ordered by dependencies see nodes_ordered_by_dependencies
def prior_sample(nodes_ordered = nodes_ordered_by_dependencies)
sample = Array.new
nodes_ordered.each { |v|
value = rand < v.get_probability(true)
nodes_ordered.each { |v|
value = nil
prob = 0.0; r_prob = rand
v.outcomes.each { |outcome|
prob += v.get_probability(outcome)
value = outcome and break if r_prob < prob
}
v.set_value(value)
sample << v.copy
}
Expand Down Expand Up @@ -181,7 +186,12 @@ def weighted_sample(e, bn = self)
value = node_actual[0].value
w = w * v.get_probability(value)
else
value = rand < v.get_probability(true)
rand_sample = rand; i_tmp = 0.0
v.outcomes.each { |outcome|
value = outcome
i_tmp += v.get_probability(value)
break if i_tmp > rand_sample
}
end
v.set_value(value)
sample << v.copy
Expand Down
21 changes: 14 additions & 7 deletions lib/bn4r/bn_export.rb
Expand Up @@ -19,7 +19,7 @@ class BayesNet < DirectedAdjacencyGraph

def to_dot(bn = self)
#TODO: label relations between nodes
#TODO: ¿print information about probabilities tables?
#TODO: print information about probabilities tables?
bn.to_dot_graph.to_s
end

Expand Down Expand Up @@ -70,9 +70,12 @@ def xbn_variables(bn = self)
x_pos_index[node.deep-1] += 1;
xbn_str += "<VAR NAME=\"#{node.name}\" TYPE=\"discrete\" XPOS=\"#{x_pos}\" YPOS=\"#{y_pos}\">\n"
xbn_str += "<FULLNAME>#{node.name}</FULLNAME>\n"
# TODO: Make statename match with node.outcomes
xbn_str += "<STATENAME>Yes</STATENAME>\n"
xbn_str += "<STATENAME>No</STATENAME>\n"
if (node.outcomes - [true,false]).empty?
xbn_str += "<STATENAME>Yes</STATENAME>\n"
xbn_str += "<STATENAME>No</STATENAME>\n"
else
node.outcomes.each {|o| xbn_str += "<STATENAME>#{o}</STATENAME>\n"}
end
xbn_str += "</VAR>\n"
}
xbn_str += "</VARIABLES>\n"
Expand Down Expand Up @@ -126,10 +129,14 @@ def xbn_distributions(bn = self)
xbn_str += " </CONDSET>\n"

xbn_str += " <DPIS>\n"
boolean_combinations = generate_boolean_combinations(node.num_parents)

boolean_combinations = generate_combinations(node.parents)
boolean_combinations.each { |boolean_combination|
# TODO: Make probs based on outcomes
probs = [true, false].collect { |n_assignment| node.get_probability(n_assignment, boolean_combination) }

probs = node.outcomes.collect { |n_assignment|
p "n_assignment: " + n_assignment.to_s
node.get_probability(n_assignment, boolean_combination) }

bc_str = boolean_combination.collect {|b| (b)?"0":"1"}.join(" ")
xbn_str += " <DPI INDEXES=\"#{bc_str} \">" + probs.join(" ") + " </DPI>\n"
#<DPI INDEXES=\"0 0 0 \">0.7 0.3 </DPI>
Expand Down
10 changes: 10 additions & 0 deletions lib/bn4r/bn_table_probabilities.rb
Expand Up @@ -77,3 +77,13 @@ def generate_boolean_combinations(num)
boolean_combinations
end

def generate_combinations(nodes)
# Selecting nodes whith outcomes diferents from true, false.
nodes_notbinaries = nodes.select {|node| !(node.outcomes - [true,false]).empty? }

#TODO: Implement combinations for nodes with outcomes different from [true,false]
raise "Function still not implemented ( parents with outcomes different from [true,false] )" \
if !nodes_notbinaries.empty?
generate_boolean_combinations(nodes.size)
end

4 changes: 2 additions & 2 deletions lib/bn4r/version.rb
@@ -1,8 +1,8 @@
module Bn4r #:nodoc:
module VERSION #:nodoc:
MAJOR = 0
MINOR = 1
TINY = 4
MINOR = 9
TINY = 0

STRING = [MAJOR, MINOR, TINY].join('.')
end
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
@@ -1,2 +1,3 @@
require 'rubygems'
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/bn4r'

0 comments on commit 9b2b97c

Please sign in to comment.