-vector< vector<double> > NMDSCommand::calculateStressGradientVector(vector<seqDist>& eDists, vector<seqDist>& D, double rawStress, double stress, vector< vector<double> >& axes) {
- try {
- vector< vector<double> > gradient; gradient.resize(dimension);
- for (int i = 0; i < gradient.size(); i++) { gradient[i].resize(axes[0].size(), 0.0); }
-
- double sumDij = 0.0;
- for (int i = 0; i < eDists.size(); i++) { sumDij += (eDists[i].dist * eDists[i].dist); }
-
- for (int i = 0; i < eDists.size(); i++) {
-
- for (int j = 0; j < dimension; j++) {
-
- if (m->control_pressed) { return gradient; }
-
- double firstTerm1 = (stress / rawStress) * (eDists[i].dist - D[i].dist);
- double firstTerm2 = eDists[i].dist * (stress / sumDij);
- double firstTerm = firstTerm1 - firstTerm2;
-
- double secondTerm = (axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]) / eDists[i].dist;
-
- double results = (firstTerm * secondTerm);
-
- gradient[j][eDists[i].seq1] += results;
- gradient[j][eDists[i].seq2] -= results;
- }
- }
-
- return gradient;
- }
- catch(exception& e) {
- m->errorOut(e, "NMDSCommand", "calculateStressGradientVector");
- exit(1);
- }
-}
-//**********************************************************************************************************************
-double NMDSCommand::calculateMagnitude(vector< vector<double> >& gradient) {
- try {
- double magnitude = 0.0;
-
- double sum = 0.0;
- for (int i = 0; i < gradient.size(); i++) {
- for (int j = 0; j < gradient[i].size(); j++) {
- sum += (gradient[i][j] * gradient[i][j]);
- }
- }
-
- magnitude = sqrt(((1.0/(float)gradient[0].size()) * sum));
-
- return magnitude;
- }
- catch(exception& e) {
- m->errorOut(e, "NMDSCommand", "calculateMagnitude");
- exit(1);
- }
-}
-//**********************************************************************************************************************
-//described in Kruskal paper page 121 + 122
-double NMDSCommand::calculateStep(vector< vector<double> >& prevGrad, vector< vector<double> >& grad, vector<double>& prevStress) {
- try {
- double newStep = step;
-
- //calc the cos theta
- double sumNum = 0.0;
- double sumDenom1 = 0.0;
- double sumDenom2 = 0.0;
- for (int i = 0; i < prevGrad.size(); i++) {
- for (int j = 0; j < prevGrad[i].size(); j++) {
- sumDenom1 += (grad[i][j] * grad[i][j]);
- sumDenom2 += (prevGrad[i][j] * prevGrad[i][j]);
- sumNum += (grad[i][j] * prevGrad[i][j]);
- }
- }
-
- double cosTheta = sumNum / (sqrt(sumDenom1) * sqrt(sumDenom2));
- cosTheta *= cosTheta;
-
- //calc angle factor
- double angle = pow(4.0, cosTheta);
-
- //calc 5 step ratio
- double currentStress = prevStress[prevStress.size()-1];
- double lastStress = prevStress[0];
- if (prevStress.size() > 1) { lastStress = prevStress[prevStress.size()-2]; }
- double fivePrevStress = prevStress[0];
- if (prevStress.size() > 5) { fivePrevStress = prevStress[prevStress.size()-6]; }
-
- double fiveStepRatio = min(1.0, (currentStress / fivePrevStress));
-
- //calc relaxation factor
- double relaxation = 1.3 / (1.0 + pow(fiveStepRatio, 5.0));
-
- //calc good luck factor
- double goodLuck = min(1.0, (currentStress / lastStress));
-
- //calc newStep
- cout << "\ncos = " << cosTheta << " step = " << step << " angle = " << angle << " relaxation = " << relaxation << " goodluck = " << goodLuck << endl;
- newStep = step * angle * relaxation * goodLuck;
-
- return newStep;
- }
- catch(exception& e) {
- m->errorOut(e, "NMDSCommand", "calculateStep");
- exit(1);
- }
-}
-//**********************************************************************************************************************
-vector< vector<double> > NMDSCommand::calculateNewConfiguration(double magnitude, vector< vector<double> >& axes, vector< vector<double> >& gradient) {