-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rb
37 lines (28 loc) · 819 Bytes
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'java'
require 'ruby-jgap'
# subclass JGAP::Problem
class MakeChangeProblem < JGAP::Problem
population_size 500
# define our solution chromosome
chromosome do
integer :quarters, min: 0, max: 3
integer :dimes, min: 0, max: 2
integer :nickels, min: 0, max: 1
integer :pennies, min: 0, max: 4
end
# define our fitness function
fitness_function do |subject|
target = 47 # goal: 47 cents
q = read subject, :quarters
d = read subject, :dimes
n = read subject, :nickels
p = read subject, :pennies
coins = q + d + n + p
value = 25*q + 10*d + 5*n + p
delta = (target - value).abs # how far are we from our goal?
minimize(coins + 2*delta)
end
end
problem = MakeChangeProblem.new
problem.run(100) # 100 generations
problem.print_best