-
Notifications
You must be signed in to change notification settings - Fork 2
/
81.med.rb
65 lines (54 loc) · 1.95 KB
/
81.med.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# For a lot of the questions today we are going to be doing some simple
# numerical calculus. Don't worry, its not too terrifying.
#
# Write a function fmin that can take in a real-valued function f(x) where x
# is a vector in 3D space (bonus points for N-D).
#
# qxout=fmin(f,x0) should return a local minimum of f close to x0.
# Example in Python
# %f is a function with 1 3-vector
# def f(x):
# return x[0]**2+x[1]**2+x[2]**2
# %find the local minimum of f, starting at (1,1,1)
# print fmin(f,[1.0,1.0,1.0])
# should print out
# [0.0,0.0,0.0] %because (0,0,0) is the global minimum of f(x,y,z)=x^2+y^2+z^2
# EDIT: To make this a little easier, I decided that it is acceptable for your
# implementation to require that fmin have additional arguments for the
# derivatives of f
# BMS: My approach will be to do a genetic algorithm search, seeding the
# population with the initial vector, with some global tuned variance
# around that initial vector. Also parameterize the dimension
# Uses the Rubystats gem for gaussian random numbers
# -- sudo gem install rubystats
# Retrieved July 26, 2012 from
# http://www.reddit.com/r/dailyprogrammer/comments/x539t/7252012_challenge_81_intermediate_local/
require 'rubygems'
require 'rubystats'
require 'pp'
SIZE = 10
DIM = 2
INIT_ARR = [3.0, -8.0, 100.0, 42, 78] #Array.new(DIM, 1.0)
DEV_ARR = [1.0, 1.0, 1.0, 0.1, 0.3] #Array.new(DIM, 1.0)
class Population
attr_accessor :pop
attr_accessor :fitness
def initialize (size, dimension, init_arr, dev_arr)
super()
@normals = Array.new(DIM) { |i| Rubystats::NormalDistribution.new(init_arr[i], dev_arr[i]) }
@pop = []
@fitness = []
for i in 0..size
@pop << Array.new(DIM) { |i| @normals[i].rng }
@fitness << f(@pop[i])
end
end
def f (arr)
arr.inject(0) { |result,n| result + (n ** 2) }
end
end
p = Population.new(SIZE, DIM, INIT_ARR, DEV_ARR)
pp(p.pop)
pp(p.f([2,2,2,2]))
#pp(p.normal_rng_arr)
#norm = Rubystats::NormalDistribution.new(3,1)