-
Notifications
You must be signed in to change notification settings - Fork 1
/
GpFunction.h
94 lines (90 loc) · 3.77 KB
/
GpFunction.h
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// GpFunction.h
// LazyPredator
//
// Created by Craig Reynolds on 12/18/20.
// Copyright © 2020 Craig Reynolds. All rights reserved.
//
#pragma once
#include "Utilities.h"
#include "GpType.h"
class GpTree; // Forward reference to class defined later.
// Class to represent functions in "strongly typed genetic programming".
class GpFunction
{
public:
GpFunction(){}
GpFunction(const std::string& name,
const std::string& return_type_name,
const std::vector<std::string>& parameter_type_names,
std::function<std::any(GpTree& t)> eval)
: GpFunction(name, return_type_name, parameter_type_names, eval, 1) {}
GpFunction(const std::string& name,
const std::string& return_type_name,
const std::vector<std::string>& parameter_type_names,
std::function<std::any(GpTree& t)> eval,
float selection_weight)
: name_(name),
return_type_name_(return_type_name),
parameter_type_names_(parameter_type_names),
eval_(eval),
selection_weight_(selection_weight) {}
// String name of this GpFunction.
const std::string& name() const { return name_; }
// String name of this GpFunction's return type.
const std::string& returnTypeName() const { return return_type_name_; }
// Pointer to this GpFunction's return GpType.
GpType* returnType() const { return return_type_; }
// GpTypes for all parameters of this GpFunction.
const std::vector<GpType*>& parameterTypes() const
{ return parameter_types_; }
// String names for GpTypes of all parameters of this GpFunction.
const std::vector<std::string>& parameterTypeNames() const
{ return parameter_type_names_; }
// Set GpTypes for return type and all parameter types of this GpFunction.
void setReturnTypePointer(GpType* rtp) { return_type_ = rtp; }
void setParameterTypePointers(const std::vector<GpType*>& vec_ptype_ptrs)
{ parameter_types_ = vec_ptype_ptrs; }
// Does this function have parameter of its return type? (Can call itself?)
bool recursive() const
{
bool r = false;
for (auto& n : parameterTypeNames()) if(n == returnTypeName()) r = true;
return r;
}
// Minimum "size" required to terminate subtree with this function at root.
int minSizeToTerminate() const { return min_size_to_terminate_; }
void setMinSizeToTerminate(int s) { min_size_to_terminate_ = s; }
// Evaluate (execute) a GpTree with this function at root
// TODO probably should assert they match (this and tree root GpFunction)
std::any eval(GpTree& tree) const { return eval_(tree); }
// Called by FunctionSet init while linking types and functions.
void linkToReturnType()
{
returnType()->addFunctionReturningThisType(this, name());
}
// Print description of this GpFunction to std::cout.
void print() const
{
std::cout << "GpFunction: " << name() << ", return_type: ";
std::cout << returnTypeName() << ", parameters: (";
bool comma = false;
for (int i = 0; i < parameterTypes().size(); i ++)
{
if (comma) std::cout << ", "; else comma = true;
std::cout << parameterTypeNames().at(i);
}
std::cout << ")." << std::endl;
}
// Multiplier on random selection during initial tree construction.
float selectionWeight() const { return selection_weight_; }
private:
std::string name_;
std::string return_type_name_;
GpType* return_type_ = nullptr;
std::vector<std::string> parameter_type_names_;
std::vector<GpType*> parameter_types_;
int min_size_to_terminate_ = std::numeric_limits<int>::max();
std::function<std::any(GpTree& t)> eval_ = nullptr;
float selection_weight_ = 1;
};