Class: FeldtRuby::Optimize::Optimizer
- Includes:
- Logging
- Defined in:
- lib/feldtruby/optimize/optimizer.rb
Overview
Find an vector of float values that optimizes a given objective.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#archive ⇒ Object
readonly
Returns the value of attribute archive.
-
#num_optimization_steps ⇒ Object
readonly
Returns the value of attribute num_optimization_steps.
-
#objective ⇒ Object
readonly
Returns the value of attribute objective.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#search_space ⇒ Object
readonly
Returns the value of attribute search_space.
-
#termination_criterion ⇒ Object
readonly
Returns the value of attribute termination_criterion.
Attributes included from Logging
Instance Method Summary collapse
- #best ⇒ Object
- #init_archive ⇒ Object
-
#initialize(objective, searchSpace = FeldtRuby::Optimize::DefaultSearchSpace, options = {}) ⇒ Optimizer
constructor
A new instance of Optimizer.
- #initialize_options(options) ⇒ Object
- #log_end_of_optimization ⇒ Object
-
#optimization_step ⇒ Object
Run one optimization step.
-
#optimize ⇒ Object
Optimize the objective in the given search space.
- #time_per_step ⇒ Object
-
#update_archive(candidates) ⇒ Object
Update the archive with newly found candidates.
Methods included from Logging
#__find_logger_set_on_instance_vars, #new_default_logger, #setup_logger_and_distribute_to_instance_variables
Constructor Details
#initialize(objective, searchSpace = FeldtRuby::Optimize::DefaultSearchSpace, options = {}) ⇒ Optimizer
Returns a new instance of Optimizer.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 20 def initialize(objective, searchSpace = FeldtRuby::Optimize::DefaultSearchSpace, = {}) @objective, @search_space = objective, searchSpace @options = FeldtRuby::Optimize.() init_archive # Must setup logger before setting options since verbosity of logger is # an option! setup_logger_and_distribute_to_instance_variables() (@options) end |
Instance Attribute Details
#archive ⇒ Object (readonly)
Returns the value of attribute archive.
17 18 19 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 17 def archive @archive end |
#num_optimization_steps ⇒ Object (readonly)
Returns the value of attribute num_optimization_steps.
18 19 20 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 18 def num_optimization_steps @num_optimization_steps end |
#objective ⇒ Object (readonly)
Returns the value of attribute objective.
17 18 19 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 17 def objective @objective end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 17 def @options end |
#search_space ⇒ Object (readonly)
Returns the value of attribute search_space.
17 18 19 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 17 def search_space @search_space end |
#termination_criterion ⇒ Object (readonly)
Returns the value of attribute termination_criterion.
18 19 20 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 18 def termination_criterion @termination_criterion end |
Instance Method Details
#best ⇒ Object
51 52 53 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 51 def best @archive.best end |
#init_archive ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 33 def init_archive if @options[:archive] @archive = @options[:archive] else if @options[:archiveDiversityObjective] diversity_objective = @options[:archiveDiversityObjective] else diversity_objective = @options[:archiveDiversityObjectiveClass].new end @archive = @options[:archiveClass].new(@objective, diversity_objective) end end |
#initialize_options(options) ⇒ Object
55 56 57 58 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 55 def () self.logger.verbose = [:verbose] @termination_criterion = [:terminationCriterion] end |
#log_end_of_optimization ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 90 def log_end_of_optimization logger.log("End of optimization\n" + " Optimizer: #{self.class}\n" + " Best found: #{@archive.best}\n" + " Quality of best: #{@objective.quality_of(@archive.best)}\n" + " Time used = #{Time.human_readable_timestr(logger.elapsed_time)}, " + "Steps performed = #{@num_optimization_steps}, " + "#{Time.human_readable_timestr(time_per_step, true)}/step") end |
#optimization_step ⇒ Object
Run one optimization step. Default is to do nothing, i.e. this is just a superclass, but subclasses need to implement this.
106 107 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 106 def optimization_step() end |
#optimize ⇒ Object
Optimize the objective in the given search space.
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 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 61 def optimize() logger.log "Optimization with optimizer #{self.class.inspect} started" @num_optimization_steps = 0 # Set up a random best for now that we can later compare to. update_archive [search_space.gen_candidate()] begin while !termination_criterion.terminate?(self) new_candidates = optimization_step() @num_optimization_steps += 1 update_archive new_candidates end rescue Exception => e puts e.inspect logger.log_data :exception, { :exception_class => e.class.inspect, :backtrace => e.backtrace.join("\n") }, "!!! - Optimization FAILED with exception: #{e.} - !!!" + e.backtrace.join("\n") ensure logger.log "!!! - Optimization FINISHED after #{@num_optimization_steps} steps - !!!" end @objective.note_end_of_optimization(self) log_end_of_optimization archive.best # return the best end |
#time_per_step ⇒ Object
100 101 102 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 100 def time_per_step logger.elapsed_time / @num_optimization_steps end |
#update_archive(candidates) ⇒ Object
Update the archive with newly found candidates. Array of candidates may be empty if no new candidates found.
111 112 113 |
# File 'lib/feldtruby/optimize/optimizer.rb', line 111 def update_archive(candidates) candidates.each {|c| @archive.add(c)} end |