Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli improvements #33

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/source/operon_gp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ auto main(int argc, char** argv) -> int
};

gp.Run(executor, random, report);
fmt::print("{}\n", Operon::InfixFormatter::Format(best.Genotype, problem.GetDataset(), 6));
fmt::print("{}\n", Operon::InfixFormatter::Format(best.Genotype, problem.GetDataset(), 8));
} catch (std::exception& e) {
fmt::print(stderr, "error: {}\n", e.what());
return EXIT_FAILURE;
Expand Down
88 changes: 63 additions & 25 deletions cli/source/operon_nsgp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#include "util.hpp"
#include "operator_factory.hpp"

void Scale(Operon::Individual& ind, Operon::Span<float const> estimated, Operon::Span<float const> target, Operon::Scalar& a, Operon::Scalar& b);


auto main(int argc, char** argv) -> int
{
auto opts = Operon::InitOptions("operon_gp", "Genetic programming symbolic regression");
Expand Down Expand Up @@ -300,23 +303,8 @@ auto main(int argc, char** argv) -> int
Operon::Scalar a{1.0};
Operon::Scalar b{0.0};
auto linearScaling = taskflow.emplace([&]() {
auto [a_, b_] = Operon::FitLeastSquares(estimatedTrain, targetTrain);
a = static_cast<Operon::Scalar>(a_);
b = static_cast<Operon::Scalar>(b_);
// add scaling terms to the tree
auto& nodes = best.Genotype.Nodes();
auto const sz = nodes.size();
if (std::abs(a - Operon::Scalar{1}) > std::numeric_limits<Operon::Scalar>::epsilon()) {
nodes.emplace_back(Operon::Node::Constant(a));
nodes.emplace_back(Operon::NodeType::Mul);
}
if (std::abs(b) > std::numeric_limits<Operon::Scalar>::epsilon()) {
nodes.emplace_back(Operon::Node::Constant(b));
nodes.emplace_back(Operon::NodeType::Add);
}
if (nodes.size() > sz) {
best.Genotype.UpdateNodes();
}
if (scale)
Scale(best, estimatedTrain, targetTrain, a, b);
});

double r2Train{};
Expand All @@ -327,13 +315,17 @@ auto main(int argc, char** argv) -> int
double maeTest{};

auto scaleTrain = taskflow.emplace([&]() {
Eigen::Map<Eigen::Array<Operon::Scalar, -1, 1>> estimated(estimatedTrain.data(), std::ssize(estimatedTrain));
estimated = estimated * a + b;
if (scale) {
Eigen::Map<Eigen::Array<Operon::Scalar, -1, 1>> estimated(estimatedTrain.data(), std::ssize(estimatedTrain));
estimated = estimated * a + b;
}
});

auto scaleTest = taskflow.emplace([&]() {
Eigen::Map<Eigen::Array<Operon::Scalar, -1, 1>> estimated(estimatedTest.data(), std::ssize(estimatedTest));
estimated = estimated * a + b;
if (scale) {
Eigen::Map<Eigen::Array<Operon::Scalar, -1, 1>> estimated(estimatedTest.data(), std::ssize(estimatedTest));
estimated = estimated * a + b;
}
});

auto calcStats = taskflow.emplace([&]() {
Expand All @@ -358,9 +350,13 @@ auto main(int argc, char** argv) -> int
auto calculateOffMemory = taskflow.transform_reduce(off.begin(), off.end(), totalMemory, std::plus{}, [&](auto const& ind) { return getSize(ind); });

// define task graph
linearScaling.succeed(evalTrain, evalTest);
linearScaling.precede(scaleTrain, scaleTest);
calcStats.succeed(scaleTrain, scaleTest);
//if (scale) {
linearScaling.succeed(evalTrain, evalTest);
linearScaling.precede(scaleTrain, scaleTest);
calcStats.succeed(scaleTrain, scaleTest);
//} else {
// calcStats.succeed(evalTrain, evalTest);
//}
calcStats.precede(calculateLength, calculateQuality, calculatePopMemory, calculateOffMemory);

executor.corun(taskflow);
Expand Down Expand Up @@ -394,11 +390,53 @@ auto main(int argc, char** argv) -> int
};

gp.Run(executor, random, report);
fmt::print("{}\n", Operon::InfixFormatter::Format(best.Genotype, problem.GetDataset(), std::numeric_limits<Operon::Scalar>::digits));
fmt::print("Best individual:\n");
fmt::print("{}\n", Operon::InfixFormatter::Format(best.Genotype, problem.GetDataset(), 8));

auto const& pop = gp.Parents();

// print all solutions in the first front
if (result["show-pareto-front"].as<bool>()) {
fmt::print("All individuals in the Pareto front:\n");
for(auto ind = pop.begin(); ind < pop.end(); ind++) {
Operon::Individual cur = *ind;
if(cur.Rank == 0) {
if (scale) {
Operon::Scalar a{1.0};
Operon::Scalar b{0.0};
auto estimatedTrain = Operon::Interpreter<Operon::Scalar, Operon::DefaultDispatch>::Evaluate(cur.Genotype, problem.GetDataset(), trainingRange);
Scale(cur, estimatedTrain, targetTrain, a, b);
}
fmt::print("{}\n", Operon::InfixFormatter::Format(cur.Genotype, problem.GetDataset(), 8));
}
}
}
} catch (std::exception& e) {
fmt::print(stderr, "error: {}\n", e.what());
return EXIT_FAILURE;
}

return 0;
}



void Scale(Operon::Individual& ind, Operon::Span<float const> estimated, Operon::Span<float const> target, Operon::Scalar& a, Operon::Scalar& b) {
auto [a_, b_] = Operon::FitLeastSquares(estimated, target);
a = static_cast<Operon::Scalar>(a_);
b = static_cast<Operon::Scalar>(b_);
// add scaling terms to the tree
auto& nodes = ind.Genotype.Nodes();
auto const sz = nodes.size();
if (std::abs(a - Operon::Scalar{1}) > std::numeric_limits<Operon::Scalar>::epsilon()) {
nodes.emplace_back(Operon::Node::Constant(a));
nodes.emplace_back(Operon::NodeType::Mul);
}
if (std::abs(b) > std::numeric_limits<Operon::Scalar>::epsilon()) {
nodes.emplace_back(Operon::Node::Constant(b));
nodes.emplace_back(Operon::NodeType::Add);
}
if (nodes.size() > sz) {
ind.Genotype.UpdateNodes();
}
}
1 change: 1 addition & 0 deletions cli/source/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ auto InitOptions(std::string const& name, std::string const& desc, int width) ->
("disable-symbols", "Comma-separated list of disabled symbols ("+symbols+")", cxxopts::value<std::string>())
("symbolic", "Operate in symbolic mode - no coefficient tuning or coefficient mutation", cxxopts::value<bool>()->default_value("false"))
("show-primitives", "Display the primitive set used by the algorithm")
("show-pareto-front", "Displays all expressions in the first Pareto front of the final generation", cxxopts::value<bool>()->default_value("false"))
("threads", "Number of threads to use for parallelism", cxxopts::value<size_t>()->default_value("0"))
("timelimit", "Time limit after which the algorithm will terminate", cxxopts::value<size_t>()->default_value(std::to_string(std::numeric_limits<size_t>::max())))
("debug", "Debug mode (more information displayed)")
Expand Down
8 changes: 4 additions & 4 deletions source/formatter/infix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ auto InfixFormatter::FormatNode(Tree const& tree, Operon::Map<Operon::Hash, std:
{
const auto& s = tree[i];
if (s.IsConstant()) {
auto formatString = fmt::format(fmt::runtime(s.Value < 0 ? "({{:.{}f}})" : "{{:.{}f}}"), decimalPrecision);
auto formatString = fmt::format(fmt::runtime(s.Value < 0 ? "({{:.{}g}})" : "{{:.{}g}}"), decimalPrecision);
fmt::format_to(std::back_inserter(current), fmt::runtime(formatString), s.Value);
} else if (s.IsVariable()) {
auto formatString = fmt::format(fmt::runtime(s.Value < 0 ? "(({{:.{}f}}) * {{}})" : "({{:.{}f}} * {{}})"), decimalPrecision);
auto formatString = fmt::format(fmt::runtime(s.Value < 0 ? "(({{:.{}g}}) * {{}})" : "({{:.{}g}} * {{}})"), decimalPrecision);
if (auto it = variableNames.find(s.HashValue); it != variableNames.end()) {
fmt::format_to(std::back_inserter(current), fmt::runtime(formatString), s.Value, it->second);
} else {
Expand All @@ -22,7 +22,7 @@ auto InfixFormatter::FormatNode(Tree const& tree, Operon::Map<Operon::Hash, std:
} else {
if (s.Value != 1) {
fmt::format_to(std::back_inserter(current), "(");
auto formatString = fmt::format(fmt::runtime(s.Value < 0 ? "({{:.{}f}})" : "{{:.{}f}}"), decimalPrecision);
auto formatString = fmt::format(fmt::runtime(s.Value < 0 ? "({{:.{}g}})" : "{{:.{}g}}"), decimalPrecision);
fmt::format_to(std::back_inserter(current), fmt::runtime(formatString), s.Value);
fmt::format_to(std::back_inserter(current), " * ");
}
Expand Down Expand Up @@ -132,4 +132,4 @@ auto InfixFormatter::Format(Tree const& tree, Dataset const& dataset, int decima
return Format(tree, variableNames, decimalPrecision);
}

} // namespace Operon
} // namespace Operon