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