TransformationModelInterpolated::TransformationModelInterpolated( const TransformationModel::DataPoints & data, const Param & params) { params_ = params; Param defaults; getDefaultParameters(defaults); params_.setDefaults(defaults); // need monotonically increasing x values (can't have the same value twice): map<DoubleReal, vector<DoubleReal> > mapping; for (TransformationModel::DataPoints::const_iterator it = data.begin(); it != data.end(); ++it) { mapping[it->first].push_back(it->second); } size_ = mapping.size(); x_.resize(size_); y_.resize(size_); size_t i = 0; for (map<DoubleReal, vector<DoubleReal> >::const_iterator it = mapping.begin(); it != mapping.end(); ++it, ++i) { x_[i] = it->first; // use average y value: y_[i] = accumulate(it->second.begin(), it->second.end(), 0.0) / it->second.size(); } String interpolation_type = params_.getValue("interpolation_type"); const gsl_interp_type * type; if (interpolation_type == "linear") type = gsl_interp_linear; else if (interpolation_type == "polynomial") type = gsl_interp_polynomial; else if (interpolation_type == "cspline") type = gsl_interp_cspline; else if (interpolation_type == "akima") type = gsl_interp_akima; else { throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "unknown/unsupported interpolation type '" + interpolation_type + "'"); } size_t min_size = type->min_size; if (size_ < min_size) { throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "'" + interpolation_type + "' interpolation model needs at least " + String(min_size) + " data points (with unique x values)"); } interp_ = gsl_interp_alloc(type, size_); acc_ = gsl_interp_accel_alloc(); double * x_start = &(x_[0]), * y_start = &(y_[0]); gsl_interp_init(interp_, x_start, y_start, size_); // linear model for extrapolation: TransformationModel::DataPoints lm_data(2); lm_data[0] = make_pair(x_[0], y_[0]); lm_data[1] = make_pair(x_[size_ - 1], y_[size_ - 1]); lm_ = new TransformationModelLinear(lm_data, Param()); }
TransformationModelLinear::TransformationModelLinear( const TransformationModel::DataPoints & data, const Param & params) { params_ = params; data_given_ = !data.empty(); if (!data_given_ && params.exists("slope") && (params.exists("intercept"))) { // don't estimate parameters, use given values slope_ = params.getValue("slope"); intercept_ = params.getValue("intercept"); } else // estimate parameters from data { Param defaults; getDefaultParameters(defaults); params_.setDefaults(defaults); symmetric_ = params_.getValue("symmetric_regression") == "true"; size_t size = data.size(); if (size == 0) // no data { throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "no data points for 'linear' model"); } else if (size == 1) // degenerate case, but we can still do something { slope_ = 1.0; intercept_ = data[0].second - data[0].first; } else // compute least-squares fit { vector<double> x(size), y(size); for (size_t i = 0; i < size; ++i) { if (symmetric_) { x[i] = data[i].second + data[i].first; y[i] = data[i].second - data[i].first; } else { x[i] = data[i].first; y[i] = data[i].second; } } double cov00, cov01, cov11, sumsq; // covariance values, sum of squares double * x_start = &(x[0]), * y_start = &(y[0]); gsl_fit_linear(x_start, 1, y_start, 1, size, &intercept_, &slope_, &cov00, &cov01, &cov11, &sumsq); if (symmetric_) // undo coordinate transformation: { slope_ = (1.0 + slope_) / (1.0 - slope_); intercept_ = intercept_ * 1.41421356237309504880; // 1.41... = sqrt(2) } } } }
TPINode::TPINode () { ParameterList params = getDefaultParameters(); curNumThreads_ = params.get<int>("Num Threads"); int verbose = params.get<int>("Verbose"); TEUCHOS_TEST_FOR_EXCEPTION( curNumThreads_ < 0, std::runtime_error, "TPINode: The \"Num Threads\" parameter must be a nonnegative integer."); if (verbose) { std::cout << "TPINode initializing with numThreads == " << curNumThreads_ << std::endl; } init (curNumThreads_); }
TransformationModelLinear::TransformationModelLinear(const TransformationModel::DataPoints& data, const Param& params) : TransformationModel(data, params) // initializes model { data_given_ = !data.empty(); if (!data_given_ && params.exists("slope") && params.exists("intercept")) { // don't estimate parameters, use given values slope_ = params.getValue("slope"); intercept_ = params.getValue("intercept"); } else // estimate parameters from data { Param defaults; getDefaultParameters(defaults); params_.setDefaults(defaults); symmetric_ = params_.getValue("symmetric_regression") == "true"; // weight the data (if weighting is specified) TransformationModel::DataPoints data_weighted = data; if ((params.exists("x_weight") && params.getValue("x_weight") != "") || (params.exists("y_weight") && params.getValue("y_weight") != "")) { weightData(data_weighted); } size_t size = data_weighted.size(); std::vector<Wm5::Vector2d> points; if (size == 0) // no data { throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "no data points for 'linear' model"); } else if (size == 1) // degenerate case, but we can still do something { slope_ = 1.0; intercept_ = data_weighted[0].second - data_weighted[0].first; } else // compute least-squares fit { for (size_t i = 0; i < size; ++i) { points.push_back(Wm5::Vector2d(data_weighted[i].first, data_weighted[i].second)); } if (!Wm5::HeightLineFit2<double>(static_cast<int>(size), &points.front(), slope_, intercept_)) { throw Exception::UnableToFit(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "TransformationModelLinear", "Unable to fit linear transformation to data points."); } } // update params params_.setValue("slope", slope_); params_.setValue("intercept", intercept_); } }
OpenMPNode::OpenMPNode (Teuchos::ParameterList &pl) : curNumThreads_ (-1), // Default: Let OpenMP pick the number of threads verbose_ (false) // Default: No verbose status output { Teuchos::ParameterList params = getDefaultParameters(); params.setParameters(pl); const int curNumThreads = params.get<int>("Num Threads"); int verboseInt = params.get<int>("Verbose"); bool verbose = (verboseInt != 0); if (verbose) { std::cout << "OpenMPNode initializing with \"Num Threads\" = " << curNumThreads << std::endl; } init (curNumThreads); curNumThreads_ = curNumThreads; // Now it's safe to set state. verbose_ = verbose; }
TransformationModelLowess::TransformationModelLowess( const TransformationModel::DataPoints& data_, const Param& params) : model_(0) { // parameter handling/checking: params_ = params; Param defaults; getDefaultParameters(defaults); params_.setDefaults(defaults); if (data_.size() < 2) { throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "'lowess' model requires more data"); } // TODO copy ... TransformationModel::DataPoints data(data_); // sort data std::sort(data.begin(), data.end(), cmpFirstDimension); vector<double> x(data.size()), y(data.size()), result(data.size()); double xmin_ = data[0].first; double xmax_ = xmin_; for (Size i = 0; i < data.size(); ++i) { x[i] = data[i].first; y[i] = data[i].second; if (x[i] < xmin_) { xmin_ = x[i]; } else if (x[i] > xmax_) { xmax_ = x[i]; } } double span = params_.getValue("span"); int nsteps = params_.getValue("num_iterations"); double delta = params_.getValue("delta"); if (delta < 0.0) { delta = (xmax_ - xmin_) * 0.01; // automatically determine delta } FastLowessSmoothing::lowess(x, y, span, nsteps, delta, result); TransformationModel::DataPoints data_out; for (Size i = 0; i < result.size(); ++i) { data_out.push_back( std::make_pair(x[i], result[i]) ); } // TODO thin out data here ? we may not need that many points here to interpolate ... it is enough if we store a few datapoints Param p; TransformationModelInterpolated::getDefaultParameters(p); /// p.setValue("interpolation_type", "cspline"); // linear interpolation between lowess pts /// p.setValue("extrapolation_type", "four-point-linear"); p.setValue("interpolation_type", params_.getValue("interpolation_type")); p.setValue("extrapolation_type", params_.getValue("extrapolation_type")); // create new interpolation model based on the lowess data model_ = new TransformationModelInterpolated(data_out, p); }
int main(int argc, char *argv[]) { vector<HeuristicChooseReversal *> hcr_seq; /* Set random seed */ unsigned long seed = mix(clock(), time(NULL), getpid()); /* Set random seed */ srand(seed); /* Exact Unitaty Parameters */ ofstream fout; /* Time results */ timestamp_t time_begin; timestamp_t time_end; time_begin = get_timestamp(); /* Log result file */ ostream *logOut = NULL; ostream *logTime = NULL; /* Check essential parameters */ // TODO: start to use parameter names instead of parameter sequence, ex: -p 1,2,4... if (argc < 7) { printf("Usage:\n"); printf("lwr_test_metaheuristic permutation number_of_iteration inversion_limit frame_limit is_signed cost_function {-r=[[...]]}.\n\n"); printf("Parameters description:\n"); printf(" 1. permutation: the input permutation.\n\n"); printf(" 2. number_of_iteration: number of iterations that will\n"); printf("be performed by the Metaheuristic.\n\n"); printf(" 3. inversion_limit: limit (upper bound) for the number of inversions\n"); printf("that will be selected at each iteration of the heuristic for building solutions.\n\n"); printf(" 4. frame_limit: percentege (in the interval (0,100]) of frames that \n"); printf("will be selected at each iteration of the Metaheuristic.\n\n"); printf(" 5. is_signed: 1 if the input permutation is signed, 0 otherwise.\n\n"); printf(" 6. cost_function: 1 for linear function, 2 for quadratic function and 3 logaritmic function.\n\n"); printf(" 7. -r=[[...]]: Optional parameter to give a seed solution as a input parameter\n"); return 0; } /* Seed result */ Result seed_result; /* Permutation parameters */ parameter perm_param; /* Exact Unitary parameters */ parameter exactP; /* Improve parameters */ parameter improveP; /* ChooseHeuristic parameters*/ parameter hcrP; /* list of solvers for choose reversals */ vector<Solver *> solver_hcr; string intervalMethod = "WINDOW"; string heuristicReversalsName = "BenefitLoss"; /* -------------------Read the parameters From terminal----------------- */ int p_read = 1; // Read the permutation's sequence vector<ll> perm_seq = read_seq(argv[p_read++]); int num_it; sscanf(argv[p_read++], "%d", &num_it); int inv_limit; sscanf(argv[p_read++], "%d", &inv_limit); double frame_limit; sscanf(argv[p_read++], "%lf", &frame_limit); int is_signed; sscanf(argv[p_read++], "%d", &is_signed); int cost_function; sscanf(argv[p_read++], "%d", &cost_function); /* Construct the permutation */ if (is_signed) { perm_param = getSignedParameters(); } else { perm_param = getUnsignedGraspParameters(); } Permutation perm = Permutation(perm_param, perm_seq); // makes a copy from the original input permutation and set the normal mode Permutation perm_orig = perm; // Look for a input solution Result input_seed_result; bool has_input_seed_result = false; if (p_read < argc) { string solution = string(argv[p_read++]); if (solution.substr(0, 2) == "-r") { // in this caseheuristicName is supposed to be like -r=[[...]] string str_result = solution.substr(3); // Alterado por Ulisses input_seed_result = Util::getResultFromStr(str_result.c_str(), cost_function); has_input_seed_result = true; Permutation perm_copy = perm; bool valid = Solver::validateResult(perm_copy, input_seed_result, cout); if (!valid) { cout<<"Invalid input solution."<<endl; exit(1); } } } /* -------------Check if the permutation is already sorted--------------- */ if (perm.isSorted(1, perm.size())) { Result::printResult(seed_result, cout); cout<<endl; return 0; } /* ----------------------- Set Default Parameters ----------------------- */ parameter default_parameters = getDefaultParameters(); /* Set the output for results logging */ if (p_read < argc) { logOut = new ofstream(argv[p_read++], std::ofstream::out|ofstream::app); } if (p_read < argc) { logTime = new ofstream(argv[p_read++], std::ofstream::out|ofstream::app); } /* Look for Parameters */ string type; parameter::iterator it; for (it = default_parameters.begin(); it != default_parameters.end(); it++) { pair<string, string> p = getParameterPair(it->F); string type = p.F; string name = p.S; double value = it->S; // get Num selected rev if (type == "HCR") { hcrP[name] = value; } else if (type == "IMP") { improveP[name] = value; } else if (type == "BUILD_SOLUTION") { exactP[name] = value; } p_read++; } /* Look for the unsigned mode */ improveP["UNSIGNED"] = !is_signed; /* Create log file */ //ofstream log_out("log_test_signed.txt",ios_base::app); /* ----------------------Set Parameters from terminal---------------------- */ hcrP["COST_FUNCTION"] = cost_function; improveP["COST_FUNCTION"] = cost_function; exactP["COST_FUNCTION"] = cost_function; improveP["NUM_ROUND"] = num_it; exactP["NUM_SELECTED_REV"] = inv_limit; if (frame_limit <= 0 || frame_limit > 100) { cerr<<"Error: frame_limit must be in the interval (0, 100]"<<endl; exit(1); } improveP["ROLETTE_PERCENT_DISCARD"] = (100.0 - frame_limit); /*---------------- Build the Choosen reversal Heuristic -------------------*/ hcr_seq = build_hcr_seq( intervalMethod, improveP, hcrP, exactP, heuristicReversalsName ); if (improveP["EXACT_UNITATY_LS"]) { solver_hcr.push_back(new ExactUnitary(exactP, hcr_seq)); } else if (improveP["GRASP_HEURISTIC_LS"]) { solver_hcr.push_back(new GRASPHeuristic(exactP, hcr_seq)); } /*---------------------- Build The seed solution -------------------------*/ if (has_input_seed_result) { seed_result = input_seed_result; } else if (is_signed){ // Alterado por Ulisses seed_result = sortByGRIMM(perm, true, cost_function); } else { seed_result = sortByGRIMMUnsigned(perm, NUM_IT_GRIMM_UNSIGNED, true, cost_function); } /*--------------------- Build Improvement Metaheuristic ------------------*/ ImproveReversals * ir = new ImproveReversals( solver_hcr, intervalMethod, improveP, logOut ); /*-------------------------- Try improve the result ----------------------*/ Result r_improved = tryImprove( seed_result, ir, perm_orig, cost_function ); /*----------------------- Validate result --------------------------------*/ bool valid = Solver::validateResult(perm_orig, r_improved, cout); Result seedR = seed_result; //seedR.reversals = seed_result; seedR = Solver::calcTotalCost(seedR, cost_function); /*------------------------- Print the result -----------------------------*/ if (valid) { Result::printResult(r_improved, cout); cout<<endl; } else { cout<<"ERROR"; return 1; } /*------------------------- Print time -----------------------------------*/ time_end = get_timestamp(); double time_sec = get_interval_time(time_begin, time_end); if (logTime != NULL) *(logTime)<<time_sec<<endl; /* Clean all resources */ for (size_t i = 0; i < solver_hcr.size(); i++) { delete solver_hcr[i]; } solver_hcr.clear(); if (logOut != NULL) delete logOut; delete ir; cleanResources(); return valid; }
PeakIntegrator::PeakIntegrator() : DefaultParamHandler("PeakIntegrator") { getDefaultParameters(defaults_); defaultsToParam_(); // write defaults into Param object param_ }
TransformationModelBSpline::TransformationModelBSpline( const TransformationModel::DataPoints & data, const Param & params) { params_ = params; Param defaults; getDefaultParameters(defaults); params_.setDefaults(defaults); if (data.size() < 4) // TODO: check number { throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "'b_spline' model needs at least four data points"); } Size num_breakpoints = params_.getValue("num_breakpoints"); String break_positions = params_.getValue("break_positions"); if ((break_positions != "uniform") && (break_positions != "quantiles")) { throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "parameter 'break_positions' for 'b_spline' model must be 'uniform' or 'quantiles'"); } size_ = data.size(); x_ = gsl_vector_alloc(size_); y_ = gsl_vector_alloc(size_); w_ = gsl_vector_alloc(size_); for (size_t i = 0; i < size_; ++i) { gsl_vector_set(x_, i, data[i].first); gsl_vector_set(y_, i, data[i].second); gsl_vector_set(w_, i, 1.0); // TODO: non-uniform weights } gsl_vector_minmax(x_, &xmin_, &xmax_); // set up cubic (k = 4) spline workspace: if (num_breakpoints < 2) { num_breakpoints = 2; LOG_WARN << "Warning: Increased parameter 'num_breakpoints' to 2 (minimum)." << endl; } else if (num_breakpoints > size_ - 2) { num_breakpoints = size_ - 2; LOG_WARN << "Warning: Decreased parameter 'num_breakpoints' to " + String(num_breakpoints) + " (maximum for this number of data points)." << endl; } workspace_ = gsl_bspline_alloc(4, num_breakpoints); if (break_positions == "uniform") { gsl_bspline_knots_uniform(xmin_, xmax_, workspace_); } else { vector<double> quantiles(num_breakpoints, 1.0); double step = 1.0 / (num_breakpoints - 1); for (Size i = 0; i < num_breakpoints - 1; ++i) { quantiles[i] = i * step; } gsl_vector * breakpoints; breakpoints = gsl_vector_alloc(num_breakpoints); getQuantiles_(x_, quantiles, breakpoints); gsl_bspline_knots(breakpoints, workspace_); gsl_vector_free(breakpoints); } ncoeffs_ = gsl_bspline_ncoeffs(workspace_); gsl_vector_minmax(workspace_->knots, &xmin_, &xmax_); computeFit_(); }