-1
mymodel = Model("OPL_Purchasing_plan");
nr_month= range(0, 12)
na_pur_req = [550, 750, 6500, 675, 210, 120, 0, 560, 140, 320, 0, 100]
na_average_cost = 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5]

for i in nr_month:
    if(na_pur_req[i] == 0):
        mymodel.add_constraint(na_actual_cost_test[0][i] <= na_actual_suply[0][i] * 0,'con9');
     
    else:
        mymodel.add_constraint(na_actual_cost_test[0][i] <= na_actual_suply[0][i] * na_average_cost[i],'con10');

I am getting an error as follows

TypeError: Cannot use == to test expression equality, try using Python is operator or method equals

Why I am getting this error.

suresh_chinthy
  • 377
  • 2
  • 12
  • Your code does not compile and also you did not include the full backtrace, so we have no idea where your code may fail. Please provide a code that compiles and runs (your code has obvious syntax errors). Please also provide the backtrace so that we can identify the statement that fails. – Daniel Junglas Aug 14 '20 at 11:58

1 Answers1

0

A DOcplex expression, built from decision variables and constants, is itself a decision object. Its value is defined only after a solve(), and can be queried by the solution_value property. Relational operators, such as ==, <= and >= have been overloaded to generate constraints, for example x==1 is actually a constraint, not a Python boolean. Writing code such as if x==1 cannot work because x is actually a decision variable and has no value until solve. This is why the message tells you you are attempting to test equality between two expressions. This could be done using is or method equals, but not with ==.

Unfortunately, your code does not allow to reproduce the issue and understand why you are trying to do this.

Philippe Couronne
  • 826
  • 1
  • 5
  • 6