Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
Also optimize in the unit tests (for better coverage)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcslws committed Oct 16, 2018
1 parent fcf0214 commit 11a9b2e
Showing 1 changed file with 43 additions and 40 deletions.
83 changes: 43 additions & 40 deletions src/nupic/experimental/GridUniqueness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,46 +514,6 @@ bool findGridCodeZeroHelper(
}
}

bool nupic::experimental::grid_uniqueness::findGridCodeZero(
const vector<vector<vector<double>>>& domainToPlaneByModule,
const vector<vector<vector<double>>>& latticeBasisByModule,
const vector<double>& x0,
const vector<double>& dims,
double readoutResolution,
vector<double>* pointWithGridCodeZero)
{
// Avoid doing any allocations in each recursion.
vector<double> x0Copy(x0);
vector<double> dimsCopy(dims);
std::atomic<bool> shouldContinue(true);

vector<double> defaultPointBuffer;

if (pointWithGridCodeZero != nullptr)
{
NTA_ASSERT(pointWithGridCodeZero->size() == dims.size());
}
else
{
defaultPointBuffer.resize(dims.size());
pointWithGridCodeZero = &defaultPointBuffer;
}

NTA_ASSERT(domainToPlaneByModule[0].size() == 2);

vector<vector<vector<double>>> inverseLatticeBasisByModule;
for (const vector<vector<double>>& latticeBasis : latticeBasisByModule)
{
inverseLatticeBasisByModule.push_back(invert2DMatrix(latticeBasis));
}

return findGridCodeZeroHelper(
domainToPlaneByModule, latticeBasisByModule, inverseLatticeBasisByModule,
dimsCopy.size(), x0Copy.data(), dimsCopy.data(), readoutResolution,
pointWithGridCodeZero->data(), shouldContinue);
}


struct GridUniquenessState {
// Constants (thread-safe)
const vector<vector<vector<double>>>& domainToPlaneByModule;
Expand Down Expand Up @@ -783,6 +743,49 @@ void optimizeMatrices(vector<vector<vector<double>>> *domainToPlaneByModule,
}
}

bool nupic::experimental::grid_uniqueness::findGridCodeZero(
const vector<vector<vector<double>>>& domainToPlaneByModule,
const vector<vector<vector<double>>>& latticeBasisByModule,
const vector<double>& x0,
const vector<double>& dims,
double readoutResolution,
vector<double>* pointWithGridCodeZero)
{
// Avoid doing any allocations in each recursion.
vector<double> x0Copy(x0);
vector<double> dimsCopy(dims);
std::atomic<bool> shouldContinue(true);

vector<double> defaultPointBuffer;

if (pointWithGridCodeZero != nullptr)
{
NTA_ASSERT(pointWithGridCodeZero->size() == dims.size());
}
else
{
defaultPointBuffer.resize(dims.size());
pointWithGridCodeZero = &defaultPointBuffer;
}

NTA_ASSERT(domainToPlaneByModule[0].size() == 2);

vector<vector<vector<double>>> domainToPlaneByModule2(domainToPlaneByModule);
vector<vector<vector<double>>> latticeBasisByModule2(latticeBasisByModule);
optimizeMatrices(&domainToPlaneByModule2, &latticeBasisByModule2);

vector<vector<vector<double>>> inverseLatticeBasisByModule;
for (const vector<vector<double>>& latticeBasis : latticeBasisByModule2)
{
inverseLatticeBasisByModule.push_back(invert2DMatrix(latticeBasis));
}

return findGridCodeZeroHelper(
domainToPlaneByModule2, latticeBasisByModule2, inverseLatticeBasisByModule,
dimsCopy.size(), x0Copy.data(), dimsCopy.data(), readoutResolution,
pointWithGridCodeZero->data(), shouldContinue);
}

pair<double,vector<double>>
nupic::experimental::grid_uniqueness::computeGridUniquenessHypercube(
const vector<vector<vector<double>>>& domainToPlaneByModule,
Expand Down

0 comments on commit 11a9b2e

Please sign in to comment.