• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
No new info detected.

thouska / spotpy / 7709518362

30 Jan 2024 10:11AM UTC coverage: 77.332% (-0.2%) from 77.518%
7709518362

push

github

thouska
Merge branch 'master' of https://github.com/thouska/spotpy

4162 of 5382 relevant lines covered (77.33%)

3.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/spotpy/algorithms/lhs.py
1
# -*- coding: utf-8 -*-
2
"""
6✔
3
Copyright (c) 2018 by Tobias Houska
4
This file is part of Statistical Parameter Optimization Tool for Python (SPOTPY).
5
:author: Tobias Houska
6
"""
7

8
import random
6✔
9

10
import numpy as np
6✔
11

12
from . import _algorithm
6✔
13

14

15
class lhs(_algorithm):
6✔
16
    """
17
    The Latin Hypercube algorithm generates random parameters from their respective
18
    distribution functions.
19
    """
20

21
    def __init__(self, *args, **kwargs):
6✔
22
        """
23
        Input
24
        ----------
25
        spot_setup: class
26
            model: function
27
                Should be callable with a parameter combination of the parameter-function
28
                and return an list of simulation results (as long as evaluation list)
29
            parameter: function
30
                When called, it should return a random parameter combination. Which can
31
                be e.g. uniform or Gaussian
32
            objectivefunction: function
33
                Should return the objectivefunction for a given list of a model simulation and
34
                observation.
35
            evaluation: function
36
                Should return the true values as return by the model.
37

38
        dbname: str
39
            * Name of the database where parameter, objectivefunction value and simulation results will be saved.
40

41
        dbformat: str
42
            * ram: fast suited for short sampling time. no file will be created and results are saved in an array.
43
            * csv: A csv file will be created, which you can import afterwards.
44

45
        parallel: str
46
            * seq: Sequentiel sampling (default): Normal iterations on one core of your cpu.
47
            * mpi: Message Passing Interface: Parallel computing on cluster pcs (recommended for unix os).
48

49
        save_sim: boolean
50
            * True:  Simulation results will be saved
51
            * False: Simulation results will not be saved
52
        """
53
        kwargs["algorithm_name"] = "Latin Hypercube Sampling (LHS)"
4✔
54
        super(lhs, self).__init__(*args, **kwargs)
4✔
55

56
    def sample(self, repetitions):
6✔
57
        """
58
        Parameters
59
        ----------
60
        repetitions: int
61
            maximum number of function evaluations allowed during optimization
62
        """
63
        self.set_repetiton(repetitions)
4✔
64
        print(
4✔
65
            "Starting the LHS algotrithm with " + str(repetitions) + " repetitions..."
66
        )
67
        print("Creating LatinHyperCube Matrix")
4✔
68
        # Get the names of the parameters to analyse
69
        names = self.parameter()["name"]
4✔
70
        # Define the jump size between the parameter
71
        segment = 1 / float(repetitions)
4✔
72
        # Get the minimum and maximum value for each parameter from the
73
        # distribution
74
        parmin, parmax = self.parameter()["minbound"], self.parameter()["maxbound"]
4✔
75

76
        # Create an matrx to store the parameter sets
77
        matrix = np.empty((repetitions, len(parmin)))
4✔
78
        # Create the LatinHypercube matrix as given in McKay et al. (1979)
79
        for i in range(int(repetitions)):
4✔
80
            segmentMin = i * segment
4✔
81
            pointInSegment = segmentMin + (random.random() * segment)
4✔
82
            parset = pointInSegment * (parmax - parmin) + parmin
4✔
83
            matrix[i] = parset
4✔
84
        for i in range(len(names)):
4✔
85
            random.shuffle(matrix[:, i])
4✔
86

87
        # A generator that produces the parameters
88
        param_generator = ((rep, matrix[rep]) for rep in range(int(repetitions)))
4✔
89
        for rep, randompar, simulations in self.repeat(param_generator):
4✔
90
            # A function that calculates the fitness of the run and the manages the database
91
            self.postprocessing(rep, randompar, simulations)
4✔
92
        self.final_call()
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc