void optimize(const EvalFunction& feval, bool reset = true) { this->_init(feval, FirstElem(), reset); acqui_optimizer_t inner_opt; while (this->_samples.size() == 0 || !this->_stop(*this, FirstElem())) { this->template update_pareto_model<EvalFunction::dim_in>(); this->update_pareto_data(); // copy in the ehvi structure to compute expected improvement std::deque<individual*> pop; for (auto x : this->pareto_data()) { individual* ind = new individual; ind->f[0] = std::get<1>(x)(0); ind->f[1] = std::get<1>(x)(1); ind->f[2] = 0; pop.push_back(ind); } auto acqui = acqui::Ehvi<Params, model_t>( this->_models, pop, Eigen::Vector3d(Params::bayes_opt_ehvi::x_ref(), Params::bayes_opt_ehvi::y_ref(), 0)); // maximize with inner opt using pair_t = std::pair<Eigen::VectorXd, double>; pair_t init(Eigen::VectorXd::Zero(1), -std::numeric_limits<float>::max()); auto body = [&](int i) -> pair_t { auto x = this->pareto_data()[i]; auto acqui_optimization = [&](const Eigen::VectorXd& x, bool g) { return opt::no_grad(acqui(x)); }; Eigen::VectorXd s = inner_opt(acqui_optimization, std::get<0>(x), true); double hv = acqui(s); return std::make_pair(s, hv); }; auto comp = [](const pair_t& v1, const pair_t& v2) { return v1.second > v2.second; }; auto m = tools::par::max(init, this->pareto_data().size(), body, comp); // take the best Eigen::VectorXd new_sample = m.first; // delete pop for (auto x : pop) delete x; // add sample this->add_new_sample(new_sample, feval(new_sample)); this->_update_stats(*this, FirstElem()); this->_current_iteration++; this->_total_iterations++; } }
void function_minimizer::pre_userfunction(void) { if (lapprox) { if (lapprox->hesstype==2) { //lapprox->num_separable_calls=0; lapprox->separable_calls_counter=0; } } userfunction(); if (lapprox) { if (lapprox->hesstype==2) { lapprox->num_separable_calls=lapprox->separable_calls_counter; double tmp=0.0; int inner_opt_value=inner_opt(); if (lapprox->saddlepointflag==2) { if (inner_opt_value !=0 ) { for (int i = 1; i <= lapprox->num_separable_calls; i++) { tmp-=(*lapprox->separable_function_difference)(i); } value(*objective_function_value::pobjfun)=tmp; } } else { for (int i = 1; i <= lapprox->num_separable_calls; i++) { tmp+=(*lapprox->separable_function_difference)(i); } value(*objective_function_value::pobjfun)=tmp; } } } }