Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop PyUtilib Dependency in pao #100

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions pao/common/shellcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@
import subprocess
import io

from pyutilib.misc import Bunch
from pyutilib.subprocess import run_command

from pyomo.common.collections import Bunch
from pyomo.common.tee import TeeStream


def run_shellcmd(cmd, *, env=None, tee=False, time_limit=None):
ostr = io.StringIO()
rc, log = run_command(cmd, tee=tee, env=env, timelimit=time_limit,
stdout=ostr,
stderr=ostr)
if not tee:
rc, log = subprocess.run(cmd, env=env, timeout=time_limit,
stdout=ostr,
stderr=ostr)
else:
with TeeStream(*ostr) as t:
results = subprocess.run(
cmd,
env=env,
stdout=t.STDOUT,
stderr=t.STDERR,
timeout=time_limit,
universal_newlines=True,
)
rc = results.returncode
log = ostr[0].getvalue()

return Bunch(rc=rc, log=ostr.getvalue())

14 changes: 7 additions & 7 deletions pao/common/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import textwrap
import logging

from pyutilib.misc import Options
from pyomo.common.collections import Bunch

import pyomo.opt.parallel.manager
import pyomo.environ as pe
Expand Down Expand Up @@ -245,7 +245,7 @@ def _update_config(self, config_options, config=None, validate_options=True):
for k,v in config_options.items():
if k in config:
config[k] = v
keys.remove(k)
keys.remove(k)
if validate_options:
assert (len(keys) == 0), "Unexpected options to solve() have been specified: %s" % " ".join(sorted(k for k in keys))
return {key:config_options[key] for key in keys}
Expand Down Expand Up @@ -331,7 +331,7 @@ def solve(self, model, **options):
return results
except pyomo.opt.parallel.manager.ActionManagerError as err:
raise RuntimeError(str(err)) from None


"""
>>> opt = Solver('my_solver')
Expand Down Expand Up @@ -370,11 +370,11 @@ class ResultsBase(abc.ABC):

def __init__(self):
self.solution_manager = None
self.solver = Options(
self.solver = Bunch(
termination_condition=TerminationCondition.unknown,
best_feasible_objective=None,
)
self.problem = Options()
self.problem = Bunch()

@abc.abstractmethod
def found_feasible_solution(self):
Expand Down Expand Up @@ -464,7 +464,7 @@ class SolverFactory(object):
"""
A class that manages a registry of solvers.

A solver factory manages a registry that enables
A solver factory manages a registry that enables
solvers to be created by name.
"""

Expand All @@ -487,7 +487,7 @@ def register(self, cls=None, *, name=None, doc=None):
Returns
-------
decorator
If the **cls** parameter is None, then a class
If the **cls** parameter is None, then a class
decorator function
is returned that can be used to register a solver.
"""
Expand Down
6 changes: 3 additions & 3 deletions pao/common/tests/test_solver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
import pyutilib.th as unittest
import pyomo.common.unittest as unittest
import pyomo.environ as pe

from pao import Solver
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_neos_cbc_available(self):
opt = Solver('cbc', server='neos', email='[email protected]')
self.assertTrue(opt.available())
res = opt.solve(M, tee=True)

self.assertTrue(math.isclose(M.x.value, 1, abs_tol=1e-6))
self.assertTrue(math.isclose(M.y.value, 0, abs_tol=1e-6))

Expand All @@ -131,7 +131,7 @@ def test_neos_cbc_options(self):
opt = Solver('cbc', server='neos', email='[email protected]', foo=1, bar=None)
self.assertTrue(opt.available())
res = opt.solve(M)

self.assertTrue(math.isclose(M.x.value, 1, abs_tol=1e-6))
self.assertTrue(math.isclose(M.y.value, 0, abs_tol=1e-6))

Expand Down
2 changes: 1 addition & 1 deletion pao/duality/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#pylint: disable-msg=too-many-branches
#pylint: disable-msg=too-many-statements

from pyutilib.misc import Bunch
from pyomo.common.collections import Bunch
from pyomo.repn import generate_standard_repn
from pyomo.core import (Var,
Constraint,
Expand Down
9 changes: 5 additions & 4 deletions pao/duality/tests/test_linear_dual.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
except ImportError:
yaml_available=False

import pyutilib.misc
from pyutilib.misc import Options, Container
import pyutilib.th as unittest
import pyomo.common.collections
from pyomo.common.collections import Bunch
import pyomo.common.unittest as unittest
from pyomo.common.fileutils import import_file

from pyomo.environ import TransformationFactory, SolverFactory, ComponentUID
import pyomo.opt
Expand All @@ -45,7 +46,7 @@ def run_bilevel(self, *_args, **kwds):
#
# Import the model file to create the model
#
usermodel = pyutilib.misc.import_file(_args[0], clear_cache=True)
usermodel = import_file(_args[0], clear_cache=True)
instance = usermodel.model
#
# Collected local variables
Expand Down
2 changes: 1 addition & 1 deletion pao/mpr/repn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import collections.abc
from scipy.sparse import csr_matrix, dok_matrix
import numpy as np
from pyutilib.misc import Bunch
from pyomo.common.collections import Bunch


def _equal_nparray(Ux, U_coef, Lx, L_coef):
Expand Down
1 change: 0 additions & 1 deletion pao/mpr/solvers/fa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#
import time
import numpy as np
import pyutilib
import pyomo.environ as pe
import pyomo.opt
from pyomo.common.config import ConfigBlock, ConfigValue
Expand Down
1 change: 0 additions & 1 deletion pao/mpr/solvers/ld.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#
import time
import numpy as np
import pyutilib
import pyomo.environ as pe
import pyomo.opt

Expand Down
1 change: 0 additions & 1 deletion pao/mpr/solvers/mibs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import sys
import time
import numpy as np
import pyutilib
import pyomo.environ as pe
import pyomo.opt
from pyomo.common.config import ConfigBlock, ConfigValue
Expand Down
5 changes: 2 additions & 3 deletions pao/mpr/solvers/pccg.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#
# A solver for linear bilevel programs using
# using projected column constraint generation
# "A projection-based reformulation and decomposition algorithm for global optimization
# "A projection-based reformulation and decomposition algorithm for global optimization
# of a class of mixed integer bilevel linear programs" by Dajun Yue, Jiyao Gao, Bo Zeng, Fengqi You
#
# Adapted from an implementation by She'ifa Punla-Green at Sandia National Labs
#
#This algorithm seeks to solve the following bilevel MILP:
# min cR*xu + cZ*yu + dR*xl0 + dZ*yl0
# min cR*xu + cZ*yu + dR*xl0 + dZ*yl0
# s.t. AR*xu + AZ*yu + BR*xl0 + BZ* yl0 <= r
# (xl0,yl0) in argmax {wR*xl+wZ*yl: PR*xl+PZ*yl<=s-QR*xu-QZ*yu}
#
import time
import numpy as np
from munch import Munch
import pyutilib
import pyomo.environ as pe
import pyomo.opt
from pyomo.common.config import ConfigBlock, ConfigValue
Expand Down
1 change: 0 additions & 1 deletion pao/mpr/solvers/reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#
import time
import numpy as np
import pyutilib
import pyomo.environ as pe
import pyomo.opt
from pyomo.common.config import ConfigBlock, ConfigValue
Expand Down
2 changes: 1 addition & 1 deletion pao/mpr/tests/test_convert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import pyutilib.th as unittest
import pyomo.common.unittest as unittest
from pao.mpr import *
from pao.mpr.convert_repn import convert_to_standard_form, convert_binaries_to_integers

Expand Down
2 changes: 1 addition & 1 deletion pao/mpr/tests/test_linearize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pprint
import numpy as np
import pyutilib.th as unittest
import pyomo.common.unittest as unittest
from pao.mpr import *
from pao.mpr.convert_repn import linearize_bilinear_terms

Expand Down
2 changes: 1 addition & 1 deletion pao/mpr/tests/test_repn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import scipy.sparse
import pyutilib.th as unittest
import pyomo.common.unittest as unittest
from pao.mpr import *
from pao.mpr.repn import SimplifiedList, LinearLevelRepn, LevelVariable, LevelValues, LevelValueWrapper1, LevelValueWrapper2

Expand Down
2 changes: 1 addition & 1 deletion pao/mpr/tests/test_solve.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
import pyutilib.th as unittest
import pyomo.common.unittest as unittest
from pao.mpr import *
from pao.mpr import examples
import pyomo.opt
Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/plugins/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
collects information that is used to in bilevel algorithm development.
"""

from pyutilib.misc import Bunch
from pyomo.common.collections import Bunch
from pyomo.repn import generate_standard_repn
from pyomo.core import (Var,
Constraint,
Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/tests/attic/test_bilevel_dualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from os.path import abspath, dirname, join
from parameterized import parameterized
import pyutilib.th as unittest
import pyomo.common.unittest as unittest

from pyomo.environ import *
from pao.pyomo.components import SubModel
Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/tests/attic/test_bilevel_matrix_repn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from os.path import abspath, dirname, join
import math
from parameterized import parameterized
import pyutilib.th as unittest
import pyomo.common.unittest as unittest

import pyomo.opt
from pyomo.environ import *
Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/tests/attic/test_highpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pyomo.opt
from os.path import abspath, dirname, join
from parameterized import parameterized
import pyutilib.th as unittest
import pyomo.common.unittest as unittest
import itertools
from pyomo.environ import *

Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/tests/attic/test_integer_bilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from os.path import abspath, dirname, join
import math
from parameterized import parameterized
import pyutilib.th as unittest
import pyomo.common.unittest as unittest

import pyomo.opt
from pyomo.environ import *
Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/tests/attic/test_linear_bilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from os.path import abspath, dirname, join
import math
from parameterized import parameterized
import pyutilib.th as unittest
import pyomo.common.unittest as unittest

import pyomo.opt
from pyomo.environ import *
Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/tests/attic/test_linear_dual.py.old
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ from os.path import abspath, dirname, normpath, join
currdir = dirname(abspath(__file__))
exdir = currdir #normpath(join(currdir,'..','..','..','examples','bilevel'))

import pyutilib.th as unittest
import pyomo.common.unittest as unittest

import pyomo.opt
import pyomo.scripting.pyomo_main as pyomo_main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from os.path import abspath, dirname, join
import math
from parameterized import parameterized
import pyutilib.th as unittest
import pyomo.common.unittest as unittest

import pyomo.opt
from pyomo.environ import *
Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/tests/attic/test_xfrm.py.old
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Test transformations for linear duality
#

import pyutilib.th as unittest
import pyomo.common.unittest as unittest
from pyomo.environ import *
import pao

Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/tests/test_convert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import pyutilib.th as unittest
import pyomo.common.unittest as unittest
import pyomo.environ as pe
from pao.pyomo.convert import collect_multilevel_tree, convert_pyomo2LinearMultilevelProblem, convert_pyomo2MultilevelProblem
from pao.pyomo import SubModel
Expand Down
2 changes: 1 addition & 1 deletion pao/pyomo/tests/test_mpr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
import pyutilib.th as unittest
import pyomo.common.unittest as unittest
from pao.pyomo import *
from pao.pyomo import examples
import pyomo.opt
Expand Down