• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

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/mc.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
from . import _algorithm
6✔
9

10

11
class mc(_algorithm):
6✔
12
    """
13
    The Monte Carlo algorithm generates random parameters from their respective
14
    distribution functions.
15
    """
16

17
    _unaccepted_parameter_types = ()
6✔
18

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

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

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

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

47
        save_sim: boolean
48
            * True:  Simulation results will be saved
49
            * False: Simulation results will not be saved
50
        """
51
        kwargs["algorithm_name"] = "Monte Carlo (MC) sampler"
4✔
52
        super(mc, self).__init__(*args, **kwargs)
4✔
53

54
    def sample(self, repetitions):
6✔
55
        """
56
        Samples from the MonteCarlo algorithm.
57

58
        Input
59
        ----------
60
        repetitions: int
61
            Maximum number of runs.
62
        """
63
        self.set_repetiton(repetitions)
4✔
64
        print("Starting the MC algorithm with {} repetitions...".format(repetitions))
4✔
65
        # A generator that produces parametersets if called
66
        param_generator = (
4✔
67
            (rep, self.parameter()["random"]) for rep in range(int(repetitions))
68
        )
69
        for rep, randompar, simulations in self.repeat(param_generator):
4✔
70
            # A function that calculates the fitness of the run and the manages the database
71
            self.postprocessing(rep, randompar, simulations)
4✔
72
        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