Class: FeldtRuby::Optimize::Optimizer

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/feldtruby/optimize/optimizer.rb

Overview

Find an vector of float values that optimizes a given objective.

Direct Known Subclasses

PopulationBasedOptimizer, RandomSearcher

Instance Attribute Summary collapse

Attributes included from Logging

#logger

Instance Method Summary collapse

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, options = {})
	@objective, @search_space = objective, searchSpace
	@options = FeldtRuby::Optimize.override_default_options_with(options)

	init_archive

	# Must setup logger before setting options since verbosity of logger is
	# an option!
	setup_logger_and_distribute_to_instance_variables(options)

	initialize_options(@options)
end

Instance Attribute Details

#archiveObject (readonly)

Returns the value of attribute archive.



17
18
19
# File 'lib/feldtruby/optimize/optimizer.rb', line 17

def archive
  @archive
end

#num_optimization_stepsObject (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

#objectiveObject (readonly)

Returns the value of attribute objective.



17
18
19
# File 'lib/feldtruby/optimize/optimizer.rb', line 17

def objective
  @objective
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/feldtruby/optimize/optimizer.rb', line 17

def options
  @options
end

#search_spaceObject (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_criterionObject (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

#bestObject



51
52
53
# File 'lib/feldtruby/optimize/optimizer.rb', line 51

def best
	@archive.best
end

#init_archiveObject



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 initialize_options(options)
	self.logger.verbose = options[:verbose]
	@termination_criterion = options[:terminationCriterion]
end

#log_end_of_optimizationObject



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_stepObject

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

#optimizeObject

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.message} - !!!" + 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_stepObject



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