kernel

template<typename T>
class kernel

Basis function.

This class represents the function defining the generic CGP node. To be constructed it accepts two functions having prototype T(const std::vector<T

>&) and std::string(const

std::vector<std::string>&) computing, respectively, the function value on generic inputs and the textual representation of the operation.

The intended use, for an example with T = double would then be something like:

inline double my_sum(const std::vector<double> &in)
{
    T retval(in[0]);
    for (auto i = 1u; i < in.size(); ++i) {
        retval += in[i];
    }
    return retval;
}

inline std::string print_my_sum(const std::vector<std::string> &in)
{
    std::string retval(in[0]);
    for (auto i = 1u; i < in.size(); ++i) {
        retval += "+" + in[i];
    }
    return "(" + retval + ")";
}

kernel<double> f(my_sum<double>, print_my_sum, "my_sum");

Template Parameters

T – The type of the function output (and inputs)

Public Functions

template<typename U, typename V>
inline kernel(U &&f, V &&pf, std::string name)

Constructor.

Constructs a kernel that can be used as kernel in a dCGP expression

Parameters
  • f[in] any callable with prototype T(const std::vector<T>&)

  • pf[in] any callable with prototype std::string(const std::vector<std::string>&)

  • name[in] string containing the function name (ex. “sum”)

inline T operator()(const std::vector<T> &in) const

Parenthesis operator.

Evaluates the kernel in the point in

Parameters

in[in] the evaluation point as an std::vector<T>

Returns

the function value

inline T operator()(const std::initializer_list<T> &in) const

Parenthesis operator.

Evaluates the kernel in the point in

Parameters

in[in] the evaluation point as an std::initializer_list<T>

Returns

the function value

inline std::string operator()(const std::vector<std::string> &in) const

Parenthesis operator.

Returns a symbolic representation of the operation made by \(f\)

Parameters

in[in] std::vector<std::string> with the symbolic names to be used

Returns

the string representation of the operation (ex. “ln(s1+s2)”)

inline const std::string &get_name() const

Kernel name.

Returns the Kernel name

Returns

the Kernel name

template<typename Archive>
inline void serialize(Archive &ar, unsigned)

Object serialization.

This method will save/load this into the archive ar.

Parameters

ar – target archive.

Throws

unspecified – any exception thrown by the serialization of the expression and of primitive types.

Friends

inline friend std::ostream &operator<<(std::ostream &os, const kernel<T> &d)

Overloaded stream operator.

Will stream the function name

Parameters
  • os[inout] target stream.

  • d[in] kernel argument.

Returns

reference to os.