0

Linear program:

Decision Variables:
x[]

Maximize:  Sum(i to n) (fare[i] * x[i])

subject to:
x[i] <= demand[i]

I am trying to add a linear constraint in cplex using Python, but I am not able to get the syntax correct.

fare = [400.0, 700.0, 600.0]
dmd= [2, 4, 3]
itins = []
for count in range(3):
        i = Itinerary(count, 1, dmd[count], fare[count])
        itins.append(i)


# problem variables
X=[] * len(itins)

def setupproblem(c):

    c.objective.set_sense(c.objective.sense.maximize)
    num_x = range(len(itins))
    print (num_x)

    varnames = ["X"+str(j) for j in range(len(itins))]
    c.variables.add(names = varnames, lb=[0.0, 0, 0], ub=[10.0, 10, 10])
    x = [c.variables.add(obj=fare)  for i in num_x]

 i_iten = range(len(itins))

    c.linear_constraints.add(lin_expr  = [cplex.SparsePair(ind = i_iten,
                                                     val = X[i])
                                          for i in range(len(itins) -1 )],
                             senses = ["L"],
                             rhs   = capacity,
                             names = ["capacity_"+str(i)
                                      for i in i_iten])

I am getting this error:

raise CplexError(" %d: Invalid name -- '%s'\n" % tuple(self._mat))
cplex.exceptions.errors.CplexError:  1210: Invalid name -- 'X'
4
  • Can you write a linear program that you are trying to solve? Commented Jan 8, 2016 at 12:24
  • @serge_k : Added the linear problem. It trying to maximize the revenue (fare * number) subject to constraints. Commented Jan 8, 2016 at 13:13
  • You have strange bounds type when adding variables: the first elements in lb and ub are float and the others are int. Are x[1] and x[2] assumed to be integer types? In that case you need to specify types as by default variables are initialized as continuous. Commented Jan 8, 2016 at 14:36
  • Thanks serge_k, its just to get it working. Im trying to get my hands on the python APIs. But yes, they are assumed to be integer types. Commented Jan 11, 2016 at 4:23

2 Answers 2

2

In cplex.SparcePair you need to specify nonzero elements under val and the corresponding variables under ind. Plus, from your linear program your right-hand side should be dmd.

c.linear_constraints.add(lin_expr  = [cplex.SparsePair(ind = xname,
                                                 val = [1.0])
                                      for xname in varnames],
                         senses = ["L"],
                         rhs   = dmd,
                         names = ["capacity_"+str(i)
                                  for i in i_iten])

Also I would suggest to indicate the objective function when you add variables:

c.variables.add(obj = fare, names = varnames, lb=[0.0, 0, 0], ub=[10.0, 10, 10])
Sign up to request clarification or add additional context in comments.

2 Comments

Tried this, but it says raise CplexError("Inconsistent arguments")
I got it working. very slight modification. c.linear_constraints.add(lin_expr=[cplex.SparsePair(ind=[xname], val=[1.0]) for xname in varn], senses=["L"] * len(varnames), rhs=dmd) But facing a weird problem. The line of code to add a new linear constraint works fine in the python interactive. But when i run the code through a file it fails. Any ideas?
2

c.linear_constraints.add(lin_expr=[cplex.SparsePair(ind=[xname], val=[1.0]) for xname in varn], senses=["L"] * len(varnames), rhs=dmd)

But before you add the constraints on the variables, please keep in mind that the variable names should be added to the function. I spent 4 hours going round and round to figure out what was going wrong.

This line should come first. c.variables.add(varnames)

Thanks serge_k for your initial pointers !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.