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

Commit

Permalink
Merge pull request #82 from mrcslws/grid-uniqueness-hypercubes
Browse files Browse the repository at this point in the history
Clean up properly when exiting early
  • Loading branch information
mrcslws authored Jan 27, 2019
2 parents 61ef03a + 17472e4 commit 2daa976
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 51 deletions.
103 changes: 54 additions & 49 deletions src/nupic/experimental/GridUniqueness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,8 @@ struct LineInfo2D {

bool latticePointOverlapsShadow(
pair<double, double> latticePoint,
const vector<pair<double, double>>& convexHullUnshifted,
const vector<pair<double, double>>& convexHull,
const vector<LineInfo2D>& lines,
pair<double, double> shift,
size_t numDims,
double rSquared)
{
Expand All @@ -635,16 +634,10 @@ bool latticePointOverlapsShadow(
bool leftRayCollided = false;
bool rightRayCollided = false;

for (size_t iPoint = 0; iPoint < convexHullUnshifted.size() - 1; iPoint++)
for (size_t iPoint = 0; iPoint < convexHull.size() - 1; iPoint++)
{
const pair<double,double> p1 = {
convexHullUnshifted[iPoint].first + shift.first,
convexHullUnshifted[iPoint].second + shift.second,
};
const pair<double,double> p2 = {
convexHullUnshifted[iPoint+1].first + shift.first,
convexHullUnshifted[iPoint+1].second + shift.second,
};
const pair<double,double>& p1 = convexHull[iPoint];
const pair<double,double>& p2 = convexHull[iPoint+1];

if (lineSegmentIntersectsCircle2(p1, p2, latticePoint,
lines[iPoint].unitVector,
Expand Down Expand Up @@ -965,7 +958,7 @@ bool tryProveGridCodeZeroImpossible(
{
// Figure out which lattice points we need to check.
const pair<double,double> shift =
transformND( domainToPlaneByModule[iModule], x0);
transformND(domainToPlaneByModule[iModule], x0);
const BoundingBox2D& boundingBox =
cachedShadowBoundingBoxes[frameNumber][iModule];
const double xmin = boundingBox.xmin + shift.first;
Expand Down Expand Up @@ -1007,11 +1000,13 @@ bool tryProveGridCodeZeroImpossible(
}
else
{
latticePoint.first -= shift.first;
latticePoint.second -= shift.second;
foundLatticeCollision =
latticePointOverlapsShadow(latticePoint,
cachedShadows[frameNumber][iModule],
cachedShadowLines[frameNumber][iModule],
shift, numDims, rSquared);
numDims, rSquared);
}
}

Expand Down Expand Up @@ -1828,10 +1823,10 @@ bool tryProveGridCodeZeroImpossible_noModulo(
const pair<double,double> shift =
transformND(domainToPlaneByModule[iModule], x0);

if (!latticePointOverlapsShadow({0.0, 0.0},
if (!latticePointOverlapsShadow({-shift.first, -shift.second},
cachedShadows[frameNumber][iModule],
cachedShadowLines[frameNumber][iModule],
shift, numDims, rSquared))
numDims, rSquared))
{
// This module never gets near grid code zero for the provided range of
// locations. So this range can't possibly contain grid code zero.
Expand Down Expand Up @@ -2046,40 +2041,46 @@ nupic::experimental::grid_uniqueness::computeBinSidelength(
double tested = 0;
double radius = 0.5;

while (findGridCodeZeroAtRadius(radius,
while (radius <= upperBound &&
findGridCodeZeroAtRadius(radius,
domainToPlaneByModule,
readoutResolution,
shouldContinue))
{
tested = radius;
radius *= 2;
}

if (radius > upperBound)
{
return -1.0;
}
double result;
if (radius > upperBound)
{
result = -1.0;
}
else
{
// The radius needs to be twice as precise to get the sidelength
// sufficiently precise.
const double resultPrecision2 = resultPrecision / 2;

// The radius needs to be twice as precise to get the sidelength sufficiently
// precise.
const double resultPrecision2 = resultPrecision / 2;
double dec = (radius - tested) / 2;

double dec = (radius - tested) / 2;
// The possible error is equal to dec*2.
while (shouldContinue && dec*2 > resultPrecision2)
{
const double testRadius = radius - dec;

// The possible error is equal to dec*2.
while (shouldContinue && dec*2 > resultPrecision2)
{
const double testRadius = radius - dec;
if (!findGridCodeZeroAtRadius(testRadius,
domainToPlaneByModule,
readoutResolution,
shouldContinue))
{
radius = testRadius;
}

if (!findGridCodeZeroAtRadius(testRadius,
domainToPlaneByModule,
readoutResolution,
shouldContinue))
{
radius = testRadius;
dec /= 2;
}

dec /= 2;
result = 2*radius;
}

//
Expand All @@ -2103,7 +2104,7 @@ nupic::experimental::grid_uniqueness::computeBinSidelength(
NTA_THROW << "interrupt";
case ExitReason::Completed:
default:
return 2*radius;
return result;
}
}

Expand Down Expand Up @@ -2236,26 +2237,30 @@ nupic::experimental::grid_uniqueness::computeBinRectangle(
//
double radius = 0.5;

while (findGridCodeZeroAtRadius(radius,
while (radius <= upperBound &&
findGridCodeZeroAtRadius(radius,
domainToPlaneByModule,
readoutResolution,
shouldContinue))
{
radius *= 2;

if (radius > upperBound)
{
return {};
}
}

const vector<double> radii = squeezeRectangleToBin(
domainToPlaneByModule, readoutResolution, resultPrecision,
radius, shouldContinue);
vector<double> result;
if (radius > upperBound)
{
// Give up.
}
else
{
const vector<double> radii = squeezeRectangleToBin(
domainToPlaneByModule, readoutResolution, resultPrecision,
radius, shouldContinue);

vector<double> sidelengths(radii.size());
std::transform(radii.begin(), radii.end(), sidelengths.begin(),
[](double r) { return 2*r; });
result.resize(radii.size());
std::transform(radii.begin(), radii.end(), result.begin(),
[](double r) { return 2*r; });
}

//
// Teardown
Expand All @@ -2278,6 +2283,6 @@ nupic::experimental::grid_uniqueness::computeBinRectangle(
NTA_THROW << "interrupt";
case ExitReason::Completed:
default:
return sidelengths;
return result;
}
}
4 changes: 2 additions & 2 deletions src/nupic/experimental/GridUniqueness.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ namespace nupic {
const std::vector<std::vector<std::vector<Real64>>>& domainToPlaneByModule,
Real64 readoutResolution,
Real64 resultPrecision,
Real64 upperBound = 1000.0,
Real64 upperBound = 2048.0,
Real64 timeout = -1.0);

/**
Expand Down Expand Up @@ -213,7 +213,7 @@ namespace nupic {
const std::vector<std::vector<std::vector<Real64>>>& domainToPlaneByModule,
Real64 readoutResolution,
Real64 resultPrecision,
Real64 upperBound = 1000.0,
Real64 upperBound = 2048.0,
Real64 timeout = -1.0);
}
}
Expand Down

0 comments on commit 2daa976

Please sign in to comment.