1 : |
agomez |
1 |
#include "optimizer.h" |
2 : |
|
|
|
3 : |
|
|
// ********************************************************
|
4 : |
|
|
// functions for class Optimizer
|
5 : |
|
|
// ********************************************************
|
6 : |
|
|
Optimizer::Optimizer(CommandLineInfo* info, NetInterface* net) {
|
7 : |
|
|
int i;
|
8 : |
|
|
netInt = net;
|
9 : |
|
|
// useSA = 0;
|
10 : |
|
|
// useHJ = 0;
|
11 : |
|
|
// useBFGS = 0;
|
12 : |
|
|
// parSA = NULL;
|
13 : |
|
|
// parHJ = NULL;
|
14 : |
|
|
// parBFGS = NULL;
|
15 : |
|
|
|
16 : |
|
|
// Initialise random number generator
|
17 : |
|
|
srand(time(NULL));
|
18 : |
|
|
|
19 : |
|
|
outputfile = info->getOutputFilename();
|
20 : |
|
|
if (info->getOptInfoFileGiven())
|
21 : |
|
|
this->readOptInfo(info->getOptFilename());
|
22 : |
|
|
else {
|
23 : |
|
|
cout << "No optimizing information given - using default information\n";
|
24 : |
|
|
// parHJ = new ParaminHooke(net);
|
25 : |
|
|
// useHJ = 1;
|
26 : |
|
|
optvec.resize(new ParaminHooke(net));
|
27 : |
|
|
}
|
28 : |
|
|
// DoubleVector startx;
|
29 : |
|
|
// Set the starting value of x and f
|
30 : |
|
|
// startx = net->getInitialX();
|
31 : |
|
|
// cout << "best x in optimizer is: " << endl;
|
32 : |
|
|
// for (i = 0; i < startx.Size(); i++)
|
33 : |
|
|
// cout << startx[i] << " ";
|
34 : |
|
|
// cout << endl;
|
35 : |
|
|
// startx is scaled if scaling is used
|
36 : |
|
|
netInt->startNewDataGroup(1);
|
37 : |
|
|
netInt->setX(netInt->getInitialX());
|
38 : |
|
|
netInt->sendAndReceiveAllData();
|
39 : |
|
|
//cout << "in optmizier got fx " << netInt->getY(0) << endl;
|
40 : |
|
|
netInt->setScore(netInt->getY(0));
|
41 : |
|
|
// startf = net->getY(0);
|
42 : |
|
|
netInt->stopUsingDataGroup();
|
43 : |
|
|
}
|
44 : |
|
|
|
45 : |
|
|
Optimizer::~Optimizer() {
|
46 : |
|
|
/*
|
47 : |
|
|
if (parSA != NULL)
|
48 : |
|
|
delete parSA;
|
49 : |
|
|
if (parHJ != NULL)
|
50 : |
|
|
delete parHJ;
|
51 : |
|
|
if (parBFGS != NULL)
|
52 : |
|
|
delete parBFGS;
|
53 : |
|
|
*/
|
54 : |
|
|
int i;
|
55 : |
|
|
for (i = 0; i < optvec.Size(); i++)
|
56 : |
|
|
delete optvec[i];
|
57 : |
|
|
|
58 : |
|
|
}
|
59 : |
|
|
|
60 : |
|
|
void Optimizer::readOptInfo(char* optfilename) {
|
61 : |
|
|
char* text = new char[MaxStrLength];
|
62 : |
|
|
strncpy(text, "", MaxStrLength);
|
63 : |
|
|
ifstream infile(optfilename);
|
64 : |
|
|
CommentStream commin(infile);
|
65 : |
|
|
int i = 0;
|
66 : |
|
|
// Now need to look for seed, [simann], [hooke] or [bfgs]
|
67 : |
|
|
commin >> text;
|
68 : |
|
|
while (!commin.eof()) {
|
69 : |
|
|
commin >> ws; //trim whitespace from infile
|
70 : |
|
|
if ((strcasecmp(text, "seed")) == 0 && (!commin.eof())) {
|
71 : |
|
|
commin >> seed >> ws >> text;
|
72 : |
|
|
srand(seed);
|
73 : |
|
|
|
74 : |
|
|
} else if (strcasecmp(text, "[simann]") == 0) {
|
75 : |
|
|
// parSA = new ParaminSimann(net);
|
76 : |
|
|
//useSA = 1;
|
77 : |
|
|
optvec.resize(new ParaminSimann(netInt));
|
78 : |
|
|
|
79 : |
|
|
} else if (strcasecmp(text, "[hooke]") == 0) {
|
80 : |
|
|
// parHJ = new ParaminHooke(net);
|
81 : |
|
|
// useHJ = 1;
|
82 : |
|
|
optvec.resize(new ParaminHooke(netInt));
|
83 : |
|
|
} else if (strcasecmp(text, "[bfgs]") == 0) {
|
84 : |
|
|
//parBFGS = new ParaminBFGS(net);
|
85 : |
|
|
// useBFGS = 1;
|
86 : |
|
|
optvec.resize(new ParaminBFGS(netInt));
|
87 : |
|
|
}
|
88 : |
|
|
else {
|
89 : |
|
|
// set log failure here...
|
90 : |
|
|
cerr << " did not find bfgs, hooke, simann or seed" << endl;
|
91 : |
|
|
// exit ..
|
92 : |
|
|
}
|
93 : |
|
|
if (!commin.eof()) {
|
94 : |
|
|
if (optvec.Size() > 0) {
|
95 : |
|
|
commin >> text;
|
96 : |
|
|
if ((text[0] == '[') || (strcasecmp(text, "seed") == 0))
|
97 : |
|
|
cerr << "Warning - no optimisation parameters specified for optimization algorithm\n";
|
98 : |
|
|
else {
|
99 : |
|
|
optvec[i]->read(commin, text);
|
100 : |
|
|
i++;
|
101 : |
|
|
}
|
102 : |
|
|
}
|
103 : |
|
|
} else
|
104 : |
|
|
cerr << "Warning - no optimisation parameters specified for optimization algorithm\n";
|
105 : |
|
|
|
106 : |
|
|
|
107 : |
|
|
}
|
108 : |
|
|
if (optvec.Size() == 0) {
|
109 : |
|
|
// set here log message...
|
110 : |
|
|
optvec.resize(new ParaminHooke(netInt));
|
111 : |
|
|
}
|
112 : |
|
|
delete [] text;
|
113 : |
|
|
infile.close();
|
114 : |
|
|
infile.clear();
|
115 : |
|
|
|
116 : |
|
|
}
|
117 : |
|
|
|
118 : |
|
|
void Optimizer::OptimizeFunc() {
|
119 : |
|
|
int i;
|
120 : |
|
|
ofstream outfile;
|
121 : |
|
|
outfile.open(outputfile);
|
122 : |
|
|
for (i = 0; i < optvec.Size(); i++)
|
123 : |
|
|
{
|
124 : |
|
|
// Keep track of how much time has taken for each optimization.
|
125 : |
|
|
time_t timestart;
|
126 : |
|
|
timestart = time(NULL);
|
127 : |
|
|
optvec[i]->OptimiseLikelihood();
|
128 : |
|
|
time_t timeafter;
|
129 : |
|
|
timeafter = time(NULL);
|
130 : |
|
|
//Added 2/07/2012 to measure time of each optimization G.E.
|
131 : |
|
|
if(optvec[i]->getType() == OPTSIMANN)
|
132 : |
|
|
{
|
133 : |
|
|
outfile << "; Simulated annealing optimization finished in " <<": " << difftime(timeafter, timestart) << " seconds\n";
|
134 : |
|
|
}
|
135 : |
|
|
else if(optvec[i]->getType() == OPTHOOKE)
|
136 : |
|
|
{
|
137 : |
|
|
outfile << "; Hooke and Jeeves optimization finished in " <<": " << difftime(timeafter, timestart) << " seconds\n";
|
138 : |
|
|
}
|
139 : |
|
|
else if(optvec[i]->getType() == OPTBFGS)
|
140 : |
|
|
{
|
141 : |
|
|
outfile << "; BFGS optimization finished in " <<": " << difftime(timeafter, timestart) << " seconds\n";
|
142 : |
|
|
}
|
143 : |
|
|
else
|
144 : |
|
|
{
|
145 : |
|
|
outfile << "; New algorithm optimization finished in " <<": " << difftime(timeafter, timestart) << " seconds\n";
|
146 : |
|
|
}
|
147 : |
|
|
}
|
148 : |
|
|
outfile.close();
|
149 : |
|
|
}
|
150 : |
|
|
|
151 : |
|
|
void Optimizer::getScore(DoubleVector& x, double fx) {
|
152 : |
|
|
//netInt->getInitialScore(x, fx);
|
153 : |
|
|
fx = netInt->getInitialScore(x);
|
154 : |
|
|
}
|
155 : |
|
|
|
156 : |
|
|
double Optimizer::getBestF() {
|
157 : |
|
|
return netInt->getScore();
|
158 : |
|
|
}
|
159 : |
|
|
|
160 : |
|
|
void Optimizer::printResult() {
|
161 : |
|
|
// write the best point out to a file
|
162 : |
|
|
int i;
|
163 : |
|
|
time_t timenow;
|
164 : |
|
|
timenow = time(NULL);
|
165 : |
|
|
ofstream outfile;
|
166 : |
|
|
outfile.open(outputfile, ios::out | ios::app);
|
167 : |
|
|
DoubleVector x;
|
168 : |
|
|
double fx;
|
169 : |
|
|
//netInt->getInitialScore(x, fx);
|
170 : |
|
|
fx = netInt->getInitialScore(x);
|
171 : |
|
|
if (!outfile) {
|
172 : |
|
|
|
173 : |
|
|
cout << "\nWarning - can't open outputfile\nThe best point calculated is f(x) = "
|
174 : |
|
|
<< fx << " at\n";
|
175 : |
|
|
this->printX(x);
|
176 : |
|
|
|
177 : |
|
|
} else {
|
178 : |
|
|
// write the data in the gadget format so this file can be used as a starting point
|
179 : |
|
|
outfile << "; Output from Paramin version " << PARAMINVERSION << " on " << ctime(&timenow);
|
180 : |
|
|
// AJ must decide on prec... set it somewher eelse. ...
|
181 : |
|
|
for (i = 0; i < optvec.Size(); i++)
|
182 : |
|
|
optvec[i]->Print(outfile, 8);
|
183 : |
|
|
|
184 : |
|
|
outfile << "; The final likelihood value was " << fx << "\nswitch\tvalue\t\tlower\tupper\toptimise\n";
|
185 : |
|
|
for (i = 0; i < x.Size(); i++) {
|
186 : |
|
|
outfile << (netInt->getSwitches())[i].getName() << TAB << setw(12) << setprecision(8)
|
187 : |
|
|
<< x[i] << TAB << setw(8) << setprecision(4) << netInt->getLowerbound()[i]
|
188 : |
|
|
<< setw(8) << setprecision(4) << netInt->getUpperbound()[i]
|
189 : |
|
|
<< setw(8) << netInt->getOptInfo()[i] << endl;
|
190 : |
|
|
|
191 : |
|
|
}
|
192 : |
|
|
}
|
193 : |
|
|
outfile.close();
|
194 : |
|
|
}
|
195 : |
|
|
void Optimizer::printX(const DoubleVector& vec) {
|
196 : |
|
|
int i;
|
197 : |
|
|
for (i = 0; i < vec.Size(); i++)
|
198 : |
|
|
cout << vec[i] << sep;
|
199 : |
|
|
cout << endl;
|
200 : |
|
|
}
|