minimization 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -1
- data/Rakefile +1 -1
- data/lib/minimization.rb +20 -8
- data/test/test_minimization.rb +16 -6
- metadata +2 -2
data/History.txt
CHANGED
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ Hoe.spec 'minimization' do
|
|
8
8
|
self.version=Minimization::VERSION
|
9
9
|
self.rubyforge_name = 'ruby-statsample' # if different than 'minimization'
|
10
10
|
self.developer('Claudio Bustos', 'clbustos_AT_gmail.com')
|
11
|
-
self.remote_rdoc_dir = '
|
11
|
+
self.remote_rdoc_dir = 'minimization'
|
12
12
|
end
|
13
13
|
|
14
14
|
# vim: syntax=ruby
|
data/lib/minimization.rb
CHANGED
@@ -18,8 +18,10 @@
|
|
18
18
|
#
|
19
19
|
|
20
20
|
module Minimization
|
21
|
-
|
22
|
-
|
21
|
+
|
22
|
+
VERSION="0.1.1"
|
23
|
+
FailedIteration=Class.new(Exception)
|
24
|
+
# Base class for unidimensional minimizers
|
23
25
|
class Unidimensional
|
24
26
|
# Default value for error
|
25
27
|
EPSILON=1e-6
|
@@ -45,10 +47,20 @@ module Minimization
|
|
45
47
|
@epsilon=EPSILON
|
46
48
|
@iterations=0
|
47
49
|
@log=""
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
50
|
+
end
|
51
|
+
# Convenience method to minimize
|
52
|
+
# Usage:
|
53
|
+
# minimizer=Minimization::GoldenSection.minimize(-1000, 1000) {|x|
|
54
|
+
# x**2 }
|
55
|
+
#
|
56
|
+
def self.minimize(lower,upper,expected=nil,&block)
|
57
|
+
minimizer=new(lower,upper,block)
|
58
|
+
minimizer.expected=expected unless expected.nil?
|
59
|
+
raise FailedIteration unless minimizer.iterate
|
60
|
+
minimizer
|
61
|
+
end
|
62
|
+
def f(x)
|
63
|
+
@proc.call(x)
|
52
64
|
end
|
53
65
|
end
|
54
66
|
# = Golden Section Minimizer.
|
@@ -61,9 +73,9 @@ module Minimization
|
|
61
73
|
# min.x_minimum
|
62
74
|
# min.f_minimum
|
63
75
|
# min.log
|
64
|
-
|
76
|
+
class GoldenSection < Unidimensional
|
65
77
|
attr_accessor :expected
|
66
|
-
|
78
|
+
def initialize(lower,upper, proc)
|
67
79
|
super
|
68
80
|
end
|
69
81
|
# Start the iteration
|
data/test/test_minimization.rb
CHANGED
@@ -3,16 +3,26 @@ require "test/unit"
|
|
3
3
|
require "minimization"
|
4
4
|
|
5
5
|
class TestMinimization < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@p1=rand(100)
|
8
|
+
@p2=rand(100)
|
9
|
+
@func=lambda {|x| (x-@p1)**2+@p2}
|
10
|
+
end
|
11
|
+
def test_facade
|
12
|
+
min=Minimization::GoldenSection.minimize(-1000,1000) {|x| (x-@p1)**2+@p2}
|
13
|
+
assert_in_delta(@p1,min.x_minimum, min.epsilon)
|
14
|
+
assert_in_delta(@p2,min.f_minimum, min.epsilon)
|
15
|
+
end
|
6
16
|
def test_golden
|
7
|
-
min=Minimization::GoldenSection.new(-1000,1000,
|
17
|
+
min=Minimization::GoldenSection.new(-1000,1000, @func)
|
8
18
|
min.iterate
|
9
|
-
assert_in_delta(
|
10
|
-
assert_in_delta(
|
19
|
+
assert_in_delta(@p1, min.x_minimum, min.epsilon)
|
20
|
+
assert_in_delta(@p2, min.f_minimum, min.epsilon)
|
11
21
|
end
|
12
22
|
def test_brent
|
13
|
-
min=Minimization::Brent.new(-1000,1000,
|
23
|
+
min=Minimization::Brent.new(-1000,1000, @func)
|
14
24
|
min.iterate
|
15
|
-
assert_in_delta(
|
16
|
-
assert_in_delta(
|
25
|
+
assert_in_delta(@p1, min.x_minimum, min.epsilon)
|
26
|
+
assert_in_delta(@p2, min.f_minimum, min.epsilon)
|
17
27
|
end
|
18
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claudio Bustos
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-03-19 00:00:00 -03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|