added basic dP calculation, removed deprication warnings
Changed for loops to auto range, changed returns to bool literals, changed return of constructor to more basic call. added dP calculation, save of the per piece runtime is still missing in puzzlebox or constraint matrix.
This commit is contained in:
parent
ccf26f8d8a
commit
2ef3f164c7
@ -34,26 +34,6 @@ void PuzzlePiece::randomCenterPiece()
|
||||
setConnections(getConnections() | 0b00000010);
|
||||
}
|
||||
|
||||
//tries all pieces in box from separator to end and places fitting into matrix. removes fitting piece
|
||||
//use separator if you have to retract to a position
|
||||
//seperator may be bigger than box size, if all puzzle pieces have already been looked at.
|
||||
// it immediately retracts again then (returns -1)
|
||||
unsigned int Puzzle::tryAllPieces(coor myCoor, vector<PuzzlePiece>& myBox, unsigned int separator)
|
||||
{
|
||||
for(int i=separator; i<myBox.size();i++)
|
||||
{
|
||||
if(testRotationPiece(myCoor,myBox[i]))
|
||||
{
|
||||
//setPiece(myCoor.m,myCoor.n,myBox[i]);
|
||||
setPiece(myCoor,myBox[i]);
|
||||
myBox.erase(myBox.begin()+i);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
//tests the myPart in all 4 rotations at position m, n
|
||||
bool Puzzle::testRotationPiece(coor myCoor, PuzzlePiece& myPart, int nrOfRotations)
|
||||
{
|
||||
@ -61,13 +41,13 @@ bool Puzzle::testRotationPiece(coor myCoor, PuzzlePiece& myPart, int nrOfRotatio
|
||||
{
|
||||
//coor myCoor(m,n);
|
||||
if(PlaceOfPartGood(myCoor,myPart))
|
||||
return 1;
|
||||
return true;
|
||||
//cout << "was rotated in testRotationPiece" << endl;
|
||||
myPart.shift(1);
|
||||
}
|
||||
|
||||
//cout << "Was a bad part" << endl;
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
//insterts piece at position in box according to boxidentifier and removes piece from puzzle
|
||||
@ -129,13 +109,13 @@ bool Puzzle::PlaceOfPartGood(coor myCoor, PuzzlePiece& myPart)
|
||||
//cout << "good Part: ";
|
||||
//myPart.printPiece();
|
||||
//cout << endl;
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
//cout << "bad Part: ";
|
||||
//myPart.printPiece();
|
||||
//cout << endl;
|
||||
|
||||
return 0;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
@ -169,9 +149,9 @@ bool Puzzle::PlaceOfPart2Good(coor myCoor, PuzzlePiece& myPart)
|
||||
//check tmp part with environment
|
||||
if(((negativePart.getConnections() & 0b11000000) == (tmpPuzzlePiece.getConnections() & 0b11000000)) && ((negativePart.getConnections() & 0b00110000) == (tmpPuzzlePiece.getConnections ()& 0b00110000)) &&
|
||||
((negativePart.getConnections() & 0b00001100) == (tmpPuzzlePiece.getConnections() & 0b00001100)) && ((negativePart.getConnections() & 0b00000011) == (tmpPuzzlePiece.getConnections() & 0b00000011)))
|
||||
return 1;
|
||||
return true;
|
||||
|
||||
return 0;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
@ -278,9 +258,9 @@ void randomBox::putAllIntoBox() {
|
||||
void randomBox::printBox()
|
||||
{
|
||||
shuffle();
|
||||
for (vector<PuzzlePiece>::iterator i = Box.begin(); i != Box.end(); i++)
|
||||
for (auto i:Box)
|
||||
{
|
||||
(*i).printPiece();
|
||||
i.printPiece();
|
||||
cout << ' ';
|
||||
}
|
||||
cout << endl;
|
||||
@ -290,10 +270,10 @@ void randomBox::printBox()
|
||||
vector<PuzzlePiece> randomBox::shuffle()
|
||||
{
|
||||
random_shuffle(Box.begin(),Box.end());
|
||||
for (vector<PuzzlePiece>::iterator i = Box.begin(); i != Box.end(); i++)
|
||||
for (auto &i:Box)
|
||||
{
|
||||
i->shift(rand()%4);
|
||||
i->resetShift();
|
||||
i.shift(rand()%4);
|
||||
i.resetShift();
|
||||
}
|
||||
|
||||
numerateBox(Box);
|
||||
@ -303,13 +283,6 @@ vector<PuzzlePiece> randomBox::shuffle()
|
||||
//creates a random box size m, n, shuffles it, and then retuns it
|
||||
vector<PuzzlePiece> createBox(coor myCoor)
|
||||
{
|
||||
/*
|
||||
<<<<<<< HEAD
|
||||
randomBox myFirstPuzzleBox(myCoor.m, myCoor.n);
|
||||
myFirstPuzzleBox.createRandomPuzzle();
|
||||
return myFirstPuzzleBox.shuffle();
|
||||
=======
|
||||
*/
|
||||
randomBox myFirstPuzzleBox(myCoor.m,myCoor.n);
|
||||
myFirstPuzzleBox.createRandomAbstraction1();
|
||||
myFirstPuzzleBox.createRandomAbstraction2();
|
||||
@ -317,20 +290,18 @@ vector<PuzzlePiece> createBox(coor myCoor)
|
||||
|
||||
myFirstPuzzleBox.printPuzzle();
|
||||
return myFirstPuzzleBox.shuffle();
|
||||
//>>>>>>> 9b282e83caf9aaacea107f878d2d6b3f413f286b
|
||||
}
|
||||
|
||||
//prints contents of box
|
||||
void printBox(vector<PuzzlePiece> myBox)
|
||||
{
|
||||
cout << "current Box: " << endl;
|
||||
for (vector<PuzzlePiece>::iterator i = myBox.begin(); i != myBox.end(); i++)
|
||||
for (auto &i:myBox)
|
||||
{
|
||||
(*i).printPiece();
|
||||
i.printPiece();
|
||||
cout << ' ';
|
||||
}
|
||||
cout << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
//gives every element in box a box identifier.
|
||||
@ -339,16 +310,15 @@ void numerateBox(vector<PuzzlePiece>& myBox)
|
||||
for(int i = 0; i< myBox.size();i++)
|
||||
myBox[i].setBoxIdentifier(i);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<PuzzlePiece> convertPart2PuzzlePiece(std::vector<Part> simplePartBox)
|
||||
{
|
||||
std::vector<PuzzlePiece> advancedPartBox;
|
||||
for(int i=0;i<simplePartBox.size();i++)
|
||||
for(auto const &i:simplePartBox)
|
||||
{
|
||||
PuzzlePiece tmpNewPiece(0);
|
||||
tmpNewPiece.setConnections(simplePartBox[i].getConnections());
|
||||
tmpNewPiece.setConnections(i.getConnections());
|
||||
advancedPartBox.push_back(tmpNewPiece);
|
||||
}
|
||||
return advancedPartBox;
|
||||
|
@ -8,8 +8,8 @@ bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& 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 0; //puzzle solved
|
||||
else createNextLogElement();
|
||||
if(!(p_Box.size())) return false; //puzzle solved
|
||||
else createNextLogElement(log,p_Box,puzzleMat);
|
||||
}
|
||||
//last log element is empty, backtrack
|
||||
else if(!(log.back().PieceCollector.size())) backtrack(log,p_Box,puzzleMat);
|
||||
@ -33,15 +33,15 @@ bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
||||
else
|
||||
setsolution(log,p_Box,puzzleMat);
|
||||
}
|
||||
else
|
||||
else
|
||||
setsolution(log,p_Box,puzzleMat);
|
||||
}
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
void createNextLogElement(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
||||
{
|
||||
log.push_back(LogEntry());
|
||||
log.emplace_back(LogEntry());
|
||||
log.back().myCoor = calculateNextCoor(log, p_Box, puzzleMat);
|
||||
getLayerDestructionPowerfromSurrounding();
|
||||
solve(log, p_Box,puzzleMat);
|
||||
@ -54,7 +54,7 @@ coor calculateNextCoor(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzl
|
||||
//go left to right, then increase current row
|
||||
|
||||
if (log.size() == 1)
|
||||
return coor(0,0);
|
||||
return {0,0};
|
||||
|
||||
|
||||
int m= log.rbegin()[1].myCoor.m;
|
||||
@ -63,8 +63,8 @@ coor calculateNextCoor(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzl
|
||||
|
||||
if(m<puzzleMat.getCols()-1) m++;
|
||||
else if(n<puzzleMat.getRows()-1){ m=0; n++;}
|
||||
else return coor();
|
||||
return coor(m,n);
|
||||
else return {};
|
||||
return {m,n};
|
||||
//return nextCoor;
|
||||
}
|
||||
|
||||
@ -93,8 +93,8 @@ void solve(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat
|
||||
void abstractionlayer0solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
||||
{
|
||||
//throw all remaining puzzle pieces into newest log
|
||||
for(int i=0;i<p_Box.size();i++)
|
||||
log.back().PieceCollector.push_back(p_Box[i]);
|
||||
for(auto i:p_Box)
|
||||
log.back().PieceCollector.push_back(i);
|
||||
}
|
||||
|
||||
void abstractionlayer1solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
||||
@ -152,7 +152,7 @@ bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzl
|
||||
puzzleMat.removePiece(log.back().myCoor);
|
||||
log.pop_back();
|
||||
backtrack(log,p_Box,puzzleMat);
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
//last log entry only one solution - delete last logd put back into box + backtrack
|
||||
@ -167,20 +167,22 @@ bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzl
|
||||
if(puzzleMat.testRotationPiece(log.back().myCoor, *(log.back().PieceCollector[0]), 1))
|
||||
{
|
||||
setsolution(log,p_Box,puzzleMat);
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
p_Box.push_back(log.back().PieceCollector[0]);
|
||||
//shuffleup
|
||||
random_shuffle(p_Box.begin(),p_Box.end());
|
||||
std::random_device rd;
|
||||
std::mt19937 g(rd());
|
||||
std::shuffle(p_Box.begin(),p_Box.end(),g);
|
||||
puzzleMat.removePiece(log.back().myCoor);
|
||||
log.pop_back();
|
||||
//cout << "removed" << endl;
|
||||
//status(log,p_Box,puzzleMat);
|
||||
backtrack(log,p_Box,puzzleMat);
|
||||
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
//last log entry multiple solutions (and current one was randomed) - delete randomed piece and go to next
|
||||
@ -196,13 +198,15 @@ bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzl
|
||||
if(puzzleMat.testRotationPiece(log.back().myCoor, *(log.back().PieceCollector[0]), 1))
|
||||
{
|
||||
setsolution(log,p_Box,puzzleMat);
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
p_Box.push_back(log.back().PieceCollector[0]);
|
||||
//shuffleup
|
||||
random_shuffle(p_Box.begin(),p_Box.end());
|
||||
std::random_device rd;
|
||||
std::mt19937 g(rd());
|
||||
std::shuffle(p_Box.begin(),p_Box.end(),g);
|
||||
log.back().PieceCollector.erase(log.back().PieceCollector.begin());
|
||||
|
||||
if(log.back().PieceCollector.size()==1)
|
||||
@ -212,11 +216,11 @@ bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzl
|
||||
(*(log.back().PieceCollector[0])).resetShift();
|
||||
setsolution(log,p_Box,puzzleMat);
|
||||
|
||||
return 1;
|
||||
//no need to remove from puzzle mat, as sersolution overwrites it anyway
|
||||
return true;
|
||||
//no need to remove from puzzle mat, as setsolution overwrites it anyway
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
@ -233,26 +237,26 @@ void status(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMa
|
||||
cout << "isset: 1" << endl;
|
||||
else
|
||||
cout << "isset: 0" << endl;
|
||||
|
||||
//cout << "Abstraction: " << log[i].abstractionLevel << endl;
|
||||
cout << "m: " << log[i].myCoor.m << " n: " << log[i].myCoor.n << endl;
|
||||
/*for(int j=0;j<log[i].PieceCollector.size();j++)
|
||||
{
|
||||
(*(log[i].PieceCollector[j])).printPiece();
|
||||
cout << endl;
|
||||
}*/
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
cout << "Box:" << endl;
|
||||
cout << "size: " << p_Box.size() << endl;
|
||||
for(vector<PuzzlePiece*>::iterator i = p_Box.begin();i!=p_Box.end();i++)
|
||||
for(auto i:p_Box)
|
||||
{
|
||||
(*(*i)).printPiece();
|
||||
i->printPiece();
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
cout << "Puzzle:" << endl;
|
||||
puzzleMat.printPuzzle();
|
||||
cout << "----------------------------" << endl;
|
||||
}
|
||||
|
||||
void calculateTrueDestructionPower(vector<LogEntry>& log, Puzzle& puzzleMat, float Layerworth)
|
||||
{
|
||||
//hier muss noch rein, wo die zeit der Abstractionlevels gespeichter wird
|
||||
float destructionPower=sqrt(Layerworth * log.back().abstractionLevel);
|
||||
puzzleMat.setdestructionPower(log.back().myCoor,log.back().abstractionLevel,destructionPower);
|
||||
}
|
@ -3,7 +3,8 @@
|
||||
#include <cstdlib>
|
||||
#include <time.h>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <algorithm>
|
||||
#include <random>
|
||||
|
||||
#define MAX_ABSTRAX 1
|
||||
#define structdebug
|
||||
|
@ -176,4 +176,4 @@ void abstractionlayer1solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box,
|
||||
void setsolution(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat);
|
||||
bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat);
|
||||
|
||||
|
||||
void createNextLogElement(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat);
|
@ -7,7 +7,7 @@ unsigned int PuzzlePiece::idcount(0);
|
||||
int main()
|
||||
{
|
||||
|
||||
int cols=2, rows=3;
|
||||
unsigned int cols=2, rows=3;
|
||||
|
||||
//some basic random puzzle stuff
|
||||
vector<PuzzlePiece> myFirstBox = createBox(coor(cols,rows));
|
||||
@ -18,12 +18,13 @@ int main()
|
||||
|
||||
//BoxClassify myFirstBox();
|
||||
cout << endl;
|
||||
for(int i=0;i<myFirstBox.size();i++)
|
||||
p_myFirstBox.push_back(&myFirstBox[i]);
|
||||
for(auto &i:myFirstBox)
|
||||
p_myFirstBox.push_back(&i);
|
||||
|
||||
Puzzle puzzleMat(cols, rows);
|
||||
|
||||
//vector<vector<PuzzlePiece*>> ab1class = abstractionLayer1classify(log, p_myFirstBox,puzzleMat);
|
||||
while(next(log, p_myFirstBox,puzzleMat));
|
||||
//while(next(log, p_myFirstBox,puzzleMat));
|
||||
|
||||
puzzleMat.printPuzzle();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user