otaf.uncertainty package

Module contents

class otaf.uncertainty.SocAssemblyAnalysisOptimized(binary_classifier, constraint_matrix_generator, X_optimization, y_optimization, calculate_true_positives=True)[source]

Bases: object

Class to do do system of constraint based tolerance analysis for assembly, using a binary classifier to pre-treat the inputs for the optimizer, to evaluate less points.

__repr__()[source]

Representation of the class providing information on threshold optimization and other details.

Returns:

String representation of the class.

Return type:

str

binary_input_array_classification(X, **kwargs)[source]
evaluate_binary_classifier(X, batch_size=50000, return_on_gpu=False)[source]
optimize_thresholds(bounds=[-5.0, 5.0], **kwargs)[source]
soc_optimization_sample(X, C=None, bounds=None, batch_size=1000, n_cpu=1, progress_bar=False, verbose=0, dtype='float32', batch_size_ai=100000)[source]

Perform optimization on samples excluding the ones classified as true negatives or true positives.

Parameters:
  • X (np.ndarray) – Input array of shape NxD.

  • bounds (Union[list[list[float]], np.ndarray], optional) – Bounds for the variables.

  • batch_size (int, optional) – Batch size for parallel processing.

  • n_cpu (int, optional) – Number of CPUs to use for parallel processing.

  • progress_bar (bool, optional) – Whether to show a progress bar.

Returns:

Combined results of predictions and optimizations.

Return type:

np.ndarray

otaf.uncertainty.compute_adaptive_failure_probability(constraint_matrix_generator, deviation_array, C=None, bounds=None, epsilon_pf=0.001, epsilon_std=0.005, verbose=0, **kwargs)[source]

Calculate the failure probability for a sample of defects.

Parameters:
  • deviation_array (np.ndarray) – Array representing the range of random deviations for each variable.

  • constraint_matrix_generator (Callable) – Class responsible for fixing deviations and defining constraints.

  • bounds (List, optional) – Bounds for the gap variables. Defaults to None.

  • C (np.ndarray, optional) – Coefficient matrix for the linear objective function. Defaults to None.

  • method (str, optional) – The algorithm to use in the optimization problem. Defaults to ‘highs’.

  • epsilon_pf (float, optional) – Convergence threshold for the ratio of change in failure probability. Defaults to 0.001.

  • epsilon_std (float, optional) – Convergence threshold for the standard deviation of failure probabilities. Defaults to 0.005.

  • verbose (int, optional) – Verbosity level for printing progress. Defaults to 0.

Returns:

Failure probability for the fixed deviations.

Return type:

float

Raises:

TypeError – If constraint_matrix_generator is not callable.

otaf.uncertainty.compute_failure_probability_FORM(otFunc, composed_distribution, threshold=0.0, start_point=None, verbose=False, solver=None)[source]
otaf.uncertainty.compute_failure_probability_NAIS(func, distribution, threshold=0.0, quantile_level=0.001, verbose=False)[source]

Compute the failure probability using the NAIS algorithm.

Parameters: - func: The function g(X) - distribution: The input random vector distribution - threshold: The threshold value (default=0.0) - verbose: Print additional information (default=False)

Returns: - proba: The estimated failure probability - result: Additional NAIS algorithm results (see docstring)

Return type:

tuple[float, SimulationResult]

Parameters:
  • func (PythonFunction)

  • distribution (ComposedDistribution)

  • threshold (float)

  • quantile_level (float)

  • verbose (bool)

otaf.uncertainty.compute_failure_probability_SUBSET(func, distribution, threshold=0.0, verbose=False, proposalRange=2, targetProbability=0.1)[source]

Compute the failure probability using the NAIS algorithm.

Parameters: - func: The function g(X) - distribution: The input random vector distribution - threshold: The threshold value (default=0.0) - verbose: Print additional information (default=False)

Returns: - proba: The estimated failure probability - result: Additional NAIS algorithm results (see docstring)

Return type:

tuple[float, SimulationResult]

Parameters:
  • func (PythonFunction)

  • distribution (ComposedDistribution)

  • threshold (float)

  • verbose (bool)

otaf.uncertainty.compute_failure_probability_basic(constraint_matrix_generator, deviation_array, C=None, bounds=None, n_cpu=1)[source]

Calculate failure probability for a sample of defects.

Parameters:
  • constraint_matrix_generator (Callable) – Class for fixing deviations and defining constraints.

  • deviation_array (np.ndarray) – Array of random deviations for each variable.

  • C (np.ndarray, optional) – Coefficient matrix for the linear objective function. Defaults to None.

  • bounds (List, optional) – Bounds for gap variables. Defaults to None.

  • method (str, optional) – Algorithm for the optimization problem. Defaults to ‘highs’.

  • n_cpu (int, optional) – Number of CPUs to use for parallel execution. Defaults to 1.

Returns:

Failure probability for the fixed deviations.

Return type:

float

Raises:

TypeError – If constraint_matrix_generator is not callable.

otaf.uncertainty.compute_failure_probability_subset_sampling(constraint_matrix_generator, defect_distribition_vector, C=None, bounds=None, n_cpu=1)[source]

Calculate failure probability for a sample of defects.

Parameters:
  • constraint_matrix_generator (Callable) – Class for fixing deviations and defining constraints.

  • deviation_array (np.ndarray) – Array of random deviations for each variable.

  • C (np.ndarray, optional) – Coefficient matrix for the linear objective function. Defaults to None.

  • bounds (List, optional) – Bounds for gap variables. Defaults to None.

  • method (str, optional) – Algorithm for the optimization problem. Defaults to ‘highs’.

  • n_cpu (int, optional) – Number of CPUs to use for parallel execution. Defaults to 1.

  • defect_distribition_vector (RandomVector)

Returns:

Failure probability for the fixed deviations.

Return type:

float

Raises:

TypeError – If constraint_matrix_generator is not callable.

otaf.uncertainty.compute_gap_optimizations_on_sample(constraint_matrix_generator, deviation_array, C=None, bounds=None, n_cpu=1, progress_bar=False)[source]

Compute gap optimizations on a sample using a constraint matrix generator.

Parameters:
  • constraint_matrix_generator (otaf.SystemOfConstraintsAssemblyModel) – Generator for constraint matrices.

  • deviation_array (np.ndarray) – Deviation array.

  • C (np.ndarray, optional) – Coefficient matrix for the linear objective function.

  • bounds (Union[list[list[float]], np.ndarray], optional) – Bounds for the variables.

  • n_cpu (int, optional) – Number of CPUs to use for parallel processing.

  • progress_bar (bool, optional) – Whether to show a progress bar.

  • verbose (int, optional) – Level of verbosity for debugging.

Returns:

List of optimization results.

Return type:

list

otaf.uncertainty.compute_gap_optimizations_on_sample_batch(constraint_matrix_generator, deviation_array, C=None, bounds=None, n_cpu=1, batch_size=1000, progress_bar=False, verbose=0, dtype='float32')[source]

Compute gap optimizations on a sample using a constraint matrix generator with batching.

Parameters:
  • constraint_matrix_generator (otaf.SystemOfConstraintsAssemblyModel) – Generator for constraint matrices.

  • deviation_array (np.ndarray) – Deviation array.

  • C (np.ndarray, optional) – Coefficient matrix for the linear objective function.

  • bounds (Union[list[list[float]], np.ndarray], optional) – Bounds for the variables.

  • n_cpu (int, optional) – Number of CPUs to use for parallel processing.

  • progress_bar (bool, optional) – Whether to show a progress bar.

  • verbose (int, optional) – Level of verbosity for debugging.

Returns:

List of optimization results.

Return type:

list

otaf.uncertainty.compute_gap_optimizations_on_sample_w_steps(constraint_matrix_generator, deviation_array, C=None, bounds=None, n_cpu=1, verbose=0, progress_bar=False)[source]

Calculate failure probability for a sample of defects.

Parameters:
  • constraint_matrix_generator (Callable) – Class for fixing deviations and defining constraints.

  • deviation_array (np.ndarray) – Array of random deviations for each variable.

  • C (np.ndarray, optional) – Coefficient matrix for the linear objective function. Defaults to None.

  • bounds (List, optional) – Bounds for gap variables. Defaults to None.

  • n_cpu (int, optional) – Number of CPUs to use for parallel execution. Defaults to 1.

  • verbose (int, optional) – Verbosity level. Defaults to 0.

  • progress_bar (bool, optional) – Show progress bar. Defaults to False.

Returns:

List of optimization results including intermediate steps.

Return type:

List

Raises:

TypeError – If constraint_matrix_generator is not callable.

otaf.uncertainty.intermediate_lambda_function(lambdas)[source]

Ensure sum(lambdas**2) == 1.

Return type:

ndarray

Parameters:

lambdas (ndarray)

otaf.uncertainty.intermediate_lambda_function_derivative(lambdas)[source]

Derivative of the lambda function.

Return type:

ndarray

Parameters:

lambdas (ndarray)

otaf.uncertainty.lambda_sample_composition(lambdas, sample)[source]

Compose lambdas with samples.

Return type:

ndarray

Parameters:
  • lambdas (ndarray)

  • sample (ndarray)

otaf.uncertainty.milp_batch_sequential(c, bounds, a_ub, b_ub, a_eq, b_eq)[source]

Optimize a batch of linear optimization problems iteratively.

Parameters:
  • c (array_like) – Coefficients of the linear objective function to be minimized.

  • bounds (array_like) – An (n, 2) array defining the lower and upper bounds of variables.

  • a_ub (array_like) – 2-D array for the upper-bound inequality constraints.

  • b_ub (array_like) – 2-D array of upper-bound values for each inequality constraint per problem.

  • a_eq (array_like) – 2-D array for the equality constraints.

  • b_eq (array_like) – 2-D array of equality constraint values per problem.

Returns:

2-D array of optimized decision variables for each problem in the batch.

Return type:

np.ndarray

Notes

Solves a batch of mixed-integer linear programming (MILP) problems iteratively using the milp solver from scipy.optimize. Each problem shares c, a_ub, and a_eq, but has unique b_ub and b_eq vectors. Solver options disable display (disp: False) and enable presolve (presolve: True).

otaf.uncertainty.monte_carlo_non_compliancy_rate_at_threshold_w_gradient(lambdas, compliancy_threshold, sample, means, standards, model, model_is_bool=False)[source]

Compute non-compliancy rate and its gradient at a given lambda.

otaf.uncertainty.monte_carlo_non_compliancy_rate_gradient(input_samples, output_non_compliancy_sample, input_distribution_mean, input_distribution_standard, ref_standards)[source]

Gradient of non-compliancy rate w.r.t. standard deviations.

Return type:

ndarray

Parameters:
  • input_samples (ndarray)

  • output_non_compliancy_sample (ndarray)

  • input_distribution_mean (ndarray | float)

  • input_distribution_standard (ndarray | float)

  • ref_standards (ndarray | list)

otaf.uncertainty.monte_carlo_non_compliancy_rate_gradient_start_space(lambdas, gradient_composed)[source]

Gradient in the space of lambdas.

Return type:

ndarray

Parameters:
  • lambdas (ndarray)

  • gradient_composed (ndarray)

otaf.uncertainty.monte_carlo_non_compliancy_rate_w_gradient(compliancy_threshold, sample, means, standards, model, model_is_bool=False)[source]

Partial function for computing non-compliancy rate and gradient.

otaf.uncertainty.normal_score_mu(sample, mean, std)[source]

Gradient of log Gaussian w.r.t. mean.

Return type:

ndarray

Parameters:
  • sample (ndarray)

  • mean (ndarray | float)

  • std (ndarray | float)

otaf.uncertainty.normal_score_sigma(sample, mean, std)[source]

Gradient of log Gaussian w.r.t. standard deviation.

Return type:

ndarray

Parameters:
  • sample (ndarray)

  • mean (ndarray | float)

  • std (ndarray | float)

otaf.uncertainty.sample_non_compliancy_at_threshold(model_results=None, compliancy_threshold=0.0, optimizations=None, optimization_variable=False)[source]

Calculate non-compliancy at a specified threshold level.

Parameters:
  • model_results (np.ndarray, optional) – Array of gap values for each sample. If provided, non-compliancy is determined based on these values.

  • compliancy_threshold (float, optional) – Threshold value for determining compliancy. Defaults to 0.0.

  • optimizations (List[OptimizeResult], optional) – List of optimization results. Used if model_results is not provided. Defaults to None.

  • optimization_variable (bool, optional) – Flag indicating whether the optimizations represent a variable to be checked for compliancy. Defaults to False.

Returns:

Array indicating non-compliancy at each sample or optimization result. Each element is 1 if the

sample/optimization is non-compliant (below threshold), otherwise 0.

Return type:

np.ndarray

otaf.uncertainty.sample_non_compliancy_rate(non_compliancy_indicator_sample)[source]

Returns the mean of an indicator sample. The sample is supposed to be representative of the distribution and only is valid in monte carlo simulations.

Parameters:

non_compliancy_indicator_sample (ndarray)