TERMINATION ERROR #649
codeninja2020
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi I am having an error when it comes to termination when implementing pymoo for optimisation please review my code NSGA2
`import csv
import numpy as np
from PIL.ImageChops import difference
from pymoo.core.problem import Problem
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.sampling import Sampling
from pymoo.operators.crossover.sbx import SBX
from pymoo.operators.mutation.pm import PM
from pymoo.operators.sampling.rnd import FloatRandomSampling
from pymoo.termination import get_termination
from pymoo.optimize import minimize
from pymoo.termination.default import DefaultTermination
from pymoo.core.termination import Termination # Correct import
from pymoo.termination.default import DefaultSingleObjectiveTermination
import runSeska
import runSeska as runSimulation
from runSeska import run_fem_simulation
import time
#TUTORIALS https://www.youtube.com/watch?v=six6afYc7RI&ab_channel=paretos
Placeholder for your experimental pressure-volume data
experimental_pv_data = runSimulation.experimental_data # Load or define your experimental data
start_time = time.time()
difference= None
Terminate based on the total cost tolerance, for example 1e-2
termination = DefaultSingleObjectiveTermination(
xtol=1e-2,
cvtol=1e-2,
ftol=1e2,
period=20,
n_max_gen=1000,
n_max_evals=100000
)
class SimulationOptimizationProblem(Problem):
def init(self):
super().init(n_var=8, # 8 parameters
n_obj=1, # Single objective: minimize difference
n_constr=0, # No constraints (you can add if needed)
xl=[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], # Lower bounds for parameters
xu=[100, 100, 100, 100, 100, 100, 100, 100]) # Upper bounds for your parameters
class CustomSampling(Sampling):
def init(self, initial_params):
self.initial_params = initial_params
self.vtype = np.double # Define the value type for variables (double for continuous)
self.repair = None # Set repair to None if no repair mechanism is needed
Define algorithm, termination, and run optimization
algorithm = NSGA2(
pop_size=40,
n_offsprings=10,
sampling=CustomSampling(runSeska.initial_params),
crossover=SBX(prob=0.9, eta=15),
mutation=PM(eta=20),
eliminate_duplicates=True
)
#termination = get_termination("n_gen", 40) # Or another suitable criterion
#termination = get_termination("n_eval", 1000) # Use evaluation-based termination
Terminate based on the total cost tolerance, for example 1e-2
problem = SimulationOptimizationProblem()
def callback(algorithm):
"""
This callback function will be called after each generation.
It writes the current generation number, parameter sets (X),
and objective values (F) to a CSV file.
"""
gen = algorithm.n_gen
X = algorithm.pop.get("X")
F = algorithm.pop.get("F")
res = minimize(problem,
algorithm,
termination=termination,
seed=1,
save_history=True,
verbose=True,
callback=callback)
Access the best parameter set and its objective value
best_params = res.X[0]
best_difference = res.F[0]
print("Best parameters:", best_params)
print("Best difference:", best_difference)
end_time = time.time()
Calculate the running time
running_time = end_time - start_time
Convert running time to minutes (optional)
running_time_minutes = running_time / 60
print(f"Running time: {running_time} seconds")
print(f"Running time: {running_time_minutes} minutes")
Specify the CSV file path
csv_file = "running_time.csv"
Write the total running time to a CSV file
with open(csv_file, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Total Running Time (seconds)"]) # Header
writer.writerow([running_time]) # Running time data
print(f"Total running time ({running_time} seconds) written to {csv_file}")
`
Beta Was this translation helpful? Give feedback.
All reactions