class Foxholes
Implementation according to www.sfu.ca/~ssurjano/shekel.html www.zsd.ict.pwr.wroc.pl/files/docs/functions.pdf
Public Class Methods
new()
click to toggle source
# File lib/gimuby/problem/foxholes/foxholes.rb, line 12 def initialize ensure_holes_coordinates end
Public Instance Methods
evaluate(x_values)
click to toggle source
# File lib/gimuby/problem/foxholes/foxholes.rb, line 16 def evaluate(x_values) sum = 0.0 get_number_holes.times do |j| hole_coordinate = @holes_coordinates[j] sub_sum = 0.10 hole_coordinate.each_index do |i| sub_sum += (x_values[i].to_f - hole_coordinate[i]) ** 2 end sum += 1.0 / sub_sum end sum end
Protected Instance Methods
ensure_holes_coordinates()
click to toggle source
# File lib/gimuby/problem/foxholes/foxholes.rb, line 39 def ensure_holes_coordinates path = $config.persistence_dir_path + '/foxholes_holes_coordinates_' + get_number_holes.to_s + '.data' load_holes_coordinates(path) if @holes_coordinates.nil? init_holes_coordinates persist_holes_coordinates(path) end end
get_holes_coordinates()
click to toggle source
# File lib/gimuby/problem/foxholes/foxholes.rb, line 35 def get_holes_coordinates @holes_coordinates end
get_number_holes()
click to toggle source
# File lib/gimuby/problem/foxholes/foxholes.rb, line 31 def get_number_holes 25 end
get_random_coordinates()
click to toggle source
# File lib/gimuby/problem/foxholes/foxholes.rb, line 70 def get_random_coordinates scale = @@x_value_max - @@x_value_min x = rand() * scale + @@x_value_min y = rand() * scale + @@x_value_min [x, y] end
init_holes_coordinates()
click to toggle source
# File lib/gimuby/problem/foxholes/foxholes.rb, line 63 def init_holes_coordinates @holes_coordinates = [] get_number_holes.times do |_| @holes_coordinates.push(get_random_coordinates) end end
load_holes_coordinates(path)
click to toggle source
# File lib/gimuby/problem/foxholes/foxholes.rb, line 49 def load_holes_coordinates(path) if File::exists? path f = File.new(path, 'r') @holes_coordinates = Marshal.load(f.read()) f.close() end end
persist_holes_coordinates(path)
click to toggle source
# File lib/gimuby/problem/foxholes/foxholes.rb, line 57 def persist_holes_coordinates(path) f = File.new(path, 'w') f.write(Marshal.dump(@holes_coordinates)) f.close() end