pso 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +7 -1
- data/lib/pso/solver.rb +18 -13
- data/lib/pso/version.rb +1 -1
- data/pso.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 494a9e31cc52065c8b22eea253e6721fe52ac0a991a655b952d7cb23ef25dbdd
|
4
|
+
data.tar.gz: 8ffebd21cb02c8238bb4b29910134092c86583def6ed7d0926dcd2c3dd9c7c03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6c895b7958f867561203cd298e94c775dae38b0b4a665e281edd54f9eb73d645513049df7275fb704bbb2294328a3e4b02c72bba1c4e910dd8d5bcf457eb456
|
7
|
+
data.tar.gz: 29e57ddf40635f9763e95f67fb0607366120077b99946453dd081543d602612da84e079793a474bae5bb1fc30d910ce7f283412fdd55d51ee5bbe43fb7413358
|
data/Gemfile.lock
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
pso (0.1.
|
4
|
+
pso (0.1.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
coderay (1.1.2)
|
9
10
|
diff-lcs (1.3)
|
11
|
+
method_source (0.9.0)
|
12
|
+
pry (0.11.3)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.9.0)
|
10
15
|
rake (10.5.0)
|
11
16
|
rspec (3.7.0)
|
12
17
|
rspec-core (~> 3.7.0)
|
@@ -27,6 +32,7 @@ PLATFORMS
|
|
27
32
|
|
28
33
|
DEPENDENCIES
|
29
34
|
bundler (~> 1.16)
|
35
|
+
pry
|
30
36
|
pso!
|
31
37
|
rake (~> 10.0)
|
32
38
|
rspec (~> 3.0)
|
data/lib/pso/solver.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'thread'
|
2
2
|
require 'pso/zero_vector'
|
3
3
|
require 'pso/functions/rastrigin'
|
4
|
+
require 'pry'
|
4
5
|
|
5
6
|
module Pso
|
6
7
|
class Solver
|
7
|
-
def initialize(din: 5, density:
|
8
|
+
def initialize(din: 5, density: 50, f: Pso::Rastrigin, center: ZeroVector[0,0,0,0,0], radius: 5.12, method: :min_by)
|
8
9
|
@f = f.new
|
9
10
|
@din = din
|
10
11
|
@center = center
|
@@ -19,6 +20,7 @@ module Pso
|
|
19
20
|
Array.new(@density)
|
20
21
|
@swarm = Array.new(@density) { generate_random_particle }
|
21
22
|
@swarm_best = @swarm.map { |particle| [@f.f(particle), particle] }
|
23
|
+
@swarm_speed = @swarm.map { generate_random_particle }
|
22
24
|
end
|
23
25
|
|
24
26
|
def generate_random_noise_particle
|
@@ -41,18 +43,16 @@ module Pso
|
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
44
|
-
def solve(precision:
|
46
|
+
def solve(precision: 200000, threads: 1)
|
45
47
|
Array.new(threads).map do
|
46
48
|
Thread.new do
|
47
|
-
(precision / threads).times do
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
if is_best(@swarm_best[index].first, @f.f(new_vector))
|
53
|
-
|
54
|
-
end
|
55
|
-
|
49
|
+
((precision / @swarm.size) / threads).times do |interation_number|
|
50
|
+
for index in 0...@density
|
51
|
+
perfect = perfect_particle
|
52
|
+
puts @f.f perfect
|
53
|
+
new_vector = normalize(interate(@swarm[index], @swarm_best[index].last, perfect, @swarm_speed[index]))
|
54
|
+
@swarm_best[index] = [@f.f(new_vector), new_vector] if is_best(@swarm_best[index].first, @f.f(new_vector))
|
55
|
+
@swarm_speed[index] = (new_vector - @swarm[index]).normalize
|
56
56
|
@swarm[index] = new_vector
|
57
57
|
end
|
58
58
|
end
|
@@ -83,8 +83,13 @@ module Pso
|
|
83
83
|
vector
|
84
84
|
end
|
85
85
|
|
86
|
-
def interate(vector, best, perfect)
|
87
|
-
|
86
|
+
def interate(vector, best, perfect, speed)
|
87
|
+
if vector == perfect
|
88
|
+
new_vec = vector + (best - vector).normalize * 0.2 + (generate_random_noise_particle) * rand * 0.05 + speed * 0.05
|
89
|
+
minimal = @f.f(vector) > @f.f(new_vec)
|
90
|
+
return @method == :min_by ? (minimal ? new_vec : vector) : ( minimal ? vector : new_vec)
|
91
|
+
end
|
92
|
+
vector + (generate_random_noise_particle) * rand * 0.1 + (best - vector).normalize * 0.5 + (perfect - vector).normalize + speed
|
88
93
|
end
|
89
94
|
end
|
90
95
|
end
|
data/lib/pso/version.rb
CHANGED
data/pso.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pso
|
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
|
- Schweigert
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Particle Swarm Optimization
|
56
70
|
email:
|
57
71
|
- marlon.henry@magrathealabs.com
|