0

I have defined a continuous variable as follows:

#Define sets
X = [(i, j) for i in range(1, a + 1) for j in range(1, b + 1)]

# Define variables
x = m.continuous_var_dict(X, name="x", lb=0, ub=1)

Is it possible to convert it to binary variable, possible in the most efficient way computationally?

diabolik
  • 55
  • 6

1 Answers1

0

If you are using continuous_var_dict then you have chosen the variable to be continuous. Use

binary_var_dict(keys, lb=None, ub=None, name=None, key_format=None) 

Instead.

  • I found that it does not convert variable from continuous to binary. Instead, it defines a new variable, and would be activated only if I redefine all the model (all the constraints, the objective, etc.). However, it would be too inefficient for me as I have too many constraints. – diabolik Feb 20 '23 at 16:18
  • Btw, just to clarify, I am not asking how to define binary variables, I am asking how to convert continuous variables to binary variables. I know how to define binary variables. – diabolik Feb 20 '23 at 16:23
  • Please say a bit more. What are you exactly trying to do? And why? If you are using continuous variables, you are already taking memory and other burdens. Now you cannot change the type of data suddenly. You have to define a new binary variable. If you share more details it would be easier to help. – Subhankar Ghosal Feb 20 '23 at 16:38
  • Sure, I am solving a problem using linear relaxation at the initial phase. Hence, I need continuous variables at the beginning. Then I need to convert some continuous variables to binary and solve the same problem in that way. If I need to redefine the model, it can be very inefficient as I have many constraints. – diabolik Feb 20 '23 at 16:43
  • @diabolik But when changing some continuous variable to binary, you reassign memory. So what you are asking is equivalent to redefining in this case. (Binary are specially allocated in memory. I don't think it would differ anyhow.) – Subhankar Ghosal Feb 20 '23 at 17:04
  • @diabolik, while changing the variable type, you are reassigning memory. If you change the variable type in a list or dict then some intelligent reallocation can happen. But I searched the documentation and did not find any redefinition routine. You may look into non-python codes if such a thing exists there. – Subhankar Ghosal Feb 20 '23 at 17:08
  • There is this discussion, talking about .set_vartype, however I tried and it did not work for me, and raises the error "'dict' object has no attribute 'set_vartype'". I could not find any proper documentation either. https://stackoverflow.com/questions/65485310/how-to-convert-a-float-to-a-binary-variable-in-docplex – diabolik Feb 20 '23 at 17:13
  • check here https://ibmdecisionoptimization.github.io/docplex-doc/mp/index.html – Subhankar Ghosal Feb 20 '23 at 18:15
  • @diabolik set_vartype is for internal usage. not much is in the documentation. – Subhankar Ghosal Feb 20 '23 at 18:25
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 24 '23 at 02:00