GlennOPT base classes

Individual Class

class glennopt.base.individual.Individual(eval_parameters: List[Parameter], objectives: List[Parameter], performance_parameters: List[Parameter])[source]

This class represents each individual or “evaluation”. In each evaluation there are a set of parameters for objectives as well as additional parameters that are kept track.

property IsFailed: bool

Checks the objectives to make sure the individual hasn’t failed

Returns

True = failed, False = passed

Return type

bool

__hash__()[source]

Return hash(self).

__str__()[source]

Return str(self).

constraints() ndarray[source]

Get the constraint matrix

Returns

Return the constraint matrix

Return type

np.ndarray

property eval_parameter_max: ndarray

returns the maximum value of each evaluation parameter as a numpy array

Returns

Array containing maximum values of evaluation parameter

Return type

np.ndarray

property eval_parameter_min: ndarray

Gets the minimum value of all the evaluation parameters

Returns

Array containing the minimum values of the evaluation paramters.

Return type

np.ndarray

property eval_parameters: ndarray

Get eval parameters

Returns

Returns a numpy array with the evaluation parameters

Return type

np.ndarray

get_eval_parameter(name: str) Parameter[source]

Gets the evaluation parameter containing name

Parameters

name (str) – Name of the evaluation parameter to get

Returns

Parameter object containing name or None

Return type

Parameter

get_eval_parameter_list() List[Parameter][source]

Gets all the evaluation parameters

Returns

List of evaluation parameters

Return type

List[Parameter]

get_objective(name: str) Parameter[source]

Searches for an objective based on a name provided and returns the objective as a parameter object

Parameters

name (str) – name of the objective. Example: “objective 1”

Returns

objective as parameter object

Return type

Parameter

get_objectives_list() List[Parameter][source]

Returns a list of objectives as parameters

Returns

List of objectives

Return type

List[Parameter]

get_performance_parameter(name: str) Parameter[source]

Gets the performance parameter given a name

Parameters

name (str) – name of performance parameter to get

Returns

Performance parameter or None

Return type

Parameter

get_performance_parameters_list() List[Parameter][source]

Gets a list of performance parameters

Returns

list of performance parameters

Return type

List[Parameter]

property name: str

Name of the individual evaluation. For example IND000

Returns

name of the individual

Return type

str

property objectives: ndarray

Returns a numpy array of the objective values

Parameters
  • include_constraint (bool, optional) – Whether or not to include constraints. Value of the objects are skewed if constraints are violated. Defaults to True.

  • C (float, optional) – Penalty coefficient. Defaults to 0.5.

  • a (int, optional) – Penalty exponential coefficient. Defaults to 2.

Returns

Array of values. For example [1,2]

Return type

np.ndarray

property performance_parameters: ndarray

Gets the performance parameters as a numpy array

Returns

numpy array of performance parameters

Return type

np.ndarray

property population: int

returns the population

Returns

[description]

Return type

int

set_eval_parameter(name: str, val: float)[source]

set the evaluation parameter value given a name

Parameters
  • name (str) – Name of the parameter

  • val (float) – value to set to

set_eval_parameter_at_indx(indx: int, val: float)[source]

Set the evaluation parameter value at a particular index

Parameters
  • indx (int) – Index in parameter array

  • val (float) – Value to replace with

set_objective(name: str, val: float)[source]
Assigns a value to a parameter for an individual

typically called by the optimizer objectives[2] = 450

Parameters
  • name (str) – Name of the objective. Example: “Objective 1”

  • val (float) – value to set the objective to

set_performance_parameter(name: str, val: float)[source]

Sets the performance parameter given the name

Parameters
  • name (str) – name of the parameter

  • val (float) – value to set it to

set_performance_parameter_at_indx(indx: int, val: float)[source]

Sets the performance parameter at an index

Parameters
  • indx (int) – index of performance parameter

  • val (float) – [description]

Optimizer Base Class

Optimizer - A base abstract class where all optimizers will inherit from. This class is never to be instantiated directly

class glennopt.base.optimizer.Optimizer(name: str, eval_command: str, eval_folder: Optional[str] = None, opt_folder: Optional[str] = None, eval_parameters: List[Parameter] = [], objectives: List[Parameter] = [], performance_parameters: List[Parameter] = [], single_folder_eval=False, overwrite_input_file=False)[source]

Base class for starting an optimization

__check_PID_running__(pid: int) bool[source]

Checks if process id is running. this checks by trying to kill the process. if there’s an error then it’s running

Parameters

pid (int) – integer corresponding to the process id

Returns

true if process is still running

Return type

bool

__check_population_folder__(population_number: int = 0) str[source]

Formats the population folder

Parameters

population_number (int, optional) – Population Number for example 20. Defaults to 0.

Returns

relative path to the population folder

Return type

str

__check_process_running__(p: Popen) bool[source]

Checks if a process id is running. This checks by using the poll method.

Parameters

p (subprocess.Popen) – Popen class

Returns

true if process id is still running

Return type

bool

__create_input_file__(individual: Individual)[source]

Creates an input file ‘inputs.txt’ which the evaluation script reads. Will only create if inputs.txt does not already exist

Parameters

individual (Individual) – Individual’s evaluation parameters are read x[1-N] are printed to an evaluation script

__evaluate_individual__(individual: Individual, individual_directory: str, cores_per_execution: list = []) Tuple[int, Popen][source]

Evaluates the individual by copying the evaluation folder into the individual’s directory

Parameters
  • individual (Individual) – [description]

  • individual_directory (str) – [description]

  • cores_per_execution (list, optional) – [description]. Defaults to [].

Returns

tuple containing:

pid (int): process id process (subprocess.Popen): logging message string

Return type

(tuple)

__read_input_file__(individual: Individual) Individual[source]

Reads the input file i.e. input.dat line by line and parses it for evaluation parameters

Parameters

individual (Individual) – Individual object that will be set

Returns

returns an individual with updated values

Return type

Individual

__read_output_file__(individual: Individual) Individual[source]

Reads the output file i.e. output.txt line by line and parses it for objectives and parameters

Parameters

individual (Individual) – Individual object that is read and parameters are set

Returns

[description]

Return type

Individual

__select_cores_per_execution__(pid_list: list)[source]
Returns the index of cores_per_execution that is not being used

Used to find out which process id has been freed

Parameters

pid_list (list) – list of process ids from pOpen

Returns

index of process id that is no longer being used.

Return type

int

__write_proc_log__(p: Popen, pop: int, ind_name: str)[source]

Write the log for each process

Parameters
  • p (subprocess.Popen) – the Popen object

  • pop (int) – population as a ninteger

  • ind_name (str) – name of the individual

append_history_file(pop: int, best_ind: Individual, diversity: float, distance: float, train_loss: float = 0, test_loss: float = 0, mse: float = 0)[source]

Writes a history.csv file containing the best design(s) this function is called by the inheriting class

Parameters
  • pop (int) – Population index

  • best_ind (Individual) – best performing individual

  • diversity (float) – diversity of the population

  • distance (float) – average distance between individuals of the population

append_restart_file(individuals: List[Individual])[source]

Appends self.population_track to a restart file, these are the individuals matter and can be restarted from. Instead of restarting from a population, lets restart from the best individuals

Parameters

individuals (List[Individual]) – list of individuals, this can be any size. these individuals will be added to the restart file

change_working_dir(new_dir: str)[source]

Changes the current working directory

Parameters

new_dir (str) – path to new directory

create_restart()[source]

Create a restart file containing all individuals of all populations

static df_to_tecplot(df_dict: Dict[str, DataFrame], filename: str)[source]

Staticmethod to convert a normal pandas dataframe to tecplot file

Parameters

df (Dict[str,pd.DataFrame]) – For example {‘DOE’: pd.DataFrame}

evaluate_population(individuals: List[Individual], population_number: int = 0)[source]

Evaluates a population of individuals, checks to see if the population exists in the directory. If it already exists, do nothing, read the results, evaluate if there is no output file

Parameters
  • individuals (List[Individual]) – List of individuals to evaluate

  • population_number (int, optional) – population corresponding to the set of individuals. Defaults to 0.

from_dict(settings: dict)[source]

Reads the dictionary file and creates the base object

Parameters

settings (dict) – dictionary created by to_dict()

get_current_directory() str[source]

returns the current working directory

Returns

Path to current directory

Return type

str

load_history_file()[source]

Reads the history file if exists

property parallel_settings

Returns the parallel settings dataclass

Returns

parallel settings class object

Return type

parallel_settings

plot_2D(obj1_name: str, obj2_name: str, xlim: Optional[list] = None, ylim: Optional[list] = None)[source]

Creates a 2D plot scatter plot of all the individuals for the two objectives specified

Parameters
  • obj1_name (str) – name of the x-axis

  • obj2_name (str) – name of the y-axis

  • xlim (list, optional) – xbounds example [-1,2]. Defaults to None.

  • ylim (list, optional) – ybounds example [-5,5]. Defaults to None.

read_calculation_folder() List[Individual][source]
Reads the entire calculation folder to a dataframe and returns all the individuals as an array

this can be useful for restarting a population

Raises

Exception – [description]

Returns

list of all individuals for all populations (could be used as a restart)

Return type

List[Individual]

read_population(population_number: int = 0) List[Individual][source]

Reads the output file in the population/DOE folder

Parameters

population_number (int, optional) – Which population we are evaluating. Defaults to 0.

Raises

Exception – Restart population was not found

Returns

list of individuals within the population

Return type

List[Individual]

read_restart_file() List[Individual][source]

Appends self.population_track to a restart file, these are the individuals matter and can be restarted from. Instead of restarting from a population, lets restart from the best individuals

Raises

Exception – Error about restart file not being the same. this can happen if you add evaluation parameters or performance parameters. Make sure number of columns match the number of parameters. You can add null or dummy values.

Returns

individual list or empty list

Return type

List[Individual]

to_dict()[source]

Export the settings used to create the optimizer to dict. Also exports the optimization results if performed

Returns

list of all the settings, could be used for creating an optimization object

Return type

dict

to_pandas(individuals: List[Parameter], pop_number: int = 0, bReturnPandas: bool = False)[source]

Exports the results to a pandas file

Parameters
  • individuals (List[Parameter]) – [description]

  • pop_number (int, optional) – [description]. Defaults to 0.

  • bReturnPandas (bool, optional) – [description]. Defaults to False.

Returns

dataframe object with all the optimization results

Return type

pd.DataFrame

to_tecplot()[source]

Converts the dataframe to a tecplot file (.tec)

property use_calculation_folder: bool

Allows the optimizer to define calculation folders for each call.

Returns

True = use calcuation folder

Return type

bool

Parameter Class

class glennopt.base.parameter.Parameter(name: str, min_value: Optional[float] = None, max_value: Optional[float] = None, value_if_failed: float = 10000, constraint_greater_than: Optional[float] = None, constraint_less_than: Optional[float] = None, value: float = 10000)[source]

This represents a value that a single evaluation keeps track of. For example the objective or if this is an computational simulation, we keep track of the stress, strain, volume, etc., anything that isn’t a constraint.

Inputs:

name: (string) name of the parameter lower_value: (float) this represents the lower bound of the variable upper_value: (float) this represents the upper bound of the variable value_if_failed: (float) this is set to 100000, probably a good value for a minimization problem constr_less_than: (float) default is None for no constraint constr_greater_than: (float) default is None for no constraint

__eq__(other)

Return self==value.

__hash__ = None