thouska / spotpy

A Statistical Parameter Optimization Tool
https://spotpy.readthedocs.io/en/latest/
MIT License
247 stars 149 forks source link

The Ackley function Bug #306

Open mym92 opened 1 year ago

mym92 commented 1 year ago

Hi, @thouska ,thank you and all contributors for the spotpy project. I have learnt a lot from your project. I found a minor bug in the the Ackley function example: your code in _spot_setupackley.py:

def simulation(self, vector):
       firstSum = 0.0
       secondSum = 0.0
       for c in range(len(vector)):
            firstSum += c**2.0
            secondSum += np.cos(2.0 * np.pi * vector[c])
            n = float(len(vector))
       return [
            -20.0 * np.exp(-0.2 * np.sqrt(firstSum / n))
            - np.exp(secondSum / n)
            + 20
            + np.e
        ]
  1. The calculation of n's value does not require placement within the FOR loop body.
  2. On line five, direct use of the value of c in calculations is improper I think the right cods is:
    def simulation(self, vector):
    firstSum = 0.0
    secondSum = 0.0
    for c in range(len(vector)):
        firstSum += vector[c]**2.0
        secondSum += np.cos(2.0 * np.pi * vector[c])
    n = float(len(vector))
    return [
            -20.0 * np.exp(-0.2 * np.sqrt(firstSum / n))
            - np.exp(secondSum / n)
            + 20
            + np.e
        ]