Class: Biopsy::ObjectiveHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/biopsy/objective_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ ObjectiveHandler

Returns a new instance of ObjectiveHandler.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/biopsy/objective_handler.rb', line 46

def initialize(target)
  @target = target
  @objectives_dir = Settings.instance.objectives_dir.first
  @objectives = {}
  $LOAD_PATH.unshift(@objectives_dir)
  if Settings.instance.respond_to?(:objectives_subset)
    @subset = Settings.instance.objectives_subset
  else
    @subset = nil
  end
  load_objectives
  # pass objective list back to caller
  @objectives.keys
end

Instance Attribute Details

#last_tempdirObject (readonly)

Returns the value of attribute last_tempdir.



43
44
45
# File 'lib/biopsy/objective_handler.rb', line 43

def last_tempdir
  @last_tempdir
end

#objectivesObject

Returns the value of attribute objectives.



44
45
46
# File 'lib/biopsy/objective_handler.rb', line 44

def objectives
  @objectives
end

Instance Method Details

#dimension_reduce(results) ⇒ Object

Perform a euclidean distance dimension reduction of multiple objectives



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/biopsy/objective_handler.rb', line 99

def dimension_reduce(results)
  # calculate the weighted Euclidean distance from optimal
  # d(p, q) = \sqrt{(p_1 - q_1)^2 + (p_2 - q_2)^2+...+(p_n - q_n)^2}
  # here the max value is sqrt(n) where n is no. of results,
  #   min value (optimum) is 0
  total = 0
  results.each_value do |value|
    o = value[:optimum]
    w = value[:weighting]
    a = value[:result]
    m = value[:max]
    total += w * (((o - a) / m)**2) if m != 0
  end
  Math.sqrt(total) / results.length
end

#load_objectivesObject



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
# File 'lib/biopsy/objective_handler.rb', line 61

def load_objectives
  # load objectives
  # load subset list if available
  subset_file = @objectives_dir + '/objectives.txt'
  if File.exist?(subset_file)
    subset = File.open(subset_file).readlines.map { |l| l.strip }
  else
    subset = nil
  end
  subset = @subset if subset.nil?
  # parse in objectives
  Dir.chdir @objectives_dir do
    Dir['*.rb'].each do |f|
      file_name = File.basename(f, '.rb')
      require file_name
      objective_name = file_name.camelize
      objective =  Module.const_get(objective_name).new
      if subset.nil? || subset.include?(file_name)
        # this objective is included
        @objectives[objective_name] = objective
      end
    end
    # puts "loaded #{@objectives.length} objectives."
  end
end

#run_for_output(raw_output, threads, allresults) ⇒ Object

Run all objectives functions for :output.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/biopsy/objective_handler.rb', line 116

def run_for_output(raw_output, threads, allresults)
  # check output files exist
  output_files = {}
  @target.output.each_pair do |key, glob|
    files = Dir[glob]
    size = files.reduce(1) { |sum, f| sum * File.size(f)}
    if files.empty? || size==0
      error = "output files for #{key} matching #{glob} do not exist" +
              " or are empty"
      raise ObjectiveHandlerError, error
    end
    output_files[key] = files.map { |f| File.expand_path(f) }
  end

  # run all objectives for output
  results = {}
  @objectives.each_pair do |name, objective|
    results[name] = self.run_objective(objective, name, raw_output,
                                       output_files, threads)
  end

  if allresults
    return { :results => results,
             :reduced => self.dimension_reduce(results) }
  else
    results.each_value do |value|
      return value.kind_of?(Hash) ? value[:result] : value
    end
  end
end

#run_objective(objective, _name, raw_output, output_files, threads) ⇒ Object

Run a specific :objective on the :output of a target with max :threads.



89
90
91
92
93
94
95
96
# File 'lib/biopsy/objective_handler.rb', line 89

def run_objective(objective, _name, raw_output, output_files, threads)
  # output is a, array: [raw_output, output_files].
  # output_files is a hash containing the absolute paths
  # to file(s) output by the target in the format expected by the
  # objective function(s), with keys as the keys expected by the
  # objective function
  return objective.run(raw_output, output_files, threads)
end