v3 working?

main
Brett 2023-07-19 21:21:16 -04:00
parent f1371a72b4
commit b09658f03f
25 changed files with 100104 additions and 33 deletions

View File

@ -402,3 +402,34 @@
4 1716 1689809894936926601 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468 4 1716 1689809894936926601 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
5 1749 1689809894970260831 CMakeFiles/parksnrec.dir/src/parks/renderer/engine.cpp.o 875432a17ebda434 5 1749 1689809894970260831 CMakeFiles/parksnrec.dir/src/parks/renderer/engine.cpp.o 875432a17ebda434
1749 1905 1689809895123598284 parksnrec fc63db74fd58366e 1749 1905 1689809895123598284 parksnrec fc63db74fd58366e
4 1753 1689813146683375615 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
4 1817 1689813146746709121 CMakeFiles/parksnrec.dir/src/parks/renderer/engine.cpp.o 875432a17ebda434
1817 1992 1689813146920042927 parksnrec fc63db74fd58366e
4 1288 1689813271026895716 CMakeFiles/parksnrec.dir/src/genetic/v3/functions_v3.cpp.o 2851fa30bacfc7d5
1289 1444 1689813271180229106 parksnrec fc63db74fd58366e
4 1456 1689813577963029145 CMakeFiles/parksnrec.dir/src/genetic/v3/functions_v3.cpp.o 2851fa30bacfc7d5
4 1809 1689813578316361251 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
5 1900 1689813578406360938 CMakeFiles/parksnrec.dir/src/parks/renderer/engine.cpp.o 875432a17ebda434
1900 2081 1689813578586360314 parksnrec fc63db74fd58366e
4 1451 1689813754448956246 CMakeFiles/parksnrec.dir/src/genetic/v3/functions_v3.cpp.o 2851fa30bacfc7d5
4 1820 1689813754815621140 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
4 1872 1689813754868954216 CMakeFiles/parksnrec.dir/src/parks/renderer/engine.cpp.o 875432a17ebda434
1872 2028 1689813755022286808 parksnrec fc63db74fd58366e
4 1724 1689813866795041840 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
4 1836 1689813866908374552 CMakeFiles/parksnrec.dir/src/parks/renderer/engine.cpp.o 875432a17ebda434
1836 1996 1689813867065040358 parksnrec fc63db74fd58366e
5 1290 1689813935121321989 CMakeFiles/parksnrec.dir/src/genetic/v3/functions_v3.cpp.o 2851fa30bacfc7d5
1290 1453 1689813935281321057 parksnrec fc63db74fd58366e
4 1461 1689814929357575706 CMakeFiles/parksnrec.dir/src/genetic/v3/functions_v3.cpp.o 2851fa30bacfc7d5
4 1774 1689814929670906592 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
4 1888 1689814929787572347 CMakeFiles/parksnrec.dir/src/parks/renderer/engine.cpp.o 875432a17ebda434
1889 2047 1689814929944237789 parksnrec fc63db74fd58366e
4 1737 1689814943447465555 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
5 1804 1689814943514131701 CMakeFiles/parksnrec.dir/src/parks/renderer/engine.cpp.o 875432a17ebda434
1804 1966 1689814943674130449 parksnrec fc63db74fd58366e
4 1626 1689815043493346674 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
1626 1784 1689815043650012107 parksnrec fc63db74fd58366e
4 1637 1689815127412683671 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
1637 1793 1689815127566015790 parksnrec fc63db74fd58366e
4 1712 1689815769560520877 CMakeFiles/parksnrec.dir/src/genetic/v3/program_v3.cpp.o 7203e7cb37c07468
1713 1866 1689815769710517695 parksnrec fc63db74fd58366e

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,3 @@
Start testing: Jul 19 19:38 EDT Start testing: Jul 19 21:16 EDT
---------------------------------------------------------- ----------------------------------------------------------
End testing: Jul 19 19:38 EDT End testing: Jul 19 21:16 EDT

View File

@ -21,5 +21,5 @@ Collapsed=0
[Window][ImNodes] [Window][ImNodes]
Pos=269,23 Pos=269,23
Size=1157,957 Size=1157,957
Collapsed=0 Collapsed=1

Binary file not shown.

View File

@ -26,6 +26,8 @@ namespace parks::genetic {
constexpr unsigned char ARGS_FUNCS = 0b00100000; constexpr unsigned char ARGS_FUNCS = 0b00100000;
Color randScalar(OperatorArguments args, const ParameterSet& params);
Color randColor(OperatorArguments args, const ParameterSet& params);
Color add(OperatorArguments args, const ParameterSet& params); Color add(OperatorArguments args, const ParameterSet& params);
Color subtract(OperatorArguments args, const ParameterSet& params); Color subtract(OperatorArguments args, const ParameterSet& params);
Color multiply(OperatorArguments args, const ParameterSet& params); Color multiply(OperatorArguments args, const ParameterSet& params);
@ -43,7 +45,7 @@ namespace parks::genetic {
Color colorNoise(OperatorArguments args, const ParameterSet& params); Color colorNoise(OperatorArguments args, const ParameterSet& params);
enum class FunctionID { enum class FunctionID {
ADD, SUBTRACT, MULTIPLY, DIVIDE, MOD, ROUND, MIN, MAX, ABS, LOG, SIN, COS, ATAN, NOISE, COLOR_NOISE RAND_SCALAR, RAND_COLOR, ADD, SUBTRACT, MULTIPLY, DIVIDE, MOD, ROUND, MIN, MAX, ABS, LOG, SIN, COS, ATAN, NOISE, COLOR_NOISE
}; };
class Function { class Function {
@ -136,6 +138,8 @@ namespace parks::genetic {
}; };
inline FunctionStorage functions = { inline FunctionStorage functions = {
{FunctionID::RAND_SCALAR, new Function{"RS", parks::genetic::randScalar, 1, 0, ARGS_NONE}},
{FunctionID::RAND_COLOR, new Function{"RC", parks::genetic::randColor, 0, 1, ARGS_NONE}},
{FunctionID::ADD, new Function{"+", parks::genetic::add, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}}, {FunctionID::ADD, new Function{"+", parks::genetic::add, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::SUBTRACT, new Function{"-", parks::genetic::subtract, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}}, {FunctionID::SUBTRACT, new Function{"-", parks::genetic::subtract, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::MULTIPLY, new Function{"*", parks::genetic::multiply, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}}, {FunctionID::MULTIPLY, new Function{"*", parks::genetic::multiply, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},

View File

@ -35,17 +35,19 @@ namespace parks::genetic {
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
nodes[i] = nullptr; nodes[i] = nullptr;
//nodes[0] = new GeneticNode(FunctionID::ADD, 0, functions[FunctionID::ADD].generateRandomParameters());
generateRandomTree(); generateRandomTree();
} }
Color execute(double x, double y); Color execute(double x, double y);
static inline int left(int pos){ static inline int left(int pos){
return 2 * pos; return 2 * (pos + 1);
} }
static inline int right(int pos){ static inline int right(int pos){
return 2 * pos + 1; return 2 * (pos + 1) + 1;
} }
static inline int parent(int pos){ static inline int parent(int pos){

View File

@ -16,7 +16,7 @@ namespace parks::genetic {
} }
Color subtract(OperatorArguments args, const ParameterSet& params) { Color subtract(OperatorArguments args, const ParameterSet& params) {
return applyFunc(std::minus(), args.left, args.right); return applyFunc(std::minus(), args.left, args.right);;
} }
Color multiply(OperatorArguments args, const ParameterSet& params) { Color multiply(OperatorArguments args, const ParameterSet& params) {
@ -86,6 +86,14 @@ namespace parks::genetic {
return Color(r, g, b); return Color(r, g, b);
} }
Color randScalar(OperatorArguments args, const ParameterSet& params) {
return params[0];
}
Color randColor(OperatorArguments args, const ParameterSet& params) {
return params[0];
}
FunctionStorage::FunctionStorage( FunctionStorage::FunctionStorage(
std::initializer_list<std::pair<FunctionID, Function*>>&& init std::initializer_list<std::pair<FunctionID, Function*>>&& init
) { ) {

View File

@ -117,6 +117,7 @@ namespace parks::genetic {
void GeneticTree::generateRandomTree() { void GeneticTree::generateRandomTree() {
std::queue<int> nodesToProcess; std::queue<int> nodesToProcess;
std::queue<int> nonFuncNodesToProcess;
nodesToProcess.push(0); nodesToProcess.push(0);
while (!nodesToProcess.empty()){ while (!nodesToProcess.empty()){
int node = nodesToProcess.front(); int node = nodesToProcess.front();
@ -125,31 +126,56 @@ namespace parks::genetic {
if (node >= size) if (node >= size)
continue; continue;
// // end early by constructing a leaf
// if (chance(5)){
// continue;
// }
auto op = functions.select(); auto op = functions.select();
auto& func = functions[op]; auto& func = functions[op];
ParameterSet set = func.generateRandomParameters(); ParameterSet set = func.generateRandomParameters();
if (func.allowsArgument() && func.allowedFuncs()) { if (func.allowsArgument()) {
auto& queueToAddTo = func.allowedFuncs() ? nodesToProcess : nonFuncNodesToProcess;
if (func.singleArgument()) if (func.singleArgument())
nodesToProcess.push(left(node)); queueToAddTo.push(left(node));
else if (func.bothArgument()){ else if (func.bothArgument()){
nodesToProcess.push(left(node)); queueToAddTo.push(left(node));
nodesToProcess.push(right(node)); queueToAddTo.push(right(node));
} else if (func.dontCareArgument()) { } else if (func.dontCareArgument()) {
if (chance(80)) if (chance(80))
nodesToProcess.push(left(node)); queueToAddTo.push(left(node));
if (chance(80)) if (chance(80))
nodesToProcess.push(right(node)); queueToAddTo.push(right(node));
} }
} }
nodes[node] = new GeneticNode(op, node, set); nodes[node] = new GeneticNode(op, node, set);
} }
while (!nonFuncNodesToProcess.empty()){
int node = nonFuncNodesToProcess.front();
nonFuncNodesToProcess.pop();
if (node >= size)
continue;
int parent = GeneticTree::parent(node);
auto parentNode = this->node(parent);
// TODO: throw error?!?
if (parentNode == nullptr)
continue;
auto& parentFunc = functions[parentNode->op];
std::vector<FunctionID> allowedFuncs;
if (parentFunc.allowedColors())
allowedFuncs.push_back(FunctionID::RAND_COLOR);
if (parentFunc.allowedScalars())
allowedFuncs.push_back(FunctionID::RAND_SCALAR);
if (allowedFuncs.empty())
continue;
auto func = allowedFuncs[randomInt(0, (int)allowedFuncs.size()-1)];
nodes[node] = new GeneticNode(func, node, functions[func].generateRandomParameters());
}
} }
int GeneticTree::height(int node) { int GeneticTree::height(int node) {
@ -170,6 +196,9 @@ namespace parks::genetic {
auto ourNode = nodes[node]; auto ourNode = nodes[node];
auto& func = functions[ourNode->op]; auto& func = functions[ourNode->op];
if (func.disallowsArgument())
return func.call({ARGS_NONE, leftC, rightC}, ourNode->set);
// functions should always take precedence // functions should always take precedence
if (func.allowedFuncs()) { if (func.allowedFuncs()) {
int l = left(node); int l = left(node);
@ -180,27 +209,23 @@ namespace parks::genetic {
if (lNode != nullptr) if (lNode != nullptr)
leftC = execute_internal(x, y, l); leftC = execute_internal(x, y, l);
else
if (func.allowedVariables())
leftC = Color(x);
if (rNode != nullptr) if (rNode != nullptr)
rightC = execute_internal(x, y, r); rightC = execute_internal(x, y, r);
else
if (func.allowedVariables())
rightC = Color(y);
} else { } else {
std::vector<Color> lefts;
std::vector<Color> rights;
if (func.allowedColors()) {
lefts.push_back(RandomColor::get());
rights.push_back(RandomColor::get());
}
if (func.allowedScalars()) {
lefts.push_back(RandomScalar::get());
rights.push_back(RandomScalar::get());
}
if (func.allowedVariables()) { if (func.allowedVariables()) {
lefts.emplace_back(x); leftC = Color(x);
rights.emplace_back(y); rightC = Color(y);
} else {
BLT_WARN("Function called (%s) from node (%d) without any args!", func.name.c_str(), node);
} }
leftC = lefts[randomInt(0, (int)lefts.size()-1)];
rightC = rights[randomInt(0, (int)rights.size()-1)];
} }
return func.call({ARGS_SINGLE, leftC, rightC}, ourNode->set); return func.call({ARGS_BOTH, leftC, rightC}, ourNode->set);
} }
} }