sbn 0.9.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +26 -0
- data/{README → README.rdoc} +6 -6
- data/Rakefile +11 -0
- data/data/grass_wetness.dot +9 -0
- data/data/grass_wetness.xml +71 -0
- data/data/images/grass_wetness.png +0 -0
- data/data/images/stringvar1.png +0 -0
- data/data/images/stringvar2.png +0 -0
- data/data/stringvar1.dot +9 -0
- data/data/stringvar2.dot +25 -0
- data/lib/sbn.rb +11 -5
- data/lib/sbn/combination.rb +80 -0
- data/lib/{formats.rb → sbn/formats.rb} +4 -1
- data/lib/{helpers.rb → sbn/helpers.rb} +0 -0
- data/lib/{inference.rb → sbn/inference.rb} +1 -1
- data/lib/{learning.rb → sbn/learning.rb} +1 -1
- data/lib/{net.rb → sbn/net.rb} +1 -1
- data/lib/{numeric_variable.rb → sbn/numeric_variable.rb} +1 -3
- data/lib/{string_variable.rb → sbn/string_variable.rb} +1 -3
- data/lib/{variable.rb → sbn/variable.rb} +1 -1
- data/lib/sbn/version.rb +3 -0
- data/sbn.gemspec +26 -0
- data/test/test_combination.rb +2 -2
- data/test/test_helpers.rb +1 -1
- data/test/test_learning.rb +1 -1
- data/test/test_net.rb +1 -1
- data/test/test_variable.rb +1 -1
- metadata +131 -54
- data/lib/combination.rb +0 -78
- data/test/sbn.rb +0 -5
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sbn (0.9.1)
|
5
|
+
activesupport (~> 3.1.0)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
i18n (~> 0.6.0)
|
8
|
+
xml-simple (~> 1.1.0)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: http://rubygems.org/
|
12
|
+
specs:
|
13
|
+
activesupport (3.1.0)
|
14
|
+
multi_json (~> 1.0)
|
15
|
+
builder (3.0.0)
|
16
|
+
i18n (0.6.0)
|
17
|
+
multi_json (1.0.3)
|
18
|
+
rake (0.9.2)
|
19
|
+
xml-simple (1.1.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
rake (~> 0.9.2)
|
26
|
+
sbn!
|
data/{README → README.rdoc}
RENAMED
@@ -1,6 +1,6 @@
|
|
1
1
|
= SBN - Simple Bayesian Networks
|
2
2
|
== Software License Agreement
|
3
|
-
Copyright (c) 2005-
|
3
|
+
Copyright (c) 2005-2011 Carl Youngblood mailto:carl@youngbloods.org
|
4
4
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
@@ -38,7 +38,7 @@ Installation of SBN is simple:
|
|
38
38
|
# gem install sbn
|
39
39
|
|
40
40
|
== A Basic Example
|
41
|
-
http://
|
41
|
+
http://dl.dropbox.com/u/654274/sbn/images/grass_wetness.png
|
42
42
|
|
43
43
|
We'll begin with a network whose probabilities have been pre-determined. This
|
44
44
|
example comes from the excellent <em>Artificial Intelligence: A Modern
|
@@ -284,7 +284,7 @@ The following example shows a network that uses these advanced variable types:
|
|
284
284
|
|
285
285
|
Before parameter learning occurs on this network, it looks like this:
|
286
286
|
|
287
|
-
http://
|
287
|
+
http://dl.dropbox.com/u/654274/sbn/images/stringvar1.png
|
288
288
|
|
289
289
|
The <em>Category</em> variable represents a budget category for a financial
|
290
290
|
transaction. The <em>Amount</em> variable is for the amount of the transaction
|
@@ -296,7 +296,7 @@ this network:
|
|
296
296
|
|
297
297
|
After adding that sample point, the network would look something like this:
|
298
298
|
|
299
|
-
http://
|
299
|
+
http://dl.dropbox.com/u/654274/sbn/images/stringvar2.png
|
300
300
|
|
301
301
|
The variables with dashed edges are the string covariables that were created
|
302
302
|
by the managing string variable when it saw a new string in the sample points.
|
@@ -312,9 +312,9 @@ are some of the possible improvements that may be added in future releases:
|
|
312
312
|
* Saving the sample points along with the network when saving to XMLBIF
|
313
313
|
* Speedier inference using native C++ with vectorization provided by macstl[http://www.pixelglow.com/macstl/]
|
314
314
|
* Speedier inference through parallelization
|
315
|
-
* Support for
|
315
|
+
* Support for inference of variable relationships from sample points
|
316
316
|
* Support for customizing the number of iterations in the MCMC algorithm (currently hard-coded)
|
317
317
|
* Support for customizing the size of ngrams used in string variables
|
318
318
|
* Support for intelligently determining the best number of iterations for MCMC at runtime based on the desired level of precision
|
319
319
|
|
320
|
-
Please share your own ideas with us and help to improve this library.
|
320
|
+
Please share your own ideas with us and help to improve this library.
|
data/Rakefile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!--
|
3
|
+
Bayesian network in XMLBIF v0.3 (BayesNet Interchange Format)
|
4
|
+
Produced by SBN4R (Simple Bayesian Network library for Ruby)
|
5
|
+
Output created Sat Jan 27 17:00:04 -0700 2007
|
6
|
+
-->
|
7
|
+
|
8
|
+
<!-- DTD for the XMLBIF 0.3 format -->
|
9
|
+
<!DOCTYPE bif [
|
10
|
+
<!ELEMENT bif (network)*>
|
11
|
+
<!ATTLIST bif version CDATA #REQUIRED>
|
12
|
+
<!ELEMENT network (name, (property | variable | definition)*)>
|
13
|
+
<!ELEMENT name (#PCDATA)>
|
14
|
+
<!ELEMENT variable (name, (outcome | property)*)>
|
15
|
+
<!ATTLIST variable type (nature | decision | utility) "nature">
|
16
|
+
<!ELEMENT outcome (#PCDATA)>
|
17
|
+
<!ELEMENT definition (for | given | table | property)*>
|
18
|
+
<!ELEMENT for (#PCDATA)>
|
19
|
+
<!ELEMENT given (#PCDATA)>
|
20
|
+
<!ELEMENT table (#PCDATA)>
|
21
|
+
<!ELEMENT property (#PCDATA)>
|
22
|
+
]>
|
23
|
+
<bif version="0.3">
|
24
|
+
<network>
|
25
|
+
<name>Grass_Wetness_Belief_Net</name>
|
26
|
+
|
27
|
+
<!-- Variables -->
|
28
|
+
<variable type="nature">
|
29
|
+
<name>Cloudy</name>
|
30
|
+
<outcome>true</outcome>
|
31
|
+
<outcome>false</outcome>
|
32
|
+
</variable>
|
33
|
+
<variable type="nature">
|
34
|
+
<name>Grass_Wet</name>
|
35
|
+
<outcome>true</outcome>
|
36
|
+
<outcome>false</outcome>
|
37
|
+
</variable>
|
38
|
+
<variable type="nature">
|
39
|
+
<name>Sprinkler</name>
|
40
|
+
<outcome>true</outcome>
|
41
|
+
<outcome>false</outcome>
|
42
|
+
</variable>
|
43
|
+
<variable type="nature">
|
44
|
+
<name>Rain</name>
|
45
|
+
<outcome>true</outcome>
|
46
|
+
<outcome>false</outcome>
|
47
|
+
</variable>
|
48
|
+
|
49
|
+
<!-- Probability distributions -->
|
50
|
+
<definition>
|
51
|
+
<for>Cloudy</for>
|
52
|
+
<table>0.5 0.5</table>
|
53
|
+
</definition>
|
54
|
+
<definition>
|
55
|
+
<for>Grass_Wet</for>
|
56
|
+
<given>Sprinkler</given>
|
57
|
+
<given>Rain</given>
|
58
|
+
<table>0.99 0.01 0.9 0.1 0.9 0.1 0.0 1.0</table>
|
59
|
+
</definition>
|
60
|
+
<definition>
|
61
|
+
<for>Sprinkler</for>
|
62
|
+
<given>Cloudy</given>
|
63
|
+
<table>0.1 0.9 0.5 0.5</table>
|
64
|
+
</definition>
|
65
|
+
<definition>
|
66
|
+
<for>Rain</for>
|
67
|
+
<given>Cloudy</given>
|
68
|
+
<table>0.8 0.2 0.2 0.8</table>
|
69
|
+
</definition>
|
70
|
+
</network>
|
71
|
+
</bif>
|
Binary file
|
Binary file
|
Binary file
|
data/data/stringvar1.dot
ADDED
data/data/stringvar2.dot
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
digraph untitled
|
2
|
+
{
|
3
|
+
category[label="Category"];
|
4
|
+
amount[label="Amount\n(NumericVariable)"];
|
5
|
+
merchant[label="Merchant\n(StringVariable)"];
|
6
|
+
covar1[label="merchant_covar_che",style="dashed"]
|
7
|
+
covar2[label="merchant_covar_hev",style="dashed"]
|
8
|
+
covar3[label="merchant_covar_evr",style="dashed"]
|
9
|
+
covar4[label="merchant_covar_vro",style="dashed"]
|
10
|
+
covar5[label="merchant_covar_ron",style="dashed"]
|
11
|
+
covar6[label="merchant_covar_chevro",style="dashed"]
|
12
|
+
covar7[label="merchant_covar_hevron",style="dashed"]
|
13
|
+
covar8[label="merchant_covar_chevron",style="dashed"]
|
14
|
+
|
15
|
+
category -> amount;
|
16
|
+
category -> merchant;
|
17
|
+
category -> covar1[style="dashed"];
|
18
|
+
category -> covar2[style="dashed"];
|
19
|
+
category -> covar3[style="dashed"];
|
20
|
+
category -> covar4[style="dashed"];
|
21
|
+
category -> covar5[style="dashed"];
|
22
|
+
category -> covar6[style="dashed"];
|
23
|
+
category -> covar7[style="dashed"];
|
24
|
+
category -> covar8[style="dashed"];
|
25
|
+
}
|
data/lib/sbn.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
-
require '
|
2
|
-
require 'active_support'
|
3
|
-
gem 'builder', '>=2.0'
|
4
|
-
require 'builder'
|
1
|
+
require 'active_support/inflector'
|
5
2
|
|
6
|
-
|
3
|
+
require 'sbn/version'
|
4
|
+
require 'sbn/helpers'
|
5
|
+
require 'sbn/variable'
|
6
|
+
require 'sbn/numeric_variable'
|
7
|
+
require 'sbn/string_variable'
|
8
|
+
require 'sbn/combination'
|
9
|
+
require 'sbn/formats'
|
10
|
+
require 'sbn/inference'
|
11
|
+
require 'sbn/learning'
|
12
|
+
require 'sbn/net'
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# = combination.rb: Class for handling variable state combinations
|
2
|
+
# Copyright (C) 2005-2007 Carl Youngblood mailto:carl@youngbloods.org
|
3
|
+
#
|
4
|
+
# Takes an array of arrays and iterates over all combinations of sub-elements.
|
5
|
+
# For example:
|
6
|
+
#
|
7
|
+
# c = Sbn::Combination.new([[1, 2], [6, 7, 8]])
|
8
|
+
# c.each {|comb| p comb }
|
9
|
+
#
|
10
|
+
# Will produce:
|
11
|
+
#
|
12
|
+
# [1, 6]
|
13
|
+
# [1, 7]
|
14
|
+
# [1, 8]
|
15
|
+
# [2, 6]
|
16
|
+
# [2, 7]
|
17
|
+
# [2, 8]
|
18
|
+
|
19
|
+
module Sbn
|
20
|
+
class Combination # :nodoc:
|
21
|
+
include Enumerable
|
22
|
+
|
23
|
+
def initialize(arr)
|
24
|
+
@arr = arr
|
25
|
+
@current = Array.new(arr.size, 0)
|
26
|
+
end
|
27
|
+
|
28
|
+
def each
|
29
|
+
iterations = @arr.inject(1) {|product, element| product * element.size } - 1
|
30
|
+
yield current
|
31
|
+
iterations.times { yield self.next_combination }
|
32
|
+
end
|
33
|
+
|
34
|
+
def <=>(other)
|
35
|
+
@current <=> other.current
|
36
|
+
end
|
37
|
+
|
38
|
+
def first
|
39
|
+
@current.fill 0
|
40
|
+
end
|
41
|
+
|
42
|
+
def last
|
43
|
+
@current.size.times {|i| @current[i] = @arr[i].size - 1 }
|
44
|
+
end
|
45
|
+
|
46
|
+
def current
|
47
|
+
returnval = []
|
48
|
+
@current.size.times {|i| returnval[i] = @arr[i][@current[i]] }
|
49
|
+
returnval
|
50
|
+
end
|
51
|
+
|
52
|
+
def next_combination
|
53
|
+
i = @current.size - 1
|
54
|
+
@current.reverse.each do |e|
|
55
|
+
if e == @arr[i].size - 1
|
56
|
+
@current[i] = 0
|
57
|
+
else
|
58
|
+
@current[i] += 1
|
59
|
+
break
|
60
|
+
end
|
61
|
+
i -= 1
|
62
|
+
end
|
63
|
+
current
|
64
|
+
end
|
65
|
+
|
66
|
+
def prev_combination
|
67
|
+
i = @current.size - 1
|
68
|
+
@current.reverse.each do |e|
|
69
|
+
if e == 0
|
70
|
+
@current[i] = @arr[i].size - 1
|
71
|
+
else
|
72
|
+
@current[i] -= 1
|
73
|
+
break
|
74
|
+
end
|
75
|
+
i -= 1
|
76
|
+
end
|
77
|
+
current
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
File without changes
|
data/lib/{net.rb → sbn/net.rb}
RENAMED
data/lib/sbn/version.rb
ADDED
data/sbn.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "sbn/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sbn"
|
7
|
+
s.version = Sbn::VERSION
|
8
|
+
s.authors = ["Carl Youngblood"]
|
9
|
+
s.email = ["carl@youngbloods.org"]
|
10
|
+
s.homepage = "http://github.com/cayblood/sbn"
|
11
|
+
s.summary = "Simple Bayesian Network Library"
|
12
|
+
s.description = "SBN makes it easy to use Bayesian Networks in your ruby application."
|
13
|
+
|
14
|
+
s.rubyforge_project = "sbn"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rake", "~> 0.9.2"
|
22
|
+
s.add_runtime_dependency "builder", "~> 3.0.0"
|
23
|
+
s.add_runtime_dependency "xml-simple", "~> 1.1.0"
|
24
|
+
s.add_runtime_dependency "i18n", "~> 0.6.0"
|
25
|
+
s.add_runtime_dependency "activesupport", "~> 3.1.0"
|
26
|
+
end
|
data/test/test_combination.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require
|
2
|
+
require 'sbn/combination'
|
3
3
|
|
4
4
|
class TestCombination < Test::Unit::TestCase # :nodoc:
|
5
5
|
def setup
|
6
|
-
@c = Combination.new([[1, 2], [3, 4, 5]])
|
6
|
+
@c = Sbn::Combination.new([[1, 2], [3, 4, 5]])
|
7
7
|
end
|
8
8
|
|
9
9
|
def test_current
|
data/test/test_helpers.rb
CHANGED
data/test/test_learning.rb
CHANGED
data/test/test_net.rb
CHANGED
data/test/test_variable.rb
CHANGED
metadata
CHANGED
@@ -1,63 +1,140 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.2
|
3
|
-
specification_version: 1
|
1
|
+
--- !ruby/object:Gem::Specification
|
4
2
|
name: sbn
|
5
|
-
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.9.
|
7
|
-
|
8
|
-
summary: Simple Bayesian Network Library
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: carl@youngbloods.org
|
12
|
-
homepage: http://youngbloods.org/
|
13
|
-
rubyforge_project:
|
14
|
-
description:
|
15
|
-
autorequire: sbn
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.1
|
5
|
+
prerelease:
|
25
6
|
platform: ruby
|
26
|
-
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
|
-
authors:
|
7
|
+
authors:
|
30
8
|
- Carl Youngblood
|
31
|
-
|
32
|
-
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-18 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70106710764580 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.2
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70106710764580
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: builder
|
27
|
+
requirement: &70106710763900 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70106710763900
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: xml-simple
|
38
|
+
requirement: &70106710763320 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.1.0
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70106710763320
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: i18n
|
49
|
+
requirement: &70106710762480 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.6.0
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70106710762480
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: activesupport
|
60
|
+
requirement: &70106710761320 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 3.1.0
|
66
|
+
type: :runtime
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70106710761320
|
69
|
+
description: SBN makes it easy to use Bayesian Networks in your ruby application.
|
70
|
+
email:
|
71
|
+
- carl@youngbloods.org
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- README.rdoc
|
80
|
+
- Rakefile
|
81
|
+
- data/grass_wetness.dot
|
82
|
+
- data/grass_wetness.xml
|
83
|
+
- data/images/grass_wetness.png
|
84
|
+
- data/images/stringvar1.png
|
85
|
+
- data/images/stringvar2.png
|
86
|
+
- data/stringvar1.dot
|
87
|
+
- data/stringvar2.dot
|
88
|
+
- lib/sbn.rb
|
89
|
+
- lib/sbn/combination.rb
|
90
|
+
- lib/sbn/formats.rb
|
91
|
+
- lib/sbn/helpers.rb
|
92
|
+
- lib/sbn/inference.rb
|
93
|
+
- lib/sbn/learning.rb
|
94
|
+
- lib/sbn/net.rb
|
95
|
+
- lib/sbn/numeric_variable.rb
|
96
|
+
- lib/sbn/string_variable.rb
|
97
|
+
- lib/sbn/variable.rb
|
98
|
+
- lib/sbn/version.rb
|
99
|
+
- sbn.gemspec
|
33
100
|
- test/test_combination.rb
|
34
101
|
- test/test_helpers.rb
|
35
102
|
- test/test_learning.rb
|
36
103
|
- test/test_net.rb
|
37
104
|
- test/test_variable.rb
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
- lib
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
-
|
48
|
-
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
105
|
+
homepage: http://github.com/cayblood/sbn
|
106
|
+
licenses: []
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
segments:
|
118
|
+
- 0
|
119
|
+
hash: -2564604872438714073
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
hash: -2564604872438714073
|
60
129
|
requirements: []
|
61
|
-
|
62
|
-
|
63
|
-
|
130
|
+
rubyforge_project: sbn
|
131
|
+
rubygems_version: 1.8.6
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: Simple Bayesian Network Library
|
135
|
+
test_files:
|
136
|
+
- test/test_combination.rb
|
137
|
+
- test/test_helpers.rb
|
138
|
+
- test/test_learning.rb
|
139
|
+
- test/test_net.rb
|
140
|
+
- test/test_variable.rb
|
data/lib/combination.rb
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
# = combination.rb: Class for handling variable state combinations
|
2
|
-
# Copyright (C) 2005-2007 Carl Youngblood mailto:carl@youngbloods.org
|
3
|
-
#
|
4
|
-
# Takes an array of arrays and iterates over all combinations of sub-elements.
|
5
|
-
# For example:
|
6
|
-
#
|
7
|
-
# c = Combination.new([[1, 2], [6, 7, 8]])
|
8
|
-
# c.each {|comb| p comb }
|
9
|
-
#
|
10
|
-
# Will produce:
|
11
|
-
#
|
12
|
-
# [1, 6]
|
13
|
-
# [1, 7]
|
14
|
-
# [1, 8]
|
15
|
-
# [2, 6]
|
16
|
-
# [2, 7]
|
17
|
-
# [2, 8]
|
18
|
-
|
19
|
-
class Combination # :nodoc:
|
20
|
-
include Enumerable
|
21
|
-
|
22
|
-
def initialize(arr)
|
23
|
-
@arr = arr
|
24
|
-
@current = Array.new(arr.size, 0)
|
25
|
-
end
|
26
|
-
|
27
|
-
def each
|
28
|
-
iterations = @arr.inject(1) {|product, element| product * element.size } - 1
|
29
|
-
yield current
|
30
|
-
iterations.times { yield self.next_combination }
|
31
|
-
end
|
32
|
-
|
33
|
-
def <=>(other)
|
34
|
-
@current <=> other.current
|
35
|
-
end
|
36
|
-
|
37
|
-
def first
|
38
|
-
@current.fill 0
|
39
|
-
end
|
40
|
-
|
41
|
-
def last
|
42
|
-
@current.size.times {|i| @current[i] = @arr[i].size - 1 }
|
43
|
-
end
|
44
|
-
|
45
|
-
def current
|
46
|
-
returnval = []
|
47
|
-
@current.size.times {|i| returnval[i] = @arr[i][@current[i]] }
|
48
|
-
returnval
|
49
|
-
end
|
50
|
-
|
51
|
-
def next_combination
|
52
|
-
i = @current.size - 1
|
53
|
-
@current.reverse.each do |e|
|
54
|
-
if e == @arr[i].size - 1
|
55
|
-
@current[i] = 0
|
56
|
-
else
|
57
|
-
@current[i] += 1
|
58
|
-
break
|
59
|
-
end
|
60
|
-
i -= 1
|
61
|
-
end
|
62
|
-
current
|
63
|
-
end
|
64
|
-
|
65
|
-
def prev_combination
|
66
|
-
i = @current.size - 1
|
67
|
-
@current.reverse.each do |e|
|
68
|
-
if e == 0
|
69
|
-
@current[i] = @arr[i].size - 1
|
70
|
-
else
|
71
|
-
@current[i] -= 1
|
72
|
-
break
|
73
|
-
end
|
74
|
-
i -= 1
|
75
|
-
end
|
76
|
-
current
|
77
|
-
end
|
78
|
-
end
|