cplex python sum constraint -
i started cplex python api , got problem creating linear_constraints model
i want that:
dvar float+ x[] minimize: sum(i in i) c[i] * x[i] subject to: sum(i in i) x[i] <= constantvalue
and problem dont know how make constraint in python api
cpx.linear_constraints.add( lin_expr= 1, senses=["l"], rhs=constantvalue, range_values= 2,
what want ask u need type in 1) , 2) sum of x[i] table need decision variable.
here example:
>>> import cplex >>> c = cplex.cplex() >>> c.variables.add(names = ["x1", "x2", "x3"]) >>> c.linear_constraints.add(lin_expr = [cplex.sparsepair(ind = ["x1", "x3"], val = [1.0, -1.0]), cplex.sparsepair(ind = ["x1", "x2"], val = [1.0, 1.0]), cplex.sparsepair(ind = ["x1", "x2", "x3"], val = [-1.0] * 3), cplex.sparsepair(ind = ["x2", "x3"], val = [10.0, -2.0])], senses = ["e", "l", "g", "r"], rhs = [0.0, 1.0, -1.0, 2.0], range_values = [0.0, 0.0, 0.0, -10.0], names = ["c0", "c1", "c2", "c3"],) >>> c.linear_constraints.get_rhs() [0.0, 1.0, -1.0, 2.0]
where range_values list of floats, specifying difference between lefthand side , righthand side of each linear constraint. if range_values[i] > 0 (zero) constraint defined rhs[i] <= rhs[i] + range_values[i]. if range_values[i] < 0 (zero) constraint defined rhs[i] + range_value[i] <= a*x <= rhs[i]. suggest leave default value (blank).
to define sum indicate variables , vector of ones, e.g.,
numcols = 10 vars = [ 'x'+str(n) n in xrange(1,numcols+1) ] coef = [1]*numcols cpx.linear_constraints.add( lin_expr= [cplex.sparsepair(ind = vars, val = coef)] , senses=["l"], rhs=[constantvalue] )
Comments
Post a Comment