Class: Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/gimuby/factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFactory

Returns a new instance of Factory.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gimuby/factory.rb', line 27

def initialize
  @optimal_population = FALSE
  @optimal_archipelago = FALSE

  # use for test purpose to avoid provided archipelago to be connected
  @connected_archipelago = TRUE

  # those values are set by the user, let them nil and we won't use them
  @solutions_number = nil
  @populations_number = nil

  # if we have to generate values we store them here
  @_solutions_number = nil
  @_populations_number = nil
end

Instance Attribute Details

#connected_archipelagoObject

Returns the value of attribute connected_archipelago


46
47
48
# File 'lib/gimuby/factory.rb', line 46

def connected_archipelago
  @connected_archipelago
end

#optimal_archipelagoObject

Returns the value of attribute optimal_archipelago


44
45
46
# File 'lib/gimuby/factory.rb', line 44

def optimal_archipelago
  @optimal_archipelago
end

#optimal_populationObject

Returns the value of attribute optimal_population


43
44
45
# File 'lib/gimuby/factory.rb', line 43

def optimal_population
  @optimal_population
end

#populations_numberObject

Returns the value of attribute populations_number


49
50
51
# File 'lib/gimuby/factory.rb', line 49

def populations_number
  @populations_number
end

#solutions_numberObject

Returns the value of attribute solutions_number


48
49
50
# File 'lib/gimuby/factory.rb', line 48

def solutions_number
  @solutions_number
end

Instance Method Details

#get_archipelago(&solution_generator) ⇒ Object


61
62
63
64
65
66
# File 'lib/gimuby/factory.rb', line 61

def get_archipelago(&solution_generator)
  if @optimal_archipelago
    return get_optimal_archipelago(&solution_generator)
  end
  get_random_archipelago(&solution_generator)
end

#get_optimal_archipelago(&solution_generator) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gimuby/factory.rb', line 88

def get_optimal_archipelago(&solution_generator)
  is_optimal = FALSE
  begin
    candidate_archipelago = get_random_archipelago(&solution_generator)
    is_optimal = TRUE
    if (candidate_archipelago.get_population_size <= 500) or
        (candidate_archipelago.get_population_size >= 1100)
      is_optimal = FALSE
    end
    if candidate_archipelago.migration_rate <= 0.25
      is_optimal = FALSE
    end
    if (candidate_archipelago.populations.length < 10) or
        (candidate_archipelago.populations.length > 29)
      is_optimal = FALSE
    end
    if candidate_archipelago.get_diameter >= 100
      is_optimal = FALSE
    end
  end until is_optimal
  candidate_archipelago
end

#get_optimal_population(optimizer, &solution_generator) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gimuby/factory.rb', line 68

def get_optimal_population(optimizer, &solution_generator)
  candidate_population = get_random_population(optimizer, &solution_generator)
  begin
    candidate_population = get_random_population(optimizer, &solution_generator)
    if candidate_population.solutions.length >= 1000
      next
    end
    if candidate_population.solutions[0].mutation_strategy.mutation_rate >= 0.1
      next
    end
    if candidate_population.replace_strategy.replace_proportion <= 0.4
      next
    end
    if candidate_population.pick_strategy.pick_proportion >= 0.4
      next
    end
  end while FALSE
  candidate_population
end

#get_population(&solution_generator) ⇒ Object


57
58
59
# File 'lib/gimuby/factory.rb', line 57

def get_population(&solution_generator)
  _get_population(:population, &solution_generator)
end

#get_random_archipelago(&solution_generator) ⇒ Object


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gimuby/factory.rb', line 131

def get_random_archipelago(&solution_generator)
  archipelago = Archipelago.new
  archipelago.connect_strategy = get_random_connect_strategy
  # between 0.01% and ~35%
  archipelago.migration_rate = (rand() * 35.0 / 100.0) + 0.0001
  archipelago.migration_symmetric = random_entry(true, false)
  archipelago.migration_type = random_entry(:random, :synchronized, :fixed_time)
  get_populations_number.times do |_|
    archipelago.add_population(_get_population(:archipelago, &solution_generator))
  end
  if @connected_archipelago
    archipelago.connect_all
  end
  archipelago
end

#get_random_population(optimizer, &solution_generator) ⇒ Object


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gimuby/factory.rb', line 111

def get_random_population(optimizer, &solution_generator)
  population = Population.new
  population.pick_strategy = get_random_pick_strategy
  population.replace_strategy = get_random_replace_strategy
  populations_number = get_populations_number
  unless optimizer == :archipelago
    populations_number = 1
  end
  solutions_number = get_solutions_number(populations_number)
  number_solutions_per_population = solutions_number / populations_number
  mutation_rate = rand * 0.7
  number_solutions_per_population.times do
    solution = solution_generator.call
    solution.mutation_strategy.mutation_rate = mutation_rate
    population.add_solution(solution)
  end

  population
end

#reset_stateObject


51
52
53
54
55
# File 'lib/gimuby/factory.rb', line 51

def reset_state
  @connected_archipelago = TRUE
  @_populations_number = nil
  @_solutions_number = nil
end