-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradDescent.m~
65 lines (55 loc) · 1.75 KB
/
gradDescent.m~
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
function [kWorking, error,t,X,idx,K] = gradDescent()
numMasses = 3;
kWorking = [];
for targetNum = 0:1:(2^numMasses-1)
targetNum
biV = de2bi(targetNum, numMasses);
biV(biV==0)=-1;
target = [biV(1),biV(2),biV(3),0,0,0];
terms = 20;
randomExplore = 1000;
[K, cost] = makeNewK(terms);
clf(figure(1))
hold on
restart = 0;
while restart == 1
minimalK = K;
minimalCost = cost;
for i = [1:1:randomExplore]
[K, cost] = makeNewK(terms);
if cost < minimalCost
minimalCost = cost;
minimalK = K;
end
end
K = minimalK;
cost = minimalCost;
i = 1;
while cost > .01 && restart == 0
stepMatrix = eye(terms)/1000;
grad = arrayfun(@(p) cost - objFun(K + stepMatrix(p,:)), 1:terms);
K = K + 20 * grad ;
cost = objFun(K);
plot(i,cost,'.')
drawnow
i = i+1;
if i > 100
restart= 1;
end
end
if restart == 0
kWorking(:, targetNum+1) = K'
break
end
end
[error,t,X,idx] = howClose(K(1:terms/2),K(terms/2+1:end),target,true);
end
function cost = objFun(M)
cutoff = length(M)/2;
cost = howClose(M(1:cutoff),M(cutoff+1:end),target,false);
end
function [K, cost] = makeNewK(terms)
K = [rand(1,terms/2),rand(1,terms/2)*5];
cost = objFun(K);
end
end