1 : |
agomez |
1 |
#include "initialinputfile.h" |
2 : |
|
|
#include "errorhandler.h" |
3 : |
|
|
#include "gadget.h" |
4 : |
|
|
#include "global.h" |
5 : |
|
|
|
6 : |
|
|
int InitialInputFile::isDataLeft() {
|
7 : |
|
|
if (!repeatedValues)
|
8 : |
|
|
return 0;
|
9 : |
|
|
infile >> ws;
|
10 : |
|
|
if (infile.eof())
|
11 : |
|
|
return 0;
|
12 : |
|
|
return 1;
|
13 : |
|
|
}
|
14 : |
|
|
|
15 : |
|
|
void InitialInputFile::getVectors(ParameterVector& sw, DoubleVector& val,
|
16 : |
|
|
DoubleVector& low, DoubleVector& upp, IntVector& opt) {
|
17 : |
|
|
|
18 : |
|
|
sw = switches;
|
19 : |
|
|
val = values;
|
20 : |
|
|
low = lowerbound;
|
21 : |
|
|
upp = upperbound;
|
22 : |
|
|
opt = optimise;
|
23 : |
|
|
}
|
24 : |
|
|
|
25 : |
|
|
void InitialInputFile::getValues(DoubleVector& val) {
|
26 : |
|
|
val = values;
|
27 : |
|
|
}
|
28 : |
|
|
|
29 : |
|
|
void InitialInputFile::getSwitches(ParameterVector& sw) {
|
30 : |
|
|
sw = switches;
|
31 : |
|
|
}
|
32 : |
|
|
|
33 : |
|
|
InitialInputFile::InitialInputFile(const char* const filename) {
|
34 : |
|
|
tmpinfile.open(filename, ios::in);
|
35 : |
|
|
handle.checkIfFailure(tmpinfile, filename);
|
36 : |
|
|
infile.setStream(tmpinfile);
|
37 : |
|
|
handle.Open(filename);
|
38 : |
|
|
}
|
39 : |
|
|
|
40 : |
|
|
InitialInputFile::~InitialInputFile() {
|
41 : |
|
|
tmpinfile.close();
|
42 : |
|
|
tmpinfile.clear();
|
43 : |
|
|
}
|
44 : |
|
|
|
45 : |
|
|
void InitialInputFile::readHeader() {
|
46 : |
|
|
|
47 : |
|
|
infile >> ws;
|
48 : |
|
|
if (infile.eof() || infile.fail()) {
|
49 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - found no parameters in data file");
|
50 : |
|
|
|
51 : |
|
|
} else if (isdigit(infile.peek())) {
|
52 : |
|
|
repeatedValues = 1;
|
53 : |
|
|
|
54 : |
|
|
} else {
|
55 : |
|
|
char text[MaxStrLength];
|
56 : |
|
|
char textInLine[LongString];
|
57 : |
|
|
strncpy(text, "", MaxStrLength);
|
58 : |
|
|
strncpy(textInLine, "", LongString);
|
59 : |
|
|
|
60 : |
|
|
infile.getLine(textInLine, LongString);
|
61 : |
|
|
if (infile.fail())
|
62 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - line too long");
|
63 : |
|
|
|
64 : |
|
|
istringstream line(textInLine);
|
65 : |
|
|
line >> text >> ws;
|
66 : |
|
|
if (strcasecmp(text, "switches") == 0) {
|
67 : |
|
|
// fileformat with switches and vector of values
|
68 : |
|
|
repeatedValues = 1;
|
69 : |
|
|
|
70 : |
|
|
Parameter tmpparam;
|
71 : |
|
|
while (!line.eof()) {
|
72 : |
|
|
line >> tmpparam >> ws;
|
73 : |
|
|
if (line.fail() && !line.eof())
|
74 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - failed to read switches");
|
75 : |
|
|
switches.resize(tmpparam);
|
76 : |
|
|
}
|
77 : |
|
|
|
78 : |
|
|
} else {
|
79 : |
|
|
// fileformat must be of the form
|
80 : |
|
|
// switch value lowerbound upperbound optimise
|
81 : |
|
|
repeatedValues = 0;
|
82 : |
|
|
if (strcasecmp(text, "switch") != 0)
|
83 : |
|
|
handle.logFileUnexpected(LOGFAIL, "switch", text);
|
84 : |
|
|
|
85 : |
|
|
line >> text >> ws;
|
86 : |
|
|
if (strcasecmp(text, "value") != 0)
|
87 : |
|
|
handle.logFileUnexpected(LOGFAIL, "value", text);
|
88 : |
|
|
|
89 : |
|
|
line >> text >> ws;
|
90 : |
|
|
if (strcasecmp(text, "lower") != 0)
|
91 : |
|
|
handle.logFileUnexpected(LOGFAIL, "lower", text);
|
92 : |
|
|
|
93 : |
|
|
line >> text >> ws;
|
94 : |
|
|
if (strcasecmp(text, "upper") != 0)
|
95 : |
|
|
handle.logFileUnexpected(LOGFAIL, "upper", text);
|
96 : |
|
|
|
97 : |
|
|
line >> text >> ws;
|
98 : |
|
|
if ((strcasecmp(text, "optimise") != 0) && (strcasecmp(text, "optimize") != 0))
|
99 : |
|
|
handle.logFileUnexpected(LOGFAIL, "optimise", text);
|
100 : |
|
|
|
101 : |
|
|
if (!line.eof())
|
102 : |
|
|
handle.logFileUnexpected(LOGFAIL, "<end of line>", text);
|
103 : |
|
|
}
|
104 : |
|
|
}
|
105 : |
|
|
}
|
106 : |
|
|
|
107 : |
|
|
void InitialInputFile::readFromFile() {
|
108 : |
|
|
|
109 : |
|
|
this->readHeader();
|
110 : |
|
|
infile >> ws;
|
111 : |
|
|
|
112 : |
|
|
if (repeatedValues) {
|
113 : |
|
|
this->readNextLine();
|
114 : |
|
|
if ((switches.Size() > 0) && (switches.Size() != values.Size()))
|
115 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - failed to read switches");
|
116 : |
|
|
|
117 : |
|
|
} else {
|
118 : |
|
|
Parameter sw;
|
119 : |
|
|
double val, lower, upper;
|
120 : |
|
|
int opt, check, rand;
|
121 : |
|
|
char c;
|
122 : |
|
|
char text[MaxStrLength];
|
123 : |
|
|
strncpy(text, "", MaxStrLength);
|
124 : |
|
|
|
125 : |
|
|
rand = check = 0;
|
126 : |
|
|
while (check == 0) {
|
127 : |
|
|
infile >> sw >> ws;
|
128 : |
|
|
switches.resize(sw);
|
129 : |
|
|
if (infile.eof())
|
130 : |
|
|
check++;
|
131 : |
|
|
|
132 : |
|
|
c = infile.peek();
|
133 : |
|
|
if (isdigit(c) || (c == '-')) {
|
134 : |
|
|
infile >> val >> ws;
|
135 : |
|
|
values.resize(1, val);
|
136 : |
|
|
} else if ((c == 'r') || (c == 'R')) {
|
137 : |
|
|
infile >> text >> ws;
|
138 : |
|
|
if (strcasecmp(text, "random") == 0)
|
139 : |
|
|
rand++;
|
140 : |
|
|
else
|
141 : |
|
|
check++;
|
142 : |
|
|
} else
|
143 : |
|
|
check++;
|
144 : |
|
|
|
145 : |
|
|
if (infile.eof())
|
146 : |
|
|
check++;
|
147 : |
|
|
|
148 : |
|
|
infile >> lower >> upper >> opt >> ws;
|
149 : |
|
|
lowerbound.resize(1, lower);
|
150 : |
|
|
upperbound.resize(1, upper);
|
151 : |
|
|
optimise.resize(1, opt);
|
152 : |
|
|
if (infile.eof())
|
153 : |
|
|
check++;
|
154 : |
|
|
|
155 : |
|
|
if (rand) {
|
156 : |
|
|
// generate a random point somewhere between the bounds
|
157 : |
|
|
val = lower + (randomNumber() * (upper - lower));
|
158 : |
|
|
handle.logMessage(LOGMESSAGE, "Generating a random starting point", val, sw.getName());
|
159 : |
|
|
values.resize(1, val);
|
160 : |
|
|
rand = 0;
|
161 : |
|
|
}
|
162 : |
|
|
}
|
163 : |
|
|
|
164 : |
|
|
// check that the parameters have been read correctly
|
165 : |
|
|
if (upperbound.Size() != lowerbound.Size())
|
166 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - failed to read bounds data");
|
167 : |
|
|
else if (upperbound.Size() == 0)
|
168 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - failed to read bounds data");
|
169 : |
|
|
else if (values.Size() != switches.Size())
|
170 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - failed to read switches data");
|
171 : |
|
|
else if (values.Size() != lowerbound.Size())
|
172 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - failed to read bounds data");
|
173 : |
|
|
else if (optimise.Size() != values.Size())
|
174 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - failed to read optimise data");
|
175 : |
|
|
|
176 : |
|
|
// check that the names of the switches are unique
|
177 : |
|
|
int i, j;
|
178 : |
|
|
for (i = 0; i < switches.Size(); i++)
|
179 : |
|
|
for (j = 0; j < switches.Size(); j++)
|
180 : |
|
|
if ((strcasecmp(switches[i].getName(), switches[j].getName()) == 0) && (i != j))
|
181 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - repeated switch", switches[i].getName());
|
182 : |
|
|
}
|
183 : |
|
|
|
184 : |
|
|
//JMB close the errorhandler to clear the filename from the stack
|
185 : |
|
|
handle.Close();
|
186 : |
|
|
}
|
187 : |
|
|
|
188 : |
|
|
void InitialInputFile::readNextLine() {
|
189 : |
|
|
int i;
|
190 : |
|
|
double tempX;
|
191 : |
|
|
DoubleVector tempValues;
|
192 : |
|
|
|
193 : |
|
|
infile >> ws;
|
194 : |
|
|
if (infile.eof())
|
195 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - failed to read vector");
|
196 : |
|
|
|
197 : |
|
|
char text[LongString];
|
198 : |
|
|
strncpy(text, "", LongString);
|
199 : |
|
|
infile.getLine(text, LongString);
|
200 : |
|
|
if (infile.fail())
|
201 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - line too long");
|
202 : |
|
|
|
203 : |
|
|
istringstream line(text);
|
204 : |
|
|
while (!line.eof() && !line.fail() && !(line.peek() == ';')) {
|
205 : |
|
|
line >> tempX;
|
206 : |
|
|
tempValues.resize(1, tempX);
|
207 : |
|
|
if (line.good())
|
208 : |
|
|
line >> ws;
|
209 : |
|
|
}
|
210 : |
|
|
if (line.fail() && !line.eof())
|
211 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial input file - failed to read vector");
|
212 : |
|
|
values.Reset();
|
213 : |
|
|
values = tempValues;
|
214 : |
|
|
}
|