gemseo / algos

database module

A database of function calls and design variables

Classes:

Database([input_hdf_file])

Class to store evaluations of functions, such as DOE or optimization histories.

HashableNdarray(wrapped[, tight])

HashableNdarray wrapper for ndarray objects.

class gemseo.algos.database.Database(input_hdf_file=None)[source]

Bases: object

Class to store evaluations of functions, such as DOE or optimization histories.

Avoids multiple calls of the same functions, useful when simulations are costly

It is also used to store inputs and retrieve them for optimization graphical post processing and plots generation

Can be serialized to HDF5 for portability and cold post processing

Constructor.

Attributes:

GRAD_TAG

ITER_TAG

KEYSSEPARATOR

missing_value_tag

Methods:

clean_from_iterate(iterate)

Delete the iterates after a given iterate number.

clear()

Clears the database.

contains_dataname(data_name[, skip_grad])

Tests if history has a value named data_name stored.

contains_x(x_vect)

Tests if history has a design variables x stored.

export_hdf([file_path, append])

Export optimization history to hdf file.

export_to_ggobi([functions, file_path, …])

Export history to xml file format for ggobi tool.

filter(data_list_to_keep)

Keeps only the values in the data list.

get(x_vect[, default])

Return the value for key if key is in the dictionary, else default.

get_all_data_names([skip_grad, skip_iter])

Return data variables (design, functions, gradient, .

get_complete_history([functions, …])

Return complete history of optimization: design variables, functions, gradients.

get_f_of_x(fname, x_vect[, dist_tol])

If x in self, get associated “fname” value, if it exists.

get_func_grad_history(funcname[, x_hist])

Return gradient values history Can also return history of design variables.

get_func_history(funcname[, x_hist])

Return function values history.

get_gradient_name(name)

Return the name of the gradient related to a function.

get_history_array([functions, …])

Return history of optimization process.

get_index_of(x_vect)

Returns the index of a particular x.

get_last_n_x(n)

Get list of n last x ordered by calls.

get_max_iteration()

Maximum iteration number.

get_value(x_vect)

Accessor for the values.

get_x_by_iter(iteration)

Return design variables at a specified iteration.

get_x_history()

Get list of x ordered by calls.

import_from_opendace(database_file)

Reads an opendace xml database.

import_hdf([filename])

Imports a database from hdf file.

is_func_grad_history_empty(funcname)

Check if history is empty.

items()

Database items generator.

keys()

Database keys generator.

pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised

remove_empty_entries()

Removes empty entries, when x is associated to an empty dict.

set_dv_names(n_dv)

Create a list of default design variables names.

setdefault(key, default)

Sets a default database entry.

store(x_vect, values_dict[, add_iter])

Stores the values associated to the variables x.

values()

Database values generator.

GRAD_TAG = '@'
ITER_TAG = 'Iter'
KEYSSEPARATOR = '__KEYSSEPARATOR__'
clean_from_iterate(iterate)[source]

Delete the iterates after a given iterate number.

Parameters

iterate – the iterate number

clear()[source]

Clears the database.

contains_dataname(data_name, skip_grad=False)[source]

Tests if history has a value named data_name stored.

Parameters
  • data_name – the name of the data

  • skip_grad – do not account for gradient names

Returns

True if data_name is in self

contains_x(x_vect)[source]

Tests if history has a design variables x stored.

Parameters

x_vect – the design variables to test

Returns

True if x_vect is in self

export_hdf(file_path='optimization_history.h5', append=False)[source]

Export optimization history to hdf file.

Parameters
  • file_path – path to file to write (Default value = ‘optimization_history.h5’)

  • append – if True, appends the data in the file (Default value = False)

export_to_ggobi(functions=None, file_path='opt_hist.xml', design_variables_names=None)[source]

Export history to xml file format for ggobi tool.

Parameters
  • functions – Default value = None)

  • file_path – Default value = “opt_hist.xml”)

  • design_variables_names – Default value = None)

filter(data_list_to_keep)[source]

Keeps only the values in the data list.

Parameters

data_list_to_keep – the list of data names to keep

get(x_vect, default=None)[source]

Return the value for key if key is in the dictionary, else default.

get_all_data_names(skip_grad=True, skip_iter=False)[source]

Return data variables (design, functions, gradient, … Gradient variables can be skipped.

Parameters
  • skip_grad – do not list gradient names (Default value = True)

  • skip_iter – do not add Iter in the list

Returns

the list of data names in the database

get_complete_history(functions=None, add_missing_tag=False, missing_tag='NA', all_iterations=False, stacked_data=None)[source]

Return complete history of optimization: design variables, functions, gradients.

Parameters
  • functions – functions names to get (Default value = None)

  • add_missing_tag – add “missing_tag” when data is not available for this iteration (Default value = False)

  • missing_tag – the missing tag to add (Default value = ‘NA’)

  • all_iterations – if True, points called at several iterations will be duplicated in the history (each duplicate corresponding to a different calling index); otherwise each point will appear only once (with the latest calling index) (Default value = False)

  • stacked_data – list of names corresponding to data stored as lists. For example the iterations indexes are stored in a list. Other examples of stacked data may be penalization parameters or trust region radii. (Default value = None)

Returns

function history and x history as lists

get_f_of_x(fname, x_vect, dist_tol=0.0)[source]

If x in self, get associated “fname” value, if it exists.

Parameters
  • fname – the function name

  • x_vect – the design variables

Returns

the values associated to x with name fname

get_func_grad_history(funcname, x_hist=False)[source]

Return gradient values history Can also return history of design variables.

Parameters
  • funcname – the function name

  • x_hist – if True, returns variables history as well (Default value = False)

Returns

the jacobian history list

get_func_history(funcname, x_hist=False)[source]

Return function values history. Can also return history of design variables.

Parameters
  • funcname – the function name

  • x_hist – if True, returns variables history as well (Default value = False)

Returns

the function history list

classmethod get_gradient_name(name)[source]

Return the name of the gradient related to a function.

This name is the concatenation of a GRAD_TAG, e.g. ‘@’, and the name of the function, e.g. ‘f’. With this example, the name of the gradient is ‘@f’.

Parameters

name (str) – The name of a function.

Returns

The name of the gradient based on the name of the function.

Return type

str

get_history_array(functions=None, design_variables_names=None, add_missing_tag=False, missing_tag='NA', add_dv=True, all_iterations=False, stacked_data=None)[source]

Return history of optimization process.

Parameters
  • functions – functions names to export (Default value = None)

  • design_variables_names – names of the design variables (Default value = None)

  • missing_tag – missing tag to add (Default value = ‘NA’)

  • add_dv – if True, adds the design variables to the returned array (Default value = True)

  • add_missing_tag – add “missing_tag” when data is not available for this iteration (Default value = False)

  • missing_tag – the missing tag to add (Default value = ‘NA’)

  • all_iterations – if True, points called at several iterations will be duplicated in the history (each duplicate corresponding to a different calling index); otherwise each point will appear only once (with the latest calling index) (Default value = False)

  • stacked_data – list of names corresponding to data stored as lists. For example the iterations indexes are stored in a list. Other examples of stacked data may be penalization parameters or trust region radii. (Default value = None)

Returns

function history and x history as lists

get_index_of(x_vect)[source]

Returns the index of a particular x.

Parameters

x_vect – x numpy array

Returns

the index of x_vect, or throws a key error

get_last_n_x(n)[source]

Get list of n last x ordered by calls.

Returns

the list of x np arrays

get_max_iteration()[source]

Maximum iteration number.

get_value(x_vect)[source]

Accessor for the values.

Parameters

x_vect – the design variables

get_x_by_iter(iteration)[source]

Return design variables at a specified iteration.

Parameters

iteration – the iteration number

Returns

the numpy array of x at iteration

get_x_history()[source]

Get list of x ordered by calls.

Returns

the list of x np arrays

import_from_opendace(database_file)[source]

Reads an opendace xml database.

Parameters

database_file – the path to the database file

import_hdf(filename='optimization_history.h5')[source]

Imports a database from hdf file.

Parameters

filename – Default value = ‘optimization_history.h5’)

is_func_grad_history_empty(funcname)[source]

Check if history is empty.

Parameters

funcname – the function name

Returns

True if history is empty

items()[source]

Database items generator.

keys()[source]

Database keys generator.

missing_value_tag = 'NA'
pop(k[, d])v, remove specified key and return the corresponding value.[source]

If key is not found, d is returned if given, otherwise KeyError is raised

remove_empty_entries()[source]

Removes empty entries, when x is associated to an empty dict.

static set_dv_names(n_dv)[source]

Create a list of default design variables names.

Parameters

n_dv – number of design variables in problem

Returns

a list of design variables names

setdefault(key, default)[source]

Sets a default database entry.

store(x_vect, values_dict, add_iter=True)[source]

Stores the values associated to the variables x.

Parameters
  • x_vect – design variables vector

  • values_dict – values to be stored

  • add_iter – add iteration information (Default value = True)

values()[source]

Database values generator.

class gemseo.algos.database.HashableNdarray(wrapped, tight=False)[source]

Bases: object

HashableNdarray wrapper for ndarray objects.

Instances of ndarray are not HashableNdarray, meaning they cannot be added to sets, nor used as keys in dictionaries. This is by design - ndarray objects are mutable, and therefore cannot reliably implement the __hash__() method.

The HashableNdarray class allows a way around this limitation. It implements the required methods for HashableNdarray objects in terms of an encapsulated ndarray object. This can be either a copied instance (which is safer) or the original object (which requires the user to be careful enough not to modify it).

Creates a new HashableNdarray object encapsulating an ndarray.

:param wrapped:The wrapped ndarray. :param tight: If True, a copy of the input ndaray is created.

Methods:

unwrap()

Returns the encapsulated ndarray.

unwrap()[source]

Returns the encapsulated ndarray.

If the wrapper is “tight”, a copy of the encapsulated ndarray is