Simple cache

This example shows the manipulation of SimpleCache instances. This cache only stores the last inputs and outputs stored.

from __future__ import annotations

from numpy import array

from gemseo import configure_logger
from gemseo.caches.simple_cache import SimpleCache

configure_logger()
<RootLogger root (INFO)>

Import

In the following lines, we import the array and the SimpleCache classes.

Create

We can create an instance of the SimpleCache class with the following line:

cache = SimpleCache()

The cache information can be displayed easily:

cache
Name: SimpleCache
  • Type: SimpleCache
  • Tolerance: 0.0
  • Input names: []
  • Output names: []
  • Length: 0


Cache

It is possible to manually add some data into the cache by using the following lines:

cache[{"x": array([1.0])}] = ({"y": array([2.0])}, None)

We can add another entry to the cache, and we can then see that its length is still one. Indeed, as previously mentioned, the SimpleCache only enable to store one evaluation.

cache[{"x": array([2.0])}] = ({"y": array([3.0])}, None)
cache
Name: SimpleCache
  • Type: SimpleCache
  • Tolerance: 0.0
  • Input names: ['x']
  • Output names: ['y']
  • Length: 1


Get all data

We can display the length and the data contained in the cache:

len(cache)
1

As mentioned before, we can see that only the last inputs and outputs cached are available:

list(cache)
[CacheEntry(inputs={'x': array([2.])}, outputs={'y': array([3.])}, jacobian={})]

Get last cached data

We can also print the last cached input and output data. For this cache, the last cached inputs and outputs are also the only ones cached.

last_entry = cache.last_entry
last_entry.inputs, last_entry.outputs
({'x': array([2.])}, {'y': array([3.])})

Clear

It is also possible to clear the cache, by using the following lines:

cache.clear()
cache
Name: SimpleCache
  • Type: SimpleCache
  • Tolerance: 0.0
  • Input names: []
  • Output names: []
  • Length: 0


Total running time of the script: (0 minutes 0.004 seconds)

Gallery generated by Sphinx-Gallery