Esempio n. 1
0
void Cell::Retire()
{
	// Release all Segments.
	Segment *segment;
	while ((segment = (Segment*)(Segments.RemoveFirst())) != NULL) {
		mem_manager.ReleaseObject(segment);
	}

	// Release all SegmentsUpdateInfo objects.
	SegmentUpdateInfo *segmentUpdateInfo;
	while ((segmentUpdateInfo = (SegmentUpdateInfo*)(_segmentUpdates.RemoveFirst())) != NULL) {
		mem_manager.ReleaseObject(segmentUpdateInfo);
	}
}
Esempio n. 2
0
void mrb_irep_free(MemManager &mm, mrb_irep *irep)
{
    if (!(irep->flags & MRB_ISEQ_NO_FREE))
        mm._free(irep->iseq);
    for (int i=0; i<irep->plen; i++) {
        if (mrb_type(irep->pool[i]) == MRB_TT_STRING) {
            if ((irep->pool[i].ptr<RString>()->flags & MRB_STR_NOFREE) == 0) {
                mm._free(irep->pool[i].ptr<RString>()->m_ptr);
            }
            mm._free(irep->pool[i].basic_ptr());
        }
#ifdef MRB_WORD_BOXING
    else if (mrb_type(irep->pool[i]) == MRB_TT_FLOAT) {
      mrb_free(mrb, mrb_obj_ptr(irep->pool[i]));
    }
#endif
    }
    mm._free(irep->pool);
    mm._free(irep->syms);
    for (int i=0; i<irep->rlen; i++) {
        mrb_irep_decref(mm, irep->reps[i]);
    }
    mm._free(irep->reps);
    mm._free((void *)irep->filename);
    mm._free(irep->lines);
    mrb_debug_info_free(mm, irep->debug_info);
    mm._free(irep);
}
Esempio n. 3
0
void FastHashTable::Remove(int _key, void *_object)
{
	int bucket_index = _key & KEY_MASK;
	FastHashTray *tray_to_remove;

	// Get pointer to first tray pointer for this bucket.
	FastHashTray **tray_ptr = &(buckets[bucket_index]);

	// Iterate through all trays in this bucket, up to the point where the given _object is found.
	while (((*tray_ptr) != NULL) && ((*tray_ptr)->key <= _key))
	{
		if (((*tray_ptr)->key == _key) && ((*tray_ptr)->pointer == _object))
		{
			// Record pointer to this tray to be removed.
			tray_to_remove = (*tray_ptr);

			// Set the pointer that points to the tray to remove, to point to the next tray instead.
			(*tray_ptr) = (*tray_ptr)->next;

			// Release the tray that's been removed.
			mem_manager.ReleaseObject(tray_to_remove);

			// Decrement the count
			count--;

			// The objecthas been removed; return.
			return;
		}

		// Advance to the next tray
		tray_ptr = &((*tray_ptr)->next);
	}
}
Esempio n. 4
0
void FastHashTable::Insert(int _key, void *_object)
{
	int bucket_index = _key & KEY_MASK;

	// Get pointer to first tray pointer for this bucket.
	FastHashTray **tray_ptr = &(buckets[bucket_index]);

	// Iterate through all trays in this bucket, up to the point where the given _object should be inserted.
	// If the given _object is already in the table, return.
	while (((*tray_ptr) != NULL) && ((*tray_ptr)->key <= _key))
	{
		if (((*tray_ptr)->key == _key) && ((*tray_ptr)->pointer == _object))
		{
			// The given _object is already in the table, under the given _key. Do not add it, just return.
			return;
		}

		// Advance to the next tray
		tray_ptr = &((*tray_ptr)->next);
	}

	// Get a new tray to represent the given object
	FastHashTray *new_tray = (FastHashTray*)(mem_manager.GetObject(MOT_FAST_HASH_TRAY));

	// Initialize the new tray
	new_tray->key = _key;
	new_tray->pointer = _object;

	// Insert the new tray in the bucket
	new_tray->next = (*tray_ptr);
	(*tray_ptr) = new_tray;

	// Increment the count
	count++;
}
Esempio n. 5
0
void FastHashTable::Clear()
{
	FastHashTray *cur_tray, *next_tray;

	// If the table is already empty, just return.
	if (count == 0) {
		return;
	}

	for (int i = 0; i < NUM_BUCKETS; i++) 
	{
		// Release all trays in the current bucket
		cur_tray = buckets[i];
		while (cur_tray != NULL)
		{
			next_tray = cur_tray->next;
			mem_manager.ReleaseObject(cur_tray);
			cur_tray = next_tray;
		}

		// Set the current bucket pointer to NULL
		buckets[i] = NULL;
	}

	// Reset the count
	count = 0;

	// Reset the iterator
	iterator_cur = NULL;
	iterator_bucket_index = NUM_BUCKETS;
}
Esempio n. 6
0
/// Creates a new segment for this Cell.
/// learningCells: A set of available learning cells to add to the segmentUpdateList.
/// Returns created segment.
///
/// The new segment will initially connect to at most newSynapseCount 
/// synapses randomly selected from the set of cells that
/// were in the learning state at t-1 (specified by the learningCells parameter).
Segment *Cell::CreateSegment(FastList &learningCells, int creationTime)
{
   Segment *newSegment = (Segment*)(mem_manager.GetMemObject(MOT_SEGMENT));
	newSegment->Initialize(creationTime, (float)(column->region->SegActiveThreshold));
	newSegment->CreateSynapsesToLearningCells(learningCells, &(column->region->DistalSynapseParams));
	Segments.InsertAtEnd(newSegment);
	return newSegment;
}
Esempio n. 7
0
/// Add a new SegmentUpdateInfo object to this Cell containing proposed changes to the 
/// specified segment. 
///
/// If the segment is NULL, then a new segment is to be added, otherwise
/// the specified segment is updated.  If the segment exists, find all active
/// synapses for the segment (either at t or t-1 based on the 'previous' parameter)
/// and mark them as needing to be updated.  If newSynapses is true, then
/// Region.newSynapseCount - len(activeSynapses) new synapses are added to the
/// segment to be updated.  The (new) synapses are randomly chosen from the set
/// of current learning cells (within Region.predictionRadius if set).
///
/// These segment updates are only applied when the applySegmentUpdates
/// method is later called on this Cell.
SegmentUpdateInfo *Cell::UpdateSegmentActiveSynapses(bool previous, Segment *segment, bool newSynapses, UpdateType updateType)
{
	FastList *activeSyns = NULL;
	if (segment != NULL)
	{
		activeSyns = previous ? &(segment->PrevActiveSynapses) : &(segment->ActiveSynapses);
	}

   SegmentUpdateInfo *segmentUpdate = (SegmentUpdateInfo*)(mem_manager.GetMemObject(MOT_SEGMENT_UPDATE_INFO));
	segmentUpdate->Initialize(this, segment, activeSyns, newSynapses, column->region->GetStepCounter(), updateType);
	_segmentUpdates.InsertAtEnd(segmentUpdate);
	return segmentUpdate;
}
Esempio n. 8
0
/// This function reinforces each segment in this Cell's SegmentUpdateInfo.
///
/// Using the segmentUpdateInfo, the following changes are
/// performed. If positiveReinforcement is true then synapses on the active
/// list get their permanence counts incremented by permanenceInc. All other
/// synapses get their permanence counts decremented by permanenceDec. If
/// positiveReinforcement is false, then synapses on the active list get
/// their permanence counts decremented by permanenceDec. After this step,
/// any synapses in segmentUpdate that do yet exist get added with a permanence
/// count of initialPerm. These new synapses are randomly chosen from the
/// set of all cells that have learnState output = 1 at time step t.
///</remarks>
void Cell::ApplySegmentUpdates(int _curTime, ApplyUpdateTrigger _trigger)
{
	Segment *segment;
	SegmentUpdateInfo *segInfo;
	Synapse *syn;
	FastList modifiedSegments;
	FastListIter synapses_iter, seg_update_iter;
	bool apply_update;

	// Iterate through all segment updates, skipping those not to be applied now, and removing those that are applied.
	seg_update_iter.SetList(_segmentUpdates);
	for (segInfo = (SegmentUpdateInfo*)(seg_update_iter.Reset()); segInfo != NULL; segInfo = (SegmentUpdateInfo*)(seg_update_iter.Get()))
	{
		// Do not apply the current segment update if it was created at the current time step, and was created as a result of the cell being predictive.
		// If this is the case, then this segment update can only be evaluated at a later time step, and so should remain in the queue for now.
		apply_update = !((segInfo->GetCreationTimeStep() == _curTime) && (segInfo->GetUpdateType() == UPDATE_DUE_TO_PREDICTIVE));

		// Do not apply the current segment update if its numPredictionSteps > 1, and if we are applying updates due to the cell still being predictive,
		// but with a greater numPredictionSteps. Unless the segment being updated predicted activation in 1 prediction step, it cannot be proven
		// incorrect, and so shouldn't be processed yet. This is because a "prediction step" can take more than one time step.
		if ((_trigger == AUT_LONGER_PREDICTION) && (segInfo->GetNumPredictionSteps() > 1)) {
			apply_update = false;
		}

		if (apply_update)
		{
			segment = segInfo->GetSegment();

			if (segment != NULL)
			{
				if (_trigger == AUT_ACTIVE)
				{
					// The cell has become actve; positively reinforce the segment.
					segment->UpdatePermanences(segInfo->ActiveDistalSynapses);
				}
				else
				{
					// The cell has become inactive of is predictive but with a longer prediction. Negatively reinforce the segment.
					segment->DecreasePermanences(segInfo->ActiveDistalSynapses);
				}

				// Record that this segment has been modified, so it can later be checked for synapses that should be removed.
				if (modifiedSegments.IsInList(segment) == false) {
					modifiedSegments.InsertAtEnd(segment);
				}
			}

			// Add new synapses (and new segment if necessary)
			if (segInfo->GetAddNewSynapses() && (_trigger == AUT_ACTIVE))
			{
				if (segment == NULL)
				{
					if (segInfo->CellsThatWillLearn.Count() > 0) //only add if learning cells available
					{
						segment = segInfo->CreateCellSegment(column->region->GetStepCounter());
					}
				}
				else if (segInfo->CellsThatWillLearn.Count() > 0)
				{
					//add new synapses to existing segment
					segInfo->CreateSynapsesToLearningCells(&(column->region->DistalSynapseParams));
				}
			}

			// Remove and release the current SegmentUpdateInfo.
			seg_update_iter.Remove();
			mem_manager.ReleaseObject(segInfo);
		}
		else
		{
			// Leave the current segment upate in the queue, to be processed at a later time step. Advance to the next one.
			seg_update_iter.Advance();
		}
	}

	// Only process modified segments if all segment updates have been processed, to avoid deleting segments or synapses that
	// are referred to by still-existing segment updates.
	if (_segmentUpdates.Count() == 0)
	{
		// All segment updates have been processed, so there are none left that may have references to this cell's 
		// synapses or segments. Therefore we can iterate through all segments modified above, to prune unneeded synpses and segments.
		while (modifiedSegments.Count() > 0)
		{
			// Get pointer to the current modified segment, and remove it from the modifiedSegments list.
			segment = (Segment*)(modifiedSegments.GetFirst());
			modifiedSegments.RemoveFirst();

			// Remove from the current modified segment any synapses that have reached permanence of 0.
			synapses_iter.SetList(segment->Synapses);
			for (syn = (Synapse*)(synapses_iter.Reset()); syn != NULL;)
			{
				if (syn->GetPermanence() == 0.0f)
				{
					// Remove and release the current synapse, whose permanence has reached 0.
					synapses_iter.Remove();
					mem_manager.ReleaseObject(syn);

					// Removing the synapse has advanced the iterator to the next synapse. Get a pointer to it.
					syn = (Synapse*)(synapses_iter.Get());
				}
				else
				{
					// Advance to the next synapse.
					syn = (Synapse*)(synapses_iter.Advance());
				}
			}

			// If this modified segment now has no synapses, remove the segment from this cell.
			if (segment->Synapses.Count() == 0)
			{
				Segments.Remove(segment, false);
				mem_manager.ReleaseObject(segment);
			}
		}
	}
	else
	{
		// Not all of the segment updates have been processed, so synapses and segments cannot be pruned. 
		// (because they may be referred to by segment updates that still exist). So just clear the list of modified segments.
		modifiedSegments.Clear();
	}
}