29 #include "gambit/ScannerBit/priors_rollcall.hpp" 59 std::vector<std::string>
expand_dots(
const std::vector<std::string> ¶m_names_in)
61 std::vector<std::string> param_names = param_names_in;
63 for (
int i = 0, end = param_names.size(); i < end; i++)
65 if (param_names[i].find(
"...") != std::string::npos)
67 auto p_it = param_names.begin() + i;
68 std::string::size_type pos = p_it->find(
"...");
69 std::string prefix = p_it->substr(0, pos) +
"_";
70 std::stringstream ss(p_it->substr(pos+3));
72 if (
bool(ss >> N) && N >0)
74 p_it = param_names.erase(p_it);
75 std::vector<std::string> temps;
77 for (
int j = 0; j <
N; j++)
81 temps.push_back(prefix + ss.str());
84 param_names.insert(p_it, temps.begin(), temps.end());
101 std::map<std::string, std::string> sameMap;
102 std::unordered_map<std::string, std::pair<double, double>> sameMapOptions;
103 std::vector<BasePrior *> phantomPriors;
104 std::set<std::string> needSet;
107 std::vector <std::string> modelNames = model_options.
getNames();
108 std::sort(modelNames.begin(), modelNames.end());
111 for (
auto mod_it = modelNames.begin(), mod_end = modelNames.end(); mod_it != mod_end; ++mod_it)
113 std::string &mod = *mod_it;
114 std::vector <std::string> parameterNames = model_options.
getNames(mod);
115 std::sort(parameterNames.begin(), parameterNames.end());
117 int default_N = 0, default_i = 0;
118 bool isDefault =
false;
119 std::string default_prefix;
120 std::string::size_type par_pos;
122 for (
auto par_it = parameterNames.begin(), par_end = parameterNames.end(); par_it != par_end; par_it++)
125 std::string par_name;
127 par_pos = par_it->find(
"...");
128 if (!isDefault && par_pos != std::string::npos)
130 std::stringstream ss(par_it->substr(par_pos + 3));
132 if (
bool(ss >> default_N) && default_N > 0)
135 default_prefix = par_it->substr(0, par_pos) +
"_";
142 std::stringstream ss;
144 par_name = default_prefix + ss.str();
151 param_names.push_back(mod + std::string(
"::") + par_name);
153 if (model_options.
getNode(mod, *par_it).IsSequence() || model_options.
getNode(mod, *par_it).IsScalar())
155 phantomPriors.push_back(
new FixedPrior(mod + std::string(
"::") + par_name, model_options.
getNode(mod, *par_it)));
161 if (par_options.
hasKey(
"same_as"))
163 std::string connectedName = par_options.
getValue<std::string>(
"same_as");
164 std::string::size_type pos = connectedName.rfind(
"::");
165 if (pos == std::string::npos)
167 connectedName += std::string(
"::") + par_name;
170 sameMap[mod + std::string(
"::") + par_name] = connectedName;
172 auto opt = std::pair<double, double>(1.0, 0.0);
173 if (par_options.
hasKey(
"scale"))
175 opt.first = par_options.
getValue<
double>(
"scale");
178 if (par_options.
hasKey(
"shift"))
180 opt.second = par_options.
getValue<
double>(
"shift");
183 sameMapOptions[mod + std::string(
"::") + par_name] = opt;
185 else if (par_options.
hasKey(
"fixed_value"))
187 phantomPriors.push_back(
new FixedPrior(mod + std::string(
"::") + par_name, par_options.
getNode(
"fixed_value")));
191 std::string joined_parname = mod + std::string(
"::") + par_name;
193 if (par_options.
hasKey(
"prior_type"))
196 std::string priortype = par_options.
getValue<std::string>(
"prior_type");
198 if(priortype ==
"same_as")
200 if (options.
hasKey(
"same_as"))
202 sameMap[joined_parname] = options.
getValue<std::string>(
"same_as");
204 auto opt = std::pair<double, double>(1.0, 0.0);
205 if (par_options.
hasKey(
"scale"))
207 opt.first = par_options.
getValue<
double>(
"scale");
210 if (par_options.
hasKey(
"shift"))
212 opt.second = par_options.
getValue<
double>(
"shift");
215 sameMapOptions[joined_parname] = opt;
219 scan_err <<
"Same_as prior for parameter \"" << mod <<
"\" in model \""<< par_name <<
"\" has no \"same_as\" entry." <<
scan_end;
226 scan_err <<
"Parameter '"<< par_name <<
"' of model '" << mod <<
"' is of type '"<<priortype
227 <<
"', but no entry for this type exists in the factory function map.\n" <<
prior_creators.print() <<
scan_end;
233 if (priortype ==
"fixed")
235 phantomPriors.push_back(new_prior);
245 else if (par_options.
hasKey(
"range"))
254 needSet.insert(joined_parname);
259 if (isDefault && default_N > 0)
261 if (default_N > default_i)
270 std::vector<std::string> priorNames = prior_options.
getNames();
271 std::sort(priorNames.begin(), priorNames.end());
274 for (
auto priorname_it = priorNames.begin(), priorname_end = priorNames.end(); priorname_it != priorname_end; priorname_it++)
276 std::string &priorname = *priorname_it;
277 if (prior_options.
hasKey(priorname,
"parameters") && prior_options.
hasKey(priorname,
"prior_type"))
279 auto params =
expand_dots(prior_options.
getValue<std::vector<std::string>>(priorname,
"parameters"));
281 for (
auto par_it = params.begin(), par_end = params.end(); par_it != par_end; par_it++)
283 if (paramSet.find(*par_it) == paramSet.end())
285 scan_err <<
"Parameter " << *par_it <<
" requested by " << priorname <<
" is either not defined by the inifile, is fixed, or is the \"same as\" another parameter." <<
scan_end;
289 auto find_it = needSet.find(*par_it);
290 if (find_it == needSet.end())
292 scan_err <<
"Parameter " << *par_it <<
" requested by prior '"<< priorname <<
"' is reserved by a different prior." <<
scan_end;
296 needSet.erase(find_it);
301 auto options = prior_options.
getOptions(priorname);
302 auto priortype = prior_options.
getValue<std::string>(priorname,
"prior_type");
306 scan_err <<
"Prior '"<< priorname <<
"' is of type '"<< priortype <<
"', but no entry for this type exists in the factory function map.\n" <<
prior_creators.print() <<
scan_end;
310 if (priortype ==
"fixed")
321 phantomPriors.push_back(
prior_creators.at(priortype)(params,options) );
323 else if (priortype ==
"same_as")
325 if (options.hasKey(
"same_as"))
327 std::string same_name = options.getValue<std::string>(
"same_as");
328 for (
auto par_it = params.begin(), par_end = params.end(); par_it != par_end; ++par_it)
334 sameMap[*par_it] = same_name;
339 scan_err <<
"Same_as prior \"" << priorname <<
"\" has no \"same_as\" entry." <<
scan_end;
366 scan_err <<
"\"parameters\" and \"prior_type\" need to be defined for prior \"" << priorname <<
"\"" <<
scan_end;
370 if (needSet.size() != 0)
372 scan_err <<
"Priors are not defined for the following parameters: [";
373 auto it = needSet.begin();
375 for (; it != needSet.end(); it++)
382 std::map<std::string, std::string> keyMap;
383 std::string
index, result;
385 for (
auto strMap = sameMap.begin(), strMap_end = sameMap.end(); strMap != strMap_end; ++strMap)
387 index = strMap->first;
388 result = strMap->second;
390 while (sameMap.find(result) != sameMap.end())
393 result = sameMap[
index];
395 if (result == strMap->first)
397 scan_err <<
"Parameter " << strMap->first <<
" is \"same as\" itself." <<
scan_end;
401 if (reps > sameMap.size())
403 scan_err <<
"Parameter's \"same as\"'s are loop in on each other." <<
scan_end;
409 if (keyMap.find(result) == keyMap.end())
410 keyMap[result] = strMap->first + std::string(
"+") + result;
412 keyMap[result] = strMap->first + std::string(
"+") + keyMap[result];
417 auto it = keyMap.find(*str_it);
418 if (it != keyMap.end())
420 *str_it = it->second;
424 for (
auto key_it = keyMap.begin(), key_end = keyMap.end(); key_it != key_end; key_it++)
426 if (paramSet.find(key_it->first) == paramSet.end())
428 scan_err <<
"same_as: " << key_it->first <<
" is not defined in inifile." <<
scan_end;
432 phantomPriors.push_back(
new MultiPriors(key_it->second, sameMapOptions));
439 param_size += (*subprior)->size();
449 std::map<std::string, std::string> sameMap;
450 std::unordered_map<std::string, std::pair<double, double>> sameMapOptions;
451 std::set<std::string> needSet(params_in.begin(), params_in.end());
452 std::set<std::string> paramSet(params_in.begin(), params_in.end());
453 std::vector<BasePrior *> phantomPriors;
455 auto priorNames = options_in.
getNames();
456 std::sort(priorNames.begin(), priorNames.end());
458 for (
auto priorname_it = priorNames.begin(), priorname_end = priorNames.end(); priorname_it != priorname_end; ++priorname_it)
460 std::string &priorname = *priorname_it;
461 if (options_in.
hasKey(priorname,
"parameters") && options_in.
hasKey(priorname,
"prior_type"))
465 auto params = Gambit::Scanner::get_yaml_vector<std::string>(options_in.
getNode(priorname,
"parameters"));
467 for (
auto par_it = params.begin(), par_end = params.end(); par_it != par_end; par_it++)
469 std::string &par = *par_it;
470 if (paramSet.find(par) == paramSet.end())
472 scan_err <<
"Parameter " << par <<
" requested by " << priorname
473 <<
" is either not defined by the inifile, is fixed, or is the \"same as\" another parameter." <<
scan_end;
477 auto find_it = needSet.find(par);
478 if (find_it == needSet.end())
480 scan_err <<
"Parameter " << par <<
" requested by prior '"<< priorname
481 <<
"' is reserved by a different prior." <<
scan_end;
485 needSet.erase(find_it);
490 auto options = options_in.
getOptions(priorname);
491 auto priortype = options_in.
getValue<std::string>(priorname,
"prior_type");
495 scan_err <<
"Prior '"<< priorname <<
"' is of type '"<< priortype
496 <<
"', but no entry for this type exists in the factory function map.\n" <<
prior_creators.print() <<
scan_end;
500 if (priortype ==
"fixed")
510 phantomPriors.push_back(
prior_creators.at(priortype)(params,options) );
512 else if (priortype ==
"same_as")
514 if (options.hasKey(
"same_as"))
516 std::string same_name = options.getValue<std::string>(
"same_as");
517 for (
auto par_it = params.begin(), par_end = params.end(); par_it != par_end; par_it++)
523 sameMap[*par_it] = same_name;
528 scan_err <<
"Same_as prior \"" << priorname <<
"\" has no \"same_as\" entry." <<
scan_end;
545 scan_err <<
"\"parameters\" and \"prior_type\" need to be defined for prior \"" << priorname <<
"\"" <<
scan_end;
549 if (needSet.size() != 0)
551 scan_err <<
"Priors are not defined for the following parameters: [";
552 auto it = needSet.begin();
554 for (; it != needSet.end(); it++)
561 std::map<std::string, std::string> keyMap;
562 std::string
index, result;
564 for (
auto strMap_it = sameMap.begin(), strMap_end = sameMap.end(); strMap_it != strMap_end; strMap_it++)
566 index = strMap_it->first;
567 result = strMap_it->second;
569 while (sameMap.find(result) != sameMap.end())
572 result = sameMap[
index];
574 if (result == strMap_it->first)
576 scan_err <<
"Parameter " << strMap_it->first <<
" is \"same as\" itself." <<
scan_end;
580 if (reps > sameMap.size())
582 scan_err <<
"Parameter's \"same as\"'s are loop in on each other." <<
scan_end;
588 if (keyMap.find(result) == keyMap.end())
589 keyMap[result] = strMap_it->first + std::string(
"+") + result;
591 keyMap[result] = strMap_it->first + std::string(
"+") + keyMap[result];
596 auto it = keyMap.find(*str_it);
597 if (it != keyMap.end())
599 *str_it = it->second;
603 for (
auto key_it = keyMap.begin(), key_end = keyMap.end(); key_it != key_end; key_it++)
605 if (paramSet.find(key_it->first) == paramSet.end())
607 scan_err <<
"same_as: " << key_it->first <<
" is not defined in inifile." <<
scan_end;
611 phantomPriors.push_back(
new MultiPriors(key_it->second, sameMapOptions));
618 param_size += (*subprior)->size();
Abstract base class for priors.
std::vector< std::string > shown_param_names
void setSize(const unsigned int size)
std::vector< std::string > param_names
const std::vector< str > getNames(const args &... keys) const
Retrieve values from key-value pairs in options node.
reg_elem< create_prior_function > prior_creators
CompositePrior(const Options &model_options, const Options &prior_options)
Special "build-a-prior" classi.
Declarations for the YAML options class.
const Options getOptions(const args &... keys) const
Recursive options retrieval.
bool hasKey(const args &... keys) const
Getters for key/value pairs (which is all the options node should contain)
TYPE getValue(const args &... keys) const
#define scan_err
Defined to macros to output errors in the form: scan_err << "error" << scan_end; scan_warn << "warnin...
Utility Functions for the Gambit Scanner.
std::vector< BasePrior * > my_subpriors
A parameter that is fixed to a different parameter.
Template class for 1d priors which need only a "range" option in their constructor.
virtual std::vector< std::string > getShownParameters() const
std::vector< std::string > expand_dots(const std::vector< std::string > ¶m_names_in)
TODO: see if we can use this one:
A small wrapper object for 'options' nodes.
YAML::Node getNode(const args &... keys) const
Retrieve raw YAML node.