Changeset 2194


Ignore:
Timestamp:
Jan 28, 2010 5:52:22 PM (10 years ago)
Author:
wehart
Message:

Merged revisions 2112-2193 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.pyomo/trunk

........

r2122 | wehart | 2010-01-02 23:44:52 -0700 (Sat, 02 Jan 2010) | 2 lines


Adding methods to clear objective/constraint data.

........

r2125 | wehart | 2010-01-03 17:00:33 -0700 (Sun, 03 Jan 2010) | 10 lines


Extended collection of linear terms to ignore identity expressions in a
ProductExpression?.


Misc documentation update in Pyomo.


If there is no rule, then Constraint.construct() does _not_ clear the constraint
set. This had the unfortunate side-effect that user-added constraints are
cleared!

........

r2128 | wehart | 2010-01-03 23:59:03 -0700 (Sun, 03 Jan 2010) | 4 lines


Adding the @set_options decorator, which can be used
to specify options for temporary sets that are defined by
index functions.

........

r2129 | jwatson | 2010-01-04 15:41:02 -0700 (Mon, 04 Jan 2010) | 7 lines


I have changed the setitem interface for the variable base class to allow invocations of the following form:


var[None] = some_value


In cases where the variable Var is a singleton, i.e., non-indexed. This slight abuse of syntax is extremely useful in PH (and anywhere else where we have automated manipulation of variable values), as it avoids the needs for around 15 if-then statements.

........

r2130 | jwatson | 2010-01-04 16:03:42 -0700 (Mon, 04 Jan 2010) | 3 lines


Mirroring previous commit for singleton (non-indexed) parameters, allowing set via param[None].

........

r2133 | wehart | 2010-01-07 08:03:46 -0700 (Thu, 07 Jan 2010) | 2 lines


A draft implementation of expressions for intrinsic functions.

........

r2142 | wehart | 2010-01-11 18:47:07 -0700 (Mon, 11 Jan 2010) | 2 lines


Bug fix. Parameter indices do not have a keys() attribute.

........

r2145 | wehart | 2010-01-11 22:03:47 -0700 (Mon, 11 Jan 2010) | 6 lines


A stab at refactoring the pyomo command-line script, to make
it easier to write related scripts.


Unfortunately, I started doing this with a Jython installation, which
has some issues running PyUtilib?. Thus, this is not well-tested.

........

r2146 | wehart | 2010-01-11 22:19:42 -0700 (Mon, 11 Jan 2010) | 2 lines


Other updates from my laptop...

........

r2147 | wehart | 2010-01-11 22:44:35 -0700 (Mon, 11 Jan 2010) | 2 lines


Fixing bugs

........

r2148 | wehart | 2010-01-11 23:16:37 -0700 (Mon, 11 Jan 2010) | 2 lines


Bug fixes and pyomo command tests.

........

r2159 | wehart | 2010-01-13 14:59:23 -0700 (Wed, 13 Jan 2010) | 2 lines


Updates to reconfigure how files are written out by Pyomo.

........

r2160 | wehart | 2010-01-13 16:09:28 -0700 (Wed, 13 Jan 2010) | 6 lines


Adding the pyomo2lp and pyomo2nl commands.
Setting up the convert.py module, which is used in these commands.
Various fixes to scripting utilities: fixed management of
traceback functions, and reworked the processing of the save_model
option.

........

r2161 | jwatson | 2010-01-13 22:38:15 -0700 (Wed, 13 Jan 2010) | 3 lines


Cut the run-time of the PyomoModel? _load_solution method by 2/3 - it's still taking too long, but even this improvement is noticeable.

........

r2163 | wehart | 2010-01-19 22:08:40 -0700 (Tue, 19 Jan 2010) | 2 lines


Adding diagnostic output.

........

r2166 | jwatson | 2010-01-24 13:51:15 -0700 (Sun, 24 Jan 2010) | 5 lines


Allowing PyomoModel? to load a solver results object with a status equal to "Warning" - the old version threw an exception if the solver status wasn't OK.


The new version does print a warning message indicating that you are loading a solver with a warning, and that the user should take a look.

........

r2170 | wehart | 2010-01-25 16:35:49 -0700 (Mon, 25 Jan 2010) | 6 lines


A rework of the NL/LP file writers. This now uses the
canonical expression representation. I'm pretty sure that the LP writer
is OK, but much less sure about the NL writer. Also, it's clear that we don't
have adequate tests of the writers to ensure that all different types of
models are written correctly.

........

Location:
coopr.pyomo/stable/2.2
Files:
1 deleted
19 edited
15 copied

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/stable/2.2

  • coopr.pyomo/stable/2.2/coopr/pyomo/base/PyomoModel.py

    r2108 r2194  
    5757        self._declarations=[]
    5858        # the _name_varmap and _name_conmap dictionarys are assigned when
    59         # the problem definition
    60         # file is written, i.e., via the write() command. intent
    61         # is to map names that will be coming back from a solver
    62         # into Pyomo variables. Values are of type _VarValue.
     59        # the problem definition file is written, i.e., via the write()
     60        # command. intent is to map names that will be coming back from
     61        # a solver into Pyomo variables. Values are of type _VarValue.
    6362        self._name_varmap={}
    6463        self._name_conmap={}
     
    284283           return True
    285284        elif type(arg) is coopr.opt.SolverResults:
    286            if arg.solver.status != coopr.opt.SolverStatus.ok:
     285           # if the solver status not one of either OK or Warning, then error.
     286           if (arg.solver.status != coopr.opt.SolverStatus.ok) and (arg.solver.status != coopr.opt.SolverStatus.warning):
    287287              raise ValueError, "Cannot load a SolverResults object with bad status: "+str(arg.solver.status)
     288           # but if there is a warning, print out a warning, as someone should probably take a look!
     289           if (arg.solver.status == coopr.opt.SolverStatus.warning):
     290              print "WARNING - Loading a SolverResults object with a warning status"
    288291           if len(arg.solution) > 0:
    289292              self._load_solution(arg.solution(0),symbol_map=arg.symbol_map)
     
    371374        # Load variable data
    372375        #
    373         for name in soln.variable:
     376        for name, entry in soln.variable.items():
    374377          #
    375378          # NOTE: the following is a hack, to handle the ONE_VAR_CONSTANT variable
     
    390393                raise TypeError, "Variable '"+name+"' in model '"+self.name+"' is currently fixed - new value is not expected in solution"
    391394
    392              #print "HERE",soln.variable[name].keys()
    393              for _key in soln.variable[name].keys():
     395             var_value = self._name_varmap[name]
     396             for _key in entry.keys():
    394397                key = _key[0].lower() + _key[1:]
    395398                if key == 'value':
    396                     self._name_varmap[name].value = soln.variable[name].value
     399                    var_value.value = entry.value
    397400                elif not key == 'id':
    398                     setattr(self._name_varmap[name], key, getattr(soln.variable[name], _key))
     401                    setattr(var_value, key, getattr(entry, _key))
    399402        #
    400403        # Load constraint data
    401404        #
    402         for name in soln.constraint:
     405        for name,entry in soln.constraint.items():
    403406             #
    404              # This is a hack
     407             # This is a hack - see above.
    405408             #
    406409             if name.endswith('ONE_VAR_CONSTANT'):
     
    414417             if name[0:2] == 'c_':
    415418                _name = name[4:-1]
    416              #print "HERE",_name
     419
    417420             if _name not in self._name_conmap:
    418421                names=""
     
    421424                raise TypeError, "Constraint '"+name+"' in model '"+self.name+"' is type "+str(type(self._name_conmap[_name]))
    422425
    423              #print "HERE",soln.constraint[name].keys()
    424              for _key in soln.constraint[name].keys():
     426             constraint_data = self._name_conmap[_name]
     427             for _key in entry.keys():
    425428                key = _key[0].lower() + _key[1:]
    426429                if key == 'value':
    427                     self._name_conmap[name].value = soln.constraint[name].value
     430                    constraint_data.value = entry.value
    428431                elif not key == 'id':
    429                     setattr(self._name_conmap[_name], key, getattr(soln.constraint[name], _key))
     432                    setattr(constraint_data, key, getattr(entry, _key))
    430433
    431434    def write(self,filename=None,format=ProblemFormat.cpxlp):
  • coopr.pyomo/stable/2.2/coopr/pyomo/base/constraint.py

    r2099 r2194  
    7979        self.sense = tmpsense
    8080        self.rule = tmprule
    81 
     81        self.trivial=False
     82
     83    def clear(self):
     84        self._data = {}
     85       
    8286    def __call__(self, exception=True):
    8387        if len(self._data) == 0:
     
    236240            self._data[None].name=tmpname
    237241        self.rule = tmprule
     242        self.trivial=False
     243
     244    def clear(self):
     245        self._data = {}
    238246
    239247    def __call__(self, exception=True):
     
    278286           print "Construcing constraint "+self.name
    279287        if self.rule is None:
    280            self._data={}
    281288           return
    282289        #
  • coopr.pyomo/stable/2.2/coopr/pyomo/base/indexed_component.py

    r2046 r2194  
    11
    22from component import Component
    3 from sets import _BaseSet, Set
     3from sets import _BaseSet, Set, set_options
     4
     5def set_options(**kwds):
     6    def decorator(func):
     7        func.set_options = kwds
     8        return func
     9    return decorator
    410
    511
     
    2127                    raise ValueError, "Cannot index a component with a non-set component"
    2228            else:
    23                 self._index=Set(initialize=args[0])
     29                try:
     30                    options = getattr(args[0],'set_options')
     31                    options['initialize'] = args[0]
     32                    self._index=Set(**options)
     33                except:
     34                    self._index=Set(initialize=args[0])
    2435        else:
    2536            tmp = []
     
    3041                    raise ValueError, "Cannot index a component with a non-set component"
    3142                else:
    32                     tmp.append( Set(initialize=arg) )
     43                    try:
     44                        options = getattr(arg,'set_options')
     45                        options['initialize'] = arg
     46                        tmp.append( Set(**options) )
     47                    except:
     48                        tmp.append( Set(initialize=arg) )
    3349            self._index_set=tuple(tmp)
    3450        self._compute_dim()
  • coopr.pyomo/stable/2.2/coopr/pyomo/base/param.py

    r2046 r2194  
    120120            return self._paramval.keys().__iter__()
    121121        else:
    122             return self._index.keys().__iter__()
     122            return self._index.__iter__()
    123123
    124124    def dim(self):
     
    159159           return
    160160
    161         if None in self._index:
     161        if (None in self._index) and (ndx is not None): # allow for None indexing if this is truly a singleton
    162162           raise KeyError, "Cannot set an array value in the simple parameter "+self.name
    163163        if ndx not in self._index:
  • coopr.pyomo/stable/2.2/coopr/pyomo/base/pyomo.py

    r2005 r2194  
    3030  return tmp is not Debug.none and tmp in _debug
    3131
     32def reset_debugging():
     33    global _debug
     34    _debug = []
     35
    3236def set_debugging(*args):
    3337    global _debug
    34     _debug = []
    3538    if args == ():
    3639       args=["all"]
  • coopr.pyomo/stable/2.2/coopr/pyomo/base/sets.py

    r2025 r2194  
    99#  _________________________________________________________________________
    1010
    11 __all__ = ['Set', '_BaseSet', '_SetContainer', '_SetArray', '_ProductSet']
     11__all__ = ['Set', '_BaseSet', '_SetContainer', '_SetArray', '_ProductSet', 'set_options']
    1212
    1313import pyutilib.plugin.core
     
    2020
    2121log = pyutilib.plugin.core.PluginGlobals.env().log
     22
     23#
     24# Decorator for set initializer functions
     25#
     26def set_options(**kwds):
     27    def decorator(func):
     28        func.set_options = kwds
     29        return func
     30    return decorator
     31
    2232
    2333##------------------------------------------------------------------------
     
    606616                self._index=args[0]
    607617            else:
    608                 self._index=Set(initialize=args[0])
     618                try:
     619                    options = getattr(args[0],'set_options')
     620                    options['initialize'] = args[0]
     621                    self._index=Set(**options)
     622                except:
     623                    self._index=Set(initialize=args[0])
    609624        else:
    610625            self._index=None
     
    614629                    tmp.append(arg)
    615630                else:
    616                     tmp.append( Set(initialize=arg) )
     631                    try:
     632                        options = getattr(arg,'set_options')
     633                        options['initialize'] = arg
     634                        tmp.append( Set(**options) )
     635                    except:
     636                        tmp.append( Set(initialize=arg) )
    617637            self._index_set=tuple(tmp)
    618638        _BaseSet.__init__(self,**kwds)
  • coopr.pyomo/stable/2.2/coopr/pyomo/base/var.py

    r2099 r2194  
    206206    def __setitem__(self,ndx,val):
    207207        #print "HERE",ndx,val, self._valid_value(val,False), self.domain
    208         if None in self._index:
     208        if (None in self._index) and (ndx is not None): # allow for None indexing if this is truly a singleton
    209209            raise KeyError, "Cannot set an array value in singleton variable "+self.name
    210210        if ndx not in self._index:
  • coopr.pyomo/stable/2.2/coopr/pyomo/expr/canonical_repn.py

    r2108 r2194  
    99#  _________________________________________________________________________
    1010
    11 __all__ = ['generate_canonical_repn', 'as_expr']
     11__all__ = ['generate_canonical_repn', 'as_expr', 'is_constant', 'is_linear', 'is_quadratic', 'is_nonlinear']
    1212
    1313import pyutilib.plugin.core
    1414from coopr.pyomo.base import IPyomoPresolver, IPyomoPresolveAction, Model
    1515from coopr.pyomo.base import expr
    16 from coopr.pyomo.base.var import _VarValue
     16from coopr.pyomo.base.var import _VarValue, Var
    1717import copy
    1818
     19
     20#
     21# A frozen dictionary that can be hashed.  This dictionary isn't _really_
     22# frozen, but it acts hashable.
     23#
    1924class frozendict(dict):
    2025    __slots__ = ('_hash',)
     
    2429            rval = self._hash = hash(frozenset(self.iteritems()))
    2530        return rval
     31
    2632
    2733
     
    118124
    119125#
    120 # The canonical representation is a dictionary.  Each element is a mapping
    121 # from a term degree to terms in the expression.  If the term degree is
    122 # None, then the map value is simply an expression of terms without a
    123 # specific degree.  Otherwise, the map value is a dictionary of terms to
    124 # their coefficients.  A term is represented as a frozen dictionary that
    125 # maps variable id to variable power.  A constant term is represented
    126 # with None.
    127 #
    128 # Examples:
    129 #  Let x[1] ... x[4] be the first 4 variables, and
    130 #      y[1] ... y[4] be the next 4 variables
    131 #
    132 # 1.3                           {0:{ None :1.3}}
    133 # 3.2*x[1]                      {1:{ {0:1} :3.2}}
    134 # 2*x[1]*y[2] + 3*x[2] + 4      {0:{None:4.0}, 1:{{1:1}:3.0}, 2:{{0:1, 5:1}:2.0}}
    135 # log(y[1]) + x[1]*x[1]         {2:{{0:2}:1.0}, None:log(y[1])}
     126# Temporary canonical expressions
    136127#
    137128temp_const = { 0: {None:0.0} }   
     
    139130temp_nonl = { None: None }   
    140131
    141 
     132#
     133# Internal function for collecting canonical representation, which is
     134# called recursively.
     135#
    142136def collect_canonical_repn(exp, model):
    143     #ans = do_collect_canonical_repn(exp, model)
    144     #exp.pprint()
    145     #print "HERE",ans
    146     #return ans
    147     #def do_collect_canonical_repn(exp, model):
    148 
    149137    global temp_const
    150138    global temp_var
     
    225213
    226214#
    227 # This function generates a canonical representation for a Pyomo expression
     215# Generate a canonical representation of an expression.
     216#
     217# The canonical representation is a dictionary.  Each element is a mapping
     218# from a term degree to terms in the expression.  If the term degree is
     219# None, then the map value is simply an expression of terms without a
     220# specific degree.  Otherwise, the map value is a dictionary of terms to
     221# their coefficients.  A term is represented as a frozen dictionary that
     222# maps variable id to variable power.  A constant term is represented
     223# with None.
     224#
     225# Examples:
     226#  Let x[1] ... x[4] be the first 4 variables, and
     227#      y[1] ... y[4] be the next 4 variables
     228#
     229# 1.3                           {0:{ None :1.3}}
     230# 3.2*x[1]                      {1:{ {0:1} :3.2}}
     231# 2*x[1]*y[2] + 3*x[2] + 4      {0:{None:4.0}, 1:{{1:1}:3.0}, 2:{{0:1, 5:1}:2.0}}
     232# log(y[1]) + x[1]*x[1]         {2:{{0:2}:1.0}, None:log(y[1])}
    228233#
    229234def generate_canonical_repn(expr, model):
    230235    return collect_canonical_repn(expr, model)
    231236
     237def is_constant(repn):
     238    """Return True if the canonical representation is a constant expression"""
     239    return (0 in repn) and (len(repn) == 1)
     240
     241def is_nonlinear(repn):
     242    """Return True if the canonical representation is a nonlinear expression"""
     243    for key in repn:
     244        if not key in [0,1]:
     245            return True
     246    return False
     247
     248def is_linear(repn):
     249    """Return True if the canonical representation is a linear expression."""
     250    return (1 in repn) and not is_nonlinear(repn)
     251
     252def is_quadratic(repn):
     253    """Return True if the canonical representation is a quadratic expression."""
     254    if not 2 in repn:
     255        return False
     256    for key in repn:
     257        if key is None or key > 2:
     258            return False
     259    return True
     260
  • coopr.pyomo/stable/2.2/coopr/pyomo/io/ampl.py

    r2064 r2194  
    5959        Con = model.active_components(Constraint)
    6060        Vars = model.active_components(Var)
    61         Onontriv = model.Onontriv
    62         for obj in Onontriv:
    63                 O = Obj[obj]
    64                 no += len(O._Onz)
    65         Cnontriv = model.Cnontriv
    66         for con in Cnontriv:
    67                 C = Con[con]
    68                 nc += len(C._Cnz)
    69                 for i in C._Cnz:
    70                   if C[i].lower is not None:
    71                            L = self._get_bound(C[i].lower)
    72                            if C[i].upper is not None:
    73                                     U = self._get_bound(C[i].upper)
    74                                     if (L == U):
    75                                              neqn += 1
    76                                     elif L > U:
    77                                              raise ValueError, "Constraint " + str(con) +\
     61        #
     62        # Count number of objectives
     63        #
     64        for obj in Obj:
     65            if Obj[obj].trivial:
     66                continue
     67            for i in Obj[obj]:
     68                if not is_constant(Obj[obj][i].repn):
     69                    no += 1
     70        #
     71        # Count number of constraints
     72        #
     73        for con in Con:
     74            C = Con[con]
     75            if C.trivial:
     76                continue
     77            for i in C:
     78                if is_constant(C[i].repn):
     79                    continue
     80                nc += 1
     81                if C[i].lower is not None:
     82                    L = self._get_bound(C[i].lower) if C[i].lower is not None else -inf
     83                    U = self._get_bound(C[i].upper) if C[i].upper is not None else inf
     84                    if (L == U):
     85                        neqn += 1
     86                elif L > U:
     87                    raise ValueError, "Constraint " + str(con) +\
    7888                                             "[" + str(i) + "]: lower bound = " + str(L) +\
    7989                                             " > upper bound = " + str(U)
    80                                     else:
    81                                              nrange += 1
    82 
     90                elif C[i].lower is not None and C[i].upper is not None:
     91                    nrange += 1
     92        #
    8393        icv = 0
    8494        Ilist = []
     
    96106                  for ndx in Vv:
    97107                           if Vv[ndx].id >= 0:
    98                               L = Vv[ndx].lb()
    99                               U = Vv[ndx].ub()
     108                              L = Vv[ndx].lb
     109                              U = Vv[ndx].ub
    100110                              if L is not None and value(L) == 0 \
    101111                                 and U is not None and value(U) == 1:
     
    167177        nsno = model.statistics.number_of_variables
    168178        ngc = ngo = 0
     179        # Compute # of nonzeros in gradient
    169180        for key in Obj:
    170                 for obj in Obj[key]._linterm:
    171                     ngo += len(Obj[key]._linterm[obj])
     181            if Obj[key].trivial:
     182                continue
     183            for ondx in Obj[key]:
     184                ngo += len(Obj[key][ondx].repn.get(1,{}))
     185        # Compute # of nonzeros in Jacobian
    172186        cu = {}
    173187        for i in xrange(nsno):
    174188                cu[i] = 0
    175         #Con = model.components(Constraint)
    176         for key in Cnontriv:
    177                 C = Con[key]
    178                 for cndx in C._Cnz:
    179                   LT = C._linterm[cndx]
     189        for key in Con:
     190            C = Con[key]
     191            if C.trivial:
     192                continue
     193            for cndx in C:
     194                for d in C[cndx].repn.get(1,{}):
     195                    ngc += 1
     196                    cu[d.keys()[0]] += 1
     197                  #LT = C._linterm[cndx]
    180198                  #print "HERE",LT.keys()
    181                   for i in LT:
     199                  #for i in LT:
    182200                           #print "HERE",i,type(LT[i]),LT[i],type(LT[i][1]),LT[i][1]
    183                            if i != '0':
    184                               ngc += 1
    185                               cu[LT[i][1].id] += 1
     201                           #if i != '0':
     202                              #ngc += 1
     203                              #cu[LT[i][1].id] += 1
    186204        print >>OUTPUT, " " + str(ngc), str(ngo) + "\t# nonzeros in Jacobian, gradients"
    187205        #
     
    197215        #
    198216        nc = 0
    199         for key in Cnontriv:
    200                 for cndx in Con[key]._Cnz:
    201                   print >>OUTPUT, "C" + str(nc) + "\t#" + str(key) + "[" + str(cndx) + "]"
    202                   nc += 1
    203                   #if '0' in Con[key]._linterm[cndx]:
    204                   #  print >>OUTPUT, "n"+str(Con[key]._linterm[cndx]['0'][0])
    205                   #else:
    206                   #  print >>OUTPUT, "n0"
    207                   print >>OUTPUT, "n0"
     217        for key in Con:
     218            if Con[key].trivial:
     219                continue
     220            for ndx in Con[key]:
     221                if is_constant(Con[key][ndx].repn):
     222                    continue
     223                print >>OUTPUT, "C" + str(nc) + "\t#" + str(key) + "[" + str(ndx) + "]"
     224                nc += 1
     225                print >>OUTPUT, "n0"
    208226        #
    209227        # "O" lines
    210228        #
    211229        no = 0
    212         for key in Obj.keys():
    213                 k = 0
    214                 if Obj[key].sense == maximize:
    215                         k = 1
    216                 for cndx in Obj[key]._Onz:
    217                   print >>OUTPUT, "O" + str(no) + " "+str(k)+"\t#" + str(key) + "[" + str(cndx) + "]"
    218                   no += 1
    219                   LT = Obj[key]._linterm[cndx]
    220                   if "0" in LT:
    221                         print >>OUTPUT, "n" + str(LT["0"][0])
    222                   else:
    223                         print >>OUTPUT, "n0"
     230        for key in Obj:
     231            k = 0
     232            if Obj[key].sense == maximize:
     233                k = 1
     234            for ndx in Obj[key]:
     235                if is_constant(Obj[key][ndx].repn):
     236                    continue
     237                print >>OUTPUT, "O" + str(no) + " "+str(k)+"\t#" + str(key) + "[" + str(ndx) + "]"
     238                no += 1
     239                if 0 in Obj[key][ndx].repn:
     240                    print >>OUTPUT, "n" + str(Obj[key][ndx].repn[0][None])
     241                else:
     242                    print >>OUTPUT, "n0"
    224243        #
    225244        # "r" lines
     
    227246        print >>OUTPUT, "r"
    228247        nc=0
    229         for key in Cnontriv:
    230                 C = Con[key]
    231                 for cndx in C._Cnz:
    232                   if '0' in Con[key]._linterm[cndx]:
    233                         offset=Con[key]._linterm[cndx]['0'][0]
    234                   else:
    235                         offset=0
    236                   if C[cndx]._equality:
    237                            print >>OUTPUT, "4", self._get_bound(C[cndx].lower)-offset,
    238                   else:
    239                            if C[cndx].lower is None:
    240                                     if C[cndx].upper is None:
    241                                              print >>OUTPUT,"3",
    242                                     else:
    243                                              print >>OUTPUT,"1", self._get_bound(C[cndx].upper)-offset,
    244                            else:
    245                                     if C[cndx].upper is None:
    246                                              print >>OUTPUT,"2", self._get_bound(C[cndx].lower)-offset,
    247                                     else:
    248                                              print >>OUTPUT,"0",\
    249                                                       self._get_bound(C[cndx].lower)-offset,\
    250                                                       self._get_bound(C[cndx].upper)-offset,
    251                   print >>OUTPUT, " # c"+ str(nc)+"  "+str(key) + "[" + str(cndx) + "]"
    252                   symbol_map["c" + str(nc)] = str(key) + "[" + str(cndx) + "]"
    253                   nc += 1
     248        for key in Con:
     249            if Con[key].trivial:
     250                continue
     251            C = Con[key]
     252            for ndx in C:
     253                if is_constant(C[ndx].repn):
     254                    continue
     255                if 0 in C[ndx].repn:
     256                    offset=C[ndx].repn[0][None]
     257                else:
     258                    offset=0
     259                if C[ndx]._equality:
     260                    print >>OUTPUT, "4", self._get_bound(C[ndx].lower)-offset,
     261                else:
     262                    if C[ndx].lower is None:
     263                        if C[ndx].upper is None:
     264                            print >>OUTPUT,"3",
     265                        else:
     266                            print >>OUTPUT,"1", self._get_bound(C[ndx].upper)-offset,
     267                    else:
     268                        if C[ndx].upper is None:
     269                            print >>OUTPUT,"2", self._get_bound(C[ndx].lower)-offset,
     270                        else:
     271                            print >>OUTPUT,"0",\
     272                                    self._get_bound(C[ndx].lower)-offset,\
     273                                    self._get_bound(C[ndx].upper)-offset,
     274                print >>OUTPUT, " # c"+ str(nc)+"  "+str(key) + "[" + str(ndx) + "]"
     275                symbol_map["c" + str(nc)] = str(key) + "[" + str(ndx) + "]"
     276                nc += 1
    254277        #
    255278        # "b" lines
     
    267290                        continue
    268291                vv = var._varval[ndx]
    269                 L = vv.lb()
    270                 U = vv.ub()
     292                L = vv.lb
     293                U = vv.ub
    271294                if L is not None:
    272295                  Lv = str(value(L))
     
    304327        #
    305328        nc = 0
    306         for key in Cnontriv:
    307                 C = Con[key]
    308                 for cndx in C._Cnz:
    309                   LT = C._linterm[cndx]
    310                   LT_len = len(LT) - ('0' in LT)
    311                   print >>OUTPUT, "J" + str(nc), LT_len
    312                   nc += 1
    313                   for x in LT:
    314                            if x != '0':
    315                               p = LT[x]
    316                               print >>OUTPUT, p[1].id, p[0]
     329        for key in Con:
     330            if Con[key].trivial:
     331                continue
     332            con = Con[key]
     333            for ndx in con:
     334                if is_constant(con[ndx].repn):
     335                    continue
     336                linear = con[ndx].repn.get(1,{})
     337                for id in linear:
     338                    print >>OUTPUT, id, linear[id]
     339                #LT = C._linterm[cndx]
     340                #LT_len = len(LT) - ('0' in LT)
     341                #print >>OUTPUT, "J" + str(nc), LT_len
     342                #nc += 1
     343                #for x in LT:
     344                    #if x != '0':
     345                        #p = LT[x]
     346                        #print >>OUTPUT, p[1].id, p[0]
    317347        #
    318348        # "G" lines
     
    320350        no = 0
    321351        for key in Obj:
    322                 OLT = Obj[key]._linterm
    323                 Ng = 0
    324                 for j in OLT:
    325                         Ng += len(OLT[j])
    326                 print >>OUTPUT, "G" + str(no), Ng
     352            if Obj[key].trivial:
     353                continue
     354            obj = Obj[key]
     355            for ndx in obj:
     356                linear = obj[ndx].repn.get(1,{})
     357                print >>OUTPUT, "G" + str(no), len(linear)
    327358                no += 1
    328                 for j in OLT:
    329                         LT = OLT[j]
    330                         for x in LT:
    331                                 if x != '0':
    332                                    p = LT[x]
    333                                    print >>OUTPUT, p[1].id, p[0]
     359                for id in linear:
     360                    print >>OUTPUT, id, linear[id]
     361                #for j in OLT:
     362                    #LT = OLT[j]
     363                    #for x in LT:
     364                        #if x != '0':
     365                            #p = LT[x]
     366                            #print >>OUTPUT, p[1].id, p[0]
    334367
    335368        return symbol_map
  • coopr.pyomo/stable/2.2/coopr/pyomo/io/cpxlp.py

    r2028 r2194  
    2121from coopr.pyomo.base import *
    2222from coopr.opt.base import problem, AbstractProblemWriter
     23from coopr.pyomo.expr import *
     24import math
    2325
    2426def convert_name(namestr):
     
    8789        return a[0]
    8890
    89     def _print_linterm(self, x, OUTPUT, print_mult=True, cleanup_names=False, print_offset=False):
    90         if len(x) <= 0:
    91            raise ValueError, "ERROR: zero-length value in _print_linterm"
    92         # x maps variable labels to (numeric-coefficient, _VarValue) pairs.
    93         # y will consist of pairs of variable names and their corresponding coefficients,
    94         # basically just stripping out the "_VarValue" component of the input x.       
    95         y = []
    96         for i in x.keys():
    97            output_label = i
    98            if (self._output_prefixes is True) and (isinstance(x[i][1], _VarValue) is True):
    99               parent_model = x[i][1].var.model
    100               if parent_model is None:
    101                  raise ValueError, "Variable="+x[i][1].var.name+" has no parent model defined - required when outputting LP files when _output_prefixes is enabled"
    102               output_label = parent_model.name + "_" + output_label
    103            y.append((output_label,x[i][0]))
    104         y = sorted(y, key=self._collect_key)
    105 #        print "_print_linterm x: ",x
    106 #        print "_print_linterm y: ",y
    107         op = None
    108         offset=0.0
     91    def _print_expr(self, model, x, OUTPUT, print_offset=False):
    10992        max_terms_on_line=5 # this is the line-limit hack
    11093        terms_output = 0
    111         for i in y:
    112                   c = i[1]
    113                   if op != None:
    114                            if c < 0:
    115                                     c = -c
    116                                     op = "-"
    117                            print >>OUTPUT, op,
    118                   if i[0] == "0":
    119                      # a key equal to "0" indicates the offset
    120                      if print_offset:
    121                         print >>OUTPUT, c, "ONE_VAR_CONSTANT"
    122                         terms_output += 1
    123                         op = "+"
    124                      else:
    125                         offset = c
    126                         op = None
    127                   else:
    128                      print >>OUTPUT, c,
    129                      print >>OUTPUT, convert_name(i[0]),
    130                      terms_output += 1
    131                      if terms_output >= max_terms_on_line:
    132                         print >>OUTPUT, ""
    133                         terms_output = 0
    134                      op = "+"
     94        #
     95        # Linear
     96        #
     97        if 1 in x:
     98            keys = x[1].keys()
     99            keys.sort()
     100            for id in keys:
     101                coef = x[1][id]
     102                name = convert_name(model._var[id.keys()[0]].name)
     103                print >>OUTPUT, ('- %f' % math.fabs(coef) if coef < 0.0 else '+ %f' % coef), name,
     104                terms_output += 1
     105                if terms_output >= max_terms_on_line:
     106                    print >>OUTPUT, ""
     107                    print >>OUTPUT, "     ",
     108                    terms_output = 0
     109        #
     110        # Quadratic
     111        #
     112        if 2 in x:
     113            keys = x[2].keys()
     114            keys.sort()
     115            for id in keys:
     116                coef = x[2][id]
     117                print >>OUTPUT, ('- %f' % math.fabs(coef) if coef < 0.0 else '+ %f' % coef),
     118                for var in id:
     119                    name = convert_name(model._var[var].name)
     120                    if id[var] > 1:
     121                        print "%s^%d" % (name,id[var]),
     122                    else:
     123                        print name,
     124                terms_output += 1
     125                if terms_output >= max_terms_on_line:
     126                    print >>OUTPUT, ""
     127                    print >>OUTPUT, "     ",
     128                    terms_output = 0
     129        #
     130        # Constant offset
     131        #
     132        offset=0.0
     133        if 0 in x:
     134            offset = x[0][None]
     135        if print_offset and offset != 0.0:
     136            print >>OUTPUT, ('- %f' % math.fabs(offset) if offset < 0.0 else '+ %f' % offset), "ONE_VAR_CONSTANT"
     137            terms_output += 1
     138        #
     139        # Return constant offset
     140        #
    135141        return offset
    136 
    137     def _print_quadterm(self, x, is_minimizing, OUTPUT):
    138         # the LP format doesn't allow for expression of constant terms in the objective.
    139         # a work-around involves tracking the sum of constant terms in the quadratic
    140         # quadratic terms, and then writing that out with a dummy variable forced equal
    141         # to one.
    142         print >>OUTPUT, ""
    143         for arg in x._args:
    144             if isinstance(arg,expr._ProductExpression):
    145                 # NOTE: we need to handle quadratics defined with the 'pow' term here.
    146                 # WARNING: The following code is very specific to progressive hedging, with
    147                 #          a very specific format assumed. we do need to handle more general
    148                 #          expressions, but we'll worry about that at a latter time.
    149                 blend = arg._numerator[0]()
    150 
    151                 if blend is 1:
    152 
    153                    rho = arg._numerator[1]()
    154 
    155                    pow_expression = arg._numerator[2]
    156                
    157                    base = pow_expression._args[0]
    158                    exponent = pow_expression._args[1]
    159 
    160                    if not isinstance(base,expr._SumExpression):
    161                        raise ValueError, "Quadratic term base must be a _SumExpression"
    162                    if not isinstance(exponent,numvalue.NumericConstant):
    163                        raise ValueError, "Quadratic term exponent must be a NumericConstant"
    164                    variable = base._args[0]
    165                    offset = base._args[1]
    166                    if variable.status is not VarStatus.unused:
    167 
    168                       if is_minimizing is True:
    169                          print >>OUTPUT, " + [" + str(rho) + " " + convert_name(variable.label) + "^2]/2",
    170                       else:
    171                          print >>OUTPUT, " - [" + str(rho) + " " + convert_name(variable.label) + "^2]/2",
    172 
    173                       if (is_minimizing is True):
    174                          if offset.value < 0.0:
    175                             print >>OUTPUT, " + " + str(abs(rho*offset.value)) + " " + convert_name(variable.label),                   
    176                          else:
    177                             print >>OUTPUT, " - " + str(rho*offset.value) + " " + convert_name(variable.label),
    178                       else:
    179                          if offset.value < 0.0:
    180                             print >>OUTPUT, " - " + str(abs(rho*offset.value)) + " " + convert_name(variable.label),                   
    181                          else:
    182                             print >>OUTPUT, " + " + str(rho*offset.value) + " " + convert_name(variable.label),
    183 
    184                       objective_offset = (rho * offset.value * offset.value / 2.0)
    185                       if is_minimizing is True:
    186                          print >>OUTPUT, " + " + str(objective_offset) + " ONE_VAR_CONSTANT"
    187                       else:
    188                          print >>OUTPUT, " - " + str(objective_offset) + " ONE_VAR_CONSTANT"
    189                      
    190             elif isinstance(arg,numvalue.NumericConstant):
    191                 # this is the "0.0" element that forms the initial expression - the
    192                 # quadratic sub-expressions aren't known to the presolve routines.
    193                 # ideally unnecessary - hacked in for now.
    194                 pass
    195 
    196             else:
    197                 print `arg`
    198                 raise ValueError, "Unknown expression sub-type found in quadratic objective expression"   
    199142
    200143    def _print_model_LP(self, model, OUTPUT):
     
    206149        #
    207150        if self._output_objectives is True:
    208            printed_quadterm = False
     151           #printed_quadterm = False
    209152           if len(_obj) == 0:
    210153              raise ValueError, "ERROR: No objectives defined for input model=" + str(model.name) + "; cannot write legal LP file"
     
    215158           print >>OUTPUT, "obj: ",
    216159           obj = _obj[ _obj.keys()[0] ]
    217            for key in obj._linterm:
    218                 if (len(obj._linterm[key]) == 0) or ((len(obj._linterm[key]) == 1) and (obj._linterm[key].keys()[0] == "0")):
     160           for key in obj:
     161                if is_constant(obj[key].repn):
     162                    print "WARNING: ignoring objective %s[%s] which is constant" % (str(obj),str(key))
    219163                    continue
    220                 self._print_linterm(obj._linterm[key], OUTPUT, print_mult=False, cleanup_names=True, print_offset=True)
    221            if obj._quad_subexpr is not None:
    222                self._print_quadterm(obj._quad_subexpr, (_obj[ _obj.keys()[0] ].sense == minimize), OUTPUT)
    223                printed_quadterm = True
     164                if is_nonlinear(obj[key].repn) and not is_quadratic(obj[key].repn):
     165                    raise ValueError, "Cannot write legal LP file.  Objective %s[%s] has nonlinear terms that are not quadratic." % (str(obj),str(key))
     166                self._print_expr(model, obj[key].repn, OUTPUT, print_offset=True)
    224167           print >>OUTPUT, ""
    225168       
    226169        #
    227170        # Constraints
    228         #
    229         # the "Cnontriv" attribute of the model is created on-the-fly, by the _Collect method in problem_utils.py.
    230171        #
    231172        # if there are no non-trivial constraints, you'll end up with an empty constraint block. CPLEX is OK with
     
    243184           
    244185           CON = model.active_components(Constraint)
    245            for key in model.Cnontriv:
     186           have_nontrivial=False
     187           for key in CON:
     188             if CON[key].trivial:
     189                continue
     190             have_nontrivial=True
    246191             i=0
    247192             C = CON[key]
    248              for cndx in C.keys():
     193             for cndx in C:
    249194               if not C[cndx].active:
    250195                    continue
    251196               try:
    252                   # there are conditions, e.g., when fixing variables, under which a constraint block might be empty.
    253                   # ignore these, for both practical reasons and the fact that the CPLEX LP format requires a variable
    254                   # in the constraint body. it is also possible that the body of the constraint consists of only a
    255                   # constant, in which case the "variable" of
    256                   if (len(C._linterm[cndx]) > 1) or ((len(C._linterm[cndx]) == 1) and (C._linterm[cndx].keys()[0] != "0")):
    257                      prefix = ""
    258                      if self._output_prefixes is True:
     197                    # there are conditions, e.g., when fixing variables, under which a constraint block might be empty.
     198                    # ignore these, for both practical reasons and the fact that the CPLEX LP format requires a variable
     199                    # in the constraint body. it is also possible that the body of the constraint consists of only a
     200                    # constant, in which case the "variable" of
     201                    if is_constant(C[cndx].repn):
     202                        print "WARNING: ignoring constraint %s[%s] which is constant" % (str(C),str(cndx))
     203                        continue
     204                    if is_nonlinear(C[cndx].repn):
     205                        raise ValueError, "Cannot write legal LP file.  Constraint %s[%s] has a body with nonlinear terms." % (str(C),str(cndx))
     206                    prefix = ""
     207                    if self._output_prefixes is True:
    259208                        if C.model is None:
    260209                           raise RuntimeError, "Constraint="+C._data[cndx].label+" has no model attribute - no label prefix can be assigned"
    261210                        prefix = C.model.name+"_"
    262211
    263                      if C._data[cndx]._equality:
     212                    if C._data[cndx]._equality:
    264213                         #
    265214                         # Equality constraint
    266215                         #
    267216                         print >>OUTPUT, prefix + "c_e_" + convert_name(C._data[cndx].label) + "_: ",
    268                          offset = self._print_linterm(C._linterm[cndx], OUTPUT, print_mult=False, cleanup_names=True)
     217                         offset = self._print_expr(model, C[cndx].repn, OUTPUT)
    269218                         print >>OUTPUT, "=",
    270219                         self._print_bound(C._data[cndx].lower, OUTPUT, -offset)
    271220                         print >>OUTPUT, ""
    272221                         i=i+1
    273                      else:
     222                    else:
    274223                         #
    275224                         # Inequality constraint
     
    277226                         if C._data[cndx].lower is not None:
    278227                            print >>OUTPUT, prefix + "c_l_" + convert_name(C._data[cndx].label) + "_: ",
    279                             offset = self._print_linterm(C._linterm[cndx], OUTPUT, print_mult=False, cleanup_names=True)
     228                            offset = self._print_expr(model, C[cndx].repn, OUTPUT)
    280229                            print >>OUTPUT, ">=",
    281230                            self._print_bound(C._data[cndx].lower, OUTPUT, -offset)
     
    284233                         if C._data[cndx].upper is not None:
    285234                            print >>OUTPUT, prefix + "c_u_" + convert_name(C._data[cndx].label) + "_: ",
    286                             offset = self._print_linterm(C._linterm[cndx], OUTPUT, print_mult=False, cleanup_names=True)
     235                            offset = self._print_expr(model, C[cndx].repn, OUTPUT)
    287236                            print >>OUTPUT, "<=",
    288237                            self._print_bound(C._data[cndx].upper, OUTPUT, -offset)
     
    292241                  print msg
    293242                  raise ValueError, "ERROR: Failed to output constraint="+C.name+", index="+`cndx`+" in generation of LP format file"
    294            if len(model.Cnontriv) == 0:
     243           if not have_nontrivial:
    295244             print "WARNING: Empty constraint block written in LP format - solver may error"
    296245         
  • coopr.pyomo/stable/2.2/coopr/pyomo/presolve/__init__.py

    r1420 r2194  
    1616import generic_varlabels
    1717#import numeric_varlabels
    18 import collect_linear_terms
     18#import collect_linear_terms
     19import compute_canonical_repn
    1920import simple_presolver
  • coopr.pyomo/stable/2.2/coopr/pyomo/presolve/collect_linear_terms.py

    r1993 r2194  
    6767              ve = v = "0"
    6868              for e in exp._numerator:
     69                 while isinstance(e,expr._IdentityExpression):
     70                        e = e._args[0]
    6971                 if e.fixed_value():
    7072                     # at this point, we have checked that the expression has a fixed value.
     
    178180                if obj._data[ondx].expr is None:
    179181                    raise ValueError, "No expression has been defined for objective %s" % str(key)
    180                 t = obj._linterm[ondx] = self._Collect3(obj._data[ondx].expr)
     182                try:
     183                    t = obj._linterm[ondx] = self._Collect3(obj._data[ondx].expr)
     184                except Exception, e:
     185                    print "Error collecting linear terms for constraint %s (index %s)" % (str(key), str(ondx))
     186                    raise e
    181187                lt = len(t)
    182188                if lt > 0:
  • coopr.pyomo/stable/2.2/coopr/pyomo/presolve/simple_presolver.py

    r1768 r2194  
    4949        self._initialize()
    5050        tmp = self.actions.service(action)
     51        if tmp is None:
     52            raise ValueError, "Cannot activate unknown action %s" % action
    5153        self.active_actions.add(action)
    5254        if rank is None:
  • coopr.pyomo/stable/2.2/coopr/pyomo/tests/examples/test_pyomo.py

    r2049 r2194  
    1414import pyutilib.subprocess
    1515import pyutilib.th
    16 import coopr.pyomo.main_script as main
     16import coopr.pyomo.scripting.pyomo as main
    1717import StringIO
    1818from pyutilib.misc import setup_redirect, reset_redirect
     
    4141        setup_redirect(OUTPUT)
    4242        os.chdir(currdir)
    43         main.run(list(args))
     43        output = main.run(list(args))
    4444        reset_redirect()
    4545        if not 'file' in kwds:
    4646            return OUTPUT.getvalue()
     47        return output
    4748       
    4849    def test1(self):
     
    7677        self.failUnlessFileEqualsBaseline(currdir+"test6.out", currdir+"test6.txt")
    7778
     79    def Xtest7(self):
     80        """Run pyomo with help option"""
     81        self.pyomo('--help', file=currdir+'test7.out')
     82        self.failUnlessFileEqualsBaseline(currdir+"test7.out", currdir+"test7.txt")
     83
     84    def test8(self):
     85        """Run pyomo with --instance-only option"""
     86        output = self.pyomo('--instance-only pmedian.py pmedian.dat', file=currdir+'test8.out')
     87        self.failUnlessEqual(type(output.instance), Model)
     88
     89    def test9(self):
     90        """Run pyomo with --disable-gc option"""
     91        output = self.pyomo('--disable-gc pmedian.py pmedian.dat', file=currdir+'test9.out')
     92
     93    def test10(self):
     94        """Run pyomo with --verbose option"""
     95        def filter(line):
     96            #print "HERE",line
     97            return line.startswith("Writing")
     98        self.pyomo('-v pmedian.py pmedian.dat', file=currdir+'test10.out')
     99        self.failUnlessFileEqualsBaseline(currdir+"test10.out", currdir+"test10.txt", filter)
     100
     101    def test11(self):
     102        """Run pyomo with --debug=generate option"""
     103        self.pyomo('--debug=generate pmedian.py pmedian.dat', file=currdir+'test11.out')
     104        self.failUnlessFileEqualsBaseline(currdir+"test11.out", currdir+"test11.txt")
     105
     106    def test12(self):
     107        """Run pyomo with --logfile option"""
     108        self.pyomo('--debug=errors --logfile=%s pmedian.py pmedian.dat' % (currdir+'test12.log'), file=currdir+'test12.out')
     109        self.failUnlessFileEqualsBaseline(currdir+"test12.log", currdir+"test12.txt")
     110
    78111
    79112if __name__ == "__main__":
  • coopr.pyomo/stable/2.2/coopr/pyomo/tests/expr/test_canonical.py

    r2056 r2194  
    4545        #PluginGlobals.pprint()
    4646        self.plugin = SimplePresolver()
    47         self.plugin.deactivate_action("collect_linear_terms")
     47        self.plugin.deactivate_action("compute_canonical_repn")
    4848
    4949    def tearDown(self):
     
    5151           os.unlink("unknown.lp")
    5252        pyutilib.services.TempfileManager.clear_tempfiles()
    53         self.plugin.activate_action("collect_linear_terms")
     53        self.plugin.activate_action("compute_canonical_repn")
    5454
    5555    def test_expr1(self):
  • coopr.pyomo/stable/2.2/coopr/pyomo/tests/unit/test_param.py

    r2046 r2194  
    376376        self.failUnlessEqual(self.instance.B[2,False],-4)
    377377
     378    def test_index2(self):
     379        self.model.A = Set(initialize=range(0,4))
     380        @set_options(dimen=3)
     381        def B_index(model):
     382            return [(i,2*i,i*i) for i in model.A if i%2 == 0]
     383        def B_init(i,ii,iii,j,model):
     384            if j:
     385                return 2+i
     386            return -(2+i)
     387        self.model.B = Param(B_index, [True,False], initialize=B_init)
     388        self.instance = self.model.create()
     389        #self.instance.pprint()
     390        self.failUnlessEqual(set(self.instance.B.keys()),set([(0,0,0,True),(2,4,4,True),(0,0,0,False),(2,4,4,False)]))
     391        self.failUnlessEqual(self.instance.B[0,0,0,True],2)
     392        self.failUnlessEqual(self.instance.B[0,0,0,False],-2)
     393        self.failUnlessEqual(self.instance.B[2,4,4,True],4)
     394        self.failUnlessEqual(self.instance.B[2,4,4,False],-4)
     395
     396    def test_index3(self):
     397        self.model.A = Set(initialize=range(0,4))
     398        def B_index(model):
     399            return [(i,2*i,i*i) for i in model.A if i%2 == 0]
     400        def B_init(i,ii,iii,j,model):
     401            if j:
     402                return 2+i
     403            return -(2+i)
     404        self.model.B = Param(B_index, [True,False], initialize=B_init)
     405        try:
     406            self.instance = self.model.create()
     407            self.fail("Expected ValueError because B_index returns a tuple")
     408        except ValueError:
     409            pass
     410
     411    def test_index4(self):
     412        self.model.A = Set(initialize=range(0,4))
     413        @set_options(within=Integers)
     414        def B_index(model):
     415            return [i/2.0 for i in model.A]
     416        def B_init(i,j,model):
     417            if j:
     418                return 2+i
     419            return -(2+i)
     420        self.model.B = Param(B_index, [True,False], initialize=B_init)
     421        try:
     422            self.instance = self.model.create()
     423            self.fail("Expected ValueError because B_index returns invalid index values")
     424        except ValueError:
     425            pass
     426
    378427    def test_dimen1(self):
    379428        model=Model()
  • coopr.pyomo/stable/2.2/coopr/pyomo/tests/unit/test_set.py

    r2025 r2194  
    14131413        self.failUnlessEqual(self.instance.B[0,True].data(),set([0,1]))
    14141414        self.failUnlessEqual(self.instance.B[2,True].data(),set([2,3]))
     1415
     1416    def test_initialize7(self):
     1417        self.model.A = Set(initialize=range(0,3))
     1418        @set_options(dimen=3)
     1419        def B_index(model):
     1420            return [(i,i+1,i*i) for i in model.A]
     1421        def B_init(i,ii,iii,j,model):
     1422            if j:
     1423                return range(i,2+i)
     1424            return []
     1425        self.model.B = Set(B_index, [True,False], initialize=B_init)
     1426        self.instance = self.model.create()
     1427        #self.instance.pprint()
     1428        self.failUnlessEqual(set(self.instance.B.keys()),set([(0,1,0,True),(1,2,1,True),(2,3,4,True),(0,1,0,False),(1,2,1,False),(2,3,4,False)]))
     1429        self.failUnlessEqual(self.instance.B[0,1,0,True].data(),set([0,1]))
     1430        self.failUnlessEqual(self.instance.B[2,3,4,True].data(),set([2,3]))
     1431
     1432    def test_initialize8(self):
     1433        self.model.A = Set(initialize=range(0,3))
     1434        def B_index(model):
     1435            return [(i,i+1,i*i) for i in model.A]
     1436        def B_init(i,ii,iii,j,model):
     1437            if j:
     1438                return range(i,2+i)
     1439            return []
     1440        self.model.B = Set(B_index, [True,False], initialize=B_init)
     1441        try:
     1442            self.instance = self.model.create()
     1443            self.fail("Expected ValueError because B_index returns a tuple")
     1444        except ValueError:
     1445            pass
     1446
     1447    def test_initialize9(self):
     1448        self.model.A = Set(initialize=range(0,3))
     1449        @set_options(domain=Integers)
     1450        def B_index(model):
     1451            return [i/2.0 for i in model.A]
     1452        def B_init(i,j,model):
     1453            if j:
     1454                return range(int(i),int(2+i))
     1455            return []
     1456        self.model.B = Set(B_index, [True,False], initialize=B_init)
     1457        self.instance = self.model.create()
     1458        try:
     1459            self.instance = self.model.create()
     1460            self.fail("Expected ValueError because B_index returns invalid set values")
     1461        except AssertionError:
     1462            pass
    14151463
    14161464    def test_dimen1(self):
  • coopr.pyomo/stable/2.2/scripts/pyomo

    r2004 r2194  
    1717import traceback
    1818import sys
    19 if sys.version_info[0:2] < (2,4):
     19if sys.version_info[0:2] < (2,5,3):
    2020   print ""
    21    print "ERROR: Pyomo requires Python 2.4 or newer"
     21   print "ERROR: Pyomo requires Python 2.5.3 or newer"
    2222   sys.exit(1)
    2323from os.path import abspath, dirname
    2424import os
    25 import pyutilib.common
    2625sys.path.insert(0, dirname(dirname(abspath(__file__))))
    2726sys.path.append(".")
     
    4847    curr = os.path.dirname(curr)
    4948
    50 import coopr.pyomo.main_script
     49import coopr.pyomo.scripting.pyomo
    5150
    5251try:
    53     coopr.pyomo.main_script.run()
     52    coopr.pyomo.scripting.pyomo.run(args=sys.argv[1:])
    5453except SystemExit:
    5554    pass
    56 #except ValueError, str:
    57     #print str
    58 #except IOError, str:
    59     #print str
    60 #except pyutilib.common.ApplicationError, str:
    61     #print str
    62 #except RuntimeError:
    63     #pass
    64 #except:
    65    #print "Encountered unhandled exception"
    66    #traceback.print_exc()
Note: See TracChangeset for help on using the changeset viewer.