Skip to content

Commit

Permalink
cli: compatibility changes required by scnlib v2
Browse files Browse the repository at this point in the history
  • Loading branch information
foolnotion committed Mar 21, 2024
1 parent 59ea4c1 commit 2f4d289
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
32 changes: 24 additions & 8 deletions cli/source/operator_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdexcept> // for runtime_error
#include <fmt/format.h> // for format
#include <tuple>
#include <scn/scan/scan.h>
#include <scn/scan.h>
#include "operon/interpreter/dispatch_table.hpp"
#include "operon/operators/creator.hpp" // for CreatorBase, BalancedTreeC...
#include "operon/operators/evaluator.hpp" // for Evaluator, EvaluatorBase
Expand Down Expand Up @@ -53,15 +53,23 @@ auto ParseSelector(std::string const& str, ComparisonCallback&& comp) -> std::un
if (name == "tournament") {
selector = std::make_unique<Operon::TournamentSelector>(std::move(comp));
size_t tournamentSize{defaultTournamentSize};
if (tok.size() > 1) { (void) scn::scan(tok[1], "{}", tournamentSize); }
if (tok.size() > 1) {
auto result = scn::scan<std::size_t>(tok[1], "{}");
ENSURE(result);
tournamentSize = result->value();
}
dynamic_cast<Operon::TournamentSelector*>(selector.get())->SetTournamentSize(tournamentSize);
} else if (name == "proportional") {
selector = std::make_unique<Operon::ProportionalSelector>(std::move(comp));
dynamic_cast<Operon::ProportionalSelector*>(selector.get())->SetObjIndex(0);
} else if (name == "rank") {
selector = std::make_unique<Operon::RankTournamentSelector>(std::move(comp));
size_t tournamentSize{defaultTournamentSize};
if (tok.size() > 1) { (void) scn::scan(tok[1], "{}", tournamentSize); }
if (tok.size() > 1) {
auto result = scn::scan<std::size_t>(tok[1], "{}");
ENSURE(result);
tournamentSize = result->value();
}
dynamic_cast<Operon::RankTournamentSelector*>(selector.get())->SetTournamentSize(tournamentSize);
} else if (name == "random") {
selector = std::make_unique<Operon::RandomSelector>();
Expand All @@ -80,7 +88,11 @@ auto ParseCreator(std::string const& str, PrimitiveSet const& pset, std::vector<
auto name = tok[0];

double bias{0}; // irregularity bias (used by btc and ptc20)
if(tok.size() > 1) { (void) scn::scan(tok[1], "{}", bias); }
if(tok.size() > 1) {
auto res = scn::scan<double>(tok[1], "{}");
ENSURE(res);
bias = res->value();
}

if (str == "btc") {
creator = std::make_unique<BalancedTreeCreator>(pset, inputs, bias);
Expand Down Expand Up @@ -131,20 +143,24 @@ auto ParseGenerator(std::string const& str, EvaluatorBase& eval, CrossoverBase&
} else if (name == "os") {
size_t maxSelectionPressure{100};
double comparisonFactor{0};
if (tok.size() > 1) { (void) scn::scan(tok[1], "{}", maxSelectionPressure); }
if (tok.size() > 2) { (void) scn::scan(tok[2], "{}", comparisonFactor); }
if (tok.size() > 1) {
maxSelectionPressure = scn::scan<size_t>(tok[1], "{}")->value();
}
if (tok.size() > 2) {
comparisonFactor = scn::scan<double>(tok[2], "{}")->value();
}
generator = std::make_unique<OffspringSelectionGenerator>(eval, cx, mut, femSel, maleSel);
dynamic_cast<OffspringSelectionGenerator*>(generator.get())->MaxSelectionPressure(maxSelectionPressure);
dynamic_cast<OffspringSelectionGenerator*>(generator.get())->ComparisonFactor(comparisonFactor);
} else if (name == "brood") {
generator = std::make_unique<BroodOffspringGenerator>(eval, cx, mut, femSel, maleSel);
size_t broodSize{BroodOffspringGenerator::DefaultBroodSize};
if (tok.size() > 1) { (void) scn::scan(tok[1], "{}", broodSize); }
if (tok.size() > 1) { broodSize = scn::scan<size_t>(tok[1], "{}")->value(); }
dynamic_cast<BroodOffspringGenerator*>(generator.get())->BroodSize(broodSize);
} else if (name == "poly") {
generator = std::make_unique<PolygenicOffspringGenerator>(eval, cx, mut, femSel, maleSel);
size_t polygenicSize{PolygenicOffspringGenerator::DefaultBroodSize};
if (tok.size() > 1) { (void) scn::scan(tok[1], "{}", polygenicSize); }
if (tok.size() > 1) { polygenicSize = scn::scan<size_t>(tok[1], "{}")->value(); }
dynamic_cast<PolygenicOffspringGenerator*>(generator.get())->PolygenicSize(polygenicSize);
} else {
throw std::invalid_argument(detail::GetErrorString("generator", str));
Expand Down
13 changes: 8 additions & 5 deletions cli/source/operon_parse_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <cxxopts.hpp>
#include <fmt/core.h>
#include <scn/scn.h>
#include <scn/scan.h>

auto main(int argc, char** argv) -> int
{
Expand Down Expand Up @@ -67,9 +67,9 @@ auto main(int argc, char** argv) -> int
Operon::DefaultDispatch dtable;
Operon::Range range{0, ds.Rows<std::size_t>()};
if (result["range"].count() > 0) {
size_t a{0};
size_t b{0};
[[maybe_unused]] auto s = scn::scan(result["range"].as<std::string>(), "{}:{}", a, b);
auto res = scn::scan<std::size_t, std::size_t>(result["range"].as<std::string>(), "{}:{}");
ENSURE(res);
auto [a, b] = res->values();
range = Operon::Range{a, b};
}

Expand All @@ -90,7 +90,10 @@ auto main(int argc, char** argv) -> int
Operon::Scalar a{0};
Operon::Scalar b{0};
if (result["scale"].count() > 0) {
[[maybe_unused]] auto s = scn::scan(result["scale"].as<std::string>(), "{}:{}", a, b);
auto res = scn::scan<Operon::Scalar, Operon::Scalar>(result["scale"].as<std::string>(), "{}:{}");
ENSURE(res);
a = std::get<0>(res->values());
b = std::get<1>(res->values());
} else {
auto [a_, b_] = Operon::FitLeastSquares(est, tgt);
a = static_cast<Operon::Scalar>(a_);
Expand Down
10 changes: 6 additions & 4 deletions cli/source/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "util.hpp"

#include <memory>
#include <scn/scn.h>
#include <scn/scan.h>

#include "operon/core/node.hpp"
#include "operon/core/pset.hpp"
Expand Down Expand Up @@ -80,9 +80,11 @@ auto FormatBytes(size_t bytes) -> std::string

auto ParseRange(std::string const& str) -> std::pair<size_t, size_t>
{
size_t a{0};
size_t b{0};
(void) scn::scan(str, "{}:{}", a, b);
auto result = scn::scan<size_t, size_t>(str, "{}:{}");
if (!result) {
throw std::runtime_error(fmt::format("could not parse range '{}'", str));
}
auto [a, b] = result->values();
return std::make_pair(a, b);
}

Expand Down
4 changes: 3 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@
fmt
icu
jemalloc
libassert
mdspan
pkg-config
pratt-parser.packages.${system}.default
simdutf_4 # required by scnlib
scnlib
taskflow
unordered_dense
Expand Down Expand Up @@ -113,7 +115,7 @@
clang-tools_17
cppcheck
include-what-you-use
cmake-language-server
#cmake-language-server
]);

buildInputs = operon.buildInputs ++ (with pkgs; [
Expand Down
12 changes: 6 additions & 6 deletions test/source/performance/nondominatedsort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "operon/operators/selector.hpp"

#include <filesystem>
#include <scn/scn.h>
#include <scn/scan.h>
#include <ranges>
#include <string_view>

Expand Down Expand Up @@ -523,9 +523,9 @@ TEST_CASE("dtlz2")
std::string name = entry.path().string();
if (name.find("rs") == std::string::npos) { continue; }
std::cout << name << std::endl;
std::size_t n{}; // population size
std::size_t m{}; // number of objectives
std::ignore = scn::scan(name, "./csv/nsga2_DTLZ2_{}_{}_rs.csv", n, m);
auto result = scn::scan<std::size_t, std::size_t>(name, "./csv/nsga2_DTLZ2_{}_{}_rs.csv");
ENSURE(result);
auto [n, m] = result->values();

// read part
Value gen;
Expand All @@ -542,7 +542,7 @@ TEST_CASE("dtlz2")
ind = Operon::Individual(m);
} else {
std::string s(sv.begin(), sv.end());
Operon::Scalar v{}; std::ignore = scn::scan(s, "{}", v);
auto v = scn::scan<Operon::Scalar>(s, "{}")->value();
ind[c++] = v;
}
}
Expand Down Expand Up @@ -590,4 +590,4 @@ TEST_CASE("dtlz2")
std::ofstream out("./dtlz2-benchmark.csv");
bench.render(nb::templates::csv(), out);
}
} // namespace Operon::Test
} // namespace Operon::Test

0 comments on commit 2f4d289

Please sign in to comment.