victory 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 030a0da8ae15c1c576f0420a877f2ad6dbe5bd666b27204a1d7832b77253336e
4
- data.tar.gz: c056640572922b46768204052095393268fd1e55a00e6119388d41f616637a42
3
+ metadata.gz: f873e67c8889c379447f6b51603f66162098507e7b9c7627bbe042c39a30d2c7
4
+ data.tar.gz: f4fcfeb27cbd400a202be5c49c20434694c936bde1f2f8077541f7abb5b6f2c7
5
5
  SHA512:
6
- metadata.gz: 5157527a3f066468eeab262aa7e22fea7818ac9668c253b2454ecc80ae2dcd1a35fa33f876a3b653bca3f6136440af062ad17eab5adf464504a58298ce5d8292
7
- data.tar.gz: 6891b77b838db2d460a56cef11472ed69eb0da900b3b423c2bd5f7f58ebcef401c02bef7867314546382937284062be1c7671b161c2ad58ef9b7cd0ecddbf52d
6
+ metadata.gz: 743beca5ef5ee4fd41095016bdd1806517c18279443e7e35f9dc9c95f31a910e1d4b1715d0a5ec2c58647309846efe5f7566b9192595ef831dd7d7398c9515d1
7
+ data.tar.gz: 4bc829f6df6fa44b20f9c733d45ca810a74c1bd8af0026ce265ab0754fd791be0b8f2928473ed701fb275c4214e39694258ad3c382c34874824252f0fc30ef0a
data/USAGE.md CHANGED
@@ -551,6 +551,7 @@ Thread-safe variables:
551
551
 
552
552
  * [Greedy](#greedy)
553
553
  * [Genetic Algorithm](#genetic)
554
+ * [Simulated Annealing](#simulated-annealing)
554
555
 
555
556
  <a name="greedy" />
556
557
 
@@ -590,6 +591,34 @@ genetic_alg.run(100)
590
591
  genetic_alg.best_solution.score
591
592
  ```
592
593
 
594
+ <a name="simulated-annealing" />
595
+
596
+ ## Simulated Annealing
597
+
598
+ ```ruby
599
+ class TestSolution
600
+ include Algorithms::SimulatedAnnealing::Solution
601
+
602
+ def score
603
+ x1, x2 = *@data
604
+ 0.2 + x1 * x1 + x2 * x2 - 0.1 * Math.cos(6 * Math::PI * x1) - 0.1 * Math.cos(6 * Math::PI * x2)
605
+ end
606
+
607
+ def next_solution
608
+ x1, x2 = *@data
609
+ new_x1 = (x1 + rand * 2 - 1) / 2
610
+ new_x2 = (x2 + rand * 2 - 1) / 2
611
+ new_data = T[new_x1, new_x2]
612
+ TestSolution.new(new_data)
613
+ end
614
+ end
615
+
616
+ solution = TestSolution.new(T[-1, -1])
617
+ sim_annealing = Algorithms::SimulatedAnnealing.init(solution)
618
+ sim_annealing.run(1000)
619
+ sim_annealing.best_solution
620
+ ```
621
+
593
622
  # Other useful links
594
623
 
595
624
  * https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/
@@ -0,0 +1,43 @@
1
+ module Algorithms::SimulatedAnnealing
2
+ module Solution
3
+ abstract_method :score, :next_solution
4
+ attr_reader :data
5
+
6
+ def initialize(data)
7
+ @data = data
8
+ end
9
+ end
10
+
11
+ def self.init(solution)
12
+ Engine.new(solution)
13
+ end
14
+
15
+ class Engine
16
+ def initialize(solution)
17
+ @initial_solution = solution
18
+ @solutions_history = [@initial_solution]
19
+ end
20
+
21
+ def run(iterations)
22
+ iterations.times do |i|
23
+ temperature = iterations.to_f / i.to_f
24
+ current_solution = @solutions_history[-1]
25
+ next_solution = current_solution.next_solution
26
+ if acceptance_probability(current_solution.score, next_solution.score, temperature) > rand
27
+ @solutions_history << next_solution
28
+ end
29
+ end
30
+ end
31
+
32
+ def best_solution; @solutions_history[-1] end
33
+
34
+ private
35
+
36
+ def acceptance_probability(current_score, next_score, temperature)
37
+ if next_score > current_score
38
+ return 1.0
39
+ end
40
+ Math.exp((next_score - current_score) / temperature)
41
+ end
42
+ end
43
+ end
data/lib/victory.rb CHANGED
@@ -14,6 +14,7 @@ require 'algorithms/genetic_algorithm/solutions/bitset'
14
14
  require 'algorithms/genetic_algorithm/solutions/array'
15
15
  require 'algorithms/genetic_algorithm/solutions/permutation'
16
16
  require 'algorithms/genetic_algorithm/solutions/combination'
17
+ require 'algorithms/simulated_annealing'
17
18
  require 'containers/prefix_tree'
18
19
  require 'containers/heap'
19
20
  require 'containers/stack'
@@ -1,3 +1,3 @@
1
1
  module Victory
2
- VERSION = '0.4.1'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: victory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arnold Szederjesi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-17 00:00:00.000000000 Z
11
+ date: 2020-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -177,6 +177,7 @@ files:
177
177
  - lib/algorithms/genetic_algorithm/solutions/permutation.rb
178
178
  - lib/algorithms/greedy.rb
179
179
  - lib/algorithms/search.rb
180
+ - lib/algorithms/simulated_annealing.rb
180
181
  - lib/algorithms/sort.rb
181
182
  - lib/algorithms/string.rb
182
183
  - lib/containers/bitset.rb