Improved algorithm

This commit is contained in:
Jonny007-MKD 2013-11-25 07:49:18 +01:00
parent b360e34a4a
commit 265c45b215

View file

@ -88,59 +88,49 @@ int _tmain(int argc, _TCHAR* argv[])
for (uint i = 0; i < numElements; i++) for (uint i = 0; i < numElements; i++)
{ {
cells[i] = new Cell(i, globalPIC); cells[i] = new Cell(i, globalPIC);
if (cells[i]->value)
numOnes++;
} }
Wertetabelle* wt = new Wertetabelle(&cells, variables); Wertetabelle* wt = new Wertetabelle(&cells, variables);
wt->Print(); wt->Print();
delete wt; delete wt;
system("pause");
// find hazards // find hazards
if (numOnes > numElements / 2) // we have more 1 than 0 --> checkerboard --> 50% of cells are checked for (uint i = 0; i < numElements; i += 2)
{ {
cout << "\nHazard-Algorithmus: Schachbrettmuster\n"; uint grayI = i ^ (i/2); // transform to gray code --> Schachbrettmuster
for (uint i = 0; i < numElements; i += 2) Cell* currentCell = cells[grayI]; // this is the cell we are currently checking
if (currentCell->value == false) // no hazard can occur
continue;
cout << " Checking cell " << grayI << endl;
vector<Cell*>* hazardousNeighbors = currentCell->getHazards(cells);
if (hazardousNeighbors->size() == 0) // no hazard found
{ {
uint grayI = i ^ (i/2); // transform to gray code delete hazardousNeighbors;
cout << " Checking cell " << grayI << endl; continue;
vector<Cell*> hazardousNeighbors = cells[grayI]->GetHazards();
if (hazardousNeighbors.size() == 0) // we found no hazard
continue;
for (vector<Cell*>::iterator c = hazardousNeighbors.begin(); c < hazardousNeighbors.end(); c++)
{
printf("Hazard found! Cell %d <--> Cell %d\n", grayI, (*c)->index);
globalPIC->add(grayI, (*c)->index); // add PI that solves hazard. Not quite smart...
}
} }
}
else // less 1 than 0 --> only check every 1 --> less than 50% (numOnes/numElements) of cells are checked for (vector<Cell*>::iterator c = hazardousNeighbors->begin(); c < hazardousNeighbors->end(); c++)
{
cout << "\nHazard-Algorithmus: Ueberspringe Nullen\n";
for (uint i = 0; i < numElements; i++)
{ {
if (!cells[i]->value) printf("Hazard found! Cell %d <--> Cell %d\n", grayI, (*c)->index);
continue; globalPIC->add(grayI, (*c)->index); // add PI that solves hazard. Not quite smart...
(*c)->refresh(globalPIC); // refresh the local PIC (a PI was added)
cout << " Checking cell " << i << endl;
vector<Cell*> hazardousNeighbors = cells[i]->GetHazards();
if (hazardousNeighbors.size() == 0) // we found no hazard
continue;
for (vector<Cell*>::iterator c = hazardousNeighbors.begin(); c < hazardousNeighbors.end(); c++)
{
printf("Hazard found! Cell %d <--> Cell %d\n", i, (*c)->index);
globalPIC->add(i, (*c)->index); // add PI that solves hazard. Not quite smart...
}
} }
currentCell->refresh(globalPIC);
delete hazardousNeighbors;
} }
system("pause"); system("pause");
wt = new Wertetabelle(&cells, variables);
wt->Print();
delete wt;
globalPIC->Dispose(); globalPIC->Dispose();
system("pause");
return 0; return 0;
} }