I want to replace the maximum with the minimum value in the list while saving it.

Asked 2 years ago, Updated 2 years ago, 21 views

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.

Add

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?

python

2022-09-29 21:34

2 Answers

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


2022-09-29 21:34

 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.


2022-09-29 21:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.