Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:davnils/kex-sudoku

  • Loading branch information...
commit 54be129edc528faae9de79b3cc781eda8895fb08 2 parents 2976c23 + 0f6387f
@Positivapatrik Positivapatrik authored
View
1  report/TODO
@@ -12,5 +12,4 @@ Comparison ej klar
Generation och parallellizing. Parallellizing ej klar
Fixa stavning.
Utvidga conclusion med future för backtrack och rule-based. Nya resultat för boltzmann. Slutsatser kring generering/parallellisering/hklöfg/timedistributions
-Fixa in kod i appendix. Dokumentera kod. <- David
Figurlista
View
BIN  report/xjobb-en.pdf
Binary file not shown
View
39 report/xjobb-en.tex
@@ -8,6 +8,7 @@
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{tabularx}
+\usepackage{listings}
% No images or other floats can pass through \FloatBarrier
\usepackage{placeins}
@@ -45,9 +46,9 @@
\section{Statement of collaboration}
This is a list of responsibilities:
\begin{itemize}
- \item Implementations: Patrik have been responsibel for the rule-based solver and the backtrack solver. David have been responsibel for the Boltzmann machine and the test framework.
- \item Analysis: Patrik have analysed data from the rule-based solver and the backtrack solver. David have analysed data from the Boltzmann machine.
- \item Report writing: Patrik have written the first draft of introduction and method. David have written the first draft of background and conclusions. The analysis part was written together. Reviewing of the whole report was also a devided responsibility.
+ \item Implementations: Patrik has been responsible for the rule-based solver and the backtrack solver. David has been responsible for the Boltzmann machine and the test framework.
+ \item Analysis: Patrik has analyzed data from the rule-based solver and the backtrack solver. David has analyzed data from the Boltzmann machine.
+ \item Report writing: Patrik has written the first draft of introduction and method. David has written the first draft of background and conclusions. The analysis part was written together. Reviewing of the whole report was also a divided responsibly.
\end{itemize}
\clearpage
@@ -652,4 +653,36 @@ \chapter{RDF}\label{appA}
\end{figure}
that we refer to here: \ref{RDF_4}
+
+\chapter{Source code}
+\lstset{language=C++}
+\section{TestFramework.cpp}
+\lstinputlisting{../src/test/TestFramework.cpp}
+\section{TestFramework.h}
+\lstinputlisting{../src/test/TestFramework.h}
+
+\section{SodukuSolver.cpp}
+\lstinputlisting{../src/test/SudokuSolver.cpp}
+\section{SodukuSolver.h}
+\lstinputlisting{../src/test/SudokuSolver.h}
+
+\section{Randomizer.cpp}
+\lstinputlisting{../src/test/Randomizer.cpp}
+\section{Randomizer.h}
+\lstinputlisting{../src/test/Randomizer.h}
+
+\section{Boltzmann.cpp}
+\lstinputlisting{../src/neural/Boltzmann.cpp}
+\section{Boltzmann.h}
+\lstinputlisting{../src/neural/Boltzmann.h}
+
+\section{Square.cpp}
+\lstinputlisting{../src/neural/Square.cpp}
+\section{Square.h}
+\lstinputlisting{../src/neural/Square.h}
+
+\section{Rulebased.cpp / Backtrack}
+\lstinputlisting{../src/rulebased/rulebased2/Rulebased.cpp}
+\section{Rulebased.h / Backtrack}
+\lstinputlisting{../src/rulebased/rulebased2/Rulebased.h}
\end{document}
View
35 src/neural/Boltzmann.cpp
@@ -4,13 +4,16 @@
#include <iostream>
/**
- *
+ * Resets the current state.
*/
Boltzmann::Boltzmann()
{
reset();
}
+/**
+ * Randomizes the RNG and performs a reset.
+ */
void Boltzmann::reset()
{
srand(time(0));
@@ -19,7 +22,8 @@ void Boltzmann::reset()
}
/**
- *
+ * Adds a puzzle to be solved.
+ * @puzzle Puzzle to be solved.
*/
void Boltzmann::addPuzzle(grid_t puzzle)
{
@@ -39,7 +43,8 @@ void Boltzmann::addPuzzle(grid_t puzzle)
}
/**
- *
+ * Returns the current grid.
+ * @return Current grid.
*/
grid_t Boltzmann::getGrid()
{
@@ -68,13 +73,14 @@ void Boltzmann::printGrid(grid_t g)
}
/**
- *
+ * Runs until a given deadline.
+ * @param endTime clock() deadline.
+ * @return True on solving success.
*/
bool Boltzmann::runStep(clock_t endTime)
{
unsigned long iteration = 0;
do {
- //if(rand() % 10000 < 10) std::cout << "temperature: " << temperature << ", iteration: " << iteration << std::endl;
internal_grid_t::iterator rowIt;
for(rowIt = grid.begin(); rowIt != grid.end(); rowIt++) {
group_t::iterator squareIt;
@@ -96,16 +102,11 @@ bool Boltzmann::runStep(clock_t endTime)
return(false);
}
-void Boltzmann::printDigits(std::vector<int> digits)
-{
- for(int i = 0; i < 9; i++) {
- std::cout << digits[i] << " ";
- }
- std::cout << std::endl;
-}
-
/**
- *
+ * Updates a single grid node.
+ * @param row Current row.
+ * @param square Current grid node.
+ * @return True on success.
*/
bool Boltzmann::updateNode(internal_grid_t::iterator row,
group_t::iterator square)
@@ -137,7 +138,11 @@ bool Boltzmann::updateNode(internal_grid_t::iterator row,
}
/**
- *
+ * Checks a single quadrant for conflicts.
+ * @param digits Current accumulator of digits offsets.
+ * @param row Current row.
+ * @param square Current grid node.
+ * @return Updated accumulator with added offsets.
*/
std::vector<int> Boltzmann::checkQuadrant(std::vector<int> digits,
internal_grid_t::iterator row, group_t::iterator square)
View
13 src/neural/Boltzmann.h
@@ -7,18 +7,17 @@
#include "Square.h"
#include "../test/SudokuSolver.h"
-const uint8_t NOT_ASSIGNED = 0;
-const int MAX_TEMPERATURE = 100;
-const float dTEMPERATURE = -0.000035; //0.0004
-const float MIN_TEMPERATURE = 0.001;
-const int REDUCED_PUZZLE_RATE = 35;
-const int REDUCED_PUZZLE_FACTOR = 4;
+const int MAX_TEMPERATURE = 100; /* Maximum temperature */
+const float dTEMPERATURE = -0.000035; /* Simulated annealing constant */
+const float MIN_TEMPERATURE = 0.001; /* Minimum temperature ever reached */
+const int REDUCED_PUZZLE_RATE = 35; /* Number of clues to draw from a completely solved puzzle */
+const int REDUCED_PUZZLE_FACTOR = 4; /* Number of puzzles to generate from every input puzzle pair */
typedef std::vector<Square> group_t;
typedef std::vector<group_t> internal_grid_t;
/*
- *
+ * Boltzmann implements the main structure of a Boltzmann machine.
*/
class Boltzmann : public SudokuSolver
{
View
19 src/neural/Square.cpp
@@ -5,7 +5,7 @@
#include "Square.h"
/**
- *
+ * Assigns all nodes to not be used.
*/
Square::Square()
{
@@ -18,7 +18,8 @@ Square::Square()
}
/**
- *
+ * Assigns the current node be clamped at a given value.
+ * @param digit Value to be used.
*/
Square::Square(int digit)
{
@@ -34,7 +35,10 @@ Square::Square(int digit)
}
/**
- *
+ * Updates the current node with the given failure offsets.
+ * @param values Current digit values for this collection of candidates.
+ * @param temperature Current temperature.
+ * @return Returns true on success.
*/
bool Square::update(std::vector<int> values, float temperature)
{
@@ -52,7 +56,8 @@ bool Square::update(std::vector<int> values, float temperature)
}
/**
- *
+ * Adds an offset for every collision with the current node.
+ * @param values Accumulator used for failure offsets.
*/
void Square::sum(std::vector<int> & values)
{
@@ -71,7 +76,8 @@ void Square::sum(std::vector<int> & values)
}
/**
- *
+ * Checks if this square is resolved to a single digit.
+ * @return True if clamped to a single value.
*/
bool Square::isResolved()
{
@@ -79,7 +85,8 @@ bool Square::isResolved()
}
/**
- *
+ * Returns the best matching digit for the current square.
+ * @return Best matching digit.
*/
uint8_t Square::bestMatch()
{
View
9 src/neural/Square.h
@@ -4,16 +4,25 @@
#include <cstdint>
#include <vector>
+/* Offset added to colliding nodes */
const int COLLISION_OFFSET = -2;
+/* Offset added to colliding nodes if already resolved */
const int COLLISION_GIVEN_OFFSET = -20;
+/* Bias value used in offset calculation */
const float BIAS = 3.0f;
+/**
+ * Node describes a possible candidate for the current grid position.
+ */
struct Node
{
bool used;
int offset;
};
+/**
+ * Square class describes a single Sudoku grid value.
+ */
class Square
{
public:
View
38 src/rulebased/rulebased2/Rulebased.cpp
@@ -155,16 +155,6 @@ int Rulebased::guess(){
Board tmp;
tmp.operator=(board);
vector<int> * tmpvector = &tmp.board[min[1]][min[2]];
- /*
- cout<<"board"<<&board<<endl;
- cout<<"tmp"<<&tmp<<endl;
- cout<<"board.board: "<<&board.board[min[1]][min[2]]<<endl;
- cout<<"board.regions: "<<board.regions[min[1]][min[2]]<<endl;
- cout<<"&tmp.board: "<<&tmp.board[min[1]][min[2]]<<endl;
- cout<<"tmp.region: "<<tmp.regions[min[1]][min[2]]<<endl;
- cout<<"tmpvector: "<<tmpvector<<endl;
- */
- //cout<<"guessändringscheck";
//tmp.printPossibilities();
(*tmpvector)[0] = g;
//tmp.printPossibilities();
@@ -186,14 +176,6 @@ int Rulebased::guess(){
return 0;
}else{
board.operator=(correctGuesses[0]);
- /*
- board.board[min[1]][min[2]].clear();
- board.board[min[1]][min[2]].push_back(correctGuesses[0]);
- board.remove(min[1],min[2]);
- */
- //cout<<"Returning"<<endl;
- //board.printPossibilities();
- //cout<<"returning:"<<endl;
return correctGuesses.size();
}
@@ -355,23 +337,3 @@ vector< vector<int> > Rulebased::findCombinations(
}
return combinations;
}
-
-/*
-int main(){
- //cout<<"START:"<<endl;
- int grid[9][9];
- for(int i=0;i<9;i++){
- for(int j=0;j<9;j++){
- char x;
- cin >> x;
- if(x=='\n'){
- j--;
- }
- grid[i][j] = (int) (x-'0');
- }
- }
- Rulebased solver(grid);
- solver.runStep(clock()+CLOCKS_PER_SEC*20);
- solver.printBoard();
-}
-*/
View
10 src/test/Randomizer.cpp
@@ -1,7 +1,9 @@
#include "Randomizer.h"
/**
- *
+ * Sets the reference puzzle solution and a reduced puzzle.
+ * @param solved Solution to puzzle.
+ * @param reduced Actual puzzle.
*/
void Randomizer::reference(std::string solved, std::string reduced)
{
@@ -10,7 +12,8 @@ void Randomizer::reference(std::string solved, std::string reduced)
}
/**
- *
+ * Sets the number of clues to be withdrawn from the complete solution.
+ * @param rate Number of clues, inverted.
*/
void Randomizer::setMutationRate(int rate)
{
@@ -18,7 +21,8 @@ void Randomizer::setMutationRate(int rate)
}
/**
- *
+ * Generates a single puzzle.
+ * @return New puzzle.
*/
std::string Randomizer::generateCandidate()
{
View
2  src/test/Randomizer.h
@@ -4,7 +4,7 @@
#include <string>
/**
- *
+ * Randomizer provides functionality for randomly generating Sudokus.
*/
class Randomizer
{
View
16 src/test/SudokuSolver.cpp
@@ -3,7 +3,9 @@
#include "SudokuSolver.h"
/**
- *
+ * Counts the number of row and columns conflicts.
+ * @param grid Grid to be used.
+ * @return Number of conflicts.
*/
unsigned int SudokuSolver::countRowColumnConflicts(const grid_t & grid)
{
@@ -38,7 +40,9 @@ unsigned int SudokuSolver::countRowColumnConflicts(const grid_t & grid)
}
/**
- *
+ * Counts the number of sub-square conflicts.
+ * @param grid Grid to be used.
+ * @return Number of conflicts.
*/
unsigned int SudokuSolver::countSubSquareConflicts(const grid_t & grid)
{
@@ -66,14 +70,12 @@ unsigned int SudokuSolver::countSubSquareConflicts(const grid_t & grid)
}
/**
- *
+ * Checks if grid is a valid solution.
+ * @param grid Grid to be used.
+ * @return True if a valid Sudoku.
*/
bool SudokuSolver::isValidSolution(const grid_t & grid)
{
int a, b;
- /*a = countRowColumnConflicts(grid);
- b = countSubSquareConflicts(grid);
- std::cout << "rowcolumns: " << a << std::endl;
- std::cout << "squares: " << b << std::endl;*/
return(!countRowColumnConflicts(grid) && !countSubSquareConflicts(grid));
}
View
35 src/test/TestFramework.cpp
@@ -8,20 +8,26 @@
#include "Randomizer.h"
/**
- *
+ * Setups a test framework with file paths.
+ * @param puzzlePath Path to puzzles.
+ * @param matlabPath Path for matlab output.
*/
TestFramework::TestFramework(std::string puzzlePath, std::string matlabPath) : of(matlabPath)
{
this->puzzlePath = puzzlePath;
}
+/**
+ * Destructor that closes the associated output file.
+ */
TestFramework::~TestFramework()
{
of.close();
}
/**
- *
+ * Reads all puzzles associated with a given solver.
+ * @param solver Solver to be used.
*/
void TestFramework::readPuzzles(SudokuSolver * solver)
{
@@ -59,7 +65,8 @@ void TestFramework::readPuzzles(SudokuSolver * solver)
}
/**
- *
+ * Adds a solver for future testing.
+ * @param solver Solver to be added.
*/
void TestFramework::addSolver(SudokuSolver * solver)
{
@@ -67,7 +74,8 @@ void TestFramework::addSolver(SudokuSolver * solver)
}
/**
- *
+ * Runs all tests.
+ * @return All time measurements.
*/
std::vector<result_t> TestFramework::runTests()
{
@@ -113,7 +121,10 @@ std::vector<result_t> TestFramework::runTests()
}
/**
- *
+ * Solves a given puzzle repeatedly and returns an average.
+ * @param solver Solver to be used.
+ * @param puzzle Puzzle to be solved.
+ * @return Average running time.
*/
float TestFramework::runSampledSolver(SudokuSolver * solver, grid_t puzzle)
{
@@ -151,7 +162,10 @@ bool compareFloat(const float a, const float b)
/**
* Percentile bootstrap implementation.
- * www.public.iastate.edu/~vardeman/stat511/BootstrapPercentile.pdf
+ * See: www.public.iastate.edu/~vardeman/stat511/BootstrapPercentile.pdf
+ * @param data Values to be used in calculation.
+ * @param confidence Confidence level used in estimation.
+ * @return Boolean indicating boostrap success.
*/
bool TestFramework::bootstrap(std::vector<float> data, float confidence)
{
@@ -174,7 +188,10 @@ bool TestFramework::bootstrap(std::vector<float> data, float confidence)
}
/**
- *
+ * Calculates sampled standard deviation.
+ * @param data Samples to be used.
+ * @param avg Average value of samples.
+ * @return standard deviation.
*/
float TestFramework::sampledStdDeviation(const std::vector<float> & data, float avg)
{
@@ -195,7 +212,9 @@ float TestFramework::sampledStdDeviation(const std::vector<float> & data, float
}
/**
- *
+ * Calculates average of all positive items.
+ * @param data Samples to be used in calculation.
+ * @return Average of samples.
*/
float TestFramework::sampledAverage(const std::vector<float> & data)
{
View
4 src/test/TestFramework.h
@@ -17,7 +17,7 @@ const float CONFIDENCE = 0.95;
const float BOOTSTRAP_INTERVAL = 1.0f;
/**
- *
+ * Structure describing results for a single solver.
*/
typedef struct
{
@@ -28,7 +28,7 @@ typedef struct
} result_t;
/**
- *
+ * Test framework with functionality for measuring Sudoku solving performance.
*/
class TestFramework
{
Please sign in to comment.
Something went wrong with that request. Please try again.