Class: MHL::GeneticAlgorithmSolver
- Inherits:
-
Object
- Object
- MHL::GeneticAlgorithmSolver
- Defined in:
- lib/mhl/genetic_algorithm_solver.rb
Instance Method Summary collapse
-
#initialize(opts) ⇒ GeneticAlgorithmSolver
constructor
A new instance of GeneticAlgorithmSolver.
-
#solve(func) ⇒ Object
This is the method that solves the optimization problem.
Constructor Details
#initialize(opts) ⇒ GeneticAlgorithmSolver
Returns a new instance of GeneticAlgorithmSolver.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mhl/genetic_algorithm_solver.rb', line 11 def initialize(opts) @population_size = opts[:population_size].to_i unless @population_size and @population_size.even? raise ArgumentError, 'Even population size required!' end # perform genotype space-specific configuration case opts[:genotype_space_type] when :integer @genotype_space = IntegerVectorGenotypeSpace.new(opts[:genotype_space_conf]) begin p_m = opts[:mutation_probability].to_f @mutation_rv = \ ERV::RandomVariable.new(:distribution => :geometric, :probability_of_success => p_m) rescue raise ArgumentError, 'Mutation probability configuration is wrong.' end begin p_r = opts[:recombination_probability].to_f @recombination_rv = \ ERV::RandomVariable.new(:distribution => :uniform, :min_value => -p_r, :max_value => 1.0 + p_r) rescue raise ArgumentError, 'Recombination probability configuration is wrong.' end when :bitstring @genotype_space = BitstringGenotypeSpace.new(opts[:genotype_space_conf]) @recombination_rv = ERV::RandomVariable.new(:distribution => :uniform, :max_value => 1.0) @mutation_rv = ERV::RandomVariable.new(:distribution => :uniform, :max_value => 1.0) else raise ArgumentError, 'Only integer and bitstring genotype representations are supported!' end @exit_condition = opts[:exit_condition] @start_population = opts[:genotype_space_conf][:start_population] end |
Instance Method Details
#solve(func) ⇒ Object
This is the method that solves the optimization problem
Parameter func is supposed to be a method (or a Proc, a lambda, or any callable object) that accepts the genotype as argument (that is, the set of parameters) and returns the phenotype (that is, the function result)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mhl/genetic_algorithm_solver.rb', line 60 def solve(func) # setup population if @start_population.nil? population = Array.new(@population_size) do # generate random genotype according to the chromosome type { :genotype => @genotype_space.get_random } end else population = @start_population.map do |x| { :genotype => x } end end # initialize variables gen = 0 overall_best = nil # default behavior is to loop forever begin gen += 1 puts "Starting generation #{gen} at #{Time.now}" # assess fitness for every member of the population population.each do |s| s[:task] = Concurrent::Future.new { func.call(s[:genotype]) } end # wait for all the evaluations to end population.each do |s| s[:fitness] = s[:task].value end # find fittest member population_best = population.max_by {|x| x[:fitness] } # calculate overall best if overall_best.nil? overall_best = population_best else overall_best = [ overall_best, population_best ].max_by {|x| x[:fitness] } end # print results puts "> gen #{gen}, best: #{overall_best[:genotype]}, #{overall_best[:fitness]}" # selection by binary tournament children = new_generation(population) # update population and generation number population = children end while @exit_condition.nil? or !@exit_condition.call(gen, overall_best) end |