Created
December 2, 2014 18:08
-
-
Save skierkowski/706ca9aa97e6a21eb462 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'awesome_print' | |
require 'annealer' | |
require 'minimization' | |
content = File.read('./data.csv') | |
data = content.split("\n").map{|n| n.to_i} | |
# A little helper class to implement split on an Array | |
class Array | |
def split(value) | |
new_return = [] | |
count = 0 | |
each do |item| | |
new_return[count] ||= [] | |
if item == value | |
count += 1 | |
else | |
new_return[count] << item | |
end | |
end | |
new_return | |
end | |
end | |
sequences = data.split(0).reject{|c| c.empty?} | |
@@sequence = sequences.first | |
def f_real(values, x) | |
f1 = f(values[0],values[1],values[2],values[3],x) | |
f2 = f(values[4],values[5],values[6],values[7],x) | |
f1 + f2 | |
end | |
def f(a,b,c,d,x) | |
a + b*Math.sin(c*(x + d)) | |
end | |
f= proc {|x| | |
delta = 0 | |
@@sequence.each_with_index do |value, index| | |
delta += (value - f_real(x,index)).abs | |
end | |
delta | |
} | |
start = [25, 9, 1, 1, 26, 8, 0, 0] | |
lower = [0,0,0,0,0,0,0,0] | |
upper = [150,150,10,10,150,150,10,10] | |
min = Minimization::Powell.minimize(f,start,lower,upper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment