Skip to content

Commit

Permalink
Merge pull request htm-community#930 from htm-community/remove-synaps…
Browse files Browse the repository at this point in the history
…e-id

Remove Synapse.id and nextSynapseOrdinal_
  • Loading branch information
breznak authored Apr 28, 2021
2 parents b2112db + 6359ba9 commit 73ad638
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
25 changes: 10 additions & 15 deletions src/htm/algorithms/Connections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Segment Connections::createSegment(const CellIdx cell,
NTA_ASSERT(numSegments(cell) <= maxSegmentsPerCell);

// Proceed to create a new segment.
const SegmentData& segmentData = SegmentData(cell, iteration_);
const SegmentData& segmentData = SegmentData(cell);
Segment segment;
if (!destroyedSegments_.empty() ) { //reuse old, destroyed segs
segment = destroyedSegments_.back();
Expand Down Expand Up @@ -186,7 +186,6 @@ Synapse Connections::createSynapse(Segment segment,
SynapseData &synapseData = synapses_[synapse];
synapseData.presynapticCell = presynapticCell;
synapseData.segment = segment;
synapseData.id = nextSynapseOrdinal_++; //TODO move these to SynData constructor
// Start in disconnected state.
synapseData.permanence = connectedThreshold_ - static_cast<Permanence>(1.0);
synapseData.presynapticMapIndex_ =
Expand Down Expand Up @@ -316,17 +315,15 @@ void Connections::destroySynapse(const Synapse synapse) {
potentialSegmentsForPresynapticCell_.erase( presynCell );
}
}

const auto synapseOnSegment = std::lower_bound(segmentData.synapses.cbegin(),
segmentData.synapses.cend(),
synapse,
[&](const Synapse a, const Synapse b) -> bool { return dataForSynapse(a).id < dataForSynapse(b).id;}
);

NTA_ASSERT(synapseOnSegment != segmentData.synapses.cend());
NTA_ASSERT(*synapseOnSegment == synapse);
for(auto i = 0u; i < segmentData.synapses.size(); i++) {
if (segmentData.synapses[i] == synapse) {
segmentData.synapses[i] = segmentData.synapses.back();
segmentData.synapses.pop_back();
break;
}
}

segmentData.synapses.erase(synapseOnSegment);
destroyedSynapses_.push_back(synapse);
}

Expand Down Expand Up @@ -399,8 +396,8 @@ bool Connections::compareSegments(const Segment a, const Segment b) const {
const SegmentData &bData = segments_[b];
// default sort by cell
if (aData.cell == bData.cell)
//fallback to ordinals:
return aData.id < bData.id; //TODO is segment's id/ordinals needed?
// fallback to segment index
return a < b;
else return aData.cell < bData.cell;
}

Expand Down Expand Up @@ -854,8 +851,6 @@ bool Connections::operator==(const Connections &o) const {
NTA_CHECK(potentialSegmentsForPresynapticCell_ == o.potentialSegmentsForPresynapticCell_);
NTA_CHECK(connectedSegmentsForPresynapticCell_ == o.connectedSegmentsForPresynapticCell_);

NTA_CHECK (nextSynapseOrdinal_ == o.nextSynapseOrdinal_ ) << "Connections equals: nextSynapseOrdinal_";

NTA_CHECK (timeseries_ == o.timeseries_ ) << "Connections equals: timeseries_";
NTA_CHECK (previousUpdates_ == o.previousUpdates_ ) << "Connections equals: previousUpdates_";
NTA_CHECK (currentUpdates_ == o.currentUpdates_ ) << "Connections equals: currentUpdates_";
Expand Down
19 changes: 5 additions & 14 deletions src/htm/algorithms/Connections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ struct SynapseData: public Serializable {
Permanence permanence;
Segment segment;
Synapse presynapticMapIndex_;
Synapse id;

SynapseData() {}

Expand All @@ -86,13 +85,12 @@ struct SynapseData: public Serializable {
ar(CEREAL_NVP(permanence),
CEREAL_NVP(presynapticCell),
CEREAL_NVP(segment),
CEREAL_NVP(presynapticMapIndex_),
CEREAL_NVP(id)
CEREAL_NVP(presynapticMapIndex_)
);
}
template<class Archive>
void load_ar(Archive & ar) {
ar( permanence, presynapticCell, segment, presynapticMapIndex_, id);
ar( permanence, presynapticCell, segment, presynapticMapIndex_);
}

//operator==
Expand All @@ -102,7 +100,6 @@ struct SynapseData: public Serializable {
NTA_CHECK(permanence == o.permanence ) << "SynapseData equals: permanence";
NTA_CHECK(segment == o.segment ) << "SynapseData equals: segment";
NTA_CHECK(presynapticMapIndex_ == o.presynapticMapIndex_ ) << "SynapseData equals: presynapticMapIndex_";
NTA_CHECK(id == o.id ) << "SynapseData equals: id";
} catch(const htm::Exception& ex) {
UNUSED(ex); // this avoids the warning if ex is not used.
//NTA_WARN << "SynapseData equals: " << ex.what(); //Note: uncomment for debug, tells you
Expand All @@ -129,12 +126,11 @@ struct SynapseData: public Serializable {
* The cell that this segment is on.
*/
struct SegmentData: public Serializable {
SegmentData(const CellIdx cell, Segment id) : cell(cell), numConnected(0), id(id) {} //default constructor
SegmentData(const CellIdx cell) : cell(cell), numConnected(0) {} //default constructor

std::vector<Synapse> synapses;
CellIdx cell; //mother cell that this segment originates from
SynapseIdx numConnected; //number of permanences from `synapses` that are >= synPermConnected, ie connected synapses
Segment id;

//Serialize
SegmentData() {}; //empty constructor for serialization, do not use
Expand All @@ -143,13 +139,12 @@ struct SegmentData: public Serializable {
void save_ar(Archive & ar) const {
ar(CEREAL_NVP(synapses),
CEREAL_NVP(cell),
CEREAL_NVP(numConnected),
CEREAL_NVP(id)
CEREAL_NVP(numConnected)
);
}
template<class Archive>
void load_ar(Archive & ar) {
ar( synapses, cell, numConnected, id);
ar( synapses, cell, numConnected);
}

//equals op==
Expand All @@ -158,7 +153,6 @@ struct SegmentData: public Serializable {
NTA_CHECK(synapses == o.synapses) << "SegmentData equals: synapses";
NTA_CHECK(cell == o.cell) << "SegmentData equals: cell";
NTA_CHECK(numConnected == o.numConnected) << "SegmentData equals: numConnected";
NTA_CHECK(id == o.id) << "SegmentData equals: id";

} catch(const htm::Exception& ex) {
UNUSED(ex); // this avoids the warning if ex is not used.
Expand Down Expand Up @@ -694,7 +688,6 @@ class Connections : public Serializable
ar(CEREAL_NVP(potentialSegmentsForPresynapticCell_));
ar(CEREAL_NVP(connectedSegmentsForPresynapticCell_));

ar(CEREAL_NVP(nextSynapseOrdinal_));
ar(CEREAL_NVP(timeseries_));
ar(CEREAL_NVP(previousUpdates_));
ar(CEREAL_NVP(currentUpdates_));
Expand All @@ -721,7 +714,6 @@ class Connections : public Serializable
ar(CEREAL_NVP(potentialSegmentsForPresynapticCell_));
ar(CEREAL_NVP(connectedSegmentsForPresynapticCell_));

ar(CEREAL_NVP(nextSynapseOrdinal_));
ar(CEREAL_NVP(timeseries_));
ar(CEREAL_NVP(previousUpdates_));
ar(CEREAL_NVP(currentUpdates_));
Expand Down Expand Up @@ -866,7 +858,6 @@ class Connections : public Serializable
std::unordered_map<CellIdx, std::vector<Segment>, identity> potentialSegmentsForPresynapticCell_;
std::unordered_map<CellIdx, std::vector<Segment>, identity> connectedSegmentsForPresynapticCell_;

Synapse nextSynapseOrdinal_ = 0;
// These three members should be used when working with highly correlated
// data. The vectors store the permanence changes made by adaptSegment.
bool timeseries_;
Expand Down

0 comments on commit 73ad638

Please sign in to comment.