optimizer_vector.py#
Lightweight vector wrapper for driver-level design variables and responses.
- class openmdao.vectors.optimizer_vector.OptimizerVector(voi_type, data, metadata, driver_scaling=False)[source]
Bases:
objectProvides name-based indexing over optimizer-level design variable or response vectors.
This is a lightweight dict-like wrapper, not a System Vector. It provides convenient access for optimization algorithms.
- Parameters:
- voi_typestr (‘design_var’, ‘constraint’, ‘objective’, or ‘lagrange_multiplier’)
A string specifying the type of optimization variable in the vector.
- datandarray
Flat numpy array containing variable values.
- metadatadict
Metadata dict mapping variable names to index information. Each entry should contain ‘slice’ and ‘size’ keys.
- driver_scalingbool
True if the data provided is in driver/optimizer-scaled space.
- Attributes:
- voi_typestr (‘design_var’, ‘constraint’, or ‘objective’)
A string specifying the type of optimization variable in the vector.
- _datandarray
Flat numpy array containing variable values.
- _metadict
Metadata dict mapping variable names to index information. Each entry should contain ‘slice’ and ‘size’ keys.
- _filtersdict[tuple, ndarray]
Cache for computed filter indices. Keys are tuples of sorted (key, value) filter criteria, values are integer arrays of indices into _data. Used by asarray() to avoid recomputing indices for repeated filter calls.
- _dist_driver_varsdict[str, tuple]
Mapping of distributed driver variables to (local_indices, sizes, _)
Methods
asarray(**kwargs)Return underlying flat numpy array, optionally filtered by metadata.
create_from_model(voi_type, driver[, ...])Populate the data in the vector based on values from the model.
items()Iterate over (name, value) pairs.
keys()Return variable names.
set_data(val[, driver_scaling])Set the values of the internal vector.
update_from_model(driver[, driver_scaling])Populate the data in the vector based on values from the model.
values()Iterate over variable values.
- __contains__(name)[source]
Check if variable name exists.
- Parameters:
- namestr
Variable name to check.
- Returns:
- bool
True if variable exists, False otherwise.
- __getitem__(name)[source]
Get variable value by name.
- Parameters:
- namestr
Variable name (promoted or alias).
- Returns:
- ndarray
1D array of variable values.
- Raises:
- KeyError
If variable name not found.
- __init__(voi_type, data, metadata, driver_scaling=False)[source]
Initialize OpimizerVector with data array and metadata.
- __iter__()[source]
Iterate over variable names.
- Yields:
- str
Variable names in iteration order.
- __setitem__(name, value)[source]
Set variable value by name.
- Parameters:
- namestr
Variable name (promoted or alias).
- valuefloat or ndarray
New value for the variable.
- Raises:
- KeyError
If variable name not found.
- asarray(**kwargs) ndarray[source]
Return underlying flat numpy array, optionally filtered by metadata.
When filters are provided, returns only elements whose metadata matches ALL specified criteria (AND logic). Filtered results are copies due to NumPy fancy indexing. Unfiltered results (no kwargs) return a view.
- Parameters:
- **kwargsdict
Filter criteria based on metadata keys. For example: - linear=False : only nonlinear constraints - linear=True : only linear constraints - equals=None : only inequality constraints Multiple criteria are combined with AND logic.
- Returns:
- ndarray
The underlying data array. If no filters provided, returns a view of the full array. If filters provided, returns a filtered copy.
Notes
Filter results are cached in self._filters for performance. Repeated calls with the same filters reuse cached indices but still return a copy (unavoidable with NumPy fancy indexing).
- classmethod create_from_model(voi_type: Literal['design_var', 'constraint', 'objective'], driver: Driver, driver_scaling: bool = True)[source]
Populate the data in the vector based on values from the model.
Note that this only returns the continuous values.
- Parameters:
- voi_typestr
The kind of vector being created (‘design_var’, ‘constraint’, or ‘objective’).
- driverDriver
The driver that owns this OptimizerVector.
- driver_scalingbool
If True, return vector values in the optimizer-scaled space. Default is True.
- Returns:
- OptimizerVector
A new OptimizerVector.
- property driver_scaling
Get the current scaling status of the vector.
- Returns:
- bool
True if the vector is currently in driver/optimizer-scaled space.
- items()[source]
Iterate over (name, value) pairs.
- Yields:
- tuple
(variable_name, variable_value) pairs in iteration order.
- keys()[source]
Return variable names.
- Returns:
- dict_keys
View of variable names.
- property metadata
Access the internal metadata dictionary for each variable.
- Returns:
- dict
The dictionary of metadata keyed by optimization variable name/alias.
- set_data(val, driver_scaling=True)[source]
Set the values of the internal vector.
The size of val must match the size of the internal data vector. val is flattened before being set into _data.
- Parameters:
- valArrayLike or dict[str, ArrayLike]
Values to which the entire internal vector should be set. May be given as an array-like value or as a dict (as in pyoptsparse) that maps names to associated values.
- driver_scalingbool
If True, set the data to the optimization-scaled values.
- update_from_model(driver, driver_scaling=True)[source]
Populate the data in the vector based on values from the model.
Note that this only returns the continuous values.
- Parameters:
- driverDriver
The driver that owns this OptimizerVector.
- driver_scalingbool
If True, return vector values in the optimizer-scaled space. Default is True.
- values()[source]
Iterate over variable values.
- Yields:
- ndarray
Variable values in iteration order.