Skip to content

Commit

Permalink
Include NSGAii in SPOTPY infrastructur, fix minor padds error
Browse files Browse the repository at this point in the history
  • Loading branch information
thouska committed Oct 9, 2020
1 parent 0964c36 commit 4ae08b6
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 176 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ __pycache__/

#other files
*.out

*.png
*.csv
site/*
Expand Down
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Some features you can use with the SPOTPY package are:
* Fitness Scaled Chaotic Artificial Bee Colony (`FSCABC`)
* Dynamically Dimensioned Search algorithm (`DDS`)
* Pareto Archived - Dynamicallly Dimensioned Search algorithm (`PA-DDS`)
* Fast and Elitist Multiobjective Genetic Algorithm (`NSGA-II`)

* Wide range of objective functions (also known as loss function, fitness function or energy function) to validate the sampled results. Available functions are

Expand Down Expand Up @@ -136,15 +137,23 @@ Some features you can use with the SPOTPY package are:
Install
=================

Installing SPOTPY is easy. Just use:
Classical Python options exist to install SPOTPY:

From PyPi:

pip install spotpy

Or, after downloading the [source code](https://pypi.python.org/pypi/spotpy "source code") and making sure python is in your OS path:
From Conda-Forge:

conda config --add channels conda-forge
conda config --set channel_priority strict
conda install spotpy

From [Source](https://pypi.python.org/pypi/spotpy):

python setup.py install


Support
=================

Expand Down
15 changes: 12 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ We want to make this task as easy as possible. Some features you can use with th
* Fitness Scaled Chaotic Artificial Bee Colony (`FSCABC`)
* Dynamically Dimensioned Search algorithm (`DDS`)
* Pareto Archived - Dynamicallly Dimensioned Search algorithm (`PA-DDS`)
* Fast and Elitist Multiobjective Genetic Algorithm (`NSGA-II`)

* Wide range of objective functions (also known as loss function, fitness function or energy function) to validate the sampled results. Available functions are

Expand Down Expand Up @@ -136,15 +137,23 @@ Documentation is available at `<https://spotpy.readthedocs.io/en/latest>`__
Install
-------

Installing SPOTPY is easy. Just use:
Classical Python options exist to install SPOTPY:

From PyPi:

pip install spotpy

Or, after downloading the source code and making sure python is in your path:
From Conda-Forge:

conda config --add channels conda-forge
conda config --set channel_priority strict
conda install spotpy

From [Source](https://pypi.python.org/pypi/spotpy):

python setup.py install


Papers citing SPOTPY
--------------------
See `Google Scholar <https://scholar.google.de/scholar?cites=17155001516727704728&as_sdt=2005&sciodt=0,5&hl=de>`__ for a continuously updated list.
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name = 'spotpy',
version = '1.5.10',
version = '1.5.14',
description = 'A Statistical Parameter Optimization Tool',
long_description=open(os.path.join(os.path.dirname(__file__),
"README.rst")).read(),
Expand All @@ -15,7 +15,7 @@
license = 'MIT',
packages=find_packages(exclude=["tests*", "docs*"]),
use_2to3 = True,
keywords = 'Monte Carlo, MCMC, MLE, SCE-UA, Simulated Annealing, DE-MCz, DREAM, ROPE, Artifical Bee Colony, DDS, PA-DDS, Uncertainty, Calibration, Model, Signatures',
keywords = 'Monte Carlo, MCMC, MLE, SCE-UA, Simulated Annealing, DE-MCz, DREAM, ROPE, Artifical Bee Colony, DDS, PA-DDS, NSGAii, Uncertainty, Calibration, Model, Signatures',
classifiers = [
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
Expand Down
3 changes: 2 additions & 1 deletion spotpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
sampling and an analyser class for the plotting of results by the sampling.
:dependencies: - Numpy >1.8 (http://www.numpy.org/)
- Scipy >1.5 (https://pypi.org/project/scipy/)
- Pandas >0.13 (optional) (http://pandas.pydata.org/)
- Matplotlib >1.4 (optional) (http://matplotlib.org/)
- CMF (optional) (http://fb09-pasig.umwelt.uni-giessen.de:8000/)
Expand All @@ -40,4 +41,4 @@
from . import describe # Contains some helper functions to describe samplers and set-ups
from .hydrology import signatures # Quantifies goodness of fit between simulation and evaluation data with hydrological signatures

__version__ = '1.5.10'
__version__ = '1.5.14'
4 changes: 2 additions & 2 deletions spotpy/algorithms/nsgaii.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def crowdDist2(self,x):



def sample(self, generations, n_obj, n_pop = None, skip_duplicates = False,
def sample(self, generations, n_obj, n_pop = None,
selection = TournamentSelection(pressure=2),
crossover = Crossover(crossProb=0.9),
mutation = PolynomialMutation(prob_mut=0.25,eta_mut=30)):
Expand All @@ -273,7 +273,7 @@ def sample(self, generations, n_obj, n_pop = None, skip_duplicates = False,
self.n_pop = n_pop
self.generations= generations
self.set_repetiton(self.generations*self.n_pop)
self.skip_duplicates = skip_duplicates
self.skip_duplicates = True # False does not work yet

Pt = np.vstack([self.parameter()['random'] for i in range(self.n_pop)])

Expand Down
10 changes: 6 additions & 4 deletions spotpy/algorithms/padds.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def get_next_x_curr(self):
else: # otherwise use the last generated solution
self.best_value.parameters, self.best_value.best_obj_val = (self.parameter_current, self.obj_func_current)

# This line is needed to get an array of data converted into a parameter object
# # This line is needed to get an array of data converted into a parameter object
self.best_value.fix_format()

yield rep, self.calculate_next_s_test(self.best_value.parameters, rep, self.generator_repetitions, self.r)
Expand Down Expand Up @@ -163,9 +163,9 @@ def calculate_initial_parameterset(self, repetitions, initial_objs, initial_para
for i in range(initial_params.shape[0]):
self.parameter_current = initial_params[i]
if len(initial_objs[i]) > 0:
self.obj_func_current = initial_objs[i]
self.obj_func_current = np.array(initial_objs[i])
else:
self.obj_func_current = self.getfitness(simulation=[], params=self.parameter_current)
self.obj_func_current = np.array(self.getfitness(simulation=[], params=self.parameter_current))

if i == 0: # Initial value
self.pareto_front = np.array([[self.obj_func_current, self.parameter_current]])
Expand Down Expand Up @@ -206,7 +206,8 @@ def sample(self, repetitions, trials=1, initial_objs=np.array([]), initial_param
self.generator_repetitions = repetions_left

for rep, x_curr, simulations in self.repeat(self.get_next_x_curr()):
self.obj_func_current = self.postprocessing(rep, x_curr, simulations)
obj_func_current = self.postprocessing(rep, x_curr, simulations)
self.obj_func_current = np.array(obj_func_current)
num_imp = np.sum(self.obj_func_current <= self.best_value.best_obj_val)
num_deg = np.sum(self.obj_func_current > self.best_value.best_obj_val)

Expand Down Expand Up @@ -335,6 +336,7 @@ def nd_check(nd_set_input, objective_values, parameter_set):
dominance_flag = 0

# These are simply reshaping problems if we want to loop over arrays but we have a single float given
objective_values = np.array(objective_values)
try:
like_struct_len = objective_values.shape[0]
except IndexError:
Expand Down
81 changes: 25 additions & 56 deletions spotpy/examples/tutorial_nsgaii.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#!/usr/bin/env python
# coding: utf-8
# -*- coding: utf-8 -*-
'''
Copyright 2015 by Tobias Houska
This file is part of Statistical Parameter Estimation Tool (SPOTPY).
:author: Tobias Houska
This class holds example code how to use the nsgaii algorithm
'''

from __future__ import division, print_function
from __future__ import absolute_import
Expand All @@ -24,7 +32,22 @@


def multi_obj_func(evaluation, simulation):
#used to overwrite objective function in hymod example
"""
This function is used to overwrite objective function in hymod example
Parameters
----------
evaluation : array
The observation data.
simulation : array
The model input
Returns
-------
list
Three different kinds of objective functions.
"""
like1 = abs(spotpy.objectivefunctions.pbias(evaluation, simulation))
like2 = spotpy.objectivefunctions.rmse(evaluation, simulation)
like3 = spotpy.objectivefunctions.rsquared(evaluation, simulation)*-1
Expand All @@ -40,61 +63,7 @@ def multi_obj_func(evaluation, simulation):
sp_setup=spot_setup(obj_func= multi_obj_func)
sampler = spotpy.algorithms.NSGAII(spot_setup=sp_setup, dbname='NSGA2', dbformat="csv")

sampler.sample(generations, n_obj= 3, n_pop = n_pop, skip_duplicates = skip_duplicates)
#sampler.sample(generations=5, paramsamp=40)


# # user config
#
# n_var = 5
#
#
# last = None
# first = None
#
# # output calibration
#
# df = pd.read_csv("NSGA2.csv")
#
# if last:
# df = df.iloc[-last:,:]
# elif first:
# df = df.iloc[:first,:]
# else:
# pass
#
#
#
# print(len(df))
# # plot objective functions
# fig = plt.figure()
# for i,name in enumerate(df.columns[:n_obj]):
# ax = fig.add_subplot(n_obj,1,i +1)
# df.loc[::5,name].plot(lw=0.5,figsize=(18,8),ax = ax,color="black")
# plt.title(name)
# plt.show()
#
# last = 100
# first = None
#
# x,y,z = df.iloc[-last:,0],df.iloc[-last:,1],df.iloc[-last:,2]
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# ax.scatter(x,y,z,marker="o")
# ax.set_xlabel("f0")
# ax.set_ylabel("f1")
# ax.set_zlabel("f2")
# plt.show()
#
# # plot parameters
# fig = plt.figure()
# for i,name in enumerate(df.columns[n_obj:8]):
# ax = fig.add_subplot(5,1,i +1)
# df.loc[:,name].plot(lw=0.5,figsize=(18,8),ax = ax,color="black")
# plt.title(name)
# plt.show()
#

sampler.sample(generations, n_obj= 3, n_pop = n_pop)


results = sampler.getdata()
Expand Down
2 changes: 1 addition & 1 deletion spotpy/examples/tutorial_padds.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ def objectivefunction(self, simulation, evaluation, params):
spot_setup = padds_spot_setup()

sampler = spotpy.algorithms.padds(spot_setup, dbname='padds_hymod', dbformat='csv')
res = sampler.sample(10000,trials=1)
res = sampler.sample(2000,trials=1)
Loading

0 comments on commit 4ae08b6

Please sign in to comment.