Simple cache

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

from gemseo.api import configure_logger
from gemseo.caches.simple_cache import SimpleCache
from numpy import array

configure_logger()

Out:

<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:
print(cache)

Out:

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)
print(cache)

Out:

Name: SimpleCache
   Type: SimpleCache
   Tolerance: 0.0
   Input names: ['x']
   Output names: ['y']
   Length: 2

Get all data

We can display the lenght and the data contained in the cache. As mentionned before, we can see that only the last inputs and outputs cached are available:

print(len(cache))
for data in cache:
    print(data)

Out:

2
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 ouputs are also the only ones cached.

last_entry = cache.last_entry
print(last_entry.inputs)
print(last_entry.outputs)

Out:

{'x': array([2.])}
{'y': array([3.])}

Clear

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

cache.clear()
print(cache)

Out:

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

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

Gallery generated by Sphinx-Gallery