Permalink
Browse files

[CodeGen] Make NumConfChecks to reference instead of global variable …

…by Dr. Rhydian Lewis
  • Loading branch information...
xiangzhai committed Dec 16, 2017
1 parent 80af0a9 commit 696246bb2b6da6878ca8b33a3aa7ee35d0b6aac1
@@ -102,7 +102,7 @@ static void addHorizontalLine(int Row, std::vector<std::vector<Cell>> &Vals,
for (int c = 0; c < N; c++) {
if (Vals[Row][c].Weight == 0 && !Vals[Row][c].Lined &&
Vals[Row][c].Visible) {
RowZeros[row]--;
RowZeros[Row]--;
ColZeros[c]--;
NumZerosRemaining--;
}
@@ -269,7 +269,7 @@ static int drawLines(std::vector<std::vector<Cell>> &Vals, int N,
RowZeros[i] = 0;
ColLined[i] = false;
ColZeros[i] = 0;
Natching[i] = -1;
Matching[i] = -1;
}
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
@@ -40,14 +40,14 @@ static int swapNodesInKempeChain(std::vector<std::vector<int>> &TheColours,
// the old ones
int BlackCnt = 0;
std::vector<int> Temp0, Temp1;
for (int i = 0; i < TheGroups[0].size(); i++) {
for (size_t i = 0; i < TheGroups[0].size(); i++) {
if (TheColours[0][i] == BLACK) {
Temp1.push_back(TheGroups[0][i]);
BlackCnt++;
} else
Temp0.push_back(TheGroups[0][i]);
}
for (int i = 0; i < TheGroups[1].size(); i++) {
for (size_t i = 0; i < TheGroups[1].size(); i++) {
if (TheColours[1][i] == BLACK) {
Temp0.push_back(TheGroups[1][i]);
BlackCnt++;
@@ -63,49 +63,52 @@ static int swapNodesInKempeChain(std::vector<std::vector<int>> &TheColours,
static void DFSVisit(int UPos, int TheGroup, Graph &G,
std::vector<std::vector<int>> &TheColours,
std::vector<std::vector<int>> &TheGroups) {
std::vector<std::vector<int>> &TheGroups,
unsigned long long &NumConfChecks) {
int TheOtherGroup = 0;
if (TheGroup == 0)
TheOtherGroup = 1;
TheColours[TheGroup][UPos] = GREY;
for (int VPos = 0; VPos < TheColours[TheOtherGroup].size(); VPos++) {
for (size_t VPos = 0; VPos < TheColours[TheOtherGroup].size(); VPos++) {
NumConfChecks++;
if (G[TheGroups[TheGroup][UPos]][TheGroups[TheOtherGroup][VPos]]) {
if (TheColours[TheOtherGroup][VPos] == WHITE)
DFSVisit(VPos, TheOtherGroup, G, TheColours, TheGroups);
DFSVisit(VPos, TheOtherGroup, G, TheColours, TheGroups, NumConfChecks);
}
}
TheColours[TheGroup][UPos] = BLACK;
};
static int performKempeChainInterchange(std::vector<std::vector<int>> &SOL,
Graph &G,
int Group0, int Group1, int NodePos) {
Graph &G, int Group0, int Group1,
int NodePos,
unsigned long long &NumConfChecks) {
// Set up some data structures for use with the restricted DepthFirstSearch
// Routine I am about to do
std::vector<std::vector<int>> TheColours(2);
std::vector<std::vector<int>> TheGroups(2);
for (int i = 0; i < SOL[Group0].size(); i++) {
for (size_t i = 0; i < SOL[Group0].size(); i++) {
TheColours[0].push_back(WHITE);
TheGroups[0].push_back(SOL[Group0][i]);
}
for (int i = 0; i < SOL[Group1].size(); i++) {
for (size_t i = 0; i < SOL[Group1].size(); i++) {
TheColours[1].push_back(WHITE);
TheGroups[1].push_back(SOL[Group1][i]);
}
// Now start the restricted DFS from the chosen node
DFSVisit(NodePos, 0, G, TheColours, TheGroups);
DFSVisit(NodePos, 0, G, TheColours, TheGroups, NumConfChecks);
// When we get to here we should be able to identify which nodes are in the
// Kempe chain by seeing which nodes are BLACK.
//(note it could be that all nodes are black ==> colour relabelling. This
//serves no purpose, but oit is carried out anyway in this case.
// serves no purpose, but oit is carried out anyway in this case.
return swapNodesInKempeChain(TheColours, SOL, TheGroups, Group0, Group1);
};
void doRandomPeturbation(std::vector<int> &OSP, int K, Graph &G) {
void doRandomPeturbation(std::vector<int> &OSP, int K, Graph &G,
unsigned long long &NumConfChecks) {
std::vector<std::vector<int>> SOL(K, std::vector<int>());
int KempeSize;
int NodePos, Group0, Group1, GroupSize;
@@ -130,15 +133,16 @@ void doRandomPeturbation(std::vector<int> &OSP, int K, Graph &G) {
Group1 = rand() % SOL.size();
} while (Group0 == Group1);
GroupSize = SOL[Group0].size() + SOL[Group1].size();
KempeSize = performKempeChainInterchange(SOL, G, Group0, Group1, NodePos);
// NOTE: if kempeSize==groupSize then this is just a colour relabelling
KempeSize = performKempeChainInterchange(SOL, G, Group0, Group1, NodePos,
NumConfChecks);
// NOTE: if kempeSize == groupSize then this is just a colour relabelling
}
// Now convert back to object form
for (int i = 0; i < G.Nodes; i++)
OSP[i] = INT_MIN;
for (int i = 0; i < K; i++) {
for (int j = 0; j < SOL[i].size(); j++)
for (size_t j = 0; j < SOL[i].size(); j++)
OSP[SOL[i][j]] = i + 1;
}
};
@@ -30,22 +30,22 @@ int ASSIGNED = INT_MIN;
static bool colourIsFeasible(int V, std::vector<std::vector<int>> &SOL, int C,
std::vector<int> &ColNode,
std::vector<std::vector<int>> &AdjList,
Graph &G) {
std::vector<std::vector<int>> &AdjList, Graph &G,
unsigned long long &NumConfChecks) {
// Checks to see whether vertex v can be feasibly inserted into colour c in
// sol.
NumConfChecks++;
if (SOL[C].size() > AdjList[V].size()) {
// check if any neighbours of v are currently in colour c
for (int i = 0; i < AdjList[V].size(); i++) {
for (size_t i = 0; i < AdjList[V].size(); i++) {
NumConfChecks++;
if (ColNode[AdjList[V][i]] == C)
return false;
}
return true;
} else {
// check if any vertices in colour c are adjacent to v
for (int i = 0; i < SOL[C].size(); i++) {
for (size_t i = 0; i < SOL[C].size(); i++) {
NumConfChecks++;
if (G[V][SOL[C][i]])
return false;
@@ -59,13 +59,15 @@ static void assignAColourDSatur(bool &FoundColour,
std::vector<int> &Permutation, int NodePos,
std::vector<int> &SatDeg, Graph &G,
std::vector<int> &ColNode,
std::vector<std::vector<int>> &AdjList) {
int j, C = 0, V = Permutation[NodePos];
std::vector<std::vector<int>> &AdjList,
unsigned long long &NumConfChecks) {
size_t j, C = 0;
int V = Permutation[NodePos];
bool AlreadyAdj;
while (C < CandSol.size() && !FoundColour) {
// check if colour c is feasible for vertex v
if (colourIsFeasible(V, CandSol, C, ColNode, AdjList, G)) {
if (colourIsFeasible(V, CandSol, C, ColNode, AdjList, G, NumConfChecks)) {
// v can be added to this colour
FoundColour = true;
CandSol[C].push_back(V);
@@ -75,7 +77,7 @@ static void assignAColourDSatur(bool &FoundColour,
// this newly coloured node v. If i is already adjacent to a node in
// colour c we do nothing,
// otherwise its saturation degree is increased...
for (int i = 0; i < SatDeg.size(); i++) {
for (size_t i = 0; i < SatDeg.size(); i++) {
NumConfChecks++;
if (G[V][Permutation[i]]) {
AlreadyAdj = false;
@@ -96,11 +98,13 @@ static void assignAColourDSatur(bool &FoundColour,
};
static void greedyCol(std::vector<std::vector<int>> &CandSol,
std::vector<int> &ColNode,
Graph &G, std::vector<std::vector<int>> &AdjList) {
std::vector<int> &ColNode, Graph &G,
std::vector<std::vector<int>> &AdjList,
unsigned long long &NumConfChecks) {
// 1) Make an empty vector representing all the unplaced nodes (i.e. all of
// them) and permute
int R;
size_t j;
std::vector<int> A(G.Nodes);
for (int i = 0; i < G.Nodes; i++)
A[i] = i;
@@ -119,8 +123,9 @@ static void greedyCol(std::vector<std::vector<int>> &CandSol,
// Now go through the remaining nodes and see if they are suitable for any
// existing colour. If it isn't, we create a new colour
for (int i = 1; i < G.Nodes; i++) {
for (int j = 0; j < CandSol.size(); j++) {
if (colourIsFeasible(A[i], CandSol, j, ColNode, AdjList, G)) {
for (j = 0; j < CandSol.size(); j++) {
if (colourIsFeasible(A[i], CandSol, j, ColNode, AdjList, G,
NumConfChecks)) {
// the Item can be inserted into this group. So we do
CandSol[j].push_back(A[i]);
ColNode[A[i]] = j;
@@ -138,8 +143,9 @@ static void greedyCol(std::vector<std::vector<int>> &CandSol,
};
static void DSaturCol(std::vector<std::vector<int>> &CandSol,
std::vector<int> &ColNode,
Graph &G, std::vector<std::vector<int>> &AdjList) {
std::vector<int> &ColNode, Graph &G,
std::vector<std::vector<int>> &AdjList,
unsigned long long &NumConfChecks) {
int R;
bool FoundColour;
@@ -168,7 +174,7 @@ static void DSaturCol(std::vector<std::vector<int>> &CandSol,
// Initialise candSol and colNode
CandSol.clear();
CandSol.push_back(std::vector<int>());
for (int i = 0; i < ColNode.size(); i++)
for (size_t i = 0; i < ColNode.size(); i++)
ColNode[i] = INT_MIN;
// Colour the rightmost node first (it has the highest degree), and remove it
@@ -178,7 +184,7 @@ static void DSaturCol(std::vector<std::vector<int>> &CandSol,
Permutation.pop_back();
//..and update the saturation degree array
SatDeg.pop_back();
for (int i = 0; i < SatDeg.size(); i++) {
for (size_t i = 0; i < SatDeg.size(); i++) {
NumConfChecks++;
if (G[CandSol[0][0]][Permutation[i]])
SatDeg[i]++;
@@ -190,7 +196,7 @@ static void DSaturCol(std::vector<std::vector<int>> &CandSol,
// choose the node to colour next (the rightmost node that has maximal
// satDegree)
MaxSat = INT_MIN;
for (int i = 0; i < SatDeg.size(); i++) {
for (size_t i = 0; i < SatDeg.size(); i++) {
if (SatDeg[i] >= MaxSat) {
MaxSat = SatDeg[i];
NodePos = i;
@@ -199,15 +205,15 @@ static void DSaturCol(std::vector<std::vector<int>> &CandSol,
// now choose which colour to assign to the node
FoundColour = false;
assignAColourDSatur(FoundColour, CandSol, Permutation, NodePos, SatDeg, G,
ColNode, AdjList);
ColNode, AdjList, NumConfChecks);
if (!FoundColour) {
// If we are here we have to make a new colour as we have tried all the
// other ones and none are suitable
CandSol.push_back(std::vector<int>());
CandSol.back().push_back(Permutation[NodePos]);
ColNode[Permutation[NodePos]] = CandSol.size() - 1;
// Remember to update the saturation degree array
for (int i = 0; i < Permutation.size(); i++) {
for (size_t i = 0; i < Permutation.size(); i++) {
NumConfChecks++;
if (G[Permutation[NodePos]][Permutation[i]])
SatDeg[i]++;
@@ -219,8 +225,8 @@ static void DSaturCol(std::vector<std::vector<int>> &CandSol,
}
};
static int generateInitialK(Graph &G, int ALG,
std::vector<int> &BestColouring) {
int generateInitialK(Graph &G, int ALG, std::vector<int> &BestColouring,
unsigned long long &NumConfChecks) {
// Make the structures needed for the constructive algorithms
std::vector<std::vector<int>> CandSol, AdjList(G.Nodes, std::vector<int>());
std::vector<int> ColNode(G.Nodes, INT_MAX);
@@ -233,12 +239,12 @@ static int generateInitialK(Graph &G, int ALG,
// Now make the solution
if (ALG == 1)
DSaturCol(CandSol, ColNode, G, AdjList);
DSaturCol(CandSol, ColNode, G, AdjList, NumConfChecks);
else
greedyCol(CandSol, ColNode, G, AdjList);
greedyCol(CandSol, ColNode, G, AdjList, NumConfChecks);
// Copy this solution into bestColouring
for (int i = 0; i < CandSol.size(); i++) {
for (int j = 0; j < CandSol[i].size(); j++)
for (size_t i = 0; i < CandSol.size(); i++) {
for (size_t j = 0; j < CandSol[i].size(); j++)
BestColouring[CandSol[i][j]] = i;
}
// And return the number of colours it has used
@@ -247,7 +253,7 @@ static int generateInitialK(Graph &G, int ALG,
static void updateColOptions(std::vector<std::vector<bool>> &AvailCols,
std::vector<int> &NumColOptions, Graph &G, int V,
int COL) {
int COL, unsigned long long &NumConfChecks) {
// Updates colOptions vector due to node v being assigned a colour
NumColOptions[V] = ASSIGNED;
for (int i = 0; i < G.Nodes; i++) {
@@ -262,7 +268,7 @@ static void updateColOptions(std::vector<std::vector<bool>> &AvailCols,
};
static bool coloursAvailable(std::vector<int> &ColOptions) {
for (int i = 0; i < ColOptions.size(); i++) {
for (size_t i = 0; i < ColOptions.size(); i++) {
if (ColOptions[i] >= 1)
return true;
}
@@ -272,7 +278,7 @@ static bool coloursAvailable(std::vector<int> &ColOptions) {
static int chooseNextNode(std::vector<int> &ColOptions) {
int MinOptions = INT_MAX;
std::vector<int> A;
for (int i = 0; i < ColOptions.size(); i++) {
for (size_t i = 0; i < ColOptions.size(); i++) {
if (ColOptions[i] != ASSIGNED) {
if (ColOptions[i] >= 1) {
if (ColOptions[i] < MinOptions) {
@@ -312,7 +318,8 @@ static int assignToColour(std::vector<std::vector<bool>> &AvailCols,
return -1;
};
void makeInitSolution(Graph &G, std::vector<int> &SOL, int K, int Verbose) {
void makeInitSolution(Graph &G, std::vector<int> &SOL, int K, int Verbose,
unsigned long long &NumConfChecks) {
int V, C;
// 1) Make a 2D vector containing all colour options for each node (initially
@@ -326,15 +333,15 @@ void makeInitSolution(Graph &G, std::vector<int> &SOL, int K, int Verbose) {
// 2) Now add a random node to the first colour and update colOptions
V = rand() % G.Nodes;
C = assignToColour(AvailCols, CandSol, K, V);
updateColOptions(AvailCols, NumColOptions, G, V, C);
updateColOptions(AvailCols, NumColOptions, G, V, C, NumConfChecks);
// 3) For each remaining node with available colour options, choose a node
// with minimal (>=1) options and assign to an early colour
while (coloursAvailable(NumColOptions)) {
// choose node to colour
V = chooseNextNode(NumColOptions);
// assign to a colour
C = assignToColour(AvailCols, CandSol, K, V);
updateColOptions(AvailCols, NumColOptions, G, V, C);
updateColOptions(AvailCols, NumColOptions, G, V, C, NumConfChecks);
}
// When we are here, we either have a full valid solution, or some nodes are
@@ -357,7 +364,7 @@ void makeInitSolution(Graph &G, std::vector<int> &SOL, int K, int Verbose) {
// 3) Now tranfer to the more convienient representation in the population
// itself and end
for (int i = 0; i < K; i++) {
for (int j = 0; j < CandSol[i].size(); j++)
for (size_t j = 0; j < CandSol[i].size(); j++)
SOL[CandSol[i][j]] = i + 1;
}
};
@@ -27,7 +27,8 @@ namespace llvm {
namespace GCol {
void initializeArrays(int **&NodesByColor, int **&Conflicts, int **&TabuStatus,
int *&NbcPosition, Graph &G, std::vector<int> &C, int K) {
int *&NbcPosition, Graph &G, std::vector<int> &C, int K,
unsigned long long &NumConfChecks) {
int N = G.Nodes;
// Allocate and initialize (k + 1) x (n + 1) array for nodesByColor and
@@ -74,7 +75,7 @@ void moveNodeToColorForTabu(int BestNode, int BestColor, Graph &G,
int **Conflicts, int *NbcPosition, int **Neighbors,
int *NodesInConflict, int *ConfPosition,
int **TabuStatus, long TotalIterations,
int TabuTenure) {
int TabuTenure, unsigned long long &NumConfChecks) {
int OldColor = C[BestNode];
// move bestNodes to bestColor
C[BestNode] = BestColor;
@@ -129,7 +130,7 @@ void freeArrays(int **&NodesByColor, int **&Conflicts, int **&TabuStatus,
delete[] NodesByColor;
delete[] Conflicts;
delete[] TabuStatus;
delete[] nbcPosition;
delete[] NbcPosition;
};
} // end namespace GCol
Oops, something went wrong.

0 comments on commit 696246b

Please sign in to comment.