Skip to content

Commit

Permalink
Merge branch 'master' of github.com:davnils/kex-sudoku
Browse files Browse the repository at this point in the history
  • Loading branch information
Positivapatrik committed Apr 11, 2012
2 parents 2976c23 + 0f6387f commit 54be129
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 91 deletions.
1 change: 0 additions & 1 deletion report/TODO
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ Comparison ej klar
Generation och parallellizing. Parallellizing ej klar Generation och parallellizing. Parallellizing ej klar
Fixa stavning. Fixa stavning.
Utvidga conclusion med future för backtrack och rule-based. Nya resultat för boltzmann. Slutsatser kring generering/parallellisering/hklöfg/timedistributions 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 Figurlista
Binary file modified report/xjobb-en.pdf
Binary file not shown.
39 changes: 36 additions & 3 deletions report/xjobb-en.tex
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
\usepackage{graphicx} \usepackage{graphicx}
\usepackage{blindtext} \usepackage{blindtext}
\usepackage{tabularx} \usepackage{tabularx}
\usepackage{listings}


% No images or other floats can pass through \FloatBarrier % No images or other floats can pass through \FloatBarrier
\usepackage{placeins} \usepackage{placeins}
Expand Down Expand Up @@ -45,9 +46,9 @@
\section{Statement of collaboration} \section{Statement of collaboration}
This is a list of responsibilities: This is a list of responsibilities:
\begin{itemize} \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 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 have analysed data from the rule-based solver and the backtrack solver. David have analysed data from the Boltzmann machine. \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 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 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} \end{itemize}


\clearpage \clearpage
Expand Down Expand Up @@ -652,4 +653,36 @@ \chapter{RDF}\label{appA}
\end{figure} \end{figure}


that we refer to here: \ref{RDF_4} 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} \end{document}
35 changes: 20 additions & 15 deletions src/neural/Boltzmann.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
#include <iostream> #include <iostream>


/** /**
* * Resets the current state.
*/ */
Boltzmann::Boltzmann() Boltzmann::Boltzmann()
{ {
reset(); reset();
} }


/**
* Randomizes the RNG and performs a reset.
*/
void Boltzmann::reset() void Boltzmann::reset()
{ {
srand(time(0)); srand(time(0));
Expand All @@ -19,7 +22,8 @@ void Boltzmann::reset()
} }


/** /**
* * Adds a puzzle to be solved.
* @puzzle Puzzle to be solved.
*/ */
void Boltzmann::addPuzzle(grid_t puzzle) void Boltzmann::addPuzzle(grid_t puzzle)
{ {
Expand All @@ -39,7 +43,8 @@ void Boltzmann::addPuzzle(grid_t puzzle)
} }


/** /**
* * Returns the current grid.
* @return Current grid.
*/ */
grid_t Boltzmann::getGrid() grid_t Boltzmann::getGrid()
{ {
Expand Down Expand Up @@ -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) bool Boltzmann::runStep(clock_t endTime)
{ {
unsigned long iteration = 0; unsigned long iteration = 0;
do { do {
//if(rand() % 10000 < 10) std::cout << "temperature: " << temperature << ", iteration: " << iteration << std::endl;
internal_grid_t::iterator rowIt; internal_grid_t::iterator rowIt;
for(rowIt = grid.begin(); rowIt != grid.end(); rowIt++) { for(rowIt = grid.begin(); rowIt != grid.end(); rowIt++) {
group_t::iterator squareIt; group_t::iterator squareIt;
Expand All @@ -96,16 +102,11 @@ bool Boltzmann::runStep(clock_t endTime)
return(false); 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, bool Boltzmann::updateNode(internal_grid_t::iterator row,
group_t::iterator square) group_t::iterator square)
Expand Down Expand Up @@ -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, std::vector<int> Boltzmann::checkQuadrant(std::vector<int> digits,
internal_grid_t::iterator row, group_t::iterator square) internal_grid_t::iterator row, group_t::iterator square)
Expand Down
13 changes: 6 additions & 7 deletions src/neural/Boltzmann.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
#include "Square.h" #include "Square.h"
#include "../test/SudokuSolver.h" #include "../test/SudokuSolver.h"


const uint8_t NOT_ASSIGNED = 0; const int MAX_TEMPERATURE = 100; /* Maximum temperature */
const int MAX_TEMPERATURE = 100; const float dTEMPERATURE = -0.000035; /* Simulated annealing constant */
const float dTEMPERATURE = -0.000035; //0.0004 const float MIN_TEMPERATURE = 0.001; /* Minimum temperature ever reached */
const float MIN_TEMPERATURE = 0.001; const int REDUCED_PUZZLE_RATE = 35; /* Number of clues to draw from a completely solved puzzle */
const int REDUCED_PUZZLE_RATE = 35; const int REDUCED_PUZZLE_FACTOR = 4; /* Number of puzzles to generate from every input puzzle pair */
const int REDUCED_PUZZLE_FACTOR = 4;


typedef std::vector<Square> group_t; typedef std::vector<Square> group_t;
typedef std::vector<group_t> internal_grid_t; typedef std::vector<group_t> internal_grid_t;


/* /*
* * Boltzmann implements the main structure of a Boltzmann machine.
*/ */
class Boltzmann : public SudokuSolver class Boltzmann : public SudokuSolver
{ {
Expand Down
19 changes: 13 additions & 6 deletions src/neural/Square.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Square.h" #include "Square.h"


/** /**
* * Assigns all nodes to not be used.
*/ */
Square::Square() Square::Square()
{ {
Expand All @@ -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) Square::Square(int digit)
{ {
Expand All @@ -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) bool Square::update(std::vector<int> values, float temperature)
{ {
Expand All @@ -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) void Square::sum(std::vector<int> & values)
{ {
Expand All @@ -71,15 +76,17 @@ 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() bool Square::isResolved()
{ {
return(resolved != 0); return(resolved != 0);
} }


/** /**
* * Returns the best matching digit for the current square.
* @return Best matching digit.
*/ */
uint8_t Square::bestMatch() uint8_t Square::bestMatch()
{ {
Expand Down
9 changes: 9 additions & 0 deletions src/neural/Square.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>


/* Offset added to colliding nodes */
const int COLLISION_OFFSET = -2; const int COLLISION_OFFSET = -2;
/* Offset added to colliding nodes if already resolved */
const int COLLISION_GIVEN_OFFSET = -20; const int COLLISION_GIVEN_OFFSET = -20;
/* Bias value used in offset calculation */
const float BIAS = 3.0f; const float BIAS = 3.0f;


/**
* Node describes a possible candidate for the current grid position.
*/
struct Node struct Node
{ {
bool used; bool used;
int offset; int offset;
}; };


/**
* Square class describes a single Sudoku grid value.
*/
class Square class Square
{ {
public: public:
Expand Down
38 changes: 0 additions & 38 deletions src/rulebased/rulebased2/Rulebased.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -155,16 +155,6 @@ int Rulebased::guess(){
Board tmp; Board tmp;
tmp.operator=(board); tmp.operator=(board);
vector<int> * tmpvector = &tmp.board[min[1]][min[2]]; 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(); //tmp.printPossibilities();
(*tmpvector)[0] = g; (*tmpvector)[0] = g;
//tmp.printPossibilities(); //tmp.printPossibilities();
Expand All @@ -186,14 +176,6 @@ int Rulebased::guess(){
return 0; return 0;
}else{ }else{
board.operator=(correctGuesses[0]); 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(); return correctGuesses.size();
} }


Expand Down Expand Up @@ -355,23 +337,3 @@ vector< vector<int> > Rulebased::findCombinations(
} }
return combinations; 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();
}
*/
10 changes: 7 additions & 3 deletions src/test/Randomizer.cpp
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "Randomizer.h" #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) void Randomizer::reference(std::string solved, std::string reduced)
{ {
Expand All @@ -10,15 +12,17 @@ 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) void Randomizer::setMutationRate(int rate)
{ {
this->rate = rate; this->rate = rate;
} }


/** /**
* * Generates a single puzzle.
* @return New puzzle.
*/ */
std::string Randomizer::generateCandidate() std::string Randomizer::generateCandidate()
{ {
Expand Down
2 changes: 1 addition & 1 deletion src/test/Randomizer.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string> #include <string>


/** /**
* * Randomizer provides functionality for randomly generating Sudokus.
*/ */
class Randomizer class Randomizer
{ {
Expand Down
Loading

0 comments on commit 54be129

Please sign in to comment.