I'd like to use Python to save the elite genetic algorithm, but how can I take the minimum value out of fitness
and replace it with the stored max_value
?
In Python's list, fitness
calculates adaptability, but I want to save the maximum adaptability separately and replace the maximum adaptation if the next generation calculates a smaller maximum.
np.min(fitness).append(max_value)
I can't do this.
If you write the code yourself again, you will get an error.
if m_value<np.max(fitness): m_value=np.max(fitness)
if m_value>np.max(fitness): {fitness.remove(np.min(fitness))
fitness.append(max_value)
}
The following error occurs:
fitness.append(max_value)
^ SyntaxError: invalid syntax
For fitness, fitness=np.array(fit).astype(np.float)
.
fit=[]
.Is the variable specification bad?
If Questioner's answer allows fitness.remove
, it seems that fitness
is an array of Pythons, not a numPy Array.However, the question says NumPy Array, so I will write the answer for NumPy Array.
It's a little useless to delete and insert and repeat , so I'll replace it instead.Also, it is useless to calculate the max
many times, so I will do it once.
current_max=np.max(fitness)
if m_value<current_max
m_value = current_max
else:
fitness [np.argmin(fitness)] = current_max
if m_value<np.max(fitness):
m_value = np.max (fitness)
ifm_value>np.max(fitness):
fitness.remove(np.min(fitness))
fitness.insert(0,m_value)
I think it would be fine if{} was not required.
© 2024 OneMinuteCode. All rights reserved.