Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/gams-writer' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
qtothec committed Jun 9, 2017
2 parents be2d8f1 + 3054e31 commit db7b142
Show file tree
Hide file tree
Showing 7 changed files with 633 additions and 417 deletions.
7 changes: 6 additions & 1 deletion pyomo/core/base/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ class ComponentData(_ComponentBase):
__pickle_slots__ = ('_component',)
__slots__ = __pickle_slots__ + ('__weakref__',)

labeler = []

def __init__(self, owner):
#
# ComponentData objects are typically *private* objects for
Expand Down Expand Up @@ -685,7 +687,10 @@ def to_string(self, ostream=None, verbose=None, precedence=0):
"""Write the component name and index to a buffer"""
if ostream is None:
ostream = sys.stdout
ostream.write(self.__str__())
if not ComponentData.labeler:
ostream.write(self.__str__())
else:
ostream.write(ComponentData.labeler[-1](self))

def getname(self, fully_qualified=False, name_buffer=None):
"""Return a string with the component name and index"""
Expand Down
66 changes: 51 additions & 15 deletions pyomo/core/base/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
# ___________________________________________________________________________

__all__ = ['CounterLabeler', 'NumericLabeler', 'CNameLabeler', 'TextLabeler',
'AlphaNumTextLabeler','NameLabeler', 'CuidLabeler']
'AlphaNumericTextLabeler','NameLabeler', 'CuidLabeler']

import six
if six.PY3:
_string = str
_translate = str.translate
else:
import string as _string
import string
_translate = string.translate

from pyomo.core.base.component import ComponentUID

# This module provides some basic functionality for generating labels
Expand All @@ -24,30 +26,64 @@
# optimization input file formats, e.g., CPLEX LP files. The purpose of
# this module is to provide a simple remap function, that will satisfy
# broadly problematic symbols. if solver-specific remaps are required,
# they should be handled either in the corresponding solver plugin.

# NOTE: Simple single-character substitutions should be handled by adding
# to the translation table constructed below - first argument is the
# "from" characters, second argument is the "to" characters.
cpxlp_translation_table = _string.maketrans("[]{} -#$%&*+.,/;<=>?@^!~':",
"()()______________________")
# they should be handled in the corresponding solver plugin.

class _CharMapper(object):
def __init__(self, preserve, translate, other):
"""
Arguments::
preserve: a string of characters to preserve
translate: a dict or key/value list of characters to translate
other: the character to return for all characters not in
preserve or translate
"""
self.table = dict(
(k if isinstance(k, int) else ord(k), v)
for k,v in six.iteritems(dict(translate)) )
for c in preserve:
_c = ord(c)
if _c in self.table and self.table[_c] != c:
raise RuntimeError("Duplicate character '%s' appears in both "
"translate table and preserve list" % (c,))
self.table[_c] = c
self.other = other

def __getitem__(self, c):
# Most of the time c should be known. For the rare cases we
# encounter a new character, remember it by adding a new entry
# into the translation table and return the default character
try:
return self.table[c]
except:
self.table[c] = self.other
return self.other

def make_table(self):
return ''.join(self[i] for i in range(256))

_alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ'
_digit = '1234567890'
_cpxlp_translation_table = _CharMapper( preserve=_alpha+_digit+'()_',
translate = zip('[]{}', '()()'),
other='_' ).make_table()
def cpxlp_label_from_name(name):

if name is None:
raise RuntimeError("Illegal name=None supplied to "
"cpxlp_label_from_name function")

return _string.translate(name, cpxlp_translation_table)
return _translate(name, _cpxlp_translation_table)

alphanum_translation_table = _string.maketrans("()[]{} -#$%&*+.,/;<=>?@^!~':",
"____________________________")
_alphanum_translation_table = _CharMapper( preserve=_alpha+_digit+'_',
translate = {},
other='_' ).make_table()
def alphanum_label_from_name(name):

if name is None:
raise RuntimeError("Illegal name=None supplied to "
"alphanum_label_from_name function")

return _string.translate(name, alphanum_translation_table)
return _translate(name, _alphanum_translation_table)

class CuidLabeler(object):

Expand Down Expand Up @@ -95,7 +131,7 @@ def __init__(self):
def __call__(self, obj):
return cpxlp_label_from_name(obj.getname(True, self.name_buffer))

class AlphaNumTextLabeler(object):
class AlphaNumericTextLabeler(object):
def __init__(self):
self.name_buffer = {}

Expand Down
14 changes: 9 additions & 5 deletions pyomo/opt/base/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
# pyomo.opt.colin.OptProblem (this can wrap a COLIN shell
# command, or provide a runtime optimization problem)
# bar - A Baron input file
# gams - A GAMS input file
#
ProblemFormat = Enum('colin', 'pyomo', 'cpxlp', 'nl', 'mps', 'mod', 'lpxlp', 'osil', 'colin_optproblem', 'FuncDesigner','bar')
ProblemFormat = Enum('colin', 'pyomo', 'cpxlp', 'nl', 'mps', 'mod', 'lpxlp', 'osil', 'colin_optproblem', 'FuncDesigner','bar','gams')

#
# osrl - osrl XML file defined by the COIN-OR OS project: Result
Expand All @@ -52,6 +53,9 @@ def guess_format(filename):
formats['mod']=ProblemFormat.mod
formats['lp']=ProblemFormat.cpxlp
formats['osil']=ProblemFormat.osil
formats['gms']=ProblemFormat.gams
formats['gams']=ProblemFormat.gams

formats['sol']=ResultsFormat.sol
formats['osrl']=ResultsFormat.osrl
formats['soln']=ResultsFormat.soln
Expand All @@ -60,7 +64,7 @@ def guess_format(filename):
formats['jsn']=ResultsFormat.json
formats['json']=ResultsFormat.json
formats['results']=ResultsFormat.yaml
for fmt in formats:
if filename.endswith('.'+fmt):
return formats[fmt]
return None
if filename:
return formats.get(filename.split('.')[-1].strip(), None)
else:
return None
1 change: 1 addition & 0 deletions pyomo/repn/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ def load():
import pyomo.repn.plugins.ampl
import pyomo.repn.plugins.baron_writer
import pyomo.repn.plugins.mps
import pyomo.repn.plugins.gams_writer

Loading

0 comments on commit db7b142

Please sign in to comment.