Repository: tBuLi/symfit

Issue:
Setting a negative fixed parameter
I am needing to set a negative fixed parameter for a model. This may be an issue or user error. I started the following stack exchange question. 

https://stackoverflow.com/questions/44562190/setting-a-negative-fixed-parameter-in-the-symfit-python-package

Sorry for leaving a comment on Git, but both Stack Exchange and Git removed the ability to send comments so I figured this would be the best way to alert those who work on the project as Stack Exchange does not have a Symfit tag. 






Discussion:

Patch:
diff --git a/symfit/core/argument.py b/symfit/core/argument.py
index 8ef7024..faa0d8e 100644
--- a/symfit/core/argument.py
+++ b/symfit/core/argument.py
@@ -88,8 +88,13 @@ class Parameter(Argument):
         self.value = value
         self.fixed = fixed
         if not self.fixed:
-            self.min = min
-            self.max = max
+            if min is not None and max is not None and min > max:
+                print(min, max)
+                raise ValueError(
+                    'The value of `min` should be less than or equal to the value of `max`.')
+            else:
+                self.min = min
+                self.max = max
 
 
 class Variable(Argument):
diff --git a/symfit/core/fit.py b/symfit/core/fit.py
index ddc73ca..c0f59c0 100644
--- a/symfit/core/fit.py
+++ b/symfit/core/fit.py
@@ -405,7 +405,16 @@ class BaseModel(Mapping):
         """
         :return: List of tuples of all bounds on parameters.
         """
-        return [(np.nextafter(p.value, 0), p.value) if p.fixed else (p.min, p.max) for p in self.params]
+        bounds = []
+        for p in self.params:
+            if p.fixed:
+                if p.value >= 0.0:
+                    bounds.append([np.nextafter(p.value, 0), p.value])
+                else:
+                    bounds.append([p.value, np.nextafter(p.value, 0)])
+            else:
+                bounds.append([p.min, p.max])
+        return bounds
 
     @property
     def shared_parameters(self):
@@ -1365,11 +1374,10 @@ class Minimize(BaseFit):
         else:
             return np.array(ans)
 
-    def execute(self, method='SLSQP', *args, **kwargs):
+    def execute(self, *args, **kwargs):
         ans = minimize(
             self.error_func,
             self.initial_guesses,
-            method=method,
             args=(self.independent_data, self.dependent_data, self.sigma_data,),
             bounds=self.model.bounds,
             constraints=self.scipy_constraints,


Test patch:
diff --git a/tests/test_minimize.py b/tests/test_minimize.py
index 9961702..e64884c 100644
--- a/tests/test_minimize.py
+++ b/tests/test_minimize.py
@@ -57,7 +57,7 @@ class TestMinimize(unittest.TestCase):
         res = minimize(func, [-1.0,1.0], args=(-1.0,), jac=func_deriv,
                method='SLSQP', options={'disp': False})
         fit = Maximize(model)
-        fit_result = fit.execute()
+        fit_result = fit.execute(method='SLSQP')
 
         self.assertAlmostEqual(fit_result.value(x), res.x[0])
         self.assertAlmostEqual(fit_result.value(y), res.x[1])
@@ -69,7 +69,7 @@ class TestMinimize(unittest.TestCase):
         fit = Maximize(model, constraints=constraints)
         self.assertEqual(fit.constraints[0].constraint_type, Ge)
         self.assertEqual(fit.constraints[1].constraint_type, Eq)
-        fit_result = fit.execute()
+        fit_result = fit.execute(method='SLSQP')
         self.assertAlmostEqual(fit_result.value(x), res.x[0])
         self.assertAlmostEqual(fit_result.value(y), res.x[1])
 
diff --git a/tests/test_model.py b/tests/test_model.py
index df42195..526d432 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -8,7 +8,7 @@ import sympy
 import numpy as np
 from scipy.optimize import curve_fit
 
-from symfit import Variable, Parameter, Fit, FitResults, LinearLeastSquares, parameters, variables, NumericalLeastSquares, NonLinearLeastSquares, Model, TaylorModel
+from symfit import Variable, Parameter, Fit, FitResults, LinearLeastSquares, parameters, variables, NumericalLeastSquares, NonLinearLeastSquares, Model, TaylorModel, exp
 from symfit.core.support import seperate_symbols, sympy_to_py
 from symfit.distributions import Gaussian
 
@@ -45,6 +45,30 @@ class TestModel(unittest.TestCase):
 
         self.assertEqual(model.dependent_vars, list(model.keys()))
 
+    def test_bounds(self):
+        """
+        The bounds of an object should always be such that lower < upper.
+        :return:
+        """
+        a = Parameter(value= - 2.482092e-01, fixed=True)
+        # a = Parameter()
+        try:
+            b = Parameter(value=5.0, min=6.0, max=4.0)
+        except ValueError:
+            b = Parameter(value=5.0, min=4.0, max=6.0)
+        c = Parameter(value=2.219756e+02, fixed=True)
+        x = Variable()
+
+        # build the model
+        model = Model(a + b * (1 - exp(-c / x)))
+        print(model.bounds)
+        for bounds in model.bounds:
+            if None in bounds:
+                pass
+            else:
+                # Both are set
+                min, max = bounds
+                self.assertGreaterEqual(max, min)
 
 if __name__ == '__main__':
     unittest.main()