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

Issue:
RangeSet construct not supporting doc example
I was trying to run the example on linear integer programming from [https://pyomo.readthedocs.io/en/stable/pyomo_overview/simple_examples.html](url), the one with TV and Film. Unfortunately when running 
```
pyomo solve LIP.py lip_2.dat --solver ps --summary
```
I get the following error:
```
[    0.00] Setting up Pyomo environment
[    0.00] Applying Pyomo preprocessing actions
[    0.00] Creating model
ERROR: Constructing component 'I' from data={None: ['TV', 'Film']} failed:
        ValueError: RangeSet.construct() does not support the data= argument.
[    0.01] Pyomo Finished
ERROR: Unexpected exception while running model:
        RangeSet.construct() does not support the data= argument.
errorcode: 1
retval: None
```
The previous example with numbers only works fine. What is the cause of this problem?


Discussion:
I figured out what is the problem - It is defined as `RangeSet` while it should be `Set` without any arguments. Still, I think it should be noted in the docs

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
@@ -2701,7 +2701,10 @@ def construct(self, data=None):
                              % (self.name, data))
         if data is not None:
             raise ValueError(
-                "RangeSet.construct() does not support the data= argument.")
+                "RangeSet.construct() does not support the data= argument.\n"
+                "Initialization data (range endpoints) can only be supplied "
+                "as numbers, constants, or Params to the RangeSet() "
+                "declaration")
         self._constructed = True
 
         args, ranges = self._init_data


Test patch:
Repository: Pyomo/pyomo

Issue:
Iterators in IndexedComponent
In making Egret fully Pyomo 6 compatible grid-parity-exchange/Egret/pull/232, I noticed an incongruity and potential performance issue in `IndexedComponent`:

https://github.com/Pyomo/pyomo/blob/acfe56514961bde3e68c781b1620387ae17d7bc1/pyomo/core/base/indexed_component.py#L361-L371

These standard dict-like iterator methods all return a `list`, which is not as promised in their docstrings.


Discussion:

Resolving patch:
diff --git a/pyomo/core/base/indexed_component.py b/pyomo/core/base/indexed_component.py
--- a/pyomo/core/base/indexed_component.py
+++ b/pyomo/core/base/indexed_component.py
@@ -360,15 +360,17 @@ def iteritems(self):
 
     def keys(self):
         """Return an iterator of the keys in the dictionary"""
-        return [ x for x in self ]
+        return iter(self)
 
     def values(self):
         """Return an iterator of the component data objects in the dictionary"""
-        return [ self[x] for x in self ]
+        for s in self:
+            yield self[s]
 
     def items(self):
         """Return an iterator of (index,data) tuples from the dictionary"""
-        return [ (x, self[x]) for x in self ]
+        for s in self:
+            yield s, self[s]
 
     def __getitem__(self, index):
         """


Test patch:
diff --git a/pyomo/core/tests/unit/test_compare.py b/pyomo/core/tests/unit/test_compare.py
--- a/pyomo/core/tests/unit/test_compare.py
+++ b/pyomo/core/tests/unit/test_compare.py
@@ -28,7 +28,7 @@ class TestConvertToPrefixNotation(unittest.TestCase):
     def test_linear_expression(self):
         m = pe.ConcreteModel()
         m.x = pe.Var([1, 2, 3, 4])
-        e = LinearExpression(constant=3, linear_coefs=m.x.keys(), linear_vars=m.x.values())
+        e = LinearExpression(constant=3, linear_coefs=list(m.x.keys()), linear_vars=list(m.x.values()))
         expected = [(LinearExpression, 9), 3, 1, 2, 3, 4, m.x[1], m.x[2], m.x[3], m.x[4]]
         pn = convert_expression_to_prefix_notation(e)
         self.assertEqual(pn, expected)
Repository: Pyomo/pyomo

Issue:
ASL Sover Version Check Timeout Increase
## Summary

Increase the subprocess timeout in the ASL solver version check. 

### Rationale

This may be an odd use case, but I have Windows batch files wrapping solvers running in WSL, so I can use solvers built for Linux on Windows.  It seems to take a bit longer to return with the version info.  The version returns fine with the longer timeout when I tested it with Ipopt.  I'm not sure exactly what the best number is but 6 seconds seems to work for me.

### Description

<!-- Describe the solution you'd like and the alternatives you have considered. -->


### Additional information
<!-- Add any other context about the feature request here. -->



<!-- If you want to ask a question (how to use Pyomo, what it can do, etc.), please refer to our [online documentation](https://github.com/Pyomo/pyomo#getting-help) on ways to get help. -->

ASL Sover Version Check Timeout Increase
## Summary

Increase the subprocess timeout in the ASL solver version check. 

### Rationale

This may be an odd use case, but I have Windows batch files wrapping solvers running in WSL, so I can use solvers built for Linux on Windows.  It seems to take a bit longer to return with the version info.  The version returns fine with the longer timeout when I tested it with Ipopt.  I'm not sure exactly what the best number is but 6 seconds seems to work for me.

### Description

<!-- Describe the solution you'd like and the alternatives you have considered. -->


### Additional information
<!-- Add any other context about the feature request here. -->



<!-- If you want to ask a question (how to use Pyomo, what it can do, etc.), please refer to our [online documentation](https://github.com/Pyomo/pyomo#getting-help) on ways to get help. -->



Discussion:

Resolving patch:
diff --git a/pyomo/solvers/plugins/solvers/ASL.py b/pyomo/solvers/plugins/solvers/ASL.py
--- a/pyomo/solvers/plugins/solvers/ASL.py
+++ b/pyomo/solvers/plugins/solvers/ASL.py
@@ -94,7 +94,7 @@ def _get_version(self):
             return _extract_version('')
         try:
             results = subprocess.run([solver_exec, "-v"],
-                                     timeout=2,
+                                     timeout=5,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.STDOUT,
                                      universal_newlines=True)


Test patch:
Repository: Pyomo/pyomo

Issue:
Floor and ceil operator codes switched in NL file writer
## Summary

I was looking for *.nl file operator codes (didn't find what I was looking for), but noticed that floor and ceil are inverted in:
https://github.com/Pyomo/pyomo/blob/main/pyomo/repn/plugins/ampl/ampl_.py#L101
```
    'ceil':   'o13',
    'floor':  'o14'
```

Either that, or the ASL code is wrong: 
https://github.com/ampl/asl/blob/master/src/solvers/opcode.hd#L10

```
#define FLOOR		13
#define CEIL		14
```

Small edge case--doubt anyone's using it, but fyi.


Discussion:

Resolving patch:
diff --git a/pyomo/repn/plugins/ampl/ampl_.py b/pyomo/repn/plugins/ampl/ampl_.py
--- a/pyomo/repn/plugins/ampl/ampl_.py
+++ b/pyomo/repn/plugins/ampl/ampl_.py
@@ -98,8 +98,8 @@ def set_pyomo_amplfunc_env(external_libs):
     'atanh':  'o47',
     'pow':    'o5',
     'abs':    'o15',
-    'ceil':   'o13',
-    'floor':  'o14'
+    'ceil':   'o14',
+    'floor':  'o13'
 }
 
 # build string templates


Test patch:
diff --git a/pyomo/repn/tests/ampl/small14.ampl.nl b/pyomo/repn/tests/ampl/small14.ampl.nl
--- a/pyomo/repn/tests/ampl/small14.ampl.nl
+++ b/pyomo/repn/tests/ampl/small14.ampl.nl
@@ -1,7 +1,7 @@
 g3 0 1 0	# problem small14
  2 19 1 0 19	# vars, constraints, objectives, ranges, eqns
  19 0	# nonlinear constraints, objectives
- 0 0	# network constraints: nonlinear, linear
+ 0 0 	# network constraints: nonlinear, linear
  2 0 0	# nonlinear vars in constraints, objectives, both
  0 0 0 1	# linear network variables; functions; arith, flags
  0 0 0 0 0	# discrete variables: binary, integer, nonlinear (b,c,o)
@@ -48,8 +48,8 @@ C12
 o52
 o3
 o0
-v0
 n7.3890560989306495
+v0
 n5.43656365691809
 C13
 o47
diff --git a/pyomo/repn/tests/ampl/small14.pyomo.nl b/pyomo/repn/tests/ampl/small14.pyomo.nl
--- a/pyomo/repn/tests/ampl/small14.pyomo.nl
+++ b/pyomo/repn/tests/ampl/small14.pyomo.nl
@@ -61,10 +61,10 @@ C15
 o39
 v0
 C16
-o13
+o14
 v0
 C17
-o14
+o13
 v0
 C18
 o15
Repository: Pyomo/pyomo

Issue:
`build-extensions` fails on pip install because it can't find `FindASL.cmake`
## Summary
In a fresh Python 3.9.9 environment, I ran `pip install pyomo`, then `pyomo build-extensions`, and get a failure to build the PyNumero libraries as `ampl_function_demo/src/FindASL.cmake` is not found. It doesn't appear to be anywhere in `site_packages`.

### Steps to reproduce the issue

```console
$ pip install pyomo
$ pyomo build-extensions
```

### Error Message

```
**** Building PyNumero libraries ****
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:18 (INCLUDE):
  INCLUDE could not find load file:

    /home/robert/idaes/fresh-env/lib/python3.9/site-packages/pyomo/contrib/pynumero/src/../../ampl_function_demo/src/FindASL.cmake


-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Configuring incomplete, errors occurred!
See also "/tmp/tmpmu0rmdiu/CMakeFiles/CMakeOutput.log".
ERROR: DistutilsExecError: command '/usr/bin/cmake' failed with exit code 1
```

### Information on your system

Pyomo version: 6.3.0
Python version: 3.9.9
Operating system: Ubuntu 18.04
How Pyomo was installed (PyPI, conda, source): pip



Discussion:
We probably need to add that file to `MANIFEST.in`

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -247,6 +247,7 @@ def _print_deps(self, deplist):
     },
     packages = find_packages(exclude=("scripts",)),
     package_data = {
+        "pyomo.contrib.ampl_function_demo": ["src/*"],
         "pyomo.contrib.appsi.cmodel": ["src/*"],
         "pyomo.contrib.mcpp": ["*.cpp"],
         "pyomo.contrib.pynumero": ['src/*', 'src/tests/*'],


Test patch:
Repository: Pyomo/pyomo

Issue:
Pyomo.environ doesn't initialize when called by cython
See full details on stack overflow:
https://stackoverflow.com/questions/71902579/pyomo-doesnt-load-when-called-via-cython

Also see
 https://github.com/Pyomo/pyomo/issues/2374#issuecomment-1115424111_ :

"...(The linked question is probably either a problem with how the pyomo driver script interacts with argparse [it really shouldn't be doing that much when pyomo.environ is imported - that is almost certainly a bug], or with how cython and argparse interact)."

_Originally posted by @jsiirola in https://github.com/Pyomo/pyomo/issues/2374#issuecomment-1115424111_


Discussion:

Resolving patch:
diff --git a/pyomo/scripting/pyomo_parser.py b/pyomo/scripting/pyomo_parser.py
--- a/pyomo/scripting/pyomo_parser.py
+++ b/pyomo/scripting/pyomo_parser.py
@@ -81,11 +81,8 @@ def get_version():
 
 to print information about the `solve` subcommand.
 """
-_pyomo_parser = argparse.ArgumentParser(
-    description=doc, epilog=epilog, formatter_class=CustomHelpFormatter )
-_pyomo_parser.add_argument("--version", action="version", version=get_version())
-_pyomo_subparsers = _pyomo_parser.add_subparsers(
-    dest='subparser_name', title='subcommands' )
+_pyomo_parser = None
+_pyomo_subparsers = None
 
 subparsers = []
 
@@ -93,7 +90,9 @@ def add_subparser(name, **args):
     """
     Add a subparser to the 'pyomo' command.
     """
-    global subparsers
+    if _pyomo_subparsers is None:
+        get_parser()
+
     func = args.pop('func', None)
     parser = _pyomo_subparsers.add_parser(name, **args)
     subparsers.append(name)
@@ -105,5 +104,16 @@ def get_parser():
     """
     Return the parser used by the 'pyomo' commmand.
     """
+    global _pyomo_parser
+    if _pyomo_parser is None:
+        _pyomo_parser = argparse.ArgumentParser(
+            description=doc,
+            epilog=epilog,
+            formatter_class=CustomHelpFormatter
+        )
+        _pyomo_parser.add_argument(
+            "--version", action="version", version=get_version())
+        global _pyomo_subparsers
+        _pyomo_subparsers = _pyomo_parser.add_subparsers(
+            dest='subparser_name', title='subcommands' )
     return _pyomo_parser
-


Test patch:
Repository: Pyomo/pyomo

Issue:
Erroneous print message in plugin availability printout
## Summary
In debugging a different issue, we discovered that some solver or interface is printing an extra message at the beginning of the `pyomo help --solvers || exit 1` command.

### Steps to reproduce the issue
Assuming APPSI is available (as we presume currently that the issue comes from APPSI):

```console
$ pyomo help --solvers
...
Serial Solver Interfaces
------------------------
The serial manager supports the following solver interfaces:

Version identifier: 22.1.0.0 | 2022-03-25 | 54982fbec
CPXPARAM_Read_DataCheck                          1
Tried aggregator 1 time.
LP Presolve eliminated 0 rows and 1 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.00 ticks)
   +appsi_cbc         2.10.4    Automated persistent interface to Cbc
   +appsi_cplex       22.1      Automated persistent interface to
                                Cplex
```

### Error Message
There is no error, but a print message from CPLEX is not being suppressed.

### Information on your system

Pyomo version: 6.4.0
Python version: All where APPSI is available
Operating system: All where APPSI is available
How Pyomo was installed (PyPI, conda, source): source
Solver (if applicable): cplex


### Additional information
See this job for an example: https://github.com/Pyomo/pyomo/runs/6281085876?check_suite_focus=true


Discussion:

Resolving patch:
diff --git a/pyomo/contrib/appsi/solvers/cplex.py b/pyomo/contrib/appsi/solvers/cplex.py
--- a/pyomo/contrib/appsi/solvers/cplex.py
+++ b/pyomo/contrib/appsi/solvers/cplex.py
@@ -94,12 +94,14 @@ def _check_license(self):
             else:
                 try:
                     m = self._cplex.Cplex()
+                    m.set_results_stream(None)
                     m.variables.add(lb=[0]*1001)
                     m.solve()
                     Cplex._available = self.Availability.FullLicense
                 except self._cplex.exceptions.errors.CplexSolverError:
                     try:
                         m = self._cplex.Cplex()
+                        m.set_results_stream(None)
                         m.variables.add(lb=[0])
                         m.solve()
                         Cplex._available = self.Availability.LimitedLicense


Test patch:
Repository: Pyomo/pyomo

Issue:
value is not imported in pyomo.core.base.external



Discussion:

Resolving patch:
diff --git a/pyomo/core/base/external.py b/pyomo/core/base/external.py
--- a/pyomo/core/base/external.py
+++ b/pyomo/core/base/external.py
@@ -10,7 +10,6 @@
 #  ___________________________________________________________________________
 
 import logging
-import os
 import types
 import weakref
 from pyomo.common.pyomo_typing import overload
@@ -22,7 +21,7 @@
 from pyomo.common.fileutils import find_library
 from pyomo.core.expr.numvalue import (
     native_types, native_numeric_types, pyomo_constant_types,
-    NonNumericValue, NumericConstant,
+    NonNumericValue, NumericConstant, value
 )
 from pyomo.core.expr import current as EXPR
 from pyomo.core.base.component import Component


Test patch:
Repository: Pyomo/pyomo

Issue:
cpxlp writer: enhance performance by not parsing quadratic terms if not supported
## Summary

Specify that `generate_standard_repn()` should not parse quadratic terms if quadratic support is not enabled.

### Rationale

This will avoid lots of potentially time consuming logic in `generate_standard_repn()`. `generate_standard_repn()` is what takes the most time when creating an `.lp` file (responsible for ~30% of time when using a medium size model with optimized constraint definitions). 

### Description

I plan to submit a pull request where `generate_standard_repn(quadratic=False)` when the solver doesn't support quadratic terms.




Discussion:

Resolving patch:
diff --git a/pyomo/repn/plugins/cpxlp.py b/pyomo/repn/plugins/cpxlp.py
--- a/pyomo/repn/plugins/cpxlp.py
+++ b/pyomo/repn/plugins/cpxlp.py
@@ -552,7 +552,10 @@ def print_expr_canonical(
                 if gen_obj_repn == False:
                     repn = block_repn[objective_data]
                 else:
-                    repn = generate_standard_repn(objective_data.expr)
+                    repn = generate_standard_repn(
+                        objective_data.expr,
+                        quadratic=supports_quadratic_objective,
+                    )
                     if gen_obj_repn:
                         block_repn[objective_data] = repn
 
@@ -637,7 +640,10 @@ def constraint_generator():
                         if constraint_data._linear_canonical_form:
                             repn = constraint_data.canonical_form()
                         else:
-                            repn = generate_standard_repn(constraint_data.body)
+                            repn = generate_standard_repn(
+                                constraint_data.body,
+                                quadratic=supports_quadratic_constraint,
+                            )
                         if gen_con_repn:
                             block_repn[constraint_data] = repn
 


Test patch:
Repository: Pyomo/pyomo

Issue:
Fixed Vars unpickle as stale
## Summary

I'm not sure if this is a bug, but it seems unexpected? Anyway, if you pickle a model that has a fixed variable (not stale), when you unpickle it, it comes back as stale.

### Steps to reproduce the issue

```
from pyomo.environ import *
import pickle

m = ConcreteModel()
m.x = Var(domain=Binary)
m.x.fix(1)

unpickle = pickle.loads(pickle.dumps(m))

m.x.pprint()
unpickle.x.pprint()
```

```
x : Size=1, Index=None
    Key  : Lower : Value : Upper : Fixed : Stale : Domain
    None :     0 :     1 :     1 :  True : False : Binary
x : Size=1, Index=None
    Key  : Lower : Value : Upper : Fixed : Stale : Domain
    None :     0 :     1 :     1 :  True :  True : Binary
```

### Error Message

It seems like these models should be identical, even up to stale-ness, right?

### Information on your system

Pyomo version: main
Python version: 3.8
Operating system: linux
How Pyomo was installed (PyPI, conda, source): source 
Solver (if applicable):


Discussion:

Resolving patch:
diff --git a/pyomo/core/staleflag.py b/pyomo/core/staleflag.py
--- a/pyomo/core/staleflag.py
+++ b/pyomo/core/staleflag.py
@@ -21,7 +21,7 @@ def stale_mapper(self, encode, value):
             if value:
                 return 0
             else:
-                self.get_flag(0)
+                return self.get_flag(0)
 
     def _get_flag(self, current_flag):
         """Return the current global stale flag value"""


Test patch:
diff --git a/pyomo/core/tests/unit/test_var.py b/pyomo/core/tests/unit/test_var.py
--- a/pyomo/core/tests/unit/test_var.py
+++ b/pyomo/core/tests/unit/test_var.py
@@ -1598,5 +1598,27 @@ def test_stale(self):
         self.assertFalse(m.x.stale)
         self.assertFalse(m.y.stale)
 
+    def test_stale_clone(self):
+        m = ConcreteModel()
+        m.x = Var(initialize=0)
+        self.assertFalse(m.x.stale)
+        m.y = Var()
+        self.assertTrue(m.y.stale)
+        m.z = Var(initialize=0)
+        self.assertFalse(m.z.stale)
+
+        i = m.clone()
+        self.assertFalse(i.x.stale)
+        self.assertTrue(i.y.stale)
+        self.assertFalse(i.z.stale)
+
+        StaleFlagManager.mark_all_as_stale(delayed=True)
+        m.z = 5
+        i = m.clone()
+        self.assertTrue(i.x.stale)
+        self.assertTrue(i.y.stale)
+        self.assertFalse(i.z.stale)
+        
+
 if __name__ == "__main__":
     unittest.main()
Repository: Pyomo/pyomo

Issue:
Timeout on very short Optimization runs
Very short optimizations result in a timeout error in Pyomo. Actually, the optimization problem is sufficiently solved and usable. However, the Callback from CPLEX to pyomo takes too long, so pyomo throws an timeout error. The problem lies in this line of code in shellcmd.py. 
![image](https://user-images.githubusercontent.com/80811406/208399286-b736e7da-ab2d-41dd-a7ed-aa6c3ae7b36d.png)

Here the max(1, 0.01 * self._timelimit) is taken, so the timeout for a 2 minute optimization is 1.19 seconds. This is sometimes not enough for the callback. So you could adjust this line to max(120, 0.01 * self._timelimit) and the problem would be solved... :) Thanks in advance 


Discussion:

Resolving patch:
diff --git a/pyomo/opt/solver/shellcmd.py b/pyomo/opt/solver/shellcmd.py
--- a/pyomo/opt/solver/shellcmd.py
+++ b/pyomo/opt/solver/shellcmd.py
@@ -32,6 +32,16 @@
 
 logger = logging.getLogger('pyomo.opt')
 
+# The minimum absolute time (in seconds) to add to the solver timeout
+# when setting the timeout for the solver subprocess.  This provides
+# time for a solver that timed out to clean up / report a solution
+# before we forcibly kill the subprocess.
+SUBPROCESS_TIMEOUT_ABS_ADJUST = 1
+# The additional time (relative to the user-specified timeout) to add to
+# the solver timeout when setting the timeout for the solver subprocess.
+# This provides time for a solver that timed out to clean up / report a
+# solution before we forcibly kill the subprocess.
+SUBPROCESS_TIMEOUT_REL_ADJUST = 0.01
 
 class SystemCallSolver(OptSolver):
     """ A generic command line solver """
@@ -312,7 +322,8 @@ def _execute_command(self,command):
 
         timeout = self._timelimit
         if timeout is not None:
-            timeout += max(1, 0.01*self._timelimit)
+            timeout += max(SUBPROCESS_TIMEOUT_ABS_ADJUST,
+                           SUBPROCESS_TIMEOUT_REL_ADJUST*self._timelimit)
 
         ostreams = [StringIO()]
         if self._tee:


Test patch: