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.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/JGAP/problem.rb', line 84

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.



82
83
84
# File 'lib/JGAP/problem.rb', line 82

def best_solution
  @best_solution
end

Class Method Details

.chromosome(&block) ⇒ Object



132
133
134
135
136
137
# File 'lib/JGAP/problem.rb', line 132

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

.fitness_function(&block) ⇒ Object



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

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

.population_size(size) ⇒ Object

MACROS



125
126
127
128
129
130
# File 'lib/JGAP/problem.rb', line 125

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

Instance Method Details



117
118
119
120
121
# File 'lib/JGAP/problem.rb', line 117

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

#read(subject, name) ⇒ Object



109
110
111
# File 'lib/JGAP/problem.rb', line 109

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

#read_best(name) ⇒ Object



113
114
115
# File 'lib/JGAP/problem.rb', line 113

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

#run(cycles = 1) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/JGAP/problem.rb', line 101

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



97
98
99
# File 'lib/JGAP/problem.rb', line 97

def setup
  # Override me!
end