Skip to content

Dynamic Discrete Spaces #2754

Closed
Closed
@quaquel

Description

@quaquel

Currently, all DiscreteSpace subclasses rely on @cache and @cached_property for neighborhood-related functionality. This is critical for performance. However, if you want to represent a dynamically changing discrete space (e.g., a dynamic network), this is currently not possible.

It seems that functools offers a clear_cache function that can be called on anything decorated with a caching decorator. This means that it is possible to retain the performance benefits of caching while also being able to clear it on specific parts of the DiscreteSpace.

Something like the following is roughly what would be needed. Note that this is not tested and just quickly put together based on some docs.

class DynamicNetwork(Network):
	
	def add_cell(cell:Cell):
		self.G.add_node(cell.coordinate)
		self._cells[cell.coordinate] = cell.coordinate

	def add_edge(cell1:Cell, cell2:Cell):
		self.G.add_edge(cell1.coordinate, cell2.coordinate)

		for cell in [cell1, cell2]:
			cell.get_neighborhood.cache_clear()
			cell.neigbhoorhood.cache_clear()
			cell._neighborhood.cache_clear()
			self._connect_single_cell(cell)

	def remove_cell(cell:Cell):
		neigbors = cell.neighborhood

		self.G.remove_node(cell.coordinate)
		
		# iterate over all neighbors
		for cell in neighbors:
			cell.get_neighborhood.cache_clear()
			cell.neigbhoorhood.cache_clear()
			cell._neighborhood.cache_clear()
			self._connect_single_cell(cell)


	def remove_edge(cell1:Cell, cell2:Cell):
		self.G.remove_edge(cell1.coordinate, cell2.coordinate)

		for cell in [cell1, cell2]:
			cell.get_neighborhood.cache_clear()
			cell.neigbhoorhood.cache_clear()
			cell._neighborhood.cache_clear()
			self._connect_single_cell(cell)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureRelease notes label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions