diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index ef2e6ee..9ef8c1e 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -7,10 +7,14 @@ set(CMAKE_CXX_STANDARD 11) set(SOURCE_FILES main.cpp header.h - functions/solve/classExtension.cpp functions/solve/structure.cpp + functions/AbstractionLayers/AbstraktionLayer_Base.h - ) + functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp + functions/AbstractionLayers/DestructionPower/DestructionPower.cpp + header/solve.h + header/input.h + functions/solve/puzzleExtension.cpp) add_executable(PuzzleSolver ${SOURCE_FILES}) include_directories(${OpenCV_INCLUDE_DIRS}) target_link_libraries(PuzzleSolver ${OpenCV_LIBS}) \ No newline at end of file diff --git a/Source/functions/AbstractionLayers/AbstraktionLayer_Base.h b/Source/functions/AbstractionLayers/AbstraktionLayer_Base.h index ccb54a8..f3213ab 100644 --- a/Source/functions/AbstractionLayers/AbstraktionLayer_Base.h +++ b/Source/functions/AbstractionLayers/AbstraktionLayer_Base.h @@ -3,7 +3,7 @@ #include #include -#include "../../header/solve.h" +#include "../../header/input.h" using namespace std; @@ -19,7 +19,7 @@ typedef map qualityVector; * @tparam T template parameter which should be the property class of the layer */ template -class AbstraktionLayer_Base +class AbstractionLayer_Base { public: /** @@ -63,7 +63,7 @@ public: m_constraintMatrix = vector>(collumns, vector(rows)); } - vector> m_constraintMatrix; //!<-- Matrix where the constraints of the layer will be saved + vector> m_constraintMatrix{}; //!<-- Matrix where the constraints of the layer will be saved }; diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp index ffc9894..f0527ac 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp @@ -3,7 +3,13 @@ // #include "DestructionPower.h" -#include + + +map DestructionPower_Properties::SpeedTable = + { + {1,0.001} + }; + void DestructionPower::PreProcessing(const vector* partArray) { @@ -31,14 +37,18 @@ void DestructionPower::DestructionOfSurrounding(const coor constraintCoordinate) if(constraintCoordinate.row > 0) { divisor++; - newDestructionArray[i] += m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row-1].m_destruction.DestructionArray[i]; + newDestructionArray[i] += m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row-1].DestructionArray[i]; } if(constraintCoordinate.col > 0) { divisor++; - newDestructionArray[i] += m_constraintMatrix[constraintCoordinate.col-1][constraintCoordinate.row].m_destruction.DestructionArray[i]; + newDestructionArray[i] += m_constraintMatrix[constraintCoordinate.col-1][constraintCoordinate.row].DestructionArray[i]; } if(divisor) newDestructionArray[i] /=divisor; } } +DestructionPower_Properties::DestructionPower_Properties() { + for(int i=0;i #include #include #include -class DestructionPower : public AbstraktionLayer_Base +class DestructionPower : public AbstractionLayer_Base { public: - void PreProcessing(const vector* partArray);//override - bool EvaluateQuality (const coor constraintCoordinate, qualityVector& qVector); - bool SetConstraintOnPosition(const coor constraintCoordinate, const AbstractionLayer_1_Properties constraint); - bool RemoveConstraintOnPosition(const coor constraintCoordinate); + void PreProcessing(const vector* partArray) override; + bool EvaluateQuality (coor constraintCoordinate, qualityVector& qVector) override; + bool SetConstraintOnPosition(coor constraintCoordinate, AbstractionLayer_1_Properties constraint); + bool RemoveConstraintOnPosition(coor constraintCoordinate)override; - void DestructionOfSurrounding(const coor constraintCoordinate); + void DestructionOfSurrounding(coor constraintCoordinate); private: }; diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h index 54d9056..2a8f0a0 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h @@ -1,47 +1,29 @@ // // Created by mpapa on 05.12.2017. // +#pragma once -#ifndef SOURCE_DESTRUCTIONPOWER_PROPERTIES_H -#define SOURCE_DESTRUCTIONPOWER_PROPERTIES_H - +#define DESTRUCTION_INIT 0.5 #include -#include "DestructionPower.h" +#include +#include + +using namespace std; class DestructionPower_Properties { public: - DestructionPower_Properties() - { - for(int i=0;i getSpeedTable() { return SpeedTable; } - void setSpeedTable() - { SpeedTable=create_SpeedTable();} + void setSpeedTable(); private: - static map create_SpeedTable(); static map SpeedTable; - - float DestructionArray[DESTRUCTION_COUNT]; - + vector DestructionArray; friend class DestructionPower; -}; - -map DestructionPower_Properties::create_SpeedTable() -{ - map m; - m[1] = 0.001; - return m; -} - -#endif //SOURCE_DESTRUCTIONPOWER_PROPERTIES_H +}; \ No newline at end of file diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp index 5dce621..c9f3880 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp @@ -3,6 +3,8 @@ // #include "AbstractionLayer_1.h" +#include "../../../header.h" + #include void AbstractionLayer_1::PreProcessing(const vector* partArray) @@ -16,7 +18,7 @@ bool AbstractionLayer_1::EvaluateQuality (const coor constraintCoordinate, quali { for(auto it = qVector.begin(); it != qVector.end(); it++) { - if(PlaceOfPartGood(constraintCoordinate, it->first->m_test1.m_connections)) + if(PlaceOfPartGood(constraintCoordinate, it->first->myLayers->m_test1.m_connections)) continue; qVector.erase(it++); } @@ -90,7 +92,7 @@ qualityVector AbstractionLayer_1::returnInBox(vector& PuzzleBox) int i=0; for(int col=1;colm_test1.m_connections=m_constraintMatrix[col][row].m_connections; } @@ -106,7 +108,7 @@ void AbstractionLayer_1::setEdgeZero() bool AbstractionLayer_1::PlaceOfPartGood(coor myCoor, uint8_t& myPart) { - uint8_t negativePart(0)=0b00000000; + uint8_t negativePart=0b00000000; negativePart or_eq (m_constraintMatrix[myCoor.col][myCoor.row+1].m_connections & 0b11000000); negativePart or_eq (m_constraintMatrix[myCoor.col-1][myCoor.row].m_connections & 0b00110000); diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h index dc04ead..55a87c3 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h @@ -5,14 +5,15 @@ #ifndef SOURCE_ABSTRACTIONLAYER_1_H #define SOURCE_ABSTRACTIONLAYER_1_H -#include "../AbstraktionLayer_Base.h" #include "AbstractionLayer_1_Properties.h" +#include "../AbstraktionLayer_Base.h" + #include #include #include #include -class AbstractionLayer_1 : public AbstraktionLayer_Base +class AbstractionLayer_1 : public AbstractionLayer_Base { public: void PreProcessing(const vector* partArray);//override diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1_Properties.h b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1_Properties.h index 86c3b77..5915ff7 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1_Properties.h +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1_Properties.h @@ -6,7 +6,6 @@ #define SOURCE_ABSTRACTIONLAYER_1_PROPERTIES_H #include -#include "AbstractionLayer_1.h" class AbstractionLayer_1_Properties { diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp new file mode 100644 index 0000000..58427be --- /dev/null +++ b/Source/functions/solve/puzzleExtension.cpp @@ -0,0 +1,11 @@ +// +// Created by Raphael Maenle on 21/12/2017. +// + +#include "../../header/solve.h" + + +void Puzzle::printPuzzle() {} +void Puzzle::putIntoBox() {}//puts a puzzlepiece back into its box +void Puzzle::shuffle() {}//shuffles the existing box in Puzzle +void Puzzle::removeConstrains(coor removeCoordinates) {}//deletes all constraints from all abstractionlayers diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 83997a1..8a10792 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -1,15 +1,14 @@ #include "../../header.h" - void status(vector& log, vector& p_Box); -bool next(vector& log, vector& p_Box) +bool next(vector& log, vector& p_Box,Puzzle& puzzleMat) { //last log element is set, create new log element or log not yet started if(!(log.size()) || log.back().isSet()) { if(!(p_Box.size())) return false; //puzzle solved - else createNextLogElement(log,p_Box); + else createNextLogElement(log,p_Box,puzzleMat); } //last log element is empty, backtrack else if(!(log.back().PieceCollector.size())) backtrack(log,p_Box,puzzleMat); @@ -25,30 +24,30 @@ bool next(vector& log, vector& p_Box) { if(log.back().hasRandomed()) { - if(log.back().abstractionLevel < MAX_ABSTRAX) + if(log.back().abstractionLevel < 2)//do 2 at least two best abstractions to check if part is okay { log.back().advance(); - solve(log,p_Box); + solve(log,p_Box,puzzleMat); } else - setsolution(log,p_Box); + setsolution(log,p_Box,puzzleMat); } else - setsolution(log,p_Box); + setsolution(log,p_Box,puzzleMat); } return true; } -void createNextLogElement(vector& log, vector& p_Box) +void createNextLogElement(vector& log, vector& p_Box, Puzzle& puzzleMat) { log.emplace_back(LogEntry()); - log.back().myCoor = calculateNextCoor(log, p_Box); + log.back().myCoor = calculateNextCoor(log, p_Box,puzzleMat); //getLayerDestructionPowerfromSurrounding(); - solve(log, p_Box); + solve(log, p_Box,puzzleMat); } -coor calculateNextCoor(vector& log, vector& p_Box) +coor calculateNextCoor(vector& log, vector& p_Box, Puzzle& puzzleMat) { //level 1: //go left to right, then increase current row @@ -57,25 +56,25 @@ coor calculateNextCoor(vector& log, vector& p_Box) return {0,0}; - int m= log.rbegin()[1].myCoor.col; - int n= log.rbegin()[1].myCoor.row; + unsigned int m= log.rbegin()[1].myCoor.col; + unsigned int n= log.rbegin()[1].myCoor.row; - if(m& log, vector& p_Box) +void solve(vector& log, vector& p_Box, Puzzle& puzzleMat) { //getNextHighestLayerworth(puzzleMat); //sets in abstractionLevel //status(log,p_Box,puzzleMat); switch(log.back().abstractionLevel) { case 1: - puzzleMat.AbstractionLayer_1solver.EvalueteQuality(log.back().PieceCollector); + puzzleMat.a1->EvaluateQuality(log.back().myCoor, log.back().PieceCollector); break; default: @@ -90,7 +89,7 @@ void solve(vector& log, vector& p_Box) } //removes from box and makes log "set" -void setsolution(vector& log, vector& p_Box) +void setsolution(vector& log, vector& p_Box, Puzzle& puzzleMat) { //advance number of randomed part count if(log.back().PieceCollector.size()>1) log.back().advanceRandomed(); @@ -106,7 +105,7 @@ void setsolution(vector& log, vector& p_Box) log.back().Set(); } -bool backtrack(vector& log, vector& p_Box, puzzleMat) +bool backtrack(vector& log, vector& p_Box, Puzzle& puzzleMat) { //if more pieces possible, take next piece if((log.back().PieceCollector.size())>1) @@ -124,15 +123,15 @@ bool backtrack(vector& log, vector& p_Box, puzzleMat) //else remove log element and backtrack once more else { - puzzleMat.removePiece(log.back().myCoor); //this should remove constraints from all layers - if(!(log.back().PieceCollector.size())) - p_Box.emplace_back(log.back().PieceCollector[0]); + puzzleMat.removeConstrains(log.back().myCoor); //this should remove constraints from all layers + if((log.back().PieceCollector.size())) + p_Box.emplace_back(log.back().PieceCollector.begin()->first); log.pop_back(); backtrack(log,p_Box,puzzleMat); } } -void status(vector& log, vector& p_Box, puzzleMat) +void status(vector& log, vector& p_Box, Puzzle& puzzleMat) { cout << "----------------------------" << endl; cout << "status:" << endl; @@ -151,18 +150,13 @@ void status(vector& log, vector& p_Box, puzzleMat) cout << endl; cout << "Box:" << endl; cout << "size: " << p_Box.size() << endl; - for(auto i:p_Box) - { - i->printPiece(); - cout << endl; - } cout << "Puzzle:" << endl; puzzleMat.printPuzzle(); cout << "----------------------------" << endl; } -void calculateTrueDestructionPower(vector& log, puzzleMat, float Layerworth) +void calculateTrueDestructionPower(vector& log, Puzzle& puzzleMat, float Layerworth) { //hier muss noch rein, wo die zeit der Abstractionlevels gespeichter wird float destructionPower=sqrt(Layerworth * log.back().abstractionLevel); diff --git a/Source/header.h b/Source/header.h index 0160c60..ad18244 100755 --- a/Source/header.h +++ b/Source/header.h @@ -1,3 +1,6 @@ +#ifndef HEADER_H +#define HEADER_H + #include #include #include @@ -6,14 +9,9 @@ #include #include -#define MAX_ABSTRAX 1 -#define structdebug - using namespace std; -/** - * IMPORTANT: Each new layer must be included there, to avoid compile errors - */ -#include "functions/AbstractionLayers/Layer1/AbstractionLayer_1.h" -#include "functions/AbstractionLayers/DestructionPower/DestructionPower.h" +#include "header/input.h" #include "header/solve.h" + +#endif //HEADER_H \ No newline at end of file diff --git a/Source/header/input.h b/Source/header/input.h index b83acbc..340b42a 100755 --- a/Source/header/input.h +++ b/Source/header/input.h @@ -1,7 +1,18 @@ +// +// Created by mpapa on 05.12.2017. +// + +#ifndef SOURCE_INPUT_H +#define SOURCE_INPUT_H + + #include #include "../functions/AbstractionLayers/Layer1/AbstractionLayer_1_Properties.h" #include "../functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h" + +class LayerContainer; + class Part { public: @@ -28,10 +39,19 @@ public: m_numOfRotations = numOfRotations; } - AbstractionLayer_1_Properties m_test1; - DestructionPower_Properties m_destruction; - + LayerContainer* myLayers; private: int32_t m_partID; uint8_t m_numOfRotations; }; + + +class coor +{ +public: + unsigned int col, row; + coor(unsigned int newcol=-1,unsigned int newrow=-1): col(newcol), row(newrow) + {} +}; + +#endif //SOURCE_INPUT_H diff --git a/Source/header/solve.h b/Source/header/solve.h index 034c193..037820b 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -1,3 +1,7 @@ +// +// Created by mpapa on 05.12.2017. +// + #ifndef SOURCE_SOLVE_H #define SOURCE_SOLVE_H @@ -6,18 +10,12 @@ #include #include -class Part; +#include "../functions/AbstractionLayers/Layer1/AbstractionLayer_1.h" +#include "../functions/AbstractionLayers/DestructionPower/DestructionPower.h" + using namespace std; -class coor -{ -public: - unsigned int col, row; - coor(int newcol=-1,int newrow=-1): col(newcol), row(newrow) - {} -}; - class LogEntry { public: @@ -34,7 +32,7 @@ public: LogEntry() { - myCoor = coor(); + myCoor = coor(0,0); abstractionLevel=0; set=0; } @@ -43,11 +41,47 @@ private: static int randomed; }; +class Puzzle +{ +public: + Puzzle(unsigned int newcols,unsigned int newrows):rows(newrows),cols(newcols){} + + coor getSizeAsCoor() + {return {cols,rows};} + + DestructionPower* dp; + AbstractionLayer_1* a1; + + void removeConstrains(coor removeCoordinates); + void printPuzzle(); + + void createRandomBox(){createRandomPuzzle();putIntoBox();shuffle();} + void createRandomPuzzle(){a1->CreateRandomPuzzle();} + void putIntoBox(); + void shuffle(); + + vector myBox; + vector p_myBox; + +private: + + unsigned int rows; + unsigned int cols; +}; + +class LayerContainer +{ +public: + AbstractionLayer_1_Properties m_test1; + DestructionPower_Properties m_destruction; +}; + + bool next(vector& log, vector& p_Box, Puzzle& puzzleMat); coor calculateNextCoor(vector& log, vector& p_Box, Puzzle& puzzleMat); void solve(vector& log, vector& p_Box, Puzzle& puzzleMat); void setsolution(vector& log, vector& p_Box, Puzzle& puzzleMat); -bool backtrack(vector& log, vector& p_Box, puzzleMat); +bool backtrack(vector& log, vector& p_Box, Puzzle& puzzleMat); void createNextLogElement(vector& log, vector& p_Box, Puzzle& puzzleMat); diff --git a/Source/main.cpp b/Source/main.cpp index e50a271..d80bc1c 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -1,8 +1,6 @@ #include "header.h" -#include "header/input.h" int LogEntry::randomed(0); -unsigned int PuzzlePiece::idcount(0); int main() { @@ -10,21 +8,17 @@ int main() unsigned int cols=5, rows=6; //some basic random puzzle stuff - vector myFirstBox = createBox(coor(cols,rows)); + Puzzle myFirstPuzzle(cols,rows); + myFirstPuzzle.createRandomBox(); //some advanced solver stuff vector log; vector p_myFirstBox; - //BoxClassify myFirstBox(); - cout << endl; - for(auto &i:myFirstBox) - p_myFirstBox.push_back(&i); - Puzzle puzzleMat(cols, rows); //vector> ab1class = abstractionLayer1classify(log, p_myFirstBox,puzzleMat); - while(next(log, p_myFirstBox,puzzleMat)); + while(next(log, puzzleMat.p_myBox,puzzleMat)); puzzleMat.printPuzzle(); }