FunctionEvaluator.cpp 2.61 KB
Newer Older
adelmann's avatar
adelmann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#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
28
    for(auto namedObjective : objectives_) {
adelmann's avatar
adelmann committed
29

30
        Expressions::Expr_t *objective = namedObjective.second;
adelmann's avatar
adelmann committed
31 32 33 34 35 36 37 38
        Expressions::Result_t result = objective->evaluate(params_);

        std::vector<double> values;
        values.push_back(boost::get<0>(result));
        bool is_valid = boost::get<1>(result);

        reqVarInfo_t tmps = {EVALUATE, values, is_valid};
        requestedVars_.insert(
39
                std::pair<std::string, reqVarInfo_t>(namedObjective.first, tmps));
adelmann's avatar
adelmann committed
40 41 42 43

    }

    // .. and constraints
44
    for(auto namedConstraint : constraints_) {
adelmann's avatar
adelmann committed
45

46
        Expressions::Expr_t *constraint = namedConstraint.second;
adelmann's avatar
adelmann committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        Expressions::Result_t result = constraint->evaluate(params_);

        std::vector<double> 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<std::string> 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<Expressions::Expr_t> lhs(
            new Expressions::Expr_t(lhs_constr_str, funcs));
        boost::scoped_ptr<Expressions::Expr_t> 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(
76
                std::pair<std::string, reqVarInfo_t>(namedConstraint.first, tmps));
adelmann's avatar
adelmann committed
77 78 79

    }
}