goalseek 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/goalseek.gemspec +28 -0
- data/lib/goalseek.rb +88 -0
- data/lib/goalseek/version.rb +3 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 747c7855df771b99e8a14ae0d7dda32bda5216c4
|
4
|
+
data.tar.gz: 64351ed03ddb34be21d332a3eeaef5f60505e51c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a6def3a9b3e69033c550fbbe666de917335c53eab464ed94a0fea65b634cf8af2bb6be4de47294e76da6e2dfd8e4703dd2d95aadb29c671aee383858820e200
|
7
|
+
data.tar.gz: eca9ff2df3c75ce9b552cd9a0e71e5133ef45fc50d46b94654e5350fed2787a10dab7990f3acc452d3763ea2eb3796f1e62d2f08aa07eda8b676157f37f8a729
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Pascal Ehlert
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# GoalSeek
|
2
|
+
|
3
|
+
This gem implements a method similar to Microsoft Excel's GoalSeek macro. According to Microsoft,
|
4
|
+
GoalSeek uses a simple linear search:
|
5
|
+
> The Goal Seek command uses a simple linear search beginning with guesses on the positive or negative side of the
|
6
|
+
> value in the source cell (By Changing Cell). Excel uses the initial guesses and recalculates the formula.
|
7
|
+
> Whichever guess brings the formula result closer to the targeted result (To Value) is the direction (positive or
|
8
|
+
> negative) in which Goal Seek heads. If neither direction appears to approach the target value, Goal Seek makes
|
9
|
+
> additional guesses that are further away from the source cell. After the direction is determined, Goal Seek uses
|
10
|
+
> an iterative process in which the source cell is incremented or decremented at varying rates until the target value
|
11
|
+
> is reached.
|
12
|
+
|
13
|
+
Source: https://support.microsoft.com/en-us/help/100782/xl-method-used-by-goal-seek-to-find-a-solution
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'goalseek'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install goalseek
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
TODO: Write usage instructions here
|
34
|
+
|
35
|
+
## Development
|
36
|
+
|
37
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
38
|
+
|
39
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/pehlert/goalseek.
|
44
|
+
|
45
|
+
## License
|
46
|
+
|
47
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "goalseek"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/goalseek.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "goalseek/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "goalseek"
|
8
|
+
spec.version = GoalSeek::VERSION
|
9
|
+
spec.authors = ["Pascal Ehlert"]
|
10
|
+
spec.email = ["pascal@threerabbits.io"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby implementation of Microsoft Excel's GoalSeek macro.}
|
13
|
+
spec.description = %q{Ruby implementation of Microsoft Excel's GoalSeek macro.
|
14
|
+
Uses a simple linear bisection method.}
|
15
|
+
spec.homepage = "https://github.com/pehlert/goalseek"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
data/lib/goalseek.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'goalseek/version'
|
2
|
+
|
3
|
+
module GoalSeek
|
4
|
+
class InvalidBoundError < Exception
|
5
|
+
end
|
6
|
+
|
7
|
+
class InvalidFunctionError < Exception
|
8
|
+
end
|
9
|
+
|
10
|
+
class GoalSeek
|
11
|
+
|
12
|
+
# block is an f(x) function
|
13
|
+
def initialize(f)
|
14
|
+
@f = f
|
15
|
+
end
|
16
|
+
|
17
|
+
def seek(goal, options = {})
|
18
|
+
@goal = goal.to_f
|
19
|
+
|
20
|
+
options = {
|
21
|
+
tolerance: 0,
|
22
|
+
max_iterations: 1000
|
23
|
+
}.merge(options)
|
24
|
+
|
25
|
+
min_max = get_min_max(options[:lower_bound], options[:upper_bound])
|
26
|
+
lower, upper = min_max[0], min_max[1]
|
27
|
+
|
28
|
+
iterate(lower, upper, options[:tolerance], options[:max_iterations])
|
29
|
+
end
|
30
|
+
|
31
|
+
def iterate(lower, upper, tolerance, max_iterations, iterations = 1)
|
32
|
+
new_bound = (lower + upper) / 2
|
33
|
+
|
34
|
+
begin
|
35
|
+
new_value = @f.call(new_bound)
|
36
|
+
rescue NoMethodError
|
37
|
+
raise InvalidFunctionError
|
38
|
+
end
|
39
|
+
|
40
|
+
if iterations == max_iterations || new_value === @goal
|
41
|
+
return new_bound
|
42
|
+
elsif new_value < @goal
|
43
|
+
return iterate(new_bound, upper, tolerance, max_iterations, iterations + 1)
|
44
|
+
elsif new_value > @goal
|
45
|
+
return iterate(lower, new_bound, tolerance, max_iterations, iterations + 1)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_min_max(a, b)
|
50
|
+
x = nil, y = nil
|
51
|
+
|
52
|
+
# Use given bounds if present
|
53
|
+
if a && b
|
54
|
+
x = a.to_f
|
55
|
+
y = b.to_f
|
56
|
+
|
57
|
+
if check_bounds(x, y)
|
58
|
+
[x, y]
|
59
|
+
elsif check_bounds(y, x)
|
60
|
+
[y, x]
|
61
|
+
else
|
62
|
+
raise InvalidBoundError
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Try exponential bound expansion from 0 to 10^10
|
67
|
+
(0..10).each do |i|
|
68
|
+
if check_bounds(0.0, 10.0 ** i)
|
69
|
+
return [0.0, 10.0 ** i]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Try exponential bound expansion from -x to x
|
74
|
+
(0..10).each do |i|
|
75
|
+
if check_bounds(-10.0 ** i, 10.0 ** i)
|
76
|
+
return [-10.0 ** i, 10.0 ** i]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Fall back to raising if no working bound could be found
|
81
|
+
raise InvalidBoundError
|
82
|
+
end
|
83
|
+
|
84
|
+
def check_bounds(x, y)
|
85
|
+
(@f.call(x) < @goal) && (@f.call(y) > @goal) || (@f.call(y) < @goal) && (@f.call(x) > @goal)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: goalseek
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pascal Ehlert
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: |-
|
56
|
+
Ruby implementation of Microsoft Excel's GoalSeek macro.
|
57
|
+
Uses a simple linear bisection method.
|
58
|
+
email:
|
59
|
+
- pascal@threerabbits.io
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- goalseek.gemspec
|
74
|
+
- lib/goalseek.rb
|
75
|
+
- lib/goalseek/version.rb
|
76
|
+
homepage: https://github.com/pehlert/goalseek
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.6.11
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Ruby implementation of Microsoft Excel's GoalSeek macro.
|
100
|
+
test_files: []
|