Jim
Jim

Reputation: 115

Why am I getting an unallowed character error in Pyomo with CPLEX?

I am trying to use CPLEX with a simple Pyomo example:

from pyomo.environ import *
model = ConcreteModel()
model.x = Var( initialize=-1.2, bounds=(-2, 2) )
model.y = Var( initialize= 1.0, bounds=(-2, 2) )
model.obj = Objective(
        expr= (1-model.x)**2 + 100*(model.y-model.x**2),
        sense= minimize )

opt = SolverFactory('cplex')
results = opt.solve(model)
print(results)

When I run this code, I get the following error:

ValueError: Unallowed character (:) found in CPLEX log file path/name. For portability reasons, only [a-zA-Z0-9 .-_] are allowed.

The only colon (:) in the path name is after the drive letter:

filename: C:\Users\USERNA~1\AppData\Local\Temp\tmpl8_ty0y5.cplex.log

The error is raised in CPLEX.py from the following:

def _validate_file_name(cplex, filename, description):
    """Validate filenames against the set of allowable characters in CPLEX.

    Returns the filename, possibly enclosed in double-quotes, or raises
    a ValueError is unallowable characters are found.

    """
    if filename is None:
        return filename
    matches = _validate_file_name.illegal_characters.search(filename)
    if matches:
        raise ValueError(
            "Unallowed character (%s) found in CPLEX %s file path/name.\n\t"
            "For portability reasons, only [%s] are allowed. Filename: %s"
            % (matches.group(), description,
               _validate_file_name.allowed_characters.replace("\\",''),filename))
    # CPLEX only supports quoting spaces starting in v12.8.
    if ' ' in filename:
        if cplex.version()[:2] >= (12,8):
            filename = '"'+filename+'"'
        else:
            raise ValueError(
                "Space detected in CPLEX %s file path/name\n\t%s\nand "
                "CPLEX older than version 12.8.  Please either upgrade "
                "CPLEX or remove the space from the %s path."
                % (description, filename, description))
    return filename
_validate_file_name.allowed_characters = r"a-zA-Z0-9 \.\-_\%s" % (os.path.sep,)
_validate_file_name.illegal_characters = re.compile(
    '[^%s]' % (_validate_file_name.allowed_characters,))

If I comment out the validation for the log file, I get the same error for the solution and LP files.

I have not seen this error anywhere else. Can anyone help me?

Thanks.

Upvotes: 3

Views: 365

Answers (2)

jsiirola
jsiirola

Reputation: 2634

This was a bug introduced into the CPLEX interface in Pyomo 5.6, and was resolved in the Pyomo 5.6.1 release.

Upvotes: 2

MENG
MENG

Reputation: 11

Recently, I have the same problem. Based on the comment by Qi Chen, when the part of 'if matches...' has been commented out, it works well. Thanks for @Qi chen.

In CPLEX.py, comments out this lines:

# if matches:
    #raise ValueError(
    #    "Unallowed character (%s) found in CPLEX %s file path/name.\n\t"
    #    "For portability reasons, only [%s] are allowed."
    #    % (matches.group(), description,
    #       _validate_file_name.allowed_characters.replace("\\",'')))
# CPLEX only supports quoting spaces starting in v12.8.

I guess that it might be caused by log file(xxx.CPLEX.log) in which ':' exists as the following: 'C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\bin\x64_win64\CPLEX.exe' or 'C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\bin\x64_win64\CPLEX.exe'

Upvotes: 1

Related Questions