roudan
roudan

Reputation: 4210

How to get optimization result for each iteration in Genetic Algorithm?

I am using a Genetic Algorithm. The optimization plot is generated automatically. I'd like to customize the plot so I am wondering how to get the optimization result for each iteration.

enter image description here

algorithm_param={'max_num_iteration': None, 'population_size':100,   #None
'mutation_probability':0.1,'elit_ratio': 0.1,
'crossover_probability': 0.5,'parents_portion': 0.2,
'crossover_type':'uniform','max_iteration_without_improv':100}

model=ga(function=f,dimension=len(x_coef),variable_type='real',
         variable_boundaries=varbound, algorithm_parameters=algorithm_param)

model.run()
convergence=model.report
solution=model.output_dict

Upvotes: 1

Views: 623

Answers (1)

Amir Charkhi
Amir Charkhi

Reputation: 846

So the model.report gives you the Objective Function values and model.iterate prints the number of iterations.

I guess using these values you can customize the plot the way you want it.

Here is an example:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

dataframe = pd.DataFrame(model.report)
dataframe.columns = ["report"]
dataframe["iteration"] = list(range(model.iterate+1))
dataframe

enter image description here

sns.set_theme(style="darkgrid")

g = sns.relplot(x="iteration", y="report", kind="line", data=dataframe)

enter image description here

Upvotes: 1

Related Questions