Class: GenCurvefit

Inherits:
Object
  • Object
show all
Defined in:
lib/ms/curvefit/curve_fit_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pts_in, function = nil, paramsize = nil, mutation_limits = nil, popsize = 0, generations = nil) ⇒ GenCurvefit

Returns a new instance of GenCurvefit.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 26

def initialize(pts_in,function = nil,paramsize = nil,mutation_limits = nil,popsize = 0,generations = nil)
  @pts_in = pts_in
  @function = function
  @paramsize = paramsize
  @mutation_limits = mutation_limits
  @popsize = popsize
  @generations = generations
  @population = []
  if @popsize != 0 and @paramsize != nil and @mutation_limits != nil and @function != nil
    init_population
  end
end

Instance Attribute Details

#functionObject (readonly)

Returns the value of attribute function.



39
40
41
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 39

def function
  @function
end

#generationsObject

Returns the value of attribute generations.



39
40
41
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 39

def generations
  @generations
end

#mutation_limitsObject

Returns the value of attribute mutation_limits.



39
40
41
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 39

def mutation_limits
  @mutation_limits
end

#paramsizeObject

Returns the value of attribute paramsize.



39
40
41
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 39

def paramsize
  @paramsize
end

#popsizeObject

Returns the value of attribute popsize.



39
40
41
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 39

def popsize
  @popsize
end

#populationObject

Returns the value of attribute population.



39
40
41
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 39

def population
  @population
end

Class Method Details

.normalize(arr) ⇒ Object



80
81
82
83
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 80

def self.normalize(arr)
  max = arr.max
  arr.map!{|i| (i.to_f/max) * 100}
end

.smoothave(arr) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 64

def self.smoothave(arr)
  smooth_ave = [nil,nil,nil]
  queue = []
  arr.each do |i|
    queue.push(i)
    if queue.size > 7
      queue.shift
    end
    smooth_ave<<queue.inject(:+)/queue.size if queue.size == 7
  end
  3.times do 
    smooth_ave<<nil
  end
  return smooth_ave
end

Instance Method Details

#fitObject



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
146
147
148
149
150
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 121

def fit
  prog = Progress.new("Generation")
  num = 0
  total = @generations
  step = total/100
  @generations.times do |i|
    if i > step * (num + 1)
	num = ((i/total.to_f)*100).to_i
	prog.update(num," #{i+1}:")
    end
    #Generate mutations
    index = rand(@popsize)
    clone = @population[index].clone
    mutate(clone)
    clone[@paramsize] = fitness(clone,@pts_in)

    if(clone.last < @population.last.last)
      @population[@population.size - (@paramsize-1)] = clone
    end
    #Re-sort
    @population = sort_by_fitness

    #Print best
    if i == @generations - 1
      @best = @population.first
    end
  end 
  prog.finish!
  return @best
end

#fitness(set, pts_in, plot = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 106

def fitness(set,pts_in,plot = false)
  pts = []
  xs = pts_in.transpose[0]
  xs.each do |x|
    fit_pt = function.call(set,x)
    pts<<[x,fit_pt]
  end

  if plot
    return pts
  end

  return rmsd(pts_in,pts)
end

#init_populationObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 42

def init_population
  @popsize.times do
    set = []
    @paramsize.times do |i|
      limits = @mutation_limits[i]
      set<<random_float(limits[0],limits[1])
    end
    set<<fitness(set,@pts_in)
    @population<<set
  end
end

#mutate(set) ⇒ Object



58
59
60
61
62
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 58

def mutate(set)
  index = rand(set.size-1)
  limits = @mutation_limits[index]
  set[index] += random_float(limits[0],limits[1])
end

#plot(file, labels = nil) ⇒ Object



152
153
154
155
156
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 152

def plot(file,labels = nil)
  pts = fitness(@best,@pts_in,true)
  Fit_plot.plot(@pts_in,pts,file,labels)
  puts "  Output File: #{file}"
end

#random_float(a, b) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 89

def random_float(a,b)
  a = a.to_f
  b = b.to_f
  random = rand(2147483647.0) / 2147483647.0
  diff = b - a
  r = random * diff
  return a + r
end

#rmsd(v, w) ⇒ Object



98
99
100
101
102
103
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 98

def rmsd(v,w)
  n = v.size
  sum = 0.0
  n.times{|i| sum += ((v[i][0]-w[i][0])**2.0 + (v[i][1]-w[i][1])**2.0) }
  return Math.sqrt( (1/n.to_f) * sum )
end

#set_fit_function(func) ⇒ Object



54
55
56
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 54

def set_fit_function(func)
  @function = func
end

#sort_by_fitnessObject



85
86
87
# File 'lib/ms/curvefit/curve_fit_helper.rb', line 85

def sort_by_fitness
  @population.sort_by!{|set| set.last}     
end