ruby-jgap 0.0.1-java → 0.0.2-java
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 +4 -4
- data/README.md +16 -5
- data/lib/JGAP/problem.rb +17 -12
- data/lib/JGAP/salesman.rb +86 -0
- data/lib/ruby-jgap.rb +1 -1
- data/ruby-jgap.gemspec +1 -1
- data/salesman_test.rb +42 -0
- data/test.rb +4 -4
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da1f5d18e0a46faaca1b2436d0c25b5017d15f2b
|
4
|
+
data.tar.gz: 7689a9b162d30599dcae4f29fda432135b3d1261
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d715b18d2df55f9415f7dc2bfbc93e52dc9012e579c403b2ed7db67b1341b3072236eece58ad960ebc8be509ff47987797bbdcb61dbac4e268241d93e29e259
|
7
|
+
data.tar.gz: e2059b74fd96d9584d239b8d9ac410e43e74f69ca915bcaf794d5b23565152277a38b58a519ea70271ba78af6d6a7f15e1aa99678c3a4a9da7c8fe376505fa92
|
data/README.md
CHANGED
@@ -10,16 +10,18 @@ JGAP is licensed under the GNU Lesser Public license; if you would like to use t
|
|
10
10
|
|
11
11
|
gem install ruby-jgap
|
12
12
|
|
13
|
+
or
|
14
|
+
|
15
|
+
jruby -S gem install ruby-jgap
|
16
|
+
|
13
17
|
### Usage
|
14
18
|
|
15
19
|
```ruby
|
16
|
-
|
17
20
|
require 'java'
|
18
21
|
require 'ruby-jgap'
|
19
22
|
|
20
23
|
# subclass JGAP::Problem
|
21
24
|
class MakeChangeProblem < JGAP::Problem
|
22
|
-
|
23
25
|
population_size 500
|
24
26
|
|
25
27
|
# define our solution chromosome
|
@@ -46,12 +48,21 @@ class MakeChangeProblem < JGAP::Problem
|
|
46
48
|
fitness += 100 - (10*coins) if value == target # reward if matches with goal
|
47
49
|
return fitness
|
48
50
|
end
|
49
|
-
|
50
|
-
|
51
51
|
end
|
52
52
|
|
53
53
|
problem = MakeChangeProblem.new
|
54
54
|
problem.run(100) # 100 generations
|
55
55
|
problem.print_best
|
56
56
|
|
57
|
-
```
|
57
|
+
```
|
58
|
+
|
59
|
+
This outputs (probably):
|
60
|
+
|
61
|
+
```
|
62
|
+
quarters: 1
|
63
|
+
dimes: 2
|
64
|
+
nickels: 0
|
65
|
+
pennies: 2
|
66
|
+
```
|
67
|
+
|
68
|
+
Adapted from the [Getting Started](http://jgap.sourceforge.net/doc/tutorial.html) page on JGAP's site.
|
data/lib/JGAP/problem.rb
CHANGED
@@ -1,8 +1,4 @@
|
|
1
|
-
require 'java'
|
2
|
-
require 'java/jgap.jar'
|
3
|
-
|
4
1
|
java_import %w(
|
5
|
-
org.jgap.FitnessFunction
|
6
2
|
org.jgap.impl.DefaultConfiguration
|
7
3
|
org.jgap.impl.IntegerGene
|
8
4
|
org.jgap.impl.BooleanGene
|
@@ -34,9 +30,16 @@ module JGAP
|
|
34
30
|
subject.gene(@names[name]).allele
|
35
31
|
end
|
36
32
|
|
33
|
+
def salesman(name, cities)
|
34
|
+
new_gene = IntegerGene.new(@config, 0, cities - 1)
|
35
|
+
new_gene.set_allele java.lang.Integer.new(@genes.length)
|
36
|
+
@names[name] = @genes.length
|
37
|
+
@genes << new_gene
|
38
|
+
end
|
39
|
+
|
37
40
|
def integer(name, opts={})
|
38
41
|
@names[name] = @genes.length
|
39
|
-
|
42
|
+
unless opts.empty?
|
40
43
|
@genes << IntegerGene.new(@config, opts[:min], opts[:max])
|
41
44
|
else
|
42
45
|
@genes << IntegerGene.new(@config)
|
@@ -45,7 +48,7 @@ module JGAP
|
|
45
48
|
|
46
49
|
def decimal(name, opts={})
|
47
50
|
@names[name] = @genes.length
|
48
|
-
|
51
|
+
unless opts.empty?
|
49
52
|
@genes << DoubleGene.new(@config, opts[:min], opts[:max])
|
50
53
|
else
|
51
54
|
@genes << DoubleGene.new(@config)
|
@@ -59,7 +62,7 @@ module JGAP
|
|
59
62
|
|
60
63
|
def string(name, opts={})
|
61
64
|
@names[name] = @genes.length
|
62
|
-
if opts && opts[:alphabet]
|
65
|
+
if !opts.empty? && opts[:alphabet]
|
63
66
|
@genes << StringGene.new(@config,
|
64
67
|
opts[:min], opts[:max], opts[:alphabet])
|
65
68
|
elsif opts
|
@@ -74,17 +77,20 @@ module JGAP
|
|
74
77
|
|
75
78
|
####
|
76
79
|
|
77
|
-
class Problem < FitnessFunction
|
80
|
+
class Problem < org.jgap.FitnessFunction
|
78
81
|
|
79
82
|
attr_reader :best_solution
|
80
83
|
|
81
84
|
def initialize
|
82
85
|
@config = DefaultConfiguration.new
|
83
86
|
@chromosome = nil
|
84
|
-
@population_size =
|
87
|
+
@population_size = 512
|
85
88
|
@population = nil
|
86
89
|
@best_solution = nil
|
87
90
|
@builder = ChromosomeBuilder.new(@config)
|
91
|
+
|
92
|
+
chromosome
|
93
|
+
population_size
|
88
94
|
end
|
89
95
|
|
90
96
|
|
@@ -93,11 +99,8 @@ module JGAP
|
|
93
99
|
end
|
94
100
|
|
95
101
|
def run(cycles=1)
|
96
|
-
chromosome
|
97
|
-
population_size
|
98
102
|
@config.set_fitness_function(self)
|
99
103
|
@config.set_sample_chromosome(@chromosome)
|
100
|
-
@config.set_population_size(@population_size)
|
101
104
|
@population = Genotype.random_initial_genotype(@config)
|
102
105
|
@population.evolve(cycles)
|
103
106
|
@best_solution = @population.get_fittest_chromosome
|
@@ -122,6 +125,7 @@ module JGAP
|
|
122
125
|
def self.population_size(size)
|
123
126
|
define_method(:population_size) do
|
124
127
|
@population_size = size
|
128
|
+
@config.set_population_size(@population_size)
|
125
129
|
end
|
126
130
|
end
|
127
131
|
|
@@ -137,4 +141,5 @@ module JGAP
|
|
137
141
|
end
|
138
142
|
|
139
143
|
end
|
144
|
+
|
140
145
|
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
java_import %w(
|
2
|
+
org.jgap.impl.DefaultConfiguration
|
3
|
+
org.jgap.impl.IntegerGene
|
4
|
+
org.jgap.impl.BooleanGene
|
5
|
+
org.jgap.impl.MapGene
|
6
|
+
org.jgap.impl.StringGene
|
7
|
+
org.jgap.impl.DoubleGene
|
8
|
+
org.jgap.Chromosome
|
9
|
+
org.jgap.Genotype
|
10
|
+
org.jgap.Configuration
|
11
|
+
)
|
12
|
+
|
13
|
+
module JGAP
|
14
|
+
|
15
|
+
class Salesman < org.jgap.impl.salesman.Salesman
|
16
|
+
attr_reader :best_solution
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
super
|
20
|
+
@config = create_configuration(nil)
|
21
|
+
@chromosome = nil
|
22
|
+
@population_size = 512
|
23
|
+
@builder = ChromosomeBuilder.new(@config)
|
24
|
+
@best_solution = nil
|
25
|
+
chromosome
|
26
|
+
population_size
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def setup
|
31
|
+
# Override me!
|
32
|
+
end
|
33
|
+
|
34
|
+
def best_solution
|
35
|
+
@best_solution
|
36
|
+
end
|
37
|
+
|
38
|
+
def run
|
39
|
+
Configuration.reset
|
40
|
+
@best_solution = find_optimal_path(nil)
|
41
|
+
end
|
42
|
+
|
43
|
+
def read(subject, name)
|
44
|
+
@builder.read(subject, name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def read_best(name)
|
48
|
+
read(best_solution, name)
|
49
|
+
end
|
50
|
+
|
51
|
+
def print_best
|
52
|
+
@builder.names.each do |k, v|
|
53
|
+
puts "#{k}: #{read_best k}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def distance(from, to)
|
58
|
+
# Override me!
|
59
|
+
end
|
60
|
+
|
61
|
+
## MACROS
|
62
|
+
|
63
|
+
def self.population_size(size)
|
64
|
+
define_method(:population_size) do
|
65
|
+
@population_size = size
|
66
|
+
@config.set_population_size(@population_size)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.chromosome(&block)
|
71
|
+
define_method(:chromosome) do
|
72
|
+
@builder.instance_eval(&block)
|
73
|
+
@chromosome = @builder.chromosome
|
74
|
+
end
|
75
|
+
|
76
|
+
define_method(:createSampleChromosome) do |init_data|
|
77
|
+
@chromosome
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.fitness_function(&block)
|
82
|
+
define_method(:evaluate, &block)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
data/lib/ruby-jgap.rb
CHANGED
data/ruby-jgap.gemspec
CHANGED
data/salesman_test.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'ruby-jgap'
|
2
|
+
|
3
|
+
$table = [
|
4
|
+
[0, 0],
|
5
|
+
[-1, 1],
|
6
|
+
[-7, -1],
|
7
|
+
[-8, -2],
|
8
|
+
[2, -1],
|
9
|
+
[10, 2],
|
10
|
+
[7, -1]
|
11
|
+
]
|
12
|
+
|
13
|
+
class TSP < JGAP::Salesman
|
14
|
+
|
15
|
+
population_size 100000
|
16
|
+
|
17
|
+
# define our solution chromosome
|
18
|
+
chromosome do
|
19
|
+
salesman :paris, 7
|
20
|
+
salesman :london, 7
|
21
|
+
salesman :new_york, 7
|
22
|
+
salesman :boston, 7
|
23
|
+
salesman :berlin, 7
|
24
|
+
salesman :tokyo, 7
|
25
|
+
salesman :shanghai, 7
|
26
|
+
end
|
27
|
+
|
28
|
+
def distance(from, to)
|
29
|
+
f = from.allele
|
30
|
+
t = to.allele
|
31
|
+
x1, y1 = $table[f]
|
32
|
+
x2, y2 = $table[t]
|
33
|
+
|
34
|
+
Math.sqrt((x1 - x2)**2 + (y1-y2)**2)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
tsp = TSP.new
|
40
|
+
tsp.run
|
41
|
+
tsp.print_best
|
42
|
+
p tsp.best_solution.fitness_value
|
data/test.rb
CHANGED
@@ -9,9 +9,9 @@ class MakeChangeProblem < JGAP::Problem
|
|
9
9
|
# define our solution chromosome
|
10
10
|
chromosome do
|
11
11
|
integer :quarters, min: 0, max: 3
|
12
|
-
integer :dimes,
|
13
|
-
integer :nickels,
|
14
|
-
integer :pennies,
|
12
|
+
integer :dimes, min: 0, max: 2
|
13
|
+
integer :nickels, min: 0, max: 1
|
14
|
+
integer :pennies, min: 0, max: 4
|
15
15
|
end
|
16
16
|
|
17
17
|
# define our fitness function
|
@@ -36,4 +36,4 @@ end
|
|
36
36
|
|
37
37
|
problem = MakeChangeProblem.new
|
38
38
|
problem.run(100) # 100 generations
|
39
|
-
problem.print_best
|
39
|
+
problem.print_best
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-jgap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- William Chen
|
@@ -22,9 +22,11 @@ files:
|
|
22
22
|
- README.md
|
23
23
|
- lib/.DS_Store
|
24
24
|
- lib/JGAP/problem.rb
|
25
|
+
- lib/JGAP/salesman.rb
|
25
26
|
- lib/java/jgap.jar
|
26
27
|
- lib/ruby-jgap.rb
|
27
28
|
- ruby-jgap.gemspec
|
29
|
+
- salesman_test.rb
|
28
30
|
- test.rb
|
29
31
|
homepage:
|
30
32
|
licenses:
|