From 9d98ed83fac216daf27fea8045b2e9c492ce8755 Mon Sep 17 00:00:00 2001 From: d-kaselautzke <33751201+d-kaselautzke@users.noreply.github.com> Date: Fri, 5 Jan 2018 01:18:23 +0100 Subject: [PATCH] Added SetBestOrMoreLayersArithmetical and CalculateNewCombinedQuality --- Source/functions/solve/structure.cpp | 167 ++++++++++++++++----------- Source/header/solve.h | 2 + 2 files changed, 101 insertions(+), 68 deletions(-) diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 08b555a..c99ac79 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -1,11 +1,11 @@ #include "../../header.h" void status(vector& log, vector& p_Box); -bool setBestOrMoreLayers(vector& log); +bool SetBestOrMoreLayersArithmetical(vector& log, qualityVector* cqVector); void calculateTrueDestructionPower(vector& log, Puzzle& puzzleMat, float Layerworth); void sort(vector& log); void cut(vector& log, Part* cutID); float capLogElements(vector& log); -void calculateNewCombinedProbabilityForPuzzlePiecesArithmetic(vector& log); +void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, qualityVector* cqVector); bool next(vector& log, vector& p_Box,Puzzle& puzzleMat) { @@ -21,7 +21,7 @@ bool next(vector& log, vector& p_Box,Puzzle& puzzleMat) else if(log.back().PieceCollector.size() > 1) { //moreLayers is 0, setbest is 1 - if (setBestOrMoreLayers(log)) setsolution(log, p_Box, puzzleMat); + if (SetBestOrMoreLayersArithmetical(log, &puzzleMat.combinedQualityVector)) setsolution(log, p_Box, puzzleMat); else solve(log, p_Box, puzzleMat); } //case last log exactly one solution @@ -87,7 +87,7 @@ void solve(vector& log, vector& p_Box, Puzzle& puzzleMat) capLogElements(log); float worth = capLogElements(log); calculateTrueDestructionPower(log,puzzleMat, worth); - calculateNewCombinedProbabilityForPuzzlePiecesArithmetic(log); + CalculateNewCombinedQuality(log, log.back().PieceCollector, &puzzleMat.combinedQualityVector); } @@ -244,85 +244,116 @@ void cut(vector& log, Part* cutID) log.back().PieceCollector.erase(it++); } -//partdavid -bool setBestOrMoreLayers(vector& log) +// -------------------- Part David: SetBest and CalculateCombinedQuality -------------------- +// pruefen, ob mehr als X combinedQualities ueber dem Grenzwert sind. Wenn nur noch Y Pieces ueber bleiben, dann setBest! +// geeignete Threshold values muessen noch getestet werden +bool SetBestOrMoreLayersArithmetical(vector& log, qualityVector* cqVector) { - int countBest = 0; - float tempBest = 0.0; + float threshold = 1.0, tempBest = 0.0; + unsigned int countHigherThreshold = 0; - // count how many Pieces are greater than the threshold value - for(auto it:log.back().PieceCollector) + if(cqVector->empty()) { - // check Probability of current Puzzle Piece in this vector - if (it.first >= 0.90) // 0.90 as threshold - countBest++; - else - if (it.first > tempBest) - tempBest = it.first; + cerr << "combinedQualityVector is empty." << endl; // should not be empty => backtrack? + return false; // Warning: can only return true or false. What return for error? } - - // return true if only one piece is left - if (1 == countBest) - { - return true; - } - //else if (countBest > 1 && countBest < 10) // TODO: add possible constraints else { - return false; - } -} - -void calculateNewCombinedProbabilityForPuzzlePiecesArithmetic(vector& log) -{ - float totalValue = 0.0; - int i; - for(int i; i < log.back().PieceCollector.size(); i++); - - // sum Probability of current Puzzle Piece in PieceCollector vector - //totalValue += *(log.back().PieceCollector.); - - //return totalValue / i; -} - -/* -//PartDavid -void calculateNewCombinedProbabilityForPuzzlePiecesTopK(vector& log, int executedLayers) -{ - float TopK[executedLayers][2] = {0.0}; // in Log speichern? - float sumTopK[executedLayers] = {0.0}; - float HighestProbability = 0.0; - - // searching for Top2 probability values in PieceCollector for each layer - for (int currentLayer = 0; currentLayer < executedLayers; currentLayer++) - { - // searching for Top2 probabilities in currentLayer - for(int i = 0; i < log.back().PieceCollector.size() && log.back().abstractionLevel == currentLayer; i++) + switch(log.back().abstractionLevel) { - if (*(log.back().PieceCollector[i]) > TopK[currentLayer][0]) + case 1: threshold = 0.90; break; + case 2: threshold = 0.80; break; + case 3: threshold = 0.75; break; + case 4: threshold = 0.66; break; + case 5: threshold = 0.60; break; + default: threshold = 0.5; break; + } + + // check Quality of current Puzzle Piece in combinedQualityVector with Threshold value + for (qualityVector::iterator it = cqVector->begin(); it != cqVector->end(); it++) + { + if ((cqVector->back().first / log.back().abstractionLevel) >= threshold) // const threshold values { - TopK[currentLayer][0] = *log.back().PieceCollector[i]; - } - else if (*(log.back().PieceCollector[i]) > TopK[currentLayer][1]) - { - TopK[currentLayer][1] = *log.back().PieceCollector[i]; + // count how many Pieces are greater than the threshold value + countHigherThreshold++; } else { - // Spezialfall fuer 0 Ueberlegen + if ((cqVector->back().first / log.back().abstractionLevel) > tempBest) + { + tempBest = cqVector->back().first; // could be used, for additional constraints + } } } - sumTopK[currentLayer] = TopK[currentLayer][0] + TopK[currentLayer][1]; - } - // searching for highest probability for designated Position - for (int currentLayer = 0; currentLayer < executedLayers; currentLayer++) - { - if (sumTopK[currentLayer+1] > sumTopK[currentLayer]) + // return true if only one piece is left + if (1 == countHigherThreshold) { - HighestProbability = sumTopK[currentLayer+1]; + return true; + } + else + { + return false; } } - } +} -*/ \ No newline at end of file +// 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) +{ + bool summarizedVectors = false; + int countSummarizedVectors = 0; + + // check if both qualityVectors are not empty + if(qVector.empty()) + { + cerr << "qualityVector is empty." << endl; // should not be empty => backtrack? + return; + } + 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++) + { + 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) + { + // sum Quality of PieceCollector (qualityVector) to combinedQualityVector + cqVector->at(j).first += qVector.at(i).first; + countSummarizedVectors++; + summarizedVectors = true; + continue; // skip remaining for loop => save time! + } + } + + // remove element at poisition X in combinedQualityVector, because it was not summarized + if (!summarizedVectors) + { + // inefficient way to delete element X + //cqVector->erase(cqVector->begin()+i); + + // efficient way, but no sorted cqVector => wayne + swap(cqVector->at(i), cqVector->back()); + cqVector->pop_back(); + } + } + + // cqVector should have the same size now as newest qVector + 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 qualityVector: " << qVector.size() << endl; + cout << "Size of countSummarizedVectors: " << countSummarizedVectors << endl; + } + } +} \ No newline at end of file diff --git a/Source/header/solve.h b/Source/header/solve.h index f6c7a4c..5105cc8 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -65,6 +65,8 @@ public: vector myBox; vector p_myBox; + qualityVector combinedQualityVector; + private: unsigned int rows;