Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

fitbenchmarking / fitbenchmarking / 2330948078

16 May 2022 - 9:26 coverage decreased (-0.2%) to 86.542%
2330948078

Pull #1036

github

GitHub
Merge f94846412 into ed08b4d37
Pull Request #1036: Enable max runtime in curve fitting

0 of 46 new or added lines in 6 files covered. (0.0%)

5 existing lines in 4 files now uncovered.

3421 of 3953 relevant lines covered (86.54%)

0.87 hits per line

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

0.0
/fitbenchmarking/controllers/matlab_opt_controller.py
1
"""
2
Implements a controller for MATLAB Optimization Toolbox
3
"""
4
import matlab
!
5
import numpy as np
!
6

7
from fitbenchmarking.controllers.base_controller import Controller
!
8
from fitbenchmarking.controllers.matlab_mixin import MatlabMixin
!
9

10

11
class MatlabOptController(MatlabMixin, Controller):
!
12
    """
13
    Controller for MATLAB Optimization Toolbox, implementing lsqcurvefit
14
    """
15

16
    algorithm_check = {
!
17
        'all': ['levenberg-marquardt', 'trust-region-reflective'],
18
        'ls': ['levenberg-marquardt', 'trust-region-reflective'],
19
        'deriv_free': [],
20
        'general': [],
21
        'simplex': [],
22
        'trust_region': ['levenberg-marquardt', 'trust-region-reflective'],
23
        'levenberg-marquardt': ['levenberg-marquardt'],
24
        'gauss_newton': [],
25
        'bfgs': [],
26
        'conjugate_gradient': [],
27
        'steepest_descent': [],
28
        'global_optimization': []}
29

30
    jacobian_enabled_solvers = ['levenberg-marquardt',
!
31
                                'trust-region-reflective']
32

33
    controller_name = 'matlab_opt'
!
34

35
    incompatible_problems = ['mantid']
!
36

37
    def __init__(self, cost_func):
!
38
        """
39
        Initialises variables used for temporary storage.
40
        :param cost_func: Cost function object selected from options.
41
        :type cost_func: subclass of
42
                :class:`~fitbenchmarking.cost_func.base_cost_func.CostFunc`
43
        """
44
        super().__init__(cost_func)
!
45
        self.support_for_bounds = True
!
46
        self.param_ranges = None
!
47
        self.x_data_mat = None
!
48
        self.y_data_mat = None
!
49
        self._status = None
!
50
        self.result = None
!
51

52
    def setup(self):
!
53
        """
54
        Setup for Matlab Optimization Toolbox fitting
55
        """
56

57
        # Convert initial params into matlab array
58
        self.y_data_mat = matlab.double(np.zeros(self.data_y.shape).tolist())
!
59
        self.initial_params_mat = matlab.double([self.initial_params])
!
60
        self.x_data_mat = matlab.double(self.data_x.tolist())
!
61

62
        # set matlab workspace variable for selected minimizer
63
        self.eng.workspace['minimizer'] = self.minimizer
!
64

65
        # set bounds if they have been set in problem definition file
66
        if self.value_ranges is not None:
!
67
            lb, ub = zip(*self.value_ranges)
!
68
            self.param_ranges = (matlab.double(lb), matlab.double(ub))
!
69
        else:
70
            # if no bounds are set, then pass empty arrays to
71
            # lsqcurvefit function
72
            self.param_ranges = (matlab.double([]), matlab.double([]))
!
73

74
        # serialize cost_func.eval_r and jacobian.eval (if not
75
        # using default jacobian) and open within matlab engine
76
        # so matlab fitting function can be called
NEW
77
        self.eng.workspace['eval_f'] = self.py_to_mat('eval_r')
!
78
        self.eng.evalc('f_wrapper = @(p, x)double(eval_f(p));')
!
79

UNCOV
80
        self.eng.workspace['init'] = self.initial_params_mat
!
81
        self.eng.workspace['x'] = self.x_data_mat
!
82

83
        # if default jacobian is not selected then pass _jeval
84
        # function to matlab
85
        if not self.cost_func.jacobian.use_default_jac:
!
NEW
86
            self.eng.workspace['eval_j'] = self.py_to_mat('jac_res')
!
UNCOV
87
            self.eng.evalc('j_wrapper = @(p, x)double(eval_j(p));')
!
88

89
            self.eng.workspace['eval_func'] = [self.eng.workspace['f_wrapper'],
!
90
                                               self.eng.workspace['j_wrapper']]
91
            self.eng.evalc('options = optimoptions("lsqcurvefit", '
!
92
                           '"Algorithm", minimizer, '
93
                           '"SpecifyObjectiveGradient", true);')
94
        else:
95
            self.eng.workspace['eval_func'] = self.eng.workspace['f_wrapper']
!
96
            self.eng.evalc('options = optimoptions("lsqcurvefit", '
!
97
                           '"Algorithm", minimizer);')
98

99
    def fit(self):
!
100
        """
101
        Run problem with Matlab Optimization Toolbox
102
        """
103
        self.result, _, _, exitflag, _ = self.eng.lsqcurvefit(
!
104
            self.eng.workspace['eval_func'], self.initial_params_mat,
105
            self.x_data_mat, self.y_data_mat, self.param_ranges[0],
106
            self.param_ranges[1], self.eng.workspace['options'], nargout=5)
107
        self._status = int(exitflag)
!
108

109
    def cleanup(self):
!
110
        """
111
        Convert the result to a numpy array and populate the variables results
112
        will be read from.
113
        """
114

115
        if self._status == 1:
!
116
            self.flag = 0
!
117
        elif self._status == 0:
!
118
            self.flag = 1
!
119
        else:
120
            self.flag = 2
!
121

122
        self.final_params = self.result[0]
!
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc