pso 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4c638f93201545f2cc9b6df97e74bcd32a9e5c33abd478d79525a01a73a125fb
4
+ data.tar.gz: 724062befc0037ce7548a0a379c8c8da1e10b78eb93924c6f8095660f45f36c9
5
+ SHA512:
6
+ metadata.gz: 80bd1ad7e0875e9fbd94cbea03e7327ab9b39995a9c1433d68d0e5dc07bd22ec2113ff5dea38369f196093015d3d1f3ee3d25cc90315d62dbb6623b8305d6cbe
7
+ data.tar.gz: 9a9f14196e96c535201510ebcd933c05c20b8e01d3ed8ef9c3dd9eeeccb643522dc0fb45ca5dabfb9e1a5c075a848b0b797b405260a58d947f4c9c8127da01db
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at marlon.henry@magrathealabs.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| 'https://github.com/#{repo_name}' }
4
+
5
+ # Specify your gem's dependencies in pso.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pso (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ pso!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Marlon Henry Schweigert
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Pso
2
+
3
+ Particle Swarm Optimization
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pso'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pso
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ 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.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/schweigert/pso. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ ## Code of Conduct
40
+
41
+ Everyone interacting in the Pso project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/pso/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'pso'
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/pso ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'pso'
5
+ require 'irb'
6
+
7
+ IRB.start(__FILE__)
@@ -0,0 +1,5 @@
1
+ require 'pso/version'
2
+ require 'pso/solver'
3
+
4
+ module Pso
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'matrix'
2
+
3
+ module Pso
4
+ class Function
5
+ def f(vector)
6
+ vector.magnitude
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'pso/function'
2
+ require 'pso/zero_vector'
3
+
4
+ module Pso
5
+ class Rastrigin < Pso::Function
6
+ def f(vector)
7
+ fitness = 10 * vector.size
8
+ fitness + vector.map { |n| n ** 2 - 10 * Math.cos(2 * Math::PI * n) }.sum
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'pso/function'
2
+ require 'pso/zero_vector'
3
+
4
+ module Pso
5
+ class Schwefel < Pso::Function
6
+ def f(vector)
7
+ alpha = 418.982887
8
+ vector.map { |n| -n * Math.sin(Math.sqrt(n.to_f.abs))}.sum + alpha * vector.size
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,90 @@
1
+ require 'thread'
2
+ require 'pso/zero_vector'
3
+ require 'pso/functions/rastrigin'
4
+
5
+ module Pso
6
+ class Solver
7
+ def initialize(din: 5, density: 5000, f: Pso::Rastrigin, center: ZeroVector[0,0,0,0,0], radius: 5.12, method: :min_by)
8
+ @f = f.new
9
+ @din = din
10
+ @center = center
11
+ @radius = radius
12
+ @method = method
13
+ @density = density
14
+
15
+ generate_swarm
16
+ end
17
+
18
+ def generate_swarm
19
+ Array.new(@density)
20
+ @swarm = Array.new(@density) { generate_random_particle }
21
+ @swarm_best = @swarm.map { |particle| [@f.f(particle), particle] }
22
+ end
23
+
24
+ def generate_random_noise_particle
25
+ @center.map { rand * 2 - 1 }
26
+ end
27
+
28
+ def generate_random_particle
29
+ @center + (generate_random_noise_particle * (@radius * rand))
30
+ end
31
+
32
+ def perfect_particle
33
+ if @method == :min_by
34
+ @swarm.min_by do |element|
35
+ @f.f(element)
36
+ end
37
+ else
38
+ @swarm.max_by do |element|
39
+ @f.f(element)
40
+ end
41
+ end
42
+ end
43
+
44
+ def solve(precision: 2000, threads: 4)
45
+ Array.new(threads).map do
46
+ Thread.new do
47
+ (precision / threads).times do
48
+ perfect = perfect_particle
49
+ for index in 0...@din
50
+ new_vector = normalize(interate(@swarm[index], @swarm_best[index].last, perfect))
51
+
52
+ if is_best(@swarm_best[index].first, @f.f(new_vector))
53
+ @swarm_best[index] = [@f.f(new_vector), new_vector]
54
+ end
55
+
56
+ @swarm[index] = new_vector
57
+ end
58
+ end
59
+ end
60
+ end.each do |thread|
61
+ thread.join
62
+ end
63
+
64
+ perfect = perfect_particle
65
+ [@f.f(perfect), perfect]
66
+ end
67
+
68
+ private
69
+
70
+ def is_best(best, now)
71
+ if @method == :min_by
72
+ now < best
73
+ else
74
+ now > best
75
+ end
76
+ end
77
+
78
+ def normalize(vector)
79
+ if (vector - @center).magnitude > @radius
80
+ ((vector - @center).normalize * @radius) + @center
81
+ end
82
+
83
+ vector
84
+ end
85
+
86
+ def interate(vector, best, perfect)
87
+ return vector + (generate_random_noise_particle) + (best - vector).normalize + (perfect - vector).normalize
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ module Pso
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'matrix'
2
+
3
+ class ZeroVector < Vector
4
+ def normalize
5
+ return self if zero?
6
+ super
7
+ end
8
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pso/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pso'
8
+ spec.version = Pso::VERSION
9
+ spec.authors = ['Schweigert']
10
+ spec.email = ['marlon.henry@magrathealabs.com']
11
+
12
+ spec.summary = 'Particle Swarm Optimization'
13
+ spec.description = 'Particle Swarm Optimization'
14
+ spec.homepage = 'http://www.github.com/schweigert/pso'
15
+ spec.license = 'MIT'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables << 'pso'
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pso
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Schweigert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-22 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: Particle Swarm Optimization
56
+ email:
57
+ - marlon.henry@magrathealabs.com
58
+ executables:
59
+ - pso
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - exe/pso
75
+ - lib/pso.rb
76
+ - lib/pso/function.rb
77
+ - lib/pso/functions/rastrigin.rb
78
+ - lib/pso/functions/schwefel.rb
79
+ - lib/pso/solver.rb
80
+ - lib/pso/version.rb
81
+ - lib/pso/zero_vector.rb
82
+ - pso.gemspec
83
+ homepage: http://www.github.com/schweigert/pso
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
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.7.3
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Particle Swarm Optimization
107
+ test_files: []