Note
Click here to download the full example code
Simple cache¶
This example shows the manipulation of SimpleCache
instances. This
cache only stores the last inputs and outputs stored.
from __future__ import absolute_import, division, print_function, unicode_literals
from future import standard_library
from numpy import array
from gemseo.api import configure_logger
from gemseo.caches.simple_cache import SimpleCache
configure_logger()
Out:
<RootLogger root (INFO)>
Import¶
In the following lines, we import the array and the SimpleCache
classes.
standard_library.install_aliases()
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: None
Output names: None
Length: 0
Cache¶
It is possible to manually add some data into the cache by using the following lines:
data = {"x": array([1.0]), "y": array([2.0])}
cache.cache_outputs(data, ["x"], data, ["y"])
We can add another entry to the cache, and we can then see that its length is
still one. Indeed, as previously mentionned, the SimpleCache
only
enable to store one evaluation.
data = {"x": array([2.0]), "y": array([3.0])}
cache.cache_outputs(data, ["x"], data, ["y"])
print(cache)
Out:
Name: SimpleCache
Type: SimpleCache
Tolerance: 0.0
Input names: x
Output names: y
Length: 1
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(cache.get_length())
print(cache.get_all_data())
Out:
1
{1: {'inputs': {'x': array([2.])}, 'outputs': {'y': array([3.])}, 'jacobian': None}}
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.
print(cache.get_last_cached_inputs())
print(cache.get_last_cached_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: None
Output names: None
Length: 0
Total running time of the script: ( 0 minutes 0.004 seconds)