Skip to content

Commit

Permalink
-!: print ranges straightaway, e.g., '0-99 solved...'
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Dec 19, 2023
1 parent 9fff326 commit 5da2fbb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 53 deletions.
9 changes: 2 additions & 7 deletions include/mp/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,6 @@ namespace sol {
* (use description from the comment.) For extra codes, use
* passing ranges (e.g., for stopping with a feasible solution
* on a limit, use 400-449), otherwise SPECIFIC+ codes.
*
* If a code description's 2nd-last word is 'codes' (case-sensitive),
* for example:
* 'solved? solution candidate may be infeasible; codes 100-199',
* this code is printed as a header.
*/
enum Status {
/** If not touched. Don't register this code. */
Expand Down Expand Up @@ -274,11 +269,11 @@ enum Status {
LIMIT_NO_FEAS_SOFTMEM = LIMIT_NO_FEAS + 10,

/** Failure, without a feasible solution.
Codes 500-599.
Codes 500-999.
With a feasible solution, use LIMIT_FEAS_FAILURE. */
FAILURE = 500,
/** End of the 'failure' range. */
FAILURE_LAST = 599,
FAILURE_LAST = 999,

/** Failure. A numeric issue without a feasible solution.
* With a feasible solution, use UNCERTAIN. */
Expand Down
38 changes: 37 additions & 1 deletion include/mp/solver-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SOLVERBASE_H

#include <map>
#include <set>

#include "solver-opt.h"
#include "common.h"
Expand Down Expand Up @@ -105,8 +106,43 @@ class SolveResultRegistry {
/// Destroy
virtual ~SolveResultRegistry() { }

/// Registry entry.
/// This is a pair<int, int>,
/// meaning a range (first-last) of result codes.
/// Plus a description.
class RegEntry {
public:
/// Construct from a range
RegEntry(int a, int b, std::string d)
: first_(a), last_(b), descr_(std::move(d))
{ assert(a<=b); }
/// Construct from a single code
RegEntry(int a, std::string d)
: first_(a), last_(a), descr_(std::move(d)) { }
/// Is signle code?
bool isSingle() const { return first()==last(); }
/// First
int first() const { return first_; }
/// Last
int last() const { return last_; }
/// Descr
const std::string& descr() const { return descr_; }
/// operator<:
/// We need range 100-199 to come before range 100-149
/// before single code 100, etc.
bool operator<(const RegEntry& k) const {
return first() < k.first()
? true : first() > k.first()
? false : last() > k.last();
}
private:
int first_;
int last_;
std::string descr_;
};

/// Registry map
using SRRegMap = std::map<int, std::string>;
using SRRegMap = std::set<RegEntry>;

/// Add a map with result descriptions
void AddSolveResults(const SRRegMap& sm,
Expand Down
72 changes: 27 additions & 45 deletions src/solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,26 +400,11 @@ bool SolverAppOptionParser::ShowSolverOptions(const char* param) {
bool SolverAppOptionParser::ShowSolveResults() {
solver_.Print("Solve result table for {}\n", solver_.long_name());
for (const auto& sr: solver_.GetSolveResultRegistry()) {
const auto& desc = sr.second;
bool fHeader = false;
auto p2=desc.size()-1; // find 2nd last word
while (p2<desc.size() && desc[p2]==' ')
--p2;
if (p2<desc.size()) {
p2 = desc.rfind(' ', p2);
while (p2<desc.size() && desc[p2]==' ')
--p2;
if (p2<desc.size()) {
auto p1 = desc.rfind(' ', p2);
if (p1<desc.size()) {
fHeader = "codes"==desc.substr(p1+1, p2-p1);
}
}
}
if (fHeader)
solver_.Print("\t{}\t{}\n", sr.first, sr.second);
else
solver_.Print("\t\t{}\t{}\n", sr.first, sr.second);
if (sr.isSingle())
solver_.Print("\t {:3}\t{}\n", sr.first(), sr.descr());
else // range, printing as a header
solver_.Print("\t{:3}-{:3}\t{}\n",
sr.first(), sr.last(), sr.descr());
}
return false;
}
Expand Down Expand Up @@ -708,30 +693,26 @@ static void ProcessLines_AvoidComments(std::istream& stream,
SolveResultRegistry::SolveResultRegistry()
: registry_ {
{ // Adding standard solver codes a priori
{ sol::SOLVED,
{ sol::SOLVED, sol::SOLVED_LAST,
"solved: optimal for an optimization problem, "
"feasible for a satisfaction problem; "
"codes 0-99 " },
{ sol::UNCERTAIN,
"solved? solution candidate returned but error likely; "
"codes 100-199 " },
{ sol::INFEASIBLE, "infeasible; codes 200-299 " },
{ sol::UNBOUNDED_FEAS,
"unbounded, feasible solution returned; "
"codes 300-349 " },
{ sol::UNBOUNDED_NO_FEAS,
"unbounded, no feasible solution returned; "
"codes 350-399 " },
{ sol::LIMIT_FEAS,
"feasible for a satisfaction problem " },
{ sol::UNCERTAIN, sol::UNCERTAIN_LAST,
"solved? solution candidate returned but error likely " },
{ sol::INFEASIBLE, sol::INFEASIBLE_LAST,
"infeasible " },
{ sol::UNBOUNDED_FEAS, sol::UNBOUNDED_FEAS_LAST,
"unbounded, feasible solution returned " },
{ sol::UNBOUNDED_NO_FEAS, sol::UNBOUNDED_NO_FEAS_LAST,
"unbounded, no feasible solution returned " },
{ sol::LIMIT_FEAS, sol::LIMIT_FEAS_LAST,
"limit, feasible: "
"stopped by a limit, e.g., on iterations or Ctrl-C; "
"codes 400-449 " },
{ sol::LIMIT_INF_UNB,
"limit, problem is either infeasible or unbounded; "
"codes 450-469 " },
{ sol::LIMIT_NO_FEAS,
"limit, no solution returned; codes 470-499 " },
{ sol::FAILURE, "failure, no solution returned; codes 500-999 " },
"stopped, e.g., on iterations or Ctrl-C " },
{ sol::LIMIT_INF_UNB, sol::LIMIT_INF_UNB_LAST,
"limit, problem is either infeasible or unbounded " },
{ sol::LIMIT_NO_FEAS, sol::LIMIT_NO_FEAS_LAST,
"limit, no solution returned " },
{ sol::FAILURE, sol::FAILURE_LAST,
"failure, no solution returned " },
{ sol::NUMERIC, "failure: numeric issue, no feasible solution" }
}
} { }
Expand All @@ -740,10 +721,11 @@ void SolveResultRegistry::AddSolveResults(
const SRRegMap &sm, bool ifCanReplace) {
for (const auto& sr : sm) {
if (!ifCanReplace // Fail unless ifCanReplace
&& registry_.end()!=registry_.find(sr.first))
&& registry_.end()!=registry_.find(sr))
MP_RAISE(fmt::format(
"Duplicated solve code {}", sr.first));
registry_[sr.first] = sr.second;
"Duplicated solve code range {}-{}",
sr.first(), sr.last()));
registry_.insert(sr);
}
}

Expand Down

0 comments on commit 5da2fbb

Please sign in to comment.