Solver Applications in Optimization and Network Modeling

The provided code constitutes a comprehensive framework for research and development in the fields of artificial intelligence and computational biology. Its architecture consists of specialized modules addressing key aspects of modeling, machine learning, and optimization, with an emphasis on code robustness and reusability.

From a technical perspective, the code includes:

Training Data Generation: Functionality for generating synthetic data and manipulating real datasets, with advanced options for controlled noise introduction and simulation of specific scenarios.

Machine Learning Model Development: Tools for constructing and customizing machine learning models, including deep neural network architectures and supervised and unsupervised learning algorithms.

Performance Evaluation: Capabilities for systematically evaluating model performance under various conditions, using statistical metrics and visualizations to analyze prediction quality and generalization ability.

Optimization Solver Comparison: Functionality for comparing different optimization methods used in solving specific problems, with tools for measuring convergence, computational efficiency, and scalability.

The underlying technical approach is based on the efficient implementation of algorithms and data structures, leveraging high-performance software libraries, and adopting software engineering practices such as modularity, encapsulation, and detailed documentation.

class optimization.model_synthetic_data_.Evaluate(result_path: str = '')[source]

Bases: object

A class to handle the evaluation of models with respect to their Mean Absolute Percentage Error (MAPE) metrics on various noise levels, using different solvers for optimization problems.

result_path

The path where results should be saved.

Type:

str

model_columns

List of column names for the models.

Type:

List[str]

solvers

An array of solver names.

Type:

np.ndarray

signal_to_noise_levels

List of signal-to-noise ratios to be evaluated, including None for the original signal.

Type:

List[Optional[int]]

ModelEval(output_path: str = '') Tuple[pandas.DataFrame, pandas.DataFrame][source]

Evaluate the model performance by measuring Mean Absolute Percentage Error (MAPE) on different levels of noise and save the results to Excel files.

Parameters:

output_path (str, optional) – The directory path where Excel files will be saved. Defaults to an empty string.

Returns:

A tuple containing two pandas DataFrames of MAPE for y and pi respectively.

Return type:

Tuple[pd.DataFrame, pd.DataFrame]

__init__(result_path: str = '')[source]

Initialize the Evaluate class with default parameters.

Parameters:

result_path (str, optional) – The path where results should be saved. Defaults to an empty string.

create_custom_model(input_size: int = 5, num_layers: int = 5, dropout_rate: float = 0.1, regularization_factor: float = 0.0001, learning_rate: float = 0.001, loss: str = 'huber') keras.Model[source]

Create a custom Keras model with specified parameters and constraints.

Parameters:
  • input_size (int, optional) – The size of the input layer. Default is 5.

  • num_layers (int, optional) – The number of neurons in each dense layer. Default is 5.

  • dropout_rate (float, optional) – The dropout rate for regularization. Default is 0.1.

  • regularization_factor (float, optional) – The regularization factor for L1/L2 regularization. Default is 1e-4.

  • learning_rate (float, optional) – The learning rate for the optimizer. Default is 1e-3.

  • loss (str, optional) – The loss function selector. Default is ‘huber’.

Returns:

A compiled Keras model.

Return type:

Model

data_generate(M: int = 10, N: int = 120, s: int = 5) None[source]

Generate data sets for the optimization problem with given parameters M, N, and s.

Parameters:
  • M (int, optional) – Number of samples to generate. Default is 10.

  • N (int, optional) – The number of observations for each sample. Default is 120.

  • s (int, optional) – The number of elements in the PIC vector. Default is 5.

  • effects (Side) –

  • ------------

  • (PIC.npy (Saves arrays to disk) –

  • Z.npy

  • U.npy

  • Y.npy).

  • console. (Prints the shapes of the generated arrays to the) –

evaluate_solver(solver: str, MAPE_y: pandas.DataFrame, MAPE_pi: pandas.DataFrame) Tuple[pandas.DataFrame, pandas.DataFrame][source]

Evaluate the solver performance by comparing the predicted results against the true values and update the Mean Absolute Percentage Error (MAPE) dataframes for y and pi variables.

Parameters:
  • solver (str) – The name of the solver used for the optimization problem.

  • MAPE_y (pd.DataFrame) – The DataFrame containing existing MAPE values for y.

  • MAPE_pi (pd.DataFrame) – The DataFrame containing existing MAPE values for pi.

Returns:

Updated MAPE dataframes for y and pi variables.

Return type:

Tuple[pd.DataFrame, pd.DataFrame]

history_plot() pandas.DataFrame[source]

Plot the training history for different loss functions.

Returns:

A DataFrame containing the history of the model’s loss and validation loss.

Return type:

pd.DataFrame

plot(solver: str, path: str = '') None[source]

Plot the Mean Absolute Percentage Error (MAPE) for model predictions using a specified solver.

Parameters:
  • solver (str) – The solver used for the optimization problem.

  • path (str, optional) – The directory path where the data files are located. Defaults to an empty string.

Raises:

FileNotFoundError – If the path does not contain the expected files.

class optimization.model_synthetic_data_.FlyEvaluate(N: int = 100, C: int = 500, t: int = 1)[source]

Bases: object

A class designed to evaluate a population growth model for fruit flies, and compare the performance of different solvers.

__init__(N: int = 100, C: int = 500, t: int = 1) None[source]

Initialize FlyEvaluate with default parameters.

Parameters:
  • N (int, optional) – Number of simulation runs. Default is 100.

  • C (int, optional) – Number of cycles in the simulation. Default is 500.

  • t (int, optional) – Time lag in the model. Default is 1.

evaluate_solver(solver: str, MAPE_y: pandas.DataFrame, MAPE_pi: pandas.DataFrame) Tuple[pandas.DataFrame, pandas.DataFrame][source]

Evaluate the solver performance and update the Mean Absolute Percentage Error (MAPE) dataframes.

Parameters:
  • solver (str) – The name of the solver used for the optimization problem.

  • MAPE_y (pd.DataFrame) – DataFrame containing MAPE values for y.

  • MAPE_pi (pd.DataFrame) – DataFrame containing MAPE values for pi.

Returns:

Updated MAPE dataframes for y and pi variables.

Return type:

Tuple[pd.DataFrame, pd.DataFrame]

fly_data(P: float, N0: float, d: float, e2: float, e1: float, C: int) Tuple[numpy.ndarray, numpy.ndarray][source]

Generate data for the fly population growth and corresponding weight matrices.

Parameters:
  • P (float) – Growth rate.

  • N0 (float) – Initial population.

  • d (float) – Death rate.

  • e2 (float) – Error term for the death rate.

  • e1 (float) – Error term for the growth rate.

  • C (int) – Number of cycles.

Returns:

Tuple of N (fly population) and W (weights) both as numpy arrays.

Return type:

Tuple[np.ndarray, np.ndarray]

generate() None[source]

Generate and save data for fly population growth, as well as the optimization problems for different solvers.

history_plot_loss(path: str = '') pandas.DataFrame[source]

Plot the history of loss and validation loss for different loss functions.

Parameters:

path (str, optional) – The directory path where the data files are to be loaded from. Defaults to an empty string.

Returns:

A DataFrame with the history of the model’s loss and validation loss.

Return type:

pd.DataFrame

model_eval(path: str = '') Tuple[pandas.DataFrame, pandas.DataFrame][source]

Evaluate the model and compute Mean Absolute Percentage Error (MAPE) for predictions.

Parameters: - path (str): The directory path to load and save data files. Defaults to an empty string.

Returns: - Tuple[pd.DataFrame, pd.DataFrame]: A tuple containing two pandas DataFrames with MAPE for predictions and weights.

model_op(loss: str = 'huber') keras.Model[source]

Create a Keras model with a set of dense layers and custom regularization.

Parameters: - loss (str): The loss function selector. Defaults to ‘huber’.

Returns: - Model: A compiled Keras model.

parameters() Tuple[float, float, float, float, float, float, float][source]

Generate parameters for the growth model using random normal distributions.

Returns:

A tuple of parameters (P, d, N0, od, op, e1, e2).

Return type:

Tuple[float, float, float, float, float, float, float]

plot_results(path: str = '') None[source]

Plot the Mean Absolute Percentage Error (MAPE) results obtained from the evaluations of the model and solver.

Parameters:

path (str, optional) – The directory path from which the data files are to be loaded. Default is an empty string.

Return type:

None

class optimization.model_synthetic_data_.SoftmaxWeightConstraint(*args: Any, **kwargs: Any)[source]

Bases: Constraint

Constraint class that applies a softmax to the weights.

__call__(weights: tensorflow.Tensor) tensorflow.Tensor[source]

Applies softmax on the weights along the specified axis.

Parameters:

weights (tf.Tensor) – The tensor to which the softmax function will be applied.

Returns:

The tensor with softmax applied.

Return type:

tf.Tensor

__init__(softmax_function: Callable[[tensorflow.Tensor, int], tensorflow.Tensor])[source]

Initializes the constraint using a given softmax function.

Parameters:

softmax_function (Callable[[tf.Tensor, int], tf.Tensor]) – The softmax function to be applied on the weights.

get_config() dict[source]

Gets the configuration of the constraint.

Returns:

A dictionary containing the configuration of the constraint.

Return type:

dict

optimization.model_synthetic_data_.add_noise(data: numpy.ndarray, snr: float, mu: float = 0.0) numpy.ndarray[source]

Generate noise to be added to a signal with a specified signal-to-noise ratio (SNR).

Parameters:
  • data (np.ndarray) – The original signal data.

  • snr (float) – The desired signal-to-noise ratio in decibels.

  • mu (float, optional) – The mean value for the noise generation, default is 0.0.

Returns:

The generated noise array with the same shape as the input data.

Return type:

np.ndarray

optimization.model_synthetic_data_.minimize_l1_norm(y: numpy.ndarray, u: numpy.ndarray, s: int, solver: str = 'CPLEX') numpy.ndarray[source]

Solve the optimization problem to find vector z that minimizes the L1 norm between y and the product of matrix u and vector z, with constraints that z sums to 1 and each entry in z is between 0 and 1 inclusive.

Parameters:
  • y (np.ndarray) – The observed data vector (n by 1).

  • u (np.ndarray) – The design matrix (n by s).

  • s (int) – The length of the solution vector z.

  • solver (str, optional) – The solver used for the optimization problem, default is ‘CPLEX’.

Returns:

The solution vector z with shape (s, 1).

Return type:

np.ndarray

optimization.model_synthetic_data_.probability_vector(num_elements: int = 5) numpy.ndarray[source]

Generate an array of size num_elements with elements rounded to two decimal places such that the sum is 1.0.

Parameters:

num_elements (int, optional) – Number of elements in the generated array. Default is 5.

Returns:

A 1-D array of shape (num_elements, 1) with elements summing to 1.0.

Return type:

np.ndarray

optimization.model_synthetic_data_.random_matrix(num_ranges: int = 5, num_rows: int = 10) numpy.ndarray[source]

Generate a matrix with uniformly distributed random values in the range [i-1, i], where i iterates from 1 to num_ranges, as columns, and num_rows rows.

Parameters:
  • num_ranges (int, optional) – Number of different ranges to use for each column. Default is 5.

  • num_rows (int, optional) – Number of rows in the generated matrix. Default is 10.

Returns:

A (num_rows, num_ranges) matrix of uniformly distributed random values.

Return type:

np.ndarray