Auxiliary class for coord calculation. More...
#include <Nps.h>
Public Member Functions | |
CoordCalcFunction (const std::vector< LandmarkDataEntry > myLandmarks) | |
double | f (const Vec_DP &initCoordinates) const |
Static Public Member Functions | |
static double | simplex_min (CoordCalcFunction *functionObject, Vec_DP &init) |
Private Member Functions | |
double | endnodeDistance (const Vec_DP &nodeCoordinates, LandmarkDataEntry landmark) const |
Private Attributes | |
std::vector< LandmarkDataEntry > | landmarks |
Auxiliary class for coord calculation.
Definition at line 62 of file Nps.h.
CoordCalcFunction::CoordCalcFunction | ( | const std::vector< LandmarkDataEntry > | myLandmarks | ) | [inline] |
double CoordCalcFunction::endnodeDistance | ( | const Vec_DP & | nodeCoordinates, | |
LandmarkDataEntry | landmark | |||
) | const [private] |
Definition at line 739 of file Nps.cc.
Referenced by f().
00741 { 00742 double sum_of_squares = 0.0; 00743 for (int i = 0; i < nodeCoordinates.size(); i++) { 00744 sum_of_squares += pow(landmark.coordinates[i] - nodeCoordinates[i], 2); 00745 } 00746 double result = sqrt(sum_of_squares); 00747 return result; 00748 }
double CoordCalcFunction::f | ( | const Vec_DP & | initCoordinates | ) | const |
Definition at line 719 of file Nps.cc.
Referenced by Simplex::high(), Simplex::low(), and simplex_min().
00720 { 00721 double sum = 0; 00722 double rel_diff = 0; 00723 00724 for (uint i = 0; i < landmarks.size(); i++) { 00725 // Distance = RTT in ms / 2 00726 double diff = SIMTIME_DBL(landmarks[i].rtt) / 2 * 1000 - 00727 endnodeDistance(initCoordinates, landmarks[i]); 00728 00729 if (SIMTIME_DBL(landmarks[i].rtt) != 0) { 00730 rel_diff = diff / (SIMTIME_DBL(landmarks[i].rtt) / 2 * 1000); 00731 } else { 00732 opp_error("[CBR] RTT == 0. Node is landmark? This shouldn't happen."); 00733 } 00734 sum += rel_diff * rel_diff; 00735 } 00736 return sum; 00737 }
double CoordCalcFunction::simplex_min | ( | CoordCalcFunction * | functionObject, | |
Vec_DP & | init | |||
) | [static] |
Definition at line 648 of file Nps.cc.
Referenced by Nps::computeOwnCoordinates().
00650 { 00651 double accf = 0.0000001; 00652 double accx = 0.0000001; 00653 uint32_t nmax = 30001; 00654 uint8_t dim = init.size(); 00655 Simplex spx(dim); 00656 00657 int ihi; // Index of highest point at start of each iteration 00658 Vec_DP phi(dim); // Highest point at start of each iteration. 00659 double vhi; // Function value at highest point at start of each iteration. 00660 double vre; // Function value at reflected point. 00661 double vlo, diff; // for checking convergence. 00662 uint32_t count; 00663 00664 // Initialize Simplex 00665 Vec_DP tmp(dim); 00666 spx.functionObject = functionObject; 00667 spx[0] = init; 00668 for (uint8_t i = 0; i < dim; i++) { 00669 tmp[i] = 1; 00670 spx[i+1] = init + tmp; 00671 tmp[i] = 0; 00672 } 00673 00674 Vec_DP debugCoords(dim); 00675 00676 for (count = 1; count <= nmax; count++) { 00677 /* 00678 if ((count % 10000) == 0) { 00679 std::cout << "running loop #" << count << " of " << nmax << endl; 00680 std::cout << "diff: " << diff << std::endl; 00681 std::cout << "vhi: " << vhi << std::endl; 00682 std::cout << "vlo: " << vlo << std::endl; 00683 debugCoords = spx[spx.low()]; 00684 std::cout << "Coords: " << debugCoords << std::endl; 00685 } 00686 */ 00687 ihi = spx.high(&vhi); 00688 phi = spx[ihi]; 00689 spx.reflect(); 00690 vre = functionObject->f(spx[ihi]); 00691 if (vre < functionObject->f(spx[spx.low()])) { 00692 spx[ihi] = phi; // Undo reflection. 00693 spx.reflect_exp(); 00694 vre = functionObject->f(spx[ihi]); 00695 if (vre > functionObject->f(spx[spx.low()])) { 00696 spx[ihi] = phi; 00697 spx.reflect(); 00698 } 00699 } else if (vre >= vhi) { // Equal sign important! 00700 spx[ihi] = phi; // Undo reflection. 00701 spx.contract(); 00702 if (functionObject->f(spx[ihi]) > vhi) { 00703 spx[ihi] = phi; // Undo contraction. 00704 spx.reduce(); 00705 } // else contraction ok. 00706 } // else reflection ok 00707 00708 spx.high(&vhi); 00709 spx.low(&vlo); 00710 diff = vhi - vlo; 00711 if (diff < accf) 00712 if (spx.size() < accx) 00713 break; 00714 } 00715 init = spx[spx.low()]; 00716 return vlo; 00717 }
std::vector<LandmarkDataEntry> CoordCalcFunction::landmarks [private] |