Skip to content

Commit

Permalink
Change pruneBranch to bestBranch
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Sep 30, 2023
1 parent bbcfda4 commit 2ea8e8e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 34 deletions.
15 changes: 7 additions & 8 deletions include/cli_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ const std::string ASSIGN_ALGO_DESC =
" Select the assignment algorithm. Valid options are `dfs' and `bfs'. Default is `dfs'.\n";
constexpr int ASSIGN_ALGO_VAL = 3001;

// TODO : swap this around, instead of pruning N most unpromising branches, make it try top N promising branches
const std::string PRUNE_BRANCHES = "prune-branches";
const std::string PRUNE_BRANCHES_DESC =
" -" + PRUNE_BRANCHES + "=<N>\n"
" Prune the N least promising branches at each node in the assignment tree search.\n"
" Specify `smart' instead of an integer to use a computed `smart' pruning at each node\n"
" instead of just a constant integer pruning.\n";
constexpr int PRUNE_BRANCHES_VAL = 3002;
const std::string BEST_BRANCHES = "best-branches";
const std::string BEST_BRANCHES_DESC =
" -" + BEST_BRANCHES + "=<N>\n"
" Use only the N most promising branches at each node in the assignment tree search.\n"
" Specify `smart' instead of an integer to use a computed `smart' cutoff at each node\n"
" instead of just a constant integer cutoff.\n";
constexpr int BEST_BRANCHES_VAL = 3002;


// Fieldmap override options
Expand Down
6 changes: 4 additions & 2 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <filesystem>
#include <iostream>
#include <png.hpp>
#include <stdint.h>
#include <string>
#include <tuple>
#include <unordered_map>
Expand Down Expand Up @@ -806,12 +807,13 @@ struct CompilerConfig {
// Palette assignment algorithm configuration
AssignAlgorithm assignAlgorithm;
std::size_t exploredNodeCutoff;
std::size_t pruneCount;
std::size_t bestBranches;
bool smartPrune;

CompilerConfig()
: mode{}, transparencyColor{RGBA_MAGENTA}, tripleLayer{true}, defaultBehavior{"MB_NORMAL"},
assignAlgorithm{AssignAlgorithm::DEPTH_FIRST}, exploredNodeCutoff{2'000'000}, pruneCount{0}, smartPrune{false}
assignAlgorithm{AssignAlgorithm::DEPTH_FIRST}, exploredNodeCutoff{2'000'000}, bestBranches{SIZE_MAX},
smartPrune{false}
{
}
};
Expand Down
16 changes: 10 additions & 6 deletions src/cli_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ DEFAULT_BEHAVIOR_DESC + "\n" +
" Color Assignment Config Options\n" +
ASSIGN_EXPLORE_CUTOFF_DESC + "\n" +
ASSIGN_ALGO_DESC + "\n" +
PRUNE_BRANCHES_DESC + "\n" +
BEST_BRANCHES_DESC + "\n" +
" Fieldmap Override Options\n" +
TILES_PRIMARY_OVERRIDE_DESC + "\n" +
TILES_TOTAL_OVERRIDE_DESC + "\n" +
Expand Down Expand Up @@ -389,7 +389,7 @@ static void parseSubcommandOptions(PtContext &ctx, int argc, char *const *argv)
// Color assignment config options
{ASSIGN_EXPLORE_CUTOFF.c_str(), required_argument, nullptr, ASSIGN_EXPLORE_CUTOFF_VAL},
{ASSIGN_ALGO.c_str(), required_argument, nullptr, ASSIGN_ALGO_VAL},
{PRUNE_BRANCHES.c_str(), required_argument, nullptr, PRUNE_BRANCHES_VAL},
{BEST_BRANCHES.c_str(), required_argument, nullptr, BEST_BRANCHES_VAL},

// Fieldmap override options
{TILES_PRIMARY_OVERRIDE.c_str(), required_argument, nullptr, TILES_PRIMARY_OVERRIDE_VAL},
Expand Down Expand Up @@ -517,12 +517,16 @@ static void parseSubcommandOptions(PtContext &ctx, int argc, char *const *argv)
case ASSIGN_ALGO_VAL:
ctx.compilerConfig.assignAlgorithm = parseAssignAlgorithm(ctx.err, ASSIGN_ALGO, optarg);
break;
case PRUNE_BRANCHES_VAL:
case BEST_BRANCHES_VAL:
if (std::string{optarg} == "smart") {
ctx.compilerConfig.smartPrune = true;
}
else {
ctx.compilerConfig.pruneCount = parseIntegralOption<std::size_t>(ctx.err, PRUNE_BRANCHES, optarg);
ctx.compilerConfig.bestBranches = parseIntegralOption<std::size_t>(ctx.err, BEST_BRANCHES, optarg);
if (ctx.compilerConfig.bestBranches == 0) {
fatalerror(ctx.err,
fmt::format("option '{}' argument cannot be 0", fmt::styled(BEST_BRANCHES, fmt::emphasis::bold)));
}
}
break;

Expand Down Expand Up @@ -880,8 +884,8 @@ static void parseSubcommandOptions(PtContext &ctx, int argc, char *const *argv)
ctx.err.setAllWarnings(WarningMode::OFF);
}

if (ctx.compilerConfig.smartPrune && ctx.compilerConfig.pruneCount > 0) {
fatalerror(ctx.err, fmt::format("found two conflicting configs for `{}' option", PRUNE_BRANCHES));
if (ctx.compilerConfig.smartPrune && ctx.compilerConfig.bestBranches > 0) {
fatalerror(ctx.err, fmt::format("found two conflicting configs for `{}' option", BEST_BRANCHES));
}

/*
Expand Down
21 changes: 3 additions & 18 deletions src/palette_assignment.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "palette_assignment.h"

#include <algorithm>
#include <deque>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -91,18 +92,10 @@ AssignResult assignDepthFirst(PtContext &ctx, AssignState &state, std::vector<Co
return pal1IntersectSize > pal2IntersectSize;
});

std::size_t stopLimit = state.hardwarePalettes.size();
std::size_t stopLimit = std::min(state.hardwarePalettes.size(), ctx.compilerConfig.bestBranches);
if (ctx.compilerConfig.smartPrune) {
throw std::runtime_error{"TODO : impl smart prune"};
}
else if (ctx.compilerConfig.pruneCount > 0) {
if (ctx.compilerConfig.pruneCount >= stopLimit) {
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("'prune-branches' parameter '{}' pruned every branch",
fmt::styled(ctx.compilerConfig.pruneCount, fmt::emphasis::bold)));
}
stopLimit -= ctx.compilerConfig.pruneCount;
}
for (size_t i = 0; i < stopLimit; i++) {
const ColorSet &palette = state.hardwarePalettes.at(i);

Expand Down Expand Up @@ -215,19 +208,11 @@ AssignResult assignBreadthFirst(PtContext &ctx, AssignState &initialState, std::
});

bool sawAssignmentWithIntersection = false;
std::size_t stopLimit = currentState.hardwarePalettes.size();
std::size_t stopLimit = std::min(currentState.hardwarePalettes.size(), ctx.compilerConfig.bestBranches);
if (ctx.compilerConfig.smartPrune) {
// TODO : impl smart prune feature
throw std::runtime_error{"TODO : impl smart prune"};
}
else if (ctx.compilerConfig.pruneCount > 0) {
if (ctx.compilerConfig.pruneCount >= stopLimit) {
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("'prune-branches' parameter '{}' pruned every branch",
fmt::styled(ctx.compilerConfig.pruneCount, fmt::emphasis::bold)));
}
stopLimit -= ctx.compilerConfig.pruneCount;
}
for (size_t i = 0; i < stopLimit; i++) {
const ColorSet &palette = currentState.hardwarePalettes.at(i);

Expand Down

0 comments on commit 2ea8e8e

Please sign in to comment.