Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  cplex 12.3 python api variable sense

    Posted 10/11/11 03:22 PM

    Originally posted by: LJXue


    I am using cplex 12.3 with python 2.7 under Windows 7. Everything is in 64 bit.

    I have the following issue which is suspicious to be a bug. In the example, I create a problem with 3 variables, and add another variable later on. Although I specify the variable name, the variable type does not match the sequence I specify.

    1. create the model
    A = [http://-1.0, 1.0, 1.0, http://1.0, -3.0, 1.0, http://0.0, 1.0, 0.0]
    rhs = http://20.0, 30.0, 0.0
    cSense =
    nCon = 3
    var_names =

    obj = http://1.0, 2.0, 3.0
    ub = http://40.0, cplex.infinity, cplex.infinity
    prob.variables.add(obj = obj, ub = ub, types = 'CCC', names = var_names)

    const_expr = []
    for i in range(nCon):
    temp = [var_names, Ai]
    const_expr.append(temp)

    prob.linear_constraints.add(lin_expr = const_expr, senses = cSense, rhs = rhs)

    qmat = [[0, 1, http://-33.0, 6.0],
    [0, 1, 2, http:// 6.0, -22.0, 11.5],
    [1, 2, http:// 11.5, -11.0]]

    prob.objective.set_quadratic(qmat)
    print(prob.variables.get_names())
    print(prob.variables.get_types())
    1. output:
    #
    #

    1. add an additional variable
    prob.variables.add(obj = http://1.0, lb = http://0.0, ub = http://3.0, types = 'I',
    names = ,
    columns = [[0, 2, http://10.0, -3.5]])
    print(prob.variables.get_names())
    print(prob.variables.get_types())
    #output
    #
    1. # this is not right, x4 should be integer variable

    Currently, a workaround I use is not to specify the variable type in add variable statement
    and change it via

    prob.variables.set_types()

    I wonder if there is any mistake in my statements, or it is a bug. Thank you very much.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: cplex 12.3 python api variable sense

    Posted 10/11/11 05:35 PM

    Originally posted by: LJXue


    sorry, seems there are some formatting issues.
    #create the model
     
    import cplex
     
    prob = cplex.Cplex()
    prob.set_problem_name('Example')
    prob.objective.set_sense(prob.objective.sense.maximize)
     
     
    A = [[-1.0, 1.0, 1.0], [1.0, -3.0, 1.0], [0.0, 1.0, 0.0]]
    rhs = [20.0, 30.0, 0.0]
    cSense = ['LLE']
    nCon = 3
    var_names = ['x' + str(i + 1) for i in range(nCon)]
     
    obj = [1.0, 2.0, 3.0]
    ub = [40.0, cplex.infinity, cplex.infinity]
    prob.variables.add(obj = obj, ub = ub, types = 'CCC', names = var_names)
     
    const_expr = []
    for i in range(nCon):
        temp = [var_names, A[i]]
        const_expr.append(temp)
     
    prob.linear_constraints.add(lin_expr = const_expr, senses = cSense, rhs = rhs)
     
    qmat = [[[0, 1], [-33.0, 6.0]],
            [[0, 1, 2], [ 6.0, -22.0, 11.5]],
            [[1, 2], [ 11.5, -11.0]]]
     
    prob.objective.set_quadratic(qmat)
     
    print(prob.variables.get_names())
    print(prob.variables.get_types())
    #output
    #['x1', 'x2', 'x3']
    #['C', 'C', 'C']
     
    # add another variable
    prob.variables.add(obj = [1.0], lb = [0.0], ub = [3.0], types = 'I',
                       names = ['x4'],
                       columns = [[[0, 2], [10.0, -3.5]]])
    print(prob.variables.get_names())
    print(prob.variables.get_types())
    #output
    #['x1', 'x2', 'x3', 'x4']
    #['I', 'C', 'C', 'C'] # this is not right, x4 should be integer variable
    

    Currently, a workaround I use is not to specify the variable type in add variable statement
    and change it via
    prob.variables.set_types()
    

    I wonder if there is any mistake in my statements, or it is a bug. Thank you very much.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: cplex 12.3 python api variable sense

    Posted 10/12/11 04:38 PM

    Originally posted by: SystemAdmin


    Hello,

    Thank you for your report, this is indeed a bug in the Python interface code. When variables are added to the problem without new columns in the constraint matrix, this bug is not triggered.

    If the workaround that you have identified is not adequate, please contact me at pstarhill (at) us (dot) ibm (dot) com.

    Philip Starhill
    CPLEX Research Engineer
    #CPLEXOptimizers
    #DecisionOptimization