Skip to content

Commit

Permalink
add option to simulate until target number is reached
Browse files Browse the repository at this point in the history
  • Loading branch information
gituser789 committed Oct 7, 2024
1 parent b477c46 commit 3840e83
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions femmt/optimization/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pickle
import logging
import shutil
from typing import List
from typing import List, Optional
import json

# 3rd party libraries
Expand Down Expand Up @@ -326,8 +326,8 @@ def single_reluctance_model_simulation(reluctance_input: ReluctanceModelInput) -
return reluctance_model_output

@staticmethod
def start_proceed_study(config: InductorOptimizationDTO, number_trials: int,
storage: str = 'sqlite',
def start_proceed_study(config: InductorOptimizationDTO, number_trials: Optional[int] = None,
target_number_trials: Optional[int] = None, storage: str = 'sqlite',
sampler=optuna.samplers.NSGAIIISampler(),
) -> None:
"""
Expand All @@ -337,6 +337,8 @@ def start_proceed_study(config: InductorOptimizationDTO, number_trials: int,
:type config: ItoSingleInputConfig
:param number_trials: Number of trials adding to the existing study
:type number_trials: int
:param target_number_trials: Number of target trials for the existing study
:type target_number_trials: int
:param storage: storage database, e.g. 'sqlite' or 'mysql'
:type storage: str
:param sampler: optuna.samplers.NSGAIISampler() or optuna.samplers.NSGAIIISampler(). Note about the brackets () !!
Expand Down Expand Up @@ -382,15 +384,30 @@ def start_proceed_study(config: InductorOptimizationDTO, number_trials: int,
storage=storage,
directions=['minimize', 'minimize'],
load_if_exists=True, sampler=sampler)
if target_number_trials is not None:
# simulation for a given number of target trials
if len(study_in_storage.trials) < target_number_trials:
study_in_memory = optuna.create_study(directions=['minimize', 'minimize'], study_name=config.inductor_study_name, sampler=sampler)
print(f"Sampler is {study_in_memory.sampler.__class__.__name__}")
study_in_memory.add_trials(study_in_storage.trials)
number_trials = target_number_trials - len(study_in_memory.trials)
study_in_memory.optimize(func, n_trials=number_trials, show_progress_bar=True)
study_in_storage.add_trials(study_in_memory.trials[-number_trials:])
print(f"Finished {number_trials} trials.")
print(f"current time: {datetime.datetime.now()}")
else:
print(f"Study has already {len(study_in_storage.trials)} trials, and target is {target_number_trials} trials.")

study_in_memory = optuna.create_study(directions=['minimize', 'minimize'], study_name=config.inductor_study_name, sampler=sampler)
print(f"Sampler is {study_in_memory.sampler.__class__.__name__}")
study_in_memory.add_trials(study_in_storage.trials)
study_in_memory.optimize(func, n_trials=number_trials, show_progress_bar=True)

study_in_storage.add_trials(study_in_memory.trials[-number_trials:])
print(f"Finished {number_trials} trials.")
print(f"current time: {datetime.datetime.now()}")
else:
# normal simulation with number_trials
study_in_memory = optuna.create_study(directions=['minimize', 'minimize'], study_name=config.inductor_study_name, sampler=sampler)
print(f"Sampler is {study_in_memory.sampler.__class__.__name__}")
study_in_memory.add_trials(study_in_storage.trials)
study_in_memory.optimize(func, n_trials=number_trials, show_progress_bar=True)

study_in_storage.add_trials(study_in_memory.trials[-number_trials:])
print(f"Finished {number_trials} trials.")
print(f"current time: {datetime.datetime.now()}")
InductorOptimization.ReluctanceModel.save_config(config)

@staticmethod
Expand Down Expand Up @@ -843,6 +860,8 @@ def full_simulation(df_geometry: pd.DataFrame, current_waveform: List, inductor_
:type inductor_config_filepath: str
:param process_number: process number to run the simulation on
:type process_number: int
:param print_derivations: True to print derivation from FEM simulaton to reluctance model
:type print_derivations: bool
:return: volume, loss
:rtype: tuple
"""
Expand Down

0 comments on commit 3840e83

Please sign in to comment.