-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedy.cpp
More file actions
78 lines (70 loc) · 1.23 KB
/
greedy.cpp
File metadata and controls
78 lines (70 loc) · 1.23 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <cstdlib>
#include "SumofGaussians.h"
using namespace std;
int main(int argc, char* argv[])
{
int seed = atoi(argv[1]);
int dims = atoi(argv[2]);
int ncenters = atoi(argv[3]);
srand(seed);
SumofGaussians sog(dims,ncenters);
int count = 0;
double input[dims];
double temp[dims];
double dz[dims];
bool peak[dims] = {false};
bool complete = false;
for(int i = 0; i < dims; i++)
{
input[i] = getRandom()*10;
}
do{
sog.deriv(input,dz);
for(int i = 0; i < dims; i++)
{
if(not peak[i])
{
for(int j = 0; j < dims; j++)
{
temp[j] = input[j] + 0.01*dz[j];
}
double diff = sog.eval(input) - sog.eval(temp);
if(diff < 0.00000001 && diff > -0.00000001)
{
peak[i] = true;
}
else
{
for(int j = 0; j < dims; j++)
{
input[j] = temp[j];
}
}
for(int k = 0; k < dims; k++)
{
cout << input[k] << " ";
}
cout << sog.eval(input) << endl;
}
}
count = 0;
for(int i = 0; i < dims; i++)
{
if(peak[i])
{
count++;
}
}
if(count == dims)
{
complete = true;
}
}while(not complete);
for(int i = 0; i < dims; i++)
{
cout << input[i] << " ";
}
cout << sog.eval(input) << endl;
return 0;
}