| 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 |
package dr.math; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class LineFunction implements UnivariateFunction |
| 34 |
{ |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public LineFunction(MultivariateFunction func) |
| 43 |
{ |
| 44 |
f = func; |
| 45 |
|
| 46 |
dim = f.getNumArguments(); |
| 47 |
|
| 48 |
x = new double[dim]; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public void update(double[] start, double[] dir) |
| 59 |
{ |
| 60 |
s = start; |
| 61 |
d = dir; |
| 62 |
|
| 63 |
computeBounds(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public void getPoint(double lambda, double[] p) |
| 75 |
{ |
| 76 |
for (int i = 0; i < dim; i++) |
| 77 |
{ |
| 78 |
p[i] = s[i] + lambda*d[i]; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public double evaluate(double lambda) |
| 89 |
{ |
| 90 |
getPoint(lambda, x); |
| 91 |
|
| 92 |
return f.evaluate(x); |
| 93 |
} |
| 94 |
|
| 95 |
public double getLowerBound() |
| 96 |
{ |
| 97 |
return lowerBound; |
| 98 |
} |
| 99 |
|
| 100 |
public double getUpperBound() |
| 101 |
{ |
| 102 |
return upperBound; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public double findMinimum() |
| 116 |
{ |
| 117 |
if (um == null) |
| 118 |
{ |
| 119 |
um = new UnivariateMinimum(); |
| 120 |
} |
| 121 |
return um.findMinimum(this); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public int getUpperBoundParameter() |
| 131 |
{ |
| 132 |
return upperBoundParam; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public int getLowerBoundParameter() |
| 141 |
{ |
| 142 |
return lowerBoundParam; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public boolean checkPoint(double[] p) |
| 155 |
{ |
| 156 |
boolean modified = false; |
| 157 |
for (int i = 0; i < dim; i++) |
| 158 |
{ |
| 159 |
if (p[i] < f.getLowerBound(i)) |
| 160 |
{ |
| 161 |
p[i] = f.getLowerBound(i); |
| 162 |
modified = true; |
| 163 |
} |
| 164 |
if (p[i] > f.getUpperBound(i)) |
| 165 |
{ |
| 166 |
p[i] = f.getUpperBound(i); |
| 167 |
modified = true; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return modified; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public int checkVariables(double[] p, double[] grad, boolean[] active) |
| 187 |
{ |
| 188 |
|
| 189 |
double EPS = MachineAccuracy.SQRT_EPSILON; |
| 190 |
|
| 191 |
int numActive = 0; |
| 192 |
for (int i = 0; i < dim; i++) |
| 193 |
{ |
| 194 |
active[i] = true; |
| 195 |
if (p[i] <= f.getLowerBound(i)+EPS) |
| 196 |
{ |
| 197 |
|
| 198 |
if (grad[i] > 0) |
| 199 |
{ |
| 200 |
active[i] = false; |
| 201 |
} |
| 202 |
} |
| 203 |
else if (p[i] >= f.getUpperBound(i)-EPS) |
| 204 |
{ |
| 205 |
|
| 206 |
if (grad[i] < 0) |
| 207 |
{ |
| 208 |
active[i] = false; |
| 209 |
} |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
numActive++; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return numActive; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public int checkDirection(double[] p, double[] dir) |
| 231 |
{ |
| 232 |
|
| 233 |
double EPS = MachineAccuracy.SQRT_EPSILON; |
| 234 |
|
| 235 |
int numChanged = 0; |
| 236 |
for (int i = 0; i < dim; i++) |
| 237 |
{ |
| 238 |
if (p[i] <= f.getLowerBound(i)+EPS) |
| 239 |
{ |
| 240 |
|
| 241 |
if (dir[i] < 0) |
| 242 |
{ |
| 243 |
dir[i] = 0; |
| 244 |
numChanged++; |
| 245 |
} |
| 246 |
} |
| 247 |
else if (p[i] >= f.getUpperBound(i)-EPS) |
| 248 |
{ |
| 249 |
|
| 250 |
if (dir[i] > 0) |
| 251 |
{ |
| 252 |
dir[i] = 0; |
| 253 |
numChanged++; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
return numChanged; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
private MultivariateFunction f; |
| 267 |
private int lowerBoundParam, upperBoundParam; |
| 268 |
private int dim; |
| 269 |
private double lowerBound, upperBound; |
| 270 |
private double[] s, d, x; |
| 271 |
private UnivariateMinimum um = null; |
| 272 |
|
| 273 |
private void computeBounds() |
| 274 |
{ |
| 275 |
boolean firstVisit = true; |
| 276 |
for (int i = 0; i < dim; i++) |
| 277 |
{ |
| 278 |
if (d[i] != 0) |
| 279 |
{ |
| 280 |
double upper = (f.getUpperBound(i) - s[i])/d[i]; |
| 281 |
double lower = (f.getLowerBound(i) - s[i])/d[i]; |
| 282 |
if (lower > upper) |
| 283 |
{ |
| 284 |
double tmp = upper; |
| 285 |
upper = lower; |
| 286 |
lower = tmp; |
| 287 |
} |
| 288 |
|
| 289 |
if (firstVisit) |
| 290 |
{ |
| 291 |
lowerBound = lower; |
| 292 |
lowerBoundParam = i; |
| 293 |
upperBound = upper; |
| 294 |
upperBoundParam = i; |
| 295 |
firstVisit = false; |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
if (lower > lowerBound) |
| 300 |
{ |
| 301 |
lowerBound = lower; |
| 302 |
lowerBoundParam = i; |
| 303 |
} |
| 304 |
if (upper < upperBound) |
| 305 |
{ |
| 306 |
upperBound = upper; |
| 307 |
upperBoundParam = i; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
} |