internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: Pyomo/pyomo

Issue:
pyomo.dae error, ncp=1, second-order, numpy
It popped up while trying to generate the second-order lagrange-radau discretization equations using a single collocation point:

```bash
ERROR: Constructing component 'd2Tdxdx_disc_eq' from data=None failed:
   AttributeError: 'numpy.ndarray' object has no attribute
   'is_expression_type'
```

The problem appears to be that when `adotdot` is computed with numpy, the `pder` argument of `numpy.polyval(pder, cp[j])` in `comp_adot(cp, order=2)` is an empty list. The resulting return value is an empty numpy array, which prints as `array(0)`. A simple fix seems to be wrapping that function in `float`, e.g.,
```python
arow.append(float(numpy.polyval(pder, cp[j])))
```
which converts the `array(0)` object into 0.0, giving the desired behavior.


Discussion:

Resolving patch:
diff --git a/pyomo/dae/plugins/colloc.py b/pyomo/dae/plugins/colloc.py
--- a/pyomo/dae/plugins/colloc.py
+++ b/pyomo/dae/plugins/colloc.py
@@ -213,7 +213,7 @@ def calc_adot(cp, order=1):
         pder = numpy.polyder(p, order)
         arow = []
         for j in range(len(cp)):
-            arow.append(numpy.polyval(pder, cp[j]))
+            arow.append(float(numpy.polyval(pder, cp[j])))
         a.append(arow)
     return a
 


Test patch:
diff --git a/pyomo/dae/tests/test_colloc.py b/pyomo/dae/tests/test_colloc.py
--- a/pyomo/dae/tests/test_colloc.py
+++ b/pyomo/dae/tests/test_colloc.py
@@ -142,6 +142,19 @@ def test_disc_second_order_radau(self):
         repn_gen = repn_to_rounded_dict(repn, 5)
         self.assertEqual(repn_baseline, repn_gen)
 
+    # test second order derivative with single collocation point
+    def test_disc_second_order_1cp(self):
+        m = ConcreteModel()
+        m.t = ContinuousSet(bounds=(0,1))
+        m.t2 = ContinuousSet(bounds=(0,10))
+        m.v = Var(m.t, m.t2)
+        m.dv = DerivativeVar(m.v, wrt=(m.t, m.t2))
+        TransformationFactory('dae.collocation').apply_to(m, nfe=2, ncp=1)
+
+        self.assertTrue(hasattr(m, 'dv_disc_eq'))
+        self.assertTrue(len(m.dv_disc_eq) == 4)
+        self.assertTrue(len(m.v) == 9)                        
+
     # test collocation discretization with legendre points 
     # on var indexed by single ContinuousSet
     def test_disc_single_index_legendre(self):
Repository: Pyomo/pyomo

Issue:
Mosek license absence causing Jenkins failures
#971 introduced a new solver plugin, but `available()` does not check for a valid license, so the Mosek tests on Jenkins currently are failing.

As discussed during the dev call, the short term solution would be to turn off the Mosek modules on Jenkins until a testing license can be installed.


Discussion:

Resolving patch:
diff --git a/pyomo/opt/base/solvers.py b/pyomo/opt/base/solvers.py
--- a/pyomo/opt/base/solvers.py
+++ b/pyomo/opt/base/solvers.py
@@ -192,6 +192,7 @@ def __call__(self, _name=None, **kwds):
 def check_available_solvers(*args):
     from pyomo.solvers.plugins.solvers.GUROBI import GUROBISHELL
     from pyomo.solvers.plugins.solvers.BARON import BARONSHELL
+    from pyomo.solvers.plugins.solvers.mosek_direct import MosekDirect
 
     logging.disable(logging.WARNING)
 
@@ -211,6 +212,9 @@ def check_available_solvers(*args):
         elif (arg[0] == "baron") and \
            (not BARONSHELL.license_is_valid()):
             available = False
+        elif (arg[0] == "mosek") and \
+           (not MosekDirect.license_is_valid()):
+            available = False
         else:
             available = \
                 (opt.available(exception_flag=False)) and \
diff --git a/pyomo/solvers/plugins/solvers/mosek_direct.py b/pyomo/solvers/plugins/solvers/mosek_direct.py
--- a/pyomo/solvers/plugins/solvers/mosek_direct.py
+++ b/pyomo/solvers/plugins/solvers/mosek_direct.py
@@ -95,6 +95,22 @@ def _init(self):
         self._capabilities.sos1 = False
         self._capabilities.sos2 = False
 
+    @staticmethod
+    def license_is_valid():
+        """
+        Runs a check for a valid Mosek license. Returns False
+        if Mosek fails to run on a trivial test case.
+        """
+        try:
+            import mosek
+        except ImportError:
+            return False
+        try:
+            mosek.Env().Task(0,0).optimize()
+        except mosek.Error:
+            return False
+        return True
+
     def _apply_solver(self):
         if not self._save_results:
             for block in self._pyomo_model.block_data_objects(descend_into=True,


Test patch:
diff --git a/pyomo/solvers/tests/solvers.py b/pyomo/solvers/tests/solvers.py
--- a/pyomo/solvers/tests/solvers.py
+++ b/pyomo/solvers/tests/solvers.py
@@ -21,7 +21,7 @@
 import pyomo.environ
 from pyomo.solvers.plugins.solvers.GUROBI import GUROBISHELL
 from pyomo.solvers.plugins.solvers.BARON import BARONSHELL
-
+from pyomo.solvers.plugins.solvers.mosek_direct import MosekDirect
 
 # ----------------------------------------------------------------
 
@@ -52,6 +52,9 @@ def initialize(**kwds):
     elif (obj.name == "baron") and \
        (not BARONSHELL.license_is_valid()):
         obj.available = False
+    elif (obj.name == "mosek") and \
+       (not MosekDirect.license_is_valid()):
+        obj.available = False
     else:
         obj.available = \
             (opt.available(exception_flag=False)) and \
Repository: Pyomo/pyomo

Issue:
Cython support is broken.
Running `python setup.py install --with-cython` yield an error about `visitor_expr.pyx` not existing.

It turns out that the list of files-to-Cythonize in `setup.py` is not up-to-date.


Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -90,7 +90,7 @@ def get_version():
             "pyomo/core/expr/numvalue.pyx",
             "pyomo/core/expr/numeric_expr.pyx",
             "pyomo/core/expr/logical_expr.pyx",
-            "pyomo/core/expr/visitor_expr.pyx",
+            #"pyomo/core/expr/visitor.pyx",
             "pyomo/core/util.pyx",
             "pyomo/repn/standard_repn.pyx",
             "pyomo/repn/plugins/cpxlp.pyx",
@@ -100,7 +100,8 @@ def get_version():
         ]
         for f in files:
             shutil.copyfile(f[:-1], f)
-        ext_modules = cythonize(files)
+        ext_modules = cythonize(files, compiler_directives={
+            "language_level": 3 if sys.version_info >= (3, ) else 2})
     except:
         if using_cython == CYTHON_REQUIRED:
             print("""


Test patch:
Repository: Pyomo/pyomo

Issue:
Error whil using mindtpy
I was trying to start the MINLP example presented here:
https://pyomo.readthedocs.io/en/stable/contributed_packages/mindtpy.html

When i run the code:

```
from pyomo.environ import *

# Create a simple model
model = ConcreteModel()

model.x = Var(bounds=(1.0, 10.0), initialize=5.0)
model.y = Var(within=Binary)

model.c1 = Constraint(expr=(model.x-3.0)**2 <= 50.0*(1-model.y))
model.c2 = Constraint(expr=model.x*log(model.x)+5.0 <= 50.0*(model.y))

model.objective = Objective(expr=abs(model.x*model.x), sense=minimize)

# Solve the model using MindtPy
SolverFactory('mindtpy').solve(model, mip_solver='glpk', nlp_solver='ipopt')
model.objective.display()
model.display()
model.pprint()
```

I get the following error:

pyomo.common.errors.DeveloperError: Internal Pyomo implementation error:
        "sympy expression type 're' not found in the operator map"
        Please report this to the Pyomo Developers.

I have installed pyomo 5.6.6, pyomo.extras 3.3, and pyomo.solvers 1.0 using Anaconda on Windows 10.



Discussion:

Resolving patch:
diff --git a/pyomo/core/expr/sympy_tools.py b/pyomo/core/expr/sympy_tools.py
--- a/pyomo/core/expr/sympy_tools.py
+++ b/pyomo/core/expr/sympy_tools.py
@@ -62,6 +62,7 @@ def _nondifferentiable(*x):
         sympy.ceiling: lambda x: current.ceil(x),
         sympy.floor: lambda x: current.floor(x),
         sympy.sqrt: lambda x: current.sqrt(x),
+        sympy.Abs: lambda x: abs(x),
         sympy.Derivative: _nondifferentiable,
         sympy.Tuple: lambda *x: x,
     }
@@ -108,7 +109,10 @@ def getPyomoSymbol(self, sympy_object, default=None):
     def getSympySymbol(self, pyomo_object):
         if pyomo_object in self.pyomo2sympy:
             return self.pyomo2sympy[pyomo_object]
-        sympy_obj = sympy.Symbol("x%d" % self.i)
+        # Pyomo currently ONLY supports Real variables (not complex
+        # variables).  If that ever changes, then we will need to
+        # revisit hard-coding the symbol type here
+        sympy_obj = sympy.Symbol("x%d" % self.i, real=True)
         self.i += 1
         self.pyomo2sympy[pyomo_object] = sympy_obj
         self.sympy2pyomo[sympy_obj] = pyomo_object


Test patch:
diff --git a/pyomo/core/tests/unit/test_symbolic.py b/pyomo/core/tests/unit/test_symbolic.py
--- a/pyomo/core/tests/unit/test_symbolic.py
+++ b/pyomo/core/tests/unit/test_symbolic.py
@@ -243,6 +243,16 @@ def test_sqrt_function(self):
         self.assertTrue(e.is_expression_type())
         self.assertEqual(s(e), s(0.5 * m.x**-0.5))
 
+    def test_abs_and_complex(self):
+        m = ConcreteModel()
+        m.x = Var()
+
+        # Unless we force sympy to know that X is real, it will return a
+        # complex expression.  This tests issue #1139.
+        e = differentiate(abs(m.x**2), wrt=m.x)
+        self.assertTrue(e.is_expression_type())
+        self.assertEqual(s(e), s(2 * m.x))
+
     def test_param(self):
         m = ConcreteModel()
         m.x = Var()
Repository: Pyomo/pyomo

Issue:
Add versioning to MC++ library shared object
Versioning needs to be added to MC++ library so that it is possible to know when the `*.so` file is out of date with the Pyomo version.


Discussion:

Resolving patch:
diff --git a/pyomo/contrib/mcpp/pyomo_mcpp.py b/pyomo/contrib/mcpp/pyomo_mcpp.py
--- a/pyomo/contrib/mcpp/pyomo_mcpp.py
+++ b/pyomo/contrib/mcpp/pyomo_mcpp.py
@@ -39,6 +39,8 @@
 
 path = os.path.dirname(__file__)
 
+__version__ = "19.11.12"
+
 
 def mcpp_available():
     """True if the MC++ shared object file exists. False otherwise."""
@@ -60,6 +62,9 @@ def _MCPP_lib():
 
     _MCPP_lib._mcpp = mcpp = ctypes.CDLL(Library('mcppInterface').path())
 
+    # Version number
+    mcpp.get_version.restype = ctypes.c_char_p
+    
     mcpp.toString.argtypes = [ctypes.c_void_p]
     mcpp.toString.restype = ctypes.c_char_p
 
@@ -198,6 +203,14 @@ class MCPP_visitor(StreamBasedExpressionVisitor):
     def __init__(self, expression, improved_var_bounds=None):
         super(MCPP_visitor, self).__init__()
         self.mcpp = _MCPP_lib()
+        so_file_version = self.mcpp.get_version()
+        if six.PY3:
+            so_file_version = so_file_version.decode("utf-8")
+        if not so_file_version == __version__:
+            raise MCPP_Error(
+                "Shared object file version %s is out of date with MC++ interface version %s. "
+                "Please rebuild the library." % (so_file_version, __version__)
+            )
         self.missing_value_warnings = []
         self.expr = expression
         vars = list(identify_variables(expression, include_fixed=False))


Test patch:
Repository: Pyomo/pyomo

Issue:
Cannot create empty two-dimensional set from a rule
I cannot create an empty two-dimensional set from a rule on master. Here is an example:

```
import pyomo.environ as pe

def a_rule(m):
    for i in range(0):
        yield i

def b_rule(m):
    for i in range(3):
        for j in range(0):
            yield i, j

m = pe.ConcreteModel()
m.a = pe.Set(initialize=a_rule, dimen=1)
m.a.pprint()
m.b = pe.Set(initialize=b_rule, dimen=2)
m.b.pprint()
```

Pyomo 5.6.9 output
--------------------
```
a : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=(None, None)
    []
b : Dim=0, Dimen=2, Size=0, Domain=None, Ordered=False, Bounds=None
    []
```

Pyomo master output/error
---------------------------
```
a : Size=1, Index=None, Ordered=Insertion
    Key  : Dimen : Domain : Size : Members
    None :     1 :    Any :    0 :      {}
ERROR: Constructing component 'b' from data=None failed: IndexError: tuple
    index out of range
Traceback (most recent call last):
  File "blah.py", line 18, in <module>
    m.b = pe.Set(initialize=b_rule, dimen=2)
  File "/.../pyomo/pyomo/core/base/block.py", line 543, in __setattr__
    self.add_component(name, val)
  File "/.../pyomo/pyomo/core/base/block.py", line 1079, in add_component
    val.construct(data)
  File "/.../pyomo/pyomo/core/base/util.py", line 125, in construct
    return base.construct(self, data)
  File "/.../pyomo/pyomo/core/base/set.py", line 1976, in construct
    self._getitem_when_not_present(index)
  File "/.../pyomo/pyomo/core/base/set.py", line 2019, in _getitem_when_not_present
    _values = self._init_values(_block, index)
  File "/.../pyomo/pyomo/core/base/set.py", line 419, in __call__
    if isinstance(_val[0], tuple):
IndexError: tuple index out of range
```


Discussion:

Resolving patch:
diff --git a/pyomo/core/base/set.py b/pyomo/core/base/set.py
--- a/pyomo/core/base/set.py
+++ b/pyomo/core/base/set.py
@@ -416,6 +416,8 @@ def __call__(self, parent, index):
 
         if not isinstance(_val, collections_Sequence):
             _val = tuple(_val)
+        if len(_val) == 0:
+            return _val
         if isinstance(_val[0], tuple):
             return _val
         return self._tuplize(_val, parent, index)


Test patch:
diff --git a/pyomo/core/tests/unit/test_set.py b/pyomo/core/tests/unit/test_set.py
--- a/pyomo/core/tests/unit/test_set.py
+++ b/pyomo/core/tests/unit/test_set.py
@@ -5862,3 +5862,19 @@ def objective_rule(model_arg):
 
         finally:
             normalize_index.flatten = _oldFlatten
+
+    def test_issue_1375(self):
+        def a_rule(m):
+            for i in range(0):
+                yield i
+
+        def b_rule(m):
+            for i in range(3):
+                for j in range(0):
+                    yield i, j
+
+        m = ConcreteModel()
+        m.a = Set(initialize=a_rule, dimen=1)
+        self.assertEqual(len(m.a), 0)
+        m.b = Set(initialize=b_rule, dimen=2)
+        self.assertEqual(len(m.b), 0)
Repository: Pyomo/pyomo

Issue:
Error with TransformationFactory('core.relax_integers')
When I use `TransformationFactory('core.relax_integrality').apply_to(m)`, a warning came up.

`WARNING: DEPRECATED: core.relax_integrality is deprecated.  Use core.relax_integers  (deprecated in TBD)`

When I changed the code to `TransformationFactory('core.relax_integers').apply_to(m)`, an error came up:

```
TransformationFactory('core.relax_integers').apply_to(m)
AttributeError: 'NoneType' object has no attribute 'apply_to'
```


Error with TransformationFactory('core.relax_integers')
When I use `TransformationFactory('core.relax_integrality').apply_to(m)`, a warning came up.

`WARNING: DEPRECATED: core.relax_integrality is deprecated.  Use core.relax_integers  (deprecated in TBD)`

When I changed the code to `TransformationFactory('core.relax_integers').apply_to(m)`, an error came up:

```
TransformationFactory('core.relax_integers').apply_to(m)
AttributeError: 'NoneType' object has no attribute 'apply_to'
```




Discussion:
That's a typo. Try `core.relax_integer_vars`.
@jsiirola Would it be difficult for `TransformationFactory` to return an object that raises an error listing the valid transformations if you happen to give it an invalid argument, instead of returning `None`, as it currently does?
That's a typo. Try `core.relax_integer_vars`.
@jsiirola Would it be difficult for `TransformationFactory` to return an object that raises an error listing the valid transformations if you happen to give it an invalid argument, instead of returning `None`, as it currently does?

Resolving patch:
diff --git a/pyomo/core/plugins/transform/relax_integrality.py b/pyomo/core/plugins/transform/relax_integrality.py
--- a/pyomo/core/plugins/transform/relax_integrality.py
+++ b/pyomo/core/plugins/transform/relax_integrality.py
@@ -23,7 +23,7 @@ class RelaxIntegrality(RelaxIntegerVars):
     """
 
     @deprecated(
-        "core.relax_integrality is deprecated.  Use core.relax_integers",
+        "core.relax_integrality is deprecated.  Use core.relax_integer_vars",
         version='TBD')
     def __init__(self, **kwds):
         super(RelaxIntegrality, self).__init__(**kwds)


Test patch:
Repository: Pyomo/pyomo

Issue:
Deprecate the pyomo install-extras subcommand
The conda pyomo.extras package supports this functionality more robustly.  We should not duplicate this logic in separate places.


Discussion:
I am not sure I agree.  If we are going to continue to support an "extras" (optional dependencies) package, I feel we should support both conda and a non-conda option.  Ideally the conda recipe would query the code behind the `pyomo install-extras` command to ensure that they install the same packages.
I'm not sure what you mean by "the conda recipe would query the code behind the pyomo install-extras command".  The conda-forge build script builds a conda package from a YAML metafile.  I don't see how this would ever be derived or be related to the pyomo logic.
Fundamentally, the pyomo install-extras logic is different from conda because we found that creating a Python package with simple dependencies for pyomo.extras was not robust.  At least a while back, pip would routinely fail for some of Pyomo's optional dependencies.  Hence, we tried out conda, which has proved more robust.  The logic executed by pyomo install-extras tries to manually install packages such that failures are handled in a graceful manner.  Maybe pip can does this itself now-adays.  But in any case these seem like separate processes to achieve the same result.

I'm still inclined to tell users that we support the easy installation of extras with just conda.  This doesn't limit *them* from installing these dependencies themselves.

Resolving patch:
diff --git a/pyomo/scripting/plugins/extras.py b/pyomo/scripting/plugins/extras.py
--- a/pyomo/scripting/plugins/extras.py
+++ b/pyomo/scripting/plugins/extras.py
@@ -11,6 +11,8 @@
 import six
 from pyomo.scripting.pyomo_parser import add_subparser, CustomHelpFormatter
 
+from pyomo.common.deprecation import deprecated
+
 def get_packages():
     packages = [
         'sympy', 
@@ -30,6 +32,11 @@ def get_packages():
         packages.append(('pyro','Pyro'))
     return packages
 
+@deprecated(
+        "Use of the pyomo install-extras is deprecated."
+        "The current recommended course of action is to manually install "
+        "optional dependencies as needed.",
+        version='TBD')
 def install_extras(args=[], quiet=False):
     #
     # Verify that pip is installed


Test patch:
Repository: Pyomo/pyomo

Issue:
ValueError: The truth value of a MultiIndex is ambiguous is caused by set.py
The PyomoGallery network interdiction examples fails with the error:
`ValueError: The truth value of a MultiIndex is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().`

The error is fixed if line 414 in pyomo/core/base/set.py is changed from:
`elif not _val:`
to
`elif _val is None:`

I am running Anaconda Python 3.6.5 under Windows 10 (64 bit)


Discussion:

Resolving patch:
diff --git a/pyomo/core/base/set.py b/pyomo/core/base/set.py
--- a/pyomo/core/base/set.py
+++ b/pyomo/core/base/set.py
@@ -411,7 +411,7 @@ def __call__(self, parent, index):
             return _val
         elif _val is Set.Skip:
             return _val
-        elif not _val:
+        elif _val is None:
             return _val
 
         if not isinstance(_val, collections_Sequence):


Test patch:
diff --git a/pyomo/core/tests/unit/test_set.py b/pyomo/core/tests/unit/test_set.py
--- a/pyomo/core/tests/unit/test_set.py
+++ b/pyomo/core/tests/unit/test_set.py
@@ -25,6 +25,7 @@
 
 from pyomo.common import DeveloperError
 from pyomo.common.dependencies import numpy as np, numpy_available
+from pyomo.common.dependencies import pandas as pd, pandas_available
 from pyomo.common.log import LoggingIntercept
 from pyomo.core.expr import native_numeric_types, native_types
 import pyomo.core.base.set as SetModule
@@ -1768,6 +1769,34 @@ def x_init(m,i):
         self.assertEqual(i.x[1].domain, i.A*i.B)
         self.assertEqual(i.x[1], [])
 
+    @unittest.skipIf(not pandas_available, "pandas is not available")
+    def test_pandas_multiindex_set_init(self):
+        # Test that TuplizeValuesInitializer does not assume truthiness
+        # If it does, pandas will complain with the following error:
+        # ValueError: The truth value of a MultiIndex is ambiguous. 
+        # Use a.empty, a.bool(), a.item(), a.any() or a.all().
+        iterables = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]
+        pandas_index = pd.MultiIndex.from_product(
+            iterables, 
+            names=['first', 'second']
+        )
+
+        model = ConcreteModel()
+        model.a = Set(initialize=pandas_index,
+                      dimen=pandas_index.nlevels)
+
+        # we will confirm that dimension is inferred correctly
+        model.b = Set(initialize=pandas_index)
+
+        self.assertIsInstance(model.a, Set)
+        self.assertEquals(list(model.a), list(pandas_index))
+        self.assertEquals(model.a.dimen, pandas_index.nlevels)
+
+        self.assertIsInstance(model.b, Set)
+        self.assertEquals(list(model.b), list(pandas_index))
+        self.assertEquals(model.b.dimen, pandas_index.nlevels)
+
+
 class TestSetUnion(unittest.TestCase):
     def test_pickle(self):
         a = SetOf([1,3,5]) | SetOf([2,3,4])
Repository: Pyomo/pyomo

Issue:
Missing value function import in persistent_solver.py
https://github.com/Pyomo/pyomo/blob/4f11cb7326c68c9aa561f4c1cc4142f1ab2b1f94/pyomo/solvers/plugins/solvers/persistent_solver.py#L35

`value` is never imported in this module.


Discussion:

Resolving patch:
diff --git a/pyomo/solvers/plugins/solvers/persistent_solver.py b/pyomo/solvers/plugins/solvers/persistent_solver.py
--- a/pyomo/solvers/plugins/solvers/persistent_solver.py
+++ b/pyomo/solvers/plugins/solvers/persistent_solver.py
@@ -13,7 +13,8 @@
 from pyomo.core.kernel.block import IBlock
 from pyomo.core.base.suffix import active_import_suffix_generator
 from pyomo.core.kernel.suffix import import_suffix_generator
-from pyomo.core.expr.numvalue import native_numeric_types
+from pyomo.core.expr.numvalue import native_numeric_types, value
+from pyomo.core.expr.visitor import evaluate_expression
 from pyomo.core.base.constraint import Constraint
 from pyomo.core.base.var import Var
 from pyomo.core.base.sos import SOSConstraint
@@ -30,7 +31,7 @@ def _convert_to_const(val):
     if val.__class__ in native_numeric_types:
         return val
     elif val.is_expression_type():
-        return EXPR.evaluate_expression(val)
+        return evaluate_expression(val)
     else:
         return value(val)
 


Test patch: