Skip to content

Commit

Permalink
Update to current spotpy code stlye
Browse files Browse the repository at this point in the history
  • Loading branch information
thouska committed May 31, 2021
1 parent 80435f9 commit a2f6637
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions spotpy/examples/tutorial_own_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@


class spot_setup(object):
slow = 1000
a = spotpy.parameter.Uniform(low=0, high=1)
b = spotpy.parameter.Uniform(low=0, high=1)

def __init__(self):
self.params = [spotpy.parameter.List('x', [1, 2, 3, 4, 6, 7, 8, 9, 0]), #Give possible x values as a List
spotpy.parameter.List('y', [0, 1, 2, 5, 7, 8, 9, 0, 1])] #Give possible y values as a List


self.db_headers = ["obj_functions", "parameters", "simulations"]

self.database = open('MyOwnDatabase.txt', 'w')
self.database.write("\t".join(self.db_headers) + "\n")

def parameters(self):
return spotpy.parameter.generate(self.params)

def simulation(self, vector):
x = np.array(vector)
for i in range(self.slow):
_ = np.sin(i)
simulations = [sum(100.0*(x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0)]
return simulations

Expand All @@ -45,14 +40,15 @@ def objectivefunction(self, simulation, evaluation):
return objectivefunction

def save(self, objectivefunctions, parameter, simulations, *args, **kwargs):
param_str = "|".join((str(p) for p in parameter))
sim_str = "|".join((str(s) for s in simulations))
param_str = "\t".join((str(p) for p in parameter))
sim_str = "\t".join((str(s) for s in simulations))
line = "\t".join([str(objectivefunctions), param_str, sim_str]) + '\n'
self.database.write(line)

spot_setup = spot_setup()

# Leave out dbformat and dbname and spotpy will return results in spot_setup.save function
sampler = spotpy.algorithms.mc(spot_setup)
sampler.sample(9) # Choose equal or less repetitions as you have parameters in your List
spot_setup.database.close() # Close the created txt file
if __name__ == "__main__":
spot_setup = spot_setup()

# set dbformat to custom and spotpy will return results in spot_setup.save function
sampler = spotpy.algorithms.mc(spot_setup, dbformat='custom')
sampler.sample(100) # Choose equal or less repetitions as you have parameters in your List
spot_setup.database.close() # Close the created txt file

0 comments on commit a2f6637

Please sign in to comment.