#include "Simulation/FunctionEvaluator.h" #include "boost/algorithm/string.hpp" #include "boost/variant.hpp" #include "boost/smart_ptr.hpp" FunctionEvaluator::FunctionEvaluator(Expressions::Named_t objectives, Expressions::Named_t constraints, Param_t params, std::string name, MPI_Comm comm, CmdArguments_t args) : Simulation(args) , objectives_(objectives) , constraints_(constraints) , params_(params) , comm_(comm) {} FunctionEvaluator::~FunctionEvaluator() { requestedVars_.clear(); } void FunctionEvaluator::collectResults() { requestedVars_.clear(); // evaluate all objectives for(auto namedObjective : objectives_) { Expressions::Expr_t *objective = namedObjective.second; Expressions::Result_t result = objective->evaluate(params_); std::vector values; values.push_back(boost::get<0>(result)); bool is_valid = boost::get<1>(result); reqVarInfo_t tmps = {EVALUATE, values, is_valid}; requestedVars_.insert( std::pair(namedObjective.first, tmps)); } // .. and constraints for(auto namedConstraint : constraints_) { Expressions::Expr_t *constraint = namedConstraint.second; Expressions::Result_t result = constraint->evaluate(params_); std::vector values; values.push_back(boost::get<0>(result)); bool is_valid = boost::get<1>(result); //FIXME: hack to evaluate LHS and RHS std::string constr_str = constraint->toString(); std::vector split; boost::split(split, constr_str, boost::is_any_of("<>!="), boost::token_compress_on); std::string lhs_constr_str = split[0]; std::string rhs_constr_str = split[1]; boost::trim_left_if(rhs_constr_str, boost::is_any_of("=")); functionDictionary_t funcs = constraint->getRegFuncs(); boost::scoped_ptr lhs( new Expressions::Expr_t(lhs_constr_str, funcs)); boost::scoped_ptr rhs( new Expressions::Expr_t(rhs_constr_str, funcs)); Expressions::Result_t lhs_res = lhs->evaluate(params_); Expressions::Result_t rhs_res = rhs->evaluate(params_); values.push_back(boost::get<0>(lhs_res)); values.push_back(boost::get<0>(rhs_res)); reqVarInfo_t tmps = {EVALUATE, values, is_valid}; requestedVars_.insert( std::pair(namedConstraint.first, tmps)); } }