Skip to content

Commit

Permalink
Added feature to overwrite input file
Browse files Browse the repository at this point in the history
By default the optimizer won't delete your previous input file when restarting failed simulations.
  • Loading branch information
pjuangph committed Dec 6, 2021
1 parent 1e21c14 commit e294a23
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 14 additions & 6 deletions glennopt/base/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Optimizer:
"""
Base class for starting an optimization
"""
def __init__(self,name:str,eval_command:str, eval_folder:str = None,opt_folder:str=None, eval_parameters:List[Parameter]=[], objectives:List[Parameter] = [], performance_parameters:List[Parameter] = [], single_folder_eval=False):
def __init__(self,name:str,eval_command:str, eval_folder:str = None,opt_folder:str=None, eval_parameters:List[Parameter]=[], objectives:List[Parameter] = [], performance_parameters:List[Parameter] = [], single_folder_eval=False, overwrite_input_file=False):
"""Initializes the optimizer. This is typically inherited by a particular type of optimizer like NSGA (multi-objective differential evolution) or SODE (single objective differential evolution). Any new optimization strategy should inherit from this class.
This class handles most of the heavy work of evaluation and reading the results.
Expand All @@ -43,6 +43,7 @@ def __init__(self,name:str,eval_command:str, eval_folder:str = None,opt_folder:s
objectives (List[Parameter], optional): The objectives you want to keep track of and the value for when they fail. Defaults to [].
performance_parameters (List[Parameter], optional): Number of parameters. Defaults to [].
single_folder_eval (bool, optional): Saves space by deleting the population folder when completed. by default this is false. Defaults to False.
overwrite_input_file(bool, optional): Specifies whether or not to overwrite the input file when restarting a simulation. Defaults to False.
Raises:
Exception: Invalid relative path could not be found for eval script
Expand Down Expand Up @@ -78,6 +79,7 @@ def __init__(self,name:str,eval_command:str, eval_folder:str = None,opt_folder:s


self.population_track = []
self.overwrite_input_file = overwrite_input_file
self.input_file = 'input.dat'
self.output_file = 'output.txt'
self.__use_calculation_folders = True
Expand Down Expand Up @@ -168,15 +170,21 @@ def parallel_settings(self,settings:parallel_settings):


def __create_input_file__(self,individual:Individual):
"""Creates an input file 'inputs.txt' which the evaluation script reads
"""Creates an input file 'inputs.txt' which the evaluation script reads. Will only create if inputs.txt does not already exist
Args:
individual (Individual): Individual's evaluation parameters are read x[1-N] are printed to an evaluation script
"""
with open(self.input_file,'w') as f:
eval_params = individual.get_eval_parameter_list()
for p in range(len(eval_params)):
f.write('{0} = {1}\n'.format(eval_params[p].name,eval_params[p].value))
def write_input():
with open(self.input_file,'w') as f:
eval_params = individual.get_eval_parameter_list()
for p in range(len(eval_params)):
f.write('{0} = {1}\n'.format(eval_params[p].name,eval_params[p].value))

if os.path.exists(self.input_file) and self.overwrite_input_file:
write_input()
else: # input file does not exists
write_input()

def __read_output_file__(self, individual:Individual) -> Individual:
"""Reads the output file i.e. output.txt line by line and parses it for objectives and parameters
Expand Down
6 changes: 4 additions & 2 deletions glennopt/optimizers/nsga3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
individual_list = List[Individual]

class NSGA3(Optimizer):
def __init__(self,eval_command:str = "python evaluation.py", eval_folder:str = "Evaluation",pop_size:int=128, optimization_folder:str=None,single_folder_eval=False):
def __init__(self,eval_command:str = "python evaluation.py", eval_folder:str = "Evaluation",pop_size:int=128, optimization_folder:str=None,single_folder_eval=False, overwrite_input_file=False):
"""
NSGA-3 multi-dimensional optimizer. This version has been tweaked to include restart capabilities. It can also keep track of additional parameters that can be considered part of the constraints.
Expand All @@ -32,8 +32,10 @@ def __init__(self,eval_command:str = "python evaluation.py", eval_folder:str = "
pop_size (int, optional): number of populations to evaluate from the starting population. Defaults to 128.
optimization_folder (str, optional): number of individuals in a given population. Defaults to None.
single_folder_eval (bool, optional): where optimization should start. Defaults to False.
overwrite_input_file(bool, optional): Specifies whether or not to overwrite the input file when restarting a simulation. Defaults to False.
"""
super().__init__(name="nsga3",eval_command=eval_command,eval_folder=eval_folder, opt_folder=optimization_folder,single_folder_eval=single_folder_eval)
super().__init__(name="nsga3",eval_command=eval_command,eval_folder=eval_folder, opt_folder=optimization_folder,single_folder_eval=single_folder_eval,overwrite_input_file=overwrite_input_file)

self.pop_size = pop_size
self.individuals = None
Expand Down

0 comments on commit e294a23

Please sign in to comment.