ruby-jgap 0.0.1-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/lib/.DS_Store +0 -0
- data/lib/JGAP/problem.rb +140 -0
- data/lib/java/jgap.jar +0 -0
- data/lib/ruby-jgap.rb +7 -0
- data/ruby-jgap.gemspec +18 -0
- data/test.rb +39 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: db26a50b8deeb21c693163a8fab9578fb2c37990
|
4
|
+
data.tar.gz: 552e7b2836d796b8d8baca0f7dbfb949ea6d77ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f423e8b532b9bc6612129c9922ab6e020fc50b9e1892d75c17eda072d41c6d1eb25231f638546f291f8dc07f3e38896cc84453925b773211abc2b2a69fbd149d
|
7
|
+
data.tar.gz: 2a86fe2c6293ea8167313676ccc3cdeba541675bcb4e3145cdc94969159a6975a3a4ec2abe4dcd2232c036bccf2d2ab13797acc569bdeba5c1af3dc43cb3d4f8
|
data/.DS_Store
ADDED
Binary file
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 William Chen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# ruby-jgap
|
2
|
+
|
3
|
+
A Ruby DSL for Genetic Algorithms using JGAP.
|
4
|
+
|
5
|
+
The JGAP binary JAR file is packaged with this gem.
|
6
|
+
|
7
|
+
JGAP is licensed under the GNU Lesser Public license; if you would like to use this gem in commercial applications, you can choose to use the Mozilla Public License and donate 50 euros to JGAP. [more details](http://jgap.sourceforge.net/#documentation)
|
8
|
+
|
9
|
+
## Install
|
10
|
+
|
11
|
+
gem install ruby-jgap
|
12
|
+
|
13
|
+
### Usage
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
|
17
|
+
require 'java'
|
18
|
+
require 'ruby-jgap'
|
19
|
+
|
20
|
+
# subclass JGAP::Problem
|
21
|
+
class MakeChangeProblem < JGAP::Problem
|
22
|
+
|
23
|
+
population_size 500
|
24
|
+
|
25
|
+
# define our solution chromosome
|
26
|
+
chromosome do
|
27
|
+
integer :quarters, min: 0, max: 3
|
28
|
+
integer :dimes, min: 0, max: 2
|
29
|
+
integer :nickels, min: 0, max: 1
|
30
|
+
integer :pennies, min: 0, max: 4
|
31
|
+
end
|
32
|
+
|
33
|
+
# define our fitness function
|
34
|
+
fitness_function do |subject|
|
35
|
+
target = 47 # goal: 47 cents
|
36
|
+
q = read subject, :quarters
|
37
|
+
d = read subject, :dimes
|
38
|
+
n = read subject, :nickels
|
39
|
+
p = read subject, :pennies
|
40
|
+
|
41
|
+
coins = q + d + n + p
|
42
|
+
value = 25*q + 10*d + 5*n + p
|
43
|
+
delta = (target - value).abs # how far are we from our goal?
|
44
|
+
|
45
|
+
fitness = (99 - delta)
|
46
|
+
fitness += 100 - (10*coins) if value == target # reward if matches with goal
|
47
|
+
return fitness
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
problem = MakeChangeProblem.new
|
54
|
+
problem.run(100) # 100 generations
|
55
|
+
problem.print_best
|
56
|
+
|
57
|
+
```
|
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/JGAP/problem.rb
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'java/jgap.jar'
|
3
|
+
|
4
|
+
java_import %w(
|
5
|
+
org.jgap.FitnessFunction
|
6
|
+
org.jgap.impl.DefaultConfiguration
|
7
|
+
org.jgap.impl.IntegerGene
|
8
|
+
org.jgap.impl.BooleanGene
|
9
|
+
org.jgap.impl.MapGene
|
10
|
+
org.jgap.impl.StringGene
|
11
|
+
org.jgap.impl.DoubleGene
|
12
|
+
org.jgap.Chromosome
|
13
|
+
org.jgap.Genotype
|
14
|
+
)
|
15
|
+
|
16
|
+
module JGAP
|
17
|
+
|
18
|
+
class ChromosomeBuilder
|
19
|
+
|
20
|
+
attr_reader :names
|
21
|
+
attr_reader :genes
|
22
|
+
|
23
|
+
def initialize(config)
|
24
|
+
@config = config
|
25
|
+
@names = Hash.new
|
26
|
+
@genes = Array.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def chromosome
|
30
|
+
Chromosome.new(@config, @genes.to_java(Java::OrgJgap::Gene))
|
31
|
+
end
|
32
|
+
|
33
|
+
def read(subject, name)
|
34
|
+
subject.gene(@names[name]).allele
|
35
|
+
end
|
36
|
+
|
37
|
+
def integer(name, opts={})
|
38
|
+
@names[name] = @genes.length
|
39
|
+
if opts
|
40
|
+
@genes << IntegerGene.new(@config, opts[:min], opts[:max])
|
41
|
+
else
|
42
|
+
@genes << IntegerGene.new(@config)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def decimal(name, opts={})
|
47
|
+
@names[name] = @genes.length
|
48
|
+
if opts
|
49
|
+
@genes << DoubleGene.new(@config, opts[:min], opts[:max])
|
50
|
+
else
|
51
|
+
@genes << DoubleGene.new(@config)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def boolean(name)
|
56
|
+
@names[name] = @genes.length
|
57
|
+
@genes << BooleanGene.new(@config)
|
58
|
+
end
|
59
|
+
|
60
|
+
def string(name, opts={})
|
61
|
+
@names[name] = @genes.length
|
62
|
+
if opts && opts[:alphabet]
|
63
|
+
@genes << StringGene.new(@config,
|
64
|
+
opts[:min], opts[:max], opts[:alphabet])
|
65
|
+
elsif opts
|
66
|
+
@genes << StringGene.new(@config, opts[:min], opts[:max])
|
67
|
+
else
|
68
|
+
@genes << StringGene.new(@config)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
####
|
76
|
+
|
77
|
+
class Problem < FitnessFunction
|
78
|
+
|
79
|
+
attr_reader :best_solution
|
80
|
+
|
81
|
+
def initialize
|
82
|
+
@config = DefaultConfiguration.new
|
83
|
+
@chromosome = nil
|
84
|
+
@population_size = 0
|
85
|
+
@population = nil
|
86
|
+
@best_solution = nil
|
87
|
+
@builder = ChromosomeBuilder.new(@config)
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
def setup
|
92
|
+
# Override me!
|
93
|
+
end
|
94
|
+
|
95
|
+
def run(cycles=1)
|
96
|
+
chromosome
|
97
|
+
population_size
|
98
|
+
@config.set_fitness_function(self)
|
99
|
+
@config.set_sample_chromosome(@chromosome)
|
100
|
+
@config.set_population_size(@population_size)
|
101
|
+
@population = Genotype.random_initial_genotype(@config)
|
102
|
+
@population.evolve(cycles)
|
103
|
+
@best_solution = @population.get_fittest_chromosome
|
104
|
+
end
|
105
|
+
|
106
|
+
def read(subject, name)
|
107
|
+
@builder.read(subject, name)
|
108
|
+
end
|
109
|
+
|
110
|
+
def read_best(name)
|
111
|
+
read(best_solution, name)
|
112
|
+
end
|
113
|
+
|
114
|
+
def print_best
|
115
|
+
@builder.names.each do |k, v|
|
116
|
+
puts "#{k}: #{read_best k}"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
## MACROS
|
121
|
+
|
122
|
+
def self.population_size(size)
|
123
|
+
define_method(:population_size) do
|
124
|
+
@population_size = size
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.chromosome(&block)
|
129
|
+
define_method(:chromosome) do
|
130
|
+
@builder.instance_eval(&block)
|
131
|
+
@chromosome = @builder.chromosome
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.fitness_function(&block)
|
136
|
+
define_method(:evaluate, &block)
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
end
|
data/lib/java/jgap.jar
ADDED
Binary file
|
data/lib/ruby-jgap.rb
ADDED
data/ruby-jgap.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ruby-jgap"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.platform = "java"
|
9
|
+
spec.authors = ["William Chen"]
|
10
|
+
spec.email = ["wchen298@gmail.com"]
|
11
|
+
spec.summary = %q{Ruby DSL for Genetic Algorithms using JGAP as a backend.}
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
end
|
data/test.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'ruby-jgap'
|
3
|
+
|
4
|
+
# subclass JGAP::Problem
|
5
|
+
class MakeChangeProblem < JGAP::Problem
|
6
|
+
|
7
|
+
population_size 500
|
8
|
+
|
9
|
+
# define our solution chromosome
|
10
|
+
chromosome do
|
11
|
+
integer :quarters, min: 0, max: 3
|
12
|
+
integer :dimes, min: 0, max: 2
|
13
|
+
integer :nickels, min: 0, max: 1
|
14
|
+
integer :pennies, min: 0, max: 4
|
15
|
+
end
|
16
|
+
|
17
|
+
# define our fitness function
|
18
|
+
fitness_function do |subject|
|
19
|
+
target = 47 # goal: 47 cents
|
20
|
+
q = read subject, :quarters
|
21
|
+
d = read subject, :dimes
|
22
|
+
n = read subject, :nickels
|
23
|
+
p = read subject, :pennies
|
24
|
+
|
25
|
+
coins = q + d + n + p
|
26
|
+
value = 25*q + 10*d + 5*n + p
|
27
|
+
delta = (target - value).abs # how far are we from our goal?
|
28
|
+
|
29
|
+
fitness = (99 - delta)
|
30
|
+
fitness += 100 - (10*coins) if value == target # reward if matches with goal
|
31
|
+
return fitness
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
problem = MakeChangeProblem.new
|
38
|
+
problem.run(100) # 100 generations
|
39
|
+
problem.print_best
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-jgap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: java
|
6
|
+
authors:
|
7
|
+
- William Chen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- wchen298@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".DS_Store"
|
21
|
+
- LICENSE.txt
|
22
|
+
- README.md
|
23
|
+
- lib/.DS_Store
|
24
|
+
- lib/JGAP/problem.rb
|
25
|
+
- lib/java/jgap.jar
|
26
|
+
- lib/ruby-jgap.rb
|
27
|
+
- ruby-jgap.gemspec
|
28
|
+
- test.rb
|
29
|
+
homepage:
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.4.5
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Ruby DSL for Genetic Algorithms using JGAP as a backend.
|
53
|
+
test_files: []
|