Class: JGAP::Problem

Inherits:
Object
  • Object
show all
Defined in:
lib/JGAP/problem.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProblem

Returns a new instance of Problem.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/JGAP/problem.rb', line 77

def initialize
  @config = DefaultConfiguration.new
  @chromosome = nil
  @population_size = 512
  @population = nil
  @best_solution = nil
  @builder = ChromosomeBuilder.new(@config)
  
  chromosome
  population_size
end

Instance Attribute Details

#best_solutionObject (readonly)

Returns the value of attribute best_solution.



75
76
77
# File 'lib/JGAP/problem.rb', line 75

def best_solution
  @best_solution
end

Class Method Details

.chromosome(&block) ⇒ Object



137
138
139
140
141
142
# File 'lib/JGAP/problem.rb', line 137

def self.chromosome(&block)
  define_method(:chromosome) do
    @builder.instance_eval(&block)
    @chromosome = @builder.chromosome
  end
end

.fitness_function(&block) ⇒ Object



144
145
146
# File 'lib/JGAP/problem.rb', line 144

def self.fitness_function(&block)
  define_method(:evaluate, &block)
end

.population_size(size) ⇒ Object

MACROS



130
131
132
133
134
135
# File 'lib/JGAP/problem.rb', line 130

def self.population_size(size)
  define_method(:population_size) do
    @population_size = size
    @config.set_population_size(@population_size)
  end
end

Instance Method Details

#maximize(value) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/JGAP/problem.rb', line 120

def maximize(value)
  if value <= 0
    1/value.abs
  else
    value
  end
end

#minimize(value) ⇒ Object



116
117
118
# File 'lib/JGAP/problem.rb', line 116

def minimize(value)
  1.0/(1 + value.abs)
end


110
111
112
113
114
# File 'lib/JGAP/problem.rb', line 110

def print_best
  @builder.names.each do |k, v|
    puts "#{k}: #{read_best k}"
  end
end

#read(subject, name) ⇒ Object



102
103
104
# File 'lib/JGAP/problem.rb', line 102

def read(subject, name)
  @builder.read(subject, name)
end

#read_best(name) ⇒ Object



106
107
108
# File 'lib/JGAP/problem.rb', line 106

def read_best(name)
  read(best_solution, name)
end

#run(cycles = 1) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/JGAP/problem.rb', line 94

def run(cycles=1)
  @config.set_fitness_function(self)
  @config.set_sample_chromosome(@chromosome)
  @population = Genotype.random_initial_genotype(@config)
  @population.evolve(cycles)
  @best_solution = @population.get_fittest_chromosome
end

#setupObject



90
91
92
# File 'lib/JGAP/problem.rb', line 90

def setup
  # Override me!
end