0

I'm using CPLEX Python API to optimize a problem with many variables (90 variable problem)

I have set up my constraints in my sense's and my rhs as:

my_senses = 'GG'
my_rhs = [0.1, 0.1]

However I want to change my first constraint (which is the sum of all variables must be greater than 0 and less than 0.15) so that instead of Greater "G" than 0.1 to a range Range between 0 - 0.15

I can't find any thing detailing the correct syntax to do this. I have tried the following to no success:

my_senses = 'RG'
my_rhs = [[0.0,0.15], 0.1]
DGradeci
  • 39
  • 9

1 Answers1

0

L is the letter for LESS.

Small cplex python example out of the zoo example

# Import packages.
import cplex
my_prob = cplex.Cplex()
my_obj = [500, 400]
my_ub = [cplex.infinity, cplex.infinity]
my_lb = [0.0, 0.0]
my_ctype = "II"
my_colnames = ["nbBus40", "nbBus30"]
my_rhs = [290, 285]
my_rownames = ["nbKids","nbKids2"]
my_sense = ["L","G"]
my_prob.objective.set_sense(my_prob.objective.sense.minimize)
my_prob.variables.add(obj=my_obj, lb=my_lb, ub=my_ub, types=my_ctype,
                       names=my_colnames)
rows = [[["nbBus40", "nbBus30"], [40,30]],[["nbBus40", "nbBus30"], [40,30]]]
my_prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
                                rhs=my_rhs, names=my_rownames)
my_prob.solve()
print("cost  = ", my_prob.solution.get_objective_value())
numcols = my_prob.variables.get_num()
    
sol = my_prob.solution.get_values()
for j in range(numcols):
    print(my_colnames[j],"  = ", sol[j])

or use "R" for range:

# Import packages.
import cplex
my_prob = cplex.Cplex()
my_obj = [500, 400]
my_ub = [cplex.infinity, cplex.infinity]
my_lb = [0.0, 0.0]
my_ctype = "II"
my_colnames = ["nbBus40", "nbBus30"]
my_rhs = [285]
my_range = [5]
my_rownames = ["nbKids"]
my_sense = ["R"]
my_prob.objective.set_sense(my_prob.objective.sense.minimize)
my_prob.variables.add(obj=my_obj, lb=my_lb, ub=my_ub, types=my_ctype,
                       names=my_colnames)
rows = [[["nbBus40", "nbBus30"], [40,30]]]
my_prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
                                rhs=my_rhs,range_values=my_range, names=my_rownames)
my_prob.solve()
print("cost  = ", my_prob.solution.get_objective_value())
numcols = my_prob.variables.get_num()
    
sol = my_prob.solution.get_values()
for j in range(numcols):
    print(my_colnames[j],"  = ", sol[j])

but I would recommend the docplex api that is also python but much higher level

And then you would write

from docplex.mp.model import Model

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_range(285,(nbbus40*40 + nbbus30*30),290,"kids")
mdl.minimize(nbbus40*500 + nbbus30*400)

mdl.solve(log_output=True,)

mdl.export("c:\\temp\\buses.lp")

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Thanks, I understand that "L" is the sign for less than but I was wondering how you would put a range "R" in the above example. For example if your constraint is that all variables need to add up to a number between x & y – DGradeci Feb 02 '21 at 15:50
  • 1
    Then use "R" for range. I updated my example with that. – Alex Fleischer Feb 02 '21 at 16:01