Knight's Tour Generator
Tourneys and the Fast Generation and Obfuscation of Closed Knight's Tours
SearchThread.cpp
Go to the documentation of this file.
1 
4 // MIT License
5 //
6 // Copyright (c) 2019 Ian Parberry
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 
26 #include "SearchThread.h"
27 
28 #include "TakefujiLee.h"
29 #include "Warnsdorff.h"
30 #include "DivideAndConquer.h"
31 #include "ConcentricBraid.h"
32 #include "FourCover.h"
33 
34 extern std::atomic_bool g_bFinished;
35 
40 
42  CSearchRequest request; //current search request
43 
44  while(m_cSearchRequest.pop(request)) //grab a search request from the queue
45  Generate(request); //perform search
46 } //operator()()
47 
50 
52  const int w = request.m_nWidth;
53  const int h = request.m_nHeight;
54  const int n = request.m_nSize;
55 
56  CBoard* pBoard = new CBoard(w, h); //pointer to chessboard
57 
58  const GeneratorType gentype = request.m_cTourneyDesc.m_eGenerator; //generator
59  const CycleType cycletype = request.m_cTourneyDesc.m_eCycle; //tour or tourney
60  const bool obfuscate = request.m_cTourneyDesc.m_bObfuscate; //whether to obfuscate
61  const int seed = request.m_nSeed; //PRNG seed
62 
63  switch(gentype){
64  case GeneratorType::Warnsdorff:
65  CWarnsdorff(seed).Generate(*pBoard, cycletype);
66  break;
67 
68  case GeneratorType::TakefujiLee:
69  CTakefujiLee(w, h, seed).Generate(*pBoard); //can only generate tourneys
70  break;
71 
72  case GeneratorType::DivideAndConquer:
73  CDivideAndConquer().Generate(*pBoard, cycletype);
74  break;
75 
76  case GeneratorType::ConcentricBraid: //can only generate tourneys
77  CConcentricBraid().Generate(*pBoard);
78 
79  case GeneratorType::FourCover: //can only generate tourneys
80  CFourCover().Generate(*pBoard);
81  } //switch
82 
83  //post-processing tourney
84 
85  if(cycletype == CycleType::TourFromTourney) //make tour from tourney
86  pBoard->JoinUntilTour();
87 
88  if(obfuscate)pBoard->Obfuscate(); //obfuscate
89 
90  if(request.m_bDiscard){ //report statistics
91  CSearchResult result(nullptr, request.m_cTourneyDesc);
92 
93  for(int i=0; i<n; i++){ //for each cell
94  int dest = (*pBoard)[i]; //destination after one move
95  int dest2 = (*pBoard)[dest]; //destination after two moves
96 
97  const int n = pBoard->GetMoveIndex(i, dest); //1st move index in 0..7
98 
99  if(0 <= n && n < 8){ //safety
100  result.m_nSingleMove[n]++; //record single move
101 
102  int n2 = pBoard->GetMoveIndex(dest, dest2) - n;
103  if(n2 < 0)n2 += 8; //2nd move index in 0..7, relative to 1st move
104  result.m_nRelativeMove[n2]++; //record double move
105  } //if
106  } //for
107 
108  m_cSearchResult.push(result);
109  delete pBoard;
110  } //if
111 
112  else{ //we are tasked with generating a single tour
113  if(!g_bFinished){ //report generated tour
114  g_bFinished = true; //signal other threads to terminate
116  } //if
117 
118  else delete pBoard;
119  } //else
120 } //Generate
121 
Header for the concentric braided tourney generator CConcentricBraid.
int GetMoveIndex(int src, int dest)
Get move index.
Definition: BaseBoard.cpp:408
Header for the neural network tourney generator CTakefujiLee.
Header for Warnsdorff's generator CWarnsdorff.
Knight's tour and tourney generator using Warnsdorff's heuristic.
Definition: Warnsdorff.h:57
void Generate(CBoard &b)
Generate a concentric braided tourney.
Four-cover tourney generator.
Definition: FourCover.h:38
Header for the divide-and conquer generator CDivideAndConquer.
void Generate(CBoard &b, CycleType t)
Generate a tour or tourney.
Definition: Warnsdorff.cpp:265
void Generate(CBoard &b, CycleType t, const CRect &rect)
Recursion.
int m_nHeight
Board height.
Definition: Structs.h:59
void push(const data &element)
Insert at tail.
Header for the four-cover tourney generator CFourCover.
Search result.
Definition: Structs.h:76
void Generate(CBoard &b)
Generate a four-cover tourney.
Definition: FourCover.cpp:45
void operator()()
The code that gets run by each thread.
int m_nSeed
PRNG seed.
Definition: Structs.h:64
void JoinUntilTour()
Join cycles to reduce tourney size.
Definition: Board.cpp:305
UINT64 m_nRelativeMove[8]
Double move count.
Definition: Structs.h:86
static CThreadSafeQueue< CSearchResult > m_cSearchResult
Search result queue.
Header for the search thread CSearchThread.
Search request.
Definition: Structs.h:55
bool m_bDiscard
Discard result.
Definition: Structs.h:62
Neural network tourney generator.
Definition: TakefujiLee.h:47
GeneratorType
Generator type.
Definition: Defines.h:47
int m_nWidth
Board width.
Definition: Structs.h:58
bool pop(data &element)
Delete from head and return.
Concentric braided tourney generator.
Chessboard.
Definition: Board.h:42
GeneratorType m_eGenerator
Generator type.
Definition: Structs.h:41
CycleType m_eCycle
Cycle type.
Definition: Structs.h:42
int m_nSize
Board size.
Definition: Structs.h:60
void Generate(CBoard &b)
Generate a tourney.
Divide-and-conquer knight's tour and tourney generator.
std::atomic_bool g_bFinished
Search termination flag.
UINT64 m_nSingleMove[8]
Single move count.
Definition: Structs.h:85
void Generate(CSearchRequest &request)
Generate knight's tour/tourney.
void Obfuscate()
Obfuscate function.
Definition: Board.cpp:214
CTourneyDesc m_cTourneyDesc
Tourney descriptor.
Definition: Structs.h:56
CycleType
Cycle type.
Definition: Defines.h:58
static CThreadSafeQueue< CSearchRequest > m_cSearchRequest
Search request queue.
bool m_bObfuscate
Whether to obfuscate.
Definition: Structs.h:43