From fc9743a77afd308343a5878cebbf94cb3c7dc133 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Sun, 7 Jan 2018 14:16:57 +0100 Subject: [PATCH] dispatcher funktioniert halb, es fehlt noch 'setflag' und average quality calculation --- .../Layer1/AbstractionLayer_1.cpp | 11 +- Source/functions/solve/puzzleExtension.cpp | 5 + Source/functions/solve/structure.cpp | 150 ++++++++---------- Source/header/solve.h | 14 +- Source/main.cpp | 2 +- 5 files changed, 89 insertions(+), 93 deletions(-) diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp index a57cbe5..3697ff9 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp @@ -44,16 +44,15 @@ bool AbstractionLayer_1::PreProcessing(coor mySize, const vector* partAr //it through qualityVector and removes all that do not trigger PlaceOfPartGood bool AbstractionLayer_1::EvaluateQuality (const coor constraintCoordinate, qualityVector& qVector) { - int j=0; for(int i = 0;iGetPartID() << ", rotations: " << (int)qVector[i].second->GetNumOfRotations() << endl; - qVector[i].second->print(); if(PlaceOfPartGood(constraintCoordinate, qVector[i].second->m_a1.m_connections)) + { + qVector[i].first=1; + continue; - qVector.erase(qVector.begin()+(i--)); cout << endl << endl; + } + qVector[i].first=0; } } diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index 177b1c2..e25a6c9 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -87,3 +87,8 @@ void Puzzle::createBox(){ } } + +bool Puzzle::allSet() { + //TODO! check if all puzzlepieces are set + return false; +} diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 5015457..42ab365 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -1,28 +1,28 @@ #include "../../header.h" void status(vector& log, vector& p_Box); -bool SetBestOrMoreLayersArithmetical(vector& log, qualityVector* cqVector); +bool SetBestOrMoreLayersArithmetical(vector& log, qualityVector& cqVector); void calculateTrueDestructionPower(vector& log, Puzzle& puzzleMat, float Layerworth); void sort(vector& log); -void cut(vector& log, Part* cutID); +void cut(vector& log, int& cutID); float capLogElements(vector& log); -void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, qualityVector* cqVector); +void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, qualityVector& cqVector); -bool next(vector& log, vector& p_Box,Puzzle& puzzleMat) +bool next(vector& log,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,puzzleMat); + if((puzzleMat.allSet())) return false; //puzzle solved + else createNextLogElement(log,puzzleMat); } //last log element is empty, backtrack - else if(!(log.back().PieceCollector.size())) backtrack(log,p_Box,puzzleMat); + else if(!(log.back().PieceCollector.size())) backtrack(log,puzzleMat); //case last log element has multiple entries else if(log.back().PieceCollector.size() > 1) { //moreLayers is 0, setbest is 1 - if (SetBestOrMoreLayersArithmetical(log, &puzzleMat.combinedQualityVector)) setsolution(log, p_Box, puzzleMat); - else solve(log, p_Box, puzzleMat); + if (SetBestOrMoreLayersArithmetical(log, puzzleMat.combinedQualityVector)) setsolution(log, puzzleMat); + else solve(log, puzzleMat); } //case last log exactly one solution else if(log.back().PieceCollector.size() == 1) @@ -32,31 +32,31 @@ bool next(vector& log, vector& p_Box,Puzzle& puzzleMat) 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,puzzleMat); + solve(log,puzzleMat); } else - setsolution(log,p_Box,puzzleMat); + setsolution(log,puzzleMat); } else - setsolution(log,p_Box,puzzleMat); + setsolution(log,puzzleMat); } return true; } -void createNextLogElement(vector& log, vector& p_Box, Puzzle& puzzleMat) +void createNextLogElement(vector& log, Puzzle& puzzleMat) { log.emplace_back(LogEntry(coor(0, 0))); - log.back().myCoor = calculateNextCoor(log, p_Box,puzzleMat); + log.back().myCoor = calculateNextCoor(log, puzzleMat); puzzleMat.dp.DestructionOfSurrounding(log.back().myCoor);//calculate dp from surrounding - for(auto it:p_Box) + for(auto it:puzzleMat.p_myBox) log.back().PieceCollector.emplace_back(pair(0,it)); - cout << p_Box.size() << endl; + cout << puzzleMat.p_myBox.size() << endl; cout << log.back().PieceCollector.size() << endl; - solve(log, p_Box,puzzleMat); + solve(log,puzzleMat); } -coor calculateNextCoor(vector& log, vector& p_Box, Puzzle& puzzleMat) +coor calculateNextCoor(vector& log, Puzzle& puzzleMat) { //level 1: //go left to right, then increase current row @@ -88,23 +88,22 @@ void solve(vector& log, vector& p_Box, Puzzle& puzzleMat) break; } - capLogElements(log); float worth = capLogElements(log); calculateTrueDestructionPower(log,puzzleMat, worth); - CalculateNewCombinedQuality(log, log.back().PieceCollector, &puzzleMat.combinedQualityVector); + CalculateNewCombinedQuality(log, log.back().PieceCollector, puzzleMat.combinedQualityVector); } //removes from box and makes log "set" -void setsolution(vector& log, vector& p_Box, Puzzle& puzzleMat) +void setsolution(vector& log, Puzzle& puzzleMat) { //advance number of randomed part count if(log.back().PieceCollector.size()>1) log.back().advanceRandomed(); //remove first element in last logelement from box - for(int i=0;isecond)//mach ich das richtig so?! - p_Box.erase(p_Box.begin()+i); + for(int i=0;isecond)//mach ich das richtig so?! + puzzleMat.p_myBox.erase(puzzleMat.p_myBox.begin()+i); else i++; @@ -112,18 +111,18 @@ void setsolution(vector& log, vector& p_Box, Puzzle& puzzleMat) log.back().Set(); } -bool backtrack(vector& log, vector& p_Box, Puzzle& puzzleMat) +bool backtrack(vector& log, Puzzle& puzzleMat) { //if more pieces possible, take next piece if((log.back().PieceCollector.size())>1) { - p_Box.push_back(log.back().PieceCollector.begin()->second); + puzzleMat.p_myBox.push_back(log.back().PieceCollector.begin()->second); log.back().PieceCollector.erase(log.back().PieceCollector.begin()); if(log.back().PieceCollector.size()==1) log.back().decreaseRandomed(); - setsolution(log,p_Box,puzzleMat); + setsolution(log,puzzleMat); return true; } @@ -132,13 +131,13 @@ bool backtrack(vector& log, vector& p_Box, Puzzle& puzzleMat) { 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()->second); + puzzleMat.p_myBox.emplace_back(log.back().PieceCollector.begin()->second); log.pop_back(); - backtrack(log,p_Box,puzzleMat); + backtrack(log,puzzleMat); } } -void status(vector& log, vector& p_Box, Puzzle& puzzleMat) +void status(vector& log, Puzzle& puzzleMat) { cout << "----------------------------" << endl; cout << "status:" << endl; @@ -156,7 +155,7 @@ void status(vector& log, vector& p_Box, Puzzle& puzzleMat) cout << endl; cout << "Box:" << endl; - cout << "size: " << p_Box.size() << endl; + cout << "size: " << puzzleMat.p_myBox.size() << endl; cout << "Puzzle:" << endl; puzzleMat.printPuzzle(); @@ -177,86 +176,77 @@ float capLogElements(vector& log) // Till Now only ground structure -> incorrect variable ans vector names double limit = 0.6; double diff = 0; + + int id=0; + double maxdiff = 0; int vectorsizeBefore = 0; int vectorsizeAfter = 0; double destroyed = 0; // destroyed parts in % - double worth = 0; vectorsizeBefore = log.back().PieceCollector.size(); - sort(log); // Sort the vector after probabilities - qualityVector::const_iterator idxcut =log.back().PieceCollector.begin(); - for(;idxcut !=log.back().PieceCollector.end();idxcut++) - if(idxcut->first < limit) - break; - - - auto newidxcut = idxcut; - - - while(idxcut != log.back().PieceCollector.end()) + sort(log.back().PieceCollector.begin(),log.back().PieceCollector.end()); // Sort the vector after probabilities + reverse(log.back().PieceCollector.begin(),log.back().PieceCollector.end()); + for(;idsecond - (++idxcut)->second; + if(log.back().PieceCollector[id].first < limit) + break; + } + + + int newid = --id; //set to the one just over limit + + + while(id maxdiff) { maxdiff = diff; - newidxcut = idxcut; + newid = id; } } - cut(log,newidxcut->second); + cut(log,newid); vectorsizeAfter = log.back().PieceCollector.size(); - - destroyed = (vectorsizeBefore - vectorsizeAfter) / vectorsizeBefore; - + destroyed = ((double)vectorsizeBefore - (double)vectorsizeAfter) / (double)vectorsizeBefore; return (float)sqrt(destroyed*maxdiff); } -void sort(vector& log) -{ - //this does all the sorting that needs to happen - //saddly this is a false statement - //the monkey desperately tried to hold on to the flying dorm room. -} - qualityVector::iterator FindPartInLog(vector& log, Part* wishedPartPointer) { qualityVector::iterator partOnPositionIterator = log.back().PieceCollector.begin(); while (partOnPositionIterator != log.back().PieceCollector.end()) { - if(partOnPositionIterator.base()->second == wishedPartPointer) - { + if(partOnPositionIterator->second == wishedPartPointer) break; - } else - { partOnPositionIterator++; - } } return partOnPositionIterator; } -void cut(vector& log, Part* cutID) +void cut(vector& log, int& cutID) { - auto it = FindPartInLog(log, cutID); - while(it != log.back().PieceCollector.end()) - log.back().PieceCollector.erase(it++); + while(cutID& log, qualityVector* cqVector) +bool SetBestOrMoreLayersArithmetical(vector& log, qualityVector& cqVector) { float threshold = 1.0, tempBest = 0.0; unsigned int countHigherThreshold = 0; - if(cqVector->empty()) + if(cqVector.empty()) { cerr << "combinedQualityVector is empty." << endl; // should not be empty => backtrack? return false; // Warning: can only return true or false. What return for error? @@ -274,18 +264,18 @@ bool SetBestOrMoreLayersArithmetical(vector& log, qualityVector* cqVec } // check Quality of current Puzzle Piece in combinedQualityVector with Threshold value - for (qualityVector::iterator it = cqVector->begin(); it != cqVector->end(); it++) + for (qualityVector::iterator it = cqVector.begin(); it != cqVector.end(); it++) { - if ((cqVector->back().first / log.back().abstractionLevel) >= threshold) // const threshold values + if ((cqVector.back().first / log.back().abstractionLevel) >= threshold) // const threshold values { // count how many Pieces are greater than the threshold value countHigherThreshold++; } else { - if ((cqVector->back().first / log.back().abstractionLevel) > tempBest) + if ((cqVector.back().first / log.back().abstractionLevel) > tempBest) { - tempBest = cqVector->back().first; // could be used, for additional constraints + tempBest = cqVector.back().first; // could be used, for additional constraints } } } @@ -304,7 +294,7 @@ bool SetBestOrMoreLayersArithmetical(vector& log, qualityVector* cqVec // jede Quality vom Piececollector zu einer combinedQuality aufsummieren (von jedem bereits verwendetem Layer) // Achtung: Es muss noch der Mittelwert gebildet werden => SetBestOrMoreLayersArithmetical -void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, qualityVector* cqVector) +void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, qualityVector& cqVector) { bool summarizedVectors = false; int countSummarizedVectors = 0; @@ -315,24 +305,24 @@ void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, cerr << "qualityVector is empty." << endl; // should not be empty => backtrack? return; } - else if(cqVector->empty()) + else if(cqVector.empty()) { cerr << "combinedQualityVector is empty." << endl; // should not be empty => backtrack? return; } else { - for (unsigned int i = 0; i < cqVector->size(); i++) + for (unsigned int i = 0; i < cqVector.size(); i++) { summarizedVectors = false; for (unsigned int j = 0; j < qVector.size(); j++) { // search same PuzzlePart of qualityVector and combinedQualityVector - if (&cqVector->at(i).second == &qVector.at(j).second) + if (&cqVector.at(i).second == &qVector.at(j).second) { // sum Quality of PieceCollector (qualityVector) to combinedQualityVector - cqVector->at(j).first += qVector.at(i).first; + cqVector.at(j).first += qVector.at(i).first; countSummarizedVectors++; summarizedVectors = true; continue; // skip remaining for loop => save time! @@ -346,16 +336,16 @@ void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, //cqVector->erase(cqVector->begin()+i); // efficient way, but no sorted cqVector => wayne - swap(cqVector->at(i), cqVector->back()); - cqVector->pop_back(); + swap(cqVector.at(i), cqVector.back()); + cqVector.pop_back(); } } // cqVector should have the same size now as newest qVector - if (cqVector->size() != qVector.size()) + if (cqVector.size() != qVector.size()) { cerr << "Size of combinedQualityVector doenst match with size of qualityVector!" << endl; - cout << "Size of combinedQualityVector: " << cqVector->size() << endl; + cout << "Size of combinedQualityVector: " << cqVector.size() << endl; cout << "Size of qualityVector: " << qVector.size() << endl; cout << "Size of countSummarizedVectors: " << countSummarizedVectors << endl; } diff --git a/Source/header/solve.h b/Source/header/solve.h index a93c896..1f3ba4b 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -65,6 +65,8 @@ public: void createBox(); void createp_box(); + bool allSet(); + vector myBox; vector p_myBox; @@ -76,11 +78,11 @@ private: unsigned int cols; }; -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, Puzzle& puzzleMat); +bool next(vector& log,Puzzle& puzzleMat); +coor calculateNextCoor(vector& log, Puzzle& puzzleMat); +void solve(vector& log, Puzzle& puzzleMat); +void setsolution(vector& log, Puzzle& puzzleMat); +bool backtrack(vector& log,Puzzle& puzzleMat); -void createNextLogElement(vector& log, vector& p_Box, Puzzle& puzzleMat); +void createNextLogElement(vector& log,Puzzle& puzzleMat); diff --git a/Source/main.cpp b/Source/main.cpp index 16cf718..9a14e61 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -18,7 +18,7 @@ int main() //puzzleMat.createRandomBox(); puzzleMat.a1.printConstraintMatrix(); - while(next(log, puzzleMat.p_myBox,puzzleMat)); + while(next(log, puzzleMat)); puzzleMat.printPuzzle(); return 0;