| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Trilinos: An Object-Oriented Solver Framework |
| 4 |
// |
| 5 |
// Copyright 2001-2024 NTESS and the Trilinos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
// 2016/09/23 |
| 11 |
// Automatic Discontinuity Detection using Spline Extrapolation |
| 12 |
|
| 13 |
#include "VPS.hpp" |
| 14 |
|
| 15 |
VPS::VPS() |
| 16 |
{ |
| 17 |
/////////// kd-tree Variables ///////////////////////////////////////////////// |
| 18 |
_tree_left = 0; _tree_right = 0; |
| 19 |
|
| 20 |
/////////// Delaunay Graph Variables ////////////////////////////////////////// |
| 21 |
_seed_box = 0; _seed_rf = 0; _seed_rc = 0; _seed_neighbors = 0; |
| 22 |
|
| 23 |
_seed_disc_neighbors = 0; // ETP |
| 24 |
|
| 25 |
/////////// Surrogate Variables ////////////////////////////////////////// |
| 26 |
_p = 0; _basis_coef = 0; _basis_index = 0; |
| 27 |
|
| 28 |
initiate_random_number_generator(1234567890); |
| 29 |
|
| 30 |
size_t seed(size_t(time(0))); |
| 31 |
seed = 1234567890; |
| 32 |
initiate_random_number_generator(seed); |
| 33 |
//std::cout << "RNG seed = " << seed << std::endl; |
| 34 |
_num_vs = 0; |
| 35 |
} |
| 36 |
|
| 37 |
VPS::~VPS() |
| 38 |
{ |
| 39 |
clear_memory(); |
| 40 |
} |
| 41 |
|
| 42 |
int VPS::clear_memory() |
| 43 |
{ |
| 44 |
/////////// kd-tree Variables ///////////////////////////////////////////////// |
| 45 |
_tree_max_height = 0; |
| 46 |
if (_tree_left != 0){ delete[] _tree_left; _tree_left = 0; } |
| 47 |
if (_tree_right != 0){ delete[] _tree_right; _tree_right = 0; } |
| 48 |
|
| 49 |
/////////// Delaunay Graph Variables ////////////////////////////////////////// |
| 50 |
if (_seed_box != 0) |
| 51 |
{ |
| 52 |
for (size_t i = 0; i < _num_samples; i++) delete[] _seed_box[i]; |
| 53 |
delete[] _seed_box; |
| 54 |
_seed_box = 0; |
| 55 |
} |
| 56 |
if (_seed_rf != 0){ delete[] _seed_rf; _seed_rf = 0; } |
| 57 |
if (_seed_rc != 0){ delete[] _seed_rc; _seed_rc = 0; } |
| 58 |
|
| 59 |
if (_seed_neighbors != 0) |
| 60 |
{ |
| 61 |
for (size_t i = 0; i < _num_samples; i++) delete[] _seed_neighbors[i]; |
| 62 |
delete[] _seed_neighbors; |
| 63 |
_seed_neighbors = 0; |
| 64 |
} |
| 65 |
if (_seed_disc_neighbors != 0) |
| 66 |
{ |
| 67 |
for (size_t iseed = 0; iseed < _num_samples; iseed++) |
| 68 |
{ |
| 69 |
if (_seed_disc_neighbors[iseed] != 0) |
| 70 |
{ |
| 71 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) |
| 72 |
{ |
| 73 |
if (_seed_disc_neighbors[iseed][ifunc] != 0) delete[] _seed_disc_neighbors[iseed][ifunc]; |
| 74 |
} |
| 75 |
delete[] _seed_disc_neighbors[iseed]; |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
delete[] _seed_disc_neighbors; |
| 80 |
_seed_disc_neighbors = 0; |
| 81 |
} |
| 82 |
|
| 83 |
/////////// Surrogate Variables ////////////////////////////////////////// |
| 84 |
if (_p != 0) |
| 85 |
{ |
| 86 |
for (size_t ibasis = 0; ibasis < _num_basis_pool; ibasis++) delete[] _p[ibasis]; |
| 87 |
delete[] _p; |
| 88 |
_p = 0; |
| 89 |
} |
| 90 |
|
| 91 |
if (_basis_coef != 0) |
| 92 |
{ |
| 93 |
for (size_t i = 0; i < _num_samples; i++) |
| 94 |
{ |
| 95 |
for (size_t j = 0; j < _num_functions; j++) delete[] _basis_coef[i][j]; |
| 96 |
delete[] _basis_coef[i]; |
| 97 |
} |
| 98 |
delete[] _basis_coef; |
| 99 |
_basis_coef = 0; |
| 100 |
} |
| 101 |
|
| 102 |
if (_basis_index != 0) |
| 103 |
{ |
| 104 |
for (size_t i = 0; i < _num_samples; i++) |
| 105 |
{ |
| 106 |
for (size_t j = 0; j < _num_functions; j++) delete[] _basis_index[i][j]; |
| 107 |
delete[] _basis_index[i]; |
| 108 |
} |
| 109 |
delete[] _basis_index; |
| 110 |
_basis_index = 0; |
| 111 |
} |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 118 |
/////////// VPS Public Methods ////////////////////////////////////////////////////////////////////////////////// |
| 119 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 120 |
|
| 121 |
int VPS::get_initial_well_spaced_samples(size_t num_dim, double* xmin, double* xmax, size_t num_samples, double** x) |
| 122 |
{ |
| 123 |
double r_sq(0.0); |
| 124 |
for (size_t idim = 0; idim < num_dim; idim++) |
| 125 |
{ |
| 126 |
double dx = xmax[idim] - xmin[idim]; |
| 127 |
r_sq += dx * dx; |
| 128 |
} |
| 129 |
|
| 130 |
size_t isample(0); |
| 131 |
size_t num_successive_misses(0), max_num_successive_misses(100); |
| 132 |
while (isample < num_samples) |
| 133 |
{ |
| 134 |
for (size_t idim = 0; idim < num_dim; idim++) x[isample][idim] = xmin[idim] + generate_a_random_number() * (xmax[idim] - xmin[idim]); |
| 135 |
bool miss(false); |
| 136 |
for (size_t i = 0; i < isample; i++) |
| 137 |
{ |
| 138 |
double dst_sq(0.0); |
| 139 |
for (size_t idim = 0; idim < num_dim; idim++) |
| 140 |
{ |
| 141 |
double dx = x[i][idim] - x[isample][idim]; |
| 142 |
dst_sq += dx * dx; |
| 143 |
} |
| 144 |
if (dst_sq < r_sq) |
| 145 |
{ |
| 146 |
miss = true; break; |
| 147 |
} |
| 148 |
} |
| 149 |
if (miss) num_successive_misses++; |
| 150 |
else |
| 151 |
{ |
| 152 |
num_successive_misses = 0; |
| 153 |
isample++; |
| 154 |
} |
| 155 |
|
| 156 |
if (num_successive_misses == max_num_successive_misses) |
| 157 |
{ |
| 158 |
num_successive_misses = 0; |
| 159 |
r_sq *= 0.81; |
| 160 |
} |
| 161 |
} |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
int VPS::build_surrogate(size_t num_dim, |
| 166 |
double* xmin, double* xmax, |
| 167 |
size_t num_functions, |
| 168 |
surrogate_method method, |
| 169 |
surrogate_basis basis, size_t desired_order, |
| 170 |
size_t budget, size_t num_samples, |
| 171 |
double** x, |
| 172 |
double** f, |
| 173 |
double*** g, |
| 174 |
double**** h |
| 175 |
) |
| 176 |
{ |
| 177 |
clear_memory(); |
| 178 |
|
| 179 |
_num_dim = num_dim; _xmin = xmin; _xmax = xmax; |
| 180 |
_num_functions = num_functions; _method = method; _basis = basis; |
| 181 |
_desired_order = desired_order; _budget = budget; _num_samples = num_samples; |
| 182 |
_x = x; _f = f; _g = g; _h = h; |
| 183 |
|
| 184 |
find_domain_diagonal(); |
| 185 |
|
| 186 |
// 1. Build kd-tree |
| 187 |
kd_tree_build_balanced(); |
| 188 |
|
| 189 |
// 2. Construct Delaunay Graph |
| 190 |
if (desired_order > 0) construct_delaunay_graph(); |
| 191 |
|
| 192 |
//plot_delaunay_graph("graph_nodisc.ps"); |
| 193 |
|
| 194 |
// 3. Detect Discontinuites |
| 195 |
//detect_discontinuities(); |
| 196 |
|
| 197 |
//plot_delaunay_graph("graph_disc.ps"); |
| 198 |
|
| 199 |
// 3. Construct local surrogates |
| 200 |
construct_local_surrogates(); |
| 201 |
|
| 202 |
|
| 203 |
std::string name_1; |
| 204 |
std::string name_2; |
| 205 |
|
| 206 |
if (true) |
| 207 |
{ |
| 208 |
std::stringstream sstm; |
| 209 |
sstm << "vps_qoi_" << _num_samples << ".ps"; |
| 210 |
name_1 = sstm.str(); |
| 211 |
} |
| 212 |
if (true) |
| 213 |
{ |
| 214 |
std::stringstream sstm; |
| 215 |
sstm << "vps_nit_" << _num_samples << ".ps"; |
| 216 |
name_2 = sstm.str(); |
| 217 |
} |
| 218 |
|
| 219 |
if (_num_samples > 1190 && _num_samples < 2100) |
| 220 |
{ |
| 221 |
//plot_vps_surrogate("vps_2d_qoi.ps", 0, 21, true); |
| 222 |
//plot_vps_surrogate("vps_2d_nit.ps", 1, 21, true); |
| 223 |
// plot_vps_frames(name_1, 0, 11, 11, 21, false); |
| 224 |
// plot_vps_frames(name_2, 1, 11, 11, 21, false); |
| 225 |
} |
| 226 |
|
| 227 |
//plot_vps_frames("vps_surrogate_r.ps", 0, 11, 11, 21, false); |
| 228 |
//plot_vps_frames("vps_surrogate_g.ps", 1, 11, 11, 21, false); |
| 229 |
//plot_vps_frames("vps_surrogate_b.ps", 2, 5, 5, 21, false); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
int VPS::evaluate_surrogate(double* x, double* fs) |
| 234 |
{ |
| 235 |
double closest_distance = _diag; size_t closest_seed; |
| 236 |
get_closest_seed_tree(x, closest_seed, closest_distance); |
| 237 |
evaluate_surrogate(closest_seed, x, fs); |
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
int VPS::evaluate_surrogate(size_t cell_index, double* x, double* fs) |
| 242 |
{ |
| 243 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) |
| 244 |
{ |
| 245 |
fs[ifunc] = 0.0; |
| 246 |
|
| 247 |
if (_desired_order == 0) |
| 248 |
{ |
| 249 |
fs[ifunc] = _f[cell_index][ifunc]; |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
for (size_t ibasis = 0; ibasis < _num_basis; ibasis++) |
| 254 |
{ |
| 255 |
size_t basis_index = _basis_index[cell_index][ifunc][ibasis]; |
| 256 |
fs[ifunc] += _basis_coef[cell_index][ifunc][ibasis] * evaluate_basis_function(x, cell_index, basis_index); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
int VPS::suggest_new_sample(double* x, double &r, double &err_est) |
| 264 |
{ |
| 265 |
double* xmc = new double[_num_dim]; |
| 266 |
for (size_t idim = 0; idim < _num_dim; idim++) xmc[idim] = _xmin[idim] + generate_a_random_number() * (_xmax[idim] - _xmin[idim]); |
| 267 |
|
| 268 |
size_t closest_seed; double dst(DBL_MAX); |
| 269 |
get_closest_seed_tree(xmc, closest_seed, dst); |
| 270 |
|
| 271 |
if (_num_samples < 5 * _num_dim) |
| 272 |
{ |
| 273 |
// find distance to closest boundary |
| 274 |
double h_dim; double h_shortest(DBL_MAX); |
| 275 |
|
| 276 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 277 |
{ |
| 278 |
h_dim = fabs(xmc[idim] - _xmin[idim]); |
| 279 |
if (h_dim < h_shortest) h_shortest = h_dim; |
| 280 |
|
| 281 |
h_dim = fabs(xmc[idim] - _xmax[idim]); |
| 282 |
if (h_dim < h_shortest) h_shortest = h_dim; |
| 283 |
} |
| 284 |
|
| 285 |
// error estimate = min(distance to closest seed, distance to closest boundary) |
| 286 |
r = dst; |
| 287 |
if (h_shortest < dst) dst = h_shortest; |
| 288 |
|
| 289 |
for (size_t idim = 0; idim < _num_dim; idim++) x[idim] = xmc[idim]; |
| 290 |
err_est = dst; |
| 291 |
} |
| 292 |
else |
| 293 |
{ |
| 294 |
size_t closest_seed; |
| 295 |
double dst(DBL_MAX); |
| 296 |
get_closest_seed_tree(xmc, closest_seed, dst); |
| 297 |
sample_voronoi_vertex(closest_seed, _xmin, _xmax, _diag, x); |
| 298 |
|
| 299 |
r = 0.0; |
| 300 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 301 |
{ |
| 302 |
double dx = _x[closest_seed][idim] - x[idim]; |
| 303 |
r += dx * dx; |
| 304 |
} |
| 305 |
r = sqrt(r); |
| 306 |
|
| 307 |
size_t capacity = 10; |
| 308 |
size_t* neighbor_seeds = new size_t[capacity]; |
| 309 |
size_t num_neighbor_seeds = 0; |
| 310 |
get_seeds_in_sphere_tree(x, r + 1E-10, num_neighbor_seeds, neighbor_seeds, capacity); |
| 311 |
|
| 312 |
if (num_neighbor_seeds == 1) |
| 313 |
{ |
| 314 |
// switch to neighbors of closest seed |
| 315 |
num_neighbor_seeds = _seed_neighbors[closest_seed][1] + 1; |
| 316 |
neighbor_seeds[0] = closest_seed; |
| 317 |
for (size_t j = 1; j < num_neighbor_seeds; j++) |
| 318 |
{ |
| 319 |
neighbor_seeds[j] = _seed_neighbors[closest_seed][2 + j]; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
double* fneighbor = new double[num_neighbor_seeds]; |
| 324 |
double* fs = new double[_num_functions]; |
| 325 |
for (size_t i = 0; i < num_neighbor_seeds; i++) |
| 326 |
{ |
| 327 |
size_t seed = neighbor_seeds[i]; |
| 328 |
evaluate_surrogate(seed, x, fs); |
| 329 |
fneighbor[i] = fs[0]; |
| 330 |
} |
| 331 |
|
| 332 |
err_est = 0.0; |
| 333 |
for (size_t i = 0; i < num_neighbor_seeds; i++) |
| 334 |
{ |
| 335 |
for (size_t j = i + 1; j < num_neighbor_seeds; j++) |
| 336 |
{ |
| 337 |
double df = fabs(fneighbor[i] - fneighbor[j]); |
| 338 |
if (df > err_est) err_est = df; |
| 339 |
} |
| 340 |
} |
| 341 |
err_est *= pow(r, _num_dim); |
| 342 |
|
| 343 |
delete[] fneighbor; |
| 344 |
delete[] fs; |
| 345 |
delete[] neighbor_seeds; |
| 346 |
} |
| 347 |
|
| 348 |
delete[]xmc; |
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
int VPS::get_ensemble(size_t num_ensemble_points, double** ensemble_points, |
| 353 |
int proc_rank) |
| 354 |
{ |
| 355 |
double* ensemble_err = new double[num_ensemble_points]; |
| 356 |
double* ensemble_nit = new double[num_ensemble_points]; |
| 357 |
double* ensemble_r = new double[num_ensemble_points]; |
| 358 |
|
| 359 |
// collect points based on WS, Error Estimate, and min variance in nit: |
| 360 |
double* x = new double[_num_dim]; double r, err_est; |
| 361 |
|
| 362 |
double* fs = new double[_num_functions]; |
| 363 |
|
| 364 |
size_t num_points(0); |
| 365 |
size_t num_misses(0), max_num_misses(100); |
| 366 |
|
| 367 |
double nit_av(DBL_MAX), nit_max(0.0), err_av(0.0), err_max(0.0); |
| 368 |
|
| 369 |
bool improve_error_only(true); |
| 370 |
while (num_misses < max_num_misses) |
| 371 |
{ |
| 372 |
suggest_new_sample(x, r, err_est); |
| 373 |
|
| 374 |
// make sure this point is outside of ensemble spheres |
| 375 |
bool conflict(false); |
| 376 |
for (size_t ipoint = 0; ipoint < num_points; ipoint++) |
| 377 |
{ |
| 378 |
double dstsq(0.0); |
| 379 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 380 |
{ |
| 381 |
double dx = x[idim] - ensemble_points[ipoint][idim]; |
| 382 |
dstsq += dx * dx; |
| 383 |
} |
| 384 |
if (dstsq < r * r || dstsq < ensemble_r[ipoint] * ensemble_r[ipoint]) |
| 385 |
{ |
| 386 |
conflict = true; break; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
if (conflict) { num_misses++; continue; } // Violation of Wellspasedness condition |
| 391 |
|
| 392 |
evaluate_surrogate(x, fs); |
| 393 |
|
| 394 |
size_t iloc(num_ensemble_points); |
| 395 |
if (num_points < num_ensemble_points) iloc = num_points; |
| 396 |
|
| 397 |
if (iloc == num_ensemble_points) |
| 398 |
{ |
| 399 |
// try replacing all prior points |
| 400 |
for (size_t jloc = 0; jloc < num_points; jloc++) |
| 401 |
{ |
| 402 |
// attempt same/better efficiency and better accuracy |
| 403 |
double new_nit_av(0.0), new_nit_max(0.0), new_err_av(0.0), new_err_max(0.0); |
| 404 |
for (size_t ipoint = 0; ipoint < num_points; ipoint++) |
| 405 |
{ |
| 406 |
double nit = ensemble_nit[ipoint]; double err = ensemble_err[ipoint]; |
| 407 |
if (ipoint == jloc) |
| 408 |
{ |
| 409 |
nit = fs[1]; err = err_est; |
| 410 |
} |
| 411 |
new_nit_av += nit; |
| 412 |
new_err_av += err; |
| 413 |
|
| 414 |
if (nit > new_nit_max) new_nit_max = nit; |
| 415 |
if (err > new_err_max) new_err_max = err; |
| 416 |
} |
| 417 |
new_nit_av /= num_points; |
| 418 |
new_err_av /= num_points; |
| 419 |
|
| 420 |
if (improve_error_only) |
| 421 |
{ |
| 422 |
if (new_err_av > err_av) |
| 423 |
{ |
| 424 |
iloc = jloc; |
| 425 |
break; |
| 426 |
} |
| 427 |
} |
| 428 |
else |
| 429 |
{ |
| 430 |
double oldR = nit_max / nit_av; |
| 431 |
double newR = new_nit_max / new_nit_av; |
| 432 |
if (newR < oldR && new_err_av > 0.8 * err_av) // better effeciency at least same accuracy |
| 433 |
{ |
| 434 |
iloc = jloc; break; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
if (iloc < num_ensemble_points) |
| 441 |
{ |
| 442 |
// Add point to ensemble |
| 443 |
num_misses = 0; |
| 444 |
for (size_t idim = 0; idim < _num_dim; idim++) ensemble_points[iloc][idim] = x[idim]; |
| 445 |
ensemble_nit[iloc] = fs[1]; |
| 446 |
ensemble_err[iloc] = err_est; |
| 447 |
ensemble_r[iloc] = r; |
| 448 |
if (iloc == num_points) num_points++; |
| 449 |
|
| 450 |
// update error and nit bounds |
| 451 |
nit_av = 0.0; nit_max = 0.0; |
| 452 |
if (improve_error_only) |
| 453 |
{ |
| 454 |
err_av = 0.0; err_max = 0.0; // update error metrics |
| 455 |
} |
| 456 |
|
| 457 |
for (size_t ipoint = 0; ipoint < num_points; ipoint++) |
| 458 |
{ |
| 459 |
if (ensemble_nit[ipoint] > nit_max) nit_max = ensemble_nit[ipoint]; |
| 460 |
nit_av += ensemble_nit[ipoint]; |
| 461 |
|
| 462 |
if (improve_error_only) |
| 463 |
{ |
| 464 |
if (ensemble_err[ipoint] > err_max) err_max = ensemble_err[ipoint]; |
| 465 |
err_av += ensemble_err[ipoint]; |
| 466 |
} |
| 467 |
} |
| 468 |
nit_av /= num_points; |
| 469 |
if (improve_error_only) err_av /= num_points; |
| 470 |
} |
| 471 |
else num_misses++; |
| 472 |
|
| 473 |
if (num_misses == max_num_misses && improve_error_only) |
| 474 |
{ |
| 475 |
num_misses = 0; improve_error_only = false; |
| 476 |
max_num_misses = 1000; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
delete[] x; delete[] fs; |
| 481 |
|
| 482 |
delete[] ensemble_err; delete[] ensemble_nit; delete[] ensemble_r; |
| 483 |
|
| 484 |
if (proc_rank == 0) |
| 485 |
std::cout << "Number of samples = " << _num_samples << " , Ensemble expected R = " << nit_max / nit_av << std::endl; |
| 486 |
return 0; |
| 487 |
} |
| 488 |
|
| 489 |
int VPS::get_stats(size_t num_mc_points, size_t function_index, double& mean, double& var) |
| 490 |
{ |
| 491 |
double* xs = new double[_num_dim]; |
| 492 |
double* fs = new double[_num_functions]; |
| 493 |
|
| 494 |
mean = 0.0; |
| 495 |
for (size_t imc = 0; imc < num_mc_points; imc++) |
| 496 |
{ |
| 497 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 498 |
{ |
| 499 |
xs[idim] = _xmin[idim] + generate_a_random_number()*(_xmax[idim] - _xmin[idim]); |
| 500 |
} |
| 501 |
evaluate_surrogate(xs, fs); |
| 502 |
mean += fs[function_index]; |
| 503 |
} |
| 504 |
mean /= num_mc_points; |
| 505 |
|
| 506 |
var = 0.0; |
| 507 |
for (size_t imc = 0; imc < num_mc_points; imc++) |
| 508 |
{ |
| 509 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 510 |
{ |
| 511 |
xs[idim] = _xmin[idim] + generate_a_random_number()*(_xmax[idim] - _xmin[idim]); |
| 512 |
} |
| 513 |
evaluate_surrogate(xs, fs); |
| 514 |
var += (fs[function_index] - mean) * (fs[function_index] - mean); |
| 515 |
} |
| 516 |
var /= num_mc_points; |
| 517 |
|
| 518 |
delete[] xs; delete[] fs; |
| 519 |
return 0; |
| 520 |
} |
| 521 |
|
| 522 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 523 |
/////////// RNG ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 524 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 525 |
|
| 526 |
void VPS::initiate_random_number_generator(unsigned long x) |
| 527 |
{ |
| 528 |
//assert(sizeof (double) >= 54) ; |
| 529 |
|
| 530 |
cc = 1.0 / 9007199254740992.0; // inverse of 2^53rd power |
| 531 |
size_t i; |
| 532 |
size_t qlen = indx = sizeof Q / sizeof Q[0]; |
| 533 |
for (i = 0; i < qlen; i++) Q[i] = 0; |
| 534 |
|
| 535 |
c = 0.0; /* current CSWB */ |
| 536 |
zc = 0.0; /* current SWB `borrow` */ |
| 537 |
zx = 5212886298506819.0 / 9007199254740992.0; /* SWB seed1 */ |
| 538 |
zy = 2020898595989513.0 / 9007199254740992.0; /* SWB seed2 */ |
| 539 |
|
| 540 |
size_t j; |
| 541 |
double s, t; /* Choose 32 bits for x, 32 for y */ |
| 542 |
if (x == 0) x = 123456789; /* default seeds */ |
| 543 |
unsigned long y = 362436069; /* default seeds */ |
| 544 |
|
| 545 |
/* Next, seed each Q[i], one bit at a time, */ |
| 546 |
for (i = 0; i < qlen; i++) |
| 547 |
{ /* using 9th bit from Cong+Xorshift */ |
| 548 |
s = 0.0; |
| 549 |
t = 1.0; |
| 550 |
for (j = 0; j < 52; j++) |
| 551 |
{ |
| 552 |
t = 0.5 * t; /* make t=.5/2^j */ |
| 553 |
x = 69069 * x + 123; |
| 554 |
y ^= (y << 13); |
| 555 |
y ^= (y >> 17); |
| 556 |
y ^= (y << 5); |
| 557 |
if (((x + y) >> 23) & 1) s = s + t; /* change bit of s, maybe */ |
| 558 |
} /* end j loop */ |
| 559 |
Q[i] = s; |
| 560 |
} /* end i seed loop, Now generate 10^9 dUNI's: */ |
| 561 |
} |
| 562 |
|
| 563 |
double VPS::generate_a_random_number() |
| 564 |
{ |
| 565 |
/* Takes 14 nanosecs, Intel Q6600,2.40GHz */ |
| 566 |
int i, j; |
| 567 |
double t; /* t: first temp, then next CSWB value */ |
| 568 |
/* First get zy as next lag-2 SWB */ |
| 569 |
t = zx - zy - zc; |
| 570 |
zx = zy; |
| 571 |
|
| 572 |
if (t < 0) |
| 573 |
{ |
| 574 |
zy = t + 1.0; |
| 575 |
zc = cc; |
| 576 |
} |
| 577 |
else |
| 578 |
{ |
| 579 |
zy = t; |
| 580 |
zc = 0.0; |
| 581 |
} |
| 582 |
|
| 583 |
/* Then get t as the next lag-1220 CSWB value */ |
| 584 |
if (indx < 1220) |
| 585 |
t = Q[indx++]; |
| 586 |
else |
| 587 |
{ /* refill Q[n] via Q[n-1220]-Q[n-1190]-c, */ |
| 588 |
for (i = 0; i < 1220; i++) |
| 589 |
{ |
| 590 |
j = (i < 30) ? i + 1190 : i - 30; |
| 591 |
t = Q[j] - Q[i] + c; /* Get next CSWB element */ |
| 592 |
if (t > 0) |
| 593 |
{ |
| 594 |
t = t - cc; |
| 595 |
c = cc; |
| 596 |
} |
| 597 |
else |
| 598 |
{ |
| 599 |
t = t - cc + 1.0; |
| 600 |
c = 0.0; |
| 601 |
} |
| 602 |
Q[i] = t; |
| 603 |
} /* end i loop */ |
| 604 |
indx = 1; |
| 605 |
t = Q[0]; /* set indx, exit 'else' with t=Q[0] */ |
| 606 |
} /* end else segment; return t-zy mod 1 */ |
| 607 |
|
| 608 |
return ((t < zy) ? 1.0 + (t - zy) : t - zy); |
| 609 |
} |
| 610 |
|
| 611 |
int VPS::sample_uniformly_from_unit_sphere(double* dart, size_t num_dim) |
| 612 |
{ |
| 613 |
size_t idim = 0; |
| 614 |
while (true) |
| 615 |
{ |
| 616 |
double u1 = generate_a_random_number(); |
| 617 |
double u2 = generate_a_random_number(); |
| 618 |
double r = sqrt(-2 * log(u1)); |
| 619 |
double theta = 2 * PI * u2; |
| 620 |
double n1 = r * cos(theta); |
| 621 |
double n2 = r * sin(theta); |
| 622 |
dart[idim] = n1; idim++; if (idim == num_dim) break; |
| 623 |
dart[idim] = n2; idim++; if (idim == num_dim) break; |
| 624 |
} |
| 625 |
double norm(0.0); |
| 626 |
for (idim = 0; idim < num_dim; idim++) norm += dart[idim] * dart[idim]; |
| 627 |
|
| 628 |
norm = 1.0 / sqrt(norm); |
| 629 |
for (idim = 0; idim < num_dim; idim++) dart[idim] *= norm; |
| 630 |
return 0; |
| 631 |
} |
| 632 |
|
| 633 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 634 |
/////////// kd-tree Methods ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 635 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 636 |
|
| 637 |
|
| 638 |
int VPS::kd_tree_build_balanced() |
| 639 |
{ |
| 640 |
//#pragma region Build Balanced kd-tree: |
| 641 |
//std::cout << "\n * Build Balanced kd-tree .......... "; |
| 642 |
//clock_t start_time, end_time; double cpu_time; |
| 643 |
//start_time = clock(); |
| 644 |
|
| 645 |
if (_tree_left == 0) _tree_left = new size_t[_budget]; |
| 646 |
if (_tree_right == 0) _tree_right = new size_t[_budget]; |
| 647 |
|
| 648 |
size_t* tree_nodes_sorted = new size_t[_num_samples]; |
| 649 |
for (size_t i = 0; i < _num_samples; i++) tree_nodes_sorted[i] = i; |
| 650 |
|
| 651 |
for (size_t iseed = 0; iseed < _budget; iseed++) |
| 652 |
{ |
| 653 |
_tree_left[iseed] = iseed; _tree_right[iseed] = iseed; |
| 654 |
} |
| 655 |
_tree_origin = _budget; |
| 656 |
|
| 657 |
size_t target_pos = _num_samples / 2; _tree_max_height = 0; |
| 658 |
kd_tree_balance_quicksort(target_pos, 0, _num_samples - 1, 0, tree_nodes_sorted); |
| 659 |
|
| 660 |
delete[] tree_nodes_sorted; |
| 661 |
|
| 662 |
//end_time = clock(); |
| 663 |
//cpu_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC; |
| 664 |
//std::cout << cpu_time << " seconds." << std::endl; |
| 665 |
return 0; |
| 666 |
//#pragma endregion |
| 667 |
} |
| 668 |
|
| 669 |
int VPS::kd_tree_balance_quicksort(size_t target_pos, size_t left, size_t right, size_t active_dim, size_t* tree_nodes_sorted) |
| 670 |
{ |
| 671 |
//#pragma region kd tree balance: |
| 672 |
kd_tree_quicksort_adjust_target_position(target_pos, left, right, active_dim, tree_nodes_sorted); |
| 673 |
|
| 674 |
// target position is correct .. add to tree |
| 675 |
if (_tree_origin == _budget) _tree_origin = tree_nodes_sorted[target_pos]; |
| 676 |
else kd_tree_add_point(tree_nodes_sorted[target_pos]); |
| 677 |
|
| 678 |
/* recursion */ |
| 679 |
active_dim++; |
| 680 |
if (active_dim == _num_dim) active_dim = 0; |
| 681 |
|
| 682 |
if (target_pos > left + 1) kd_tree_balance_quicksort((left + target_pos - 1) / 2, left, target_pos - 1, active_dim, tree_nodes_sorted); |
| 683 |
else if (left < target_pos) kd_tree_add_point(tree_nodes_sorted[left]); |
| 684 |
|
| 685 |
if (target_pos + 1 < right) kd_tree_balance_quicksort((target_pos + 1 + right) / 2, target_pos + 1, right, active_dim, tree_nodes_sorted); |
| 686 |
else if (right > target_pos) kd_tree_add_point(tree_nodes_sorted[right]); |
| 687 |
|
| 688 |
return 0; |
| 689 |
//#pragma endregion |
| 690 |
} |
| 691 |
|
| 692 |
int VPS::kd_tree_quicksort_adjust_target_position(size_t target_pos, size_t left, size_t right, size_t active_dim, size_t* tree_nodes_sorted) |
| 693 |
{ |
| 694 |
//#pragma region kd tree Quick sort pivot: |
| 695 |
size_t i = left, j = right; |
| 696 |
|
| 697 |
size_t pivot_seed = tree_nodes_sorted[(left + right) / 2]; |
| 698 |
double pivot = _x[pivot_seed][active_dim]; |
| 699 |
|
| 700 |
/* partition */ |
| 701 |
while (i <= j) |
| 702 |
{ |
| 703 |
while (_x[tree_nodes_sorted[i]][active_dim] < pivot) |
| 704 |
i++; |
| 705 |
while (_x[tree_nodes_sorted[j]][active_dim] > pivot) |
| 706 |
j--; |
| 707 |
if (i <= j) |
| 708 |
{ |
| 709 |
size_t tmp_index = tree_nodes_sorted[i]; |
| 710 |
tree_nodes_sorted[i] = tree_nodes_sorted[j]; |
| 711 |
tree_nodes_sorted[j] = tmp_index; |
| 712 |
|
| 713 |
i++; |
| 714 |
if (j > 0) j--; |
| 715 |
} |
| 716 |
}; |
| 717 |
|
| 718 |
/* recursion */ |
| 719 |
|
| 720 |
if (j > 0 && left < j && left <= target_pos && j >= target_pos) |
| 721 |
kd_tree_quicksort_adjust_target_position(target_pos, left, j, active_dim, tree_nodes_sorted); |
| 722 |
if (i < right && i <= target_pos && right >= target_pos) |
| 723 |
kd_tree_quicksort_adjust_target_position(target_pos, i, right, active_dim, tree_nodes_sorted); |
| 724 |
|
| 725 |
return 0; |
| 726 |
//#pragma endregion |
| 727 |
} |
| 728 |
|
| 729 |
int VPS::kd_tree_add_point(size_t seed_index) |
| 730 |
{ |
| 731 |
//#pragma region kd tree add point: |
| 732 |
// insert sphere into tree |
| 733 |
size_t parent_index(_tree_origin); size_t d_index(0); |
| 734 |
size_t branch_height(1); |
| 735 |
while (true) |
| 736 |
{ |
| 737 |
if (_x[seed_index][d_index] > _x[parent_index][d_index]) |
| 738 |
{ |
| 739 |
if (_tree_right[parent_index] == parent_index) |
| 740 |
{ |
| 741 |
_tree_right[parent_index] = seed_index; |
| 742 |
branch_height++; |
| 743 |
break; |
| 744 |
} |
| 745 |
else |
| 746 |
{ |
| 747 |
parent_index = _tree_right[parent_index]; |
| 748 |
branch_height++; |
| 749 |
} |
| 750 |
} |
| 751 |
else |
| 752 |
{ |
| 753 |
if (_tree_left[parent_index] == parent_index) |
| 754 |
{ |
| 755 |
_tree_left[parent_index] = seed_index; |
| 756 |
branch_height++; |
| 757 |
break; |
| 758 |
} |
| 759 |
else |
| 760 |
{ |
| 761 |
parent_index = _tree_left[parent_index]; |
| 762 |
branch_height++; |
| 763 |
} |
| 764 |
} |
| 765 |
d_index++; |
| 766 |
if (d_index == _num_dim) d_index = 0; |
| 767 |
} |
| 768 |
if (branch_height > _tree_max_height) _tree_max_height = branch_height; |
| 769 |
return 0; |
| 770 |
//#pragma endregion |
| 771 |
} |
| 772 |
|
| 773 |
int VPS::kd_tree_get_seeds_in_sphere(double* x, // Sphere center |
| 774 |
double r, // neighborhood radius |
| 775 |
size_t d_index, size_t node_index, // indices to traverse the kd-tree |
| 776 |
size_t &num_points_in_sphere, size_t* &points_in_sphere, // number of points in sphere and their indices |
| 777 |
size_t &capacity // Size of points in sphere array |
| 778 |
) |
| 779 |
{ |
| 780 |
//#pragma region kd tree neighbor search: |
| 781 |
if (d_index == _num_dim) d_index = 0; |
| 782 |
|
| 783 |
double dst_sq(0.0); |
| 784 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 785 |
{ |
| 786 |
double dx = _x[node_index][idim] - x[idim]; |
| 787 |
dst_sq += dx * dx; |
| 788 |
} |
| 789 |
if (dst_sq < r * r + DST_TOL) add_entry(node_index, num_points_in_sphere, points_in_sphere, capacity); |
| 790 |
|
| 791 |
bool check_right(false), check_left(false); |
| 792 |
double neighbor_min(x[d_index] - r), neighbor_max(x[d_index] + r); |
| 793 |
|
| 794 |
if (_tree_right[node_index] != node_index && neighbor_max > _x[node_index][d_index]) check_right = true; |
| 795 |
if (_tree_left[node_index] != node_index && neighbor_min < _x[node_index][d_index]) check_left = true; |
| 796 |
|
| 797 |
if (check_right) kd_tree_get_seeds_in_sphere(x, r, d_index + 1, _tree_right[node_index], num_points_in_sphere, points_in_sphere, capacity); |
| 798 |
if (check_left) kd_tree_get_seeds_in_sphere(x, r, d_index + 1, _tree_left[node_index], num_points_in_sphere, points_in_sphere, capacity); |
| 799 |
|
| 800 |
return 0; |
| 801 |
//#pragma endregion |
| 802 |
} |
| 803 |
|
| 804 |
int VPS::kd_tree_get_closest_seed(size_t seed_index, // seed index |
| 805 |
size_t d_index, size_t node_index, // indices to traverse the kd-tree |
| 806 |
size_t &closest_seed, double &closest_distance // index of closest seed and distance from it |
| 807 |
) |
| 808 |
{ |
| 809 |
//#pragma region kd tree closest neighbor search: |
| 810 |
if (d_index == _num_dim) d_index = 0; |
| 811 |
|
| 812 |
if (seed_index != node_index) |
| 813 |
{ |
| 814 |
double dst_sq(0.0); |
| 815 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 816 |
{ |
| 817 |
double dx = _x[seed_index][idim] - _x[node_index][idim]; |
| 818 |
dst_sq += dx * dx; |
| 819 |
} |
| 820 |
|
| 821 |
if (dst_sq < closest_distance * closest_distance) |
| 822 |
{ |
| 823 |
// add to neighbors |
| 824 |
closest_seed = node_index; |
| 825 |
closest_distance = sqrt(dst_sq); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
bool check_right(false), check_left(false); |
| 830 |
|
| 831 |
double neighbor_min, neighbor_max; |
| 832 |
if (closest_distance == DBL_MAX){ check_right = true; check_left = true; } |
| 833 |
else |
| 834 |
{ |
| 835 |
neighbor_min = _x[seed_index][d_index] - closest_distance; neighbor_max = _x[seed_index][d_index] + closest_distance; |
| 836 |
if (_tree_right[node_index] != node_index && neighbor_max > _x[node_index][d_index]) check_right = true; |
| 837 |
if (_tree_left[node_index] != node_index && neighbor_min < _x[node_index][d_index]) check_left = true; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
if (check_right && check_left) |
| 842 |
{ |
| 843 |
// check the half that x blongs to first |
| 844 |
if (_x[seed_index][d_index] > _x[node_index][d_index]) |
| 845 |
{ |
| 846 |
kd_tree_get_closest_seed(seed_index, d_index + 1, _tree_right[node_index], closest_seed, closest_distance); |
| 847 |
neighbor_min = _x[seed_index][d_index] - closest_distance; // update neighbor min since closest_distance might have been shrunk |
| 848 |
if (neighbor_min < _x[node_index][d_index]) |
| 849 |
{ |
| 850 |
kd_tree_get_closest_seed(seed_index, d_index + 1, _tree_left[node_index], closest_seed, closest_distance); |
| 851 |
} |
| 852 |
} |
| 853 |
else |
| 854 |
{ |
| 855 |
kd_tree_get_closest_seed(seed_index, d_index + 1, _tree_left[node_index], closest_seed, closest_distance); |
| 856 |
neighbor_max = _x[seed_index][d_index] + closest_distance; |
| 857 |
if (neighbor_max > _x[node_index][d_index]) |
| 858 |
{ |
| 859 |
kd_tree_get_closest_seed(seed_index, d_index + 1, _tree_right[node_index], closest_seed, closest_distance); |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
else if (check_right) kd_tree_get_closest_seed(seed_index, d_index + 1, _tree_right[node_index], closest_seed, closest_distance); |
| 864 |
else if (check_left) kd_tree_get_closest_seed(seed_index, d_index + 1, _tree_left[node_index], closest_seed, closest_distance); |
| 865 |
|
| 866 |
return 0; |
| 867 |
//#pragma endregion |
| 868 |
} |
| 869 |
|
| 870 |
int VPS::kd_tree_get_closest_seed(double* x, // point location |
| 871 |
size_t d_index, size_t node_index, // indices to traverse the kd-tree |
| 872 |
size_t &closest_seed, double &closest_distance // index of closest seed and distance from it |
| 873 |
) |
| 874 |
{ |
| 875 |
//#pragma region kd tree closest neighbor search: |
| 876 |
if (d_index == _num_dim) d_index = 0; |
| 877 |
|
| 878 |
|
| 879 |
double dst_sq(0.0); |
| 880 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 881 |
{ |
| 882 |
double dx = x[idim] - _x[node_index][idim]; |
| 883 |
dst_sq += dx * dx; |
| 884 |
} |
| 885 |
|
| 886 |
if (dst_sq < closest_distance * closest_distance) |
| 887 |
{ |
| 888 |
// add to neighbors |
| 889 |
closest_seed = node_index; |
| 890 |
closest_distance = sqrt(dst_sq); |
| 891 |
} |
| 892 |
|
| 893 |
bool check_right(false), check_left(false); |
| 894 |
|
| 895 |
double neighbor_min, neighbor_max; |
| 896 |
if (closest_distance == DBL_MAX){ check_right = true; check_left = true; } |
| 897 |
else |
| 898 |
{ |
| 899 |
neighbor_min = x[d_index] - closest_distance; neighbor_max = x[d_index] + closest_distance; |
| 900 |
if (_tree_right[node_index] != node_index && neighbor_max > _x[node_index][d_index]) check_right = true; |
| 901 |
if (_tree_left[node_index] != node_index && neighbor_min < _x[node_index][d_index]) check_left = true; |
| 902 |
} |
| 903 |
|
| 904 |
if (check_right && check_left) |
| 905 |
{ |
| 906 |
// check the half that x blongs to first |
| 907 |
if (x[d_index] > _x[node_index][d_index]) |
| 908 |
{ |
| 909 |
kd_tree_get_closest_seed(x, d_index + 1, _tree_right[node_index], closest_seed, closest_distance); |
| 910 |
neighbor_min = x[d_index] - closest_distance; // update neighbor min since closest_distance might have been shrunk |
| 911 |
if (neighbor_min < _x[node_index][d_index]) |
| 912 |
{ |
| 913 |
kd_tree_get_closest_seed(x, d_index + 1, _tree_left[node_index], closest_seed, closest_distance); |
| 914 |
} |
| 915 |
|
| 916 |
} |
| 917 |
else |
| 918 |
{ |
| 919 |
kd_tree_get_closest_seed(x, d_index + 1, _tree_left[node_index], closest_seed, closest_distance); |
| 920 |
neighbor_max = x[d_index] + closest_distance; |
| 921 |
if (neighbor_max > _x[node_index][d_index]) |
| 922 |
{ |
| 923 |
kd_tree_get_closest_seed(x, d_index + 1, _tree_right[node_index], closest_seed, closest_distance); |
| 924 |
} |
| 925 |
} |
| 926 |
} |
| 927 |
else if (check_right) kd_tree_get_closest_seed(x, d_index + 1, _tree_right[node_index], closest_seed, closest_distance); |
| 928 |
else if (check_left) kd_tree_get_closest_seed(x, d_index + 1, _tree_left[node_index], closest_seed, closest_distance); |
| 929 |
|
| 930 |
return 0; |
| 931 |
//#pragma endregion |
| 932 |
} |
| 933 |
|
| 934 |
int VPS::kd_tree_get_closest_seed(double* x, // a point in space |
| 935 |
size_t d_index, size_t node_index, // indices to traverse the kd-tree |
| 936 |
size_t num_exculded_seeds, size_t* exculded_seeds, // Number of excluded seeds and their indices |
| 937 |
size_t &closest_seed, double &closest_distance // index of closest seed and distance from it |
| 938 |
) |
| 939 |
{ |
| 940 |
//#pragma region kd tree closest neighbor search: |
| 941 |
if (d_index == _num_dim) d_index = 0; |
| 942 |
|
| 943 |
|
| 944 |
if (!find_brute(node_index, exculded_seeds, num_exculded_seeds)) |
| 945 |
{ |
| 946 |
double dst_sq(0.0); |
| 947 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 948 |
{ |
| 949 |
double dx = x[idim] - _x[node_index][idim]; |
| 950 |
dst_sq += dx * dx; |
| 951 |
} |
| 952 |
|
| 953 |
if (dst_sq < closest_distance * closest_distance) |
| 954 |
{ |
| 955 |
// add to neighbors |
| 956 |
closest_seed = node_index; |
| 957 |
closest_distance = sqrt(dst_sq); |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
bool check_right(false), check_left(false); |
| 962 |
|
| 963 |
double neighbor_min, neighbor_max; |
| 964 |
if (closest_distance == DBL_MAX){ check_right = true; check_left = true; } |
| 965 |
else |
| 966 |
{ |
| 967 |
neighbor_min = x[d_index] - closest_distance; neighbor_max = x[d_index] + closest_distance; |
| 968 |
if (_tree_right[node_index] != node_index && neighbor_max > _x[node_index][d_index]) check_right = true; |
| 969 |
if (_tree_left[node_index] != node_index && neighbor_min < _x[node_index][d_index]) check_left = true; |
| 970 |
} |
| 971 |
|
| 972 |
if (check_right && check_left) |
| 973 |
{ |
| 974 |
// check the half that x blongs to first |
| 975 |
if (x[d_index] > _x[node_index][d_index]) |
| 976 |
{ |
| 977 |
kd_tree_get_closest_seed(x, d_index + 1, _tree_right[node_index], num_exculded_seeds, exculded_seeds, closest_seed, closest_distance); |
| 978 |
neighbor_min = x[d_index] - closest_distance; // update neighbor min since closest_distance might have been shrunk |
| 979 |
if (neighbor_min < _x[node_index][d_index]) |
| 980 |
{ |
| 981 |
kd_tree_get_closest_seed(x, d_index + 1, _tree_left[node_index], num_exculded_seeds, exculded_seeds, closest_seed, closest_distance); |
| 982 |
} |
| 983 |
|
| 984 |
} |
| 985 |
else |
| 986 |
{ |
| 987 |
kd_tree_get_closest_seed(x, d_index + 1, _tree_left[node_index], num_exculded_seeds, exculded_seeds, closest_seed, closest_distance); |
| 988 |
neighbor_max = x[d_index] + closest_distance; |
| 989 |
if (neighbor_max > _x[node_index][d_index]) |
| 990 |
{ |
| 991 |
kd_tree_get_closest_seed(x, d_index + 1, _tree_right[node_index], num_exculded_seeds, exculded_seeds, closest_seed, closest_distance); |
| 992 |
} |
| 993 |
} |
| 994 |
} |
| 995 |
else if (check_right) kd_tree_get_closest_seed(x, d_index + 1, _tree_right[node_index], num_exculded_seeds, exculded_seeds, closest_seed, closest_distance); |
| 996 |
else if (check_left) kd_tree_get_closest_seed(x, d_index + 1, _tree_left[node_index], num_exculded_seeds, exculded_seeds, closest_seed, closest_distance); |
| 997 |
|
| 998 |
return 0; |
| 999 |
//#pragma endregion |
| 1000 |
} |
| 1001 |
|
| 1002 |
int VPS::get_closest_seed_tree(size_t seed_index, size_t &closest_seed, double &closest_distance) |
| 1003 |
{ |
| 1004 |
//#pragma region Closest Neighbor Search using tree: |
| 1005 |
|
| 1006 |
closest_seed = _budget; |
| 1007 |
kd_tree_get_closest_seed(seed_index, 0, _tree_origin, closest_seed, closest_distance); |
| 1008 |
|
| 1009 |
return 0; |
| 1010 |
//#pragma endregion |
| 1011 |
} |
| 1012 |
|
| 1013 |
int VPS::get_closest_seed_tree(double* x, size_t &closest_seed, double &closest_distance) |
| 1014 |
{ |
| 1015 |
//#pragma region Closest Neighbor Search using tree: |
| 1016 |
closest_seed = _budget; |
| 1017 |
kd_tree_get_closest_seed(x, 0, _tree_origin, closest_seed, closest_distance); |
| 1018 |
return 0; |
| 1019 |
//#pragma endregion |
| 1020 |
} |
| 1021 |
|
| 1022 |
int VPS::get_closest_seed_tree(double* x, size_t num_exculded_seeds, size_t* exculded_seeds, size_t &closest_seed, double& closest_distance) |
| 1023 |
{ |
| 1024 |
//#pragma region Closest Neighbor Search using a tree: |
| 1025 |
|
| 1026 |
closest_seed = _budget; |
| 1027 |
kd_tree_get_closest_seed(x, 0, _tree_origin, num_exculded_seeds, exculded_seeds, closest_seed, closest_distance); |
| 1028 |
return 0; |
| 1029 |
|
| 1030 |
//#pragma endregion |
| 1031 |
} |
| 1032 |
|
| 1033 |
int VPS::get_seeds_in_sphere_tree(double* x, double r, size_t &num_points_in_sphere, size_t* &points_in_sphere, size_t &capacity) |
| 1034 |
{ |
| 1035 |
//#pragma region tree neighbor search: |
| 1036 |
num_points_in_sphere = 0; |
| 1037 |
kd_tree_get_seeds_in_sphere(x, r, 0, _tree_origin, num_points_in_sphere, points_in_sphere, capacity); |
| 1038 |
|
| 1039 |
return 0; |
| 1040 |
//#pragma endregion |
| 1041 |
} |
| 1042 |
|
| 1043 |
int VPS::get_closest_seed_brute(double* x, size_t num_significant_neighbors, size_t* significant_neighbors, size_t num_exculded_seeds, size_t* exculded_seeds, size_t &closest_seed) |
| 1044 |
{ |
| 1045 |
//#pragma region Brute Force closest Neighbor search over significant non-excluded seeds: |
| 1046 |
// Brute Force to retrieve Significant closest Seed |
| 1047 |
|
| 1048 |
double closest_distance_sq = DBL_MAX; closest_seed = _budget; |
| 1049 |
for (size_t isig_seed = 0; isig_seed < num_significant_neighbors; isig_seed++) |
| 1050 |
{ |
| 1051 |
size_t iseed = significant_neighbors[isig_seed]; |
| 1052 |
|
| 1053 |
bool excluded(false); |
| 1054 |
for (size_t iex = 0; iex < num_exculded_seeds; iex++) |
| 1055 |
{ |
| 1056 |
if (exculded_seeds[iex] == iseed){ excluded = true; break; } |
| 1057 |
} |
| 1058 |
if (excluded) continue; |
| 1059 |
|
| 1060 |
double dst_sq(0.0); |
| 1061 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 1062 |
{ |
| 1063 |
double dx = _x[iseed][idim] - x[idim]; |
| 1064 |
dst_sq += dx * dx; |
| 1065 |
} |
| 1066 |
if (dst_sq < closest_distance_sq) |
| 1067 |
{ |
| 1068 |
closest_distance_sq = dst_sq; |
| 1069 |
closest_seed = iseed; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
return 0; |
| 1073 |
//#pragma endregion |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1078 |
/////////// General Methods ////////////////////////////////////////////////////////////////////////////////////////////// |
| 1079 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1080 |
|
| 1081 |
int VPS::quicksort(size_t* I, size_t left, size_t right) |
| 1082 |
{ |
| 1083 |
//#pragma region Quick Sort: |
| 1084 |
size_t i = left, j = right; |
| 1085 |
size_t pivot = I[(left + right) / 2]; |
| 1086 |
|
| 1087 |
/* partition */ |
| 1088 |
while (i <= j) |
| 1089 |
{ |
| 1090 |
while (I[i] < pivot) |
| 1091 |
i++; |
| 1092 |
while (I[j] > pivot) |
| 1093 |
j--; |
| 1094 |
if (i <= j) |
| 1095 |
{ |
| 1096 |
size_t tmp = I[i]; |
| 1097 |
I[i] = I[j]; |
| 1098 |
I[j] = tmp; |
| 1099 |
|
| 1100 |
i++; |
| 1101 |
if (j > 0) j--; |
| 1102 |
} |
| 1103 |
}; |
| 1104 |
|
| 1105 |
/* recursion */ |
| 1106 |
|
| 1107 |
if (j > 0 && left < j) |
| 1108 |
quicksort(I, left, j); |
| 1109 |
if (i < right) |
| 1110 |
quicksort(I, i, right); |
| 1111 |
|
| 1112 |
return 0; |
| 1113 |
//#pragma endregion |
| 1114 |
} |
| 1115 |
|
| 1116 |
int VPS::quicksort(double* V, size_t* I, size_t left, size_t right) |
| 1117 |
{ |
| 1118 |
//#pragma region Quick Sort: |
| 1119 |
size_t i = left, j = right; |
| 1120 |
double pivot = V[(left + right) / 2]; |
| 1121 |
|
| 1122 |
/* partition */ |
| 1123 |
while (i <= j) |
| 1124 |
{ |
| 1125 |
while (V[i] < pivot) |
| 1126 |
i++; |
| 1127 |
while (V[j] > pivot) |
| 1128 |
j--; |
| 1129 |
if (i <= j) |
| 1130 |
{ |
| 1131 |
double tmpd = V[i]; V[i] = V[j]; V[j] = tmpd; |
| 1132 |
size_t tmpi = I[i]; I[i] = I[j]; I[j] = tmpi; |
| 1133 |
i++; |
| 1134 |
if (j > 0) j--; |
| 1135 |
} |
| 1136 |
}; |
| 1137 |
|
| 1138 |
/* recursion */ |
| 1139 |
if (j > 0 && left < j) |
| 1140 |
quicksort(V, I, left, j); |
| 1141 |
|
| 1142 |
if (i < right) |
| 1143 |
quicksort(V, I, i, right); |
| 1144 |
|
| 1145 |
return 0; |
| 1146 |
//#pragma endregion |
| 1147 |
} |
| 1148 |
|
| 1149 |
bool VPS::find_binary(size_t entry, size_t* I, size_t left, size_t right) |
| 1150 |
{ |
| 1151 |
//#pragma region find using Binary search: |
| 1152 |
size_t ipivot = (left + right) / 2; |
| 1153 |
size_t pivot = I[ipivot]; |
| 1154 |
if (pivot == entry) return true; |
| 1155 |
if (left == right) return false; |
| 1156 |
|
| 1157 |
if (pivot < entry && ipivot + 1 <= right) return find_binary(entry, I, ipivot + 1, right); |
| 1158 |
if (pivot > entry && left + 1 <= ipivot) return find_binary(entry, I, left, ipivot - 1); |
| 1159 |
|
| 1160 |
return false; |
| 1161 |
//#pragma endregion |
| 1162 |
} |
| 1163 |
|
| 1164 |
bool VPS::find_brute(size_t entry, size_t* I, size_t num_entries) |
| 1165 |
{ |
| 1166 |
//#pragma region find using Brutal search: |
| 1167 |
for (size_t i = 0; i < num_entries; i++) if (I[i] == entry) return true; |
| 1168 |
|
| 1169 |
return false; |
| 1170 |
//#pragma endregion |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
int VPS::add_entry(size_t entry, size_t &num_entries, size_t* &I, size_t &capacity) |
| 1175 |
{ |
| 1176 |
//#pragma region add an entry to a list: |
| 1177 |
// add to neighbors |
| 1178 |
I[num_entries] = entry; |
| 1179 |
num_entries++; |
| 1180 |
if (num_entries == capacity) |
| 1181 |
{ |
| 1182 |
capacity *= 2; |
| 1183 |
size_t* tmp = new size_t[capacity]; |
| 1184 |
for (size_t i = 0; i < num_entries; i++) tmp[i] = I[i]; |
| 1185 |
delete[] I; |
| 1186 |
I = tmp; |
| 1187 |
} |
| 1188 |
return 0; |
| 1189 |
//#pragma endregion |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
size_t VPS::retrieve_num_permutations(size_t num_dim, size_t upper_bound, bool force_sum_constraint, size_t sum_constraint) |
| 1195 |
{ |
| 1196 |
//#pragma region Retrieve Number of Permutation: |
| 1197 |
size_t* t = new size_t[num_dim]; |
| 1198 |
for (size_t idim = 0; idim < num_dim; idim++) t[idim] = 0; |
| 1199 |
|
| 1200 |
// count output |
| 1201 |
size_t num_perm = 0; |
| 1202 |
size_t k_dim(num_dim - 1); |
| 1203 |
while (true) |
| 1204 |
{ |
| 1205 |
while (t[k_dim] <= upper_bound) |
| 1206 |
{ |
| 1207 |
bool valid(true); |
| 1208 |
|
| 1209 |
if (force_sum_constraint) |
| 1210 |
{ |
| 1211 |
size_t s_const(0); |
| 1212 |
for (size_t idim = 0; idim < num_dim; idim++) s_const += t[idim]; |
| 1213 |
|
| 1214 |
if (s_const > upper_bound) |
| 1215 |
{ |
| 1216 |
valid = false; |
| 1217 |
} |
| 1218 |
} |
| 1219 |
if (valid) num_perm++; |
| 1220 |
|
| 1221 |
t[k_dim]++; // move to the next enumeration |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
size_t kk_dim(k_dim - 1); |
| 1226 |
|
| 1227 |
bool done(false); |
| 1228 |
while (true) |
| 1229 |
{ |
| 1230 |
t[kk_dim]++; |
| 1231 |
if (t[kk_dim] > upper_bound) |
| 1232 |
{ |
| 1233 |
t[kk_dim] = 0; |
| 1234 |
if (kk_dim == 0) |
| 1235 |
{ |
| 1236 |
done = true; |
| 1237 |
break; |
| 1238 |
} |
| 1239 |
kk_dim--; |
| 1240 |
} |
| 1241 |
else break; |
| 1242 |
} |
| 1243 |
if (done) break; |
| 1244 |
t[k_dim] = 0; |
| 1245 |
} |
| 1246 |
delete[] t; |
| 1247 |
return num_perm; |
| 1248 |
//#pragma endregion |
| 1249 |
} |
| 1250 |
|
| 1251 |
|
| 1252 |
int VPS::retrieve_permutations(size_t &num_perm, size_t** &perm, size_t num_dim, size_t upper_bound, bool force_sum_constraint, size_t sum_constraint) |
| 1253 |
{ |
| 1254 |
//#pragma region Retrieve Permutation: |
| 1255 |
size_t* t = new size_t[num_dim]; |
| 1256 |
num_perm = retrieve_num_permutations(num_dim, upper_bound, force_sum_constraint, sum_constraint); |
| 1257 |
|
| 1258 |
//std::cout<< "Number of permutations = " << m << std::endl; |
| 1259 |
|
| 1260 |
perm = new size_t*[num_perm]; |
| 1261 |
for (size_t i = 0; i < num_perm; i++) |
| 1262 |
{ |
| 1263 |
perm[i] = new size_t[num_dim]; |
| 1264 |
for (size_t idim = 0; idim < num_dim; idim++) perm[i][idim] = 0; |
| 1265 |
} |
| 1266 |
|
| 1267 |
// Taxi Counter to evaluate the surrogate |
| 1268 |
for (size_t idim = 0; idim < num_dim; idim++) t[idim] = 0; |
| 1269 |
size_t k_dim = num_dim - 1; |
| 1270 |
num_perm = 0; // index of alpha |
| 1271 |
|
| 1272 |
while (true) |
| 1273 |
{ |
| 1274 |
while (t[k_dim] <= upper_bound) |
| 1275 |
{ |
| 1276 |
bool valid(true); |
| 1277 |
|
| 1278 |
if (force_sum_constraint) |
| 1279 |
{ |
| 1280 |
size_t s_const(0); |
| 1281 |
for (size_t idim = 0; idim < num_dim; idim++) s_const += t[idim]; |
| 1282 |
|
| 1283 |
if (s_const > upper_bound) |
| 1284 |
{ |
| 1285 |
valid = false; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
if (valid) |
| 1289 |
{ |
| 1290 |
// store t in t_alpha of counter |
| 1291 |
for (size_t idim = 0; idim < num_dim; idim++) perm[num_perm][idim] = t[idim]; |
| 1292 |
|
| 1293 |
//for (size_t idim = 0; idim < num_dim; idim++) std::cout << perm[m][idim] << " "; |
| 1294 |
//std::cout << std::endl; |
| 1295 |
|
| 1296 |
num_perm++; |
| 1297 |
} |
| 1298 |
|
| 1299 |
t[k_dim]++; // move to the next enumeration |
| 1300 |
} |
| 1301 |
|
| 1302 |
|
| 1303 |
size_t kk_dim(k_dim - 1); |
| 1304 |
|
| 1305 |
bool done(false); |
| 1306 |
while (true) |
| 1307 |
{ |
| 1308 |
t[kk_dim]++; |
| 1309 |
if (t[kk_dim] > upper_bound) |
| 1310 |
{ |
| 1311 |
t[kk_dim] = 0; |
| 1312 |
if (kk_dim == 0) |
| 1313 |
{ |
| 1314 |
done = true; |
| 1315 |
break; |
| 1316 |
} |
| 1317 |
kk_dim--; |
| 1318 |
} |
| 1319 |
else break; |
| 1320 |
} |
| 1321 |
if (done) break; |
| 1322 |
t[k_dim] = 0; |
| 1323 |
} |
| 1324 |
|
| 1325 |
// re-order perm: |
| 1326 |
size_t num_basis = num_perm; |
| 1327 |
//std::cout << "*** VPS:: num_basis = " << num_basis << std::endl; |
| 1328 |
if (num_basis >= num_dim + 1) |
| 1329 |
{ |
| 1330 |
// reorder first order terms |
| 1331 |
for (size_t idim = 0; idim < num_dim; idim++) |
| 1332 |
{ |
| 1333 |
// search fot the corresponding basis and bring it to its proper location |
| 1334 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) |
| 1335 |
{ |
| 1336 |
size_t sum(0), sig_dim(0); |
| 1337 |
for (size_t jdim = 0; jdim < num_dim; jdim++) |
| 1338 |
{ |
| 1339 |
sum += perm[ibasis][jdim]; |
| 1340 |
if (perm[ibasis][jdim] == 1) sig_dim = jdim; |
| 1341 |
} |
| 1342 |
if (sum == 1 && sig_dim == idim) |
| 1343 |
{ |
| 1344 |
// move this basis to location idim + 1 |
| 1345 |
size_t* tmp = perm[ibasis]; |
| 1346 |
perm[ibasis] = perm[idim + 1]; |
| 1347 |
perm[idim + 1] = tmp; |
| 1348 |
break; |
| 1349 |
} |
| 1350 |
} |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
if (num_basis >= num_dim * (num_dim + 1) / 2 + num_dim + 1) |
| 1355 |
{ |
| 1356 |
// reorder second order terms |
| 1357 |
size_t iloc(num_dim + 1); |
| 1358 |
for (size_t idim = 0; idim < num_dim; idim++) |
| 1359 |
{ |
| 1360 |
for (size_t jdim = idim; jdim < num_dim; jdim++) |
| 1361 |
{ |
| 1362 |
// search fot the corresponding basis and bring it to its proper location |
| 1363 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) |
| 1364 |
{ |
| 1365 |
size_t sum(0), sig_dim_i(num_dim), sig_dim_j(num_dim); |
| 1366 |
for (size_t kdim = 0; kdim < num_dim; kdim++) |
| 1367 |
{ |
| 1368 |
sum += perm[ibasis][kdim]; |
| 1369 |
if (perm[ibasis][kdim] > 0 && sig_dim_i == num_dim) |
| 1370 |
{ |
| 1371 |
sig_dim_i = kdim; |
| 1372 |
sig_dim_j = sig_dim_i; |
| 1373 |
} |
| 1374 |
else if (perm[ibasis][kdim] > 0 && sig_dim_i < num_dim) sig_dim_j = kdim; |
| 1375 |
} |
| 1376 |
if (sum == 2 && sig_dim_i == idim && sig_dim_j == jdim) |
| 1377 |
{ |
| 1378 |
// move this basis to location iloc |
| 1379 |
size_t* tmp = perm[ibasis]; |
| 1380 |
perm[ibasis] = perm[iloc]; |
| 1381 |
perm[iloc] = tmp; iloc++; |
| 1382 |
break; |
| 1383 |
} |
| 1384 |
} |
| 1385 |
} |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
// make sure that the permutation order increases |
| 1390 |
for (size_t i = 0; i < num_perm; i++) |
| 1391 |
{ |
| 1392 |
size_t sum_i = 0; |
| 1393 |
for (size_t idim = 0; idim < num_dim; idim++) sum_i += perm[i][idim]; |
| 1394 |
for (size_t j = i + 1; j < num_perm; j++) |
| 1395 |
{ |
| 1396 |
size_t sum_j = 0; |
| 1397 |
for (size_t idim = 0; idim < num_dim; idim++) sum_j += perm[j][idim]; |
| 1398 |
|
| 1399 |
if (sum_j < sum_i) |
| 1400 |
{ |
| 1401 |
size_t* tmp = perm[i]; perm[i] = perm[j]; perm[j] = tmp; |
| 1402 |
sum_i = sum_j; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
/* |
| 1408 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) |
| 1409 |
{ |
| 1410 |
for (size_t idim = 0; idim < num_dim; idim++) std::cout << perm[ibasis][idim] << " "; |
| 1411 |
std::cout << std::endl; |
| 1412 |
} |
| 1413 |
*/ |
| 1414 |
|
| 1415 |
delete[] t; |
| 1416 |
|
| 1417 |
return 0; |
| 1418 |
//#pragma endregion |
| 1419 |
} |
| 1420 |
|
| 1421 |
|
| 1422 |
void VPS::plot_graph(std::string file_name, size_t num_points, double* x, size_t num_functions, double** f) |
| 1423 |
{ |
| 1424 |
//#pragma region Plot Graph: |
| 1425 |
std::fstream file(file_name.c_str(), std::ios::out); |
| 1426 |
file << "%!PS-Adobe-3.0" << std::endl; |
| 1427 |
file << "72 72 scale % one unit = one inch" << std::endl; |
| 1428 |
|
| 1429 |
double xmin(x[0]), ymin(f[0][0]), xmax(xmin), ymax(ymin); |
| 1430 |
for (size_t ipoint = 0; ipoint < num_points; ipoint++) |
| 1431 |
{ |
| 1432 |
if (x[ipoint] < xmin) xmin = x[ipoint]; |
| 1433 |
if (x[ipoint] > xmax) xmax = x[ipoint]; |
| 1434 |
|
| 1435 |
for (size_t ifunc = 0; ifunc < num_functions; ifunc++) |
| 1436 |
{ |
| 1437 |
if (f[ifunc][ipoint] < ymin) |
| 1438 |
ymin = f[ifunc][ipoint]; |
| 1439 |
if (f[ifunc][ipoint] > ymax) |
| 1440 |
ymax = f[ifunc][ipoint]; |
| 1441 |
} |
| 1442 |
} |
| 1443 |
|
| 1444 |
if (xmax < xmin + 0.01 * (xmax - xmin)) xmax = xmin + 0.01 * (xmax - xmin); |
| 1445 |
if (ymax < ymin + 0.01 * (ymax - ymin)) ymax = ymin + 0.01 * (ymax - ymin); |
| 1446 |
|
| 1447 |
double Lx(xmax - xmin); |
| 1448 |
double Ly(ymax - ymin); |
| 1449 |
|
| 1450 |
double scale_x, scale_y; |
| 1451 |
double shift_x, shift_y; |
| 1452 |
|
| 1453 |
scale_x = 6.5 / Lx; |
| 1454 |
scale_y = 9.0 / Ly; |
| 1455 |
|
| 1456 |
shift_x = 1.0 - xmin * scale_x; |
| 1457 |
shift_y = 1.0 - ymin * scale_y; |
| 1458 |
file << shift_x << " " << shift_y << " translate" << std::endl; |
| 1459 |
|
| 1460 |
file << "/Courier findfont" << std::endl; |
| 1461 |
file << "0.12 scalefont" << std::endl; |
| 1462 |
file << "setfont" << std::endl; |
| 1463 |
|
| 1464 |
for (size_t ifunc = 0; ifunc < num_functions; ifunc++) |
| 1465 |
{ |
| 1466 |
double gs = ifunc * 1.0 / num_functions; |
| 1467 |
|
| 1468 |
double r, g, b; |
| 1469 |
|
| 1470 |
if (gs < 0.25) r = 1.0; |
| 1471 |
//else if (gs < 0.5) r = 2.0 - 4.0 * gs; |
| 1472 |
else if (gs < 0.5) r = 1.0 - 16.0 * (gs - 0.25) * (gs - 0.25); |
| 1473 |
else r = 0.0; |
| 1474 |
|
| 1475 |
double go(0.25), gn(1.0 - go); |
| 1476 |
if (gs < go) g = gs / go; |
| 1477 |
else if (gs < gn) g = 1.0; |
| 1478 |
else g = 1.0 / (1.0 - gn) - gs / (1.0 - gn); |
| 1479 |
|
| 1480 |
|
| 1481 |
if (gs < 0.5) b = 0.0; |
| 1482 |
else if (gs < 0.75) b = 1.0 - 16.0 * (gs - 0.75) * (gs - 0.75); |
| 1483 |
else b = 1.0; |
| 1484 |
|
| 1485 |
for (size_t ipoint = 0; ipoint < num_points - 1; ipoint++) |
| 1486 |
{ |
| 1487 |
size_t jpoint = ipoint + 1; |
| 1488 |
|
| 1489 |
file << "newpath" << std::endl; |
| 1490 |
file << x[ipoint] * scale_x << " " << f[ifunc][ipoint] * scale_y << " moveto" << std::endl; |
| 1491 |
file << x[jpoint] * scale_x << " " << f[ifunc][jpoint] * scale_y << " lineto" << std::endl; |
| 1492 |
file << " closepath" << std::endl; |
| 1493 |
file << " gsave" << std::endl; |
| 1494 |
file << " grestore" << std::endl; |
| 1495 |
file << " " << r << " " << g << " " << b << " setrgbcolor" << std::endl; |
| 1496 |
file << " 0.01 setlinewidth" << std::endl; |
| 1497 |
file << " stroke" << std::endl; |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
size_t num_steps(10); |
| 1502 |
double dx((xmax - xmin) / num_steps); |
| 1503 |
double dy((ymax - ymin) / num_steps); |
| 1504 |
|
| 1505 |
// Vertical Grid |
| 1506 |
for (size_t istep = 0; istep <= num_steps; istep++) |
| 1507 |
{ |
| 1508 |
double xx = xmin; |
| 1509 |
double yy = ymin + istep * dy; |
| 1510 |
|
| 1511 |
std::stringstream ss; |
| 1512 |
ss << trunc((yy + 0.0001) * 1000) / 1000; |
| 1513 |
std::string str = ss.str(); |
| 1514 |
|
| 1515 |
file << "newpath " << (xx - dx) * scale_x << " " << yy * scale_y << " moveto (" << str << ") show" << std::endl; |
| 1516 |
|
| 1517 |
file << "newpath" << std::endl; |
| 1518 |
file << xmin * scale_x << " " << yy * scale_y << " moveto" << std::endl; |
| 1519 |
file << xmax * scale_x << " " << yy * scale_y << " lineto" << std::endl; |
| 1520 |
file << " closepath" << std::endl; |
| 1521 |
file << " gsave" << std::endl; |
| 1522 |
file << " grestore" << std::endl; |
| 1523 |
file << " " << 0.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1524 |
file << " 0.01 setlinewidth" << std::endl; |
| 1525 |
file << " stroke" << std::endl; |
| 1526 |
} |
| 1527 |
|
| 1528 |
// Horizontal Grid |
| 1529 |
for (size_t istep = 0; istep <= num_steps; istep++) |
| 1530 |
{ |
| 1531 |
double xx = xmin + istep * dx; |
| 1532 |
double yy = ymin; |
| 1533 |
|
| 1534 |
std::stringstream ss; |
| 1535 |
ss << trunc((xx + 0.001) * 100) / 100; |
| 1536 |
std::string str = ss.str(); |
| 1537 |
|
| 1538 |
file << "newpath " << xx * scale_x << " " << (yy - 0.2 * dy) * scale_y << " moveto (" << str << ") show" << std::endl; |
| 1539 |
|
| 1540 |
file << "newpath" << std::endl; |
| 1541 |
file << xx * scale_x << " " << ymin * scale_y << " moveto" << std::endl; |
| 1542 |
file << xx * scale_x << " " << ymax * scale_y << " lineto" << std::endl; |
| 1543 |
file << " closepath" << std::endl; |
| 1544 |
file << " gsave" << std::endl; |
| 1545 |
file << " grestore" << std::endl; |
| 1546 |
file << " " << 0.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1547 |
file << " 0.01 setlinewidth" << std::endl; |
| 1548 |
file << " stroke" << std::endl; |
| 1549 |
} |
| 1550 |
|
| 1551 |
//#pragma endregion |
| 1552 |
} |
| 1553 |
|
| 1554 |
void VPS::plot_polynomial(std::string file_name, size_t num_basis, double* c, double xmin, double xmax, size_t num_points, double* px, double* py) |
| 1555 |
{ |
| 1556 |
//#pragma region Plot Polynomial: |
| 1557 |
std::fstream file(file_name.c_str(), std::ios::out); |
| 1558 |
file << "%!PS-Adobe-3.0" << std::endl; |
| 1559 |
file << "72 72 scale % one unit = one inch" << std::endl; |
| 1560 |
|
| 1561 |
size_t num_p_points = 101; |
| 1562 |
double dx = (xmax - xmin) / (num_p_points - 1); |
| 1563 |
|
| 1564 |
double ymin(DBL_MAX), ymax(-DBL_MAX); |
| 1565 |
for (size_t ipoint = 0; ipoint < num_p_points - 1; ipoint++) |
| 1566 |
{ |
| 1567 |
//size_t jpoint = ipoint + 1; // ETP |
| 1568 |
|
| 1569 |
double x = xmin + ipoint * dx; |
| 1570 |
double f = 0.0; |
| 1571 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) f += c[ibasis] * pow(x, ibasis); |
| 1572 |
if (f < ymin) ymin = f; |
| 1573 |
if (f > ymax) ymax = f; |
| 1574 |
} |
| 1575 |
|
| 1576 |
double Lx(xmax - xmin); |
| 1577 |
double Ly(ymax - ymin); |
| 1578 |
|
| 1579 |
if (Ly < 1E-10){ymin -= 0.5; ymax += 0.5; Ly = 1.0;} |
| 1580 |
|
| 1581 |
double scale_x, scale_y; |
| 1582 |
double shift_x, shift_y; |
| 1583 |
|
| 1584 |
scale_x = 6.5 / Lx; |
| 1585 |
scale_y = 9.0 / Ly; |
| 1586 |
|
| 1587 |
shift_x = 1.0 - xmin * scale_x; |
| 1588 |
shift_y = 1.0 - ymin * scale_y; |
| 1589 |
file << shift_x << " " << shift_y << " translate" << std::endl; |
| 1590 |
|
| 1591 |
file << "/Courier findfont" << std::endl; |
| 1592 |
file << "0.12 scalefont" << std::endl; |
| 1593 |
file << "setfont" << std::endl; |
| 1594 |
|
| 1595 |
|
| 1596 |
size_t num_steps(10); |
| 1597 |
double dxg((xmax - xmin) / num_steps); |
| 1598 |
double dyg((ymax - ymin) / num_steps); |
| 1599 |
|
| 1600 |
// Vertical Grid |
| 1601 |
for (size_t istep = 0; istep <= num_steps; istep++) |
| 1602 |
{ |
| 1603 |
double xx = xmin; |
| 1604 |
double yy = ymin + istep * dyg; |
| 1605 |
|
| 1606 |
std::stringstream ss; |
| 1607 |
ss << trunc((yy + 0.0001) * 1000) / 1000; |
| 1608 |
std::string str = ss.str(); |
| 1609 |
|
| 1610 |
file << "newpath " << (xx - dxg) * scale_x << " " << yy * scale_y << " moveto (" << str << ") show" << std::endl; |
| 1611 |
|
| 1612 |
file << "newpath" << std::endl; |
| 1613 |
file << xmin * scale_x << " " << yy * scale_y << " moveto" << std::endl; |
| 1614 |
file << xmax * scale_x << " " << yy * scale_y << " lineto" << std::endl; |
| 1615 |
file << " closepath" << std::endl; |
| 1616 |
file << " gsave" << std::endl; |
| 1617 |
file << " grestore" << std::endl; |
| 1618 |
file << " " << 0.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1619 |
file << " 0.01 setlinewidth" << std::endl; |
| 1620 |
file << " stroke" << std::endl; |
| 1621 |
} |
| 1622 |
|
| 1623 |
// Horizontal Grid |
| 1624 |
for (size_t istep = 0; istep <= num_steps; istep++) |
| 1625 |
{ |
| 1626 |
double xx = xmin + istep * dxg; |
| 1627 |
double yy = ymin; |
| 1628 |
|
| 1629 |
std::stringstream ss; |
| 1630 |
ss << trunc((xx + 0.001) * 100) / 100; |
| 1631 |
std::string str = ss.str(); |
| 1632 |
|
| 1633 |
file << "newpath " << xx * scale_x << " " << (yy - 0.2 * dyg) * scale_y << " moveto (" << str << ") show" << std::endl; |
| 1634 |
|
| 1635 |
file << "newpath" << std::endl; |
| 1636 |
file << xx * scale_x << " " << ymin * scale_y << " moveto" << std::endl; |
| 1637 |
file << xx * scale_x << " " << ymax * scale_y << " lineto" << std::endl; |
| 1638 |
file << " closepath" << std::endl; |
| 1639 |
file << " gsave" << std::endl; |
| 1640 |
file << " grestore" << std::endl; |
| 1641 |
file << " " << 0.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1642 |
file << " 0.01 setlinewidth" << std::endl; |
| 1643 |
file << " stroke" << std::endl; |
| 1644 |
} |
| 1645 |
|
| 1646 |
|
| 1647 |
for (size_t ipoint = 0; ipoint < num_p_points - 1; ipoint++) |
| 1648 |
{ |
| 1649 |
//size_t jpoint = ipoint + 1; // ETP |
| 1650 |
|
| 1651 |
double x = xmin + ipoint * dx; |
| 1652 |
double f = 0.0; |
| 1653 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) f += c[ibasis] * pow(x, ibasis); |
| 1654 |
|
| 1655 |
double xp = x + dx; |
| 1656 |
double fp = 0.0; |
| 1657 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) fp += c[ibasis] * pow(xp, ibasis); |
| 1658 |
|
| 1659 |
file << "newpath" << std::endl; |
| 1660 |
file << x * scale_x << " " << f * scale_y << " moveto" << std::endl; |
| 1661 |
file << xp * scale_x << " " << fp * scale_y << " lineto" << std::endl; |
| 1662 |
file << " closepath" << std::endl; |
| 1663 |
file << " gsave" << std::endl; |
| 1664 |
file << " grestore" << std::endl; |
| 1665 |
file << " " << 1.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1666 |
file << " 0.01 setlinewidth" << std::endl; |
| 1667 |
file << " stroke" << std::endl; |
| 1668 |
} |
| 1669 |
|
| 1670 |
for (size_t ipoint = 0; ipoint < num_points; ipoint++) |
| 1671 |
{ |
| 1672 |
file << "newpath" << std::endl; |
| 1673 |
file << px[ipoint] * scale_x << " " << py[ipoint] * scale_y << " " << 0.005 * scale_x << " 0 360 arc" << std::endl; |
| 1674 |
file << "closepath" << std::endl; |
| 1675 |
file << "gsave" << std::endl; |
| 1676 |
file << "0 0 1 setrgbcolor" << std::endl; // Inactive seeds |
| 1677 |
file << "fill" << std::endl; |
| 1678 |
file << "grestore" << std::endl; |
| 1679 |
file << "0.0 setlinewidth" << std::endl; |
| 1680 |
file << "0 0 0 setrgbcolor" << std::endl; // discs borders |
| 1681 |
file << "stroke " << std::endl; |
| 1682 |
} |
| 1683 |
|
| 1684 |
//#pragma endregion |
| 1685 |
} |
| 1686 |
|
| 1687 |
void VPS::plot_piecewise_polynomial(std::string file_name, size_t num_pieces, double* pmin, double* pmax, size_t* num_basis, double** c, size_t num_points, double* px, double* py) |
| 1688 |
{ |
| 1689 |
//#pragma region Plot Polynomial: |
| 1690 |
std::fstream file(file_name.c_str(), std::ios::out); |
| 1691 |
file << "%!PS-Adobe-3.0" << std::endl; |
| 1692 |
file << "72 72 scale % one unit = one inch" << std::endl; |
| 1693 |
|
| 1694 |
double xmin = pmin[0]; double xmax = pmax[num_pieces - 1]; |
| 1695 |
|
| 1696 |
double ymin(DBL_MAX), ymax(-DBL_MAX); |
| 1697 |
for (size_t ipiece = 0; ipiece < num_pieces; ipiece++) |
| 1698 |
{ |
| 1699 |
size_t num_steps(50); |
| 1700 |
double ds = (pmax[ipiece] - pmin[ipiece]) / num_steps; |
| 1701 |
for (size_t istep = 0; istep <= num_steps; istep++) |
| 1702 |
{ |
| 1703 |
double x = pmin[ipiece] + istep * ds; |
| 1704 |
double f = 0.0; |
| 1705 |
for (size_t ibasis = 0; ibasis < num_basis[ipiece]; ibasis++) f += c[ipiece][ibasis] * pow(x, ibasis); |
| 1706 |
if (f < ymin) ymin = f; |
| 1707 |
if (f > ymax) ymax = f; |
| 1708 |
} |
| 1709 |
} |
| 1710 |
|
| 1711 |
double Lx(xmax - xmin); |
| 1712 |
double Ly(ymax - ymin); |
| 1713 |
|
| 1714 |
if (Ly < 1E-10){ ymin -= 0.5; ymax += 0.5; Ly = 1.0; } |
| 1715 |
|
| 1716 |
double scale_x, scale_y; |
| 1717 |
double shift_x, shift_y; |
| 1718 |
|
| 1719 |
scale_x = 6.5 / Lx; |
| 1720 |
scale_y = 9.0 / Ly; |
| 1721 |
|
| 1722 |
shift_x = 1.0 - xmin * scale_x; |
| 1723 |
shift_y = 1.0 - ymin * scale_y; |
| 1724 |
file << shift_x << " " << shift_y << " translate" << std::endl; |
| 1725 |
|
| 1726 |
file << "/Courier findfont" << std::endl; |
| 1727 |
file << "0.12 scalefont" << std::endl; |
| 1728 |
file << "setfont" << std::endl; |
| 1729 |
|
| 1730 |
|
| 1731 |
size_t num_steps(10); |
| 1732 |
double dxg((xmax - xmin) / num_steps); |
| 1733 |
double dyg((ymax - ymin) / num_steps); |
| 1734 |
|
| 1735 |
// Vertical Grid |
| 1736 |
for (size_t istep = 0; istep <= num_steps; istep++) |
| 1737 |
{ |
| 1738 |
double xx = xmin; |
| 1739 |
double yy = ymin + istep * dyg; |
| 1740 |
|
| 1741 |
std::stringstream ss; |
| 1742 |
ss << trunc((yy + 0.0001) * 1000) / 1000; |
| 1743 |
std::string str = ss.str(); |
| 1744 |
|
| 1745 |
file << "newpath " << (xx - dxg) * scale_x << " " << yy * scale_y << " moveto (" << str << ") show" << std::endl; |
| 1746 |
|
| 1747 |
file << "newpath" << std::endl; |
| 1748 |
file << xmin * scale_x << " " << yy * scale_y << " moveto" << std::endl; |
| 1749 |
file << xmax * scale_x << " " << yy * scale_y << " lineto" << std::endl; |
| 1750 |
file << " closepath" << std::endl; |
| 1751 |
file << " gsave" << std::endl; |
| 1752 |
file << " grestore" << std::endl; |
| 1753 |
file << " " << 0.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1754 |
file << " 0.01 setlinewidth" << std::endl; |
| 1755 |
file << " stroke" << std::endl; |
| 1756 |
} |
| 1757 |
|
| 1758 |
// Horizontal Grid |
| 1759 |
for (size_t istep = 0; istep <= num_steps; istep++) |
| 1760 |
{ |
| 1761 |
double xx = xmin + istep * dxg; |
| 1762 |
double yy = ymin; |
| 1763 |
|
| 1764 |
std::stringstream ss; |
| 1765 |
ss << trunc((xx + 0.001) * 100) / 100; |
| 1766 |
std::string str = ss.str(); |
| 1767 |
|
| 1768 |
file << "newpath " << xx * scale_x << " " << (yy - 0.2 * dyg) * scale_y << " moveto (" << str << ") show" << std::endl; |
| 1769 |
|
| 1770 |
file << "newpath" << std::endl; |
| 1771 |
file << xx * scale_x << " " << ymin * scale_y << " moveto" << std::endl; |
| 1772 |
file << xx * scale_x << " " << ymax * scale_y << " lineto" << std::endl; |
| 1773 |
file << " closepath" << std::endl; |
| 1774 |
file << " gsave" << std::endl; |
| 1775 |
file << " grestore" << std::endl; |
| 1776 |
file << " " << 0.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1777 |
file << " 0.01 setlinewidth" << std::endl; |
| 1778 |
file << " stroke" << std::endl; |
| 1779 |
} |
| 1780 |
|
| 1781 |
for (size_t ipiece = 0; ipiece < num_pieces; ipiece++) |
| 1782 |
{ |
| 1783 |
size_t num_steps(50); |
| 1784 |
double ds = (pmax[ipiece] - pmin[ipiece]) / num_steps; |
| 1785 |
for (size_t istep = 0; istep < num_steps; istep++) |
| 1786 |
{ |
| 1787 |
double x = pmin[ipiece] + istep * ds; |
| 1788 |
double f = 0.0; |
| 1789 |
for (size_t ibasis = 0; ibasis < num_basis[ipiece]; ibasis++) f += c[ipiece][ibasis] * pow(x, ibasis); |
| 1790 |
|
| 1791 |
double xp = x + ds; |
| 1792 |
double fp = 0.0; |
| 1793 |
for (size_t ibasis = 0; ibasis < num_basis[ipiece]; ibasis++) fp += c[ipiece][ibasis] * pow(xp, ibasis); |
| 1794 |
|
| 1795 |
file << "newpath" << std::endl; |
| 1796 |
file << x * scale_x << " " << f * scale_y << " moveto" << std::endl; |
| 1797 |
file << xp * scale_x << " " << fp * scale_y << " lineto" << std::endl; |
| 1798 |
file << " closepath" << std::endl; |
| 1799 |
file << " gsave" << std::endl; |
| 1800 |
file << " grestore" << std::endl; |
| 1801 |
file << " " << 1.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1802 |
file << " 0.01 setlinewidth" << std::endl; |
| 1803 |
file << " stroke" << std::endl; |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
for (size_t ipoint = 0; ipoint < num_points; ipoint++) |
| 1808 |
{ |
| 1809 |
file << "newpath" << std::endl; |
| 1810 |
file << px[ipoint] * scale_x << " " << py[ipoint] * scale_y << " " << 0.005 * scale_x << " 0 360 arc" << std::endl; |
| 1811 |
file << "closepath" << std::endl; |
| 1812 |
file << "gsave" << std::endl; |
| 1813 |
file << "0 0 1 setrgbcolor" << std::endl; // Inactive seeds |
| 1814 |
file << "fill" << std::endl; |
| 1815 |
file << "grestore" << std::endl; |
| 1816 |
file << "0.0 setlinewidth" << std::endl; |
| 1817 |
file << "0 0 0 setrgbcolor" << std::endl; // discs borders |
| 1818 |
file << "stroke " << std::endl; |
| 1819 |
} |
| 1820 |
|
| 1821 |
//#pragma endregion |
| 1822 |
} |
| 1823 |
|
| 1824 |
void VPS::plot_FourierExpansion(std::string file_name, size_t num_basis, double xmin, double xmax, double* a, double* b, size_t num_points, double* px, double* py, size_t* num_cbasis, double** c) |
| 1825 |
{ |
| 1826 |
//#pragma region Plot Fourier Expansion: |
| 1827 |
std::fstream file(file_name.c_str(), std::ios::out); |
| 1828 |
file << "%!PS-Adobe-3.0" << std::endl; |
| 1829 |
file << "72 72 scale % one unit = one inch" << std::endl; |
| 1830 |
|
| 1831 |
size_t num_p_points = 501; |
| 1832 |
double dx = (xmax - xmin) / (num_p_points - 1); |
| 1833 |
|
| 1834 |
|
| 1835 |
double DX = xmax - xmin; |
| 1836 |
|
| 1837 |
double ymin(DBL_MAX), ymax(-DBL_MAX); |
| 1838 |
for (size_t ipoint = 0; ipoint < num_p_points; ipoint++) |
| 1839 |
{ |
| 1840 |
// size_t jpoint = ipoint + 1; // ETP |
| 1841 |
|
| 1842 |
double x = xmin + ipoint * dx; |
| 1843 |
double f = 0.5 * a[0]; |
| 1844 |
for (size_t ibasis = 1; ibasis < num_basis; ibasis++) f += a[ibasis] * cos(2.0 * PI * ibasis * x / DX) + b[ibasis] * sin(2.0 * PI * ibasis * x / DX); |
| 1845 |
if (f < ymin) ymin = f; |
| 1846 |
if (f > ymax) ymax = f; |
| 1847 |
} |
| 1848 |
|
| 1849 |
double Lx(xmax - xmin); |
| 1850 |
double Ly(ymax - ymin); |
| 1851 |
|
| 1852 |
if (Ly < 1E-10){ ymin -= 0.5; ymax += 0.5; Ly = 1.0; } |
| 1853 |
|
| 1854 |
double scale_x, scale_y; |
| 1855 |
double shift_x, shift_y; |
| 1856 |
|
| 1857 |
scale_x = 6.5 / Lx; |
| 1858 |
scale_y = 9.0 / Ly; |
| 1859 |
|
| 1860 |
shift_x = 1.0 - xmin * scale_x; |
| 1861 |
shift_y = 1.0 - ymin * scale_y; |
| 1862 |
file << shift_x << " " << shift_y << " translate" << std::endl; |
| 1863 |
|
| 1864 |
file << "/Courier findfont" << std::endl; |
| 1865 |
file << "0.12 scalefont" << std::endl; |
| 1866 |
file << "setfont" << std::endl; |
| 1867 |
|
| 1868 |
|
| 1869 |
size_t num_steps(10); |
| 1870 |
double dxg((xmax - xmin) / num_steps); |
| 1871 |
double dyg((ymax - ymin) / num_steps); |
| 1872 |
|
| 1873 |
// Vertical Grid |
| 1874 |
for (size_t istep = 0; istep <= num_steps; istep++) |
| 1875 |
{ |
| 1876 |
double xx = xmin; |
| 1877 |
double yy = ymin + istep * dyg; |
| 1878 |
|
| 1879 |
std::stringstream ss; |
| 1880 |
ss << trunc((yy + 0.0001) * 1000) / 1000; |
| 1881 |
std::string str = ss.str(); |
| 1882 |
|
| 1883 |
file << "newpath " << (xx - dxg) * scale_x << " " << yy * scale_y << " moveto (" << str << ") show" << std::endl; |
| 1884 |
|
| 1885 |
file << "newpath" << std::endl; |
| 1886 |
file << xmin * scale_x << " " << yy * scale_y << " moveto" << std::endl; |
| 1887 |
file << xmax * scale_x << " " << yy * scale_y << " lineto" << std::endl; |
| 1888 |
file << " closepath" << std::endl; |
| 1889 |
file << " gsave" << std::endl; |
| 1890 |
file << " grestore" << std::endl; |
| 1891 |
file << " " << 0.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1892 |
file << " 0.01 setlinewidth" << std::endl; |
| 1893 |
file << " stroke" << std::endl; |
| 1894 |
} |
| 1895 |
|
| 1896 |
// Horizontal Grid |
| 1897 |
for (size_t istep = 0; istep <= num_steps; istep++) |
| 1898 |
{ |
| 1899 |
double xx = xmin + istep * dxg; |
| 1900 |
double yy = ymin; |
| 1901 |
|
| 1902 |
std::stringstream ss; |
| 1903 |
ss << trunc((xx + 0.001) * 100) / 100; |
| 1904 |
std::string str = ss.str(); |
| 1905 |
|
| 1906 |
file << "newpath " << xx * scale_x << " " << (yy - 0.2 * dyg) * scale_y << " moveto (" << str << ") show" << std::endl; |
| 1907 |
|
| 1908 |
file << "newpath" << std::endl; |
| 1909 |
file << xx * scale_x << " " << ymin * scale_y << " moveto" << std::endl; |
| 1910 |
file << xx * scale_x << " " << ymax * scale_y << " lineto" << std::endl; |
| 1911 |
file << " closepath" << std::endl; |
| 1912 |
file << " gsave" << std::endl; |
| 1913 |
file << " grestore" << std::endl; |
| 1914 |
file << " " << 0.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1915 |
file << " 0.01 setlinewidth" << std::endl; |
| 1916 |
file << " stroke" << std::endl; |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
for (size_t ipoint = 1; ipoint < num_p_points; ipoint++) |
| 1921 |
{ |
| 1922 |
double xm = xmin + (ipoint - 1) * dx; |
| 1923 |
double fm = 0.5 * a[0]; |
| 1924 |
for (size_t ibasis = 1; ibasis < num_basis; ibasis++) fm += a[ibasis] * cos(2.0 * PI * ibasis * xm / DX) + b[ibasis] * sin(2.0 * PI * ibasis * xm / DX); |
| 1925 |
|
| 1926 |
double xx = xm + dx; |
| 1927 |
double ff = 0.5 * a[0]; |
| 1928 |
for (size_t ibasis = 1; ibasis < num_basis; ibasis++) ff += a[ibasis] * cos(2.0 * PI * ibasis * xx / DX) + b[ibasis] * sin(2.0 * PI * ibasis * xx / DX); |
| 1929 |
|
| 1930 |
file << "newpath" << std::endl; |
| 1931 |
file << xm * scale_x << " " << fm * scale_y << " moveto" << std::endl; |
| 1932 |
file << xx * scale_x << " " << ff * scale_y << " lineto" << std::endl; |
| 1933 |
file << " closepath" << std::endl; |
| 1934 |
file << " gsave" << std::endl; |
| 1935 |
file << " grestore" << std::endl; |
| 1936 |
file << " " << 1.0 << " " << 0.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1937 |
file << " 0.01 setlinewidth" << std::endl; |
| 1938 |
file << " stroke" << std::endl; |
| 1939 |
|
| 1940 |
xm = xmin + (ipoint - 1) * dx; |
| 1941 |
fm = 0.5 * a[0]; |
| 1942 |
for (size_t ibasis = 1; ibasis < num_basis/2; ibasis++) fm += a[ibasis] * cos(2.0 * PI * ibasis * xm / DX) + b[ibasis] * sin(2.0 * PI * ibasis * xm / DX); |
| 1943 |
|
| 1944 |
xx = xm + dx; |
| 1945 |
ff = 0.5 * a[0]; |
| 1946 |
for (size_t ibasis = 1; ibasis < num_basis/2; ibasis++) ff += a[ibasis] * cos(2.0 * PI * ibasis * xx / DX) + b[ibasis] * sin(2.0 * PI * ibasis * xx / DX); |
| 1947 |
|
| 1948 |
file << "newpath" << std::endl; |
| 1949 |
file << xm * scale_x << " " << fm * scale_y << " moveto" << std::endl; |
| 1950 |
file << xx * scale_x << " " << ff * scale_y << " lineto" << std::endl; |
| 1951 |
file << " closepath" << std::endl; |
| 1952 |
file << " gsave" << std::endl; |
| 1953 |
file << " grestore" << std::endl; |
| 1954 |
file << " " << 0.0 << " " << 0.0 << " " << 1.0 << " setrgbcolor" << std::endl; |
| 1955 |
file << " 0.01 setlinewidth" << std::endl; |
| 1956 |
file << " stroke" << std::endl; |
| 1957 |
} |
| 1958 |
|
| 1959 |
size_t num_pieces = num_points - 1; |
| 1960 |
for (size_t ipiece = 0; ipiece < num_pieces; ipiece++) |
| 1961 |
{ |
| 1962 |
size_t num_steps(50); |
| 1963 |
double pmin = px[ipiece]; double pmax = px[ipiece + 1]; |
| 1964 |
double ds = (pmax - pmin) / num_steps; |
| 1965 |
for (size_t istep = 0; istep < num_steps; istep++) |
| 1966 |
{ |
| 1967 |
double x = pmin + istep * ds; |
| 1968 |
double f = 0.0; |
| 1969 |
for (size_t ibasis = 0; ibasis < num_cbasis[ipiece]; ibasis++) f += c[ipiece][ibasis] * pow(x, ibasis); |
| 1970 |
|
| 1971 |
double xp = x + ds; |
| 1972 |
double fp = 0.0; |
| 1973 |
for (size_t ibasis = 0; ibasis < num_cbasis[ipiece]; ibasis++) fp += c[ipiece][ibasis] * pow(xp, ibasis); |
| 1974 |
|
| 1975 |
file << "newpath" << std::endl; |
| 1976 |
file << x * scale_x << " " << f * scale_y << " moveto" << std::endl; |
| 1977 |
file << xp * scale_x << " " << fp * scale_y << " lineto" << std::endl; |
| 1978 |
file << " closepath" << std::endl; |
| 1979 |
file << " gsave" << std::endl; |
| 1980 |
file << " grestore" << std::endl; |
| 1981 |
file << " " << 0.0 << " " << 1.0 << " " << 0.0 << " setrgbcolor" << std::endl; |
| 1982 |
file << " 0.01 setlinewidth" << std::endl; |
| 1983 |
file << " stroke" << std::endl; |
| 1984 |
} |
| 1985 |
} |
| 1986 |
|
| 1987 |
for (size_t ipoint = 0; ipoint < num_points; ipoint++) |
| 1988 |
{ |
| 1989 |
file << "newpath" << std::endl; |
| 1990 |
file << px[ipoint] * scale_x << " " << py[ipoint] * scale_y << " " << 0.005 * scale_x << " 0 360 arc" << std::endl; |
| 1991 |
file << "closepath" << std::endl; |
| 1992 |
file << "gsave" << std::endl; |
| 1993 |
file << "0 0 1 setrgbcolor" << std::endl; // Inactive seeds |
| 1994 |
file << "fill" << std::endl; |
| 1995 |
file << "grestore" << std::endl; |
| 1996 |
file << "0.0 setlinewidth" << std::endl; |
| 1997 |
file << "0 0 0 setrgbcolor" << std::endl; // discs borders |
| 1998 |
file << "stroke " << std::endl; |
| 1999 |
} |
| 2000 |
|
| 2001 |
//#pragma endregion |
| 2002 |
} |
| 2003 |
|
| 2004 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2005 |
/////////// VPS General Methods ////////////////////////////////////////////////////////////////////////////////////////// |
| 2006 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2007 |
|
| 2008 |
int VPS::find_domain_diagonal() |
| 2009 |
{ |
| 2010 |
//#pragma region Calculate Domain Diagonal |
| 2011 |
_diag = 0.0; |
| 2012 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 2013 |
{ |
| 2014 |
double dx = _xmax[idim] - _xmin[idim]; |
| 2015 |
_diag += dx * dx; |
| 2016 |
} |
| 2017 |
_diag = sqrt(_diag); |
| 2018 |
return 0; |
| 2019 |
//#pragma endregion |
| 2020 |
} |
| 2021 |
|
| 2022 |
void VPS::plot_delaunay_graph(const std::string outFile) |
| 2023 |
{ |
| 2024 |
//#pragma region Plot a Delaunay Mesh: |
| 2025 |
std::fstream file(outFile.c_str(), std::ios::out); |
| 2026 |
|
| 2027 |
file << "%!PS-Adobe-3.0" << std::endl; |
| 2028 |
file << "72 72 scale % one unit = one inch" << std::endl; |
| 2029 |
|
| 2030 |
double Lx(_xmax[0] - _xmin[0]), Ly(_xmax[1] - _xmin[1]); |
| 2031 |
|
| 2032 |
double scale_x, scale_y, scale; |
| 2033 |
double shift_x, shift_y; |
| 2034 |
|
| 2035 |
scale_x = 6.5 / Lx; |
| 2036 |
scale_y = 9.0 / Ly; |
| 2037 |
|
| 2038 |
if (scale_x < scale_y) |
| 2039 |
{ |
| 2040 |
scale = scale_x; |
| 2041 |
shift_x = 1.0 - _xmin[0] * scale; |
| 2042 |
shift_y = 0.5 * (11.0 - Ly * scale) - _xmin[1] * scale; |
| 2043 |
} |
| 2044 |
else |
| 2045 |
{ |
| 2046 |
scale = scale_y; |
| 2047 |
shift_x = 0.5 * (8.5 - Lx * scale) - _xmin[0] * scale; |
| 2048 |
shift_y = 1.0 - _xmin[1] * scale; |
| 2049 |
} |
| 2050 |
file << shift_x << " " << shift_y << " translate" << std::endl; |
| 2051 |
|
| 2052 |
|
| 2053 |
file << "/Courier findfont" << std::endl; |
| 2054 |
file << "0.12 scalefont" << std::endl; |
| 2055 |
file << "setfont" << std::endl; |
| 2056 |
|
| 2057 |
for (size_t i = 0; i < _num_samples; i++) |
| 2058 |
{ |
| 2059 |
for (size_t j = 0; j < _seed_neighbors[i][1]; j++) |
| 2060 |
{ |
| 2061 |
size_t neighbor = _seed_neighbors[i][2 + j]; |
| 2062 |
|
| 2063 |
file << "newpath" << std::endl; |
| 2064 |
// draw a line between isample and neighbor |
| 2065 |
file << _x[i][0] * scale << " " << _x[i][1] * scale << " "; |
| 2066 |
file << "moveto" << std::endl; |
| 2067 |
file << _x[neighbor][0] * scale << " " << _x[neighbor][1] * scale << " "; |
| 2068 |
file << "lineto" << std::endl; |
| 2069 |
file << "0.0 setlinewidth" << std::endl; |
| 2070 |
file << "0 0 0 setrgbcolor" << std::endl; // Element borders |
| 2071 |
file << "stroke " << std::endl; |
| 2072 |
} |
| 2073 |
} |
| 2074 |
|
| 2075 |
for (size_t i = 0; i < _num_samples; i++) |
| 2076 |
{ |
| 2077 |
file << "newpath" << std::endl; |
| 2078 |
file << _x[i][0] * scale << " " << _x[i][1] * scale << " " << 0.005 * scale << " 0 360 arc" << std::endl; |
| 2079 |
file << "closepath" << std::endl; |
| 2080 |
file << "gsave" << std::endl; |
| 2081 |
file << "0 0 0 setrgbcolor" << std::endl; // Inactive seeds |
| 2082 |
file << "fill" << std::endl; |
| 2083 |
file << "grestore" << std::endl; |
| 2084 |
file << "0.0 setlinewidth" << std::endl; |
| 2085 |
file << "0 0 0 setrgbcolor" << std::endl; // discs borders |
| 2086 |
file << "stroke " << std::endl; |
| 2087 |
|
| 2088 |
std::stringstream ss; |
| 2089 |
ss << i; |
| 2090 |
std::string str = ss.str(); |
| 2091 |
|
| 2092 |
file << "newpath " << _x[i][0] * scale << " " << _x[i][1] * scale << " moveto (" << str << ") show" << std::endl; |
| 2093 |
} |
| 2094 |
|
| 2095 |
for (size_t i = 0; i < _num_vs; i++) |
| 2096 |
{ |
| 2097 |
if (_vs[i] == 0) continue; |
| 2098 |
|
| 2099 |
file << "newpath" << std::endl; |
| 2100 |
file << _vs[i][0] * scale << " " << _vs[i][1] * scale << " " << 0.005 * scale << " 0 360 arc" << std::endl; |
| 2101 |
file << "closepath" << std::endl; |
| 2102 |
file << "gsave" << std::endl; |
| 2103 |
file << "1 0 0 setrgbcolor" << std::endl; // Inactive seeds |
| 2104 |
file << "fill" << std::endl; |
| 2105 |
file << "grestore" << std::endl; |
| 2106 |
file << "0.0 setlinewidth" << std::endl; |
| 2107 |
file << "1 0 0 setrgbcolor" << std::endl; // discs borders |
| 2108 |
file << "stroke " << std::endl; |
| 2109 |
|
| 2110 |
std::stringstream ss; |
| 2111 |
ss << i; |
| 2112 |
std::string str = ss.str(); |
| 2113 |
|
| 2114 |
file << "newpath " << _vs[i][0] * scale << " " << _vs[i][1] * scale << " moveto (" << str << ") show" << std::endl; |
| 2115 |
} |
| 2116 |
|
| 2117 |
// draw bounding box |
| 2118 |
file << "newpath" << std::endl; |
| 2119 |
file << _xmin[0] * scale << " " << _xmin[1] * scale << " "; |
| 2120 |
file << "moveto" << std::endl; |
| 2121 |
file << _xmax[0] * scale << " " << _xmin[1] * scale << " "; |
| 2122 |
file << "lineto" << std::endl; |
| 2123 |
file << _xmax[0] * scale << " " << _xmax[1] * scale << " "; |
| 2124 |
file << "lineto" << std::endl; |
| 2125 |
file << _xmin[0] * scale << " " << _xmax[1] * scale << " "; |
| 2126 |
file << "lineto" << std::endl; |
| 2127 |
file << _xmin[0] * scale << " " << _xmin[1] * scale << " "; |
| 2128 |
file << "lineto" << std::endl; |
| 2129 |
file << "0.02 setlinewidth" << std::endl; |
| 2130 |
file << "0 0 1 setrgbcolor" << std::endl; // Element borders |
| 2131 |
file << "stroke " << std::endl; |
| 2132 |
|
| 2133 |
file << "showpage" << std::endl; |
| 2134 |
//#pragma endregion |
| 2135 |
} |
| 2136 |
|
| 2137 |
void VPS::plot_vps_surrogate(std::string file_name, size_t function_index, size_t num_contours, bool plot_graph) |
| 2138 |
{ |
| 2139 |
//#pragma region Plot Solid Isocontours: |
| 2140 |
//std::cout << ".: VPS Debug Mode :. Plotting ps files .... " << std::endl; |
| 2141 |
|
| 2142 |
std::fstream file(file_name.c_str(), std::ios::out); |
| 2143 |
file << "%!PS-Adobe-3.0" << std::endl; |
| 2144 |
file << "72 72 scale % one unit = one inch" << std::endl; |
| 2145 |
|
| 2146 |
double xmin(_xmin[0]); |
| 2147 |
double ymin(_xmin[1]); |
| 2148 |
double Lx(_xmax[0] - _xmin[0]); |
| 2149 |
double Ly(_xmax[1] - _xmin[0]); |
| 2150 |
|
| 2151 |
double scale_x, scale_y, scale; |
| 2152 |
double shift_x, shift_y; |
| 2153 |
|
| 2154 |
scale_x = 6.5 / Lx; |
| 2155 |
scale_y = 9.0 / Ly; |
| 2156 |
|
| 2157 |
if (scale_x < scale_y) |
| 2158 |
{ |
| 2159 |
scale = scale_x; |
| 2160 |
shift_x = 1.0 - xmin * scale; |
| 2161 |
shift_y = 0.5 * (11.0 - Ly * scale) - ymin * scale; |
| 2162 |
} |
| 2163 |
else |
| 2164 |
{ |
| 2165 |
scale = scale_y; |
| 2166 |
shift_x = 0.5 * (8.5 - Lx * scale) - xmin * scale; |
| 2167 |
shift_y = 1.0 - ymin * scale; |
| 2168 |
} |
| 2169 |
file << shift_x << " " << shift_y << " translate" << std::endl; |
| 2170 |
|
| 2171 |
file << "/redseg % stack: x1 y1 x2 y2" << std::endl; |
| 2172 |
file << "{newpath" << std::endl; |
| 2173 |
file << " moveto" << std::endl; |
| 2174 |
file << " lineto" << std::endl; |
| 2175 |
file << " closepath" << std::endl; |
| 2176 |
file << " gsave" << std::endl; |
| 2177 |
file << " grestore" << std::endl; |
| 2178 |
file << " 1 0 0 setrgbcolor" << std::endl; |
| 2179 |
file << " 0.01 setlinewidth" << std::endl; |
| 2180 |
file << " stroke" << std::endl; |
| 2181 |
file << "} def" << std::endl; |
| 2182 |
|
| 2183 |
file << "/greenseg % stack: x1 y1 x2 y2" << std::endl; |
| 2184 |
file << "{newpath" << std::endl; |
| 2185 |
file << " moveto" << std::endl; |
| 2186 |
file << " lineto" << std::endl; |
| 2187 |
file << " closepath" << std::endl; |
| 2188 |
file << " gsave" << std::endl; |
| 2189 |
file << " grestore" << std::endl; |
| 2190 |
file << " 0 1 0 setrgbcolor" << std::endl; |
| 2191 |
file << " 0.01 setlinewidth" << std::endl; |
| 2192 |
file << " stroke" << std::endl; |
| 2193 |
file << "} def" << std::endl; |
| 2194 |
|
| 2195 |
|
| 2196 |
file << "/blueseg % stack: x1 y1 x2 y2" << std::endl; |
| 2197 |
file << "{newpath" << std::endl; |
| 2198 |
file << " moveto" << std::endl; |
| 2199 |
file << " lineto" << std::endl; |
| 2200 |
file << " closepath" << std::endl; |
| 2201 |
file << " gsave" << std::endl; |
| 2202 |
file << " grestore" << std::endl; |
| 2203 |
file << " 0 0 1 setrgbcolor" << std::endl; |
| 2204 |
file << " 0.02 setlinewidth" << std::endl; |
| 2205 |
file << " stroke" << std::endl; |
| 2206 |
file << "} def" << std::endl; |
| 2207 |
|
| 2208 |
file << "/blackquad % stack: x1 y1 x2 y2 x3 y3 x4 y4" << std::endl; |
| 2209 |
file << "{newpath" << std::endl; |
| 2210 |
file << " moveto" << std::endl; |
| 2211 |
file << " lineto" << std::endl; |
| 2212 |
file << " lineto" << std::endl; |
| 2213 |
file << " lineto" << std::endl; |
| 2214 |
file << " closepath" << std::endl; |
| 2215 |
file << " gsave" << std::endl; |
| 2216 |
file << " grestore" << std::endl; |
| 2217 |
file << " 0 0 0 setrgbcolor" << std::endl; |
| 2218 |
file << " 0.02 setlinewidth" << std::endl; |
| 2219 |
file << " stroke" << std::endl; |
| 2220 |
file << "} def" << std::endl; |
| 2221 |
|
| 2222 |
file << "/circ % stack: x y r" << std::endl; |
| 2223 |
file << "{0 360 arc" << std::endl; |
| 2224 |
file << " closepath" << std::endl; |
| 2225 |
file << " 0.002 setlinewidth" << std::endl; |
| 2226 |
file << " stroke" << std::endl; |
| 2227 |
file << "} def" << std::endl; |
| 2228 |
|
| 2229 |
file << "/blackfcirc % stack: x y r" << std::endl; |
| 2230 |
file << "{0 360 arc" << std::endl; |
| 2231 |
file << " closepath" << std::endl; |
| 2232 |
file << " gsave" << std::endl; |
| 2233 |
file << " 0 0 0 setrgbcolor" << std::endl; |
| 2234 |
file << " fill" << std::endl; |
| 2235 |
file << " grestore" << std::endl; |
| 2236 |
file << " 0 0 0 setrgbcolor" << std::endl; |
| 2237 |
file << " 0.0 setlinewidth" << std::endl; |
| 2238 |
file << " stroke" << std::endl; |
| 2239 |
file << "} def" << std::endl; |
| 2240 |
|
| 2241 |
file << "/redfcirc % stack: x y r" << std::endl; |
| 2242 |
file << "{0 360 arc" << std::endl; |
| 2243 |
file << " closepath" << std::endl; |
| 2244 |
file << " gsave" << std::endl; |
| 2245 |
file << " 1 0 0 setrgbcolor" << std::endl; |
| 2246 |
file << " fill" << std::endl; |
| 2247 |
file << " grestore" << std::endl; |
| 2248 |
file << " 0 0 0 setrgbcolor" << std::endl; |
| 2249 |
file << " 0.0 setlinewidth" << std::endl; |
| 2250 |
file << " stroke" << std::endl; |
| 2251 |
file << "} def" << std::endl; |
| 2252 |
|
| 2253 |
file << "/bluefcirc % stack: x y r" << std::endl; |
| 2254 |
file << "{0 360 arc" << std::endl; |
| 2255 |
file << " closepath" << std::endl; |
| 2256 |
file << " gsave" << std::endl; |
| 2257 |
file << " 0 0 1 setrgbcolor" << std::endl; |
| 2258 |
file << " fill" << std::endl; |
| 2259 |
file << " grestore" << std::endl; |
| 2260 |
file << " 0 0 0 setrgbcolor" << std::endl; |
| 2261 |
file << " 0.0 setlinewidth" << std::endl; |
| 2262 |
file << " stroke" << std::endl; |
| 2263 |
file << "} def" << std::endl; |
| 2264 |
|
| 2265 |
file << "/greenfcirc % stack: x y r" << std::endl; |
| 2266 |
file << "{0 360 arc" << std::endl; |
| 2267 |
file << " closepath" << std::endl; |
| 2268 |
file << " gsave" << std::endl; |
| 2269 |
file << " 0 1 0 setrgbcolor" << std::endl; |
| 2270 |
file << " fill" << std::endl; |
| 2271 |
file << " grestore" << std::endl; |
| 2272 |
file << " 0 0 0 setrgbcolor" << std::endl; |
| 2273 |
file << " 0.0 setlinewidth" << std::endl; |
| 2274 |
file << " stroke" << std::endl; |
| 2275 |
file << "} def" << std::endl; |
| 2276 |
|
| 2277 |
file << "/quad_white % stack: x1 y1 x2 y2 x3 y3 x4 y4" << std::endl; |
| 2278 |
file << "{newpath" << std::endl; |
| 2279 |
file << " moveto" << std::endl; |
| 2280 |
file << " lineto" << std::endl; |
| 2281 |
file << " lineto" << std::endl; |
| 2282 |
file << " lineto" << std::endl; |
| 2283 |
file << " closepath" << std::endl; |
| 2284 |
file << " gsave" << std::endl; |
| 2285 |
file << " 1.0 setgray fill" << std::endl; |
| 2286 |
file << " grestore" << std::endl; |
| 2287 |
file << "} def" << std::endl; |
| 2288 |
|
| 2289 |
file << "/quad_bold % stack: x1 y1 x2 y2 x3 y3 x4 y4" << std::endl; |
| 2290 |
file << "{newpath" << std::endl; |
| 2291 |
file << " moveto" << std::endl; |
| 2292 |
file << " lineto" << std::endl; |
| 2293 |
file << " lineto" << std::endl; |
| 2294 |
file << " lineto" << std::endl; |
| 2295 |
file << " closepath" << std::endl; |
| 2296 |
file << " gsave" << std::endl; |
| 2297 |
file << " grestore" << std::endl; |
| 2298 |
file << " 0 0 0 setrgbcolor" << std::endl; |
| 2299 |
file << " 0.01 setlinewidth" << std::endl; |
| 2300 |
file << " stroke" << std::endl; |
| 2301 |
file << "} def" << std::endl; |
| 2302 |
|
| 2303 |
file << "/Courier findfont" << std::endl; |
| 2304 |
file << "0.12 scalefont" << std::endl; |
| 2305 |
file << "setfont" << std::endl; |
| 2306 |
|
| 2307 |
std::vector<double> poly_x; |
| 2308 |
std::vector<double> poly_y; |
| 2309 |
|
| 2310 |
size_t num_cells(100); |
| 2311 |
double* xx = new double[_num_dim]; |
| 2312 |
double* f = new double[_num_functions]; |
| 2313 |
double sx = (_xmax[0] - _xmin[0]) / num_cells; |
| 2314 |
double sy = (_xmax[1] - _xmin[1]) / num_cells; |
| 2315 |
|
| 2316 |
|
| 2317 |
double fmin, fmax; |
| 2318 |
|
| 2319 |
for (size_t i = 0; i < num_cells; i++) |
| 2320 |
{ |
| 2321 |
xx[0] = _xmin[0] + i * sx; |
| 2322 |
for (size_t j = 0; j < num_cells; j++) |
| 2323 |
{ |
| 2324 |
xx[1] = _xmin[1] + j * sy; |
| 2325 |
evaluate_surrogate(xx, f); |
| 2326 |
if (i == 0) |
| 2327 |
{ |
| 2328 |
fmin = f[function_index]; |
| 2329 |
fmax = f[function_index]; |
| 2330 |
continue; |
| 2331 |
} |
| 2332 |
if (f[function_index] < fmin) fmin = f[function_index]; |
| 2333 |
if (f[function_index] > fmax) fmax = f[function_index]; |
| 2334 |
} |
| 2335 |
} |
| 2336 |
|
| 2337 |
std::vector<double> contours; |
| 2338 |
contours.push_back(fmin - 2 * (fmax - fmin)); |
| 2339 |
for (size_t i = 0; i < num_contours; i++) contours.push_back(fmin + (1.0 / num_contours) * i * (fmax - fmin)); |
| 2340 |
contours.push_back(fmax + 2 * (fmax - fmin)); |
| 2341 |
|
| 2342 |
for (size_t i = 0; i < num_cells; i++) |
| 2343 |
{ |
| 2344 |
double xo = _xmin[0] + i * sx; |
| 2345 |
for (size_t j = 0; j < num_cells; j++) |
| 2346 |
{ |
| 2347 |
double fo(0.0), f1(0.0), f2(0.0), f3(0.0); |
| 2348 |
|
| 2349 |
double yo = _xmin[1] + j * sy; |
| 2350 |
xx[0] = xo; xx[1] = yo; |
| 2351 |
evaluate_surrogate(xx, f); |
| 2352 |
fo = f[function_index]; |
| 2353 |
|
| 2354 |
xx[0] = xo + sx; xx[1] = yo; |
| 2355 |
evaluate_surrogate(xx, f); |
| 2356 |
f1 = f[function_index]; |
| 2357 |
|
| 2358 |
xx[0] = xo + sx; xx[1] = yo + sy; |
| 2359 |
evaluate_surrogate(xx, f); |
| 2360 |
f2 = f[function_index]; |
| 2361 |
|
| 2362 |
xx[0] = xo; xx[1] = yo + sy; |
| 2363 |
evaluate_surrogate(xx, f); |
| 2364 |
f3 = f[function_index]; |
| 2365 |
|
| 2366 |
|
| 2367 |
size_t num_isocontours = contours.size(); |
| 2368 |
for (size_t icont = 0; icont < num_isocontours; icont++) |
| 2369 |
{ |
| 2370 |
double contour = contours[icont]; |
| 2371 |
double contour_m = -1000.00; |
| 2372 |
if (icont > 0) contour_m = contours[icont - 1]; |
| 2373 |
|
| 2374 |
//std::cout<< "contour_m = " << contour_m << " , contour = " << contour << std::endl; |
| 2375 |
|
| 2376 |
poly_x.clear(); poly_y.clear(); |
| 2377 |
|
| 2378 |
// moving right |
| 2379 |
if (fo >= contour_m - 1E-10 && fo < contour + 1E-10) |
| 2380 |
{ |
| 2381 |
poly_x.push_back(xo); |
| 2382 |
poly_y.push_back(yo); |
| 2383 |
if ((fo > contour && f1 < contour) || (fo < contour && f1 > contour)) |
| 2384 |
{ |
| 2385 |
double h = sx * (contour - fo) / (f1 - fo); |
| 2386 |
poly_x.push_back(xo + h); |
| 2387 |
poly_y.push_back(yo); |
| 2388 |
} |
| 2389 |
else if ((fo > contour_m && f1 < contour_m) || (fo < contour_m && f1 > contour_m)) |
| 2390 |
{ |
| 2391 |
double h = sx * (contour_m - fo) / (f1 - fo); |
| 2392 |
poly_x.push_back(xo + h); |
| 2393 |
poly_y.push_back(yo); |
| 2394 |
} |
| 2395 |
} |
| 2396 |
else if ((fo > contour_m && f1 < contour_m) || (fo < contour_m && f1 > contour_m)) |
| 2397 |
{ |
| 2398 |
double hm = sx * (contour_m - fo) / (f1 - fo); |
| 2399 |
double h = hm; |
| 2400 |
if ((fo > contour && f1 < contour) || (fo < contour && f1 > contour)) |
| 2401 |
{ |
| 2402 |
h = sx * (contour - fo) / (f1 - fo); |
| 2403 |
} |
| 2404 |
if (h < hm) |
| 2405 |
{ |
| 2406 |
double tmp = h; h = hm; hm = tmp; |
| 2407 |
} |
| 2408 |
poly_x.push_back(xo + hm); |
| 2409 |
poly_y.push_back(yo); |
| 2410 |
|
| 2411 |
if (h - hm > 1E-10) |
| 2412 |
{ |
| 2413 |
poly_x.push_back(xo + h); |
| 2414 |
poly_y.push_back(yo); |
| 2415 |
} |
| 2416 |
} |
| 2417 |
else if ((fo > contour && f1 < contour) || (fo < contour && f1 > contour)) |
| 2418 |
{ |
| 2419 |
double h = sx * (contour - fo) / (f1 - fo); |
| 2420 |
poly_x.push_back(xo + h); |
| 2421 |
poly_y.push_back(yo); |
| 2422 |
} |
| 2423 |
|
| 2424 |
// moving up |
| 2425 |
if (f1 >= contour_m - 1E-10 && f1 < contour + 1E-10) |
| 2426 |
{ |
| 2427 |
poly_x.push_back(xo + sx); |
| 2428 |
poly_y.push_back(yo); |
| 2429 |
if ((f1 > contour && f2 < contour) || (f1 < contour && f2 > contour)) |
| 2430 |
{ |
| 2431 |
double h = sy * (contour - f1) / (f2 - f1); |
| 2432 |
poly_x.push_back(xo + sx); |
| 2433 |
poly_y.push_back(yo + h); |
| 2434 |
} |
| 2435 |
else if ((f1 > contour_m && f2 < contour_m) || (f1 < contour_m && f2 > contour_m)) |
| 2436 |
{ |
| 2437 |
double h = sy * (contour_m - f1) / (f2 - f1); |
| 2438 |
poly_x.push_back(xo + sx); |
| 2439 |
poly_y.push_back(yo + h); |
| 2440 |
} |
| 2441 |
|
| 2442 |
} |
| 2443 |
else if ((f1 > contour_m && f2 < contour_m) || (f1 < contour_m && f2 > contour_m)) |
| 2444 |
{ |
| 2445 |
double hm = sy * (contour_m - f1) / (f2 - f1); |
| 2446 |
double h = hm; |
| 2447 |
if ((f1 > contour && f2 < contour) || (f1 < contour && f2 > contour)) |
| 2448 |
{ |
| 2449 |
h = sy * (contour - f1) / (f2 - f1); |
| 2450 |
} |
| 2451 |
if (h < hm) |
| 2452 |
{ |
| 2453 |
double tmp = h; h = hm; hm = tmp; |
| 2454 |
} |
| 2455 |
poly_x.push_back(xo + sx); |
| 2456 |
poly_y.push_back(yo + hm); |
| 2457 |
|
| 2458 |
if (h - hm > 1E-10) |
| 2459 |
{ |
| 2460 |
poly_x.push_back(xo + sx); |
| 2461 |
poly_y.push_back(yo + h); |
| 2462 |
} |
| 2463 |
} |
| 2464 |
else if ((f1 > contour && f2 < contour) || (f1 < contour && f2 > contour)) |
| 2465 |
{ |
| 2466 |
double h = sy * (contour - f1) / (f2 - f1); |
| 2467 |
poly_x.push_back(xo + sx); |
| 2468 |
poly_y.push_back(yo + h); |
| 2469 |
} |
| 2470 |
|
| 2471 |
// moving left |
| 2472 |
if (f2 >= contour_m - 1E-10 && f2 < contour + 1E-10) |
| 2473 |
{ |
| 2474 |
poly_x.push_back(xo + sx); |
| 2475 |
poly_y.push_back(yo + sy); |
| 2476 |
if ((f2 > contour && f3 < contour) || (f2 < contour && f3 > contour)) |
| 2477 |
{ |
| 2478 |
double h = sx * (contour - f2) / (f3 - f2); |
| 2479 |
poly_x.push_back(xo + sx - h); |
| 2480 |
poly_y.push_back(yo + sy); |
| 2481 |
} |
| 2482 |
else if ((f2 > contour_m && f3 < contour_m) || (f2 < contour_m && f3 > contour_m)) |
| 2483 |
{ |
| 2484 |
double h = sx * (contour_m - f2) / (f3 - f2); |
| 2485 |
poly_x.push_back(xo + sx - h); |
| 2486 |
poly_y.push_back(yo + sy); |
| 2487 |
} |
| 2488 |
} |
| 2489 |
else if ((f2 > contour_m && f3 < contour_m) || (f2 < contour_m && f3 > contour_m)) |
| 2490 |
{ |
| 2491 |
double hm = sx * (contour_m - f2) / (f3 - f2); |
| 2492 |
double h = hm; |
| 2493 |
if ((f2 > contour && f3 < contour) || (f2 < contour && f3 > contour)) |
| 2494 |
{ |
| 2495 |
h = sx * (contour - f2) / (f3 - f2); |
| 2496 |
} |
| 2497 |
if (h < hm) |
| 2498 |
{ |
| 2499 |
double tmp = h; h = hm; hm = tmp; |
| 2500 |
} |
| 2501 |
poly_x.push_back(xo + sx - hm); |
| 2502 |
poly_y.push_back(yo + sy); |
| 2503 |
|
| 2504 |
if (h - hm > 1E-10) |
| 2505 |
{ |
| 2506 |
poly_x.push_back(xo + sx - h); |
| 2507 |
poly_y.push_back(yo + sy); |
| 2508 |
} |
| 2509 |
} |
| 2510 |
else if ((f2 > contour && f3 < contour) || (f2 < contour && f3 > contour)) |
| 2511 |
{ |
| 2512 |
double h = sx * (contour - f2) / (f3 - f2); |
| 2513 |
poly_x.push_back(xo + sx - h); |
| 2514 |
poly_y.push_back(yo + sy); |
| 2515 |
} |
| 2516 |
|
| 2517 |
// moving down |
| 2518 |
if (f3 >= contour_m - 1E-10 && f3 < contour + 1E-10) |
| 2519 |
{ |
| 2520 |
poly_x.push_back(xo); |
| 2521 |
poly_y.push_back(yo + sy); |
| 2522 |
if ((f3 > contour && fo < contour) || (f3 < contour && fo > contour)) |
| 2523 |
{ |
| 2524 |
double h = sy * (contour - f3) / (fo - f3); |
| 2525 |
poly_x.push_back(xo); |
| 2526 |
poly_y.push_back(yo + sy - h); |
| 2527 |
} |
| 2528 |
else if ((f3 > contour_m && fo < contour_m) || (f3 < contour_m && fo > contour_m)) |
| 2529 |
{ |
| 2530 |
double h = sy * (contour_m - f3) / (fo - f3); |
| 2531 |
poly_x.push_back(xo); |
| 2532 |
poly_y.push_back(yo + sy - h); |
| 2533 |
} |
| 2534 |
} |
| 2535 |
else if ((f3 > contour_m && fo < contour_m) || (f3 < contour_m && fo > contour_m)) |
| 2536 |
{ |
| 2537 |
double hm = sy * (contour_m - f3) / (fo - f3); |
| 2538 |
double h = hm; |
| 2539 |
if ((f3 > contour && fo < contour) || (f3 < contour && fo > contour)) |
| 2540 |
{ |
| 2541 |
h = sy * (contour - f3) / (fo - f3); |
| 2542 |
} |
| 2543 |
if (h < hm) |
| 2544 |
{ |
| 2545 |
double tmp = h; h = hm; hm = tmp; |
| 2546 |
} |
| 2547 |
poly_x.push_back(xo); |
| 2548 |
poly_y.push_back(yo + sy - hm); |
| 2549 |
|
| 2550 |
if (h - hm > 1E-10) |
| 2551 |
{ |
| 2552 |
poly_x.push_back(xo); |
| 2553 |
poly_y.push_back(yo + sy - h); |
| 2554 |
} |
| 2555 |
} |
| 2556 |
else if ((f3 > contour && fo < contour) || (f3 < contour && fo > contour)) |
| 2557 |
{ |
| 2558 |
double h = sy * (contour - f3) / (fo - f3); |
| 2559 |
poly_x.push_back(xo); |
| 2560 |
poly_y.push_back(yo + sy - h); |
| 2561 |
} |
| 2562 |
|
| 2563 |
|
| 2564 |
size_t num_corners(poly_x.size()); |
| 2565 |
if (num_corners > 1) |
| 2566 |
{ |
| 2567 |
double gs = 1.0 - icont * 1.0 / num_isocontours; |
| 2568 |
file << "newpath" << std::endl; |
| 2569 |
file << poly_x[0] * scale << " " << poly_y[0] * scale << " moveto" << std::endl; |
| 2570 |
//std::cout<< "*** x = " << poly_x[0] << ", y = " << poly_y[0] << std::endl; |
| 2571 |
for (size_t icorner = 1; icorner < num_corners; icorner++) |
| 2572 |
{ |
| 2573 |
file << poly_x[icorner] * scale << " " << poly_y[icorner] * scale << " lineto" << std::endl; |
| 2574 |
//std::cout << "*** x = " << poly_x[icorner] << ", y = " << poly_y[icorner] << std::endl; |
| 2575 |
} |
| 2576 |
//std::cout << std::endl; |
| 2577 |
|
| 2578 |
file << "closepath" << std::endl; |
| 2579 |
file << "gsave" << std::endl; |
| 2580 |
file << "grestore" << std::endl; |
| 2581 |
|
| 2582 |
double r, g, b; |
| 2583 |
|
| 2584 |
if (gs < 0.25) r = 1.0; |
| 2585 |
//else if (gs < 0.5) r = 2.0 - 4.0 * gs; |
| 2586 |
else if (gs < 0.5) r = 1.0 - 16.0 * (gs - 0.25) * (gs - 0.25); |
| 2587 |
else r = 0.0; |
| 2588 |
|
| 2589 |
double go(0.25), gn(1.0 - go); |
| 2590 |
if (gs < go) g = gs / go; |
| 2591 |
else if (gs < gn) g = 1.0; |
| 2592 |
else g = 1.0 / (1.0 - gn) - gs / (1.0 - gn); |
| 2593 |
|
| 2594 |
|
| 2595 |
if (gs < 0.5) b = 0.0; |
| 2596 |
else if (gs < 0.75) b = 1.0 - 16.0 * (gs - 0.75) * (gs - 0.75); |
| 2597 |
else b = 1.0; |
| 2598 |
|
| 2599 |
file << r << " " << g << " " << b << " setrgbcolor" << std::endl; |
| 2600 |
|
| 2601 |
file << " fill" << std::endl; |
| 2602 |
} |
| 2603 |
} |
| 2604 |
} |
| 2605 |
} |
| 2606 |
delete[] xx; delete[] f; |
| 2607 |
|
| 2608 |
if (plot_graph) |
| 2609 |
{ |
| 2610 |
for (size_t i = 0; i < _num_samples; i++) |
| 2611 |
{ |
| 2612 |
for (size_t j = 0; j < _seed_neighbors[i][1]; j++) |
| 2613 |
{ |
| 2614 |
|
| 2615 |
if (_seed_disc_neighbors != 0 && _seed_disc_neighbors[i] != 0) |
| 2616 |
{ |
| 2617 |
if (_seed_disc_neighbors[i][function_index][j]) continue; |
| 2618 |
} |
| 2619 |
|
| 2620 |
size_t neighbor = _seed_neighbors[i][2 + j]; |
| 2621 |
file << "newpath" << std::endl; |
| 2622 |
// draw a line between isample and neighbor |
| 2623 |
file << _x[i][0] * scale << " " << _x[i][1] * scale << " "; |
| 2624 |
file << "moveto" << std::endl; |
| 2625 |
file << _x[neighbor][0] * scale << " " << _x[neighbor][1] * scale << " "; |
| 2626 |
file << "lineto" << std::endl; |
| 2627 |
file << "0.0 setlinewidth" << std::endl; |
| 2628 |
file << "0 0 0 setrgbcolor" << std::endl; // Element borders |
| 2629 |
file << "stroke " << std::endl; |
| 2630 |
} |
| 2631 |
} |
| 2632 |
|
| 2633 |
for (size_t i = 0; i < _num_samples; i++) |
| 2634 |
{ |
| 2635 |
file << "newpath" << std::endl; |
| 2636 |
file << _x[i][0] * scale << " " << _x[i][1] * scale << " " << 0.005 * scale << " 0 360 arc" << std::endl; |
| 2637 |
file << "closepath" << std::endl; |
| 2638 |
file << "gsave" << std::endl; |
| 2639 |
file << "0 0 0 setrgbcolor" << std::endl; // Inactive seeds |
| 2640 |
file << "fill" << std::endl; |
| 2641 |
file << "grestore" << std::endl; |
| 2642 |
file << "0.0 setlinewidth" << std::endl; |
| 2643 |
file << "0 0 0 setrgbcolor" << std::endl; // discs borders |
| 2644 |
file << "stroke " << std::endl; |
| 2645 |
|
| 2646 |
std::stringstream ss; |
| 2647 |
ss << i; |
| 2648 |
std::string str = ss.str(); |
| 2649 |
|
| 2650 |
file << "newpath " << _x[i][0] * scale << " " << _x[i][1] * scale << " moveto (" << str << ") show" << std::endl; |
| 2651 |
} |
| 2652 |
|
| 2653 |
for (size_t i = 0; i < _num_vs; i++) |
| 2654 |
{ |
| 2655 |
if (_vs[i] == 0) continue; |
| 2656 |
|
| 2657 |
file << "newpath" << std::endl; |
| 2658 |
file << _vs[i][0] * scale << " " << _vs[i][1] * scale << " " << 0.005 * scale << " 0 360 arc" << std::endl; |
| 2659 |
file << "closepath" << std::endl; |
| 2660 |
file << "gsave" << std::endl; |
| 2661 |
file << "1 0 0 setrgbcolor" << std::endl; // Inactive seeds |
| 2662 |
file << "fill" << std::endl; |
| 2663 |
file << "grestore" << std::endl; |
| 2664 |
file << "0.0 setlinewidth" << std::endl; |
| 2665 |
file << "1 0 0 setrgbcolor" << std::endl; // discs borders |
| 2666 |
file << "stroke " << std::endl; |
| 2667 |
|
| 2668 |
std::stringstream ss; |
| 2669 |
ss << i; |
| 2670 |
std::string str = ss.str(); |
| 2671 |
|
| 2672 |
file << "newpath " << _vs[i][0] * scale << " " << _vs[i][1] * scale << " moveto (" << str << ") show" << std::endl; |
| 2673 |
} |
| 2674 |
} |
| 2675 |
|
| 2676 |
double DX = _xmax[0] - _xmin[0]; |
| 2677 |
double DY = _xmax[1] - _xmin[1]; |
| 2678 |
|
| 2679 |
// plot domain boundaries |
| 2680 |
file << (_xmin[0] - DX) * scale << " " << _xmin[1] * scale << " "; |
| 2681 |
file << (_xmax[0] + DX) * scale << " " << _xmin[1] * scale << " "; |
| 2682 |
file << (_xmax[0] + DX) * scale << " " << (_xmin[1] - DY) * scale << " "; |
| 2683 |
file << (_xmin[0] - DX) * scale << " " << (_xmin[1] - DY) * scale << " "; |
| 2684 |
file << "quad_white" << std::endl; |
| 2685 |
|
| 2686 |
file << (_xmin[0] - DX) * scale << " " << _xmax[1] * scale << " "; |
| 2687 |
file << (_xmax[0] + DX) * scale << " " << _xmax[1] * scale << " "; |
| 2688 |
file << (_xmax[0] + DX) * scale << " " << (_xmax[1] + DY) * scale << " "; |
| 2689 |
file << (_xmin[0] - DX) * scale << " " << (_xmax[1] + DY) * scale << " "; |
| 2690 |
file << "quad_white" << std::endl; |
| 2691 |
|
| 2692 |
file << _xmax[0] * scale << " " << (_xmin[1] - DY) * scale << " "; |
| 2693 |
file << (_xmax[0] + DX) * scale << " " << (_xmin[1] - DY) * scale << " "; |
| 2694 |
file << (_xmax[0] + DX) * scale << " " << (_xmax[1] + DY) * scale << " "; |
| 2695 |
file << _xmax[0] * scale << " " << (_xmax[1] + DY) * scale << " "; |
| 2696 |
file << "quad_white" << std::endl; |
| 2697 |
|
| 2698 |
file << (_xmin[0] - DX) * scale << " " << (_xmin[1] - DY) * scale << " "; |
| 2699 |
file << _xmin[0] * scale << " " << (_xmin[1] - DY) * scale << " "; |
| 2700 |
file << _xmin[0] * scale << " " << (_xmax[1] + DY) * scale << " "; |
| 2701 |
file << (_xmin[0] - DX) * scale << " " << (_xmax[1] + DY) * scale << " "; |
| 2702 |
file << "quad_white" << std::endl; |
| 2703 |
|
| 2704 |
|
| 2705 |
// plot domain boundaries |
| 2706 |
file << _xmin[0] * scale << " " << _xmin[1] * scale << " "; |
| 2707 |
file << _xmax[0] * scale << " " << _xmin[1] * scale << " "; |
| 2708 |
file << _xmax[0] * scale << " " << _xmax[1] * scale << " "; |
| 2709 |
file << _xmin[0] * scale << " " << _xmax[1] * scale << " "; |
| 2710 |
file << "quad_bold" << std::endl; |
| 2711 |
|
| 2712 |
file << "showpage" << std::endl; |
| 2713 |
|
| 2714 |
//#pragma endregion |
| 2715 |
} |
| 2716 |
|
| 2717 |
|
| 2718 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2719 |
/////////// Delaunay Graph Methods /////////////////////////////////////////////////////////////////////////////////////// |
| 2720 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2721 |
|
| 2722 |
int VPS::construct_delaunay_graph() |
| 2723 |
{ |
| 2724 |
//#pragma region Construct Delaunay Graph: |
| 2725 |
|
| 2726 |
if (_seed_neighbors == 0) |
| 2727 |
{ |
| 2728 |
_seed_neighbors = new size_t*[_budget]; |
| 2729 |
_seed_box = new double*[_budget]; |
| 2730 |
_seed_rf = new double[_budget]; |
| 2731 |
_seed_rc = new double[_budget]; |
| 2732 |
for (size_t i = 0; i < _budget; i++) |
| 2733 |
{ |
| 2734 |
_seed_neighbors[i] = 0; |
| 2735 |
_seed_box[i] = 0; |
| 2736 |
_seed_rf[i] = 0; // ETP |
| 2737 |
_seed_rc[i] = 0; // ETP |
| 2738 |
} |
| 2739 |
} |
| 2740 |
|
| 2741 |
for (size_t i = 0; i < _num_samples; i++) |
| 2742 |
{ |
| 2743 |
update_delaunay_graph(i); |
| 2744 |
} |
| 2745 |
|
| 2746 |
if (_seed_disc_neighbors == 0) |
| 2747 |
{ |
| 2748 |
_seed_disc_neighbors = new bool**[_budget]; |
| 2749 |
|
| 2750 |
for (size_t i = 0; i < _budget; i++) _seed_disc_neighbors[i] = 0; |
| 2751 |
} |
| 2752 |
|
| 2753 |
for (size_t iseed = 0; iseed < _num_samples; iseed++) |
| 2754 |
{ |
| 2755 |
size_t num_neighbors; get_num_seed_neighbors(iseed, num_neighbors); |
| 2756 |
|
| 2757 |
if (num_neighbors == 0) continue; |
| 2758 |
|
| 2759 |
if (_seed_disc_neighbors[iseed] != 0) |
| 2760 |
{ |
| 2761 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) delete[] _seed_disc_neighbors[iseed][ifunc]; |
| 2762 |
delete[] _seed_disc_neighbors[iseed]; |
| 2763 |
} |
| 2764 |
_seed_disc_neighbors[iseed] = new bool*[_num_functions]; |
| 2765 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) |
| 2766 |
{ |
| 2767 |
_seed_disc_neighbors[iseed][ifunc] = new bool[num_neighbors]; |
| 2768 |
for (size_t i = 0; i < num_neighbors; i++) _seed_disc_neighbors[iseed][ifunc][i] = false; |
| 2769 |
} |
| 2770 |
} |
| 2771 |
|
| 2772 |
return 0; |
| 2773 |
|
| 2774 |
//#pragma endregion |
| 2775 |
} |
| 2776 |
|
| 2777 |
int VPS::update_delaunay_graph(size_t seed_index) |
| 2778 |
{ |
| 2779 |
//#pragma region Update Delaunay Graph of a given Seed: |
| 2780 |
size_t max_num_misses(5); |
| 2781 |
if (_num_samples > 19000) std::cout << "Del graph:" << seed_index << std::endl; |
| 2782 |
if (_seed_neighbors[seed_index] != 0) |
| 2783 |
{ |
| 2784 |
while (_seed_neighbors[seed_index][1] != 0) disconnect_seeds(seed_index, _seed_neighbors[seed_index][2]); |
| 2785 |
delete[] _seed_neighbors[seed_index]; |
| 2786 |
_seed_neighbors[seed_index] = 0; |
| 2787 |
} |
| 2788 |
|
| 2789 |
if (_seed_box[seed_index] == 0) _seed_box[seed_index] = new double[2 * _num_dim]; |
| 2790 |
|
| 2791 |
_seed_rc[seed_index] = 0; |
| 2792 |
_seed_rf[seed_index] = _diag; |
| 2793 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 2794 |
{ |
| 2795 |
_seed_box[seed_index][idim] = _x[seed_index][idim]; |
| 2796 |
_seed_box[seed_index][_num_dim + idim] = _x[seed_index][idim]; |
| 2797 |
} |
| 2798 |
|
| 2799 |
double* v = new double[_num_dim]; |
| 2800 |
|
| 2801 |
size_t num_misses(0), old_neighbors(0); |
| 2802 |
get_num_seed_neighbors(seed_index, old_neighbors); |
| 2803 |
while (num_misses < max_num_misses) |
| 2804 |
{ |
| 2805 |
sample_voronoi_vertex(seed_index, _xmin, _xmax, _diag, v); |
| 2806 |
|
| 2807 |
// update Coverage radius |
| 2808 |
double rc(0.0); |
| 2809 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 2810 |
{ |
| 2811 |
double dx = v[idim] - _x[seed_index][idim]; |
| 2812 |
rc += dx * dx; |
| 2813 |
} |
| 2814 |
rc = sqrt(rc); |
| 2815 |
if (rc > _seed_rc[seed_index]) _seed_rc[seed_index] = rc; |
| 2816 |
|
| 2817 |
// update bounding box for Voronoi Cell |
| 2818 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 2819 |
{ |
| 2820 |
if (v[idim] < _seed_box[seed_index][idim]) _seed_box[seed_index][idim] = v[idim]; |
| 2821 |
if (v[idim] > _seed_box[seed_index][_num_dim + idim]) _seed_box[seed_index][_num_dim + idim] = v[idim]; |
| 2822 |
} |
| 2823 |
|
| 2824 |
// Collect all seeds that are equidistant from xend |
| 2825 |
size_t num_neighbors(0); |
| 2826 |
size_t neighbors_capacity(100); |
| 2827 |
size_t* neighbors = new size_t[neighbors_capacity]; |
| 2828 |
|
| 2829 |
get_seeds_in_sphere_tree(v, rc + 1E-10, num_neighbors, neighbors, neighbors_capacity); |
| 2830 |
|
| 2831 |
// Sorting ensures |
| 2832 |
quicksort(neighbors, 0, num_neighbors - 1); |
| 2833 |
for (size_t i = 0; i < num_neighbors; i++) |
| 2834 |
{ |
| 2835 |
for (size_t j = i + 1; j < num_neighbors; j++) |
| 2836 |
{ |
| 2837 |
connect_seeds(neighbors[i], neighbors[j]); |
| 2838 |
} |
| 2839 |
} |
| 2840 |
|
| 2841 |
delete[] neighbors; |
| 2842 |
|
| 2843 |
size_t new_neighbors; |
| 2844 |
get_num_seed_neighbors(seed_index, new_neighbors); |
| 2845 |
|
| 2846 |
if (new_neighbors == old_neighbors) num_misses++; |
| 2847 |
else |
| 2848 |
{ |
| 2849 |
old_neighbors = new_neighbors; |
| 2850 |
num_misses = 0; |
| 2851 |
} |
| 2852 |
} |
| 2853 |
delete[] v; |
| 2854 |
return 0; |
| 2855 |
//#pragma endregion |
| 2856 |
} |
| 2857 |
|
| 2858 |
int VPS::sample_voronoi_vertex(double* v) |
| 2859 |
{ |
| 2860 |
//#pragma region Sample Voronoi Vertex: |
| 2861 |
double* dart = new double[_num_dim]; |
| 2862 |
for (size_t idim = 0; idim < _num_dim; idim++) dart[idim] = _xmin[idim] + generate_a_random_number() * (_xmax[idim] - _xmin[idim]); |
| 2863 |
|
| 2864 |
double closest_distance = _diag; size_t closest_seed; |
| 2865 |
get_closest_seed_tree(dart, closest_seed, closest_distance); |
| 2866 |
|
| 2867 |
sample_voronoi_vertex(closest_seed, _xmin, _xmax, _diag, v); |
| 2868 |
|
| 2869 |
delete[] dart; |
| 2870 |
return 0; |
| 2871 |
//#pragma endregion |
| 2872 |
} |
| 2873 |
|
| 2874 |
int VPS::connect_seeds(size_t seed_i, size_t seed_j) |
| 2875 |
{ |
| 2876 |
//#pragma region Connect Seeds: |
| 2877 |
|
| 2878 |
if (seed_i == seed_j) return 1; |
| 2879 |
|
| 2880 |
double dst(0.0); |
| 2881 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 2882 |
{ |
| 2883 |
double dx = _x[seed_i][idim] - _x[seed_j][idim]; |
| 2884 |
dst += dx * dx; |
| 2885 |
} |
| 2886 |
dst = sqrt(dst); |
| 2887 |
if (dst < _seed_rf[seed_i]) _seed_rf[seed_i] = dst; |
| 2888 |
if (dst < _seed_rf[seed_j]) _seed_rf[seed_j] = dst; |
| 2889 |
|
| 2890 |
if (_seed_neighbors[seed_i] == 0) |
| 2891 |
{ |
| 2892 |
_seed_rf[seed_i] = dst; |
| 2893 |
_seed_neighbors[seed_i] = new size_t[12]; |
| 2894 |
_seed_neighbors[seed_i][0] = 12; |
| 2895 |
_seed_neighbors[seed_i][1] = 1; |
| 2896 |
_seed_neighbors[seed_i][2] = seed_j; |
| 2897 |
if (seed_i < seed_j) connect_seeds(seed_j, seed_i); |
| 2898 |
return 0; |
| 2899 |
} |
| 2900 |
for (size_t i = 0; i < _seed_neighbors[seed_i][1]; i++) |
| 2901 |
{ |
| 2902 |
if (_seed_neighbors[seed_i][2 + i] == seed_j) return 0; // two seeds are already connected |
| 2903 |
} |
| 2904 |
|
| 2905 |
size_t num_neighbors = _seed_neighbors[seed_i][1]; |
| 2906 |
_seed_neighbors[seed_i][num_neighbors + 2] = seed_j; |
| 2907 |
_seed_neighbors[seed_i][1]++; |
| 2908 |
|
| 2909 |
if (_seed_neighbors[seed_i][1] + 2 == _seed_neighbors[seed_i][0]) |
| 2910 |
{ |
| 2911 |
size_t new_capacity = _seed_neighbors[seed_i][0] * 2; |
| 2912 |
size_t* tmp = new size_t[new_capacity]; |
| 2913 |
tmp[0] = new_capacity; tmp[1] = _seed_neighbors[seed_i][1]; |
| 2914 |
for (size_t i = 0; i < _seed_neighbors[seed_i][1]; i++) tmp[2 + i] = _seed_neighbors[seed_i][2 + i]; |
| 2915 |
delete[] _seed_neighbors[seed_i]; |
| 2916 |
_seed_neighbors[seed_i] = tmp; |
| 2917 |
} |
| 2918 |
if (seed_i < seed_j) connect_seeds(seed_j, seed_i); |
| 2919 |
return 0; |
| 2920 |
//#pragma endregion |
| 2921 |
} |
| 2922 |
|
| 2923 |
int VPS::disconnect_seeds(size_t seed_i, size_t seed_j) |
| 2924 |
{ |
| 2925 |
//#pragma region Disconnect Seeds: |
| 2926 |
|
| 2927 |
if (seed_i == seed_j) return 1; |
| 2928 |
|
| 2929 |
if (_seed_neighbors[seed_i] == 0) return 1; |
| 2930 |
|
| 2931 |
for (size_t i = 0; i < _seed_neighbors[seed_i][1]; i++) |
| 2932 |
{ |
| 2933 |
if (_seed_neighbors[seed_i][2 + i] == seed_j) |
| 2934 |
{ |
| 2935 |
_seed_neighbors[seed_i][2 + i] = _seed_neighbors[seed_i][1 + _seed_neighbors[seed_i][1]]; |
| 2936 |
_seed_neighbors[seed_i][1]--; |
| 2937 |
break; |
| 2938 |
} |
| 2939 |
} |
| 2940 |
|
| 2941 |
for (size_t i = 0; i < _seed_neighbors[seed_j][1]; i++) |
| 2942 |
{ |
| 2943 |
if (_seed_neighbors[seed_j][2 + i] == seed_i) |
| 2944 |
{ |
| 2945 |
_seed_neighbors[seed_j][2 + i] = _seed_neighbors[seed_j][1 + _seed_neighbors[seed_j][1]]; |
| 2946 |
_seed_neighbors[seed_j][1]--; |
| 2947 |
break; |
| 2948 |
} |
| 2949 |
} |
| 2950 |
return 0; |
| 2951 |
//#pragma endregion |
| 2952 |
} |
| 2953 |
|
| 2954 |
int VPS::get_num_seed_neighbors(size_t seed_index, size_t &num_seed_neighbors) |
| 2955 |
{ |
| 2956 |
//#pragma region Get Number od Delaunay Edges: |
| 2957 |
if (_seed_neighbors[seed_index] == 0) num_seed_neighbors = 0; |
| 2958 |
else num_seed_neighbors = _seed_neighbors[seed_index][1]; |
| 2959 |
return 0; |
| 2960 |
//#pragma endregion |
| 2961 |
} |
| 2962 |
|
| 2963 |
|
| 2964 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2965 |
/////////// Least Square QR Solver Methods ////////////////////////////////////////////////////////////////////////// |
| 2966 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2967 |
|
| 2968 |
int VPS::LeastSquare_QR(size_t num_data_points, double xmin, double xmax, double* x, double* f, size_t num_basis, double* c) |
| 2969 |
{ |
| 2970 |
//#pragma region Least Square QR graph fit: |
| 2971 |
double** A = new double*[num_data_points]; |
| 2972 |
|
| 2973 |
for (size_t idatapoint = 0; idatapoint < num_data_points; idatapoint++) |
| 2974 |
{ |
| 2975 |
A[idatapoint] = new double[num_basis]; |
| 2976 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) A[idatapoint][ibasis] = pow(x[idatapoint], ibasis); |
| 2977 |
} |
| 2978 |
|
| 2979 |
LS_QR_Solver(num_data_points, num_basis, A, f, c); |
| 2980 |
|
| 2981 |
for (size_t idatapoint = 0; idatapoint < num_data_points; idatapoint++) delete[] A[idatapoint]; |
| 2982 |
delete[] A; |
| 2983 |
|
| 2984 |
return 0; |
| 2985 |
//#pragma endregion |
| 2986 |
} |
| 2987 |
|
| 2988 |
int VPS::NaturalCubicSplineInterpolation(size_t num_data_points, double* x, double* y, double* co, double* c1, double* c2, double* c3) |
| 2989 |
{ |
| 2990 |
//#pragma region Natural Cubic Spline Interpolation: |
| 2991 |
// Second Derivative at start points of each quadratic Curve |
| 2992 |
size_t n = num_data_points - 1; |
| 2993 |
|
| 2994 |
if (n == 1) |
| 2995 |
{ |
| 2996 |
// linear interpolation |
| 2997 |
co[0] = y[0] - x[0] * (y[1] - y[0]) / (x[1] - x[0]); |
| 2998 |
c1[0] = (y[1] - y[0]) / (x[1] - x[0]); |
| 2999 |
c2[0] = 0.0; |
| 3000 |
c3[0] = 0.0; |
| 3001 |
return 0; |
| 3002 |
} |
| 3003 |
|
| 3004 |
double* G = new double[n + 1]; |
| 3005 |
G[0] = 0.0; G[n] = 0.0; |
| 3006 |
|
| 3007 |
double* LD = new double[n - 1]; |
| 3008 |
double* DD = new double[n - 1]; |
| 3009 |
double* UD = new double[n - 1]; |
| 3010 |
double* b = new double[n - 1]; |
| 3011 |
double dm = x[1] - x[0]; double dp = x[2] - x[1]; |
| 3012 |
double Dm = y[1] - y[0]; double Dp = y[2] - y[1]; |
| 3013 |
DD[0] = 2 * (dm + dp); UD[0] = dp; b[0] = 6 * (Dp / dp - Dm / dm); |
| 3014 |
|
| 3015 |
dp = x[n] - x[n - 1]; dm = x[n - 1] - x[n - 2]; |
| 3016 |
Dp = y[n] - y[n - 1]; Dm = y[n - 1] - y[n - 2]; |
| 3017 |
LD[n - 2] = dm; DD[n - 2] = 2 * (dm + dp); b[n - 2] = 6 * (Dp / dp - Dm / dm); |
| 3018 |
if (n > 2) |
| 3019 |
{ |
| 3020 |
for (size_t i = 1; i < n - 2; i++) |
| 3021 |
{ |
| 3022 |
dp = x[i + 2] - x[i + 1]; dm = x[i + 1] - x[i]; |
| 3023 |
Dp = y[i + 2] - y[i + 1]; Dm = y[i + 1] - y[i]; |
| 3024 |
LD[i] = dm; |
| 3025 |
DD[i] = 2 * (dm + dp); |
| 3026 |
UD[i] = dp; |
| 3027 |
b[i] = 6 * (Dp / dp - Dm / dm); |
| 3028 |
} |
| 3029 |
} |
| 3030 |
|
| 3031 |
double* kk = new double[n - 1]; |
| 3032 |
|
| 3033 |
solve_LU_Doolittle(n - 1, LD, DD, UD, b, kk); |
| 3034 |
for (size_t i = 0; i < n - 1; i++) G[i + 1] = kk[i]; |
| 3035 |
|
| 3036 |
for (size_t i = 0; i < num_data_points - 1; i++) |
| 3037 |
{ |
| 3038 |
dp = x[i + 1] - x[i]; Dp = y[i + 1] - y[i]; |
| 3039 |
double ki = (Dp / dp) - ((2.0 / 3.0) * dp * 0.5 * G[i]) - (1.0 / 3.0) * dp * 0.5 * G[i + 1]; |
| 3040 |
double di = 0.5 * (G[i + 1] - G[i]) / (3.0 * dp); |
| 3041 |
|
| 3042 |
co[i] = y[i] - ki * x[i] + 0.5 * G[i] * pow(x[i], 2) - di * pow(x[i], 3); |
| 3043 |
|
| 3044 |
c1[i] = ki - G[i] * x[i] + 3 * di * pow(x[i], 2);; |
| 3045 |
|
| 3046 |
c2[i] = 0.5 * G[i] - 3 * di * x[i]; |
| 3047 |
|
| 3048 |
c3[i] = di; |
| 3049 |
|
| 3050 |
//yp[j] = y[i] + ki * (xp[j] - x[i]) + 0.5 * G[i] * pow((xp[j] - x[i]), 2) + di * pow((xp[j] - x[i]), 3); |
| 3051 |
} |
| 3052 |
delete[] G; delete[] LD; delete[] DD; delete[] UD; delete[] b; delete[] kk; |
| 3053 |
return 0; |
| 3054 |
//#pragma endregion |
| 3055 |
} |
| 3056 |
|
| 3057 |
int VPS::FourierExpansion(size_t num_data_points, double xmin, double xmax, double* x, double* f, double** c, size_t num_basis, double* a, double* b) |
| 3058 |
{ |
| 3059 |
//#pragma region Fourier Expansion of a function: |
| 3060 |
|
| 3061 |
//size_t num_pieces(num_data_points - 1); // ETP |
| 3062 |
|
| 3063 |
double P = xmax - xmin; |
| 3064 |
|
| 3065 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) |
| 3066 |
{ |
| 3067 |
a[ibasis] = 0.0; b[ibasis] = 0.0; |
| 3068 |
|
| 3069 |
size_t num_pieces(num_data_points - 1); |
| 3070 |
for (size_t ipiece = 0; ipiece < num_pieces; ipiece++) |
| 3071 |
{ |
| 3072 |
double xm = x[ipiece]; double xp = x[ipiece + 1]; |
| 3073 |
|
| 3074 |
if (ibasis == 0) |
| 3075 |
{ |
| 3076 |
a[ibasis] += c[ipiece][0] * (xp - xm); |
| 3077 |
a[ibasis] += c[ipiece][1] * (pow(xp, 2) - pow(xm, 2)) / 2; |
| 3078 |
a[ibasis] += c[ipiece][2] * (pow(xp, 3) - pow(xm, 3)) / 3; |
| 3079 |
a[ibasis] += c[ipiece][3] * (pow(xp, 4) - pow(xm, 4)) / 4; |
| 3080 |
continue; |
| 3081 |
} |
| 3082 |
|
| 3083 |
double tm = 2.0 * PI * ibasis * xm / P; double tp = 2.0 * PI * ibasis * xp / P; |
| 3084 |
|
| 3085 |
a[ibasis] += c[ipiece][0] * P * (sin(tp) - sin(tm)) / (2.0 * PI * ibasis); |
| 3086 |
|
| 3087 |
a[ibasis] += c[ipiece][1] * pow(P, 2) * (cos(tp) - cos(tm)) / (4 * pow(PI, 2) * pow(ibasis, 2)); |
| 3088 |
a[ibasis] += c[ipiece][1] * P * (xp * sin(tp) - xm * sin(tm)) / (2.0 * PI * ibasis); |
| 3089 |
|
| 3090 |
a[ibasis] += c[ipiece][2] * pow(P, 2) * (xp * cos(tp) - xm * cos(tm)) / (2.0 * pow(PI, 2) * pow(ibasis, 2)); |
| 3091 |
a[ibasis] -= c[ipiece][2] * pow(P, 3) * (sin(tp) - sin(tm)) / (4.0 * pow(PI, 3) * pow(ibasis, 3)); |
| 3092 |
a[ibasis] += c[ipiece][2] * P * (pow(xp, 2) * sin(tp) - pow(xm, 2) * sin(tm)) / (2.0 * PI * ibasis); |
| 3093 |
|
| 3094 |
a[ibasis] += c[ipiece][3] * P * (pow(xp, 3) * sin(tp) - pow(xm, 3) * sin(tm)) / (2.0 * PI * ibasis); |
| 3095 |
a[ibasis] -= c[ipiece][3] * 3 * pow(P, 3) * (xp * sin(tp) - xm * sin(tm)) / (4.0 * pow(PI, 3) * pow(ibasis, 3)); |
| 3096 |
a[ibasis] -= c[ipiece][3] * 3 * pow(P, 4) * (cos(tp) - cos(tm)) / (8.0 * pow(PI, 4) * pow(ibasis, 4)); |
| 3097 |
a[ibasis] += c[ipiece][3] * 3 * pow(P, 2) * (pow(xp, 2) * cos(tp) - pow(xm, 2) * cos(tm)) / (4.0 * pow(PI, 2) * pow(ibasis, 2)); |
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
b[ibasis] -= c[ipiece][0] * P * (cos(tp) - cos(tm)) / (2.0 * PI * ibasis); |
| 3102 |
|
| 3103 |
b[ibasis] += c[ipiece][1] * pow(P, 2) * (sin(tp) - sin(tm)) / (4 * pow(PI, 2) * pow(ibasis, 2)); |
| 3104 |
b[ibasis] -= c[ipiece][1] * P * (xp * cos(tp) - xm * cos(tm)) / (2.0 * PI * ibasis); |
| 3105 |
|
| 3106 |
|
| 3107 |
b[ibasis] += c[ipiece][2] * pow(P, 3) * (cos(tp) - cos(tm)) / (4.0 * pow(PI, 3) * pow(ibasis, 3)); |
| 3108 |
b[ibasis] -= c[ipiece][2] * P * (pow(xp, 2) * cos(tp) - pow(xm, 2) * cos(tm)) / (2.0 * PI * ibasis); |
| 3109 |
b[ibasis] += c[ipiece][2] * pow(P, 2) * (xp * sin(tp) - xm * sin(tm)) / (2.0 * pow(PI, 2) * pow(ibasis, 2)); |
| 3110 |
|
| 3111 |
b[ibasis] += c[ipiece][3] * 3 * pow(P, 3) * (xp * cos(tp) - xm * cos(tm)) / (4.0 * pow(PI, 3) * pow(ibasis, 3)); |
| 3112 |
b[ibasis] -= c[ipiece][3] * P * (pow(xp, 3) * cos(tp) - pow(xm, 3) * cos(tm)) / (2.0 * PI * ibasis); |
| 3113 |
b[ibasis] -= c[ipiece][3] * 3 * pow(P, 4) * (sin(tp) - sin(tm)) / (8.0 * pow(PI, 4) * pow(ibasis, 4)); |
| 3114 |
b[ibasis] += c[ipiece][3] * 3 * pow(P, 2) * (pow(xp, 2) * sin(tp) - pow(xm, 2) * sin(tm)) / (4.0 * pow(PI, 2) * pow(ibasis, 2)); |
| 3115 |
|
| 3116 |
} |
| 3117 |
a[ibasis] *= (2.0 / P); b[ibasis] *= (2.0 / P); |
| 3118 |
} |
| 3119 |
return 0; |
| 3120 |
//#pragma endregion |
| 3121 |
} |
| 3122 |
|
| 3123 |
|
| 3124 |
int VPS::solve_LU_Doolittle(size_t num_eq, double* LD, double* DD, double* UD, double* b, double* x) |
| 3125 |
{ |
| 3126 |
//#pragma region Solve LU Doolittle: |
| 3127 |
if (num_eq == 0) return 1; |
| 3128 |
|
| 3129 |
// L and U Matrices |
| 3130 |
double* L = new double[num_eq]; |
| 3131 |
double** U = new double*[num_eq]; |
| 3132 |
for (size_t i = 0; i < num_eq; i++) U[i] = new double[2]; |
| 3133 |
|
| 3134 |
U[0][0] = DD[0]; |
| 3135 |
for (size_t i = 0; i < num_eq - 1; i++) U[i][1] = UD[i]; |
| 3136 |
|
| 3137 |
for (size_t i = 1; i < num_eq; i++) |
| 3138 |
{ |
| 3139 |
L[i] = LD[i] / U[i - 1][0]; |
| 3140 |
U[i][0] = DD[i] - L[i] * U[i - 1][1]; |
| 3141 |
} |
| 3142 |
|
| 3143 |
// Solution Vectors: |
| 3144 |
double* y = new double[num_eq]; |
| 3145 |
y[0] = b[0]; |
| 3146 |
for (size_t i = 1; i < num_eq; i++) |
| 3147 |
{ |
| 3148 |
y[i] = b[i] - L[i] * y[i - 1]; |
| 3149 |
} |
| 3150 |
// Back step substitution: |
| 3151 |
x[num_eq - 1] = y[num_eq - 1] / U[num_eq - 1][0]; |
| 3152 |
if (num_eq > 1) |
| 3153 |
{ |
| 3154 |
size_t i(num_eq - 2); |
| 3155 |
while (true) |
| 3156 |
{ |
| 3157 |
x[i] = (y[i] - U[i][1] * x[i + 1]) / U[i][0]; |
| 3158 |
if (i == 0) break; |
| 3159 |
i--; |
| 3160 |
} |
| 3161 |
} |
| 3162 |
for (size_t i = 0; i < num_eq; i++) delete[] U[i]; |
| 3163 |
delete[] L; delete[] U; delete[] y; |
| 3164 |
return 0; |
| 3165 |
//#pragma endregion |
| 3166 |
} |
| 3167 |
|
| 3168 |
void VPS::LS_QR_Solver(size_t nrow, size_t ncol, double** A, double* b, double* x) |
| 3169 |
{ |
| 3170 |
//#pragma region Least Square QR Solver: |
| 3171 |
double** Q = new double*[nrow]; |
| 3172 |
for (size_t irow = 0; irow < nrow; irow++) Q[irow] = new double[ncol]; |
| 3173 |
|
| 3174 |
double** R = new double*[ncol]; // A square matrix |
| 3175 |
for (size_t irow = 0; irow < ncol; irow++) R[irow] = new double[ncol]; |
| 3176 |
|
| 3177 |
QR_Factorization(nrow, ncol, A, Q, R); |
| 3178 |
|
| 3179 |
double* bmod = new double[ncol]; |
| 3180 |
for (size_t irow = 0; irow < ncol; irow++) |
| 3181 |
{ |
| 3182 |
bmod[irow] = 0.0; |
| 3183 |
for (size_t icol = 0; icol < nrow; icol++) bmod[irow] += Q[icol][irow] * b[icol]; |
| 3184 |
} |
| 3185 |
|
| 3186 |
// backward solve |
| 3187 |
size_t jrow = ncol - 1; |
| 3188 |
while (true) |
| 3189 |
{ |
| 3190 |
if (fabs(R[jrow][jrow]) > 1E-8) |
| 3191 |
{ |
| 3192 |
double sum(0.0); |
| 3193 |
for (size_t jcol = ncol - 1; jcol > jrow; jcol--) sum += R[jrow][jcol] * x[jcol]; |
| 3194 |
x[jrow] = (bmod[jrow] - sum) / R[jrow][jrow]; |
| 3195 |
} |
| 3196 |
else |
| 3197 |
{ |
| 3198 |
// This column is in the Null Space of A |
| 3199 |
x[jrow] = 0.0; |
| 3200 |
} |
| 3201 |
|
| 3202 |
if (jrow == 0) break; |
| 3203 |
jrow--; |
| 3204 |
} |
| 3205 |
|
| 3206 |
for (size_t irow = 0; irow < nrow; irow++) delete[] Q[irow]; |
| 3207 |
delete[] Q; |
| 3208 |
|
| 3209 |
for (size_t irow = 0; irow < ncol; irow++) delete[] R[irow]; |
| 3210 |
delete[] R; |
| 3211 |
|
| 3212 |
delete[] bmod; |
| 3213 |
//#pragma endregion |
| 3214 |
} |
| 3215 |
|
| 3216 |
void VPS::GramSchmidt(size_t nrow, size_t ncol, double** A, double** Q) |
| 3217 |
{ |
| 3218 |
//#pragma region GramSchmidt Process: |
| 3219 |
|
| 3220 |
// norw >= ncol (for LS problems) |
| 3221 |
bool* null_basis = new bool[ncol]; |
| 3222 |
for (size_t ibasis = 0; ibasis < ncol; ibasis++) null_basis[ibasis] = false; |
| 3223 |
|
| 3224 |
double** ebasis = new double*[ncol]; |
| 3225 |
for (size_t ibasis = 0; ibasis < ncol; ibasis++) ebasis[ibasis] = new double[nrow]; |
| 3226 |
|
| 3227 |
double* v = new double[nrow]; |
| 3228 |
|
| 3229 |
size_t num_basis(0); |
| 3230 |
|
| 3231 |
for (size_t icol = 0; icol < ncol; icol++) |
| 3232 |
{ |
| 3233 |
for (size_t irow = 0; irow < nrow; irow++) ebasis[num_basis][irow] = A[irow][icol]; |
| 3234 |
|
| 3235 |
// normalize |
| 3236 |
double norm(0.0); |
| 3237 |
for (size_t irow = 0; irow < nrow; irow++) norm += ebasis[num_basis][irow] * ebasis[num_basis][irow]; |
| 3238 |
norm = sqrt(norm); |
| 3239 |
|
| 3240 |
if (norm > 1E-8) |
| 3241 |
{ |
| 3242 |
for (size_t irow = 0; irow < nrow; irow++) ebasis[num_basis][irow] /= norm; |
| 3243 |
} |
| 3244 |
else |
| 3245 |
{ |
| 3246 |
null_basis[num_basis] = true; |
| 3247 |
num_basis++; |
| 3248 |
continue; |
| 3249 |
} |
| 3250 |
|
| 3251 |
orthonormalize_vector(num_basis, nrow, ebasis, ebasis[num_basis], norm); |
| 3252 |
|
| 3253 |
if (norm <= 1E-8) |
| 3254 |
{ |
| 3255 |
null_basis[num_basis] = true; |
| 3256 |
} |
| 3257 |
num_basis++; |
| 3258 |
} |
| 3259 |
|
| 3260 |
// Replacing Null basis with basis from Null Space of A |
| 3261 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) |
| 3262 |
{ |
| 3263 |
if (!null_basis[ibasis]) continue; |
| 3264 |
for (size_t idim = 0; idim < nrow; idim++) |
| 3265 |
{ |
| 3266 |
for (size_t jdim = 0; jdim < nrow; jdim++) v[jdim] = 0.0; |
| 3267 |
v[idim] = 1.0; |
| 3268 |
double norm; |
| 3269 |
orthonormalize_vector(num_basis, nrow, ebasis, v, norm); |
| 3270 |
|
| 3271 |
if (norm <= 1E-8) continue; |
| 3272 |
|
| 3273 |
for (size_t jdim = 0; jdim < nrow; jdim++) ebasis[ibasis][jdim] = v[jdim]; |
| 3274 |
break; |
| 3275 |
} |
| 3276 |
} |
| 3277 |
|
| 3278 |
MAT_Transpose(num_basis, nrow, ebasis, Q); |
| 3279 |
|
| 3280 |
for (size_t ibasis = 0; ibasis < ncol; ibasis++) delete[] ebasis[ibasis]; |
| 3281 |
delete[] ebasis; delete[] null_basis; delete[] v; |
| 3282 |
|
| 3283 |
//#pragma endregion |
| 3284 |
} |
| 3285 |
|
| 3286 |
|
| 3287 |
void VPS::orthonormalize_vector(size_t nbasis, size_t ndim, double** e_basis, double* v, double &norm) |
| 3288 |
{ |
| 3289 |
//#pragma region orthonomalize Vector: |
| 3290 |
for (size_t ibasis = 0; ibasis < nbasis; ibasis++) |
| 3291 |
{ |
| 3292 |
// project |
| 3293 |
double dot(0.0); |
| 3294 |
for (size_t idim = 0; idim < ndim; idim++) dot += v[idim] * e_basis[ibasis][idim]; |
| 3295 |
for (size_t idim = 0; idim < ndim; idim++) v[idim] -= dot * e_basis[ibasis][idim]; |
| 3296 |
|
| 3297 |
// normalize |
| 3298 |
norm = 0.0; |
| 3299 |
for (size_t idim = 0; idim < ndim; idim++) norm += v[idim] * v[idim]; |
| 3300 |
norm = sqrt(norm); |
| 3301 |
|
| 3302 |
if (norm > 1E-8) |
| 3303 |
{ |
| 3304 |
for (size_t idim = 0; idim < ndim; idim++) v[idim] /= norm; |
| 3305 |
} |
| 3306 |
else |
| 3307 |
{ |
| 3308 |
norm = 0.0; |
| 3309 |
return; |
| 3310 |
} |
| 3311 |
} |
| 3312 |
//#pragma endregion |
| 3313 |
} |
| 3314 |
|
| 3315 |
|
| 3316 |
void VPS::QR_Factorization(size_t nrow, size_t ncol, double** A, double** Q, double** R) |
| 3317 |
{ |
| 3318 |
//#pragma region QR Factorization: |
| 3319 |
// norw >= ncol (for LS problems) |
| 3320 |
GramSchmidt(nrow, ncol, A, Q); |
| 3321 |
double** QT = new double*[ncol]; |
| 3322 |
for (size_t irow = 0; irow < ncol; irow++) QT[irow] = new double[nrow]; |
| 3323 |
|
| 3324 |
MAT_Transpose(nrow, ncol, Q, QT); |
| 3325 |
|
| 3326 |
MAT_MAT(ncol, nrow, QT, ncol, A, R); |
| 3327 |
|
| 3328 |
for (size_t irow = 0; irow < ncol; irow++) delete[] QT[irow]; |
| 3329 |
delete[] QT; |
| 3330 |
//#pragma endregion |
| 3331 |
} |
| 3332 |
|
| 3333 |
void VPS::MAT_MAT(size_t nrow, size_t ncol, double** A, size_t mcol, double** B, double** C) |
| 3334 |
{ |
| 3335 |
//#pragma region Matrix Matrix Multiplication: |
| 3336 |
for (size_t irow = 0; irow < nrow; irow++) |
| 3337 |
{ |
| 3338 |
for (size_t jcol = 0; jcol < mcol; jcol++) |
| 3339 |
{ |
| 3340 |
C[irow][jcol] = 0.0; |
| 3341 |
for (size_t icol = 0; icol < ncol; icol++) |
| 3342 |
{ |
| 3343 |
C[irow][jcol] += A[irow][icol] * B[icol][jcol]; |
| 3344 |
} |
| 3345 |
} |
| 3346 |
} |
| 3347 |
//#pragma endregion |
| 3348 |
} |
| 3349 |
|
| 3350 |
void VPS::MAT_Transpose(size_t nrow, size_t ncol, double** A, double** AT) |
| 3351 |
{ |
| 3352 |
//#pragma region Matrix Transpose: |
| 3353 |
for (size_t irow = 0; irow < nrow; irow++) |
| 3354 |
{ |
| 3355 |
for (size_t icol = 0; icol < ncol; icol++) |
| 3356 |
{ |
| 3357 |
AT[icol][irow] = A[irow][icol]; |
| 3358 |
} |
| 3359 |
} |
| 3360 |
//#pragma endregion |
| 3361 |
} |
| 3362 |
|
| 3363 |
|
| 3364 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 3365 |
/////////// Surrogate Methods /////////////////////////////////////////////////////////////////////////////////////// |
| 3366 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 3367 |
|
| 3368 |
int VPS::construct_local_surrogates() |
| 3369 |
{ |
| 3370 |
init_vps_containers(); |
| 3371 |
for (size_t isample = 0; isample < _num_samples; isample++) |
| 3372 |
{ |
| 3373 |
if (_num_samples > 19000) std::cout << "Building Surrogate:" << isample << std::endl; |
| 3374 |
if (_method == Regression) retrieve_weights_regression(isample); |
| 3375 |
} |
| 3376 |
return 0; |
| 3377 |
} |
| 3378 |
|
| 3379 |
|
| 3380 |
int VPS::init_vps_containers() |
| 3381 |
{ |
| 3382 |
//#pragma region init_vps_containers: |
| 3383 |
if (_p == 0) |
| 3384 |
{ |
| 3385 |
_num_basis = retrieve_num_permutations(_num_dim, _desired_order, true, _desired_order); |
| 3386 |
_num_basis_pool = _num_basis; size_t order = _desired_order; |
| 3387 |
while (_num_basis_pool < 10 * _budget) |
| 3388 |
{ |
| 3389 |
order++; |
| 3390 |
_num_basis_pool = retrieve_num_permutations(_num_dim, order, true, order); |
| 3391 |
} |
| 3392 |
retrieve_permutations(_num_basis_pool, _p, _num_dim, order, true, order); |
| 3393 |
} |
| 3394 |
|
| 3395 |
if (_basis_coef == 0) |
| 3396 |
{ |
| 3397 |
// init Surrogate container |
| 3398 |
_basis_coef = new double**[_budget]; |
| 3399 |
_basis_index = new size_t**[_budget]; |
| 3400 |
for (size_t i = 0; i < _budget; i++) |
| 3401 |
{ |
| 3402 |
_basis_coef[i] = 0; _basis_index[i] = 0; |
| 3403 |
} |
| 3404 |
} |
| 3405 |
return 0; |
| 3406 |
//#pragma endregion |
| 3407 |
} |
| 3408 |
|
| 3409 |
int VPS::add_neighbor_layer(size_t cell_index, size_t function_index, size_t &num_neighbors, size_t* &neighbors, size_t &neighbors_capacity) |
| 3410 |
{ |
| 3411 |
//#pragma region Add neighbor Layer: |
| 3412 |
|
| 3413 |
if (_seed_neighbors == 0 || _seed_neighbors[cell_index] == 0) return 0; |
| 3414 |
|
| 3415 |
size_t num_new_neighbors(0), new_neighbors_capacity(100); |
| 3416 |
size_t* new_neighbors = new size_t[new_neighbors_capacity]; |
| 3417 |
|
| 3418 |
if (num_neighbors == 1) |
| 3419 |
{ |
| 3420 |
// add delaunay neighbors |
| 3421 |
for (size_t j = 0; j < _seed_neighbors[cell_index][1]; j++) |
| 3422 |
{ |
| 3423 |
size_t potential_neighbor = _seed_neighbors[cell_index][2 + j]; |
| 3424 |
|
| 3425 |
bool disc(false); |
| 3426 |
if (_seed_disc_neighbors != 0 && _seed_disc_neighbors[cell_index] != 0) |
| 3427 |
{ |
| 3428 |
disc = _seed_disc_neighbors[cell_index][function_index][j]; |
| 3429 |
} |
| 3430 |
if (disc) continue; |
| 3431 |
add_entry(potential_neighbor, num_new_neighbors, new_neighbors, new_neighbors_capacity); |
| 3432 |
} |
| 3433 |
} |
| 3434 |
else |
| 3435 |
{ |
| 3436 |
// add neighbors of existing neighbors |
| 3437 |
for (size_t ineighbor = 0; ineighbor < num_neighbors; ineighbor++) |
| 3438 |
{ |
| 3439 |
size_t neighbor = neighbors[ineighbor]; |
| 3440 |
for (size_t j = 0; j < _seed_neighbors[neighbor][1]; j++) |
| 3441 |
{ |
| 3442 |
size_t potential_neighbor = _seed_neighbors[neighbor][2 + j]; |
| 3443 |
|
| 3444 |
bool disc(false); |
| 3445 |
if (_seed_disc_neighbors != 0 && _seed_disc_neighbors[neighbor] != 0) |
| 3446 |
{ |
| 3447 |
disc = _seed_disc_neighbors[neighbor][function_index][j]; |
| 3448 |
} |
| 3449 |
if (disc) continue; |
| 3450 |
|
| 3451 |
if (potential_neighbor == cell_index) continue; |
| 3452 |
if (find_brute(potential_neighbor, neighbors, num_neighbors)) continue; |
| 3453 |
if (find_brute(potential_neighbor, new_neighbors, num_new_neighbors)) continue; |
| 3454 |
|
| 3455 |
add_entry(potential_neighbor, num_new_neighbors, new_neighbors, new_neighbors_capacity); |
| 3456 |
} |
| 3457 |
} |
| 3458 |
} |
| 3459 |
|
| 3460 |
for (size_t inew = 0; inew < num_new_neighbors; inew++) |
| 3461 |
{ |
| 3462 |
add_entry(new_neighbors[inew], num_neighbors, neighbors, neighbors_capacity); |
| 3463 |
} |
| 3464 |
delete[] new_neighbors; |
| 3465 |
return 0; |
| 3466 |
//#pragma endregion |
| 3467 |
} |
| 3468 |
|
| 3469 |
int VPS::retrieve_weights_regression(size_t cell_index) |
| 3470 |
{ |
| 3471 |
//#pragma region Retrieve Weight Using Regression: |
| 3472 |
|
| 3473 |
if (_desired_order == 0) |
| 3474 |
{ |
| 3475 |
_basis_coef[cell_index] = new double*[_num_functions]; |
| 3476 |
_basis_index[cell_index] = new size_t*[_num_functions]; |
| 3477 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) |
| 3478 |
{ |
| 3479 |
_basis_coef[cell_index][ifunc] = new double[_num_basis]; |
| 3480 |
_basis_index[cell_index][ifunc] = new size_t[_num_basis]; |
| 3481 |
_basis_coef[cell_index][ifunc][0] = _f[cell_index][ifunc]; |
| 3482 |
_basis_index[cell_index][ifunc][0] = 0; |
| 3483 |
} |
| 3484 |
return 0; |
| 3485 |
} |
| 3486 |
|
| 3487 |
if (_basis_coef[cell_index] == 0) |
| 3488 |
{ |
| 3489 |
_basis_coef[cell_index] = new double*[_num_functions]; |
| 3490 |
_basis_index[cell_index] = new size_t*[_num_functions]; |
| 3491 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) |
| 3492 |
{ |
| 3493 |
_basis_coef[cell_index][ifunc] = new double[_num_basis]; |
| 3494 |
_basis_index[cell_index][ifunc] = new size_t[_num_basis]; |
| 3495 |
} |
| 3496 |
} |
| 3497 |
|
| 3498 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) |
| 3499 |
{ |
| 3500 |
for (size_t ibasis = 0; ibasis < _num_basis; ibasis++) |
| 3501 |
{ |
| 3502 |
_basis_coef[cell_index][ifunc][ibasis] = 0.0; |
| 3503 |
_basis_index[cell_index][ifunc][ibasis] = ibasis; |
| 3504 |
} |
| 3505 |
} |
| 3506 |
|
| 3507 |
|
| 3508 |
|
| 3509 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) |
| 3510 |
{ |
| 3511 |
// collect neighbors |
| 3512 |
size_t num_data_points(1), data_points_capacity(10); |
| 3513 |
size_t* data_points = new size_t[data_points_capacity]; |
| 3514 |
data_points[0] = cell_index; |
| 3515 |
while (_desired_order > 0 && num_data_points < 2 * _num_basis) |
| 3516 |
{ |
| 3517 |
size_t old_num = num_data_points; |
| 3518 |
add_neighbor_layer(cell_index, ifunc, num_data_points, data_points, data_points_capacity); |
| 3519 |
if (old_num == num_data_points) break; // no more neighbors to add |
| 3520 |
} |
| 3521 |
|
| 3522 |
size_t num_basis(_num_basis), order(_desired_order); |
| 3523 |
while (num_data_points < num_basis) |
| 3524 |
{ |
| 3525 |
order--; |
| 3526 |
num_basis = retrieve_num_permutations(_num_dim, order, true, order); |
| 3527 |
} |
| 3528 |
|
| 3529 |
// Select basis that is less than or equal to order |
| 3530 |
size_t nb(0); |
| 3531 |
for (size_t ibasis = 0; ibasis < _num_basis; ibasis++) |
| 3532 |
{ |
| 3533 |
size_t sum(0); |
| 3534 |
for (size_t idim = 0; idim < _num_dim; idim++) sum += _p[ibasis][idim]; |
| 3535 |
if (sum > order) continue; |
| 3536 |
|
| 3537 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) _basis_index[cell_index][ifunc][nb] = ibasis; |
| 3538 |
nb++; |
| 3539 |
} |
| 3540 |
|
| 3541 |
|
| 3542 |
|
| 3543 |
|
| 3544 |
double** A = new double*[num_data_points]; |
| 3545 |
double* b = new double[num_data_points]; |
| 3546 |
for (size_t ipoint = 0; ipoint < num_data_points; ipoint++) |
| 3547 |
{ |
| 3548 |
size_t data_point_index = data_points[ipoint]; |
| 3549 |
|
| 3550 |
A[ipoint] = new double[num_basis]; |
| 3551 |
|
| 3552 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) |
| 3553 |
{ |
| 3554 |
size_t basis_index = _basis_index[cell_index][ifunc][ibasis]; |
| 3555 |
A[ipoint][ibasis] = evaluate_basis_function(_x[data_point_index], cell_index, basis_index); |
| 3556 |
} |
| 3557 |
b[ipoint] = _f[data_point_index][ifunc]; |
| 3558 |
} |
| 3559 |
|
| 3560 |
bool constrained_regression(true); |
| 3561 |
if (constrained_regression) |
| 3562 |
{ |
| 3563 |
for (size_t ipoint = 0; ipoint < num_data_points; ipoint++) |
| 3564 |
{ |
| 3565 |
size_t data_point_index = data_points[ipoint]; |
| 3566 |
if (data_point_index == cell_index) continue; |
| 3567 |
A[ipoint][0] = 0.0; |
| 3568 |
b[ipoint] -= _f[cell_index][ifunc]; |
| 3569 |
} |
| 3570 |
} |
| 3571 |
|
| 3572 |
LS_QR_Solver(num_data_points, /*_*/num_basis, A, b, _basis_coef[cell_index][ifunc]); // ETP |
| 3573 |
|
| 3574 |
|
| 3575 |
delete[] data_points; |
| 3576 |
|
| 3577 |
for (size_t ipoint = 0; ipoint < num_data_points; ipoint++) delete[] A[ipoint]; |
| 3578 |
delete[] A; delete[] b; |
| 3579 |
|
| 3580 |
} |
| 3581 |
return 0; |
| 3582 |
//#pragma endregion |
| 3583 |
} |
| 3584 |
|
| 3585 |
double VPS::evaluate_basis_function(double* x, size_t cell_index, size_t ibasis) |
| 3586 |
{ |
| 3587 |
//#pragma region Evaluate Basis Function: |
| 3588 |
if (_basis == monomials || _basis == legendre || _basis == chebyshev) |
| 3589 |
{ |
| 3590 |
double f_basis(1.0); |
| 3591 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3592 |
{ |
| 3593 |
f_basis *= evaluate_one_dimensional_basis_function(x[idim], _x[cell_index][idim], _seed_box[cell_index][idim], _seed_box[cell_index][_num_dim + idim], _p[ibasis][idim]); |
| 3594 |
} |
| 3595 |
return f_basis; |
| 3596 |
} |
| 3597 |
else if (_basis == radial) |
| 3598 |
{ |
| 3599 |
/* |
| 3600 |
size_t basis_index = icell; |
| 3601 |
|
| 3602 |
double dst_sq = 0.0; |
| 3603 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3604 |
{ |
| 3605 |
double dx = x[idim] - _sample_basis[icell][ibasis][idim]; |
| 3606 |
dst_sq += dx * dx; |
| 3607 |
} |
| 3608 |
h = _seed_rc[cell_index]; |
| 3609 |
double r_basis = 4.0 * h; |
| 3610 |
double r_sq = r_basis * r_basis; |
| 3611 |
|
| 3612 |
return exp(-dst_sq / r_sq); |
| 3613 |
*/ |
| 3614 |
} |
| 3615 |
return 0.0; |
| 3616 |
//#pragma endregion |
| 3617 |
} |
| 3618 |
|
| 3619 |
double VPS::evaluate_one_dimensional_basis_function(double x, double xo, double xmin, double xmax, size_t ibasis) |
| 3620 |
{ |
| 3621 |
//#pragma region Evaluate One Dimensional Basis Function: |
| 3622 |
if (_basis == monomials) |
| 3623 |
{ |
| 3624 |
double fbasis = 0.0; |
| 3625 |
x -= xo; // shift origin |
| 3626 |
double dxmin = xo - xmin; |
| 3627 |
double dxmax = xmax - xo; |
| 3628 |
if (dxmin > dxmax) x /= dxmin; |
| 3629 |
else x /= dxmax; |
| 3630 |
|
| 3631 |
fbasis = pow(x, ibasis); |
| 3632 |
return fbasis; |
| 3633 |
} |
| 3634 |
else if (_basis == legendre) |
| 3635 |
{ |
| 3636 |
double fbasis = 0.0; |
| 3637 |
|
| 3638 |
x -= xo; // shift origin |
| 3639 |
double dxmin = xo - xmin; |
| 3640 |
double dxmax = xmax - xo; |
| 3641 |
if (dxmin > dxmax) x /= dxmin; |
| 3642 |
else x /= dxmax; |
| 3643 |
|
| 3644 |
if (ibasis == 0) fbasis = 1.0; |
| 3645 |
else if (ibasis == 1) fbasis = x; |
| 3646 |
else |
| 3647 |
{ |
| 3648 |
double fm = evaluate_one_dimensional_basis_function(x, 0.0, -1.0, 1.0, ibasis - 1); |
| 3649 |
double fmm = evaluate_one_dimensional_basis_function(x, 0.0, -1.0, 1.0, ibasis - 2); |
| 3650 |
fbasis = ((2 * ibasis - 1) * x * fm - (ibasis - 1) * fmm) / ibasis; |
| 3651 |
} |
| 3652 |
return fbasis; |
| 3653 |
} |
| 3654 |
else if (_basis == chebyshev) |
| 3655 |
{ |
| 3656 |
double fbasis = 0.0; |
| 3657 |
x -= xo; // shift origin |
| 3658 |
double dxmin = xo - xmin; |
| 3659 |
double dxmax = xmax - xo; |
| 3660 |
if (dxmin > dxmax) x /= dxmin; |
| 3661 |
else x /= dxmax; |
| 3662 |
|
| 3663 |
if (ibasis == 0) fbasis = 1.0; |
| 3664 |
else if (ibasis == 1) fbasis = x; |
| 3665 |
else |
| 3666 |
{ |
| 3667 |
double fm = evaluate_one_dimensional_basis_function(x, 0.0, -1.0, 1.0, ibasis - 1); |
| 3668 |
double fmm = evaluate_one_dimensional_basis_function(x, 0.0, -1.0, 1.0, ibasis - 2); |
| 3669 |
fbasis = 2 * x * fm - fmm; |
| 3670 |
} |
| 3671 |
return fbasis; |
| 3672 |
} |
| 3673 |
return 0.0; |
| 3674 |
//#pragma endregion |
| 3675 |
} |
| 3676 |
|
| 3677 |
|
| 3678 |
int VPS::detect_discontinuities() |
| 3679 |
{ |
| 3680 |
//#pragma region Detect Discontinuities: |
| 3681 |
for (size_t iseed = 0; iseed < _num_samples; iseed++) |
| 3682 |
{ |
| 3683 |
size_t num_neighbors; get_num_seed_neighbors(iseed, num_neighbors); |
| 3684 |
|
| 3685 |
if (num_neighbors == 0) return 0; |
| 3686 |
|
| 3687 |
for (size_t ifunc = 0; ifunc < _num_functions; ifunc++) |
| 3688 |
{ |
| 3689 |
for (size_t i = 0; i < num_neighbors; i++) |
| 3690 |
{ |
| 3691 |
if (_seed_disc_neighbors[iseed][ifunc][i]) continue; |
| 3692 |
size_t neighbor_i = _seed_neighbors[iseed][2 + i]; |
| 3693 |
|
| 3694 |
for (size_t j = i + 1; j < num_neighbors; j++) |
| 3695 |
{ |
| 3696 |
if (_seed_disc_neighbors[iseed][ifunc][j]) continue; |
| 3697 |
size_t neighbor_j = _seed_neighbors[iseed][2 + j]; |
| 3698 |
|
| 3699 |
// Asser that neighbor_i and neighbor_j lie on opposite direction with regard to iseed |
| 3700 |
|
| 3701 |
double hi(0.0), hj(0.0), dot(0.0); |
| 3702 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3703 |
{ |
| 3704 |
double dxi = _x[neighbor_i][idim] - _x[iseed][idim]; |
| 3705 |
double dxj = _x[neighbor_j][idim] - _x[iseed][idim]; |
| 3706 |
hi += dxi * dxi; |
| 3707 |
hj += dxj * dxj; |
| 3708 |
dot += dxi * dxj; |
| 3709 |
} |
| 3710 |
hi = sqrt(hi); hj = sqrt(hj); |
| 3711 |
dot /= hi; dot /= hj; |
| 3712 |
if (dot > -0.5) continue; |
| 3713 |
|
| 3714 |
double dfi = _f[neighbor_i][ifunc] - _f[iseed][ifunc]; |
| 3715 |
double dfj = _f[neighbor_j][ifunc] - _f[iseed][ifunc]; |
| 3716 |
|
| 3717 |
double norm_hfi = sqrt(hi * hi + dfi * dfi); |
| 3718 |
double norm_hfj = sqrt(hj * hj + dfj * dfj); |
| 3719 |
double dot_hf = -hi * hj + dfi * dfj; |
| 3720 |
|
| 3721 |
dot_hf /= norm_hfi; dot_hf /= norm_hfj; |
| 3722 |
|
| 3723 |
if (dot_hf < -0.5) continue; |
| 3724 |
|
| 3725 |
double si = fabs(dfi) / hi; |
| 3726 |
double sj = fabs(dfj) / hj; |
| 3727 |
|
| 3728 |
if (si > sj) |
| 3729 |
{ |
| 3730 |
_seed_disc_neighbors[iseed][ifunc][i] = true; |
| 3731 |
size_t num_ii_neighbors; get_num_seed_neighbors(neighbor_i, num_ii_neighbors); |
| 3732 |
for (size_t ii = 0; ii < num_ii_neighbors; ii++) |
| 3733 |
{ |
| 3734 |
if (_seed_neighbors[neighbor_i][2 + ii] == iseed) _seed_disc_neighbors[neighbor_i][ifunc][ii] = true; |
| 3735 |
} |
| 3736 |
} |
| 3737 |
else |
| 3738 |
{ |
| 3739 |
_seed_disc_neighbors[iseed][ifunc][j] = true; |
| 3740 |
size_t num_jj_neighbors; get_num_seed_neighbors(neighbor_j, num_jj_neighbors); |
| 3741 |
for (size_t jj = 0; jj < num_jj_neighbors; jj++) |
| 3742 |
{ |
| 3743 |
if (_seed_neighbors[neighbor_j][2 + jj] == iseed) _seed_disc_neighbors[neighbor_j][ifunc][jj] = true; |
| 3744 |
} |
| 3745 |
} |
| 3746 |
} |
| 3747 |
} |
| 3748 |
} |
| 3749 |
} |
| 3750 |
return 0; |
| 3751 |
//#pragma endregion |
| 3752 |
} |
| 3753 |
|
| 3754 |
int VPS::sample_voronoi_facet(size_t seed_index, double* xmin, double* xmax, double diag, double* v) |
| 3755 |
{ |
| 3756 |
|
| 3757 |
//#pragma region Sample A Voronoi facet bounding Cell with seed_index: |
| 3758 |
|
| 3759 |
size_t num_basis(0); |
| 3760 |
double** basis = new double*[_num_dim]; |
| 3761 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3762 |
{ |
| 3763 |
|
| 3764 |
basis[idim] = new double[_num_dim]; |
| 3765 |
|
| 3766 |
for (size_t jdim = 0; jdim < _num_dim; jdim++) basis[idim][jdim] = 0.0; |
| 3767 |
|
| 3768 |
basis[idim][idim] = 1.0; |
| 3769 |
|
| 3770 |
} |
| 3771 |
|
| 3772 |
double* xst = new double[_num_dim]; |
| 3773 |
double* xend = new double[_num_dim]; |
| 3774 |
double* dart = new double[_num_dim]; |
| 3775 |
size_t* simplex = new size_t[_num_dim + 1]; |
| 3776 |
double* vect = new double[_num_dim]; |
| 3777 |
for (size_t idim = 0; idim < _num_dim; idim++) xst[idim] = _x[seed_index][idim]; |
| 3778 |
|
| 3779 |
double closest_distance = 0.0; |
| 3780 |
// add first point |
| 3781 |
size_t num_simplex_seeds(0); |
| 3782 |
simplex[num_simplex_seeds] = seed_index; num_simplex_seeds++; |
| 3783 |
|
| 3784 |
size_t num_misses(0); size_t vertex_dim(0); |
| 3785 |
|
| 3786 |
while (num_simplex_seeds <= _num_dim - vertex_dim) |
| 3787 |
{ |
| 3788 |
// Throw a random spoke |
| 3789 |
sample_uniformly_from_unit_sphere(dart, _num_dim - num_basis); |
| 3790 |
|
| 3791 |
for (size_t idim = 0; idim < _num_dim; idim++) xend[idim] = xst[idim]; |
| 3792 |
|
| 3793 |
for (size_t ibasis = num_basis; ibasis < _num_dim; ibasis++) |
| 3794 |
{ |
| 3795 |
for (size_t idim = 0; idim < _num_dim; idim++) xend[idim] += diag * dart[ibasis - num_basis] * basis[ibasis][idim]; |
| 3796 |
} |
| 3797 |
|
| 3798 |
closest_distance = 0.0; |
| 3799 |
|
| 3800 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3801 |
{ |
| 3802 |
double dx = xend[idim] - _x[seed_index][idim]; |
| 3803 |
closest_distance += dx * dx; |
| 3804 |
} |
| 3805 |
closest_distance = sqrt(closest_distance); |
| 3806 |
|
| 3807 |
bool done(true); |
| 3808 |
size_t neighbor(seed_index); |
| 3809 |
while (true) |
| 3810 |
{ |
| 3811 |
// get closest point to xend |
| 3812 |
size_t closest_seed(seed_index); |
| 3813 |
|
| 3814 |
double excluded_closest_distance = closest_distance + 1E-10; |
| 3815 |
|
| 3816 |
get_closest_seed_tree(xend, num_simplex_seeds, simplex, closest_seed, excluded_closest_distance); |
| 3817 |
|
| 3818 |
bool vertex_out_of_bounding_box(false); |
| 3819 |
|
| 3820 |
if (neighbor == closest_seed) |
| 3821 |
{ |
| 3822 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3823 |
{ |
| 3824 |
if (xend[idim] > xmin[idim] - 1E-10 && xend[idim] < xmax[idim] + 1E-10) continue; |
| 3825 |
vertex_out_of_bounding_box = true; |
| 3826 |
break; |
| 3827 |
} |
| 3828 |
} |
| 3829 |
|
| 3830 |
if (closest_seed == _num_samples || (neighbor == closest_seed && vertex_out_of_bounding_box)) |
| 3831 |
{ |
| 3832 |
// reset sampling |
| 3833 |
num_simplex_seeds = 1; |
| 3834 |
for (size_t idim = 0; idim < _num_dim; idim++) xst[idim] = _x[seed_index][idim]; |
| 3835 |
|
| 3836 |
num_basis = 0; |
| 3837 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3838 |
{ |
| 3839 |
for (size_t jdim = 0; jdim < _num_dim; jdim++) basis[idim][jdim] = 0.0; |
| 3840 |
basis[idim][idim] = 1.0; |
| 3841 |
} |
| 3842 |
num_misses++; |
| 3843 |
|
| 3844 |
if (num_misses == 100) |
| 3845 |
{ |
| 3846 |
vertex_dim++; num_misses = 0; |
| 3847 |
} |
| 3848 |
done = false; |
| 3849 |
break; |
| 3850 |
} |
| 3851 |
|
| 3852 |
if (neighbor == closest_seed || fabs(excluded_closest_distance - closest_distance) < 1E-10) |
| 3853 |
{ |
| 3854 |
//#pragma region A voronoi neighbor apply recursion: |
| 3855 |
simplex[num_simplex_seeds] = neighbor; num_simplex_seeds++; |
| 3856 |
|
| 3857 |
// update spoke start |
| 3858 |
for (size_t idim = 0; idim < _num_dim; idim++) xst[idim] = xend[idim]; |
| 3859 |
|
| 3860 |
// update basis |
| 3861 |
for (size_t idim = 0; idim < _num_dim; idim++) vect[idim] = _x[closest_seed][idim] - _x[seed_index][idim]; |
| 3862 |
|
| 3863 |
double norm; |
| 3864 |
get_normal_component(_num_dim, num_basis, basis, vect, norm); |
| 3865 |
for (size_t idim = 0; idim < _num_dim; idim++) basis[num_basis][idim] = vect[idim]; |
| 3866 |
num_basis++; |
| 3867 |
|
| 3868 |
// update remaining basis |
| 3869 |
for (size_t ibasis = num_basis; ibasis < _num_dim; ibasis++) |
| 3870 |
{ |
| 3871 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3872 |
{ |
| 3873 |
for (size_t jdim = 0; jdim < _num_dim; jdim++) vect[jdim] = 0.0; |
| 3874 |
vect[idim] = 1.0; |
| 3875 |
get_normal_component(_num_dim, num_basis, basis, vect, norm); |
| 3876 |
if (norm > 0.1) break; |
| 3877 |
} |
| 3878 |
|
| 3879 |
for (size_t idim = 0; idim < _num_dim; idim++) basis[ibasis][idim] = vect[idim]; |
| 3880 |
} |
| 3881 |
break; |
| 3882 |
//#pragma endregion |
| 3883 |
} |
| 3884 |
else |
| 3885 |
{ |
| 3886 |
neighbor = closest_seed; |
| 3887 |
trim_spoke(_num_dim, xst, xend, _x[seed_index], _x[neighbor]); |
| 3888 |
closest_distance = 0.0; |
| 3889 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3890 |
{ |
| 3891 |
double dx = xend[idim] - _x[seed_index][idim]; |
| 3892 |
closest_distance += dx * dx; |
| 3893 |
} |
| 3894 |
closest_distance = sqrt(closest_distance); |
| 3895 |
} |
| 3896 |
} |
| 3897 |
if (done) break; |
| 3898 |
} |
| 3899 |
|
| 3900 |
for (size_t idim = 0; idim < _num_dim; idim++) v[idim] = xend[idim]; |
| 3901 |
for (size_t idim = 0; idim < _num_dim; idim++) delete[] basis[idim]; |
| 3902 |
delete[] basis; |
| 3903 |
|
| 3904 |
delete[] xst; delete[] xend; delete[] dart; delete[] simplex; delete[] vect; |
| 3905 |
return 0; |
| 3906 |
//#pragma endregion |
| 3907 |
} |
| 3908 |
|
| 3909 |
int VPS::sample_voronoi_vertex(size_t seed_index, double* xmin, double* xmax, double diag, double* v) |
| 3910 |
{ |
| 3911 |
|
| 3912 |
//#pragma region Sample A Voronoi Vertex bounding Cell with seed_index: |
| 3913 |
|
| 3914 |
size_t num_basis(0); |
| 3915 |
double** basis = new double*[_num_dim]; |
| 3916 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3917 |
{ |
| 3918 |
|
| 3919 |
basis[idim] = new double[_num_dim]; |
| 3920 |
|
| 3921 |
for (size_t jdim = 0; jdim < _num_dim; jdim++) basis[idim][jdim] = 0.0; |
| 3922 |
|
| 3923 |
basis[idim][idim] = 1.0; |
| 3924 |
|
| 3925 |
} |
| 3926 |
|
| 3927 |
double* xst = new double[_num_dim]; |
| 3928 |
double* xend = new double[_num_dim]; |
| 3929 |
double* dart = new double[_num_dim]; |
| 3930 |
size_t* simplex = new size_t[_num_dim + 1]; |
| 3931 |
double* vect = new double[_num_dim]; |
| 3932 |
for (size_t idim = 0; idim < _num_dim; idim++) xst[idim] = _x[seed_index][idim]; |
| 3933 |
|
| 3934 |
double closest_distance = 0.0; |
| 3935 |
// add first point |
| 3936 |
size_t num_simplex_seeds(0); |
| 3937 |
simplex[num_simplex_seeds] = seed_index; num_simplex_seeds++; |
| 3938 |
|
| 3939 |
size_t num_misses(0); size_t vertex_dim(0); |
| 3940 |
|
| 3941 |
while (num_simplex_seeds <= _num_dim - vertex_dim) |
| 3942 |
{ |
| 3943 |
// Throw a random spoke |
| 3944 |
sample_uniformly_from_unit_sphere(dart, _num_dim - num_basis); |
| 3945 |
|
| 3946 |
for (size_t idim = 0; idim < _num_dim; idim++) xend[idim] = xst[idim]; |
| 3947 |
|
| 3948 |
for (size_t ibasis = num_basis; ibasis < _num_dim; ibasis++) |
| 3949 |
{ |
| 3950 |
for (size_t idim = 0; idim < _num_dim; idim++) xend[idim] += diag * dart[ibasis - num_basis] * basis[ibasis][idim]; |
| 3951 |
} |
| 3952 |
|
| 3953 |
closest_distance = 0.0; |
| 3954 |
|
| 3955 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3956 |
{ |
| 3957 |
double dx = xend[idim] - _x[seed_index][idim]; |
| 3958 |
closest_distance += dx * dx; |
| 3959 |
} |
| 3960 |
closest_distance = sqrt(closest_distance); |
| 3961 |
|
| 3962 |
size_t neighbor(seed_index); |
| 3963 |
while (true) |
| 3964 |
{ |
| 3965 |
// get closest point to xend |
| 3966 |
size_t closest_seed(seed_index); |
| 3967 |
|
| 3968 |
double excluded_closest_distance = closest_distance + 1E-10; |
| 3969 |
|
| 3970 |
get_closest_seed_tree(xend, num_simplex_seeds, simplex, closest_seed, excluded_closest_distance); |
| 3971 |
|
| 3972 |
bool vertex_out_of_bounding_box(false); |
| 3973 |
|
| 3974 |
if (neighbor == closest_seed) |
| 3975 |
{ |
| 3976 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3977 |
{ |
| 3978 |
if (xend[idim] > xmin[idim] - 1E-10 && xend[idim] < xmax[idim] + 1E-10) continue; |
| 3979 |
vertex_out_of_bounding_box = true; |
| 3980 |
break; |
| 3981 |
} |
| 3982 |
} |
| 3983 |
|
| 3984 |
if (closest_seed == _num_samples || (neighbor == closest_seed && vertex_out_of_bounding_box)) |
| 3985 |
{ |
| 3986 |
// reset sampling |
| 3987 |
num_simplex_seeds = 1; |
| 3988 |
for (size_t idim = 0; idim < _num_dim; idim++) xst[idim] = _x[seed_index][idim]; |
| 3989 |
|
| 3990 |
num_basis = 0; |
| 3991 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 3992 |
{ |
| 3993 |
for (size_t jdim = 0; jdim < _num_dim; jdim++) basis[idim][jdim] = 0.0; |
| 3994 |
basis[idim][idim] = 1.0; |
| 3995 |
} |
| 3996 |
num_misses++; |
| 3997 |
|
| 3998 |
if (num_misses == 100) |
| 3999 |
{ |
| 4000 |
vertex_dim++; num_misses = 0; |
| 4001 |
} |
| 4002 |
break; |
| 4003 |
} |
| 4004 |
|
| 4005 |
if (neighbor == closest_seed || fabs(excluded_closest_distance - closest_distance) < 1E-10) |
| 4006 |
{ |
| 4007 |
//#pragma region A voronoi neighbor apply recursion: |
| 4008 |
simplex[num_simplex_seeds] = neighbor; num_simplex_seeds++; |
| 4009 |
|
| 4010 |
// update spoke start |
| 4011 |
for (size_t idim = 0; idim < _num_dim; idim++) xst[idim] = xend[idim]; |
| 4012 |
|
| 4013 |
// update basis |
| 4014 |
for (size_t idim = 0; idim < _num_dim; idim++) vect[idim] = _x[closest_seed][idim] - _x[seed_index][idim]; |
| 4015 |
|
| 4016 |
double norm; |
| 4017 |
get_normal_component(_num_dim, num_basis, basis, vect, norm); |
| 4018 |
for (size_t idim = 0; idim < _num_dim; idim++) basis[num_basis][idim] = vect[idim]; |
| 4019 |
num_basis++; |
| 4020 |
|
| 4021 |
// update remaining basis |
| 4022 |
for (size_t ibasis = num_basis; ibasis < _num_dim; ibasis++) |
| 4023 |
{ |
| 4024 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 4025 |
{ |
| 4026 |
for (size_t jdim = 0; jdim < _num_dim; jdim++) vect[jdim] = 0.0; |
| 4027 |
vect[idim] = 1.0; |
| 4028 |
get_normal_component(_num_dim, num_basis, basis, vect, norm); |
| 4029 |
if (norm > 0.1) break; |
| 4030 |
} |
| 4031 |
|
| 4032 |
for (size_t idim = 0; idim < _num_dim; idim++) basis[ibasis][idim] = vect[idim]; |
| 4033 |
} |
| 4034 |
break; |
| 4035 |
//#pragma endregion |
| 4036 |
} |
| 4037 |
else |
| 4038 |
{ |
| 4039 |
neighbor = closest_seed; |
| 4040 |
trim_spoke(_num_dim, xst, xend, _x[seed_index], _x[neighbor]); |
| 4041 |
closest_distance = 0.0; |
| 4042 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 4043 |
{ |
| 4044 |
double dx = xend[idim] - _x[seed_index][idim]; |
| 4045 |
closest_distance += dx * dx; |
| 4046 |
} |
| 4047 |
closest_distance = sqrt(closest_distance); |
| 4048 |
} |
| 4049 |
} |
| 4050 |
} |
| 4051 |
|
| 4052 |
for (size_t idim = 0; idim < _num_dim; idim++) v[idim] = xend[idim]; |
| 4053 |
for (size_t idim = 0; idim < _num_dim; idim++) delete[] basis[idim]; |
| 4054 |
delete[] basis; |
| 4055 |
|
| 4056 |
delete[] xst; delete[] xend; delete[] dart; delete[] simplex; delete[] vect; |
| 4057 |
return 0; |
| 4058 |
//#pragma endregion |
| 4059 |
} |
| 4060 |
|
| 4061 |
bool VPS::trim_spoke(size_t num_dim, double* xst, double* xend, double* p, double* q) |
| 4062 |
{ |
| 4063 |
|
| 4064 |
//#pragma region Trim a Spoke: |
| 4065 |
double* nH = new double[num_dim]; |
| 4066 |
double* xH = new double[num_dim]; |
| 4067 |
|
| 4068 |
// trim spoke using Voronoi hyperplane between ipoint and iclosest |
| 4069 |
//double norm(0.0); |
| 4070 |
for (size_t idim = 0; idim < num_dim; idim++) |
| 4071 |
{ |
| 4072 |
nH[idim] = q[idim] - p[idim]; |
| 4073 |
xH[idim] = 0.5 * (q[idim] + p[idim]); |
| 4074 |
} |
| 4075 |
|
| 4076 |
double dotv(0.0), dote(0.0); |
| 4077 |
for (size_t idim = 0; idim < num_dim; idim++) |
| 4078 |
{ |
| 4079 |
double dxv = xH[idim] - xst[idim]; |
| 4080 |
double dxe = xend[idim] - xst[idim]; |
| 4081 |
dotv += dxv * nH[idim]; |
| 4082 |
dote += dxe * nH[idim]; |
| 4083 |
} |
| 4084 |
delete[] nH; delete[] xH; |
| 4085 |
bool trimmed = false; |
| 4086 |
|
| 4087 |
if (fabs(dote) > 1E-10) |
| 4088 |
{ |
| 4089 |
double u = dotv / dote; |
| 4090 |
if (u < 0.0) u = 0.0; |
| 4091 |
if (u > -1.0E-10 && u < 1.0 - 1.0E-10) |
| 4092 |
{ |
| 4093 |
for (size_t idim = 0; idim < num_dim; idim++) xend[idim] = xst[idim] + u * (xend[idim] - xst[idim]); |
| 4094 |
trimmed = true; |
| 4095 |
} |
| 4096 |
} |
| 4097 |
return trimmed; |
| 4098 |
//#pragma endregion |
| 4099 |
} |
| 4100 |
|
| 4101 |
int VPS::get_normal_component(size_t num_dim, size_t num_basis, double** basis, double* vect, double &norm) |
| 4102 |
{ |
| 4103 |
//#pragma region Get Normal component to some basis: |
| 4104 |
double* comp = new double[num_basis]; |
| 4105 |
|
| 4106 |
// project point to current basis |
| 4107 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) |
| 4108 |
{ |
| 4109 |
comp[ibasis] = 0.0; |
| 4110 |
for (size_t idim = 0; idim < num_dim; idim++) comp[ibasis] += vect[idim] * basis[ibasis][idim]; |
| 4111 |
} |
| 4112 |
// get vector component orthogonal to current basis |
| 4113 |
for (size_t ibasis = 0; ibasis < num_basis; ibasis++) |
| 4114 |
{ |
| 4115 |
for (size_t idim = 0; idim < num_dim; idim++) vect[idim] -= comp[ibasis] * basis[ibasis][idim]; |
| 4116 |
} |
| 4117 |
delete[] comp; |
| 4118 |
|
| 4119 |
norm = 0.0; |
| 4120 |
for (size_t idim = 0; idim < num_dim; idim++) norm += vect[idim] * vect[idim]; |
| 4121 |
|
| 4122 |
if (fabs(norm) < 1E-10) return 1; |
| 4123 |
|
| 4124 |
norm = 1.0 / sqrt(norm); |
| 4125 |
|
| 4126 |
for (size_t idim = 0; idim < num_dim; idim++) vect[idim] *= norm; |
| 4127 |
|
| 4128 |
return 0; |
| 4129 |
|
| 4130 |
//#pragma endregion |
| 4131 |
} |
| 4132 |
|
| 4133 |
|
| 4134 |
void VPS::plot_vps_frames(std::string file_name, size_t function_index, size_t nx, size_t ny, size_t num_contours, bool plot_graph) |
| 4135 |
{ |
| 4136 |
// #pragma region Plot Frames: |
| 4137 |
double* xsec = new double[_num_dim]; |
| 4138 |
for (size_t idim = 0; idim < _num_dim; idim++) xsec[idim] = 0.5 * (_xmin[idim] + _xmax[idim]); |
| 4139 |
size_t dim_i(0), dim_j(1), dim_k(2), dim_l(3); |
| 4140 |
|
| 4141 |
std::fstream ps_file; |
| 4142 |
double scale; |
| 4143 |
create_ps_file(file_name, nx, ny, ps_file, scale); |
| 4144 |
|
| 4145 |
double fmin(DBL_MAX), fmax(-DBL_MAX); |
| 4146 |
|
| 4147 |
size_t num_misses(0), max_misses(100); |
| 4148 |
double* xx = new double[_num_dim]; |
| 4149 |
double* ff = new double[_num_functions]; |
| 4150 |
while (num_misses < max_misses) |
| 4151 |
{ |
| 4152 |
for (size_t idim = 0; idim < _num_dim; idim++) |
| 4153 |
{ |
| 4154 |
xx[idim] = _xmin[idim] + generate_a_random_number() * (_xmax[idim] - _xmin[idim]); |
| 4155 |
} |
| 4156 |
evaluate_surrogate(xx, ff); |
| 4157 |
if (ff[function_index] < fmin) |
| 4158 |
{ |
| 4159 |
fmin = ff[function_index]; num_misses = 0; |
| 4160 |
} |
| 4161 |
else num_misses++; |
| 4162 |
if (ff[function_index] > fmax) |
| 4163 |
{ |
| 4164 |
fmax = ff[function_index]; |
| 4165 |
num_misses = 0; |
| 4166 |
} |
| 4167 |
else num_misses++; |
| 4168 |
} |
| 4169 |
delete[] xx; |
| 4170 |
delete[] ff; |
| 4171 |
|
| 4172 |
std::vector<double> contours; |
| 4173 |
contours.push_back(fmin - 2 * (fmax - fmin)); |
| 4174 |
for (size_t i = 0; i < num_contours; i++) contours.push_back(fmin + (1.0 / num_contours) * i * (fmax - fmin)); |
| 4175 |
contours.push_back(fmax + 2 * (fmax - fmin)); |
| 4176 |
|
| 4177 |
for (size_t i = 0; i < nx; i++) |
| 4178 |
{ |
| 4179 |
xsec[dim_k] = _xmin[dim_k] + i * 1.0 / (nx - 1) * (_xmax[dim_k] - _xmin[dim_k]); |
| 4180 |
for (size_t j = 0; j < ny; j++) |
| 4181 |
{ |
| 4182 |
xsec[dim_l] = _xmin[dim_l] + j * 1.0 / (ny - 1) * (_xmax[dim_l] - _xmin[dim_l]); |
| 4183 |
plot_vps_surrogate_frame(ps_file, scale, function_index, xsec, dim_i, dim_j, i, j, nx, ny, false, contours); |
| 4184 |
} |
| 4185 |
} |
| 4186 |
ps_file << "showpage" << std::endl; |
| 4187 |
|
| 4188 |
delete[] xsec; |
| 4189 |
//#pragma endregion |
| 4190 |
} |
| 4191 |
|
| 4192 |
void VPS::create_ps_file(std::string file_name, size_t nx, size_t ny, std::fstream &file, double &scale) |
| 4193 |
{ |
| 4194 |
//#pragma region Create PS file: |
| 4195 |
//std::cout << ".: VPS Debug Mode :. Plotting ps files .... " << std::endl; |
| 4196 |
|
| 4197 |
file.open(file_name.c_str(), std::ios::out); |
| 4198 |
file << "%!PS-Adobe-3.0" << std::endl; |
| 4199 |
file << "72 72 scale % one unit = one inch" << std::endl; |
| 4200 |
|
| 4201 |
double xmin(_xmin[0]); |
| 4202 |
double ymin(_xmin[1]); |
| 4203 |
double Lx(_xmax[0] - _xmin[0]); |
| 4204 |
double Ly(_xmax[1] - _xmin[0]); |
| 4205 |
|
| 4206 |
Lx *= (nx + 1); Ly *= (ny + 1); |
| 4207 |
|
| 4208 |
double scale_x, scale_y; |
| 4209 |
double shift_x, shift_y; |
| 4210 |
|
| 4211 |
scale_x = 6.5 / Lx; |
| 4212 |
scale_y = 9.0 / Ly; |
| 4213 |
|
| 4214 |
if (scale_x < scale_y) |
| 4215 |
{ |
| 4216 |
scale = scale_x; |
| 4217 |
shift_x = 1.0 - xmin * scale; |
| 4218 |
shift_y = 0.5 * (11.0 - Ly * scale) - ymin * scale; |
| 4219 |
} |
| 4220 |
else |
| 4221 |
{ |
| 4222 |
scale = scale_y; |
| 4223 |
shift_x = 0.5 * (8.5 - Lx * scale) - xmin * scale; |
| 4224 |
shift_y = 1.0 - ymin * scale; |
| 4225 |
} |
| 4226 |
file << shift_x << " " << shift_y << " translate" << std::endl; |
| 4227 |
|
| 4228 |
file << "/Courier findfont" << std::endl; |
| 4229 |
file << "0.05 scalefont" << std::endl; |
| 4230 |
file << "setfont" << std::endl; |
| 4231 |
|
| 4232 |
//#pragma endregion |
| 4233 |
} |
| 4234 |
|
| 4235 |
void VPS::plot_vps_surrogate_frame(std::fstream &file, double scale, size_t function_index, double* xsec, size_t dim_i, size_t dim_j, |
| 4236 |
size_t frame_i, size_t frame_j, size_t frame_ni, size_t frame_nj, bool plot_graph, std::vector<double> &contours) |
| 4237 |
{ |
| 4238 |
//#pragma region Plot Solid Isocontours Of a Single Frame: |
| 4239 |
|
| 4240 |
double dx = (_xmax[dim_i] - _xmin[dim_i]); |
| 4241 |
double DX = frame_i * dx *(1.0 + 1.0 / (frame_ni - 1)); |
| 4242 |
|
| 4243 |
double dy = (_xmax[dim_j] - _xmin[dim_j]); |
| 4244 |
double DY = frame_j * dy *(1.0 + 1.0 / (frame_nj - 1)); |
| 4245 |
|
| 4246 |
std::vector<double> poly_x; |
| 4247 |
std::vector<double> poly_y; |
| 4248 |
|
| 4249 |
size_t num_cells(100); |
| 4250 |
double* xx = new double[_num_dim]; |
| 4251 |
double* f = new double[_num_functions]; |
| 4252 |
double sx = (_xmax[dim_i] - _xmin[dim_i]) / num_cells; |
| 4253 |
double sy = (_xmax[dim_j] - _xmin[dim_j]) / num_cells; |
| 4254 |
|
| 4255 |
for (size_t idim = 0; idim < _num_dim; idim++) xx[idim] = xsec[idim]; |
| 4256 |
|
| 4257 |
for (size_t i = 0; i < num_cells; i++) |
| 4258 |
{ |
| 4259 |
double xo = _xmin[dim_i] + i * sx; |
| 4260 |
for (size_t j = 0; j < num_cells; j++) |
| 4261 |
{ |
| 4262 |
double fo(0.0), f1(0.0), f2(0.0), f3(0.0); |
| 4263 |
|
| 4264 |
double yo = _xmin[dim_j] + j * sy; |
| 4265 |
xx[dim_i] = xo; xx[dim_j] = yo; |
| 4266 |
evaluate_surrogate(xx, f); |
| 4267 |
fo = f[function_index]; |
| 4268 |
|
| 4269 |
xx[dim_i] = xo + sx; xx[dim_j] = yo; |
| 4270 |
evaluate_surrogate(xx, f); |
| 4271 |
f1 = f[function_index]; |
| 4272 |
|
| 4273 |
xx[dim_i] = xo + sx; xx[dim_j] = yo + sy; |
| 4274 |
evaluate_surrogate(xx, f); |
| 4275 |
f2 = f[function_index]; |
| 4276 |
|
| 4277 |
xx[dim_i] = xo; xx[dim_j] = yo + sy; |
| 4278 |
evaluate_surrogate(xx, f); |
| 4279 |
f3 = f[function_index]; |
| 4280 |
|
| 4281 |
size_t num_isocontours = contours.size(); |
| 4282 |
for (size_t icont = 0; icont < num_isocontours; icont++) |
| 4283 |
{ |
| 4284 |
double contour = contours[icont]; |
| 4285 |
double contour_m = -1000.00; |
| 4286 |
if (icont > 0) contour_m = contours[icont - 1]; |
| 4287 |
|
| 4288 |
//std::cout<< "contour_m = " << contour_m << " , contour = " << contour << std::endl; |
| 4289 |
|
| 4290 |
poly_x.clear(); poly_y.clear(); |
| 4291 |
|
| 4292 |
// moving right |
| 4293 |
if (fo >= contour_m - 1E-10 && fo < contour + 1E-10) |
| 4294 |
{ |
| 4295 |
poly_x.push_back(xo); |
| 4296 |
poly_y.push_back(yo); |
| 4297 |
if ((fo > contour && f1 < contour) || (fo < contour && f1 > contour)) |
| 4298 |
{ |
| 4299 |
double h = sx * (contour - fo) / (f1 - fo); |
| 4300 |
poly_x.push_back(xo + h); |
| 4301 |
poly_y.push_back(yo); |
| 4302 |
} |
| 4303 |
else if ((fo > contour_m && f1 < contour_m) || (fo < contour_m && f1 > contour_m)) |
| 4304 |
{ |
| 4305 |
double h = sx * (contour_m - fo) / (f1 - fo); |
| 4306 |
poly_x.push_back(xo + h); |
| 4307 |
poly_y.push_back(yo); |
| 4308 |
} |
| 4309 |
} |
| 4310 |
else if ((fo > contour_m && f1 < contour_m) || (fo < contour_m && f1 > contour_m)) |
| 4311 |
{ |
| 4312 |
double hm = sx * (contour_m - fo) / (f1 - fo); |
| 4313 |
double h = hm; |
| 4314 |
if ((fo > contour && f1 < contour) || (fo < contour && f1 > contour)) |
| 4315 |
{ |
| 4316 |
h = sx * (contour - fo) / (f1 - fo); |
| 4317 |
} |
| 4318 |
if (h < hm) |
| 4319 |
{ |
| 4320 |
double tmp = h; h = hm; hm = tmp; |
| 4321 |
} |
| 4322 |
poly_x.push_back(xo + hm); |
| 4323 |
poly_y.push_back(yo); |
| 4324 |
|
| 4325 |
if (h - hm > 1E-10) |
| 4326 |
{ |
| 4327 |
poly_x.push_back(xo + h); |
| 4328 |
poly_y.push_back(yo); |
| 4329 |
} |
| 4330 |
} |
| 4331 |
else if ((fo > contour && f1 < contour) || (fo < contour && f1 > contour)) |
| 4332 |
{ |
| 4333 |
double h = sx * (contour - fo) / (f1 - fo); |
| 4334 |
poly_x.push_back(xo + h); |
| 4335 |
poly_y.push_back(yo); |
| 4336 |
} |
| 4337 |
|
| 4338 |
// moving up |
| 4339 |
if (f1 >= contour_m - 1E-10 && f1 < contour + 1E-10) |
| 4340 |
{ |
| 4341 |
poly_x.push_back(xo + sx); |
| 4342 |
poly_y.push_back(yo); |
| 4343 |
if ((f1 > contour && f2 < contour) || (f1 < contour && f2 > contour)) |
| 4344 |
{ |
| 4345 |
double h = sy * (contour - f1) / (f2 - f1); |
| 4346 |
poly_x.push_back(xo + sx); |
| 4347 |
poly_y.push_back(yo + h); |
| 4348 |
} |
| 4349 |
else if ((f1 > contour_m && f2 < contour_m) || (f1 < contour_m && f2 > contour_m)) |
| 4350 |
{ |
| 4351 |
double h = sy * (contour_m - f1) / (f2 - f1); |
| 4352 |
poly_x.push_back(xo + sx); |
| 4353 |
poly_y.push_back(yo + h); |
| 4354 |
} |
| 4355 |
|
| 4356 |
} |
| 4357 |
else if ((f1 > contour_m && f2 < contour_m) || (f1 < contour_m && f2 > contour_m)) |
| 4358 |
{ |
| 4359 |
double hm = sy * (contour_m - f1) / (f2 - f1); |
| 4360 |
double h = hm; |
| 4361 |
if ((f1 > contour && f2 < contour) || (f1 < contour && f2 > contour)) |
| 4362 |
{ |
| 4363 |
h = sy * (contour - f1) / (f2 - f1); |
| 4364 |
} |
| 4365 |
if (h < hm) |
| 4366 |
{ |
| 4367 |
double tmp = h; h = hm; hm = tmp; |
| 4368 |
} |
| 4369 |
poly_x.push_back(xo + sx); |
| 4370 |
poly_y.push_back(yo + hm); |
| 4371 |
|
| 4372 |
if (h - hm > 1E-10) |
| 4373 |
{ |
| 4374 |
poly_x.push_back(xo + sx); |
| 4375 |
poly_y.push_back(yo + h); |
| 4376 |
} |
| 4377 |
} |
| 4378 |
else if ((f1 > contour && f2 < contour) || (f1 < contour && f2 > contour)) |
| 4379 |
{ |
| 4380 |
double h = sy * (contour - f1) / (f2 - f1); |
| 4381 |
poly_x.push_back(xo + sx); |
| 4382 |
poly_y.push_back(yo + h); |
| 4383 |
} |
| 4384 |
|
| 4385 |
// moving left |
| 4386 |
if (f2 >= contour_m - 1E-10 && f2 < contour + 1E-10) |
| 4387 |
{ |
| 4388 |
poly_x.push_back(xo + sx); |
| 4389 |
poly_y.push_back(yo + sy); |
| 4390 |
if ((f2 > contour && f3 < contour) || (f2 < contour && f3 > contour)) |
| 4391 |
{ |
| 4392 |
double h = sx * (contour - f2) / (f3 - f2); |
| 4393 |
poly_x.push_back(xo + sx - h); |
| 4394 |
poly_y.push_back(yo + sy); |
| 4395 |
} |
| 4396 |
else if ((f2 > contour_m && f3 < contour_m) || (f2 < contour_m && f3 > contour_m)) |
| 4397 |
{ |
| 4398 |
double h = sx * (contour_m - f2) / (f3 - f2); |
| 4399 |
poly_x.push_back(xo + sx - h); |
| 4400 |
poly_y.push_back(yo + sy); |
| 4401 |
} |
| 4402 |
} |
| 4403 |
else if ((f2 > contour_m && f3 < contour_m) || (f2 < contour_m && f3 > contour_m)) |
| 4404 |
{ |
| 4405 |
double hm = sx * (contour_m - f2) / (f3 - f2); |
| 4406 |
double h = hm; |
| 4407 |
if ((f2 > contour && f3 < contour) || (f2 < contour && f3 > contour)) |
| 4408 |
{ |
| 4409 |
h = sx * (contour - f2) / (f3 - f2); |
| 4410 |
} |
| 4411 |
if (h < hm) |
| 4412 |
{ |
| 4413 |
double tmp = h; h = hm; hm = tmp; |
| 4414 |
} |
| 4415 |
poly_x.push_back(xo + sx - hm); |
| 4416 |
poly_y.push_back(yo + sy); |
| 4417 |
|
| 4418 |
if (h - hm > 1E-10) |
| 4419 |
{ |
| 4420 |
poly_x.push_back(xo + sx - h); |
| 4421 |
poly_y.push_back(yo + sy); |
| 4422 |
} |
| 4423 |
} |
| 4424 |
else if ((f2 > contour && f3 < contour) || (f2 < contour && f3 > contour)) |
| 4425 |
{ |
| 4426 |
double h = sx * (contour - f2) / (f3 - f2); |
| 4427 |
poly_x.push_back(xo + sx - h); |
| 4428 |
poly_y.push_back(yo + sy); |
| 4429 |
} |
| 4430 |
|
| 4431 |
// moving down |
| 4432 |
if (f3 >= contour_m - 1E-10 && f3 < contour + 1E-10) |
| 4433 |
{ |
| 4434 |
poly_x.push_back(xo); |
| 4435 |
poly_y.push_back(yo + sy); |
| 4436 |
if ((f3 > contour && fo < contour) || (f3 < contour && fo > contour)) |
| 4437 |
{ |
| 4438 |
double h = sy * (contour - f3) / (fo - f3); |
| 4439 |
poly_x.push_back(xo); |
| 4440 |
poly_y.push_back(yo + sy - h); |
| 4441 |
} |
| 4442 |
else if ((f3 > contour_m && fo < contour_m) || (f3 < contour_m && fo > contour_m)) |
| 4443 |
{ |
| 4444 |
double h = sy * (contour_m - f3) / (fo - f3); |
| 4445 |
poly_x.push_back(xo); |
| 4446 |
poly_y.push_back(yo + sy - h); |
| 4447 |
} |
| 4448 |
} |
| 4449 |
else if ((f3 > contour_m && fo < contour_m) || (f3 < contour_m && fo > contour_m)) |
| 4450 |
{ |
| 4451 |
double hm = sy * (contour_m - f3) / (fo - f3); |
| 4452 |
double h = hm; |
| 4453 |
if ((f3 > contour && fo < contour) || (f3 < contour && fo > contour)) |
| 4454 |
{ |
| 4455 |
h = sy * (contour - f3) / (fo - f3); |
| 4456 |
} |
| 4457 |
if (h < hm) |
| 4458 |
{ |
| 4459 |
double tmp = h; h = hm; hm = tmp; |
| 4460 |
} |
| 4461 |
poly_x.push_back(xo); |
| 4462 |
poly_y.push_back(yo + sy - hm); |
| 4463 |
|
| 4464 |
if (h - hm > 1E-10) |
| 4465 |
{ |
| 4466 |
poly_x.push_back(xo); |
| 4467 |
poly_y.push_back(yo + sy - h); |
| 4468 |
} |
| 4469 |
} |
| 4470 |
else if ((f3 > contour && fo < contour) || (f3 < contour && fo > contour)) |
| 4471 |
{ |
| 4472 |
double h = sy * (contour - f3) / (fo - f3); |
| 4473 |
poly_x.push_back(xo); |
| 4474 |
poly_y.push_back(yo + sy - h); |
| 4475 |
} |
| 4476 |
|
| 4477 |
|
| 4478 |
size_t num_corners(poly_x.size()); |
| 4479 |
|
| 4480 |
// shift ploy |
| 4481 |
for (size_t icorner = 0; icorner < num_corners; icorner++) |
| 4482 |
{ |
| 4483 |
poly_x[icorner] += DX; poly_y[icorner] += DY; |
| 4484 |
} |
| 4485 |
|
| 4486 |
if (num_corners > 1) |
| 4487 |
{ |
| 4488 |
double gs = 1.0 - icont * 1.0 / num_isocontours; |
| 4489 |
file << "newpath" << std::endl; |
| 4490 |
file << poly_x[0] * scale << " " << poly_y[0] * scale << " moveto" << std::endl; |
| 4491 |
//std::cout<< "*** x = " << poly_x[0] << ", y = " << poly_y[0] << std::endl; |
| 4492 |
for (size_t icorner = 1; icorner < num_corners; icorner++) |
| 4493 |
{ |
| 4494 |
file << poly_x[icorner] * scale << " " << poly_y[icorner] * scale << " lineto" << std::endl; |
| 4495 |
//std::cout << "*** x = " << poly_x[icorner] << ", y = " << poly_y[icorner] << std::endl; |
| 4496 |
} |
| 4497 |
//std::cout << std::endl; |
| 4498 |
|
| 4499 |
file << "closepath" << std::endl; |
| 4500 |
file << "gsave" << std::endl; |
| 4501 |
file << "grestore" << std::endl; |
| 4502 |
|
| 4503 |
double r, g, b; |
| 4504 |
|
| 4505 |
if (gs < 0.25) r = 1.0; |
| 4506 |
//else if (gs < 0.5) r = 2.0 - 4.0 * gs; |
| 4507 |
else if (gs < 0.5) r = 1.0 - 16.0 * (gs - 0.25) * (gs - 0.25); |
| 4508 |
else r = 0.0; |
| 4509 |
|
| 4510 |
double go(0.25), gn(1.0 - go); |
| 4511 |
if (gs < go) g = gs / go; |
| 4512 |
else if (gs < gn) g = 1.0; |
| 4513 |
else g = 1.0 / (1.0 - gn) - gs / (1.0 - gn); |
| 4514 |
|
| 4515 |
|
| 4516 |
if (gs < 0.5) b = 0.0; |
| 4517 |
else if (gs < 0.75) b = 1.0 - 16.0 * (gs - 0.75) * (gs - 0.75); |
| 4518 |
else b = 1.0; |
| 4519 |
|
| 4520 |
file << r << " " << g << " " << b << " setrgbcolor" << std::endl; |
| 4521 |
|
| 4522 |
file << " fill" << std::endl; |
| 4523 |
} |
| 4524 |
} |
| 4525 |
} |
| 4526 |
} |
| 4527 |
delete[] xx; delete[] f; |
| 4528 |
} |