1 : |
agomez |
1 |
#include "lineseeker.h" |
2 : |
|
|
|
3 : |
|
|
// ********************************************************
|
4 : |
|
|
// Functions for class lineseeker
|
5 : |
|
|
// ********************************************************
|
6 : |
|
|
LineSeeker::LineSeeker() {
|
7 : |
|
|
}
|
8 : |
|
|
|
9 : |
|
|
LineSeeker::~LineSeeker() {
|
10 : |
|
|
}
|
11 : |
|
|
|
12 : |
|
|
int LineSeeker::outstandingRequests() {
|
13 : |
|
|
int pending = net->getNumNotAns();
|
14 : |
|
|
return pending;
|
15 : |
|
|
}
|
16 : |
|
|
|
17 : |
|
|
/* In the initial version of Hooke and Jeeves, the point xnew+d is */
|
18 : |
|
|
/* checked after each run of bestNearby, where d=xnew-xold. That is, */
|
19 : |
|
|
/* it's tried to go a bit further in the direction that improves the */
|
20 : |
|
|
/* function value. Here, some more points on the line through xnew and */
|
21 : |
|
|
/* xold are tried as most of the computers would just be waiting during */
|
22 : |
|
|
/* this time anyway. These tries depend on the number of free computers */
|
23 : |
|
|
/* Nr. of computers: Tries made: */
|
24 : |
|
|
/* 1.....................xnew+d */
|
25 : |
|
|
/* 2.....................xnew+d, xnew+2*d */
|
26 : |
|
|
/* 3.....................xnew+d, xnew+3/2*d, xnew+2*d */
|
27 : |
|
|
/* 4.....................xnew+1/2*d, xnew+d, xnew+3/2*d, xnew+2*d */
|
28 : |
|
|
/* 5.....................xnew-1/2*d, xnew+1/2*d, xnew+d, xnew+3/2*d, */
|
29 : |
|
|
/* xnew+2*d */
|
30 : |
|
|
/* 6.....................xnew-1/2*d, xnew+1/2*d, xnew+d, xnew+3/2*d, */
|
31 : |
|
|
/* xnew+2*d, xnew+4*d */
|
32 : |
|
|
/* 7.....................xnew-1/2*d, xnew+1/2*d, xnew+d, xnew+3/2*d, */
|
33 : |
|
|
/* xnew+2*d, xnew+4*d, xnew+8*d */
|
34 : |
|
|
/* and so further. (THLTH 29.08.01) */
|
35 : |
|
|
int LineSeeker::doLineseek(const DoubleVector& xold, const DoubleVector& xnew,
|
36 : |
|
|
double fnew, NetInterface *netI) {
|
37 : |
|
|
|
38 : |
|
|
int i, j, k, l;
|
39 : |
|
|
int numSendReceive, numDataItems;
|
40 : |
|
|
|
41 : |
|
|
net = netI;
|
42 : |
|
|
int numvar = net->getNumVarsInDataGroup();
|
43 : |
|
|
int numberOfHosts = net->getNumFreeProcesses();
|
44 : |
|
|
if (numberOfHosts == 0)
|
45 : |
|
|
numberOfHosts = 1;
|
46 : |
|
|
|
47 : |
|
|
DoubleVector d(numvar);
|
48 : |
|
|
DoubleVector z(numvar);
|
49 : |
|
|
for (i = 0; i < numvar; i++)
|
50 : |
|
|
d[i] = xnew[i] - xold[i];
|
51 : |
|
|
DoubleVector upper(netI->getUpperScaleConstant());
|
52 : |
|
|
DoubleVector lower(netI->getLowerScaleConstant());
|
53 : |
|
|
f = fnew;
|
54 : |
|
|
x = xnew;
|
55 : |
|
|
|
56 : |
|
|
net->startNewDataGroup(numberOfHosts);
|
57 : |
|
|
if (numberOfHosts < 5) {
|
58 : |
|
|
l = 1;
|
59 : |
|
|
for (i = 0; i < numvar; i++) {
|
60 : |
|
|
z[i] = xnew[i] + d[i];
|
61 : |
|
|
if ((z[i] < lower[i]) || (z[i] > upper[i]))
|
62 : |
|
|
l = 0;
|
63 : |
|
|
}
|
64 : |
|
|
if (l)
|
65 : |
|
|
net->setX(z);
|
66 : |
|
|
|
67 : |
|
|
for (j = 1; j < numberOfHosts; j++) {
|
68 : |
|
|
for (i = 0; i < numvar; i++) {
|
69 : |
|
|
z[i] = xnew[i] + (2 - 1 / 2 * (j - 1) - 1 / 4 * (j - 1) * (j - 2)) * d[i];
|
70 : |
|
|
if ((z[i] < lower[i]) || (z[i] > upper[i]))
|
71 : |
|
|
l = 0;
|
72 : |
|
|
}
|
73 : |
|
|
if (l)
|
74 : |
|
|
net->setX(z);
|
75 : |
|
|
else
|
76 : |
|
|
break;
|
77 : |
|
|
}
|
78 : |
|
|
|
79 : |
|
|
} else {
|
80 : |
|
|
l = 1;
|
81 : |
|
|
for (i = 0; i < numvar; i++) {
|
82 : |
|
|
z[i] = xnew[i] - 1 / 2 * d[i];
|
83 : |
|
|
if ((z[i] < lower[i]) || (z[i] > upper[i]))
|
84 : |
|
|
l = 0;
|
85 : |
|
|
}
|
86 : |
|
|
if (l)
|
87 : |
|
|
net->setX(z);
|
88 : |
|
|
|
89 : |
|
|
for (j = 1; j < 4; j++) {
|
90 : |
|
|
for (i = 0; i < numvar; i++) {
|
91 : |
|
|
z[i] = xnew[i] + 1 / 2 * j * d[i];
|
92 : |
|
|
if ((z[i] < lower[i]) || (z[i] > upper[i]))
|
93 : |
|
|
l = 0;
|
94 : |
|
|
}
|
95 : |
|
|
if (l)
|
96 : |
|
|
net->setX(z);
|
97 : |
|
|
else
|
98 : |
|
|
break;
|
99 : |
|
|
}
|
100 : |
|
|
|
101 : |
|
|
k = 2;
|
102 : |
|
|
for (j = 4; j < numberOfHosts; j++) {
|
103 : |
|
|
for (i = 0; i < numvar; i++) {
|
104 : |
|
|
z[i] = xnew[i] + k * d[i];
|
105 : |
|
|
if ((z[i] < lower[i]) || (z[i] > upper[i]))
|
106 : |
|
|
l = 0;
|
107 : |
|
|
}
|
108 : |
|
|
if (l)
|
109 : |
|
|
net->setX(z);
|
110 : |
|
|
else
|
111 : |
|
|
break;
|
112 : |
|
|
k = 2 * k;
|
113 : |
|
|
}
|
114 : |
|
|
}
|
115 : |
|
|
|
116 : |
|
|
numSendReceive = net->sendAndReceiveAllData();
|
117 : |
|
|
if (numSendReceive == net->netError()) {
|
118 : |
|
|
cerr << "Error in lineseeker - could not receive data\n";
|
119 : |
|
|
net->stopUsingDataGroup();
|
120 : |
|
|
exit(EXIT_FAILURE);
|
121 : |
|
|
|
122 : |
|
|
} else if (numSendReceive == net->netSuccess()) {
|
123 : |
|
|
numDataItems = net->getNumDataItemsAnswered();
|
124 : |
|
|
for (i = 0; i < numDataItems; i++) {
|
125 : |
|
|
if (net->getY(i) < f) {
|
126 : |
|
|
f = net->getY(i);
|
127 : |
|
|
x = net->getX(i);
|
128 : |
|
|
}
|
129 : |
|
|
}
|
130 : |
|
|
net->stopUsingDataGroup();
|
131 : |
|
|
|
132 : |
|
|
} else {
|
133 : |
|
|
cerr << "Error in lineseeker - could not receive data\n";
|
134 : |
|
|
net->stopNetComm();
|
135 : |
|
|
net->stopUsingDataGroup();
|
136 : |
|
|
exit(EXIT_FAILURE);
|
137 : |
|
|
}
|
138 : |
|
|
return numDataItems;
|
139 : |
|
|
}
|