Skip to content

Commit

Permalink
Merge branch 'pyros-vars-as-params' of github.com:shermanjasonaf/pyom…
Browse files Browse the repository at this point in the history
…o into pyros-vars-as-params
  • Loading branch information
shermanjasonaf committed Jan 30, 2025
2 parents 8bbb7ac + 198f153 commit 04663a4
Show file tree
Hide file tree
Showing 36 changed files with 264 additions and 74 deletions.
4 changes: 2 additions & 2 deletions examples/gdp/circles/circles.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
# ___________________________________________________________________________

"""
The "circles" GDP example problem originating in Lee and Grossman (2000). The
goal is to choose a point to minimize a convex quadratic function over a set of
The "circles" GDP example problem originating in Lee and Grossman (2000). The
goal is to choose a point to minimize a convex quadratic function over a set of
disjoint hyperspheres.
"""

Expand Down
8 changes: 4 additions & 4 deletions examples/gdp/farm_layout/farm_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

"""
Farm layout example from Sawaya (2006). The goal is to determine optimal placements and dimensions for farm
plots of specified areas to minimize the perimeter of a minimal enclosing fence. This is a GDP problem with
some hyperbolic constraints to establish consistency of areas with length and width. The FLay05 and FLay06
instances may take some time to solve; the others should be fast. Note that the Sawaya paper contains a
little bit of nonclarity: it references "height" variables which do not exist - we use "length" for the x-axis
plots of specified areas to minimize the perimeter of a minimal enclosing fence. This is a GDP problem with
some hyperbolic constraints to establish consistency of areas with length and width. The FLay05 and FLay06
instances may take some time to solve; the others should be fast. Note that the Sawaya paper contains a
little bit of nonclarity: it references "height" variables which do not exist - we use "length" for the x-axis
and "width" on the y-axis, and it also is unclear on the way the coordinates define the rectangles; we have
decided that they are on the bottom-left and adapted the disjunction constraints to match.
"""
Expand Down
4 changes: 1 addition & 3 deletions examples/gdp/nine_process/small_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

"""Small process synthesis-inspired toy GDP example.
"""
"""Small process synthesis-inspired toy GDP example."""

from pyomo.core import ConcreteModel, RangeSet, Var, Constraint, Objective
from pyomo.core.expr.current import exp, log, sqrt
Expand Down
4 changes: 2 additions & 2 deletions examples/gdp/small_lit/basic_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

""" Example from Section 3.2 in paper of Pseudo Basic Steps
"""Example from Section 3.2 in paper of Pseudo Basic Steps
Ref:
Pseudo basic steps: bound improvement guarantees from Lagrangian
Pseudo basic steps: bound improvement guarantees from Lagrangian
decomposition in convex disjunctive programming
Papageorgiou and Trespalacios, 2017
Expand Down
2 changes: 1 addition & 1 deletion examples/pyomo/columngeneration/cutting_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Bradley, S.P., A.C. Hax, and T.L. Magnanti. 1977. Applied Mathematical Programming,
Addison-Wesley, Reading, MA. Available: http://web.mit.edu/15.053/www/AMP.htm.
Data from https://en.wikipedia.org/wiki/Cutting_stock_problem
Data from https://en.wikipedia.org/wiki/Cutting_stock_problem
'''

import pyomo.environ as pyo
Expand Down
4 changes: 2 additions & 2 deletions examples/pyomo/sos/sos2_piecewise.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
# ___________________________________________________________________________

"""
This example shows how to represent a piecewise function using
This example shows how to represent a piecewise function using
Pyomo's built SOSConstraint component. The function is defined as:
/ 3x-2 , 1 <= x <= 2
f(x) = |
f(x) = |
\ 5x-6 , 2 <= x <= 3
"""

Expand Down
1 change: 1 addition & 0 deletions pyomo/common/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def _display(obj, *args):

class GlobalClass(object):
"test class for test_known_types"

pass


Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/community_detection/event_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

""" Logger function for community_graph.py """
"""Logger function for community_graph.py"""

from logging import getLogger
from pyomo.core import Constraint, Objective, Var
Expand Down
3 changes: 1 addition & 2 deletions pyomo/contrib/incidence_analysis/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
"""Configuration options for incidence graph generation
"""
"""Configuration options for incidence graph generation"""

import enum
from pyomo.common.config import ConfigDict, ConfigValue, InEnum
Expand Down
3 changes: 1 addition & 2 deletions pyomo/contrib/incidence_analysis/incidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
"""Functionality for identifying variables that participate in expressions
"""
"""Functionality for identifying variables that participate in expressions"""

from contextlib import nullcontext

Expand Down
4 changes: 1 addition & 3 deletions pyomo/contrib/incidence_analysis/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
"""Module for visualizing results of incidence graph or matrix analysis
"""
"""Module for visualizing results of incidence graph or matrix analysis"""
from pyomo.contrib.incidence_analysis.config import IncidenceOrder
from pyomo.contrib.incidence_analysis.interface import (
IncidenceGraphInterface,
Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/mindtpy/tests/MINLP4_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# ___________________________________________________________________________

# -*- coding: utf-8 -*-
""" Example 1 in Paper 'Using regularization and second order information in outer approximation for convex MINLP'
"""Example 1 in Paper 'Using regularization and second order information in outer approximation for convex MINLP'
The expected optimal solution value is -56.981.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# ___________________________________________________________________________

# -*- coding: utf-8 -*-
""" Example of constraint qualification.
"""Example of constraint qualification.
The expected optimal solution value is 3.
Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/mindtpy/tests/online_doc_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

""" Example in the online doc.
"""Example in the online doc.
The expected optimal solution value is 2.438447187191098.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
'''
'''
Example from Y. Bard, "Nonlinear Parameter Estimation", (pg. 124)
This example shows:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
# ___________________________________________________________________________

"""
Rooney Biegler model, based on Rooney, W. C. and Biegler, L. T. (2001). Design for
model parameter uncertainty using nonlinear confidence regions. AIChE Journal,
Rooney Biegler model, based on Rooney, W. C. and Biegler, L. T. (2001). Design for
model parameter uncertainty using nonlinear confidence regions. AIChE Journal,
47(8), 1794-1804.
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# ___________________________________________________________________________

"""
The following script can be used to run semibatch parameter estimation in
The following script can be used to run semibatch parameter estimation in
parallel and save results to files for later analysis and graphics.
Example command: mpiexec -n 4 python parallel_example.py
"""
Expand Down
4 changes: 2 additions & 2 deletions pyomo/contrib/parmest/examples/semibatch/semibatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
"""
Semibatch model, based on Nicholson et al. (2018). pyomo.dae: A modeling and
Semibatch model, based on Nicholson et al. (2018). pyomo.dae: A modeling and
automatic discretization framework for optimization with di
erential and
erential and
algebraic equations. Mathematical Programming Computation, 10(2), 187-223.
"""
import json
Expand Down
4 changes: 2 additions & 2 deletions pyomo/contrib/sensitivity_toolbox/examples/rooney_biegler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
# at the URL "https://github.com/IDAES/idaes-pse".
##############################################################################
"""
Rooney Biegler model, based on Rooney, W. C. and Biegler, L. T. (2001). Design for
model parameter uncertainty using nonlinear confidence regions. AIChE Journal,
Rooney Biegler model, based on Rooney, W. C. and Biegler, L. T. (2001). Design for
model parameter uncertainty using nonlinear confidence regions. AIChE Journal,
47(8), 1794-1804.
"""
from pyomo.common.dependencies import pandas as pd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
# This software is distributed under the 3-clause BSD License.
# ____________________________________________________________________________

"""
"""
import os
import pyomo.common.unittest as unittest
from io import StringIO
Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/viewer/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# ___________________________________________________________________________

"""
Try to import PySide6, which is the current official Qt 6 Python interface. Then,
Try to import PySide6, which is the current official Qt 6 Python interface. Then,
try PyQt5 if that doesn't work. If no compatible Qt Python interface is found,
use some dummy classes to allow some testing.
"""
Expand Down
7 changes: 7 additions & 0 deletions pyomo/contrib/viewer/tests/test_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
"""
UI Tests
"""
# The pytest-qt plugin can generate exceptions / core dumps when it is
# run in a terminal (without an active X11 screen). Setting the
# QT_QPA_PLATFORM environment variable *before* initializing Qt can work
# around this error (see https://stackoverflow.com/a/74719383):
import os

os.environ['QT_QPA_PLATFORM'] = 'offscreen'

from pyomo.environ import (
ConcreteModel,
Expand Down
41 changes: 24 additions & 17 deletions pyomo/solvers/plugins/solvers/SAS.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def _presolve(self, *args, **kwds):

def available(self, exception_flag=False):
"""True if the solver is available"""
return self._python_api_exists
if not self._python_api_exists:
return False
return self.start_sas_session() is not None

def _has_integer_variables(self):
"""True if the problem has integer variables."""
Expand Down Expand Up @@ -285,12 +287,7 @@ def __init__(self, **kwds):

# Store other options for the SAS session
self._session_options = kwds

# Create the session
try:
self._sas_session = self._sas.SASsession(**self._session_options)
except:
self._sas_session = None
self._sas_session = None

def __del__(self):
# Close the session, if we created one
Expand All @@ -314,6 +311,15 @@ def _create_statement_str(self, statement):
def sas_version(self):
return self._sasver

def start_sas_session(self):
if self._sas_session is None:
# Create (and cache) the session
try:
self._sas_session = self._sas.SASsession(**self._session_options)
except:
pass
return self._sas_session

def _apply_solver(self):
""" "Prepare the options and run the solver. Then store the data to be returned."""
logger.debug("Running SAS")
Expand Down Expand Up @@ -374,10 +380,7 @@ def _apply_solver(self):
sas_options = "option notes nonumber nodate nosource pagesize=max;"

# Get the current SAS session, submit the code and return the results
if not self._sas_session:
sas = self._sas_session = self._sas.SASsession(**self._session_options)
else:
sas = self._sas_session
sas = self.start_sas_session()

# Find the version of 9.4 we are using
self._sasver = sas.sasver
Expand Down Expand Up @@ -585,19 +588,23 @@ def __init__(self, **kwds):
self._python_api_exists = True

self._session_options = kwds

# Create the session
try:
self._sas_session = self._sas.CAS(**self._session_options)
except:
self._sas_session = None
self._sas_session = None

def __del__(self):
# Close the session, if we created one
if self._sas_session:
self._sas_session.close()
del self._sas_session

def start_sas_session(self):
if self._sas_session is None:
# Create (and cache) the session
try:
self._sas_session = self._sas.CAS(**self._session_options)
except:
pass
return self._sas_session

def _uploadMpsFile(self, s, unique):
# Declare a unique table name for the mps table
mpsdata_table_name = "mps" + unique
Expand Down
2 changes: 1 addition & 1 deletion pyomo/solvers/plugins/solvers/mosek_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ def _warm_start(self):
for pyomo_var, mosek_var in self._pyomo_var_to_solver_var_map.items():
if pyomo_var.value is not None:
self._solver_model.putxxslice(
self._whichsol, mosek_var, mosek_var + 1, [(pyomo_var.value)]
self._whichsol, mosek_var, mosek_var + 1, [pyomo_var.value]
)

if (self._version[0] > 9) & (self._whichsol == mosek.soltype.itg):
Expand Down
15 changes: 9 additions & 6 deletions pyomo/solvers/tests/checks/test_SAS.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Suffix,
)
from pyomo.opt.results import SolverStatus, TerminationCondition, ProblemSense
from pyomo.opt import SolverFactory, check_available_solvers
from pyomo.opt import SolverFactory
import warnings

CFGFILE = os.environ.get("SAS_CFG_FILE_PATH", None)
Expand All @@ -38,7 +38,10 @@
}


sas_available = check_available_solvers("sas")
try:
sas94_available = SolverFactory('_sas94').available()
except:
sas94_available = False


class SASTestAbc:
Expand Down Expand Up @@ -292,7 +295,7 @@ def test_solver_with_milp(self):
)


@unittest.skipIf(not sas_available, "The SAS solver is not available")
@unittest.skipIf(not sas94_available, "The SAS94 solver interface is not available")
class SASTestLP94(SASTestLP, unittest.TestCase):
@mock.patch(
"pyomo.solvers.plugins.solvers.SAS.SAS94.sas_version",
Expand Down Expand Up @@ -325,7 +328,7 @@ def test_solver_error(self, submit_mock, symget_mock):
self.assertEqual(results.solver.status, SolverStatus.error)


# @unittest.skipIf(not sas_available, "The SAS solver is not available")
# @unittest.skipIf(not sascas_available, "The SAS solver is not available")
@unittest.skip("Tests not yet configured for SAS Viya interface.")
class SASTestLPCAS(SASTestLP, unittest.TestCase):
solver_io = "_sascas"
Expand Down Expand Up @@ -526,13 +529,13 @@ def test_solver_warmstart_capable(self):
self.assertTrue(self.opt_sas.warm_start_capable())


# @unittest.skipIf(not sas_available, "The SAS solver is not available")
# @unittest.skipIf(not sas94_available, "The SAS solver is not available")
@unittest.skip("MILP94 tests disabled.")
class SASTestMILP94(SASTestMILP, unittest.TestCase):
pass


# @unittest.skipIf(not sas_available, "The SAS solver is not available")
# @unittest.skipIf(not sascas_available, "The SAS solver is not available")
@unittest.skip("Tests not yet configured for SAS Viya interface.")
class SASTestMILPCAS(SASTestMILP, unittest.TestCase):
solver_io = "_sascas"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
| 7x+12 , -4 <= x <= -3
| 5x+6 , -3 <= x <= -2
| 3x+2 , -2 <= x <= -1
f(x) = | 1 , -1 <= x <= 1
f(x) = | 1 , -1 <= x <= 1
| -3x+2 , 1 <= x <= 2
| -5x+6 , 2 <= x <= 3
\ -7x+12, 3 <= x <= 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
| 7x+12 , -4 <= x <= -3
| 5x+6 , -3 <= x <= -2
| 3x+2 , -2 <= x <= -1
f(x) = | 1 , -1 <= x <= 1
f(x) = | 1 , -1 <= x <= 1
| -3x+2 , 1 <= x <= 2
| -5x+6 , 2 <= x <= 3
\ -7x+12, 3 <= x <= 4
Expand Down
Loading

0 comments on commit 04663a4

Please sign in to comment.