예제 #1
0
int DifferentialEvolution::recombination(RunManagerAbstract &run_manager)
{
	ostream &os = file_manager.rec_ofstream();

	int best_run_idx = 0;
	ModelRun run_target(obj_func_ptr);
	ModelRun run_canidate(obj_func_ptr);
	Parameters tmp_pars_targ;
	Observations tmp_obs_targ;
	Parameters tmp_pars_can;
	Observations tmp_obs_can;

	best_phi = std::numeric_limits<double>::max();

	int d = gen_1.get_nruns();
	int n_good_runs_targ = gen_1.get_num_good_runs();
	int n_good_runs_can = run_manager.get_num_good_runs();
	double phi_sum_targ = 0.0;
	double phi_sum_can = 0.0;
	double phi_sum_new = 0.0;
	double phi_max_targ = 0.0;
	double phi_max_can = 0.0;
	double phi_max_new = 0.0;
	double phi_min_targ = std::numeric_limits<double>::max();
	double phi_min_can = std::numeric_limits<double>::max();
	double phi_min_new = std::numeric_limits<double>::max();

	os << "  population phi values:" << endl;
	os << "              parent         canidate" << endl;
	os << "    id        phi            phi" << endl;
	os << "    ----     ---------      ---------" << endl;
	for (int i_run = 0; i_run < d; ++i_run)
	{
		double new_phi = std::numeric_limits<double>::max();
		bool run_target_ok = gen_1.get_run(i_run, tmp_pars_targ, tmp_obs_targ);
		bool  run_canidate_ok = run_manager.get_run(i_run, tmp_pars_can, tmp_obs_can);
		if (!run_canidate_ok && !run_target_ok)
		{
			//keep current target
			new_phi = std::numeric_limits<double>::max();
			++failed_runs_old;
			++failed_runs_new;
			os << "    " << left << setw(10) << i_run << setw(15) << "N/A" << setw(15) << "N/A";
			os << endl;

		}
		else if (!run_canidate_ok)
		{
			//keep current target
			++failed_runs_new;
			par_transform.model2ctl_ip(tmp_pars_targ);
			// compute phi
			run_target.update_ctl(tmp_pars_targ, tmp_obs_targ);
			double phi_target = run_target.get_phi(DynamicRegularization::get_unit_reg_instance());
			phi_sum_targ += phi_target;
			phi_sum_new += phi_target;
			phi_min_targ = min(phi_min_targ, phi_target);
			phi_max_targ = max(phi_max_targ, phi_target);
			os << "    " << left << setw(10) << i_run << setw(15) << phi_target << setw(15) << "N/A";
			os << endl;
		}
		else if (!run_target_ok)
		{
			gen_1.update_run(i_run, tmp_pars_can, tmp_obs_can);
			// compute phi
			par_transform.model2ctl_ip(tmp_pars_can);
			run_canidate.update_ctl(tmp_pars_can, tmp_obs_can);
			new_phi = run_canidate.get_phi(DynamicRegularization::get_unit_reg_instance());
			++failed_runs_old;
			phi_sum_can += new_phi;
			phi_sum_new += new_phi;
			phi_min_can = min(phi_min_targ, new_phi);
			phi_max_can = max(phi_max_targ, new_phi);
			os << "    " << left << setw(10) << i_run << setw(15) << "N/A" << setw(15) << new_phi;
			os << endl;
		}
		else
		{
			// process target parameters and observations
			par_transform.model2ctl_ip(tmp_pars_targ);
			run_target.update_ctl(tmp_pars_targ, tmp_obs_targ);
			double phi_target = run_target.get_phi(DynamicRegularization::get_unit_reg_instance());
			//process canidate parameters and observations
			par_transform.model2ctl_ip(tmp_pars_can);
			run_canidate.update_ctl(tmp_pars_can, tmp_obs_can);
			double phi_canidate = run_canidate.get_phi(DynamicRegularization::get_unit_reg_instance());
			new_phi = min(phi_target, phi_canidate);
			os << "    " << left << setw(10) << i_run;
			os << setw(15) << phi_target;
			os << setw(15) << phi_canidate;
			os << endl;
			if (phi_canidate < phi_target)
			{
				gen_1.update_run(i_run, tmp_pars_can, tmp_obs_can);
			}
			phi_sum_targ += phi_target;
			phi_sum_can += phi_canidate;
			phi_sum_new += new_phi;
			phi_min_can = min(phi_min_can, phi_canidate);
			phi_max_can = max(phi_max_can, phi_canidate);
			phi_min_targ = min(phi_min_targ, phi_target);
			phi_max_targ = max(phi_max_targ, phi_target);

		}
		phi_min_new = min(phi_min_new, new_phi);
		phi_max_new = max(phi_max_new, new_phi);
		if (new_phi < best_phi)
		{
			best_phi = new_phi;
			best_run_idx = i_run;
		}
	}
	double phi_avg_targ = std::numeric_limits<double>::max();
	double phi_avg_can = std::numeric_limits<double>::max();
	double phi_avg_new = std::numeric_limits<double>::max();
	int n_good_runs_new = gen_1.get_num_good_runs();
	if (n_good_runs_targ > 0)
	{
		phi_avg_targ = phi_sum_targ / n_good_runs_targ;
	}
	if (n_good_runs_can > 0)
	{
		phi_avg_can = phi_sum_can / n_good_runs_can;
	}
	if (n_good_runs_new > 0)
	{
		phi_avg_new = phi_sum_new / n_good_runs_new;
	}

	write_run_summary(cout, n_good_runs_targ, phi_avg_targ, phi_min_targ, phi_max_targ,
		n_good_runs_can, phi_avg_can, phi_min_can, phi_max_can,
		n_good_runs_new, phi_avg_new, phi_min_new, phi_max_new);
	cout << endl;
	os << endl;
	write_run_summary(os, n_good_runs_targ, phi_avg_targ, phi_min_targ, phi_max_targ,
		n_good_runs_can, phi_avg_can, phi_min_can, phi_max_can,
		n_good_runs_new, phi_avg_new, phi_min_new, phi_max_new);
	os << endl;
	
	return best_run_idx;
}