Esempio n. 1
0
can be optimized with the Acro COLIN optimizers.
"""

__all__ = [
    'OptProblem', 'RealOptProblem', 'MixedIntOptProblem', 'response_enum'
]

import os
import sys

from pyutilib.enum import Enum

from pyomo.opt.blackbox.problem_io import BlackBoxOptProblemIOFactory
from pyomo.opt.blackbox.point import MixedIntVars, RealVars

response_enum = Enum("FunctionValue", "FunctionValues", "Gradient", "Hessian",
                     "NonlinearConstraintValues", "Jacobian")


class OptProblem(object):
    """
    A class that defines an application that can be optimized
    by a COLIN optimizer via system calls.
    """
    def __init__(self):
        """
        The constructor.  Derived classes should define the response types.

        By default, only function evaluations are supported in an OptProblem
        instance.
        """
        self.response_types = [response_enum.FunctionValue]
Esempio n. 2
0
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and 
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain 
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________


__all__ = ['ActionManagerError', 'ActionHandle', 'AsynchronousActionManager', 'ActionStatus', 'FailedActionHandle', 'solve_all_instances']

from pyutilib.enum import Enum

from six import itervalues

ActionStatus = Enum('done', 'error', 'queued', 'executing', 'unknown')

def solve_all_instances(solver_manager, solver, instances, **kwds):
    """
    A simple utility to apply a solver to a list of problem instances.
    """
    solver_manager.solve_all(solver, instances, **kwds)

class ActionManagerError(Exception):
    """
    An exception used when an error occurs within an ActionManager.
    """

    def __init__(self,*args,**kargs):
        Exception.__init__(self,*args,**kargs)      #pragma:nocover
Esempio n. 3
0
#  This software is distributed under the BSD License.
#  _________________________________________________________________________

__all__ = ['SolverInformation', 'SolverStatus', 'TerminationCondition']

from pyutilib.enum import Enum
from pyomo.opt.results.container import *


#
# A coarse summary of how the solver terminated.
#
SolverStatus = Enum(
    'ok',                   # Normal termination
    'warning',              # Termination with unusual condition
    'error',                # Terminated internally with error
    'aborted',              # Terminated due to external conditions (e.g. interrupts)
    'unknown'               # An unitialized value
    )

#
# A description of how the solver terminated
#
TerminationCondition = Enum(
    # UNKNOWN
    'unknown',              # An unitialized value
    # OK
    'maxTimeLimit',         # Exceeded maximum time limited allowed by user
    'maxIterations',        # Exceeded maximum number of iterations allowed by user (e.g., simplex iterations)
    'minFunctionValue',     # Found solution smaller than specified function value
    'minStepLength',        # Step length is smaller than specified limit
Esempio n. 4
0
#  ___________________________________________________________________________
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________

__all__ = ['ProblemInformation', 'ProblemSense']

from pyutilib.enum import Enum
from pyomo.opt.results.container import *

ProblemSense = Enum('unknown', 'minimize', 'maximize')


class ProblemInformation(MapContainer):
    def __init__(self):
        MapContainer.__init__(self)
        self.declare('name')
        self.declare('lower_bound', value=float('-inf'))
        self.declare('upper_bound', value=float('inf'))
        self.declare('number_of_objectives', value=1, required=True)
        self.declare('number_of_constraints', value=0)
        self.declare('number_of_variables', value=0)
        self.declare('number_of_binary_variables')
        self.declare('number_of_integer_variables')
        self.declare('number_of_continuous_variables')
        self.declare('number_of_nonzeros')
Esempio n. 5
0
from .diff_with_sympy import differentiate as sympy_diff
from .diff_with_pyomo import reverse_sd, reverse_ad
from pyutilib.enum import Enum
from pyomo.core.kernel.component_map import ComponentMap

Modes = Enum('sympy', 'reverse_symbolic', 'reverse_numeric')


def differentiate(expr, wrt=None, wrt_list=None, mode=Modes.reverse_numeric):
    """Return derivative of expression.

    This function returns the derivative of expr with respect to one or
    more variables.  The type of the return value depends on the
    arguments wrt, wrt_list, and mode. See below for details.

    Parameters
    ----------
    expr: pyomo.core.expr.numeric_expr.ExpressionBase
        The expression to differentiate
    wrt: pyomo.core.base.var._GeneralVarData
        If specified, this function will return the derivative with
        respect to wrt. wrt is normally a _GeneralVarData, but could
        also be a _ParamData. wrt and wrt_list cannot both be specified.
    wrt_list: list of pyomo.core.base.var._GeneralVarData
        If specified, this function will return the derivative with
        respect to each element in wrt_list.  A list will be returned
        where the values are the derivatives with respect to the
        corresponding entry in wrt_list.
    mode: pyomo.core.expr.calculus.derivatives.Modes
        Specifies the method to use for differentiation. Should be one
        of the members of the Modes enum:
Esempio n. 6
0
from pyutilib.math import as_number
from pyomo.opt.results.container import *

default_print_options = Bunch(schema=False,
                              sparse=True,
                              num_solutions=None,
                              ignore_time=False,
                              ignore_defaults=False)

SolutionStatus = Enum(
    'bestSoFar',
    'error',
    'feasible',
    'globallyOptimal',
    'infeasible',
    'locallyOptimal',
    'optimal',
    'other',
    'stoppedByLimit',
    'unbounded',
    'unknown',
    'unsure',
)

try:
    unicode
except NameError:
    basestring = unicode = str

try:
    long
    intlist = (int, long)
Esempio n. 7
0
                                              number_variables,
                                              number_activated_constraints,
                                              number_activated_blocks)

# Some more inforation about this module
__author__ = "John Eslick, Qi Chen, Andrew Lee"

__all__ = ['ProcessBlockData']

useDefault = object()

# Set up logger
_log = logging.getLogger(__name__)

# Enumerate options for material flow basis
MaterialFlowBasis = Enum('molar', 'mass', 'other')


@declare_process_block_class("ProcessBaseBlock")
class ProcessBlockData(_BlockData):
    """
    Base class for most IDAES process models and classes.

    The primary purpose of this class is to create the local config block to
    handle arguments provided by the user when constructing an object and to
    ensure that these arguments are stored in the config block.

    Additionally, this class contains a number of methods common to all IDAES
    classes.
    """
    CONFIG = ConfigBlock("ProcessBlockData", implicit=False)
Esempio n. 8
0
class Sentence(object):
    """A class to support word-based, span-based and structural annotations
    over a sentence.
    """
    # An enumerator for the type of annotations currently supported
    tag_type = Enum('TOKEN', 'SPAN', 'STRUCT')

    def __init__(self, text):
        """Initialize with a list of words or a string representing a sentence.
        """
        # If a string is supplied, it is tokenized using the standard Tokenizer
        if isinstance(text, basestring):
            self.raw = text
            self.tokens = tuple(self._tokenize(text))
        else:
            self.raw = ' '.join(text)
            self.tokens = tuple(text)

        # Keep track of length for quick lookups
        # TODO: maybe create a 'SCALAR' tag type for fields like this
        self.length = len(self.tokens)

        # "annotator_name" -> tag type
        self.annotation_types = {}

        # "name" -> list of annotators
        self.annotators = {}

    def _tokenize(self, text, tokenizer=tokenizer.Tokenizer()):
        """Tokenize the raw text of the sentence in a standard way that should
        be compatible with all potential annotation schemes.
        """
        # Default argument tokenizer will only be initialized once
        # TODO: should this be a classmethod?
        return tokenizer.tokenize(text)

    def untokenize(self):
        """Untokenize the words of the sentence in a standard way and return a
        readable sentence.
        """
        return untokenizer.Untokenizer.untokenize(self.tokens)

    def has_annotation(self, name, annotator=None):
        """Return whether the sentence has a particular annotation, optionally
        specifying the annotator. The default annotator used for any
        annotation is the last one added.
        """
        if annotator is None:
            return hasattr(self, name)
        else:
            return hasattr(self, '_'.join((annotator.lower(), name)))

    def get_annotation(self, name, annotator=None):
        """Return an annotation, optionally specifying a particular
        annotator. The default annotator used for any annotation is the
        last one added.
        """
        if annotator is None:
            return getattr(self, name)
        else:
            return getattr(self, '_'.join((annotator.lower(), name)))

    def _save_annotation(self, annotation, name, annotator, tag_type):
        """Save the annotation.
        """
        tag_name = '_'.join((annotator.lower(), name))
        if hasattr(self, tag_name):
            print "ERROR: Annotation name \'", tag_name, "\' already in use"
            raise Exception
        setattr(self, tag_name, annotation)

        # Redirect the default annotation name to this newest annotation
        setattr(self, name, getattr(self, tag_name))

        # Update metadata
        self.annotation_types[tag_name] = tag_type
        annotator_name = annotator.lower()
        if name not in self.annotators:
            self.annotators[name] = [annotator_name]
        elif annotator_name not in self.annotators[name]:
            self.annotators[name].append(annotator_name)

    def annotate_with(self, *annotator_names):
        """Annotate this sentence with the given annotators.
        """
        annotations.annotate(self, *annotator_names)

    def add_token_tags(self, tags, name, annotator):
        """Add annotations in the form of a list of tags, one for each word.
        The list must be the same size as the list of words. A common use-case
        is the introduction of part-of-speech tags for the sentence.
        """
        if len(tags) != self.length:
            print len(tags), " tags given for ", self.length, " words"
            print "ERROR: Number of tags must equal number of words"
            raise Exception
        else:
            self._save_annotation(tags, name, annotator, self.tag_type.TOKEN)

    def add_span_tags(self, spans, name, annotator):
        """Add span annotations in the form of a dictionary of begin/end
        indices to labels. The begin and end indices should describe a valid
        span in the sentence. A common use-case is the introduction of chunk
        tags for the sentence.
        """
        error = False
        for span in spans.keys():
            if span[0] > span[1]:
                error = True
                print "ERROR: Span ", span, " is badly formed"
                print "Start index greater than end index"
                raise Exception
            elif span[0] < 0 or span[1] >= self.length:
                error = True
                print "ERROR: Span ", span, " is badly formed"
                print "Index beyond sentence indexing boundaries"
                raise Exception

        if not error:
            self._save_annotation(spans, name, annotator, self.tag_type.SPAN)

    def add_structure(self, structure, name, annotator):
        """Add structural annotations in the form of an object which
        contains relations between words in which both the words and relations
        may have tags.
        """
        self._save_annotation(structure, name, annotator, self.tag_type.STRUCT)
Esempio n. 9
0
import copy
import math

import pyutilib.math
from pyutilib.misc import Bunch
from pyutilib.enum import EnumValue, Enum

from six import iterkeys, itervalues, iteritems, advance_iterator, StringIO
from six.moves import xrange
try:
    unicode
except NameError:
    basestring = unicode = str

ScalarType = Enum('int', 'time', 'string', 'float', 'enum', 'undefined')

default_print_options = Bunch(schema=False, ignore_time=False)

strict = False


class UndefinedData(object):
    def __str__(self):
        return "<undefined>"


undefined = UndefinedData()
ignore = UndefinedData()

Esempio n. 10
0
# the intent of this module is to provide functions to interface from
# a PH client to a set of PH solver servers.

import time
import itertools

from pyutilib.enum import Enum

from pyomo.core import *

from six import iteritems, itervalues

InvocationType = Enum('SingleInvocation',
                      'PerBundleInvocation',
                      'PerBundleChainedInvocation',
                      'PerScenarioInvocation',
                      'PerScenarioChainedInvocation',
                      'PerNodeInvocation',
                      'PerNodeChainedInvocation')

class TransmitType(object):

    # Transmit stale variables
    stale       = 0b0000001
    # Transmit fixed variables
    fixed       = 0b0000010
    # Transmit derived variables
    derived     = 0b0000100
    # Transmit blended variables
    blended     = 0b0001000
Esempio n. 11
0
# pyomo - A pyomo.core.PyomoModel object, or a *.py file that defines such an object
# cpxlp - A CPLEX LP file
# nl - AMPL *.nl file
# mps - A free-format MPS file
# mod - AMPL *.mod file
# lpxlp - A LPSolve LP file
# osil - An XML file defined by the COIN-OR OS project: Instance
# FuncDesigner - A FuncDesigner problem
# colin - A COLIN shell command
# colin_optproblem - A Python object that inherits from
#                   pyomo.opt.colin.OptProblem (this can wrap a COLIN shell
#                   command, or provide a runtime optimization problem)
# bar - A Baron input file
# gams - A GAMS input file
#
ProblemFormat = Enum('colin', 'pyomo', 'cpxlp', 'nl', 'mps', 'mod', 'lpxlp',
                     'osil', 'colin_optproblem', 'FuncDesigner', 'bar', 'gams')

#
# osrl - osrl XML file defined by the COIN-OR OS project: Result
# results - A Pyomo results object  (reader define by solver class)
# sol - AMPL *.sol file
# soln - A solver-specific solution file  (reader define by solver class)
# yaml - A Pyomo results file in YAML format
# json - A Pyomo results file in JSON format
#
ResultsFormat = Enum('osrl', 'results', 'sol', 'soln', 'yaml', 'json')


def guess_format(filename):
    formats = {}
    formats['py'] = ProblemFormat.pyomo