1

I would really appreciate any input for this optimization problem:

enter image description here

I am failing to assign my variables the values 1,2,3,4,5 for i and j, I think that is why I am getting this error:

Dual infeasible due to empty column x1. 

Am I on the right track?


import cplex
import docplex

col_vars = ["x1", "x2", "x3", "x4", "x5"]
variables= len(col_vars)
numberofx= 5
constraintNames=["xixj"]

from docplex.mp.model import Model

mdl = Model(name="Homework10_2", log_output=True)

x=mdl.continuous_var_list(numberofx)

myObjective=mdl.sum(x[i] for i in range(numberofx))
print(myObjective)
mdl.maximize(myObjective)
   
for j in range(numberofx): 
    if  (x[j] != x[i] for i in range(variables)):
        continue
        myConstraint=mdl.sum(x[j]+x[i] for i in range(variables))<=1
        mdl.add(myConstraint, name=constraintNames[j])
    #mdl.add(x[j] != x[i] for i in range(variables))

mdl.export_as_lp("Homework10.lp")

mdl.solve()
mdl.print_solution()
Tonechas
  • 13,398
  • 16
  • 46
  • 80
jopro
  • 11
  • 1

1 Answers1

0

Actually, your code has some serious issues. First, the test:

   if  (x[j] != x[i] for i in range(variables)):

is wrong as it tests the boolean value of a Python generator (look it up in Python doc). So this test is always true, but then it finds the continue instruction and starts over, without posting any constraint.

If the idea is to post a constraint that every pair of different variables is less than 1, you should use indices to reference variables, and test for index equality or difference, not variables. DOcplex variables are complex Python objects, for which operators such as == or != has been overloaded with a nonstandard behavior, in short return constraint objects.

In your case, a simple solution is to loop twice over indices:

n_vars = len(x)
for i in range(n_vars):
    for j in range(n_vars):
        if i < j:
            mdl.add( x[i] + x[j] <= 1)

Note that I avoid posting two constraint for every pair (i,j) of different indices with the i < j test.

The second constraint x >= 0 is implicit as the default lower bound for variables is 0.

Philippe Couronne
  • 826
  • 1
  • 5
  • 6