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

LSDOlab / modopt / 12820278359

17 Jan 2025 12:39AM UTC coverage: 74.252%. Remained the same
12820278359

push

github

anugrahjo
Add API reference for merit functions.

4790 of 6451 relevant lines covered (74.25%)

0.74 hits per line

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

90.0
/modopt/core/merit_function.py
1
import numpy as np
1✔
2
from modopt.utils.options_dictionary import OptionsDictionary
1✔
3
from types import MethodType, FunctionType
1✔
4

5

6
# Note: Merit functions are only needed for constrained optimization
7
class MeritFunction(object):
1✔
8
    def __init__(self, **kwargs):
1✔
9
        self.options = OptionsDictionary()
1✔
10

11
        self.options.declare('f', types=(MethodType, FunctionType))
1✔
12
        self.options.declare('g', types=(MethodType, FunctionType))
1✔
13
        self.options.declare('c', types=(MethodType, FunctionType))
1✔
14
        self.options.declare('j', types=(MethodType, FunctionType))
1✔
15

16
        self.options.declare('nx',
1✔
17
                             types=(int, np.int32, np.int64, float, np.float64))
18
        self.options.declare('nc',
1✔
19
                             types=(int, np.int32, np.int64, float, np.float64))
20
        self.options.declare('nc_e', default=0,
1✔
21
                             types=(int, np.int32, np.int64, float, np.float64))
22

23
        self.cache = {}
1✔
24
        self.eval_count = {'f': 0, 'c': 0, 'g': 0, 'j': 0}
1✔
25

26
        self.initialize()
1✔
27

28
        self.options.update(kwargs)
1✔
29

30
        self.setup()
1✔
31

32
    def clear_cache(self):
1✔
33
        """
34
        Clear the cache of function evaluations.
35
        """
36
        self.cache = {}
×
37

38
    def initialize(self):
1✔
39
        pass
1✔
40

41
    def setup(self):
1✔
42
        pass
1✔
43

44
    def update_functions_in_cache(self, fnames, x):
1✔
45
        """
46
        Update the function values in the cache for the specified functions 
47
        in `fnames` with their values at the given point `x`.
48
        If the cache contains no values for a function in `fnames` 
49
        or the cached values correspond to a point other than `x`, 
50
        the function is evaluated at `x` and the cache is updated accordingly.
51

52
        Parameters
53
        ----------
54
        fnames : str or list of str
55
            Function names whose values need to be updated in the cache.
56
            `fnames` must be one or a subset of ['f', 'g', 'c', 'j'].
57
        x : np.ndarray
58
            Point at which to update the functions in `fnames`.
59
        """
60

61
        if not isinstance(fnames, (list, str)):
1✔
62
            raise ValueError("'fnames' must be a string or list of strings")
×
63
        
64
        fnames = [fnames] if isinstance(fnames, str) else fnames
1✔
65
        possible_fnames = ['f', 'g', 'c', 'j']
1✔
66
        if any([fname not in possible_fnames for fname in fnames]):
1✔
67
            raise ValueError("Invalid function name. Must be one of: ", possible_fnames)
×
68
        
69
        if not isinstance(x, np.ndarray):
1✔
70
            raise ValueError("'x' must be a numpy array")
×
71
        
72
        for fname in fnames:
1✔
73
            if fname not in self.cache:
1✔
74
                self.cache[fname] = (x, self.options[fname](x))
1✔
75
                self.eval_count[fname] += 1
1✔
76
            elif not np.array_equal(x, self.cache[fname][0]):
1✔
77
                self.cache[fname] = (x, self.options[fname](x))
1✔
78
                self.eval_count[fname] += 1
1✔
79
            # else:
80
            #     print("Cache hit for function: ", fname)
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