Skip to content

Commit

Permalink
Improved stats reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzm committed May 23, 2019
1 parent ac39145 commit 93e26f1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 42 deletions.
19 changes: 7 additions & 12 deletions Pentominoes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ void Pentominoes::addAspectPlacements(
}


void Pentominoes::init(int& rows, int& primaryCols, int& totalCols, int& elems)
void Pentominoes::init()
{
subGoals.emplace_back();
auto& matrix = subGoals.back();
auto& sg = subGoals.back();

set<Piece> aspects;
for(auto const& piece: pieces) {
Expand All @@ -168,26 +168,21 @@ void Pentominoes::init(int& rows, int& primaryCols, int& totalCols, int& elems)
}

for(auto const& placement: placements) {
auto c = matrix.findColumn(placement.name);
auto c = sg.matrix.findColumn(placement.name);
auto e = new Element();
e->insertUD(c);
++elems;
++sg.elems;
for(auto const& cell: placement.cells) {
ostringstream cname;
cname << setfill('0') << setw(2) << cell.rOffset << setw(2) << cell.cOffset;
auto c2 = matrix.findColumn(cname.str());
auto c2 = sg.matrix.findColumn(cname.str());
auto e2 = new Element();
e2->insertUD(c2);
e2->insertLR(e);
++elems;
++rows;
++sg.elems;
++sg.rows;
}
}

int primary, total;
matrix.getColumnStats(primary, total);
primaryCols += primary;
totalCols += total;
}


Expand Down
2 changes: 1 addition & 1 deletion Pentominoes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Pentominoes : public Puzzle
{
protected:

void init(int& rows, int& primaryCols, int& totalCols, int& elem) override;
void init() override;
void print(std::vector<Element *>& solution) override;

virtual void buildBoard(std::set<Cell>& board) = 0;
Expand Down
33 changes: 23 additions & 10 deletions Puzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,34 @@ Puzzle::~Puzzle()

void Puzzle::solve(bool countOnly)
{
int rows = 0, primaryCols = 0, totalCols = 0, elems = 0;
init(rows, primaryCols, totalCols, elems);
init();

int nodeCount = 0, solutionCount = 0;
for(auto &matrix: subGoals) {
matrix.findCovers(nodeCount, solutionCount, countOnly
for(auto& sg: subGoals) {
sg.matrix.findCovers(sg.nodes, sg.solutions, countOnly
? function<void (vector<Element *>&)>()
: [this](vector<Element *>& solution){ print(solution); }
);
solutionCount += sg.solutions;
}

cout << solutionCount << " solutions found" << endl << endl;

if (!subGoals.empty()) {

int primaryCols, totalCols;
subGoals[0].matrix.getColumnStats(primaryCols, totalCols);
cout << totalCols << " constraint columns (" << primaryCols << " primary)" << endl;

int n = 0;
for(auto& sg: subGoals) {
if (subGoals.size() > 1) cout << "Subgoal " << ++n << ": ";
cout << sg.rows << " rows, " << sg.elems << " elements, ";
if (subGoals.size() > 1) cout << sg.solutions << " solutions, ";
cout << sg.nodes << " nodes traversed" << endl;
}

cout << endl;
}

cout << subGoals.size() << " subgoals, ";
cout << rows << "x" << totalCols << " constraint matrix, ";
cout << primaryCols << " primary columns, ";
cout << elems << " elements" << endl;
cout << solutionCount << " solutions, ";
cout << nodeCount << " search nodes visted" << endl;
}
12 changes: 10 additions & 2 deletions Puzzle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ class Puzzle

protected:

virtual void init(int& rows, int& primaryCols, int& totalCols, int& elems) = 0;
virtual void init() = 0;
virtual void print(std::vector<Element *>& solution) = 0;

std::vector<Matrix> subGoals;
struct SubGoal {
Matrix matrix;
int rows = 0;
int elems = 0;
int nodes = 0;
int solutions = 0;
};

std::vector<SubGoal> subGoals;

};

Expand Down
27 changes: 11 additions & 16 deletions Queens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ Queens::Queens(int n)
}


void Queens::init(int& rows, int& primaryCols, int& totalCols, int& elems)
void Queens::init()
{
subGoals.emplace_back();
auto& matrix = subGoals.back();
auto& sg = subGoals.back();

int r = (n-1)/2, rs = -1;
for(int i=0; i<n; ++i) {
Expand All @@ -36,48 +36,43 @@ void Queens::init(int& rows, int& primaryCols, int& totalCols, int& elems)
ostringstream rname;
rname << "R" << r;
auto e1 = new Element();
auto c1 = matrix.findColumn(rname.str());
auto c1 = sg.matrix.findColumn(rname.str());
e1->insertUD(c1);
++elems;
++sg.elems;

ostringstream fname;
fname << "F" << f;
auto e2 = new Element();
auto c2 = matrix.findColumn(fname.str());
auto c2 = sg.matrix.findColumn(fname.str());
e2->insertUD(c2);
e2->insertLR(e1);
++elems;
++sg.elems;

int a = r+f;
if (a!=0 && a!=(2*n-2)) {
ostringstream aname;
aname << "A" << a;
auto e3 = new Element();
auto c3 = matrix.findColumn(aname.str(), false);
auto c3 = sg.matrix.findColumn(aname.str(), false);
e3->insertUD(c3);
e3->insertLR(e1);
++elems;
++sg.elems;
}

int b = n - 1 - r + f;
if (b!=0 && b!=(2*n-2)) {
ostringstream bname;
bname << "B" << b;
auto e4 = new Element();
auto c4 = matrix.findColumn(bname.str(), false);
auto c4 = sg.matrix.findColumn(bname.str(), false);
e4->insertUD(c4);
e4->insertLR(e1);
++elems;
++sg.elems;
}

++rows;
++sg.rows;
}
}

int primary, total;
matrix.getColumnStats(primary, total);
primaryCols += primary;
totalCols += total;
}


Expand Down
2 changes: 1 addition & 1 deletion Queens.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Queens : public Puzzle

private:

void init(int& rows, int& primaryCols, int& totalCols, int& elem) override;
void init() override;
void print(std::vector<Element *>& solution) override;

int n;
Expand Down

0 comments on commit 93e26f1

Please sign in to comment.