void RandomSampleJobList::CalcAccuracyStats (double& accuracyMean, double& accuracyStdDev, double& trainTimeMean, double& testTimeMean, double& supportVectorsMean ) const { VectorDouble accuracies; VectorDouble trainTimes; VectorDouble testTimes; VectorDouble supportVectors; RandomSampleJobList::const_iterator idx; for (idx = begin(); idx != end (); idx++) { RandomSampleJobPtr j = *idx; accuracies.push_back (j->Accuracy ()); trainTimes.push_back (j->TrainTime ()); testTimes.push_back (j->TestTime ()); supportVectors.push_back (j->SupportVectors ()); } CalcMeanAndStdDev (accuracies, accuracyMean, accuracyStdDev); double stdDev; CalcMeanAndStdDev (trainTimes, trainTimeMean, stdDev); CalcMeanAndStdDev (testTimes, testTimeMean, stdDev); CalcMeanAndStdDev (supportVectors, supportVectorsMean, stdDev); } /* CalcAccuracyStats */
double SegmentorOTSU::sig_func (VectorDouble k, kkint32 nbins, const VectorDouble& P, kkint32 numClasses ) { kkint32 x = 0, y = 0; // muT = sum((1:nbins).*P); double muT = 0.0; for (x = 0, y = 1; x < nbins; ++x, ++y) muT += y * P[x]; //sigma2T = sum(((1:nbins)-muT).^2.*P); double sigma2T = 0.0; for (x = 0, y = 1; x < nbins; ++x, ++y) sigma2T += (pow ((y - muT), 2.0) * P[x]); //k = round(k*(nbins-1)+1); for (x = 0; x < (kkint32)k.size (); ++x) k[x] = floor (k[x] * (nbins - 1) + 1.0 + 0.5); //k = sort(k); sort (k.begin (), k.end ()); //if any(k<1 | k>nbins), y = 1; return, end if ((k[0] < 1.0) || (k[k.size () - 1] > nbins)) return 1.0; //k = [0 k nbins]; % Puts '0' at beginning and 'nbins' at the end. k.insert (k.begin (), 0.0); k.push_back (nbins); // sigma2B = 0; double sigma2B = 0.0; //for j = 1:numClasses for (kkint32 j = 0; j < numClasses; ++j) { //wj = sum(P(k(j)+1:k(j+1))); double wj = SumSubSet (P, (kkint32)(k[j] + 1), (kkint32)k[j + 1]); //if wj==0, y = 1; return, end if (wj == 0.0) return 1.0; //muj = sum((k(j)+1:k(j+1)).*P(k(j)+1:k(j+1)))/wj; kkint32 idxStart = (kkint32)(k[j] + 1); kkint32 idxEnd = (kkint32)(k[j + 1]); double muj = 0.0; for (kkint32 i = idxStart; i <= idxEnd; ++i) muj += (i * P[i] / wj); //sigma2B = sigma2B + wj*(muj-muT)^2; sigma2B = sigma2B + wj * pow ((muj - muT), 2.0); } //y = 1-sigma2B/sigma2T; % within the range [0 1] return 1.0 - sigma2B / sigma2T; } /* sig_func */
VectorDouble SegmentorOTSU::LinSpace (double start, double end, kkint32 numPoints ) { // The First and last point are known leaving (numPoints - 2) left to distribute between "start" and "end". // which means there will be a total of (numPoints - 1) intervals. double inc = (end - start) / (double)(numPoints - 1); VectorDouble result; while (result.size () < (kkuint32)numPoints) { result.push_back (start); start += inc; } return result; } /* LinSpace */
int CProblem::setModel() { //time_t start, end; numvar = 1+n; // lambda + all c; IloEnv env; try { IloModel model(env); IloCplex cplex(env); /*Variables*/ IloNumVar lambda(env, "lambda"); IloNumVarArray c(env, n);// for (unsigned int u=0; u<n; u++) { std::stringstream ss; ss << u; std::string str = "c" + ss.str(); c[u]=IloNumVar(env, str.c_str()); } IloArray<IloIntVarArray> z(env,n); for (unsigned int u=0; u<n; u++) { z[u]= IloIntVarArray(env, n); for (unsigned int v=0; v<n; v++) { std::stringstream ss; ss << u; ss << v; std::string str = "z" + ss.str(); z[u][v] = IloIntVar(env, 0, 1, str.c_str()); } } /* Constant M*/ int M=n*max_d; UB = M; /* Objective*/ model.add(IloMinimize(env, lambda)); //model.add(IloMinimize(env, IloSum(c))); /*Constrains*/ model.add(lambda - UB <= 0); /* d=function of the distance */ IloArray<IloNumArray> Par_d(env,n); for (unsigned int u=0; u<n; u++) { Par_d[u]=IloNumArray(env,n); for (unsigned int v=0; v<n; v++) { Par_d[u][v]=d[u][v]; } } for (unsigned u=0; u<n; u++) { for (unsigned v=0; v<u; v++) { model.add(c[v]-c[u]+M* z[u][v] >= Par_d[u][v] ); model.add(c[u]-c[v]+M*(1-z[u][v]) >= Par_d[u][v]); numvar++; // + z[u][v] } } /* for (unsigned i=0; i<sqrt(n)-1; i=i+1) { for (unsigned j=0; j<sqrt(n)-1; j=j+1) { //square lattice model.add (c[i*sqrt(n)+j] +c[i*sqrt(n)+j+1] + c[(i+1)*sqrt(n)+j] +c[(i+1)*sqrt(n)+j+1]>= 16-4*sqrt(2)); // Bedingung fuer Quadratischen Gridgraph und d(x) = 3-x // // triangular lattice // model.add (c[i*5+j]+c[i*5+j+1] + // c[(i+1)+j] >= 4); // Bedingung fuer Quadratischen Gridgraph und d(x) = 3-x // model.add (c[i*sqrt(n)+j]+c[i*sqrt(n)+j+1] + // c[(i+1)*sqrt(n)+j]+c[(i+1)*sqrt(n)+j+1] >= 22 - 4*sqrt(2)); // Bedingung fuer Quadratischen Gridgraph und d(x) = 4-x } } */ /* for (unsigned i=0; i<sqrt(n)-2; i+=3) { for (unsigned j=0; j<sqrt(n)-2; j+=3) { // model.add (c[i*sqrt(n)+j] + c[i*sqrt(n)+j+1] + c[i*sqrt(n)+j+2] + // c[(i+1)*sqrt(n)+j]+ c[(i+1)*sqrt(n)+j+1]+ c[(i+1)*sqrt(n)+j+2] + // c[(i+2)*sqrt(n)+j]+ c[(i+2)*sqrt(n)+j+1]+ c[(i+2)*sqrt(n)+j+2] // >= 60-17*sqrt(2)-3*sqrt(5)); // Bedingung fuer Quadratischen Gridgraph und d(x) = 3-x // model.add (c[i*sqrt(n)+j] + c[i*sqrt(n)+j+1] + c[i*sqrt(n)+j+2] + // c[(i+1)*sqrt(n)+j]+ c[(i+1)*sqrt(n)+j+1]+ c[(i+1)*sqrt(n)+j+2] + // c[(i+2)*sqrt(n)+j]+ c[(i+2)*sqrt(n)+j+1]+ c[(i+2)*sqrt(n)+j+2] // >= 82-8*sqrt(2)-2*sqrt(5)); // Bedingung fuer Quadratischen Gridgraph und d(x) = 4-x } } */ for (unsigned int v=0; v<n; v++) { IloExpr expr; model.add (c[v] <= lambda); model.add (c[v] >= 0); expr.end(); } std::cout << "Number of variables " << numvar << "\n"; /* solve the Model*/ cplex.extract(model); cplex.exportModel("L-Labeling.lp"); /* start = clock(); int solveError = cplex.solve(); end = clock (); */ IloTimer timer(env); timer.start(); int solveError = cplex.solve(); timer.stop(); if ( !solveError ) { std::cout << "STATUS : "<< cplex.getStatus() << "\n"; env.error() << "Failed to optimize LP \n"; exit(1); } //Info.time = (double)(end-start)/(double)CLOCKS_PER_SEC; Info.time = timer.getTime(); std::cout << "STATUS : "<< cplex.getStatus() << "\n"; /* get the solution*/ env.out() << "Solution status = " << cplex.getStatus() << "\n"; numconst = cplex.getNrows(); env.out() << " Number of constraints = " << numconst << "\n"; lambda_G_d=cplex.getObjValue(); env.out() << "Solution value = " << lambda_G_d << "\n"; for (unsigned int u=0; u<n; u++) { C.push_back(cplex.getValue(c[u])); std::cout << "c(" << u << ")=" << C[u]<< " "; } std::cout <<"\n"; /* for (unsigned int u=0; u<n; u++) { for (unsigned int v=0; v<u; v++) { std::cout<< "z[" << u <<"][" << v << "]="<< cplex.getValue( z[u][v]) << " "; Z.push_back(cplex.getValue( z[u][v])); } std::cout << "\n"; } std::cout <<"\n"; */ } // end try catch (IloException& e) { std::cerr << "Concert exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } env.end(); return 0; }
int CProblem::setModel() { //time_t start, end; IloEnv env; try { IloModel model(env); IloCplex cplex(env); /*Variables*/ IloNumVar lambda(env, "lambda"); IloNumVarArray c(env, n); // for (unsigned int u = 0; u < n; u++) { std::stringstream ss; ss << u; std::string str = "c" + ss.str(); c[u] = IloNumVar(env, str.c_str()); } IloIntVarArray z0(env, Info.numAddVar); for (int i = 0; i < Info.numAddVar; i++) { std::stringstream ss; ss << i; std::string str = "z0_" + ss.str(); z0[i] = IloIntVar(env, 0, 1, str.c_str()); } /* Objective*/ model.add(IloMinimize(env, lambda)); /*Constrains*/ /* d=function of the distance */ IloArray<IloNumArray> Par_d(env, n); for (unsigned int u = 0; u < n; u++) { Par_d[u] = IloNumArray(env, n); for (unsigned int v = 0; v < n; v++) { Par_d[u][v] = d[u][v]; } } int M = (max_distance + 1) * n; for (int i = 0; i < Info.numAddVar; i++) { int u = Info.ConstrIndex[i * 2]; int v = Info.ConstrIndex[i * 2 + 1]; model.add(c[u] - c[v] + M * (z0[i]) >= Par_d[u][v]); model.add(c[v] - c[u] + M * (1 - z0[i]) >= Par_d[u][v]); } // d(x) = 3 - x if (max_distance == 2) { // Gridgraphen 1x2 (6 Knoten) for (int i = 0; i < sqrt(n)-1; i+=1) { for (int j = 0; j < sqrt(n)-2; j+=2) { model.add(c[i * sqrt(n) + j] + c[i * sqrt(n) + j+2] + c[(i+1) * sqrt(n) + j] + c[(i+1) * sqrt(n) + j+2] >= 16.2273); } } // } //d(x) = 4 - x if (max_distance == 3) { // Gridgraphen 1x2 (6 Knoten) for (int i = 0; i < sqrt(n)-1; i+=1) { for (int j = 0; j < sqrt(n)-2; j+=2) { model.add(c[i * sqrt(n) + j] + c[i * sqrt(n) + j+2] + c[(i+1) * sqrt(n) + j] + c[(i+1) * sqrt(n) + j+2] >= 30.283); } } } for (unsigned int v = 0; v < n; v++) { model.add(c[v] <= lambda); model.add(c[v] >= 0); } std::cout << "Number of variables " << Info.numVar << "\n"; /* solve the Model*/ cplex.extract(model); cplex.exportModel("L-Labeling.lp"); IloTimer timer(env); timer.start(); int solveError = cplex.solve(); timer.stop(); if (!solveError) { std::cout << "STATUS : " << cplex.getStatus() << "\n"; env.error() << "Failed to optimize LP \n"; exit(1); } //Info.time = (double)(end-start)/(double)CLOCKS_PER_SEC; Info.time = timer.getTime(); std::cout << "STATUS : " << cplex.getStatus() << "\n"; /* get the solution*/ env.out() << "Solution status = " << cplex.getStatus() << "\n"; Info.numConstr = cplex.getNrows(); env.out() << " Number of constraints = " << Info.numConstr << "\n"; lambda_G_d = cplex.getObjValue(); env.out() << "Solution value = " << lambda_G_d << "\n"; for (unsigned int u = 0; u < n; u++) { C.push_back(cplex.getValue(c[u])); std::cout << "c(" << u << ")=" << C[u] << " "; } } // end try catch (IloException& e) { std::cerr << "Concert exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } env.end(); return 0; }
int CProblem::setModel() { //time_t start, end; numvar = 1+n; // lambda + all c; IloEnv env; try { IloModel model(env); IloCplex cplex(env); /*Variables*/ IloNumVar lambda(env, "lambda"); IloNumVarArray c(env, n);// for (unsigned int u=0; u<n; u++) { std::stringstream ss; ss << u; std::string str = "c" + ss.str(); c[u]=IloNumVar(env, str.c_str()); } IloArray<IloIntVarArray> z(env,n); for (unsigned int u=0; u<n; u++) { z[u]= IloIntVarArray(env, n); for (unsigned int v=0; v<n; v++) { std::stringstream ss; ss << u; ss << v; std::string str = "z" + ss.str(); z[u][v] = IloIntVar(env, 0, 1, str.c_str()); } } /* Constant M*/ int M=n*max_d; UB = M; /* Objective*/ model.add(IloMinimize(env, lambda)); /*Constrains*/ model.add(lambda - UB <= 0); /* d=function of the distance */ IloArray<IloNumArray> Par_d(env,n); for (unsigned int u=0; u<n; u++) { Par_d[u]=IloNumArray(env,n); for (unsigned int v=0; v<n; v++) { Par_d[u][v]=d[u][v]; } } for (unsigned u=0; u<n; u++) { for (unsigned v=0; v<u; v++) { model.add(c[v]-c[u]+M* z[u][v] >= Par_d[u][v] ); model.add(c[u]-c[v]+M*(1-z[u][v]) >= Par_d[u][v]); numvar++; // + z[u][v] } } for (unsigned int v=0; v<n; v++) { IloExpr expr; model.add (c[v] <= lambda); model.add (c[v] >= 0); expr.end(); } std::cout << "Number of variables " << numvar << "\n"; /* solve the Model*/ cplex.extract(model); cplex.exportModel("L-Labeling.lp"); /* start = clock(); int solveError = cplex.solve(); end = clock (); */ // cplex.setParam(IloCplex::Threads, 1); cplex.setParam(IloCplex::ClockType , 1 ); // CPU time IloTimer timer(env); const double startt = cplex.getTime(); timer.start(); int solveError = cplex.solve(); timer.stop(); const double stopt = cplex.getTime() - startt; if ( !solveError ) { std::cout << "STATUS : "<< cplex.getStatus() << "\n"; env.error() << "Failed to optimize LP \n"; exit(1); } //Info.time = (double)(end-start)/(double)CLOCKS_PER_SEC; Info.time = timer.getTime(); std::cout << "STATUS : "<< cplex.getStatus() << "\n"; /* get the solution*/ env.out() << "Solution status = " << cplex.getStatus() << "\n"; env.out() << "Time cplex.getTime " << stopt << "\n"; numconst = cplex.getNrows(); env.out() << " Number of constraints = " << numconst << "\n"; lambda_G_d=cplex.getObjValue(); env.out() << "Solution value = " << lambda_G_d << "\n"; for (unsigned int u=0; u<n; u++) { C.push_back(cplex.getValue(c[u])); std::cout << "c(" << u << ")=" << C[u]<< " "; } std::cout <<"\n"; /* for (unsigned int u=0; u<n; u++) { for (unsigned int v=0; v<u; v++) { std::cout<< "z[" << u <<"][" << v << "]="<< cplex.getValue( z[u][v]) << " "; Z.push_back(cplex.getValue( z[u][v])); } std::cout << "\n"; } std::cout <<"\n"; */ } // end try catch (IloException& e) { std::cerr << "Concert exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } env.end(); return 0; }
int CProblem::setModel() { //time_t start, end; numvar = 1+n; // lambda + all c; IloEnv env; try { IloModel model(env); IloCplex cplex(env); /*Variables*/ IloNumVar lambda(env, "lambda"); IloNumVarArray c(env, n);// for (unsigned int u=0; u<n; u++) { std::stringstream ss; ss << u; std::string str = "c" + ss.str(); c[u]=IloNumVar(env, str.c_str()); } IloArray<IloIntVarArray> z(env,n); for (unsigned int u=0; u<n; u++) { z[u]= IloIntVarArray(env, n); for (unsigned int v=0; v<n; v++) { std::stringstream ss; ss << u; ss << v; std::string str = "z" + ss.str(); z[u][v] = IloIntVar(env, 0, 1, str.c_str()); } } /* Constant M*/ int UB, LB; switch (lattice){ case 1: // hexagonal_lattice if (max_d==3) {LB = 5; UB = 10;} //d(x) = 3 -x else {LB = 9; UB = 27;} break; case 2:// triangular_lattice if (max_d==3) { LB = 8; UB = 16;} //d(x) = 3 -x else {LB = 18; UB = 54;} break; case 3:// square_lattice if (max_d==3) { LB = 6; UB = 12;} //d(x) = 3 -x else { LB = 11; UB = 33;} break; default: UB=n*max_d;break; } std::cout << "Lattice " << lattice << "\n"; std::cout << "UB for lambda " << UB << "\n"; /* Objective*/ model.add(IloMinimize(env, lambda)); /*Constrains*/ model.add(lambda - UB <= 0); model.add(lambda - LB >= 0); /* d=function of the distance */ IloArray<IloNumArray> Par_d(env,n); for (unsigned int u=0; u<n; u++) { Par_d[u]=IloNumArray(env,n); for (unsigned int v=0; v<n; v++) { Par_d[u][v]=d[u][v]; } } int M = INT_MAX;//Par_d[0][0] + UB; for (unsigned u=0; u<n; u++) { for (unsigned v=0; v<u; v++) { model.add(c[v]-c[u]+M* z[u][v] >= Par_d[u][v] ); model.add(c[u]-c[v]+M*(1-z[u][v]) >= Par_d[u][v]); numvar++; // + z[u][v] } } for (unsigned int v=0; v<n; v++) { IloExpr expr; model.add (c[v] <= lambda); model.add (c[v] >= 0); expr.end(); } std::cout << "Number of variables " << numvar << "\n"; /* solve the Model*/ cplex.extract(model); cplex.exportModel("L-Labeling.lp"); IloTimer timer(env); timer.start(); int solveError = cplex.solve(); timer.stop(); if ( !solveError ) { std::cout << "STATUS : "<< cplex.getStatus() << "\n"; env.error() << "Failed to optimize LP \n"; exit(1); } //Info.time = (double)(end-start)/(double)CLOCKS_PER_SEC; Info.time = timer.getTime(); std::cout << "STATUS : "<< cplex.getStatus() << "\n"; /* get the solution*/ env.out() << "Solution status = " << cplex.getStatus() << "\n"; numconst = cplex.getNrows(); env.out() << " Number of constraints = " << numconst << "\n"; lambda_G_d=cplex.getObjValue(); env.out() << "Solution value = " << lambda_G_d << "\n"; for (unsigned int u=0; u<n; u++) { C.push_back(cplex.getValue(c[u])); std::cout << "c(" << u << ")=" << C[u]<< " "; } std::cout <<"\n"; /* for (unsigned int u=0; u<n; u++) { for (unsigned int v=0; v<u; v++) { std::cout<< "z[" << u <<"][" << v << "]="<< cplex.getValue( z[u][v]) << " "; Z.push_back(cplex.getValue( z[u][v])); } std::cout << "\n"; } std::cout <<"\n"; */ } // end try catch (IloException& e) { std::cerr << "Concert exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } env.end(); return 0; }
int CProblem::setModel() { //time_t start, end; IloEnv env; try { IloModel model(env); IloCplex cplex(env); /*Variables*/ IloNumVar lambda(env, "lambda"); IloNumVarArray c(env, n); // for (unsigned int u = 0; u < n; u++) { std::stringstream ss; ss << u; std::string str = "c" + ss.str(); c[u] = IloNumVar(env, str.c_str()); } IloArray<IloIntVarArray> z0(env,n); for (unsigned int i=0; i<n; i++) { std::stringstream ss; ss << i; std::string str = "z0_" + ss.str(); z0[i]= IloIntVarArray(env, n, 0, 1); } /* IloIntVarArray z0(env, Info.numAddVar); for (int i = 0; i < Info.numAddVar; i++) { std::stringstream ss; ss << i; std::string str = "z0_" + ss.str(); z0[i] = IloIntVar(env, 0, 1, str.c_str()); } */ /* Objective*/ model.add(IloMinimize(env, lambda)); /*Constrains*/ /* d=function of the distance */ IloArray<IloNumArray> Par_d(env, n); for (unsigned int u = 0; u < n; u++) { Par_d[u] = IloNumArray(env, n); for (unsigned int v = 0; v < n; v++) { Par_d[u][v] = d[u][v]; } } int M = (max_distance + 1) * n; /* for (int i = 0; i < Info.numAddVar; i++) { int u = Info.ConstrIndex[i * 2]; int v = Info.ConstrIndex[i * 2 + 1]; model.add(c[u] - c[v] + M * (z0[i]) >= Par_d[u][v]); model.add(c[v] - c[u] + M * (1 - z0[i]) >= Par_d[u][v]); } */ for (unsigned u=0; u<n; u++) { for (unsigned v=0; v<u; v++) { if (Par_d[u][v] <= max_distance) { model.add(c[v]-c[u]+M*z0[u][v] >=Par_d[u][v]); model.add(c[u]-c[v]+M*(1-z0[u][v]) >=Par_d[u][v]); //Info.numvar++; } } } // d(x) = 3 - x if (max_distance == 2) { // Sechsecke mit der Seitenlaenge 1 model.add(c[0]+c[2] +c[3] +c[5] +c[6] +c[9]>=16.6077); model.add(c[1]+c[3] +c[4] +c[6] +c[7] +c[10]>=16.6077); model.add(c[5]+c[8] +c[9] +c[12]+c[13]+c[16]>=16.6077); model.add(c[6]+c[9] +c[10]+c[13]+c[14]+c[17]>=16.6077); model.add(c[7]+c[10]+c[11]+c[14]+c[15]+c[18]>=16.6077); model.add(c[13]+c[16]+c[17]+c[19]+c[20]+c[22]>=16.6077); model.add(c[14]+c[17]+c[18]+c[20]+c[21]+c[23]>=16.6077); } //d(x) = 4 - x if (max_distance == 3) { // Sechsecke mit der Seitenlaenge 1 model.add(c[0]+c[2] +c[3] +c[5] +c[6] +c[9]>=31.6077); model.add(c[1]+c[3] +c[4] +c[6] +c[7] +c[10]>=31.6077); model.add(c[5]+c[8] +c[9] +c[12]+c[13]+c[16]>=31.6077); model.add(c[6]+c[9] +c[10]+c[13]+c[14]+c[17]>=31.6077); model.add(c[7]+c[10]+c[11]+c[14]+c[15]+c[18]>=31.6077); model.add(c[13]+c[16]+c[17]+c[19]+c[20]+c[22]>=31.6077); model.add(c[14]+c[17]+c[18]+c[20]+c[21]+c[23]>=31.6077); } for (unsigned int v = 0; v < n; v++) { model.add(c[v] <= lambda); model.add(c[v] >= 0); } std::cout << "Number of variables " << Info.numVar << "\n"; /* solve the Model*/ cplex.extract(model); cplex.exportModel("L-Labeling.lp"); cplex.setParam(IloCplex::ClockType,1); IloTimer timer(env); timer.start(); int solveError = cplex.solve(); timer.stop(); if (!solveError) { std::cout << "STATUS : " << cplex.getStatus() << "\n"; env.error() << "Failed to optimize LP \n"; exit(1); } //Info.time = (double)(end-start)/(double)CLOCKS_PER_SEC; Info.time = timer.getTime(); std::cout << "STATUS : " << cplex.getStatus() << "\n"; /* get the solution*/ env.out() << "Solution status = " << cplex.getStatus() << "\n"; Info.numConstr = cplex.getNrows(); env.out() << " Number of constraints = " << Info.numConstr << "\n"; lambda_G_d = cplex.getObjValue(); env.out() << "Solution value = " << lambda_G_d << "\n"; for (unsigned int u = 0; u < n; u++) { C.push_back(cplex.getValue(c[u])); std::cout << "c(" << u << ")=" << C[u] << " "; } } // end try catch (IloException& e) { std::cerr << "Concert exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } env.end(); return 0; }
void ClassificationBiasMatrix::PerformAdjustmnts (const VectorDouble& classifiedCounts, VectorDouble& adjCounts, VectorDouble& stdErrors ) { // For description of calc's see the paper: // "Estimating the Taxonomic composition of a sample when individuals are classified with error" // by Andrew Solow, Cabll Davis, Qiao Hu // Woods Hole Oceanographic Institution, Woods Hole Massachusetts // Marine Ecology Progress Series // published 2006-july-06; vol 216:309-311 if (classifiedCounts.size () != (kkuint32)numClasses) { KKStr errMsg = "ClassificationBiasMatrix::PerformAdjustmnts ***ERROR*** Disagreement in length of classifiedCounts[" + StrFormatInt ((kkint32)classifiedCounts.size (), "ZZZ0") + "] and Prev Defined ClassList[" + StrFormatInt (numClasses, "ZZZ0") + "]."; runLog.Level (-1) << errMsg << endl; valid = false; throw KKException (errMsg); } kkint32 x = 0; kkint32 i, j, k; // We need to deal with the special case when one entry in the probability diagonal is zero. { for (x = 0; x < numClasses; x++) { if ((*probabilities)[x][x] == 0.0) { // This will cause the inversion of the diagonal matrix to fail. To deal // with this situation; I will steal some probability from other buckets on // same row. double totalAmtStolen = 0.0; double percentToSteal = 0.01; for (i = 0; i < numClasses; i++) { if ((*probabilities)[x][i] != 0.0) { double amtToSteal = (*probabilities)[x][i] * percentToSteal; (*probabilities)[x][i] = (*probabilities)[x][i] - amtToSteal; totalAmtStolen += amtToSteal; } } (*probabilities)[x][x] = totalAmtStolen; } } } Matrix m (numClasses, 1); for (x = 0; x < numClasses; x++) m[x][0] = classifiedCounts[x]; Matrix transposed = probabilities->Transpose (); Matrix Q = transposed.Inverse (); Matrix n = Q * m; Matrix varM (numClasses, numClasses); for (j = 0; j < numClasses; j++) { double varM_j = 0.0; for (i = 0; i < numClasses; i++) { double p = (*probabilities)[i][j]; varM_j += n[i][0] * p * (1.0 - p); } varM[j][j] = varM_j; } for (j = 0; j < numClasses; j++) { for (k = 0; k < numClasses; k++) { if (j != k) { double covM_jk = 0.0; for (i = 0; i < numClasses; i++) covM_jk -= n[i][0] * (*probabilities)[i][j] * (*probabilities)[j][k]; varM[j][k] = covM_jk; } } } Matrix varN = Q * varM * Q.Transpose (); adjCounts.clear (); stdErrors.clear (); for (x = 0; x < numClasses; x++) { adjCounts.push_back (n[x][0]); stdErrors.push_back (sqrt (varN[x][x])); } return; } /* PerformAdjustmnts */