37 #include <OpenMS/config.h>
66 RANSACParam(
size_t p_n,
size_t p_k,
double p_t,
size_t p_d,
bool p_relative_d =
false,
int (*p_rng)(
int) =
nullptr)
71 if (
d >= 100)
throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
String(
"RANSAC: Relative 'd' >= 100% given. Use a lower value; the more outliers you expect, the lower it should be."));
78 r <<
"RANSAC param:\n n: " <<
n <<
"\n k: " <<
k <<
" iterations\n t: " <<
t <<
" threshold\n d: " <<
d <<
" inliers\n\n";
95 template<
typename TModelType = RansacModelLinear>
101 static std::vector<std::pair<double, double> >
ransac(
102 const std::vector<std::pair<double, double> >& pairs,
139 static std::vector<std::pair<double, double> >
ransac(
140 const std::vector<std::pair<double, double> >& pairs,
145 bool relative_d =
false,
146 int (*rng)(
int) =
nullptr)
151 if (d >= 100)
throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
String(
"RANSAC: Relative 'd' >= 100% given. Use a lower value; the more outliers you expect, the lower it should be."));
152 d = pairs.size() * d / 100;
157 if (pairs.size() <= n)
160 String(
"RANSAC: Number of total data points (") +
String(pairs.size()) +
") must be larger than number of initial points (n=" +
String(n) +
").");
165 std::vector< std::pair<double, double> > alsoinliers, betterdata, bestdata;
166 std::vector<std::pair<double, double> > pairs_shuffled = pairs;
167 double besterror = std::numeric_limits<double>::max();
168 typename TModelType::ModelParameters coeff;
170 std::pair<double, double > bestcoeff;
171 double betterrsq = 0;
175 for (
size_t ransac_int=0; ransac_int<
k; ransac_int++)
178 if (bestdata.size() == pairs.size())
break;
182 std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end(), rng);
184 std::random_shuffle(pairs_shuffled.begin(), pairs_shuffled.end());
190 coeff = model.rm_fit(pairs_shuffled.begin(), pairs_shuffled.begin()+n);
197 alsoinliers = model.rm_inliers(pairs_shuffled.begin()+n, pairs_shuffled.end(), coeff, t);
199 if (alsoinliers.size() > d
200 || alsoinliers.size() >= (pairs_shuffled.size()-n))
203 std::copy( pairs_shuffled.begin(), pairs_shuffled.begin()+n, back_inserter(betterdata) );
204 betterdata.insert( betterdata.end(), alsoinliers.begin(), alsoinliers.end() );
205 typename TModelType::ModelParameters bettercoeff = model.rm_fit(betterdata.begin(), betterdata.end());
206 double bettererror = model.rm_rss(betterdata.begin(), betterdata.end(), bettercoeff);
208 betterrsq = model.rm_rsq(betterdata);
215 if (betterdata.size() > bestdata.size() || (betterdata.size() == bestdata.size() && (bettererror < besterror)))
217 besterror = bettererror;
218 bestdata = betterdata;
220 bestcoeff = bettercoeff;
222 std::cout <<
"RANSAC " << ransac_int <<
": Points: " << betterdata.size() <<
" RSQ: " << bestrsq <<
" Error: " << besterror <<
" c0: " << bestcoeff.first <<
" c1: " << bestcoeff.second << std::endl;
229 std::cout <<
"=======STARTPOINTS=======" << std::endl;
230 for (std::vector<std::pair<double, double> >::iterator it = bestdata.begin(); it != bestdata.end(); ++it)
232 std::cout << it->first <<
"\t" << it->second << std::endl;
234 std::cout <<
"=======ENDPOINTS=======" << std::endl;