Algorithmically 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c91ca3b58d7d092993dc20a3f8ecf2434f34fd45
4
+ data.tar.gz: e3506c96aa0aae8e90cd47c463430bd0cbf2111a
5
+ SHA512:
6
+ metadata.gz: 3a9feda8f6f49348e02129966da3e153ba96134ea893415ceaa18623e8b84baf750fa26e81c9f1e6a23b2ac704f63a39a905f5772fb6ea66ccdd785ba28d1fa5
7
+ data.tar.gz: f9ee9e53fe0449c5f32686fd751cc1f0ea47b84e82c96bdad03daf741497c47f02b3bf3d3c9d7eb3d21e592c9a09b2f91cf561909b91493da63dbcdcea407a46
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /pkg/
2
+ .idea
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'Algorithmically/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'Algorithmically'
8
+ spec.version = Algorithmically::VERSION
9
+ spec.authors = ['popac']
10
+ spec.email = ['web.guru.serbia@gmail.com']
11
+
12
+ spec.summary = %q{Algorithmically}
13
+ spec.description = %q{Nature-Inspired Programming Recipes}
14
+ spec.homepage = 'https://github.com/popac/Algorithmically'
15
+ spec.license = 'MIT'
16
+
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
21
+ end
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.13', '>= 1.13.1'
29
+ spec.add_development_dependency 'rake', '~> 11.3'
30
+ spec.add_development_dependency 'pry', '~> 0.10.4'
31
+ end
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at web.guru.serbia@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ Algorithmically (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.1)
10
+ method_source (0.8.2)
11
+ pry (0.10.4)
12
+ coderay (~> 1.1.0)
13
+ method_source (~> 0.8.1)
14
+ slop (~> 3.4)
15
+ rake (11.3.0)
16
+ slop (3.6.0)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ Algorithmically!
23
+ bundler (~> 1.13, >= 1.13.1)
24
+ pry (~> 0.10.4)
25
+ rake (~> 11.3)
26
+
27
+ BUNDLED WITH
28
+ 1.13.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 popac
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,20 @@
1
+ # Algorithmically
2
+
3
+ Nature-Inspired Programming Recipes
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'Algorithmically'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install Algorithmically
20
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'Algorithmically'
5
+
6
+ require 'pry'
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,7 @@
1
+ require 'Algorithmically/version'
2
+ require 'Algorithmically/Stochastic/random_search'
3
+ require 'Algorithmically/Stochastic/adaptive_random_search'
4
+
5
+ module Algorithmically
6
+ include Stochastic
7
+ end
@@ -0,0 +1,82 @@
1
+ module Stochastic
2
+
3
+ class AdaptiveRandomSearch
4
+
5
+ def initialize(max_iter, bounds, init_factor, s_factor, l_factor, iter_mult, max_no_impr)
6
+ problem_size = bounds
7
+ @bounds1 = Array.new(problem_size) {|i| [-5, +5]}
8
+ @max_iter = max_iter
9
+ @init_factor = init_factor
10
+ @s_factor = s_factor
11
+ @l_factor = l_factor
12
+ @iter_mult = iter_mult
13
+ @max_no_impr = max_no_impr
14
+ @best = search(@max_iter, @bounds1, @init_factor, @s_factor, @l_factor, @iter_mult, @max_no_impr)
15
+ puts "Done. Best Solution: c=#{@best[:cost]}, v=#{@best[:vector].inspect}"
16
+ end
17
+
18
+ def objective_function(vector)
19
+ vector.inject(0) { |sum, x| sum + (x ** 2.0) }
20
+ end
21
+
22
+ def rand_in_bounds(min, max)
23
+ min + ((max-min) * rand())
24
+ end
25
+
26
+ def random_vector(minmax)
27
+ Array.new(minmax.size) do |i|
28
+ rand_in_bounds(minmax[i][0], minmax[i][1])
29
+ end
30
+ end
31
+
32
+ def take_step(minmax, current, step_size)
33
+ position = Array.new(current.size)
34
+ position.size.times do |i|
35
+ min = [minmax[i][0], current[i]-step_size].max
36
+ max = [minmax[i][1], current[i]+step_size].min
37
+ position[i] = rand_in_bounds(min, max)
38
+ end
39
+ position
40
+ end
41
+
42
+ def large_step_size(iter, step_size, s_factor, l_factor, iter_mult)
43
+ step_size * l_factor if iter > 0 and iter.modulo(iter_mult) == 0
44
+ step_size * s_factor
45
+ end
46
+
47
+ def take_steps(bounds, current, step_size, big_stepsize)
48
+ step, big_step = {}, {}
49
+ step[:vector] = take_step(bounds, current[:vector], step_size)
50
+ step[:cost] = objective_function(step[:vector])
51
+ big_step[:vector] = take_step(bounds, current[:vector], big_stepsize)
52
+ big_step[:cost] = objective_function(big_step[:vector])
53
+ return step, big_step
54
+ end
55
+
56
+ def search(max_iter, bounds, init_factor, s_factor, l_factor, iter_mult, max_no_impr)
57
+ step_size = (bounds[0][1]-bounds[0][0]) * init_factor
58
+ current, count = {}, 0
59
+ current[:vector] = random_vector(bounds)
60
+ current[:cost] = objective_function(current[:vector])
61
+ max_iter.times do |iter|
62
+ big_stepsize = large_step_size(iter, step_size, s_factor, l_factor, iter_mult)
63
+ step, big_step = take_steps(bounds, current, step_size, big_stepsize)
64
+ if step[:cost] <= current[:cost] or big_step[:cost] <= current[:cost]
65
+ if big_step[:cost] <= step[:cost]
66
+ step_size, current = big_stepsize, big_step
67
+ else
68
+ current = step
69
+ end
70
+ count = 0
71
+ else
72
+ count += 1
73
+ count, step_size = 0, (step_size/s_factor) if count >= max_no_impr
74
+ end
75
+ puts " > iteration #{(iter+1)}, best=#{current[:cost]}"
76
+ end
77
+ current
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,37 @@
1
+ module Stochastic
2
+
3
+ class RandomSearch
4
+
5
+ def initialize(size, max_iter)
6
+ problem_size = size
7
+ search_space = Array.new(problem_size) { |i| [-5, +5] }
8
+ maximum_iterations = max_iter
9
+ best_solution = self.search(search_space, maximum_iterations)
10
+ puts "Done. Best Solution: c = #{best_solution[:cost]}, v = #{best_solution[:vector].inspect}"
11
+ end
12
+
13
+ def objective_function(vector)
14
+ vector.inject(0) { |sum, x| sum + (x ** 2.0) }
15
+ end
16
+
17
+ def random_vector(minmax)
18
+ Array.new(minmax.size) do |i|
19
+ minmax[i][0] + ((minmax[i][1]) - minmax[i][0] * rand())
20
+ end
21
+ end
22
+
23
+ def search(search_space, maximum_iterations)
24
+ best_solution = nil
25
+ maximum_iterations.times do |iterate|
26
+ search_candidate = {}
27
+ search_candidate[:vector] = random_vector(search_space)
28
+ search_candidate[:cost] = objective_function(search_candidate[:vector])
29
+ best_solution = search_candidate if best_solution.nil? or search_candidate[:cost] < best_solution[:cost]
30
+ puts " > iteration = #{(iterate + 1)}, best = #{best_solution[:cost]}"
31
+ end
32
+ best_solution
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,3 @@
1
+ module Algorithmically
2
+ VERSION = '0.1.1'
3
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Algorithmically
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - popac
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-25 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.13'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.13.1
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.13'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.13.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '11.3'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '11.3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: pry
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 0.10.4
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 0.10.4
61
+ description: Nature-Inspired Programming Recipes
62
+ email:
63
+ - web.guru.serbia@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".gitignore"
69
+ - Algorithmically.gemspec
70
+ - CODE_OF_CONDUCT.md
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - bin/console
77
+ - bin/setup
78
+ - lib/Algorithmically.rb
79
+ - lib/Algorithmically/Stochastic/adaptive_random_search.rb
80
+ - lib/Algorithmically/Stochastic/random_search.rb
81
+ - lib/Algorithmically/version.rb
82
+ homepage: https://github.com/popac/Algorithmically
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ allowed_push_host: https://rubygems.org
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.6.6
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Algorithmically
107
+ test_files: []