module PGM::Bidirectional
describe bidirectionally function to Graph
Public Class Methods
Instantiate DiscreateVariable
@param edgelist_class [Object] Object to store edge class @param other_graphs [PGM::BayesianNet] List of variable states
# File lib/pgm/bidirectional.rb 19 def initialize(edgelist_class = Set, *other_graphs) 20 @vertices_rev_dict = Hash.new 21 @variables = Hash.new 22 super 23 end
Public Instance Methods
Create variable
@param name [String, Symbol] Name of variable @param states [Array<String, Symbol>] List of variable states @param parents [Array<PGM::Variable>] List of parent variable @param type [PGM::Variable] type of variable
# File lib/pgm/bidirectional.rb 55 def create_var(name, states, parents: [], type: PGM::DiscreateVariable) 56 add_vertex(name) 57 @variables[name.to_sym] = type.new(self, name, states, parents: parents) 58 end
Check if model has the variable named as input
@param name [String, Symbol] Name of variable @return [Boolean] if model has the variable
# File lib/pgm/bidirectional.rb 64 def has_var?(name) 65 return @variables.has_key?(name.to_sym) 66 end
Copy internal vertices_dict
@param orig [PGM::BayesianNet] Copy from other graph
# File lib/pgm/bidirectional.rb 28 def initialize_copy(orig) 29 super 30 @vertices_rev_dict = orig.instance_eval { @vertices_rev_dict }.dup 31 @vertices_rev_dict.keys.each do |v| 32 @vertices_rev_dict[v] = @vertices_rev_dict[v].dup 33 end 34 @variables = orig.instance_eval { @variables }.dup 35 end
Return variable with the name
@param name [String, Symbol] The name of variable @return [PGM::Variable] Variable
object
# File lib/pgm/bidirectional.rb 41 def var(name=nil) 42 if nil 43 return @variables 44 else 45 return @variables[name.to_sym] 46 end 47 end
Protected Instance Methods
See MutableGraph#add_vertex.
If the vertex is already in the graph (using eql?), the method does nothing.
# File lib/pgm/bidirectional.rb 90 def add_vertex(v) 91 super 92 @vertices_rev_dict[v] ||= @edgelist_class.new 93 end
# File lib/pgm/bidirectional.rb 80 def basic_add_edge(u, v) 81 super 82 @vertices_rev_dict[v].add(u) 83 end
# File lib/pgm/bidirectional.rb 118 def parent_vertices(v) 119 r = [] 120 each_parent(v) { |u| r << u } 121 r 122 end
See MutableGraph::remove_edge.
Also removes (v,u)
# File lib/pgm/bidirectional.rb 108 def remove_edge(u, v) 109 super 110 @vertices_rev_dict[v].delete(u) unless @vertices_dict[v].nil? 111 end
See MutableGraph#remove_vertex.
# File lib/pgm/bidirectional.rb 97 def remove_vertex(v) 98 super 99 @vertices_rev_dict.delete(v) 100 # remove v from all adjacency lists 101 @vertices_rev_dict.each_value { |adjList| adjList.delete(v) } 102 end