Changeset 3046


Ignore:
Timestamp:
Sep 26, 2010 12:20:56 PM (11 years ago)
Author:
wehart
Message:

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

........

r3007 | wehart | 2010-09-08 18:00:36 -0500 (Wed, 08 Sep 2010) | 2 lines


Eliminating tab character.

........

r3008 | wehart | 2010-09-08 18:12:02 -0500 (Wed, 08 Sep 2010) | 2 lines


Adding a rough draft of an ODBC plugin.

........

r3009 | wehart | 2010-09-08 21:32:18 -0500 (Wed, 08 Sep 2010) | 2 lines


Setting up plugins for database interfaces.

........

r3010 | wehart | 2010-09-08 21:32:58 -0500 (Wed, 08 Sep 2010) | 2 lines


Renaming the DB interface classes.

........

r3011 | wehart | 2010-09-09 12:16:41 -0500 (Thu, 09 Sep 2010) | 1 line


Adding preliminary data for a database connection.

........

r3012 | wehart | 2010-09-09 12:17:23 -0500 (Thu, 09 Sep 2010) | 1 line


Updates and bug fixes while testing the database connectivity with pyodbc.

........

r3013 | jwatson | 2010-09-15 12:19:57 -0500 (Wed, 15 Sep 2010) | 3 lines


Improving an error diagnostic message in the LP writer.

........

r3015 | wehart | 2010-09-16 01:26:57 -0500 (Thu, 16 Sep 2010) | 2 lines


Baseline update due to diagnostic change that JP added.

........

r3016 | wehart | 2010-09-16 13:56:50 -0500 (Thu, 16 Sep 2010) | 2 lines


Bug fix: allow set tuple templates with multiple wildcard dimension.

........

r3017 | wehart | 2010-09-16 17:27:20 -0500 (Thu, 16 Sep 2010) | 3 lines


Allowing for indexing with quoted strings. Including a new test
for this case.

........

r3019 | wehart | 2010-09-18 11:34:45 -0500 (Sat, 18 Sep 2010) | 5 lines


Changing *.dat parser to recognize filenames that start with
<string>:


Adding ability to specify slices of parameter indices.

........

r3020 | wehart | 2010-09-19 01:34:40 -0500 (Sun, 19 Sep 2010) | 4 lines


Changes to the processing of header information to omit headers that are not
specified in a mapped_header array. This allows subsets of a specified
relational table to be selected by an 'import' command.

........

r3021 | jwatson | 2010-09-19 20:44:44 -0500 (Sun, 19 Sep 2010) | 1 line


Various performance improvements to identify_vars.py.

........

r3022 | jwatson | 2010-09-19 20:48:04 -0500 (Sun, 19 Sep 2010) | 1 line


Various performance improvements to generic_varlabels.py

........

r3023 | jwatson | 2010-09-19 20:53:09 -0500 (Sun, 19 Sep 2010) | 1 line


Deleting collect_linear_terms.py - deprecated module

........

r3024 | wehart | 2010-09-19 21:01:55 -0500 (Sun, 19 Sep 2010) | 2 lines


Bug fix with error checking.

........

r3025 | wehart | 2010-09-19 21:07:37 -0500 (Sun, 19 Sep 2010) | 2 lines


Creating a 'table' option, which creates a query automatically.

........

r3026 | wehart | 2010-09-19 21:15:25 -0500 (Sun, 19 Sep 2010) | 2 lines


Adding error handling when 'using' option specifies a bad value.

........

r3027 | wehart | 2010-09-19 23:40:37 -0500 (Sun, 19 Sep 2010) | 7 lines


Explicitly adding a DB interface class for pyodbc. There are many
different ODBC drivers, and I wanted to provide a modest level of automation
for this interface.


Got the ABCD8.py example working from the Pyomo book, which loads
data from an Excel spreadsheet using pyodbc.

........

r3028 | wehart | 2010-09-19 23:40:47 -0500 (Sun, 19 Sep 2010) | 2 lines


Adding a 'driver' option.

........

r3029 | wehart | 2010-09-19 23:51:05 -0500 (Sun, 19 Sep 2010) | 2 lines


Bug fixes to allow connection strings to work with Excel data files.

........

r3030 | wehart | 2010-09-20 00:27:46 -0500 (Mon, 20 Sep 2010) | 2 lines


Removing tab characters.

........

r3037 | jwatson | 2010-09-21 15:42:58 -0500 (Tue, 21 Sep 2010) | 3 lines


Improving the error message output associated with set initialization.

........

r3041 | wehart | 2010-09-23 22:10:03 -0500 (Thu, 23 Sep 2010) | 7 lines


Resolving #4057. If a function with <name>_rule is
in the same scope as a model component, then the component.rule attribute
is set to the function.


The parameter and set classes are specialized to use this self.rule
attribute when self.initialize is None.

........

r3042 | wehart | 2010-09-23 22:44:21 -0500 (Thu, 23 Sep 2010) | 5 lines


Bug fix in parameter initialization with rules.


Creating test case where set/param/obj/con data is
initialized with rules.

........

Location:
coopr.pyomo/stable
Files:
1 deleted
18 edited
4 copied

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/stable

  • coopr.pyomo/stable/coopr/pyomo/base/block.py

    r2911 r3046  
    5656    def __init__(self, *args, **kwargs):
    5757        """Constructor"""
    58         if 'ctype' in kwargs:
    59             tkwargs = {'ctype':kwargs['ctype']}
    60         else:
    61             tkwargs = {'ctype':Block}
     58        if 'ctype' in kwargs:
     59            tkwargs = {'ctype':kwargs['ctype']}
     60        else:
     61            tkwargs = {'ctype':Block}
    6262        IndexedComponent.__init__(self, *args, **tkwargs)
    63         #
     63        #
    6464        self.name=kwargs.get('name', 'unknown')
    6565        self._defer_construction=True
    66         self._parent_block = None
     66        self._parent_block = None
    6767        #
    6868        # Define component dictionary: component type -> instance
     
    8383
    8484    def concrete_mode(self):
    85         """Configure block to immediately construct components"""
     85        """Configure block to immediately construct components"""
    8686        self._defer_construction=False
    8787
    8888    def symbolic_mode(self):
    89         """Configure block to defer construction of components"""
     89        """Configure block to defer construction of components"""
    9090        self._defer_construction=True
    9191
    9292    def components(self, ctype=None):
    93         """
    94         Return information about the block components.  If ctype is None, return the dictionary
    95         that maps {component type -> {name -> instance}}.  Otherwise, return the dictionary
    96         that maps {name -> instance} for the specified component type.
    97         """
     93        """
     94        Return information about the block components.  If ctype is None, return the dictionary
     95        that maps {component type -> {name -> instance}}.  Otherwise, return the dictionary
     96        that maps {name -> instance} for the specified component type.
     97        """
    9898        if ctype is None:
    9999            return self._component
     
    103103
    104104    def active_components(self, _ctype=None):
    105         """
    106         Returns the active components in this block.  If _ctype is None, return the
    107         dictionary that maps {component type -> {name -> instance}}.  Otherwise, return
    108         the dictionary that maps {name -> instance} for the specified component type.
    109         """
     105        """
     106        Returns the active components in this block.  If _ctype is None, return the
     107        dictionary that maps {component type -> {name -> instance}}.  Otherwise, return
     108        the dictionary that maps {name -> instance} for the specified component type.
     109        """
    110110        tmp = {}
    111111        if _ctype is None:
     
    159159
    160160    def _clear_attribute(self,name):
    161         """
     161        """
    162162        Cleanup the pre-existing model attribute
    163         """
     163        """
    164164        if name in self._declarations:
    165165            self.__dict__[name]=None
    166             del self._component[ self._declarations[name].type() ][name]
     166            del self._component[ self._declarations[name].type() ][name]
    167167            del self._declarations[name]
    168168
     
    203203#                curr = self._parent_block
    204204
     205        frame = sys._getframe(2)
     206        locals_ = frame.f_locals
     207        if getattr(val,'rule',None) is None and val.name+'_rule' in locals_:
     208            val.rule = locals_[val.name+'_rule']
    205209        if not self._defer_construction:
    206210            val.construct(None)
     
    289293        else:
    290294            print >>ostream, "Model "+self.name
    291         #
     295        #
    292296        print >>ostream, ""
    293297        print >>ostream, "  Variables:"
     
    298302            for ndx in VAR:
    299303                VAR[ndx].display(prefix="    ",ostream=ostream)
    300         #
     304        #
    301305        print >>ostream, ""
    302306        print >>ostream, "  Objectives:"
     
    308312                OBJ[ndx].display(prefix="    ",ostream=ostream)
    309313        print >>ostream, ""
    310         #
     314        #
    311315        CON = self.active_components(Constraint)
    312316        print >>ostream, "  Constraints:"
  • coopr.pyomo/stable/coopr/pyomo/base/param.py

    r2922 r3046  
    7171        IndexedComponent.__init__(self, *args, **tkwd)
    7272
    73         self._initialize = kwd.pop('initialize', {} )
     73        self._initialize = kwd.pop('initialize', None )
    7474        self._initialize = kwd.pop('rule', self._initialize )
    7575        self._validate   = kwd.pop('validate', None )
     
    8989            msg = "Unknown keyword to Param constructor: '%s'"
    9090            raise ValueError, msg % kwd.values()[0]
     91
     92
     93    def initialize(self, data):
     94        self._initialize = data
    9195
    9296
     
    207211            pass
    208212
     213        if self._initialize is None:
     214            self._initialize = getattr(self,'rule',{})
    209215        #
    210216        # Construct using the initial data or the data loaded from an
  • coopr.pyomo/stable/coopr/pyomo/base/sets.py

    r2921 r3046  
    181181        if self.domain is not None and element not in self.domain:
    182182           if use_exception:
    183               raise ValueError, "Value "+str(element)+" is not valid for set "+self.name+", because it is not within set "+self.domain.name
     183              raise ValueError, "The value="+str(element)+" is not valid for set="+self.name+", because it is not within the domain="+self.domain.name
    184184           return False
    185185        if self.validate is not None and not self.validate(element,self.model):
    186186           if use_exception:
    187               raise ValueError, "Value "+str(element)+" violates the validation rule of set "+self.name
     187              raise ValueError, "The value="+str(element)+" violates the validation rule of set="+self.name
    188188           return False
    189189        if self.dimen > 1 and type(element) is not tuple:
    190190           if use_exception:
    191               raise ValueError, "Value "+str(element)+" is not a tuple for set "+self.name+", which has dimen "+str(self.dimen)
     191              raise ValueError, "The value="+str(element)+" is not a tuple for set="+self.name+", which has dimen="+str(self.dimen)
    192192           return False
    193193        elif self.dimen == 1 and type(element) is tuple:
    194194           if use_exception:
    195               raise ValueError, "Value "+str(element)+" is a tuple for set "+self.name+", which has dimen "+str(self.dimen)
     195              raise ValueError, "The value="+str(element)+" is a tuple for set="+self.name+", which has dimen="+str(self.dimen)
    196196           return False
    197197        elif type(element) is tuple and len(element) != self.dimen:
    198198           if use_exception:
    199               raise ValueError, "Value "+str(element)+" does not have dimension "+str(self.dimen)+", which is needed for set "+self.name
     199              raise ValueError, "The value="+str(element)+" does not have dimension="+str(self.dimen)+", which is needed for set="+self.name
    200200           return False
    201201        return True
     
    227227            return
    228228        self._constructed=True
     229
     230        if self.initialize is None:
     231            self.initialize = getattr(self,'rule',None)
    229232        #
    230233        # Construct using the values list
     
    837840        if self.virtual:                                #pragma:nocover
    838841           raise TypeError, "It doesn't make sense to create a virtual set array"
     842
     843        if self.initialize is None:
     844            self.initialize = getattr(self,'rule',None)
    839845        #
    840846        # Construct using the values list
     
    974980            return
    975981        self._constructed=True
     982
     983        if self.initialize is None:
     984            self.initialize = getattr(self,'rule',None)
     985
    976986        if self.virtual:
    977987           if len(self.set_tuple) == 0:
  • coopr.pyomo/stable/coopr/pyomo/data/TableData.py

    r2763 r3046  
    8282                if header in self.options.param_name:
    8383                    mapped_headers.append(self.options.param_name[header])
    84                 else:
    85                     mapped_headers.append(header)
    8684        #print "X", mapped_headers, self.options.param_name, \
    8785        #           self.options.index, self.options.format
     
    112110
    113111            self._info = ["set",self.options.set, ":"]
    114             self._info = self._info + list(mapped_headers[1:])
     112            self._info = self._info + list(headers[1:])
    115113            self._info = self._info + [":="]
    116114            for row in rows:
     
    119117        elif self.options.format == 'transposed_array':
    120118            self._info = ["param",self.options.param,"(tr)",":"]              \
    121                        + mapped_headers[1:]
     119                       + headers[1:]
    122120            self._info.append(":=")
    123121            for row in rows:
     
    125123
    126124        elif self.options.format == 'array':
    127             self._info = ["param",self.options.param,":"] + mapped_headers[1:]
     125            self._info = ["param",self.options.param,":"] + headers[1:]
    128126            self._info.append(":=")
    129127            for row in rows:
     
    155153            msg = "Unknown parameter format: '%s'"
    156154            raise ValueError, msg % self.options.format
     155        #print "FINAL",self._info
  • coopr.pyomo/stable/coopr/pyomo/data/__init__.py

    r2304 r3046  
    1414import parse_datacmds
    1515import csv_table
    16 #import db
     16import db_table
  • coopr.pyomo/stable/coopr/pyomo/data/csv_table.py

    r2592 r3046  
    3535    def read(self):
    3636        tmp=[]
    37         for tokens in csv.reader(self.INPUT):
     37        for tokens in csv.reader(self.INPUT):
    3838            if tokens != ['']:
    3939                tmp.append(tokens)
  • coopr.pyomo/stable/coopr/pyomo/data/parse_datacmds.py

    r3004 r3046  
    5555    "WORD",
    5656    "WORDWITHINDEX",
     57    "WORDWITHSQUOTEDINDEX",
    5758    "STRING",
    5859    "QUOTEDSTRING",
     
    9394    return t
    9495
     96def t_WORDWITHSQUOTEDINDEX(t):
     97    r'[a-zA-Z_0-9][a-zA-Z_0-9\.\-]*\[[\'\"a-zA-Z_0-9\.\-,\* ]*\]'
     98    t.type = reserved.get(t.value,'WORDWITHSQUOTEDINDEX')    # Check for reserved words
     99    return t
     100
    95101def t_WORD(t):
    96102    r'[a-zA-Z_0-9][a-zA-Z_0-9\.+\-]*'
     
    109115
    110116def t_FILENAME(t):
    111     r'[a-zA-Z_0-9\./\\]*(/|\\)[a-zA-Z_0-9\-\./\\]*'
     117    r'[a-zA-Z]+:[a-zA-Z_0-9\./\\]*(/|\\)[a-zA-Z_0-9\-\./\\]*|[a-zA-Z_0-9\./\\]*(/|\\)[a-zA-Z_0-9\-\./\\]*'
    112118    t.type = reserved.get(t.value,'FILENAME')    # Check for reserved words
    113119    return t
     
    184190                 | SET WORDWITHINDEX COLONEQ setdecl SEMICOLON
    185191                 | SET WORDWITHINDEX COLONEQ SEMICOLON
     192                 | SET WORDWITHSQUOTEDINDEX COLONEQ setdecl SEMICOLON
     193                 | SET WORDWITHSQUOTEDINDEX COLONEQ SEMICOLON
    186194                 | PARAM items COLONEQ paramdecl SEMICOLON
    187195                 | IMPORT importdecl SEMICOLON
     
    300308        p[0] = [p[2]]
    301309
    302 def p_template(p):
    303     '''template : LPAREN WORD index_list RPAREN
     310def p_set_template(p):
     311    '''set_template : LPAREN WORD index_list RPAREN
    304312                | LPAREN ASTERISK index_list RPAREN
    305313                | LPAREN WORD RPAREN
     
    311319        p[0] = p[1]+p[2]+p[3]
    312320
     321def p_param_template(p):
     322    '''param_template : LBRACKET WORD index_list RBRACKET
     323                | LBRACKET ASTERISK index_list RBRACKET
     324                | LBRACKET WORD RBRACKET
     325                | LBRACKET ASTERISK RBRACKET
     326    '''
     327    if len(p) == 5:
     328        p[0] = p[1]+",".join([p[2]]+p[3])+p[4]
     329    else:
     330        p[0] = p[1]+p[2]+p[3]
     331
    313332def p_items(p):
    314333    '''items : item items
     
    322341    '''item : WORD
    323342            | WORDWITHINDEX
     343            | WORDWITHSQUOTEDINDEX
    324344            | NONWORD
    325345            | STRING
     
    335355            | RPAREN
    336356            | ASTERISK
    337             | template
     357            | set_template
     358            | param_template
    338359    '''
    339360    if p[1][0] == '"' and len(p[1]) > 2 and p[1][-1] == '"' and not ' ' in p[1]:
     
    347368                | QUOTEDSTRING
    348369                | FILENAME
    349                 | WORD COLON FILENAME
    350370    '''
    351371    if len(p) == 2:
  • coopr.pyomo/stable/coopr/pyomo/data/process_data.py

    r2585 r3046  
    1717from coopr.pyomo.base import pyomo
    1818from coopr.pyomo.base.plugin import *
     19import pyutilib.common
    1920
    2021global Lineno
     
    111112        ans=[]
    112113        i=0
    113         flag=type(cmd[0]) is tuple
    114114        tmp=None
    115         ndx=None
     115        ndx=[]
    116116        while i<len(cmd):
    117117          if type(cmd[i]) is not tuple:
    118             if flag:
     118            if len(ndx) > 0:
    119119               #if type(cmd[i]) is not tuple:
    120120               #   raise ValueError, "Problem initializing set="+sname+" with input data="+str(cmd)+" - first element was interpreted as a tuple, but element="+str(i)+" is of type="+str(type(cmd[i]))+"; types must be consistent"
    121121               tmpval=tmp
    122                tmpval[ndx] = _data_eval([cmd[i]])[0]
     122               for kk in range(len(ndx)):
     123                    if i == len(cmd):
     124                        raise IOError, "Expected another set value to flush out a tuple pattern!"
     125                    tmpval[ndx[kk]] = _data_eval([cmd[i]])[0]
     126                    i += 1
    123127               #
    124128               # WEH - I'm not sure what the next two lines are for
     
    128132               #   tmpval = util.tuplize(tmpval,d,sname)
    129133               ans.append(tuple(tmpval))
     134               continue
    130135            else:
    131136               ans.append(cmd[i])
     
    135140            j = i
    136141            tmp=list(cmd[j])
    137             ndx=tmp.index("*")
     142            ndx=[]
     143            for kk in range(len(tmp)):
     144                if tmp[kk] == '*':
     145                    ndx.append(kk)
     146            #print 'NDX',ndx
    138147          i += 1
    139148        if pyomo.debug("reader"):               #pragma:nocover
     
    146155        Called by _process_data to process data for a Parameter declaration
    147156        """
     157        #print 'x',cmd,_data
    148158        if pyomo.debug("reader"):               #pragma:nocover
    149159           print "DEBUG: _process_param(start)",cmd
     
    177187              if pyomo.debug("reader"):             #pragma:nocover
    178188                 print "DEBUG: _process_param (singledef without :...:=)",cmd
     189              cmd = _apply_templates(cmd)
     190              #print 'cmd',cmd
    179191              if not transpose:
    180192                 if pname not in _data:
     
    315327
    316328
     329def _apply_templates(cmd):
     330    cmd = _data_eval(cmd)
     331    template = []
     332    ilist = set()
     333    ans = []
     334    i = 0
     335    while i < len(cmd):
     336        #print i, len(cmd), cmd[i], ilist, template, ans
     337        if type(cmd[i]) is tuple and '*' in cmd[i]:
     338            j = i
     339            tmp=list(cmd[j])
     340            nindex = len(tmp)
     341            template=tmp
     342            ilist = set()
     343            for kk in range(len(tmp)):
     344                if tmp[kk] == '*':
     345                    ilist.add(kk)
     346        elif len(ilist) == 0:
     347            ans.append(cmd[i])
     348        else:
     349            for kk in range(len(template)):
     350                if kk in ilist:
     351                    ans.append(cmd[i])
     352                    i += 1
     353                else:
     354                    ans.append(template[kk])
     355            ans.append(cmd[i])
     356        i += 1
     357    return ans
     358                   
     359           
     360           
     361
    317362def _process_data_list(dim, cmd):
    318363        """
     
    357402             continue
    358403          tmp = None
    359           if "(" in val and ")" in val:
     404          tval = val.strip()
     405          if (tval[0] == "(" and tval[-1] == ")") or (tval[0] == '[' and tval[-1] == ']'):
    360406             vals = []
    361              tval = val[1:-1]
     407             tval = tval[1:-1]
    362408             for item in tval.split(","):
    363409               tmp=_data_eval([item])
     
    375421               ans.append(tmp)
    376422             except ValueError:
    377                ans.append(val)
     423               if val[0] == "'" or val[0] == '"':
     424                    ans.append(val[1:-1])
     425               else:
     426                    ans.append(val)
    378427        if pyomo.debug("reader"):               #pragma:nocover
    379428           print "DEBUG: _data_eval(end)",ans
     
    473522    options = Options(**cmd[1])
    474523    for key in options:
    475         if not key in ['range','filename','format','using','query']:
     524        if not key in ['range','filename','format','using','driver','query','table','user','password']:
    476525            raise ValueError, "Unknown import option '%s'" % key
    477526
     
    484533    # TODO: process mapping info
    485534    #
    486     tmp = options.filename.split(".")[-1]
    487     data = DataManagerFactory(tmp)
     535    if options.using is None:
     536        tmp = options.filename.split(".")[-1]
     537        data = DataManagerFactory(tmp)
     538        if data is None:
     539            raise pyutilib.common.ApplicationError, "Cannot create data manager '%s'" % tmp
     540    else:
     541        data = DataManagerFactory(options.using)
     542        if data is None:
     543            raise pyutilib.common.ApplicationError, "Cannot create data manager '%s'" % options.using
    488544    set_name=None
    489545    param_name=None
     
    518574        param_name = None
    519575    #
    520     data.initialize(options.filename, index=index, index_name=index_name, param_name=symb_map, set=set_name, param=param_name, format=options.format, range=options.range, query=options.query, using=options.using)
     576    data.initialize(options.filename, index=index, index_name=index_name, param_name=symb_map, set=set_name, param=param_name, format=options.format, range=options.range, query=options.query, using=options.using, table=options.table)
    521577    #
    522578    data.open()
     
    527583        raise e
    528584    data.close()
    529     print "Y",_data
    530585    data.process(_model, _data, _default)
    531     #print "Y",_data
    532586       
    533587   
  • coopr.pyomo/stable/coopr/pyomo/data/sheet.py

    r2592 r3046  
    1313from pyutilib.excel import ExcelSpreadsheet
    1414from pyutilib.component.core import alias
     15import pyutilib.common
    1516
    1617
     
    3334            try:
    3435                self.sheet = ExcelSpreadsheet(self.filename)
    35             except pyutilib.ApplicationError:
     36            except pyutilib.common.ApplicationError:
    3637                raise
    3738
  • coopr.pyomo/stable/coopr/pyomo/io/cpxlp.py

    r2907 r3046  
    101101    def _no_label_error ( var ):
    102102        msg  = "Unable to find label for variable '%s'.\n"                    \
    103         'Possibly uninstantiated model.  Do any constraint or objective '     \
    104         'rules reference the original Model() object, as opposed to the '     \
    105         'passed model object?'
     103        'Possibly uninstantiated model.  Do any constraint or objective  '     \
     104        'rules reference the original Model() object, as opposed to the  '     \
     105        'passed model object? Alternatively, if you are developing code, '     \
     106        'has the model instance been pre-processed?'
    106107
    107108        raise ValueError, msg % str(var)
  • coopr.pyomo/stable/coopr/pyomo/preprocess/generic_varlabels.py

    r2359 r3046  
    7070        return tmp
    7171
    72     def preprocess(self,model):
     72    def preprocess(self, model):
    7373        """
    7474        The main routine to perform the preprocess
     
    7979        # variables.
    8080        #
    81         Vars = model.active_components(Var)
    82         for var in Vars.values():
     81        active_variables = model.active_components(Var)
     82        for var in active_variables.values():
    8383            for V in var._varval.keys():
    8484                var._varval[V].label = self._name_fix( var._varval[V].name )
    8585                model._name_varmap[ var._varval[V].label ] = var._varval[V]
    86         Cons = model.active_components(Constraint)
    87         for con in Cons.values():
     86        active_constraints = model.active_components(Constraint)
     87        for con in active_constraints.values():
    8888            for C in con._data:
    8989                con._data[C].label = self._name_fix( con._data[C].name )
    9090                model._name_conmap[ con._data[C].label ] = con._data[C]
    91         Objs = model.active_components(Objective)
    92         for obj in Objs.values():
     91        active_objectives = model.active_components(Objective)
     92        for obj in active_objectives.values():
    9393            for O in obj._data:
    9494                obj._data[O].label = self._name_fix( obj._data[O].name )
  • coopr.pyomo/stable/coopr/pyomo/preprocess/identify_vars.py

    r3004 r3046  
    3636    #
    3737    def _identify_variables(self, exp, model):
     38
    3839        try:
    3940            if exp.fixed_value():
     
    7374            if exp.id in model._var:
    7475                return
    75             exp.id = self.vnum
    76             model._var[self.vnum] = exp
    77             self.vnum += 1
     76            exp.id = self.var_count
     77            model._var[self.var_count] = exp
     78            self.var_count += 1
    7879        #
    7980        # Variable Base
     
    8687            if tmp.id in model._var:
    8788                return
    88             tmp.id = self.vnum
    89             model._var[self.vnum] = tmp
    90             self.vnum += 1           
     89            tmp.id = self.var_count
     90            model._var[self.var_count] = tmp
     91            self.var_count += 1           
    9192        #
    9293        # If not a constant, then this is an error
     
    9596            raise ValueError, "Unexpected expression type in identify_variables: " + str(type(exp))+" "+str(exp)
    9697
     98    #
     99    # this pre-processor computes a number of attributes of model components, in
     100    # addition to models themselves. for VarValue, ConstraintData, and ObjectiveData,
     101    # the routine assigns the "id" attribute. in addition, for VarValues, the routine
     102    # sets the "status" attribute - to used or unused, depending on whether it appears
     103    # in an active constraint/objective. on the Model object, the routine populates
     104    # the "statistics" (variable/constraint/objective counts) attribute, and builds
     105    # the _var, _con, and _obj dictionaries of a Model, which map the id attributes
     106    # to the respective VarValue, ConstraintData, or ObjectiveData object.
     107    #
     108    # NOTE: if variables are fixed, then this preprocessor will flag them as unused
     109    #       and consequently not create entries for them in the model _var map.
     110    #
    97111
    98112    def preprocess(self,model):
     
    101115        """
    102116
    103         Vars = model.active_components(Var)
    104         Con = model.active_components(Constraint)
    105         Obj = model.active_components(Objective)
     117        active_variables = model.active_components(Var)
     118        active_constraints = model.active_components(Constraint)
     119        active_objectives = model.active_components(Objective)
    106120
    107         Con.update(model.active_components(SOSConstraint))
     121        active_constraints.update(model.active_components(SOSConstraint))
    108122
    109         self.vnum=0
    110         self.cnum=0
    111         self.onum=0
     123        self.var_count=0
     124        self.constraint_count=0
     125        self.objective_count=0
    112126       
    113127        model.statistics.number_of_binary_variables = 0
     
    124138        # Until proven otherwise, all variables are unused.
    125139        #
    126         for var in Vars.values():
    127             for V in var._varval.keys():
    128                 var._varval[V].status = VarStatus.unused
    129                 var._varval[V].id = -1
     140        for var in active_variables.values():
     141            for var_value in var._varval.values():
     142                var_value.status = VarStatus.unused
     143                var_value.id = -1
    130144
    131145        #
     
    133147        # used in the objective and constraints.
    134148        #
    135         for key in Obj.keys():
    136             for ondx in Obj[key]._data:
    137                 if not Obj[key]._data[ondx].active:
     149        for name, objective in active_objectives.items():
     150            for index, objective_data in objective._data.items():
     151                if not objective_data.active:
    138152                    continue
    139153                try:
    140                     self._identify_variables(Obj[key]._data[ondx].expr, model)
     154                    self._identify_variables(objective_data.expr, model)
    141155                except ValueError, err:
    142                     raise ValueError, "Problem processing objective %s (index %s): %s" % (str(key), str(ondx), str(err))
     156                    raise ValueError, "Problem processing objective %s (index %s): %s" % (str(name), str(index), str(err))
    143157
    144                 Obj[key]._data[ondx].id = self.onum
    145                 self.onum += 1
     158                objective_data.id = self.objective_count
     159                model._obj[self.objective_count] = objective_data
     160                self.objective_count += 1
    146161
    147         for key in Con.keys():
    148             C = Con[key]
    149             for cndx in C.keys():
    150                 if not C._data[cndx].active:
     162        for name, constraint in active_constraints.items():
     163            for index, constraint_data in constraint._data.items():
     164                if not constraint_data.active:
    151165                    continue
    152166                try:
    153                     self._identify_variables(C._data[cndx].body, model)
     167                    self._identify_variables(constraint_data.body, model)
    154168                except ValueError, err:
    155                     raise ValueError, "Problem processing constraint %s (index %s): %s" % (str(key), str(cndx), str(err))
    156                 C._data[cndx].id = self.cnum
    157                 model._con[self.cnum] = C._data[cndx]
    158                 self.cnum += 1
     169                    raise ValueError, "Problem processing constraint %s (index %s): %s" % (str(name), str(index), str(err))
     170                constraint_data.id = self.constraint_count
     171                model._con[self.constraint_count] = constraint_data
     172                self.constraint_count += 1
    159173
    160174        #
     
    166180        # each variable value.
    167181        #
    168         for var in Vars.values():
     182        for var in active_variables.values():
    169183            model.statistics.number_of_binary_variables += len(var.binary_keys())
    170184            model.statistics.number_of_integer_variables += len(var.integer_keys())
     
    175189        #
    176190
    177         model.statistics.number_of_variables = self.vnum
    178         model.statistics.number_of_constraints = self.cnum
    179         model.statistics.number_of_objectives = self.onum
     191        model.statistics.number_of_variables = self.var_count
     192        model.statistics.number_of_constraints = self.constraint_count
     193        model.statistics.number_of_objectives = self.objective_count
    180194
    181195        return model
  • coopr.pyomo/stable/coopr/pyomo/tests/examples/test_pyomo.py

    r2611 r3046  
    4545        self.pyomo('pmedian.py pmedian.dat', file=currdir+'test1.out')
    4646        self.failUnlessFileEqualsBaseline(currdir+"test1.out", currdir+"test1.txt")
     47
     48    def test1a(self):
     49        """Simple execution of 'pyomo' with implicit rules"""
     50        self.pyomo('pmedian3.py pmedian.dat', file=currdir+'test1a.out')
     51        self.failUnlessFileEqualsBaseline(currdir+"test1a.out", currdir+"test1.txt")
    4752
    4853    def test2(self):
  • coopr.pyomo/stable/coopr/pyomo/tests/unit/data8.dat

    r2310 r3046  
    11param A := False;
    22param B := True;
    3 set Z := foo[*] bar [ * ] bar[1,*,a,*] foo-bar hello-goodbye ;
    4 set Z[A] := foo[*] bar [ * ] bar[1,*,a,*] foo-bar hello-goodbye ;
     3set Z := foo[*] bar[ * ] bar[1,*,a,*] foo-bar hello-goodbye ;
     4set Z[A] := foo[*] bar[ * ] bar[1,*,a,*] foo-bar hello-goodbye ;
  • coopr.pyomo/stable/coopr/pyomo/tests/unit/test_param.py

    r3004 r3046  
    615615        self.failUnlessEqual( self.instance.B['dd'], -14)
    616616
     617    def test_io10(self):
     618        OUTPUT=open("param.dat","w")
     619        print >>OUTPUT, "data;"
     620        print >>OUTPUT, "set A1 := a b c d e f g h i j k l ;"
     621        print >>OUTPUT, "set A2 := 2 4 6 ;"
     622        print >>OUTPUT, "param B :="
     623        print >>OUTPUT, " [*,2,*] a b 1 c d 2 e f 3"
     624        print >>OUTPUT, " [*,4,*] g h 4 i j 5"
     625        print >>OUTPUT, " [*,6,*] k l 6"
     626        print >>OUTPUT, ";"
     627        print >>OUTPUT, "end;"
     628        OUTPUT.close()
     629        self.model.A1=Set()
     630        self.model.A2=Set()
     631        self.model.B=Param(self.model.A1,self.model.A2,self.model.A1)
     632        self.instance = self.model.create("param.dat")
     633        self.failUnlessEqual( set(self.instance.B.keys()), set([('e', 2, 'f'), ('c', 2, 'd'), ('a', 2, 'b'), ('i', 4, 'j'), ('g', 4, 'h'), ('k', 6, 'l')]))
     634
    617635
    618636class TestParamError(PyomoModel):
  • coopr.pyomo/stable/coopr/pyomo/tests/unit/test_set.py

    r2867 r3046  
    21112111        self.failUnlessEqual( len(self.instance.F.keys()), 3)
    21122112        self.failUnlessEqual( len(self.instance.F['A1']), 3)
     2113
     2114    def test_io8(self):
     2115        OUTPUT=open(currdir+"setA.dat","w")
     2116        print >>OUTPUT, "data;"
     2117        print >>OUTPUT, "set E :="
     2118        print >>OUTPUT, "(A1,1,*) A1 A2"
     2119        print >>OUTPUT, "(*,2,*) A2 A3"
     2120        print >>OUTPUT, "(A3,3,*) A1 A3 ;"
     2121        print >>OUTPUT, "end;"
     2122        OUTPUT.close()
     2123        self.model.E = Set(dimen=3)
     2124        self.instance = self.model.create(currdir+"setA.dat")
     2125        self.failUnlessEqual( len(self.instance.E), 5)
     2126
     2127    def test_io9(self):
     2128        OUTPUT=open(currdir+"setA.dat","w")
     2129        print >>OUTPUT, "data;"
     2130        print >>OUTPUT, "set E :="
     2131        print >>OUTPUT, "(A1,1,A1) (A1,1,A2)"
     2132        print >>OUTPUT, "(A2,2,A3)"
     2133        print >>OUTPUT, "(A3,3,A1) (A3,3,A3) ;"
     2134        print >>OUTPUT, "end;"
     2135        OUTPUT.close()
     2136        self.model.E = Set(dimen=3)
     2137        self.instance = self.model.create(currdir+"setA.dat")
     2138        self.failUnlessEqual( len(self.instance.E), 5)
     2139
     2140    def test_io10(self):
     2141        OUTPUT=open(currdir+"setA.dat","w")
     2142        print >>OUTPUT, "data;"
     2143        print >>OUTPUT, "set A := 'A1 x' ' A2' \"A3\";"
     2144        print >>OUTPUT, "set F['A1 x'] := 1 3 5;"
     2145        print >>OUTPUT, "set F[\" A2\"] := 2 4 6;"
     2146        print >>OUTPUT, "set F['A3'] := 3 5 7;"
     2147        print >>OUTPUT, "end;"
     2148        OUTPUT.close()
     2149        self.model.A = Set()
     2150        self.model.F = Set(self.model.A)
     2151        self.instance = self.model.create(currdir+"setA.dat")
     2152        self.failUnlessEqual( self.instance.F.dim(), 1)
     2153        self.failUnlessEqual( len(self.instance.F.keys()), 3)
     2154        self.failUnlessEqual( len(self.instance.F['A1 x']), 3)
    21132155
    21142156
  • coopr.pyomo/stable/coopr/pyomo/tests/unit/test_uninstantiated_model.txt

    r2762 r3046  
    11
    22ERROR: Unable to find label for variable 'x'.
    3 Possibly uninstantiated model.  Do any constraint or objective rules reference the original Model() object, as opposed to the passed model object?
     3Possibly uninstantiated model.  Do any constraint or objective  rules reference the original Model() object, as opposed to the  passed model object? Alternatively, if you are developing code, has the model instance been pre-processed?
Note: See TracChangeset for help on using the changeset viewer.