📄 gplib_func.cpp
字号:
// *****************// GPLIB_func.cpp// Available Functions// Colin Frayn// December 2006// *****************
// GPLib v2.0, A Genetic programming Library for C++
// Copyright (C) 2006 Colin Frayn
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "GPLib.h"// ---== Functions defined by the user for use in this algorithm ==---// Standard Binary Multiply functionclass GP_Mult : public GPLIB_GPFunction { private : public : GP_Mult() { SetWeight(3); SetNP(2); SetName("*"); } float FunctionBody(int pos) { return RunSubFunction(pos,1) * RunSubFunction(pos,2); } int EditCheck(GPLIB_EntityParent *ent, int pos) { int loc1,loc2; loc1 = ent->GetBranchByPos(pos,1); if ((ent->genome[loc1].func == GPLIB_ILeaf || ent->genome[loc1].func == GPLIB_FLeaf) && ent->genome[loc1].value == 0.0f) return GPLIB_SNIP; loc2 = ent->GetBranchByPos(pos,2); if ((ent->genome[loc2].func == GPLIB_ILeaf || ent->genome[loc2].func == GPLIB_FLeaf) && ent->genome[loc2].value == 0.0f) return GPLIB_SNIP; if ((ent->genome[loc1].func == GPLIB_ILeaf || ent->genome[loc1].func == GPLIB_FLeaf) && (ent->genome[loc2].func == GPLIB_ILeaf || ent->genome[loc2].func == GPLIB_FLeaf)) return GPLIB_SIMPLIFY; return GPLIB_NOP; }};// Standard Binary Addition functionclass GP_Add : public GPLIB_GPFunction { private : public : GP_Add() { SetWeight(3); SetNP(2); SetName("+"); } float FunctionBody(int pos) { return RunSubFunction(pos,1) + RunSubFunction(pos,2); } int EditCheck(GPLIB_EntityParent *ent, int pos) { int loc1,loc2; loc1 = ent->GetBranchByPos(pos,1); if ((ent->genome[loc1].func == GPLIB_ILeaf || ent->genome[loc1].func == GPLIB_FLeaf) && ent->genome[loc1].value == 0.0f) return GPLIB_SECOND; loc2 = ent->GetBranchByPos(pos,2); if ((ent->genome[loc2].func == GPLIB_ILeaf || ent->genome[loc2].func == GPLIB_FLeaf) && ent->genome[loc2].value == 0.0f) return GPLIB_FIRST; if ((ent->genome[loc1].func == GPLIB_ILeaf || ent->genome[loc1].func == GPLIB_FLeaf) && (ent->genome[loc2].func == GPLIB_ILeaf || ent->genome[loc2].func == GPLIB_FLeaf)) return GPLIB_SIMPLIFY; return GPLIB_NOP; }};// Standard Binary subtract functionclass GP_Sub : public GPLIB_GPFunction { private : public : GP_Sub() { SetWeight(3); SetNP(2); SetName("-"); } float FunctionBody(int pos) { return RunSubFunction(pos,1) - RunSubFunction(pos,2); } int EditCheck(GPLIB_EntityParent *ent, int pos) { int loc1,loc2; loc2 = ent->GetBranchByPos(pos,2); if ((ent->genome[loc2].func == GPLIB_ILeaf || ent->genome[loc2].func == GPLIB_FLeaf) && ent->genome[loc2].value == 0.0f) return GPLIB_FIRST; loc1 = ent->GetBranchByPos(pos,1); if ((ent->genome[loc1].func == GPLIB_ILeaf || ent->genome[loc1].func == GPLIB_FLeaf) && (ent->genome[loc2].func == GPLIB_ILeaf || ent->genome[loc2].func == GPLIB_FLeaf)) return GPLIB_SIMPLIFY; return GPLIB_NOP; }};// Standard Binary Divide functionclass GP_Divide : public GPLIB_GPFunction { private : public : GP_Divide() { SetWeight(3); SetNP(2); SetName("/"); } float FunctionBody(int pos) { return RunSubFunction(pos,1) / RunSubFunction(pos,2); } int EditCheck(GPLIB_EntityParent *ent, int pos) { int loc1,loc2; loc1 = ent->GetBranchByPos(pos,1); if ((ent->genome[loc1].func == GPLIB_ILeaf || ent->genome[loc1].func == GPLIB_FLeaf) && ent->genome[loc1].value == 0.0f) return GPLIB_SNIP; loc2 = ent->GetBranchByPos(pos,2); if ((ent->genome[loc1].func == GPLIB_ILeaf || ent->genome[loc1].func == GPLIB_FLeaf) && (ent->genome[loc2].func == GPLIB_ILeaf || ent->genome[loc2].func == GPLIB_FLeaf)) return GPLIB_SIMPLIFY; return GPLIB_NOP; }};// Standard Binary Power functionclass GP_Power : public GPLIB_GPFunction { private : public : GP_Power() { SetWeight(2); SetNP(2); SetName("^"); } float FunctionBody(int pos) { return pow(RunSubFunction(pos,1),RunSubFunction(pos,2)); } int EditCheck(GPLIB_EntityParent *ent, int pos) { int loc1,loc2; loc1 = ent->GetBranchByPos(pos,1); if ((ent->genome[loc1].func == GPLIB_ILeaf || ent->genome[loc1].func == GPLIB_FLeaf) && ent->genome[loc1].value == 0.0f) return GPLIB_SNIP; loc2 = ent->GetBranchByPos(pos,2); if ((ent->genome[loc1].func == GPLIB_ILeaf || ent->genome[loc1].func == GPLIB_FLeaf) && (ent->genome[loc2].func == GPLIB_ILeaf || ent->genome[loc2].func == GPLIB_FLeaf)) return GPLIB_SIMPLIFY; return GPLIB_NOP; }};// Sine functionclass GP_Sine : public GPLIB_GPFunction { private : public : GP_Sine() { SetWeight(1); SetNP(1); SetName("sin"); } float FunctionBody(int pos) { return sin(RunSubFunction(pos,1)); } int EditCheck(GPLIB_EntityParent *ent, int pos) { int loc; loc = ent->GetBranchByPos(pos,1); if ((ent->genome[loc].func == GPLIB_ILeaf || ent->genome[loc].func == GPLIB_FLeaf)) return GPLIB_SIMPLIFY; return GPLIB_NOP; }};// Cosine functionclass GP_Cosine : public GPLIB_GPFunction { private : public : GP_Cosine() { SetWeight(1); SetNP(1); SetName("cos"); } float FunctionBody(int pos) { return cos(RunSubFunction(pos,1)); } int EditCheck(GPLIB_EntityParent *ent, int pos) { int loc; loc = ent->GetBranchByPos(pos,1); if ((ent->genome[loc].func == GPLIB_ILeaf || ent->genome[loc].func == GPLIB_FLeaf)) return GPLIB_SIMPLIFY; return GPLIB_NOP; }};// Exponential functionclass GP_Exp : public GPLIB_GPFunction { private : public : GP_Exp() { SetWeight(3); SetNP(1); SetName("exp"); } float FunctionBody(int pos) { return exp(RunSubFunction(pos,1)); } int EditCheck(GPLIB_EntityParent *ent, int pos) { int loc; loc = ent->GetBranchByPos(pos,1); if ((ent->genome[loc].func == GPLIB_ILeaf || ent->genome[loc].func == GPLIB_FLeaf)) return GPLIB_SIMPLIFY; return GPLIB_NOP; }};// Square root functionclass GP_Sqrt : public GPLIB_GPFunction { private : public : GP_Sqrt() { SetWeight(2); SetNP(1); SetName("sqrt"); } float FunctionBody(int pos) { return sqrt(RunSubFunction(pos,1)); } int EditCheck(GPLIB_EntityParent *ent, int pos) { int loc; loc = ent->GetBranchByPos(pos,1); if ((ent->genome[loc].func == GPLIB_ILeaf || ent->genome[loc].func == GPLIB_FLeaf)) return GPLIB_SIMPLIFY; return GPLIB_NOP; }};// Return 'pi'class GP_Pi : public GPLIB_GPFunction { private : public : GP_Pi() { SetWeight(1); SetNP(0); SetName("pi"); } float FunctionBody(int pos) { return GPLIB_PI; } };// ---== Set up the function list : add in new entries here ==--- // Add the function nodes to the master 'in use' listvoid GPLIB_Environment::SetupFunctionArray(void) { Functions.push_back(new GP_Mult); Functions.push_back(new GP_Add); Functions.push_back(new GP_Sub); Functions.push_back(new GP_Divide); Functions.push_back(new GP_Power); Functions.push_back(new GP_Exp);// Functions.push_back(new GP_Sqrt);// Functions.push_back(new GP_Sine);// Functions.push_back(new GP_Cosine);// Functions.push_back(new GP_Pi); // ************************************* // * Terminal nodes are included by default; // * You do not need to add them here. // *************************************}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -