1#  ___________________________________________________________________________
2#
3#  Pyomo: Python Optimization Modeling Objects
4#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
5#  Under the terms of Contract DE-NA0003525 with National Technology and
6#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
7#  rights in this software.
8#  This software is distributed under the 3-clause BSD License.
9#  ___________________________________________________________________________
10
11import sys as _sys
12
13if _sys.version_info[0] >= 3:
14    import importlib
15
16
17    def _do_import(pkg_name):
18        importlib.import_module(pkg_name)
19else:
20    def _do_import(pkg_name):
21        __import__(pkg_name, globals(), locals(), [], -1)
22
23#
24# These packages contain plugins that need to be loaded
25#
26_packages = [
27    'pyomo.common',
28    'pyomo.core',
29    'pyomo.opt',
30    'pyomo.dataportal',
31    'pyomo.duality',
32    'pyomo.checker',
33    'pyomo.repn',
34    'pyomo.neos',
35    'pyomo.solvers',
36    'pyomo.gdp',
37    'pyomo.mpec',
38    'pyomo.dae',
39    'pyomo.bilevel',
40    'pyomo.scripting',
41    'pyomo.network',
42]
43#
44#
45# These packages also contain plugins that need to be loaded, but
46# we silently ignore any import errors because these
47# packages are optional and/or under development.
48#
49_optional_packages = {
50    'pyomo.contrib.appsi',
51    'pyomo.contrib.example',
52    'pyomo.contrib.fme',
53    'pyomo.contrib.gdpbb',
54    'pyomo.contrib.gdpopt',
55    'pyomo.contrib.gdp_bounds',
56    'pyomo.contrib.mcpp',
57    'pyomo.contrib.mindtpy',
58    'pyomo.contrib.multistart',
59    'pyomo.contrib.petsc',
60    'pyomo.contrib.preprocessing',
61    'pyomo.contrib.pynumero',
62    'pyomo.contrib.trustregion',
63    'pyomo.contrib.community_detection',
64}
65
66
67def _import_packages():
68    #
69    # Import required packages
70    #
71    for _package in _packages:
72        pname = _package + '.plugins'
73        try:
74            _do_import(pname)
75        except ImportError:
76            exctype, err, tb = _sys.exc_info()  # BUG?
77            import traceback
78            msg = "pyomo.environ failed to import %s:\nOriginal %s: %s\n" \
79                  "Traceback:\n%s" \
80                  % (pname, exctype.__name__, err,
81                     ''.join(traceback.format_tb(tb)),)
82            # clear local variables to remove circular references
83            exctype = err = tb = None
84            # TODO: Should this just log an error and re-raise the
85            # original exception?
86            raise ImportError(msg)
87
88        pkg = _sys.modules[pname]
89        pkg.load()
90    #
91    # Import optional packages
92    #
93    for _package in _optional_packages:
94        pname = _package + '.plugins'
95        try:
96            _do_import(pname)
97        except ImportError:
98            continue
99        pkg = _sys.modules[pname]
100        pkg.load()
101
102
103_import_packages()
104
105#
106# Expose the symbols from pyomo.core
107#
108from pyomo.dataportal import DataPortal
109import pyomo.core.kernel
110from pyomo.common.collections import ComponentMap
111import pyomo.core.base.indexed_component
112import pyomo.core.base.util
113from pyomo.core import expr, base, beta, kernel, plugins
114from pyomo.core.base import util
115
116from pyomo.core import (numvalue, numeric_expr, boolean_value,
117                             current, symbol_map, sympy_tools,
118                             taylor_series, visitor, expr_common, expr_errors,
119                             calculus, native_types,
120                             linear_expression, nonlinear_expression,
121                             land, lor, equivalent, exactly,
122                             atleast, atmost, implies, lnot,
123                             xor, inequality, log, log10, sin, cos, tan, cosh,
124                             sinh, tanh, asin, acos, atan, exp, sqrt, asinh, acosh,
125                             atanh, ceil, floor, Expr_if, differentiate,
126                             taylor_series_expansion, SymbolMap, PyomoObject,
127                             nonpyomo_leaf_types, native_numeric_types,
128                             value, is_constant, is_fixed, is_variable_type,
129                             is_potentially_variable, polynomial_degree,
130                             NumericValue, ZeroConstant, as_boolean, BooleanConstant,
131                             BooleanValue, native_logical_values, minimize,
132                             maximize, PyomoOptions, Expression, CuidLabeler,
133                             CounterLabeler, NumericLabeler,
134                             CNameLabeler, TextLabeler,
135                             AlphaNumericTextLabeler, NameLabeler, ShortNameLabeler,
136                             name, Component, ComponentUID, BuildAction,
137                             BuildCheck, Set, SetOf, simple_set_rule, RangeSet,
138                             Param, Var, VarList, ScalarVar,
139                             BooleanVar, BooleanVarList, ScalarBooleanVar,
140                             logical_expr, simple_constraint_rule,
141                             simple_constraintlist_rule, ConstraintList,
142                             Constraint, LogicalConstraint,
143                             LogicalConstraintList, simple_objective_rule,
144                             simple_objectivelist_rule, Objective,
145                             ObjectiveList, Connector, SOSConstraint,
146                             Piecewise, active_export_suffix_generator,
147                             active_import_suffix_generator, Suffix,
148                             ExternalFunction, symbol_map_from_instance,
149                             Reference, Reals, PositiveReals, NonPositiveReals,
150                             NegativeReals, NonNegativeReals, Integers,
151                             PositiveIntegers, NonPositiveIntegers,
152                             NegativeIntegers, NonNegativeIntegers,
153                             Boolean, Binary, Any, AnyWithNone, EmptySet,
154                             UnitInterval, PercentFraction, RealInterval,
155                             IntegerInterval, display, SortComponents,
156                             TraversalStrategy, Block, ScalarBlock,
157                             active_components, components,
158                             active_components_data, components_data,
159                             global_option, Model, ConcreteModel,
160                             AbstractModel,
161                             ModelComponentFactory, Transformation,
162                             TransformationFactory, instance2dat,
163                             set_options, RealSet, IntegerSet, BooleanSet,
164                             prod, quicksum, sum_product, dot_product,
165                             summation, sequence)
166
167from pyomo.opt import (
168    SolverFactory, SolverManagerFactory, UnknownSolver,
169    TerminationCondition, SolverStatus, check_optimal_termination,
170    assert_optimal_termination
171    )
172from pyomo.core.base.units_container import units
173
174# These APIs are deprecated and should be removed in the near future
175from pyomo.common.deprecation import relocated_module_attribute
176relocated_module_attribute(
177    'SimpleBlock', 'pyomo.core.base.block.SimpleBlock', version='6.0')
178relocated_module_attribute(
179    'SimpleVar', 'pyomo.core.base.var.SimpleVar', version='6.0')
180relocated_module_attribute(
181    'SimpleBooleanVar', 'pyomo.core.base.boolean_var.SimpleBooleanVar',
182    version='6.0'
183)
184del relocated_module_attribute
185