Skip to content

Commit

Permalink
initial attempt and chance schedule file
Browse files Browse the repository at this point in the history
  • Loading branch information
jtwhite79 committed Mar 25, 2024
1 parent 8ccd2f4 commit 13f169b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
83 changes: 82 additions & 1 deletion src/libs/pestpp_common/constraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2272,11 +2272,92 @@ bool Constraints::should_update_chance(int iter)
else
return true;
}
else if ((iter) % pest_scenario.get_pestpp_options().get_opt_recalc_fosm_every() == 0)
//else if ((iter) % pest_scenario.get_pestpp_options().get_opt_recalc_fosm_every() == 0)
else if (chance_schedule[iter])
return true;
return false;
}

void Constraints::initialize_chance_schedule(ofstream& frec)
{
stringstream ss;
chance_schedule.clear();

string fname = pest_scenario.get_pestpp_options().get_opt_chance_schedule();
string line;
vector<string> tokens;
int lcount = 0, gen;
bool should_eval = false;
int recalc_every = pest_scenario.get_pestpp_options().get_opt_recalc_fosm_every();
for (int i=0;i<max(1,pest_scenario.get_control_info().noptmax+1);i++)
if (i%recalc_every == 0)
chance_schedule[i] = true;
else
chance_schedule[i] = false;
chance_schedule[0] = true;
if (fname.size() > 0)
{
ss.str("");
ss << "...reading population schedule from file '" << fname << "' " << endl;
frec << ss.str();
cout << ss.str();
ifstream in(fname);
if (in.bad())
{
throw runtime_error("error opening opt_chance_schedule file '"+fname+"'");
}
while (getline(in,line))
{
lcount++;
tokens.clear();
pest_utils::tokenize(line,tokens,"\t ,");
if (tokens.size() < 2)
{
ss.str("");
ss << "opt_chance_schedule file '" << fname << "' line " << lcount << " needs at least two entries";
throw runtime_error(ss.str());
}
try
{
gen = stoi(tokens[0]);
}
catch (...)
{
ss.str("");
ss << "error casting '" << tokens[0] << "' to generation integer on line " << lcount << " in opt_chance_schedule file";
throw runtime_error(ss.str());
}

try
{
should_eval = pest_utils::parse_string_arg_to_bool(tokens[1]);
}
catch (...)
{
ss.str("");
ss << "error casting '" << tokens[1] << "' to bool on line " << lcount << " in opt_chance_schedule file";
throw runtime_error(ss.str());
}
chance_schedule[gen] = should_eval;
}
in.close();
}

if (!chance_schedule[0])
{
ss.str("");
ss << "...chances must be evaluated during the first generation, resetting chance_schedule" << endl;
frec << ss.str();
cout << ss.str();
chance_schedule[0] = true;
}

frec << "...chance schedule: generation,should_eval_chances:" << endl;
for (int i=0;i<pest_scenario.get_control_info().noptmax;i++)
{
frec << "... " << i << ", " << chance_schedule.at(i) << endl;
}
}

void Constraints::postsolve_obs_constraints_report(Observations& old_obs, Observations& new_obs, string tag, int iter,
map<string,string> status_map, map<string,double> price_map)
Expand Down
5 changes: 5 additions & 0 deletions src/libs/pestpp_common/constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class Constraints
double risk;
double probit_val;
double dbl_max;
map<int,bool> chance_schedule;


Covariance obscov;
Expand Down Expand Up @@ -255,5 +256,9 @@ class Constraints

pair<vector<string>,vector<string>> get_working_set(Parameters& par_and_dec_vars, Observations& constraints_sim, bool do_shift, double working_set_tol=0.1);
void augment_constraint_mat_with_pi(Mat& mat, vector<string>& pi_names);

void initialize_chance_schedule(ofstream& frec);


};
#endif
9 changes: 9 additions & 0 deletions src/libs/pestpp_common/pest_data_structs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,12 @@ bool PestppOptions::assign_mou_value_by_key(const string& key, const string& val
mou_population_schedule = org_value;
return true;
}
else if (key == "OPT_CHANCE_SCHEDULE")
{
opt_chance_schedule = org_value;
return true;
}

else if (key == "MOU_SIMPLEX_REFLECTIONS")
{
convert_ip(value, mou_simplex_reflections);
Expand Down Expand Up @@ -1707,6 +1713,8 @@ void PestppOptions::summary(ostream& os) const
os << "opt_iter_tol: " << opt_iter_tol << endl;
os << "opt_recalc_fosm_every: " << opt_recalc_fosm_every << endl;
os << "opt_chance_points: " << opt_chance_points << endl;
os << "opt_chance_schedule: " << opt_chance_schedule << endl;




Expand Down Expand Up @@ -1905,6 +1913,7 @@ void PestppOptions::set_defaults()
set_opt_par_stack("");
set_opt_obs_stack("");
set_opt_chance_points("SINGLE");
set_opt_chance_schedule("");


set_sqp_dv_en("");
Expand Down
3 changes: 3 additions & 0 deletions src/libs/pestpp_common/pest_data_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ class PestppOptions {
void set_opt_obs_stack(string _stack) { opt_obs_stack = _stack; }
string get_opt_chance_points() const { return opt_chance_points; }
void set_opt_chance_points(string chance_points) { opt_chance_points = chance_points; }
string get_opt_chance_schedule() const {return opt_chance_schedule;}
void set_opt_chance_schedule(string fname) {opt_chance_schedule = fname;}


string get_sqp_dv_en()const { return sqp_dv_en; }
Expand Down Expand Up @@ -726,6 +728,7 @@ class PestppOptions {
string opt_par_stack;
string opt_obs_stack;
string opt_chance_points;
string opt_chance_schedule;

string sqp_dv_en;
string sqp_obs_restart_en;
Expand Down

0 comments on commit 13f169b

Please sign in to comment.