1 : |
agomez |
1 |
#include "netinterface.h" |
2 : |
|
|
|
3 : |
|
|
// ********************************************************
|
4 : |
|
|
// Functions for starting/stopping using a new data group
|
5 : |
|
|
// ********************************************************
|
6 : |
|
|
void NetInterface::startNewDataGroup() {
|
7 : |
|
|
if (dctrl != NULL)
|
8 : |
|
|
stopUsingDataGroup();
|
9 : |
|
|
int newTag = getNextMsgTag();
|
10 : |
|
|
dctrl = new NetDataControl(maxNumX, numVarInDataGroup, newTag);
|
11 : |
|
|
if (scaler != NULL)
|
12 : |
|
|
scaler->setPenalty(maxNumX);
|
13 : |
|
|
dataSet = new Queue();
|
14 : |
|
|
isAlpha = 0;
|
15 : |
|
|
}
|
16 : |
|
|
|
17 : |
|
|
void NetInterface::startNewDataGroup(const DoubleVector& x1, const DoubleVector& h1) {
|
18 : |
|
|
if (dctrl != NULL)
|
19 : |
|
|
stopUsingDataGroup();
|
20 : |
|
|
int newTag = getNextMsgTag();
|
21 : |
|
|
dctrl = new NetDataControl(maxNumX, 1, newTag);
|
22 : |
|
|
if (scaler != NULL)
|
23 : |
|
|
scaler->setPenalty(maxNumX);
|
24 : |
|
|
dataSet = new Queue();
|
25 : |
|
|
h = h1;
|
26 : |
|
|
alphaX = x1;
|
27 : |
|
|
isAlpha = 1;
|
28 : |
|
|
}
|
29 : |
|
|
|
30 : |
|
|
void NetInterface::startNewDataGroup(int numInGroup) {
|
31 : |
|
|
if (dctrl != NULL)
|
32 : |
|
|
stopUsingDataGroup();
|
33 : |
|
|
int newTag = getNextMsgTag();
|
34 : |
|
|
dctrl = new NetDataControl(numInGroup, numVarInDataGroup, newTag);
|
35 : |
|
|
if (scaler != NULL)
|
36 : |
|
|
scaler->setPenalty(numInGroup);
|
37 : |
|
|
dataSet = new Queue();
|
38 : |
|
|
isAlpha = 0;
|
39 : |
|
|
}
|
40 : |
|
|
|
41 : |
|
|
void NetInterface::startNewDataGroup(int numInGroup, const DoubleVector& x1, const DoubleVector& h1) {
|
42 : |
|
|
if (dctrl != NULL)
|
43 : |
|
|
stopUsingDataGroup();
|
44 : |
|
|
int newTag = getNextMsgTag();
|
45 : |
|
|
dctrl = new NetDataControl(numInGroup, 1, newTag);
|
46 : |
|
|
if (scaler != NULL)
|
47 : |
|
|
scaler->setPenalty(numInGroup);
|
48 : |
|
|
dataSet = new Queue();
|
49 : |
|
|
h = h1;
|
50 : |
|
|
alphaX = x1;
|
51 : |
|
|
isAlpha = 1;
|
52 : |
|
|
}
|
53 : |
|
|
|
54 : |
|
|
void NetInterface::stopUsingDataGroup() {
|
55 : |
|
|
if (dctrl != NULL) {
|
56 : |
|
|
delete dctrl;
|
57 : |
|
|
dctrl = NULL;
|
58 : |
|
|
}
|
59 : |
|
|
if (dataSet != NULL) {
|
60 : |
|
|
delete dataSet;
|
61 : |
|
|
dataSet = NULL;
|
62 : |
|
|
}
|
63 : |
|
|
receiveID = -1;
|
64 : |
|
|
isAlpha = -1;
|
65 : |
|
|
}
|
66 : |
|
|
|
67 : |
|
|
// ********************************************************
|
68 : |
|
|
// Functions for setting/getting netdata
|
69 : |
|
|
// ********************************************************
|
70 : |
|
|
void NetInterface::setX(const DoubleVector& x1) {
|
71 : |
|
|
if (dctrl == NULL) {
|
72 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
73 : |
|
|
exit(EXIT_FAILURE);
|
74 : |
|
|
}
|
75 : |
|
|
if (dataGroupFull()) {
|
76 : |
|
|
cerr << "Error in netinterface - datagroup is full\n";
|
77 : |
|
|
exit(EXIT_FAILURE);
|
78 : |
|
|
}
|
79 : |
|
|
dctrl->setX(x1);
|
80 : |
|
|
int id = dctrl->getLastSetID();
|
81 : |
|
|
dataSet->put(id);
|
82 : |
|
|
}
|
83 : |
|
|
|
84 : |
|
|
void NetInterface::setXFirstToSend(const DoubleVector& x1) {
|
85 : |
|
|
if (dctrl == NULL) {
|
86 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
87 : |
|
|
exit(EXIT_FAILURE);
|
88 : |
|
|
}
|
89 : |
|
|
if (dataGroupFull()) {
|
90 : |
|
|
cerr << "Error in netinterface - datagroup is full\n";
|
91 : |
|
|
exit(EXIT_FAILURE);
|
92 : |
|
|
}
|
93 : |
|
|
dctrl->setX(x1);
|
94 : |
|
|
int id = dctrl->getLastSetID();
|
95 : |
|
|
dataSet->putFirst(id);
|
96 : |
|
|
}
|
97 : |
|
|
|
98 : |
|
|
void NetInterface::setDataPair(const DoubleVector& x1, double fx) {
|
99 : |
|
|
if (dctrl == NULL) {
|
100 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
101 : |
|
|
exit(EXIT_FAILURE);
|
102 : |
|
|
}
|
103 : |
|
|
if (dataGroupFull()) {
|
104 : |
|
|
cerr << "Error in netinterface - datagroup is full\n";
|
105 : |
|
|
exit(EXIT_FAILURE);
|
106 : |
|
|
}
|
107 : |
|
|
dctrl->setDataPair(x1, fx);
|
108 : |
|
|
}
|
109 : |
|
|
|
110 : |
|
|
const DoubleVector& NetInterface::getX(int id) {
|
111 : |
|
|
if (dctrl == NULL) {
|
112 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
113 : |
|
|
exit(EXIT_FAILURE);
|
114 : |
|
|
}
|
115 : |
|
|
return dctrl->getX(id);
|
116 : |
|
|
}
|
117 : |
|
|
|
118 : |
|
|
double NetInterface::getY(int id) {
|
119 : |
|
|
if (dctrl == NULL) {
|
120 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
121 : |
|
|
exit(EXIT_FAILURE);
|
122 : |
|
|
}
|
123 : |
|
|
return dctrl->getY(id);
|
124 : |
|
|
}
|
125 : |
|
|
|
126 : |
|
|
const DoubleVector& NetInterface::getNextAnswerX() {
|
127 : |
|
|
if (dctrl == NULL) {
|
128 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
129 : |
|
|
exit(EXIT_FAILURE);
|
130 : |
|
|
}
|
131 : |
|
|
return dctrl->getNextAnsweredX();
|
132 : |
|
|
}
|
133 : |
|
|
|
134 : |
|
|
double NetInterface::getNextAnswerY() {
|
135 : |
|
|
if (dctrl == NULL) {
|
136 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
137 : |
|
|
exit(EXIT_FAILURE);
|
138 : |
|
|
}
|
139 : |
|
|
return dctrl->getNextAnsweredY();
|
140 : |
|
|
}
|
141 : |
|
|
|
142 : |
|
|
const DoubleVector& NetInterface::getUpperScaleConstant() {
|
143 : |
|
|
return upperScale;
|
144 : |
|
|
}
|
145 : |
|
|
|
146 : |
|
|
const DoubleVector& NetInterface::getLowerScaleConstant() {
|
147 : |
|
|
return lowerScale;
|
148 : |
|
|
}
|
149 : |
|
|
|
150 : |
|
|
const DoubleVector& NetInterface::getInitialX() {
|
151 : |
|
|
return initialX;
|
152 : |
|
|
}
|
153 : |
|
|
double NetInterface::getScore() {
|
154 : |
|
|
return initialScore;
|
155 : |
|
|
}
|
156 : |
|
|
void NetInterface::setScore(double fx) {
|
157 : |
|
|
initialScore = fx;
|
158 : |
|
|
}
|
159 : |
|
|
double NetInterface::getInitialScore(DoubleVector& x) {
|
160 : |
|
|
//void NetInterface::getInitialScore(DoubleVector& x, double fx) {
|
161 : |
|
|
// must make sure this is ok
|
162 : |
|
|
x = this->prepareVectorToSend(initialX);
|
163 : |
|
|
//fx = initialScore;
|
164 : |
|
|
return initialScore;
|
165 : |
|
|
}
|
166 : |
|
|
void NetInterface::setInitialScore(const DoubleVector& x, double fx) {
|
167 : |
|
|
if (x.Size() != initialX.Size()) {
|
168 : |
|
|
cerr << "Error in netinterface - vectors different size\n";
|
169 : |
|
|
exit(EXIT_FAILURE);
|
170 : |
|
|
}
|
171 : |
|
|
initialX = x;
|
172 : |
|
|
initialScore = fx;
|
173 : |
|
|
}
|
174 : |
|
|
// ********************************************************
|
175 : |
|
|
// Functions for getting/setting information about datagroup
|
176 : |
|
|
// ********************************************************
|
177 : |
|
|
int NetInterface::getReceiveID() {
|
178 : |
|
|
return receiveID;
|
179 : |
|
|
}
|
180 : |
|
|
|
181 : |
|
|
void NetInterface::sentDataItem(int id) {
|
182 : |
|
|
if (dctrl == NULL) {
|
183 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
184 : |
|
|
exit(EXIT_FAILURE);
|
185 : |
|
|
}
|
186 : |
|
|
dctrl->sentOne(id);
|
187 : |
|
|
}
|
188 : |
|
|
|
189 : |
|
|
int NetInterface::getNumNotAns() {
|
190 : |
|
|
if (dctrl == NULL) {
|
191 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
192 : |
|
|
exit(EXIT_FAILURE);
|
193 : |
|
|
}
|
194 : |
|
|
return dctrl->getNumNotAnswered();
|
195 : |
|
|
}
|
196 : |
|
|
|
197 : |
|
|
int NetInterface::getNumDataItemsSet() {
|
198 : |
|
|
if (dctrl == NULL) {
|
199 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
200 : |
|
|
exit(EXIT_FAILURE);
|
201 : |
|
|
}
|
202 : |
|
|
return dctrl->getTotalSet();
|
203 : |
|
|
}
|
204 : |
|
|
|
205 : |
|
|
int NetInterface::getNumDataItemsAnswered() {
|
206 : |
|
|
if (dctrl == NULL) {
|
207 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
208 : |
|
|
exit(EXIT_FAILURE);
|
209 : |
|
|
}
|
210 : |
|
|
return dctrl->getNumAnswered();
|
211 : |
|
|
}
|
212 : |
|
|
|
213 : |
|
|
void NetInterface::setFirstAnsweredData() {
|
214 : |
|
|
if (dctrl == NULL) {
|
215 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
216 : |
|
|
exit(EXIT_FAILURE);
|
217 : |
|
|
}
|
218 : |
|
|
dctrl->setFirstAnswered();
|
219 : |
|
|
}
|
220 : |
|
|
|
221 : |
|
|
int NetInterface::getNumVarsInDataGroup() {
|
222 : |
|
|
return numVarInDataGroup;
|
223 : |
|
|
}
|
224 : |
|
|
|
225 : |
|
|
int NetInterface::getNumVarsInSendData() {
|
226 : |
|
|
return numVarToSend;
|
227 : |
|
|
}
|
228 : |
|
|
|
229 : |
|
|
int NetInterface::dataGroupFull() {
|
230 : |
|
|
if (dctrl == NULL) {
|
231 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
232 : |
|
|
exit(EXIT_FAILURE);
|
233 : |
|
|
}
|
234 : |
|
|
return dctrl->isFull();
|
235 : |
|
|
}
|
236 : |
|
|
|
237 : |
|
|
int NetInterface::allSent() {
|
238 : |
|
|
if (dctrl == NULL) {
|
239 : |
|
|
cerr << "Error in netInterface - no valid datagroup\n";
|
240 : |
|
|
exit(EXIT_FAILURE);
|
241 : |
|
|
}
|
242 : |
|
|
return dctrl->allSent();
|
243 : |
|
|
}
|
244 : |
|
|
|
245 : |
|
|
// ********************************************************
|
246 : |
|
|
// Input and output functions for data
|
247 : |
|
|
// ********************************************************
|
248 : |
|
|
void NetInterface::printResult(char* outputFileName) {
|
249 : |
|
|
ofstream outputfile;
|
250 : |
|
|
outputfile.open(outputFileName);
|
251 : |
|
|
if (!outputfile) {
|
252 : |
|
|
cerr << "Error in netinterface - cannot open " << outputFileName << endl;
|
253 : |
|
|
exit(EXIT_FAILURE);
|
254 : |
|
|
}
|
255 : |
|
|
if (dctrl == NULL) {
|
256 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
257 : |
|
|
exit(EXIT_FAILURE);
|
258 : |
|
|
}
|
259 : |
|
|
int numAnswers = dctrl->getTotalReceived();
|
260 : |
|
|
int counter = 0;
|
261 : |
|
|
while (counter < numAnswers) {
|
262 : |
|
|
if (dctrl->hasAnswer(counter)) {
|
263 : |
|
|
outputfile << (dctrl->getY(counter));
|
264 : |
|
|
outputfile << endl;
|
265 : |
|
|
counter++;
|
266 : |
|
|
}
|
267 : |
|
|
}
|
268 : |
|
|
outputfile.close();
|
269 : |
|
|
}
|
270 : |
|
|
|
271 : |
|
|
void NetInterface::printX(int id) {
|
272 : |
|
|
int i;
|
273 : |
|
|
DoubleVector temp;
|
274 : |
|
|
if (dctrl == NULL) {
|
275 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
276 : |
|
|
exit(EXIT_FAILURE);
|
277 : |
|
|
}
|
278 : |
|
|
temp = dctrl->getX(id);
|
279 : |
|
|
for (i = 0; i < temp.Size(); i++)
|
280 : |
|
|
cout << temp[i] << sep;
|
281 : |
|
|
cout << endl;
|
282 : |
|
|
}
|
283 : |
|
|
|
284 : |
|
|
void NetInterface::printXUnscaled(int id) {
|
285 : |
|
|
int i;
|
286 : |
|
|
if (dctrl == NULL) {
|
287 : |
|
|
cerr << "Error in netinterface - no valid datagroup\n";
|
288 : |
|
|
exit(EXIT_FAILURE);
|
289 : |
|
|
}
|
290 : |
|
|
DoubleVector vec;
|
291 : |
|
|
if (scaler != NULL) {
|
292 : |
|
|
// DoubleVector vecscaled = scaler->unscaleX(vec);
|
293 : |
|
|
// cout << scaler->unscaleX(dctrl->getX(id));
|
294 : |
|
|
//cout << vecscaled;
|
295 : |
|
|
vec = scaler->unscaleX(dctrl->getX(id));
|
296 : |
|
|
} else
|
297 : |
|
|
vec = dctrl->getX(id);
|
298 : |
|
|
for (i = 0; i < vec.Size(); i++)
|
299 : |
|
|
cout << vec[i] << sep;
|
300 : |
|
|
cout << endl;
|
301 : |
|
|
}
|
302 : |
|
|
|
303 : |
|
|
// ********************************************************
|
304 : |
|
|
// Functions for data converting and data scaling vectors
|
305 : |
|
|
// ********************************************************
|
306 : |
|
|
const DoubleVector& NetInterface::makeVector(const DoubleVector& vec) {
|
307 : |
|
|
int i;
|
308 : |
|
|
// must check if numvarindatagroup is correct and formula to make vector is correct!!!! also check if ok to set here DoubleVector temp and then return const DoubleVector& will it be a copy or not!!!!
|
309 : |
|
|
if (isAlpha == 1) {
|
310 : |
|
|
// DoubleVector tmp(numVarInDataGroup);
|
311 : |
|
|
for (i = 0; i < numVarInDataGroup; i++)
|
312 : |
|
|
alphaX_h[i] = alphaX[i] + (vec[0] * h[i]);
|
313 : |
|
|
return alphaX_h;
|
314 : |
|
|
} else
|
315 : |
|
|
return vec;
|
316 : |
|
|
}
|
317 : |
|
|
|
318 : |
|
|
const DoubleVector& NetInterface::unscaleX(const DoubleVector& vec) {
|
319 : |
|
|
if (scaler != NULL) {
|
320 : |
|
|
// xUnscale = scaler->unscaleX(vec);
|
321 : |
|
|
return scaler->unscaleX(vec);
|
322 : |
|
|
// return xUnscale;
|
323 : |
|
|
} else
|
324 : |
|
|
return vec;
|
325 : |
|
|
}
|
326 : |
|
|
|
327 : |
|
|
const DoubleVector& NetInterface::convertX(const DoubleVector& vec) {
|
328 : |
|
|
if (dataConvert != NULL) {
|
329 : |
|
|
// xConvert = dataConvert->convertX(vec);
|
330 : |
|
|
return dataConvert->convertX(vec);
|
331 : |
|
|
} else
|
332 : |
|
|
return vec;
|
333 : |
|
|
}
|
334 : |
|
|
|
335 : |
|
|
const DoubleVector& NetInterface::prepareVectorToSend(const DoubleVector& vec) {
|
336 : |
|
|
// xToSend = convertX(unscaleX(makeVector(vec)));
|
337 : |
|
|
return convertX(unscaleX(makeVector(vec)));
|
338 : |
|
|
}
|
339 : |
|
|
|
340 : |
|
|
const DoubleVector& NetInterface::getLowerbound() {
|
341 : |
|
|
assert(lowerBound.Size() != 0);
|
342 : |
|
|
return lowerBound;
|
343 : |
|
|
}
|
344 : |
|
|
|
345 : |
|
|
const DoubleVector& NetInterface::getUpperbound() {
|
346 : |
|
|
assert(upperBound.Size() != 0);
|
347 : |
|
|
return upperBound;
|
348 : |
|
|
}
|
349 : |
|
|
|
350 : |
|
|
const ParameterVector& NetInterface::getSwitches() {
|
351 : |
|
|
assert(switches.Size() != 0);
|
352 : |
|
|
return switches;
|
353 : |
|
|
}
|
354 : |
|
|
|
355 : |
|
|
const IntVector& NetInterface::getOptInfo() {
|
356 : |
|
|
int i;
|
357 : |
|
|
if (dataConvert != NULL)
|
358 : |
|
|
return dataConvert->getOptInfo();
|
359 : |
|
|
else {
|
360 : |
|
|
// no dataconverter, so all parameters must be optimised
|
361 : |
|
|
optVec.Reset();
|
362 : |
|
|
optVec.resize(numVarToSend, 1);
|
363 : |
|
|
return optVec;
|
364 : |
|
|
}
|
365 : |
|
|
}
|