Changeset 2315


Ignore:
Timestamp:
Feb 11, 2010 11:32:29 AM (10 years ago)
Author:
wehart
Message:

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

........

r2242 | wehart | 2010-02-01 20:08:36 -0700 (Mon, 01 Feb 2010) | 2 lines


Documentation update.

........

r2244 | wehart | 2010-02-01 20:10:18 -0700 (Mon, 01 Feb 2010) | 2 lines


Tagging coopr.pyomo 2.3 release

........

r2253 | wehart | 2010-02-03 13:57:16 -0700 (Wed, 03 Feb 2010) | 2 lines


Changes due to termination condition refinement

........

r2264 | wehart | 2010-02-05 08:20:30 -0700 (Fri, 05 Feb 2010) | 3 lines


Misc rework of the scripting modules. These changes allow this
functionality to be used more effectively within other Python scripts.

........

r2274 | wehart | 2010-02-06 08:13:24 -0700 (Sat, 06 Feb 2010) | 8 lines


Added the 'include' command, which can include other *.dat files. This works
like the AMPL include command, with the exception that it must be terminated by
a semicolon.


Reworked the AMPL datafile plugin to simple execute an include command
on a *.dat file. This rework has the advantage that filename/lineno information
is now propigated throught he AMPL command processing.

........

r2278 | wehart | 2010-02-07 13:06:10 -0700 (Sun, 07 Feb 2010) | 7 lines


A major rework of the parsing of data command files (formerly known
as AMPL DAT files). This rework is the first step towards a full
parse of these files. I've used PLY to lex/yacc these files. This should
resolve a variety of issues that we've observed relating to the sensitivity of
the old parser to whitespace. This is also a first step towards a careful parse
of command lines for the 'import' command.

........

r2279 | wehart | 2010-02-08 06:07:14 -0700 (Mon, 08 Feb 2010) | 2 lines


Adding support for a basic 'import' command.

........

r2283 | jwatson | 2010-02-08 11:28:22 -0700 (Mon, 08 Feb 2010) | 1 line


Added suffix option to pyomo script, to enable specification of suffixes on the command-line.

........

r2285 | wehart | 2010-02-08 11:48:54 -0700 (Mon, 08 Feb 2010) | 2 lines


Working through more examples of the 'import' command.

........

r2287 | wehart | 2010-02-08 12:12:41 -0700 (Mon, 08 Feb 2010) | 2 lines


Adding a script to test the parser.

........

r2289 | wehart | 2010-02-08 15:13:22 -0700 (Mon, 08 Feb 2010) | 4 lines


Setting up 'import' tests for the other simple examples.
These seem to work, but the internal code is too ugly. I'm
going to clean that up next to help explain what is going on...

........

r2292 | wehart | 2010-02-09 12:21:24 -0700 (Tue, 09 Feb 2010) | 10 lines


Rework of the 'import' semantics. This supports
a simple import semantics, such as:


import foobar.tab: [A,B] X Y Z;


as well as index/parameter renaming:


import foobar.tab: J=[A,B] R=X S=Y T=Z;

........

r2293 | wehart | 2010-02-09 14:22:23 -0700 (Tue, 09 Feb 2010) | 2 lines


Bug fix for dealing with MS Windows filenames.

........

r2295 | wehart | 2010-02-09 16:23:37 -0700 (Tue, 09 Feb 2010) | 4 lines


Reworking tests to do basic evaluation of the
interface to Excel spreadsheets.

........

r2302 | wehart | 2010-02-09 20:25:42 -0700 (Tue, 09 Feb 2010) | 2 lines


Adding tests for loading sets and parameters from spreadsheets.

........

r2303 | wehart | 2010-02-09 20:50:12 -0700 (Tue, 09 Feb 2010) | 2 lines


Adding a plugin for reading CSV files.

........

r2304 | wehart | 2010-02-09 20:51:27 -0700 (Tue, 09 Feb 2010) | 4 lines


Renaming csv.py to csv_table.py


The former filename masked the system 'csv' module. :P

........

r2305 | wehart | 2010-02-09 20:57:12 -0700 (Tue, 09 Feb 2010) | 2 lines


Adding CSV data files for the tutorial.

........

r2306 | wehart | 2010-02-09 20:58:42 -0700 (Tue, 09 Feb 2010) | 2 lines


Adding tests for importing CSV files.

........

r2307 | wehart | 2010-02-09 22:02:40 -0700 (Tue, 09 Feb 2010) | 3 lines


Adding a test that is used to verify that data commands can include indexed variables with astrices
in their arguments.

........

r2309 | wehart | 2010-02-10 22:06:28 -0700 (Wed, 10 Feb 2010) | 3 lines


Misc edits, which preserve some data that I was playing with to validate some
advanced parser features.

........

r2310 | wehart | 2010-02-10 22:38:44 -0700 (Wed, 10 Feb 2010) | 2 lines


Generalizing parsing behavior for set strings.

........

Location:
coopr.pyomo/stable/2.3
Files:
21 edited
23 copied

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/stable/2.3

  • coopr.pyomo/stable/2.3/coopr/pyomo/__init__.py

    r2227 r2315  
    2323import coopr.opt
    2424import transform
     25import scripting
    2526
    2627pyutilib.component.core.PluginGlobals.pop_env()
  • coopr.pyomo/stable/2.3/coopr/pyomo/base/param.py

    r2142 r2315  
    282282           raise ValueError, "Invalid value "+str(value)+" for parameter "+self.name
    283283        return False
     284
     285    def data(self):
     286        tmp = {}
     287        for key in self.keys():
     288            tmp[key] = self._paramval[key].value
     289        return tmp
    284290       
    285291
  • coopr.pyomo/stable/2.3/coopr/pyomo/base/plugin.py

    r2227 r2315  
    168168    if name is None:
    169169        return map(lambda x:x.name, ep())
    170     return ep.service(name).create()
     170    service = ep.service(name)
     171    if service is None:
     172        return None
     173    return service.create()
    171174
    172175
  • coopr.pyomo/stable/2.3/coopr/pyomo/data/TableData.py

    r2227 r2315  
    3030        self.options = Options(**kwds)
    3131
    32     def open(self, filename):
     32    def open(self):
    3333        """
    3434        Open the table
     
    5252        Return the data that was extracted from this table
    5353        """
    54         return _process_data(self._info, model, data, default)
     54        return _process_data(self._info, model, data, default, self.filename)
    5555
    5656    def clear(self):
     
    6161
    6262    def _set_data(self, headers, rows):
    63         if not self.options.set is None:
    64            if self.options.format is None:
    65               self._info = ["set",self.options.set,":="]
    66               for row in rows:
     63        #print "_SET_DATA",headers,rows
     64        if self.options.param_name is None:
     65            mapped_headers = map(str,list(headers))
     66        else:
     67            mapped_headers = [self.options.param_name[header] if header in self.options.param_name else header for header in map(str,list(headers))]
     68        #print "X",mapped_headers,self.options.param_name,self.options.index,self.options.format
     69
     70        if self.options.format == 'set':
     71            if self.options.set is None:
     72                raise IOError, "Must specify set name for data with the 'set' format"
     73            if not (self.options.index is None or self.options.index == []):
     74                raise IOError, "Cannot specify index for data with the 'set' format: %s" % str(self.options.index)
     75            self._info = ["set",self.options.set,":="]
     76            for row in rows:
    6777                self._info = self._info + list(row)
    68            elif self.options.format is "set_array":
    69               self._info = ["set",self.options.set, ":"]
    70               self._info = self._info + list(headers[1:])
    71               self._info = self._info + [":="]
    72               for row in rows:
     78
     79        elif self.options.format == 'set_array':
     80            if self.options.set is None:
     81                raise IOError, "Must specify set name for data with the 'set_array' format"
     82            if not (self.options.index is None or self.options.index == []):
     83                raise IOError, "Cannot specify index for data with the 'set_array' format: %s" % str(self.options.index)
     84            self._info = ["set",self.options.set, ":"]
     85            self._info = self._info + list(mapped_headers[1:])
     86            self._info = self._info + [":="]
     87            for row in rows:
    7388                self._info = self._info + list(row)
    74            else:
    75               raise ValueError, "Unknown set format: "+self.options.format
    7689
    77         elif self.options.index is not None and self.options.param is None:
    78            self._info = ["param",":",self.options.index,":"]
    79            self._info = self._info + map(str,list(headers[1:]))
    80            self._info.append(":=")
    81            for row in rows:
    82              self._info = self._info + list(row)
     90        elif self.options.format == 'transposed_array':
     91            self._info = ["param",self.options.param,"(tr)",":"] + mapped_headers[1:]
     92            self._info.append(":=")
     93            for row in rows:
     94                self._info = self._info + list(row)
    8395
    84         elif self.options.index is not None and self.options.param is not None:
    85            self._info = ["param",":",self.options.index,":"]
    86            if type(self.options.param) is str:
    87               self._info = self._info + [self.options.param]
    88            elif type(self.options.param) is tuple:
    89               self._info = self._info + list(self.options.param)
    90            else:
    91               self._info = self._info + self.options.param
    92            self._info.append(":=")
    93            for row in rows:
    94              self._info = self._info + list(row)
     96        elif self.options.format == 'array':
     97            self._info = ["param",self.options.param,":"] + mapped_headers[1:]
     98            self._info.append(":=")
     99            for row in rows:
     100                self._info = self._info + list(row)
    95101
    96         elif self.options.param is not None and self.options.format is not None:
    97            if self.options.format is "transposed_array":
    98               self._info = ["param",self.options.param,"(tr)",":"] + map(str,list(headers[1:]))
    99            elif self.options.format is "array":
    100               self._info = ["param",self.options.param,":"] + map(str,list(headers[1:]))
    101            else:
    102               raise ValueError, "Unknown parameter format: "+self.options.format
    103            self._info.append(":=")
    104            for row in rows:
    105              self._info = self._info + list(row)
     102        elif self.options.format == 'param':
     103            self._info = ["param",":",self.options.param,':=']
     104            for row in rows:
     105                self._info = self._info + list(row)
     106
     107        elif self.options.format == 'table' or self.options.format is None:
     108            if self.options.index is None or len(self.options.index) == 0:
     109                raise IOError, "Cannot import a relational table without specifying index values"
     110            if self.options.index_name is not None:
     111                self._info = ["param",":",self.options.index_name,":"]
     112            else:
     113                self._info = ["param",":"]
     114            for header in mapped_headers:
     115                if not header in self.options.index:
     116                    self._info.append(header)
     117            self._info.append(":=")
     118            for row in rows:
     119                self._info = self._info + list(row)
    106120
    107121        else:
    108            if len(headers) == 1:
    109               self._info = ["set",headers[0],":="]
    110               for row in rows:
    111                 self._info = self._info + list(row)
    112            else:
    113               self._info = ["param",":"]
    114               if self.options.param is None:
    115                  self._info = self._info + map(str,list(headers[1:]))
    116               elif type(self.options.param) is str:
    117                  self._info = self._info + [self.options.param]
    118               else:
    119                  self._info = self._info + list(self.options.param)
    120               self._info.append(":=")
    121               for row in rows:
    122                 self._info = self._info + list(row)
     122            raise ValueError, "Unknown parameter format: '%s'" % self.options.format
    123123
  • coopr.pyomo/stable/2.3/coopr/pyomo/data/__init__.py

    r2227 r2315  
    1212import text
    1313import sheet
     14import parse_datacmds
     15import csv_table
    1416#import db
  • coopr.pyomo/stable/2.3/coopr/pyomo/data/ampl.py

    r2227 r2315  
     1#  _________________________________________________________________________
     2#
     3#  Coopr: A COmmon Optimization Python Repository
     4#  Copyright (c) 2008 Sandia Corporation.
     5#  This software is distributed under the BSD License.
     6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
     7#  the U.S. Government retains certain rights in this software.
     8#  For more information, see the Coopr README.txt file.
     9#  _________________________________________________________________________
    110
    211import os.path
    312import re
    4 from pyutilib.misc import Options, quote_split
     13from pyutilib.misc import Options
    514from coopr.pyomo.base.plugin import DataManagerRegistration
    6 from process_data import _process_data
     15from process_data import _process_include
    716
    817class AmplDataCommands(object):
     
    2029        if not os.path.exists(self.filename):
    2130            raise IOError, "Cannot find file '%s'" % self.filename
    22         self.INPUT = open(self.filename, 'r')
    2331
    2432    def close(self):
    25         self.INPUT.close()
     33        pass
    2634       
    2735    def read(self):
    2836        """
    29         Create a table of tuples to values, based on data from a file.
    30         We assume that this data is defined in a format that is
    31         compatible with AMPL.
     37        This function does nothing, since executing AMPL data commands
     38        both reads and processes the data all at once.
    3239        """
    33         global Filename
    34         Filename = self.filename
    35         global Lineno
    36         Lineno = 0
    37         cmd=""
    38         status=True
    39         for line in self.INPUT:
    40           Lineno += 1
    41           line = re.sub(":"," :",line)
    42           line = line.strip()
    43           if line == "" or line[0] == '#':
    44              continue
    45           cmd = cmd + " " + line
    46           if ';' in cmd:
    47              #
    48              # We assume that a ';' indicates an end-of-command declaration.
    49              # However, the user might have put multiple commands on a single
    50              # line, so we need to split the line based on these values.
    51              # BUT, at the end of the line we should see an 'empty' command,
    52              # which we ignore.
    53              #
    54              for item in cmd.split(';'):
    55                item = item.strip()
    56                if item != "":
    57                   self._info.append(quote_split("[\t ]+",item))
    58              cmd = ""
    59         if cmd != "":
    60             raise IOError, "ERROR: There was unprocessed text at the end of the data file!: \"" + cmd + "\""
    61             self.INPUT.close()
    62         self.INPUT.close()
    63         return status
     40        return True
    6441
    6542    def process(self, model, data, default):
    6643        """
    67         Return the data that was extracted from this table
     44        Read AMPL data commands and process the data.
    6845        """
    69         for item in self._info:
    70             _process_data(item, model, data, default)
     46        _process_include(['include', self.filename], model, data, default)
    7147
    7248    def clear(self):
  • coopr.pyomo/stable/2.3/coopr/pyomo/data/process_data.py

    r2227 r2315  
    1212import copy
    1313import math
     14from parse_datacmds import parse_data_commands
     15from pyutilib.misc import quote_split, Options
    1416from coopr.pyomo.base.plugin import *
    1517from coopr.pyomo.base import pyomo
     18from coopr.pyomo.base.plugin import *
     19
     20global Lineno
     21global Filename
    1622
    1723
     
    97103        if len(cmd) == 0:
    98104           return []
    99         sd = getattr(_model,sname)
     105        try:
     106            sd = getattr(_model,sname)
     107        except AttributeError:
     108            raise IOError, "Cannot process data for set '%s' because it is not defined in model '%s'" % (sname, _model.name)
    100109        #d = sd.dimen
    101110        cmd = _data_eval(cmd)
     
    372381
    373382
    374 def _process_data(cmd, _model, _data, _default):
     383def _process_include(cmd, _model, _data, _default):
     384    if len(cmd) == 1:
     385        raise IOError, "Cannot execute 'include' command without a filename"
     386    if len(cmd) > 2:
     387        raise IOError, "The 'include' command only accepts a single filename"
     388
     389    global Filename
     390    Filename = cmd[1]
     391    global Lineno
     392    Lineno = 0
     393
     394    try:
     395        cmds = parse_data_commands(filename=cmd[1])
     396    except IOError, err:
     397        raise IOError, "Error parsing file '%s': %s" % (Filename, str(err))
     398    for cmd in cmds:
     399        _process_data(cmd, _model, _data, _default, Filename, Lineno)
     400    return True
     401
     402
     403def X_process_include(cmd, _model, _data, _default):
     404    if len(cmd) == 1:
     405        raise IOError, "Cannot execute 'include' command without a filename"
     406    if len(cmd) > 2:
     407        raise IOError, "The 'include' command only accepts a single filename"
     408
     409    global Filename
     410    Filename = cmd[1]
     411    global Lineno
     412    Lineno = 0
     413    cmd=""
     414    status=True
     415    INPUT=open(Filename,'r')
     416    for line in INPUT:
     417        Lineno = Lineno + 1
     418        line = re.sub(":"," :",line)
     419        line = line.strip()
     420        if line == "" or line[0] == '#':
     421            continue
     422        cmd = cmd + " " + line
     423        if ';' in cmd:
     424            #
     425            # We assume that a ';' indicates an end-of-command declaration.
     426            # However, the user might have put multiple commands on a single
     427            # line, so we need to split the line based on these values.
     428            # BUT, at the end of the line we should see an 'empty' command,
     429            # which we ignore.
     430            #
     431            for item in cmd.split(';'):
     432                item = item.strip()
     433                if item != "":
     434                    _process_data(quote_split("[\t ]+",item), _model, _data, _default, Filename, Lineno)
     435                cmd = ""
     436    if cmd != "":
     437        raise IOError, "ERROR: There was unprocessed text at the end of the data file!: \"" + cmd + "\""
     438        INPUT.close()
     439    INPUT.close()
     440    return status
     441
     442
     443def _process_import(cmd, _model, _data, _default):
     444    #print "IMPORT",cmd
     445    if len(cmd) < 2:
     446        raise IOError, "The 'import' command must specify a filename"
     447
     448    options = Options(**cmd[1])
     449    for key in options:
     450        if not key in ['range','filename','format','using','query']:
     451            raise ValueError, "Unknown import option '%s'" % key
     452
     453    global Filename
     454    Filename = cmd[1]
     455    global Lineno
     456    Lineno = 0
     457
     458    #
     459    # TODO: process mapping info
     460    #
     461    tmp = options.filename.split(".")[-1]
     462    data = DataManagerFactory(tmp)
     463    set_name=None
     464    param_name=None
     465    #
     466    # Create symbol map
     467    #
     468    symb_map = cmd[3]
     469    if len(symb_map) == 0:
     470        raise IOError, "Must specify at least one set or parameter name that will be imported"
     471    #
     472    # Process index data
     473    #
     474    index=cmd[2][1]
     475    index_name=cmd[2][0]
     476    #
     477    # Set the 'set name' based on the format
     478    #
     479    if options.format == 'set' or options.format == 'set_array':
     480        if len(cmd[3]) != 1:
     481            raise IOError, "A single set name must be specified when using format '%s'" % options.format
     482        set_name=cmd[3].keys()[0]
     483    else:
     484        set_name=None
     485    #
     486    # Set the 'param name' based on the format
     487    #
     488    if options.format == 'transposed_array' or options.format == 'array' or options.format == 'param':
     489        if len(cmd[3]) != 1:
     490            raise IOError, "A single parameter name must be specified when using format '%s'" % options.format
     491        param_name = cmd[3].keys()[0]
     492    else:
     493        param_name = None
     494    #
     495    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)
     496    #
     497    data.open()
     498    data.read()
     499    data.close()
     500    data.process(_model, _data, _default)
     501       
     502   
     503
     504
     505def _process_data(cmd, _model, _data, _default, Filename_, Lineno_=0):
    375506        """
    376507        Called by import_file() to (1) preprocess data and (2) call
    377508        subroutines to process different types of data
    378509        """
    379         #global Lineno
    380         #global Filename
     510        #print "CMD",cmd
     511        global Lineno
     512        global Filename
     513        Lineno=Lineno_
     514        Filename=Filename_
     515
    381516        if pyomo.debug("reader"):               #pragma:nocover
    382517           print "DEBUG: _process_data (start)",cmd
    383518        if len(cmd) == 0:                       #pragma:nocover
    384519           raise ValueError, "ERROR: Empty list passed to Model::_process_data"
     520
    385521        cmd = _preprocess_data(cmd)
     522
    386523        if cmd[0] == "data":
    387524           return True
    388525        if cmd[0] == "end":
    389526           return False
    390         if cmd[0][0:3] == "set":
     527        if cmd[0].startswith('set'):
    391528           _process_set(cmd, _model, _data)
    392         elif cmd[0][0:5] == "param":
     529        elif cmd[0].startswith('param'):
    393530           _process_param(cmd, _model, _data, _default)
     531        elif cmd[0] == 'include':
     532           _process_include(cmd, _model, _data, _default)
     533        elif cmd[0] == 'import':
     534           _process_import(cmd, _model, _data, _default)
    394535        else:
    395536           raise IOError, "ERROR: Unknown data command: "+" ".join(cmd)
  • coopr.pyomo/stable/2.3/coopr/pyomo/data/sheet.py

    r2227 r2315  
     1#  _________________________________________________________________________
     2#
     3#  Coopr: A COmmon Optimization Python Repository
     4#  Copyright (c) 2008 Sandia Corporation.
     5#  This software is distributed under the BSD License.
     6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
     7#  the U.S. Government retains certain rights in this software.
     8#  For more information, see the Coopr README.txt file.
     9#  _________________________________________________________________________
    110
    211import os.path
     
    2938        tmp = self.sheet.get_range(self.options.range, raw=True)
    3039        if type(tmp) in (int,long,float):
    31             self._info = ["param",self.options.param,":=",tmp]
     40            if not self.options.param is None:
     41                self._info = ["param",self.options.param,":=",tmp]
     42            elif len(self.options.symbol_map) == 1:
     43                self._info = ["param",self.options.symbol_map[self.options.symbol_map.keys()[0]],":=",tmp]
     44            else:
     45                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
     46        elif len(tmp) == 0:
     47            raise IOError, "Empty range '%s'" % self.options.range
    3248        else:
    3349            self._set_data(tmp[0], tmp[1:])
  • coopr.pyomo/stable/2.3/coopr/pyomo/data/text.py

    r2227 r2315  
     1#  _________________________________________________________________________
     2#
     3#  Coopr: A COmmon Optimization Python Repository
     4#  Copyright (c) 2008 Sandia Corporation.
     5#  This software is distributed under the BSD License.
     6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
     7#  the U.S. Government retains certain rights in this software.
     8#  For more information, see the Coopr README.txt file.
     9#  _________________________________________________________________________
    110
    211import os.path
     
    3140            raise IOError, "Empty *.tab file"
    3241        elif len(tmp) == 1:
    33             self._info = ["param",self.options.param,":=",tmp[0][0]]
     42            if not self.options.param is None:
     43                self._info = ["param",self.options.param,":=",tmp[0][0]]
     44            elif len(self.options.symbol_map) == 1:
     45                self._info = ["param",self.options.symbol_map[self.options.symbol_map.keys()[0]],":=",tmp[0][0]]
     46            else:
     47                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
    3448        else:
    3549            self._set_data(tmp[0], tmp[1:])
  • coopr.pyomo/stable/2.3/coopr/pyomo/scripting/__init__.py

    r2146 r2315  
    88#  _________________________________________________________________________
    99
    10 
     10from convert import *
  • coopr.pyomo/stable/2.3/coopr/pyomo/scripting/convert.py

    r2160 r2315  
    121121    if not util.apply_preprocessing(options, parser, args):
    122122        return
    123     instance = util.create_model(options, args)
     123    model = util.create_model(options, args)
     124    return model
    124125
    125126def pyomo2lp(args=None):
  • coopr.pyomo/stable/2.3/coopr/pyomo/scripting/pyomo.py

    r2170 r2315  
    139139            type="float",
    140140            default=None)
     141    parser.add_option("--solver-suffixes",
     142            help="One or more solution suffixes to be extracted by the solver",
     143            action="append",
     144            dest="solver_suffixes",
     145            type="string",
     146            default=[])
    141147    parser.add_option("--model-name",
    142148            help="The name of the model object that is created in the specified Pyomo module",
     
    199205    if not util.apply_preprocessing(options, parser, args):
    200206        return Container()                                   #pragma:nocover
    201     instance = util.create_model(options, args)
     207    model = util.create_model(options, args)
    202208    if options.save_model or options.only_instance:
    203         return Container(instance=instance)
    204     results, opt = util.apply_optimizer(options, instance)
    205     if not util.process_results(options, instance, results, opt):
    206         return Container(instance=instance, results=results) #pragma:nocover
    207     util.apply_postprocessing(options, instance, results)
    208     return Container(instance=instance, results=results)
     209        return Container(instance=model.instance)
     210    results, opt = util.apply_optimizer(options, model.instance)
     211    if not util.process_results(options, model.instance, results, opt):
     212        return Container(instance=model.instance, results=results) #pragma:nocover
     213    util.apply_postprocessing(options, model.instance, results)
     214    return Container(instance=model.instance, results=results)
    209215
    210216
  • coopr.pyomo/stable/2.3/coopr/pyomo/scripting/util.py

    r2218 r2315  
    199199       print ""
    200200
     201    fname=None
     202    symbol_map=None
    201203    if not options.save_model is None:
    202204        if options.save_model == True:
     
    209211            fname = options.save_model
    210212            format=None
    211         instance.write(filename=fname, format=format)
     213        [fname, symbol_map] = instance.write(filename=fname, format=format)
    212214        if not os.path.exists(fname):
    213215            print "ERROR: file "+fname+" has not been created!"
     
    215217            print "Model written to file '"+str(fname)+"'"
    216218
    217     return instance
     219    return pyutilib.misc.Container(instance=instance, symbol_map=symbol_map, filename=fname)
    218220
    219221#
     
    236238    if options.solver_mipgap is not None:
    237239       opt.mipgap = options.solver_mipgap
     240    opt.suffixes = options.solver_suffixes
    238241    if not subsolver is None:
    239242        subsolver=' solver='+subsolver
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/examples/test1.txt

    r2049 r2315  
    2121Solver:
    2222- Status: ok
    23   Termination condition: unsure
     23  Termination condition: unknown
    2424  Error rc: 0
    2525
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/examples/test10.txt

    r2227 r2315  
    6969Solver:
    7070- Status: ok
    71   Termination condition: unsure
     71  Termination condition: unknown
    7272  Error rc: 0
    7373
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/examples/test11.txt

    r2148 r2315  
    15301530Solver:
    15311531- Status: ok
    1532   Termination condition: unsure
     1532  Termination condition: unknown
    15331533  Error rc: 0
    15341534
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/examples/test12.txt

    r2148 r2315  
    2121Solver:
    2222- Status: ok
    23   Termination condition: unsure
     23  Termination condition: unknown
    2424  Error rc: 0
    2525
  • coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/data8.dat

    r1448 r2315  
    11param A := False;
    22param B := True;
     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/2.3/coopr/pyomo/tests/unit/test_modeldata.py

    r2227 r2315  
    1111from coopr.pyomo import *
    1212import coopr
     13import pyutilib.common
    1314
    1415currdir=dirname(abspath(__file__))+os.sep
    15 example_dir=coopr_dir+os.sep+"example"+os.sep+"pyomo"+os.sep+"tutorials"+os.sep+"tab"+os.sep
     16example_dir=coopr_dir+os.sep+".."+os.sep+"examples"+os.sep+"pyomo"+os.sep+"tutorials"+os.sep+"tab"+os.sep
     17csv_dir=coopr_dir+os.sep+".."+os.sep+"examples"+os.sep+"pyomo"+os.sep+"tutorials"+os.sep+"csv"+os.sep
     18tutorial_dir=coopr_dir+os.sep+".."+os.sep+"examples"+os.sep+"pyomo"+os.sep+"tutorials"+os.sep
    1619
    1720try:
     
    3740
    3841    def test_read_set(self):
    39         td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",set="X")
     42        td = DataManagerFactory('xls')
     43        td.initialize(currdir+"Book1.xls", range="TheRange", format='set', set="X")
    4044        try:
    4145           td.open()
    4246           td.read()
    4347           td.close()
    44            self.failUnlessEqual( td.data(), ['set', 'X', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
    45         except coopr.ApplicationError:
     48           self.failUnlessEqual( td._info, ['set', 'X', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
     49        except pyutilib.common.ApplicationError:
    4650           pass
    4751
    48     def test_read_set2(self):
    49         td = TableData(range="TheRange",set="X")
    50         try:
    51            td.open(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls")
    52            td.read()
    53            td.close()
    54            self.failUnlessEqual( td.data(), ['set', 'X', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
    55         except coopr.ApplicationError:
    56            pass
    57 
    5852    def test_read_param1(self):
    59         td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange")
     53        td = DataManagerFactory('xls')
     54        td.initialize(currdir+"Book1.xls", range="TheRange", index=['aa'], param=['bb','cc','dd'])
    6055        try:
    6156          td.open()
    6257          td.read()
    6358          td.close()
    64           self.failUnlessEqual( td.data(), ['param', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
    65         except coopr.ApplicationError:
     59          self.failUnlessEqual( td._info, ['param', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
     60        except pyutilib.common.ApplicationError:
    6661          pass
    6762
    6863    def test_read_param2(self):
    69         td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",index="X")
     64        td = DataManagerFactory('xls')
     65        td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa'], param=['bb','cc','dd'])
    7066        try:
    7167          td.open()
    7268          td.read()
    7369          td.close()
    74           self.failUnlessEqual( td.data(), ['param', ':', 'X', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
    75         except coopr.ApplicationError:
     70          self.failUnlessEqual( td._info, ['param', ':', 'X', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
     71        except pyutilib.common.ApplicationError:
    7672          pass
    7773
    7874    def test_read_param3(self):
    79         td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",index="X", param="a")
     75        td = DataManagerFactory('xls')
     76        td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa','bb','cc'], param=["dd"], param_name={'dd':'a'})
    8077        try:
    8178          td.open()
    8279          td.read()
    8380          td.close()
    84           self.failUnlessEqual( td.data(), ['param', ':', 'X', ':', 'a', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
    85         except coopr.ApplicationError:
     81          self.failUnlessEqual( td._info, ['param', ':', 'X', ':', 'a', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
     82        except pyutilib.common.ApplicationError:
    8683          pass
    8784
    8885    def test_read_param4(self):
    89         td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",index="X", param=("a","b"))
     86        td = DataManagerFactory('xls')
     87        td.initialize(currdir+"Book1.xls",range="TheRange", index_name="X", index=['aa','bb'], param=['cc','dd'], param_name={'cc':'a', 'dd':'b'})
    9088        try:
    9189          td.open()
    9290          td.read()
    9391          td.close()
    94           self.failUnlessEqual( td.data(), ['param', ':', 'X', ':', 'a', 'b', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
    95         except coopr.ApplicationError:
     92          self.failUnlessEqual( td._info, ['param', ':', 'X', ':', 'a', 'b', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
     93        except pyutilib.common.ApplicationError:
    9694          pass
    9795
    9896    def test_read_array1(self):
    99         td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",param="X", format="array")
     97        td = DataManagerFactory('xls')
     98        td.initialize(currdir+"Book1.xls",range="TheRange", param="X", format="array")
    10099        try:
    101100          td.open()
    102101          td.read()
    103102          td.close()
    104           self.failUnlessEqual( td.data(), ['param', 'X', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
    105         except coopr.ApplicationError:
     103          self.failUnlessEqual( td._info, ['param', 'X', ':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
     104        except pyutilib.common.ApplicationError:
    106105          pass
    107106
    108107    def test_read_array2(self):
    109         td = TableData(filename=coopr_dir+"\\test\\acro\\unit\\Book1.xls",range="TheRange",param="X",format="transposed_array")
     108        td = DataManagerFactory('xls')
     109        td.initialize(currdir+"Book1.xls",range="TheRange",param="X",format="transposed_array")
    110110        try:
    111111          td.open()
    112112          td.read()
    113113          td.close()
    114           self.failUnlessEqual( td.data(), ['param', 'X', '(tr)',':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
    115         except coopr.ApplicationError:
     114          self.failUnlessEqual( td._info, ['param', 'X', '(tr)',':', 'bb', 'cc', 'dd', ':=', 'A1', 2.0, 3.0, 4.0, 'A5', 6.0, 7.0, 8.0, 'A9', 10.0, 11.0, 12.0, 'A13', 14.0, 15.0, 16.0])
     115        except pyutilib.common.ApplicationError:
    116116          pass
    117117
    118118    def test_error1(self):
    119         td = TableData(filename="bad")
     119        td = DataManagerFactory('xls')
     120        td.initialize("bad")
    120121        try:
    121122            td.open()
     
    125126
    126127    def test_error2(self):
    127         td = TableData()
     128        td = DataManagerFactory('xls')
    128129        try:
    129130            td.open()
    130131            self.fail("Expected IOError because no file specified")
    131         except IOError:
     132        except (IOError,AttributeError):
    132133            pass
    133134
    134135    def test_error3(self):
    135         td = TableData(filename=currdir+"display.txt")
    136         try:
     136        td = DataManagerFactory('txt')
     137        try:
     138            td.initialize(currdir+"display.txt")
    137139            td.open()
    138140            self.fail("Expected IOError because of bad file type")
    139         except IOError:
     141        except (IOError, AttributeError):
    140142            pass
    141143
    142144    def test_error4(self):
    143         td = TableData(filename=currdir+"dummy")
    144         try:
     145        td = DataManagerFactory('txt')
     146        try:
     147            td.initialize(filename=currdir+"dummy")
    145148            td.open()
    146149            self.fail("Expected IOError because of bad file type")
    147         except IOError:
     150        except (IOError, AttributeError):
    148151            pass
    149152
    150153    def test_error5(self):
    151         td = TableData(filename=example_dir+"D.tab", param="D", format="foo")
     154        td = DataManagerFactory('tab')
     155        td.initialize(example_dir+"D.tab", param="D", format="foo")
    152156        td.open()
    153157        try:
     
    157161            pass
    158162
    159     def test_error7(self):
    160         td = TableData(filename=example_dir+"D.tab", set="D", format="foo")
    161         td.open()
    162         try:
    163             td.read()
    164             self.fail("Expected IOError because of bad format")
    165         except ValueError:
    166             pass
    167 
    168 
    169163
    170164class PyomoModelData(unittest.TestCase):
     
    220214        try:
    221215            md.read(model)
    222         except ValueError:
     216        except (ValueError,IOError):
    223217            pass
    224218
     
    268262        model.A=Param(within=Boolean)
    269263        model.B=Param(within=Boolean)
    270         md.read(model)
     264        model.Z=Set()
     265        md.read(model)
     266        instance = model.create(md)
     267        #self.failUnlessEqual(instance.Z.data(), set(['foo[*]' 'bar' '[' '*' ']' 'bar[1,*,a,*]' 'foo-bar' 'hello-goodbye']))
     268
     269    def test_md11(self):
     270        cwd = os.getcwd()
     271        os.chdir(currdir)
     272        md = ModelData()
     273        md.add(currdir+"data11.dat")
     274        model=Model()
     275        model.A=Set()
     276        model.B=Set()
     277        model.C=Set()
     278        model.D=Set()
     279        md.read(model)
     280        os.chdir(cwd)
     281
     282
     283class TestTextImport(unittest.TestCase):
     284
     285    def test_tableA1(self):
     286        """Importing a single column of data"""
     287        pyutilib.misc.setup_redirect(currdir+'importA1.dat')
     288        print "import "+os.path.abspath(example_dir+'A.tab')+" format=set: A;"
     289        pyutilib.misc.reset_redirect()
     290        model=Model()
     291        model.A = Set()
     292        instance = model.create(currdir+'importA1.dat')
     293        self.failUnlessEqual(instance.A.data(), set(['A1', 'A2', 'A3']))
     294        os.remove(currdir+'importA1.dat')
     295
     296    def test_tableA2(self):
     297        """Importing a single column of data"""
     298        pyutilib.misc.setup_redirect(currdir+'importA2.dat')
     299        print "import "+os.path.abspath(example_dir+'A.tab')+" ;"
     300        pyutilib.misc.reset_redirect()
     301        model=Model()
     302        model.A = Set()
     303        try:
     304            instance = model.create(currdir+'importA2.dat')
     305            self.fail("Should fail because no set name is specified")
     306        except IOError:
     307            pass
     308        os.remove(currdir+'importA2.dat')
     309
     310    def test_tableA3(self):
     311        """Importing a single column of data"""
     312        pyutilib.misc.setup_redirect(currdir+'importA3.dat')
     313        print "import "+os.path.abspath(example_dir+'A.tab')+" : A ;"
     314        pyutilib.misc.reset_redirect()
     315        model=Model()
     316        model.A = Set()
     317        try:
     318            instance = model.create(currdir+'importA3.dat')
     319            self.fail("Should fail because no index is specified")
     320        except IOError:
     321            pass
     322        os.remove(currdir+'importA3.dat')
     323
     324    def test_tableB(self):
     325        """Same as test_tableA"""
     326        pyutilib.misc.setup_redirect(currdir+'importB.dat')
     327        print "import "+os.path.abspath(example_dir+'B.tab')+" format=set:B;"
     328        pyutilib.misc.reset_redirect()
     329        model=Model()
     330        model.B = Set()
     331        instance = model.create(currdir+'importB.dat')
     332        self.failUnlessEqual(instance.B.data(), set([1, 2, 3]))
     333        os.remove(currdir+'importB.dat')
     334
     335    def test_tableC(self):
     336        """Importing a multi-column table, where all columns are
     337        treated as values for a set with tuple values."""
     338        pyutilib.misc.setup_redirect(currdir+'importC.dat')
     339        print "import "+os.path.abspath(example_dir+'C.tab')+" format=set: C ;"
     340        pyutilib.misc.reset_redirect()
     341        model=Model()
     342        model.C = Set(dimen=2)
     343        instance = model.create(currdir+'importC.dat')
     344        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A1',2), ('A1',3), ('A2',1), ('A2',2), ('A2',3), ('A3',1), ('A3',2), ('A3',3)]))
     345        os.remove(currdir+'importC.dat')
     346
     347    def test_tableD(self):
     348        """Importing a 2D array of data as a set."""
     349        pyutilib.misc.setup_redirect(currdir+'importD.dat')
     350        print "import "+os.path.abspath(example_dir+'D.tab')+" format=set_array: C ;"
     351        pyutilib.misc.reset_redirect()
     352        model=Model()
     353        model.C = Set(dimen=2)
     354        instance = model.create(currdir+'importD.dat')
     355        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A2',2), ('A3',3)]))
     356        os.remove(currdir+'importD.dat')
     357
     358    def test_tableZ(self):
     359        """Importing a single parameter"""
     360        pyutilib.misc.setup_redirect(currdir+'importZ.dat')
     361        print "import "+os.path.abspath(example_dir+'Z.tab')+" format=param: Z ;"
     362        pyutilib.misc.reset_redirect()
     363        model=Model()
     364        model.Z = Param(default=99.0)
     365        instance = model.create(currdir+'importZ.dat')
     366        self.failUnlessEqual(instance.Z.value, 1.1)
     367        os.remove(currdir+'importZ.dat')
     368
     369    def test_tableY(self):
     370        """Same as tableXW."""
     371        pyutilib.misc.setup_redirect(currdir+'importY.dat')
     372        print "import "+os.path.abspath(example_dir+'Y.tab')+" : [A] Y;"
     373        pyutilib.misc.reset_redirect()
     374        model=Model()
     375        model.A = Set(initialize=['A1','A2','A3','A4'])
     376        model.Y = Param(model.A)
     377        instance = model.create(currdir+'importY.dat')
     378        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     379        self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     380        os.remove(currdir+'importY.dat')
     381
     382    def test_tableXW_1(self):
     383        """Importing a table, but only reporting the values for the non-index
     384        parameter columns.  The first column is assumed to represent an
     385        index column."""
     386        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     387        print "import "+os.path.abspath(example_dir+'XW.tab')+": [A] X W;"
     388        pyutilib.misc.reset_redirect()
     389        model=Model()
     390        model.A = Set(initialize=['A1','A2','A3','A4'])
     391        model.X = Param(model.A)
     392        model.W = Param(model.A)
     393        instance = model.create(currdir+'importXW.dat')
     394        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     395        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     396        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     397        os.remove(currdir+'importXW.dat')
     398
     399    def test_tableXW_2(self):
     400        """Like test_tableXW_1, except that set A is not defined."""
     401        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     402        print "import "+os.path.abspath(example_dir+'XW.tab')+": [A] X W;"
     403        pyutilib.misc.reset_redirect()
     404        model=Model()
     405        model.A = Set()
     406        model.X = Param(model.A)
     407        model.W = Param(model.A)
     408        instance = model.create(currdir+'importXW.dat')
     409        self.failUnlessEqual(instance.A.data(), set())
     410        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     411        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     412        os.remove(currdir+'importXW.dat')
     413
     414    def test_tableXW_3(self):
     415        """Like test_tableXW_1, except that set A is defined in the import statment."""
     416        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     417        print "import "+os.path.abspath(example_dir+'XW.tab')+": A=[A] X W;"
     418        pyutilib.misc.reset_redirect()
     419        model=Model()
     420        model.A = Set()
     421        model.X = Param(model.A)
     422        model.W = Param(model.A)
     423        instance = model.create(currdir+'importXW.dat')
     424        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3']))
     425        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     426        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     427        os.remove(currdir+'importXW.dat')
     428
     429    def test_tableXW_4(self):
     430        """Like test_tableXW_1, except that set A is defined in the import statment and all values are mapped."""
     431        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     432        print "import "+os.path.abspath(example_dir+'XW.tab')+": B=[A] R=X S=W;"
     433        pyutilib.misc.reset_redirect()
     434        model=Model()
     435        model.B = Set()
     436        model.R = Param(model.B)
     437        model.S = Param(model.B)
     438        instance = model.create(currdir+'importXW.dat')
     439        self.failUnlessEqual(instance.B.data(), set(['A1','A2','A3']))
     440        self.failUnlessEqual(instance.R.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     441        self.failUnlessEqual(instance.S.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     442        os.remove(currdir+'importXW.dat')
     443
     444    def test_tableT(self):
     445        """Importing a 2D array of parameters that are transposed."""
     446        pyutilib.misc.setup_redirect(currdir+'importT.dat')
     447        print "import "+os.path.abspath(example_dir+'T.tab')+" format=transposed_array : T;"
     448        pyutilib.misc.reset_redirect()
     449        model=Model()
     450        model.A = Set()
     451        model.B = Set()
     452        model.T = Param(model.A, model.B)
     453        instance = model.create(currdir+'importT.dat')
     454        self.failUnlessEqual(instance.T.data(), {('A2', 'I1'): 2.3, ('A1', 'I2'): 1.4, ('A1', 'I3'): 1.5, ('A1', 'I4'): 1.6, ('A1', 'I1'): 1.3, ('A3', 'I4'): 3.6, ('A2', 'I4'): 2.6, ('A3', 'I1'): 3.3, ('A2', 'I3'): 2.5, ('A3', 'I2'): 3.4, ('A2', 'I2'): 2.4, ('A3', 'I3'): 3.5})
     455        os.remove(currdir+'importT.dat')
     456
     457    def test_tableU(self):
     458        """Importing a 2D array of parameters."""
     459        pyutilib.misc.setup_redirect(currdir+'importU.dat')
     460        print "import "+os.path.abspath(example_dir+'U.tab')+" format=array : U;"
     461        pyutilib.misc.reset_redirect()
     462        model=Model()
     463        model.A = Set()
     464        model.B = Set()
     465        model.U = Param(model.A, model.B)
     466        instance = model.create(currdir+'importU.dat')
     467        self.failUnlessEqual(instance.U.data(), {('I2', 'A1'): 1.4, ('I3', 'A1'): 1.5, ('I3', 'A2'): 2.5, ('I4', 'A1'): 1.6, ('I3', 'A3'): 3.5, ('I1', 'A2'): 2.3, ('I4', 'A3'): 3.6, ('I1', 'A3'): 3.3, ('I4', 'A2'): 2.6, ('I2', 'A3'): 3.4, ('I1', 'A1'): 1.3, ('I2', 'A2'): 2.4})
     468        os.remove(currdir+'importU.dat')
     469
     470    def test_tableS(self):
     471        """Importing a table, but only reporting the values for the non-index
     472        parameter columns.  The first column is assumed to represent an
     473        index column.  A missing value is represented in the column data."""
     474        pyutilib.misc.setup_redirect(currdir+'importS.dat')
     475        print "import "+os.path.abspath(example_dir+'S.tab')+": [A] S ;"
     476        pyutilib.misc.reset_redirect()
     477        model=Model()
     478        model.A = Set(initialize=['A1','A2','A3','A4'])
     479        model.S = Param(model.A)
     480        instance = model.create(currdir+'importS.dat')
     481        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     482        self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5})
     483        os.remove(currdir+'importS.dat')
     484
     485    def test_tablePO(self):
     486        """Importing a table that has multiple indexing columns"""
     487        pyutilib.misc.setup_redirect(currdir+'importPO.dat')
     488        print "import "+os.path.abspath(example_dir+'PO.tab')+" : J=[A,B] P O;"
     489        pyutilib.misc.reset_redirect()
     490        model=Model()
     491        model.J = Set(dimen=2)
     492        model.P = Param(model.J)
     493        model.O = Param(model.J)
     494        instance = model.create(currdir+'importPO.dat')
     495        self.failUnlessEqual(instance.J.data(), set([('A3', 'B3'), ('A1', 'B1'), ('A2', 'B2')]) )
     496        self.failUnlessEqual(instance.P.data(), {('A3', 'B3'): 4.5, ('A1', 'B1'): 4.3, ('A2', 'B2'): 4.4} )
     497        self.failUnlessEqual(instance.O.data(), {('A3', 'B3'): 5.5, ('A1', 'B1'): 5.3, ('A2', 'B2'): 5.4})
     498        os.remove(currdir+'importPO.dat')
     499
     500
     501class TestCsvImport(unittest.TestCase):
     502
     503    def test_tableA1(self):
     504        """Importing a single column of data"""
     505        pyutilib.misc.setup_redirect(currdir+'importA1.dat')
     506        print "import "+os.path.abspath(csv_dir+'A.csv')+" format=set: A;"
     507        pyutilib.misc.reset_redirect()
     508        model=Model()
     509        model.A = Set()
     510        instance = model.create(currdir+'importA1.dat')
     511        self.failUnlessEqual(instance.A.data(), set(['A1', 'A2', 'A3']))
     512        os.remove(currdir+'importA1.dat')
     513
     514    def test_tableA2(self):
     515        """Importing a single column of data"""
     516        pyutilib.misc.setup_redirect(currdir+'importA2.dat')
     517        print "import "+os.path.abspath(csv_dir+'A.csv')+" ;"
     518        pyutilib.misc.reset_redirect()
     519        model=Model()
     520        model.A = Set()
     521        try:
     522            instance = model.create(currdir+'importA2.dat')
     523            self.fail("Should fail because no set name is specified")
     524        except IOError:
     525            pass
     526        os.remove(currdir+'importA2.dat')
     527
     528    def test_tableA3(self):
     529        """Importing a single column of data"""
     530        pyutilib.misc.setup_redirect(currdir+'importA3.dat')
     531        print "import "+os.path.abspath(csv_dir+'A.csv')+" : A ;"
     532        pyutilib.misc.reset_redirect()
     533        model=Model()
     534        model.A = Set()
     535        try:
     536            instance = model.create(currdir+'importA3.dat')
     537            self.fail("Should fail because no index is specified")
     538        except IOError:
     539            pass
     540        os.remove(currdir+'importA3.dat')
     541
     542    def test_tableB(self):
     543        """Same as test_tableA"""
     544        pyutilib.misc.setup_redirect(currdir+'importB.dat')
     545        print "import "+os.path.abspath(csv_dir+'B.csv')+" format=set:B;"
     546        pyutilib.misc.reset_redirect()
     547        model=Model()
     548        model.B = Set()
     549        instance = model.create(currdir+'importB.dat')
     550        self.failUnlessEqual(instance.B.data(), set([1, 2, 3]))
     551        os.remove(currdir+'importB.dat')
     552
     553    def test_tableC(self):
     554        """Importing a multi-column table, where all columns are
     555        treated as values for a set with tuple values."""
     556        pyutilib.misc.setup_redirect(currdir+'importC.dat')
     557        print "import "+os.path.abspath(csv_dir+'C.csv')+" format=set: C ;"
     558        pyutilib.misc.reset_redirect()
     559        model=Model()
     560        model.C = Set(dimen=2)
     561        instance = model.create(currdir+'importC.dat')
     562        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A1',2), ('A1',3), ('A2',1), ('A2',2), ('A2',3), ('A3',1), ('A3',2), ('A3',3)]))
     563        os.remove(currdir+'importC.dat')
     564
     565    def test_tableD(self):
     566        """Importing a 2D array of data as a set."""
     567        pyutilib.misc.setup_redirect(currdir+'importD.dat')
     568        print "import "+os.path.abspath(csv_dir+'D.csv')+" format=set_array: C ;"
     569        pyutilib.misc.reset_redirect()
     570        model=Model()
     571        model.C = Set(dimen=2)
     572        instance = model.create(currdir+'importD.dat')
     573        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A2',2), ('A3',3)]))
     574        os.remove(currdir+'importD.dat')
     575
     576    def test_tableZ(self):
     577        """Importing a single parameter"""
     578        pyutilib.misc.setup_redirect(currdir+'importZ.dat')
     579        print "import "+os.path.abspath(csv_dir+'Z.csv')+" format=param: Z ;"
     580        pyutilib.misc.reset_redirect()
     581        model=Model()
     582        model.Z = Param(default=99.0)
     583        instance = model.create(currdir+'importZ.dat')
     584        self.failUnlessEqual(instance.Z.value, 1.1)
     585        os.remove(currdir+'importZ.dat')
     586
     587    def test_tableY(self):
     588        """Same as tableXW."""
     589        pyutilib.misc.setup_redirect(currdir+'importY.dat')
     590        print "import "+os.path.abspath(csv_dir+'Y.csv')+" : [A] Y;"
     591        pyutilib.misc.reset_redirect()
     592        model=Model()
     593        model.A = Set(initialize=['A1','A2','A3','A4'])
     594        model.Y = Param(model.A)
     595        instance = model.create(currdir+'importY.dat')
     596        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     597        self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     598        os.remove(currdir+'importY.dat')
     599
     600    def test_tableXW_1(self):
     601        """Importing a table, but only reporting the values for the non-index
     602        parameter columns.  The first column is assumed to represent an
     603        index column."""
     604        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     605        print "import "+os.path.abspath(csv_dir+'XW.csv')+": [A] X W;"
     606        pyutilib.misc.reset_redirect()
     607        model=Model()
     608        model.A = Set(initialize=['A1','A2','A3','A4'])
     609        model.X = Param(model.A)
     610        model.W = Param(model.A)
     611        instance = model.create(currdir+'importXW.dat')
     612        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     613        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     614        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     615        os.remove(currdir+'importXW.dat')
     616
     617    def test_tableXW_2(self):
     618        """Like test_tableXW_1, except that set A is not defined."""
     619        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     620        print "import "+os.path.abspath(csv_dir+'XW.csv')+": [A] X W;"
     621        pyutilib.misc.reset_redirect()
     622        model=Model()
     623        model.A = Set()
     624        model.X = Param(model.A)
     625        model.W = Param(model.A)
     626        instance = model.create(currdir+'importXW.dat')
     627        self.failUnlessEqual(instance.A.data(), set())
     628        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     629        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     630        os.remove(currdir+'importXW.dat')
     631
     632    def test_tableXW_3(self):
     633        """Like test_tableXW_1, except that set A is defined in the import statment."""
     634        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     635        print "import "+os.path.abspath(csv_dir+'XW.csv')+": A=[A] X W;"
     636        pyutilib.misc.reset_redirect()
     637        model=Model()
     638        model.A = Set()
     639        model.X = Param(model.A)
     640        model.W = Param(model.A)
     641        instance = model.create(currdir+'importXW.dat')
     642        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3']))
     643        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     644        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     645        os.remove(currdir+'importXW.dat')
     646
     647    def test_tableXW_4(self):
     648        """Like test_tableXW_1, except that set A is defined in the import statment and all values are mapped."""
     649        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     650        print "import "+os.path.abspath(csv_dir+'XW.csv')+": B=[A] R=X S=W;"
     651        pyutilib.misc.reset_redirect()
     652        model=Model()
     653        model.B = Set()
     654        model.R = Param(model.B)
     655        model.S = Param(model.B)
     656        instance = model.create(currdir+'importXW.dat')
     657        self.failUnlessEqual(instance.B.data(), set(['A1','A2','A3']))
     658        self.failUnlessEqual(instance.R.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     659        self.failUnlessEqual(instance.S.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     660        os.remove(currdir+'importXW.dat')
     661
     662    def test_tableT(self):
     663        """Importing a 2D array of parameters that are transposed."""
     664        pyutilib.misc.setup_redirect(currdir+'importT.dat')
     665        print "import "+os.path.abspath(csv_dir+'T.csv')+" format=transposed_array : T;"
     666        pyutilib.misc.reset_redirect()
     667        model=Model()
     668        model.A = Set()
     669        model.B = Set()
     670        model.T = Param(model.A, model.B)
     671        instance = model.create(currdir+'importT.dat')
     672        self.failUnlessEqual(instance.T.data(), {('A2', 'I1'): 2.3, ('A1', 'I2'): 1.4, ('A1', 'I3'): 1.5, ('A1', 'I4'): 1.6, ('A1', 'I1'): 1.3, ('A3', 'I4'): 3.6, ('A2', 'I4'): 2.6, ('A3', 'I1'): 3.3, ('A2', 'I3'): 2.5, ('A3', 'I2'): 3.4, ('A2', 'I2'): 2.4, ('A3', 'I3'): 3.5})
     673        os.remove(currdir+'importT.dat')
     674
     675    def test_tableU(self):
     676        """Importing a 2D array of parameters."""
     677        pyutilib.misc.setup_redirect(currdir+'importU.dat')
     678        print "import "+os.path.abspath(csv_dir+'U.csv')+" format=array : U;"
     679        pyutilib.misc.reset_redirect()
     680        model=Model()
     681        model.A = Set()
     682        model.B = Set()
     683        model.U = Param(model.A, model.B)
     684        instance = model.create(currdir+'importU.dat')
     685        self.failUnlessEqual(instance.U.data(), {('I2', 'A1'): 1.4, ('I3', 'A1'): 1.5, ('I3', 'A2'): 2.5, ('I4', 'A1'): 1.6, ('I3', 'A3'): 3.5, ('I1', 'A2'): 2.3, ('I4', 'A3'): 3.6, ('I1', 'A3'): 3.3, ('I4', 'A2'): 2.6, ('I2', 'A3'): 3.4, ('I1', 'A1'): 1.3, ('I2', 'A2'): 2.4})
     686        os.remove(currdir+'importU.dat')
     687
     688    def test_tableS(self):
     689        """Importing a table, but only reporting the values for the non-index
     690        parameter columns.  The first column is assumed to represent an
     691        index column.  A missing value is represented in the column data."""
     692        pyutilib.misc.setup_redirect(currdir+'importS.dat')
     693        print "import "+os.path.abspath(csv_dir+'S.csv')+": [A] S ;"
     694        pyutilib.misc.reset_redirect()
     695        model=Model()
     696        model.A = Set(initialize=['A1','A2','A3','A4'])
     697        model.S = Param(model.A)
     698        instance = model.create(currdir+'importS.dat')
     699        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     700        self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5})
     701        os.remove(currdir+'importS.dat')
     702
     703    def test_tablePO(self):
     704        """Importing a table that has multiple indexing columns"""
     705        pyutilib.misc.setup_redirect(currdir+'importPO.dat')
     706        print "import "+os.path.abspath(csv_dir+'PO.csv')+" : J=[A,B] P O;"
     707        pyutilib.misc.reset_redirect()
     708        model=Model()
     709        model.J = Set(dimen=2)
     710        model.P = Param(model.J)
     711        model.O = Param(model.J)
     712        instance = model.create(currdir+'importPO.dat')
     713        self.failUnlessEqual(instance.J.data(), set([('A3', 'B3'), ('A1', 'B1'), ('A2', 'B2')]) )
     714        self.failUnlessEqual(instance.P.data(), {('A3', 'B3'): 4.5, ('A1', 'B1'): 4.3, ('A2', 'B2'): 4.4} )
     715        self.failUnlessEqual(instance.O.data(), {('A3', 'B3'): 5.5, ('A1', 'B1'): 5.3, ('A2', 'B2'): 5.4})
     716        os.remove(currdir+'importPO.dat')
     717
     718
     719
     720class TestSpreadsheet(unittest.TestCase):
     721
     722    def run(self, result=None):
     723        """ Disable the tests if win32com is not available """
     724        if not _win32com:
     725           return
     726        unittest.TestCase.run(self,result)
     727
     728    def test_tableA1(self):
     729        """Importing a single column of data"""
     730        pyutilib.misc.setup_redirect(currdir+'importA1.dat')
     731        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Atable format=set: A;"
     732        pyutilib.misc.reset_redirect()
     733        model=Model()
     734        model.A = Set()
     735        instance = model.create(currdir+'importA1.dat')
     736        self.failUnlessEqual(instance.A.data(), set(['A1', 'A2', 'A3']))
     737        os.remove(currdir+'importA1.dat')
     738
     739    def test_tableA2(self):
     740        """Importing a single column of data"""
     741        pyutilib.misc.setup_redirect(currdir+'importA2.dat')
     742        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Atable ;"
     743        pyutilib.misc.reset_redirect()
     744        model=Model()
     745        model.A = Set()
     746        try:
     747            instance = model.create(currdir+'importA2.dat')
     748            self.fail("Should fail because no set name is specified")
     749        except IOError:
     750            pass
     751        os.remove(currdir+'importA2.dat')
     752
     753    def test_tableA3(self):
     754        """Importing a single column of data"""
     755        pyutilib.misc.setup_redirect(currdir+'importA3.dat')
     756        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Atable : A ;"
     757        pyutilib.misc.reset_redirect()
     758        model=Model()
     759        model.A = Set()
     760        try:
     761            instance = model.create(currdir+'importA3.dat')
     762            self.fail("Should fail because no index is specified")
     763        except IOError:
     764            pass
     765        os.remove(currdir+'importA3.dat')
     766
     767    def test_tableB(self):
     768        """Same as test_tableA"""
     769        pyutilib.misc.setup_redirect(currdir+'importB.dat')
     770        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Btable format=set:B;"
     771        pyutilib.misc.reset_redirect()
     772        model=Model()
     773        model.B = Set()
     774        instance = model.create(currdir+'importB.dat')
     775        self.failUnlessEqual(instance.B.data(), set([1, 2, 3]))
     776        os.remove(currdir+'importB.dat')
     777
     778    def test_tableC(self):
     779        """Importing a multi-column table, where all columns are
     780        treated as values for a set with tuple values."""
     781        pyutilib.misc.setup_redirect(currdir+'importC.dat')
     782        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Ctable format=set: C ;"
     783        pyutilib.misc.reset_redirect()
     784        model=Model()
     785        model.C = Set(dimen=2)
     786        instance = model.create(currdir+'importC.dat')
     787        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A1',2), ('A1',3), ('A2',1), ('A2',2), ('A2',3), ('A3',1), ('A3',2), ('A3',3)]))
     788        os.remove(currdir+'importC.dat')
     789
     790    def test_tableD(self):
     791        """Importing a 2D array of data as a set."""
     792        pyutilib.misc.setup_redirect(currdir+'importD.dat')
     793        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Dtable format=set_array: C ;"
     794        pyutilib.misc.reset_redirect()
     795        model=Model()
     796        model.C = Set(dimen=2)
     797        instance = model.create(currdir+'importD.dat')
     798        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A2',2), ('A3',3)]))
     799        os.remove(currdir+'importD.dat')
     800
     801    def test_tableZ(self):
     802        """Importing a single parameter"""
     803        pyutilib.misc.setup_redirect(currdir+'importZ.dat')
     804        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Zparam format=param: Z ;"
     805        pyutilib.misc.reset_redirect()
     806        model=Model()
     807        model.Z = Param(default=99.0)
     808        instance = model.create(currdir+'importZ.dat')
     809        self.failUnlessEqual(instance.Z.value, 1.1)
     810        os.remove(currdir+'importZ.dat')
     811
     812    def test_tableY(self):
     813        """Same as tableXW."""
     814        pyutilib.misc.setup_redirect(currdir+'importY.dat')
     815        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Ytable : [A] Y;"
     816        pyutilib.misc.reset_redirect()
     817        model=Model()
     818        model.A = Set(initialize=['A1','A2','A3','A4'])
     819        model.Y = Param(model.A)
     820        instance = model.create(currdir+'importY.dat')
     821        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     822        self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     823        os.remove(currdir+'importY.dat')
     824
     825    def test_tableXW_1(self):
     826        """Importing a table, but only reporting the values for the non-index
     827        parameter columns.  The first column is assumed to represent an
     828        index column."""
     829        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     830        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=XWtable : [A] X W;"
     831        pyutilib.misc.reset_redirect()
     832        model=Model()
     833        model.A = Set(initialize=['A1','A2','A3','A4'])
     834        model.X = Param(model.A)
     835        model.W = Param(model.A)
     836        instance = model.create(currdir+'importXW.dat')
     837        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     838        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     839        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     840        os.remove(currdir+'importXW.dat')
     841
     842    def test_tableXW_2(self):
     843        """Like test_tableXW_1, except that set A is not defined."""
     844        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     845        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=XWtable : [A] X W;"
     846        pyutilib.misc.reset_redirect()
     847        model=Model()
     848        model.A = Set()
     849        model.X = Param(model.A)
     850        model.W = Param(model.A)
     851        instance = model.create(currdir+'importXW.dat')
     852        self.failUnlessEqual(instance.A.data(), set())
     853        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     854        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     855        os.remove(currdir+'importXW.dat')
     856
     857    def test_tableXW_3(self):
     858        """Like test_tableXW_1, except that set A is defined in the import statment."""
     859        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     860        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=XWtable: A=[A] X W;"
     861        pyutilib.misc.reset_redirect()
     862        model=Model()
     863        model.A = Set()
     864        model.X = Param(model.A)
     865        model.W = Param(model.A)
     866        instance = model.create(currdir+'importXW.dat')
     867        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3']))
     868        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     869        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     870        os.remove(currdir+'importXW.dat')
     871
     872    def test_tableXW_4(self):
     873        """Like test_tableXW_1, except that set A is defined in the import statment and all values are mapped."""
     874        pyutilib.misc.setup_redirect(currdir+'importXW.dat')
     875        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=XWtable: B=[A] R=X S=W;"
     876        pyutilib.misc.reset_redirect()
     877        model=Model()
     878        model.B = Set()
     879        model.R = Param(model.B)
     880        model.S = Param(model.B)
     881        instance = model.create(currdir+'importXW.dat')
     882        self.failUnlessEqual(instance.B.data(), set(['A1','A2','A3']))
     883        self.failUnlessEqual(instance.R.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     884        self.failUnlessEqual(instance.S.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     885        os.remove(currdir+'importXW.dat')
     886
     887    def test_tableT(self):
     888        """Importing a 2D array of parameters that are transposed."""
     889        pyutilib.misc.setup_redirect(currdir+'importT.dat')
     890        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Ttable format=transposed_array : T;"
     891        pyutilib.misc.reset_redirect()
     892        model=Model()
     893        model.A = Set()
     894        model.B = Set()
     895        model.T = Param(model.A, model.B)
     896        instance = model.create(currdir+'importT.dat')
     897        self.failUnlessEqual(instance.T.data(), {('A2', 'I1'): 2.3, ('A1', 'I2'): 1.4, ('A1', 'I3'): 1.5, ('A1', 'I4'): 1.6, ('A1', 'I1'): 1.3, ('A3', 'I4'): 3.6, ('A2', 'I4'): 2.6, ('A3', 'I1'): 3.3, ('A2', 'I3'): 2.5, ('A3', 'I2'): 3.4, ('A2', 'I2'): 2.4, ('A3', 'I3'): 3.5})
     898        os.remove(currdir+'importT.dat')
     899
     900    def test_tableU(self):
     901        """Importing a 2D array of parameters."""
     902        pyutilib.misc.setup_redirect(currdir+'importU.dat')
     903        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Utable format=array : U;"
     904        pyutilib.misc.reset_redirect()
     905        model=Model()
     906        model.A = Set()
     907        model.B = Set()
     908        model.U = Param(model.A, model.B)
     909        instance = model.create(currdir+'importU.dat')
     910        self.failUnlessEqual(instance.U.data(), {('I2', 'A1'): 1.4, ('I3', 'A1'): 1.5, ('I3', 'A2'): 2.5, ('I4', 'A1'): 1.6, ('I3', 'A3'): 3.5, ('I1', 'A2'): 2.3, ('I4', 'A3'): 3.6, ('I1', 'A3'): 3.3, ('I4', 'A2'): 2.6, ('I2', 'A3'): 3.4, ('I1', 'A1'): 1.3, ('I2', 'A2'): 2.4})
     911        os.remove(currdir+'importU.dat')
     912
     913    def test_tableS(self):
     914        """Importing a table, but only reporting the values for the non-index
     915        parameter columns.  The first column is assumed to represent an
     916        index column.  A missing value is represented in the column data."""
     917        pyutilib.misc.setup_redirect(currdir+'importS.dat')
     918        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=Stable : [A] S ;"
     919        pyutilib.misc.reset_redirect()
     920        model=Model()
     921        model.A = Set(initialize=['A1','A2','A3','A4'])
     922        model.S = Param(model.A)
     923        instance = model.create(currdir+'importS.dat')
     924        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     925        self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5})
     926        os.remove(currdir+'importS.dat')
     927
     928    def test_tablePO(self):
     929        """Importing a table that has multiple indexing columns"""
     930        pyutilib.misc.setup_redirect(currdir+'importPO.dat')
     931        print "import "+os.path.abspath(tutorial_dir+'excel.xls')+" range=POtable : J=[A,B] P O;"
     932        pyutilib.misc.reset_redirect()
     933        model=Model()
     934        model.J = Set(dimen=2)
     935        model.P = Param(model.J)
     936        model.O = Param(model.J)
     937        instance = model.create(currdir+'importPO.dat')
     938        self.failUnlessEqual(instance.J.data(), set([('A3', 'B3'), ('A1', 'B1'), ('A2', 'B2')]) )
     939        self.failUnlessEqual(instance.P.data(), {('A3', 'B3'): 4.5, ('A1', 'B1'): 4.3, ('A2', 'B2'): 4.4} )
     940        self.failUnlessEqual(instance.O.data(), {('A3', 'B3'): 5.5, ('A1', 'B1'): 5.3, ('A2', 'B2'): 5.4})
     941        os.remove(currdir+'importPO.dat')
     942
     943
     944class TestModelData(unittest.TestCase):
     945
     946    def test_tableA1(self):
     947        """Importing a single column of data"""
     948        md = ModelData()
     949        md.add(os.path.abspath(example_dir+'A.tab'), format='set', set='A')
     950        model=Model()
     951        model.A = Set()
     952        md.read(model)
     953        instance = model.create(md)
     954        self.failUnlessEqual(instance.A.data(), set(['A1', 'A2', 'A3']))
     955
     956    def test_tableA2(self):
     957        """Importing a single column of data"""
     958        md = ModelData()
     959        md.add(os.path.abspath(example_dir+'A.tab'))
     960        model=Model()
     961        model.A = Set()
     962        try:
     963            md.read(model)
     964            instance = model.create(md)
     965            self.fail("Should fail because no set name is specified")
     966        except IOError:
     967            pass
     968
     969    def test_tableA3(self):
     970        """Importing a single column of data"""
     971        md = ModelData()
     972        md.add(os.path.abspath(example_dir+'A.tab'), set='A')
     973        model=Model()
     974        model.A = Set()
     975        try:
     976            md.read(model)
     977            instance = model.create(md)
     978            self.fail("Should fail because no index is specified")
     979        except IOError:
     980            pass
     981
     982    def test_tableB(self):
     983        """Same as test_tableA"""
     984        md = ModelData()
     985        md.add(os.path.abspath(example_dir+'B.tab'), format='set', set='B')
     986        model=Model()
     987        model.B = Set()
     988        md.read(model)
     989        instance = model.create(md)
     990        self.failUnlessEqual(instance.B.data(), set([1, 2, 3]))
     991
     992    def test_tableC(self):
     993        """Importing a multi-column table, where all columns are
     994        treated as values for a set with tuple values."""
     995        md = ModelData()
     996        md.add(os.path.abspath(example_dir+'C.tab'), format='set', set='C')
     997        model=Model()
     998        model.C = Set(dimen=2)
     999        md.read(model)
     1000        instance = model.create(md)
     1001        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A1',2), ('A1',3), ('A2',1), ('A2',2), ('A2',3), ('A3',1), ('A3',2), ('A3',3)]))
     1002
     1003    def test_tableD(self):
     1004        """Importing a 2D array of data as a set."""
     1005        md = ModelData()
     1006        md.add(os.path.abspath(example_dir+'D.tab'), format='set_array', set='C')
     1007        model=Model()
     1008        model.C = Set(dimen=2)
     1009        md.read(model)
     1010        instance = model.create(md)
     1011        self.failUnlessEqual(instance.C.data(), set([('A1',1), ('A2',2), ('A3',3)]))
     1012
     1013    def test_tableZ(self):
     1014        """Importing a single parameter"""
     1015        md = ModelData()
     1016        md.add(os.path.abspath(example_dir+'Z.tab'), format='param', param='Z')
     1017        model=Model()
     1018        model.Z = Param(default=99.0)
     1019        md.read(model)
     1020        instance = model.create(md)
     1021        self.failUnlessEqual(instance.Z.value, 1.1)
     1022
     1023    def test_tableY(self):
     1024        """Same as tableXW."""
     1025        md = ModelData()
     1026        md.add(os.path.abspath(example_dir+'Y.tab'), index=['A'], param=['Y'])
     1027        model=Model()
     1028        model.A = Set(initialize=['A1','A2','A3','A4'])
     1029        model.Y = Param(model.A)
     1030        md.read(model)
     1031        instance = model.create(md)
     1032        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     1033        self.failUnlessEqual(instance.Y.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     1034
     1035    def test_tableXW_1(self):
     1036        """Importing a table, but only reporting the values for the non-index
     1037        parameter columns.  The first column is assumed to represent an
     1038        index column."""
     1039        md = ModelData()
     1040        md.add(os.path.abspath(example_dir+'XW.tab'), index=['A'], param=['X','W'])
     1041        model=Model()
     1042        model.A = Set(initialize=['A1','A2','A3','A4'])
     1043        model.X = Param(model.A)
     1044        model.W = Param(model.A)
     1045        md.read(model)
     1046        instance = model.create(md)
     1047        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     1048        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     1049        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     1050
     1051    def test_tableXW_2(self):
     1052        """Like test_tableXW_1, except that set A is not defined."""
     1053        md = ModelData()
     1054        md.add(os.path.abspath(example_dir+'XW.tab'), index=['A'], param=['X','W'])
     1055        model=Model()
     1056        model.A = Set()
     1057        model.X = Param(model.A)
     1058        model.W = Param(model.A)
     1059        md.read(model)
     1060        instance = model.create(md)
     1061        self.failUnlessEqual(instance.A.data(), set())
     1062        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     1063        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     1064
     1065    def test_tableXW_3(self):
     1066        """Like test_tableXW_1, except that set A is defined in the import statment."""
     1067        md = ModelData()
     1068        md.add(os.path.abspath(example_dir+'XW.tab'), index_name='A', index=['A'], param=['X','W'])
     1069        model=Model()
     1070        model.A = Set()
     1071        model.X = Param(model.A)
     1072        model.W = Param(model.A)
     1073        md.read(model)
     1074        instance = model.create(md)
     1075        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3']))
     1076        self.failUnlessEqual(instance.X.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     1077        self.failUnlessEqual(instance.W.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     1078
     1079    def test_tableXW_4(self):
     1080        """Like test_tableXW_1, except that set A is defined in the import statment and all values are mapped."""
     1081        md = ModelData()
     1082        md.add(os.path.abspath(example_dir+'XW.tab'), index_name='B', index=['A'], param=['X','W'], param_name={'X':'R', 'W':'S'})
     1083        model=Model()
     1084        model.B = Set()
     1085        model.R = Param(model.B)
     1086        model.S = Param(model.B)
     1087        md.read(model)
     1088        instance = model.create(md)
     1089        self.failUnlessEqual(instance.B.data(), set(['A1','A2','A3']))
     1090        self.failUnlessEqual(instance.R.data(), {'A1':3.3,'A2':3.4,'A3':3.5})
     1091        self.failUnlessEqual(instance.S.data(), {'A1':4.3,'A2':4.4,'A3':4.5})
     1092
     1093    def test_tableT(self):
     1094        """Importing a 2D array of parameters that are transposed."""
     1095        md = ModelData()
     1096        md.add(os.path.abspath(example_dir+'T.tab'), format='transposed_array', param='T')
     1097        model=Model()
     1098        model.A = Set()
     1099        model.B = Set()
     1100        model.T = Param(model.A, model.B)
     1101        md.read(model)
     1102        instance = model.create(md)
     1103        self.failUnlessEqual(instance.T.data(), {('A2', 'I1'): 2.3, ('A1', 'I2'): 1.4, ('A1', 'I3'): 1.5, ('A1', 'I4'): 1.6, ('A1', 'I1'): 1.3, ('A3', 'I4'): 3.6, ('A2', 'I4'): 2.6, ('A3', 'I1'): 3.3, ('A2', 'I3'): 2.5, ('A3', 'I2'): 3.4, ('A2', 'I2'): 2.4, ('A3', 'I3'): 3.5})
     1104
     1105    def test_tableU(self):
     1106        """Importing a 2D array of parameters."""
     1107        md = ModelData()
     1108        md.add(os.path.abspath(example_dir+'T.tab'), format='array', param='U')
     1109        model=Model()
     1110        model.A = Set()
     1111        model.B = Set()
     1112        model.U = Param(model.A, model.B)
     1113        md.read(model)
     1114        instance = model.create(md)
     1115        self.failUnlessEqual(instance.U.data(), {('I2', 'A1'): 1.4, ('I3', 'A1'): 1.5, ('I3', 'A2'): 2.5, ('I4', 'A1'): 1.6, ('I3', 'A3'): 3.5, ('I1', 'A2'): 2.3, ('I4', 'A3'): 3.6, ('I1', 'A3'): 3.3, ('I4', 'A2'): 2.6, ('I2', 'A3'): 3.4, ('I1', 'A1'): 1.3, ('I2', 'A2'): 2.4})
     1116
     1117    def test_tableS(self):
     1118        """Importing a table, but only reporting the values for the non-index
     1119        parameter columns.  The first column is assumed to represent an
     1120        index column.  A missing value is represented in the column data."""
     1121        md = ModelData()
     1122        md.add(os.path.abspath(example_dir+'S.tab'), index=['A'], param=['S'])
     1123        model=Model()
     1124        model.A = Set(initialize=['A1','A2','A3','A4'])
     1125        model.S = Param(model.A)
     1126        md.read(model)
     1127        instance = model.create(md)
     1128        self.failUnlessEqual(instance.A.data(), set(['A1','A2','A3','A4']))
     1129        self.failUnlessEqual(instance.S.data(), {'A1':3.3,'A3':3.5})
     1130
     1131    def test_tablePO(self):
     1132        """Importing a table that has multiple indexing columns"""
     1133        pyutilib.misc.setup_redirect(currdir+'importPO.dat')
     1134        print "import "+os.path.abspath(example_dir+'PO.tab')+" : J=[A,B] P O;"
     1135        pyutilib.misc.reset_redirect()
     1136        md = ModelData()
     1137        md.add(os.path.abspath(example_dir+'PO.tab'), index_name='J', index=['A','B'], param=['P','O'])
     1138        model=Model()
     1139        model.J = Set(dimen=2)
     1140        model.P = Param(model.J)
     1141        model.O = Param(model.J)
     1142        md.read(model)
     1143        instance = model.create(md)
     1144        self.failUnlessEqual(instance.J.data(), set([('A3', 'B3'), ('A1', 'B1'), ('A2', 'B2')]) )
     1145        self.failUnlessEqual(instance.P.data(), {('A3', 'B3'): 4.5, ('A1', 'B1'): 4.3, ('A2', 'B2'): 4.4} )
     1146        self.failUnlessEqual(instance.O.data(), {('A3', 'B3'): 5.5, ('A1', 'B1'): 5.3, ('A2', 'B2'): 5.4})
    2711147
    2721148
  • coopr.pyomo/stable/2.3/examples/pyomo/tutorials/table.py

    r2227 r2315  
    109109
    110110data = ModelData(model=model)
    111 data.add("tab/A.tab")
    112 data.add("tab/B.tab")
    113 data.add("tab/C.tab", set="C")
    114 data.add("tab/D.tab", set="D", format="set_array")
    115 data.add("tab/E.tab", set="E")
    116 data.add("tab/I.tab")
    117 data.add("tab/Z.tab", param="Z")
    118 data.add("tab/Y.tab")
    119 data.add("tab/XW.tab")
     111data.add("tab/A.tab", format='set', set='A')
     112data.add("tab/B.tab", format='set', set='B')
     113data.add("tab/C.tab", format='set', set="C")
     114data.add("tab/D.tab", format="set_array", set='D')
     115data.add("tab/E.tab", format='set', set="E")
     116data.add("tab/I.tab", format='set', set='I')
     117data.add("tab/Z.tab", format='param', param="Z")
     118data.add("tab/Y.tab", index=['A'], param=['Y'])
     119data.add("tab/XW.tab", index=['A'], param=['X','W'])
    120120data.add("tab/T.tab", param="T", format="transposed_array")
    121121data.add("tab/U.tab", param="U", format="array")
    122 data.add("tab/S.tab")
    123 data.add("tab/RQ.tab", index="H")
    124 data.add("tab/PO.tab", index="J", param=("P","O"))
    125 data.add("tab/PP.tab", param="PP")
     122data.add("tab/S.tab", index=['A'], param=['S'])
     123data.add("tab/RQ.tab", index_name="H", index=['H'], param=("R","Q"))
     124data.add("tab/PO.tab", index_name="J", index=('A','B'), param=("P","O"))
     125data.add("tab/PP.tab", index=('A','B'), param="PP")
    126126data.read()
    127127
Note: See TracChangeset for help on using the changeset viewer.