X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=nmdscommand.cpp;h=a90ed2965888928f4a90049fe04fe888d773c450;hp=9984247427d5fde267532d2ce2e96a2087afdf0c;hb=df7e3ff9f68ef157b0328a2d353c3258c5d45d89;hpb=c921bbf0623d5200f69b5d83b3c70ea533c69412 diff --git a/nmdscommand.cpp b/nmdscommand.cpp index 9984247..a90ed29 100644 --- a/nmdscommand.cpp +++ b/nmdscommand.cpp @@ -11,51 +11,79 @@ #include "readphylipvector.h" //********************************************************************************************************************** -vector NMDSCommand::getValidParameters(){ +vector NMDSCommand::setParameters(){ try { - string Array[] = {"phylip","axes","mindim","maxdim","iters","maxiters","trace","epsilon","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + CommandParameter paxes("axes", "InputTypes", "", "", "none", "none", "none","",false,false,true); parameters.push_back(paxes); + CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "none", "none","nmds-stress",false,true,true); parameters.push_back(pphylip); + CommandParameter pmaxdim("maxdim", "Number", "", "2", "", "", "","",false,false); parameters.push_back(pmaxdim); + CommandParameter pmindim("mindim", "Number", "", "2", "", "", "","",false,false); parameters.push_back(pmindim); + CommandParameter piters("iters", "Number", "", "10", "", "", "","",false,false); parameters.push_back(piters); + CommandParameter pmaxiters("maxiters", "Number", "", "500", "", "", "","",false,false); parameters.push_back(pmaxiters); + CommandParameter pepsilon("epsilon", "Number", "", "0.000000000001", "", "", "","",false,false); parameters.push_back(pepsilon); + CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir); + CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir); + + vector myArray; + for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); } return myArray; } catch(exception& e) { - m->errorOut(e, "NMDSCommand", "getValidParameters"); + m->errorOut(e, "NMDSCommand", "setParameters"); exit(1); } } //********************************************************************************************************************** -NMDSCommand::NMDSCommand(){ +string NMDSCommand::getHelpString(){ try { - abort = true; - //initialize outputTypes - vector tempOutNames; - outputTypes["nmds"] = tempOutNames; - outputTypes["stress"] = tempOutNames; + string helpString = ""; + helpString += "The nmds command is modelled after the nmds code written in R by Sarah Goslee, using Non-metric multidimensional scaling function using the majorization algorithm from Borg & Groenen 1997, Modern Multidimensional Scaling.\n"; + helpString += "The nmds command parameters are phylip, axes, mindim, maxdim, maxiters, iters and epsilon.\n"; + helpString += "The phylip parameter allows you to enter your distance file.\n"; + helpString += "The axes parameter allows you to enter a file containing a starting configuration.\n"; + helpString += "The maxdim parameter allows you to select the maximum dimensions to use. Default=2\n"; + helpString += "The mindim parameter allows you to select the minimum dimensions to use. Default=2\n"; + helpString += "The maxiters parameter allows you to select the maximum number of iters to try with each random configuration. Default=500\n"; + helpString += "The iters parameter allows you to select the number of random configuration to try. Default=10\n"; + helpString += "The epsilon parameter allows you to select set an acceptable stopping point. Default=1e-12.\n"; + helpString += "Example nmds(phylip=yourDistanceFile).\n"; + helpString += "Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n"; + return helpString; } catch(exception& e) { - m->errorOut(e, "NMDSCommand", "NMDSCommand"); + m->errorOut(e, "NMDSCommand", "getHelpString"); exit(1); } } //********************************************************************************************************************** -vector NMDSCommand::getRequiredParameters(){ - try { - string Array[] = {"phylip"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); - return myArray; - } - catch(exception& e) { - m->errorOut(e, "NMDSCommand", "getRequiredParameters"); - exit(1); - } +string NMDSCommand::getOutputPattern(string type) { + try { + string pattern = ""; + + if (type == "nmds") { pattern = "[filename],nmds.axes"; } + else if (type == "stress") { pattern = "[filename],nmds.stress"; } + else if (type == "iters") { pattern = "[filename],nmds.iters"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; } + + return pattern; + } + catch(exception& e) { + m->errorOut(e, "NMDSCommand", "getOutputPattern"); + exit(1); + } } + //********************************************************************************************************************** -vector NMDSCommand::getRequiredFiles(){ +NMDSCommand::NMDSCommand(){ try { - vector myArray; - return myArray; + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["nmds"] = tempOutNames; + outputTypes["stress"] = tempOutNames; + outputTypes["iters"] = tempOutNames; } catch(exception& e) { - m->errorOut(e, "NMDSCommand", "getRequiredFiles"); + m->errorOut(e, "NMDSCommand", "NMDSCommand"); exit(1); } } @@ -63,15 +91,14 @@ vector NMDSCommand::getRequiredFiles(){ NMDSCommand::NMDSCommand(string option) { try { - abort = false; + abort = false; calledHelp = false; //allow user to run help - if(option == "help") { help(); abort = true; } + if(option == "help") { help(); abort = true; calledHelp = true; } + else if(option == "citation") { citation(); abort = true; calledHelp = true;} else { - //valid paramters for this command - string Array[] = {"phylip","axes","mindim","maxdim","iters","maxiters","trace","epsilon","outputdir", "inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser. getParameters(); @@ -108,12 +135,18 @@ NMDSCommand::NMDSCommand(string option) { //initialize outputTypes vector tempOutNames; outputTypes["nmds"] = tempOutNames; + outputTypes["iters"] = tempOutNames; outputTypes["stress"] = tempOutNames; //required parameters phylipfile = validParameter.validFile(parameters, "phylip", true); if (phylipfile == "not open") { phylipfile = ""; abort = true; } - else if (phylipfile == "not found") { phylipfile = ""; m->mothurOut("You must provide a distance file before running the nmds command."); m->mothurOutEndLine(); abort = true; } + else if (phylipfile == "not found") { + //if there is a current phylip file, use it + phylipfile = m->getPhylipFile(); + if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); } + else { m->mothurOut("You have no current phylip file and the phylip parameter is required."); m->mothurOutEndLine(); abort = true; } + }else { m->setPhylipFile(phylipfile); } axesfile = validParameter.validFile(parameters, "axes", true); if (axesfile == "not open") { axesfile = ""; abort = true; } @@ -125,26 +158,23 @@ NMDSCommand::NMDSCommand(string option) { outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it } - string temp = validParameter.validFile(parameters, "mindim", false); if (temp == "not found") { temp = "1"; } - convert(temp, mindim); + string temp = validParameter.validFile(parameters, "mindim", false); if (temp == "not found") { temp = "2"; } + m->mothurConvert(temp, mindim); temp = validParameter.validFile(parameters, "maxiters", false); if (temp == "not found") { temp = "500"; } - convert(temp, maxIters); + m->mothurConvert(temp, maxIters); temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "10"; } - convert(temp, iters); + m->mothurConvert(temp, iters); temp = validParameter.validFile(parameters, "maxdim", false); if (temp == "not found") { temp = "2"; } - convert(temp, maxdim); + m->mothurConvert(temp, maxdim); temp = validParameter.validFile(parameters, "epsilon", false); if (temp == "not found") { temp = "0.000000000001"; } - convert(temp, epsilon); - - temp = validParameter.validFile(parameters, "trace", false); if (temp == "not found") { temp = "F"; } - trace = m->isTrue(temp); + m->mothurConvert(temp, epsilon); if (mindim < 1) { m->mothurOut("mindim must be at least 1."); m->mothurOutEndLine(); abort = true; } - if (maxdim < mindim) { m->mothurOut("maxdim must be greater than mindim."); m->mothurOutEndLine(); abort = true; } + if (maxdim < mindim) { maxdim = mindim; } } } @@ -154,33 +184,10 @@ NMDSCommand::NMDSCommand(string option) { } } //********************************************************************************************************************** -void NMDSCommand::help(){ - try { - m->mothurOut("The nmds command is modelled after the nmds code written in R by Sarah Goslee, using Non-metric multidimensional scaling function using the majorization algorithm from Borg & Groenen 1997, Modern Multidimensional Scaling."); m->mothurOutEndLine(); - m->mothurOut("The nmds command parameters are phylip, axes, mindim, maxdim, maxiters, iters, epsilon and trace."); m->mothurOutEndLine(); - m->mothurOut("The phylip parameter allows you to enter your distance file."); m->mothurOutEndLine(); - m->mothurOut("The axes parameter allows you to enter a file containing a starting configuration."); m->mothurOutEndLine(); - m->mothurOut("The maxdim parameter allows you to select how maximum dimensions to use. Default=2"); m->mothurOutEndLine(); - m->mothurOut("The mindim parameter allows you to select how minimum dimensions to use. Default=1"); m->mothurOutEndLine(); - m->mothurOut("The maxiters parameter allows you to select the maximum number of iters to try with each random configuration. Default=500"); m->mothurOutEndLine(); - m->mothurOut("The iters parameter allows you to select the number of random configuration to try. Default=10"); m->mothurOutEndLine(); - m->mothurOut("The epsilon parameter allows you to select set an acceptable stopping point. Default=1e-12."); m->mothurOutEndLine(); - m->mothurOut("The trace parameter allows you to see the output after each iter. Default=F"); m->mothurOutEndLine(); - m->mothurOut("Example nmds(phylip=yourDistanceFile).\n"); - m->mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n\n"); - } - catch(exception& e) { - m->errorOut(e, "NMDSCommand", "help"); - exit(1); - } -} -//********************************************************************************************************************** -NMDSCommand::~NMDSCommand(){} -//********************************************************************************************************************** int NMDSCommand::execute(){ try { - if (abort == true) { return 0; } + if (abort == true) { if (calledHelp) { return 0; } return 2; } cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); @@ -197,64 +204,99 @@ int NMDSCommand::execute(){ vector< vector > axes; if (axesfile != "") { axes = readAxes(names); } + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(phylipfile)); + string outputFileName = getOutputFileName("iters",variables); + string stressFileName = getOutputFileName("stress",variables); + outputNames.push_back(outputFileName); outputTypes["iters"].push_back(outputFileName); + outputNames.push_back(stressFileName); outputTypes["stress"].push_back(stressFileName); + + ofstream out, out2; + m->openOutputFile(outputFileName, out); + m->openOutputFile(stressFileName, out2); + + out2.setf(ios::fixed, ios::floatfield); + out2.setf(ios::showpoint); + out.setf(ios::fixed, ios::floatfield); + out.setf(ios::showpoint); + + out2 << "Dimension\tIter\tStress\tRsq" << endl; + + double bestStress = 10000000; + double bestR2 = 10000000; + vector< vector > bestConfig; + int bestDim = 0; + for (int i = mindim; i <= maxdim; i++) { m->mothurOut("Processing Dimension: " + toString(i)); m->mothurOutEndLine(); - string outputFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "dim" + toString(i) + ".nmds"; - string stressFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "dim" + toString(i) + ".stress.nmds"; - outputNames.push_back(outputFileName); outputTypes["nmds"].push_back(outputFileName); - outputNames.push_back(stressFileName); outputTypes["stress"].push_back(stressFileName); - - ofstream out, out2; - m->openOutputFile(outputFileName, out); - m->openOutputFile(stressFileName, out2); - - out2.setf(ios::fixed, ios::floatfield); - out2.setf(ios::showpoint); - out.setf(ios::fixed, ios::floatfield); - out.setf(ios::showpoint); - - out2 << "Iter\tStress\tCorr" << endl; - for (int j = 0; j < iters; j++) { - if (trace) { m->mothurOut(toString(j+1)); m->mothurOutEndLine(); } + m->mothurOut(toString(j+1)); m->mothurOutEndLine(); //get configuration - either randomly generate or resize to this dimension vector< vector > thisConfig; if (axesfile == "") { thisConfig = generateStartingConfiguration(names.size(), i); } else { thisConfig = getConfiguration(axes, i); } - if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; } + if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { m->mothurRemove(outputNames[k]); } return 0; } //calc nmds for this dimension double stress; vector< vector > endConfig = nmdsCalc(matrix, thisConfig, stress); - if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; } + if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { m->mothurRemove(outputNames[k]); } return 0; } //calc euclid distances for new config vector< vector > newEuclid = linearCalc.calculateEuclidianDistance(endConfig); - if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; } + if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { m->mothurRemove(outputNames[k]); } return 0; } //calc correlation between original distances and euclidean distances from this config - double corr = linearCalc.calcPearson(matrix, newEuclid); - corr *= corr; - if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; } + double rsquared = linearCalc.calcPearson(newEuclid, matrix); + rsquared *= rsquared; + if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { m->mothurRemove(outputNames[k]); } return 0; } //output results out << "Config" << (j+1) << '\t'; - for (int k = 0; k < i; k++) { out << "X" << (k+1) << '\t'; } + for (int k = 0; k < i; k++) { out << "axis" << (k+1) << '\t'; } out << endl; - out2 << (j+1) << '\t' << stress << '\t' << corr << endl; + out2 << i << '\t' << (j+1) << '\t' << stress << '\t' << rsquared << endl; output(endConfig, names, out); - if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; } - + //save best + if (stress < bestStress) { + bestDim = i; + bestStress = stress; + bestR2 = rsquared; + bestConfig = endConfig; + } + + if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { m->mothurRemove(outputNames[k]); } return 0; } } - - out.close(); out2.close(); } - if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } + out.close(); out2.close(); + + //output best config + string BestFileName = getOutputFileName("nmds",variables); + outputNames.push_back(BestFileName); outputTypes["nmds"].push_back(BestFileName); + + m->mothurOut("\nNumber of dimensions:\t" + toString(bestDim) + "\n"); + m->mothurOut("Lowest stress :\t" + toString(bestStress) + "\n"); + m->mothurOut("R-squared for configuration:\t" + toString(bestR2) + "\n"); + + ofstream outBest; + m->openOutputFile(BestFileName, outBest); + outBest.setf(ios::fixed, ios::floatfield); + outBest.setf(ios::showpoint); + + outBest << '\t'; + for (int k = 0; k < bestConfig.size(); k++) { outBest << "axis" << (k+1) << '\t'; } + outBest << endl; + + output(bestConfig, names, outBest); + + outBest.close(); + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); @@ -281,8 +323,6 @@ vector< vector > NMDSCommand::nmdsCalc(vector< vector >& matrix, double stress2 = calculateStress(matrix, euclid); stress1 = stress2 + 1.0 + epsilon; - if (trace) { m->mothurOutEndLine(); m->mothurOut("Iter\tStress"); m->mothurOutEndLine(); } - int count = 0; while ((count < maxIters) && (abs(stress1 - stress2) > epsilon)) { count++; @@ -323,9 +363,6 @@ vector< vector > NMDSCommand::nmdsCalc(vector< vector >& matrix, euclid = linearCalc.calculateEuclidianDistance(newConfig); stress2 = calculateStress(matrix, euclid); - - if (trace) { m->mothurOut(count + "\t" + toString(stress1)); m->mothurOutEndLine(); } - } return newConfig; @@ -558,253 +595,7 @@ vector< vector > NMDSCommand::readAxes(vector names){ exit(1); } } -/********************************************************************************************************************** - vector< vector > NMDSCommand::calculateStressGradientVector(vector& eDists, vector& D, double rawStress, double stress, vector< vector >& axes) { - try { - vector< vector > 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 = (stress / sumDij) * eDists[i].dist; - double firstTerm = firstTerm1 - firstTerm2; - - float r = (dimension-1.0); - double temp = 1.0 / (pow(eDists[i].dist, r)); - float absTemp = abs(axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]); - double secondTerm = pow(absTemp, r) * temp; - - double sigNum = 1.0; - if ((axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]) == 0) { sigNum = 0.0; } - else if ((axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]) < 0) { sigNum = -1.0; } - - double results = (firstTerm * secondTerm * sigNum); - cout << i << '\t' << j << '\t' << "results = " << results << endl; - 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 >& 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 >& prevGrad, vector< vector >& grad, vector& 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 > NMDSCommand::calculateNewConfiguration(double magnitude, vector< vector >& axes, vector< vector >& gradient) { - try { - - vector< vector > newAxes = axes; - - for (int i = 0; i < newAxes.size(); i++) { - - if (m->control_pressed) { return newAxes; } - - for (int j = 0; j < newAxes[i].size(); j++) { - newAxes[i][j] = axes[i][j] + ((step / magnitude) * gradient[i][j]); - } - } - - return newAxes; - } - catch(exception& e) { - m->errorOut(e, "NMDSCommand", "calculateNewConfiguration"); - exit(1); - } - }*/ -/********************************************************************************************************************** - //adjust eDists so that it creates monotonically increasing series of succesive values that increase or stay the same, but never decrease - vector NMDSCommand::satisfyMonotonicity(vector eDists, vector partitions) { - try { - - //find averages of each partitions - vector sums; sums.resize(partitions.size(), 0.0); - vector sizes; sizes.resize(partitions.size(), 0); - - for (int i = 0; i < partitions.size(); i++) { - //i is not the last one - int start = partitions[i]; - int end; - if (i != (partitions.size()-1)) { end = partitions[i+1]; } - else{ end = eDists.size(); } - - for (int j = start; j < end; j++) { sums[i] += eDists[j].dist; } - - sizes[i] = (end - start); - } - - - vector D = eDists; - - //i represents the "active block" - int i = 0; - while (i < partitions.size()) { - - if (m->control_pressed) { return D; } - - bool upActive = true; - bool upSatisfied = false; - bool downSatisfied = false; - - //while we are not done with this block - while ((!upSatisfied) || (!downSatisfied)) { - - if (upActive) { - - //are we are upSatisfied? - is the average of the next block greater than mine? - if (i != (partitions.size()-1)) { //if we are the last guy then we are upsatisfied - if ((sums[i+1]/(float)sizes[i+1]) >= (sums[i]/(float)sizes[i])) { - upSatisfied = true; - upActive = false; - }else { - //find new weighted average - double newSum = sums[i] + sums[i+1]; - - //merge blocks - putting everything in i - sums[i] = newSum; - sizes[i] += sizes[i+1]; - partitions[i] = partitions[i+1]; - - sums.erase(sums.begin()+(i+1)); - sizes.erase(sizes.begin()+(i+1)); - partitions.erase(partitions.begin()+(i+1)); - - upActive = false; - } - }else { upSatisfied = true; upActive = false; } - - }else { //downActive - - //are we are DownSatisfied? - is the average of the previous block less than mine? - if (i != 0) { //if we are the first guy then we are downSatisfied - if ((sums[i-1]/(float)sizes[i-1]) <= (sums[i]/(float)sizes[i])) { - downSatisfied = true; - upActive = true; - }else { - //find new weighted average - double newSum = sums[i] + sums[i-1];; - - //merge blocks - putting everything in i-1 - sums[i-1] = newSum; - sizes[i-1] += sizes[i]; - - sums.erase(sums.begin()+i); - sizes.erase(sizes.begin()+i); - partitions.erase(partitions.begin()+i); - i--; - - upActive = true; - } - }else { downSatisfied = true; upActive = true; } - } - } - - i++; // go to next block - } - - //sanity check - for rounding errors - vector averages; averages.resize(sums.size(), 0.0); - for (int i = 0; i < sums.size(); i++) { averages[i] = sums[i] / (float) sizes[i]; } - for (int i = 0; i < averages.size(); i++) { if (averages[i+1] < averages[i]) { averages[i+1] = averages[i]; } } - - //fill D - int placeHolder = 0; - for (int i = 0; i < averages.size(); i++) { - for (int j = 0; j < sizes[i]; j++) { - D[placeHolder].dist = averages[i]; - placeHolder++; - } - } - - return D; - } - catch(exception& e) { - m->errorOut(e, "NMDSCommand", "satisfyMonotonicity"); - exit(1); - } - }*/ +/**********************************************************************************************************************/ -//**********************************************************************************************************************