Wang Yiran
Wang Yiran

Reputation: 11

'TypeError: Unrecognized linear expression' happened when adding constraints in CP-SAT of OR-Tools

I want to use CP-Sat of or-tools, which is coded in Python, to solve a Constraints Satisfaction Problem. The package installed in Python is 'ortools 9.9.3963'.

The error is, when I construct a constraint with initialized variables, there comes:

File "D:\Python39\lib\site-packages\ortools\sat\python\cp_model.py", line 342, in get_integer_var_value_map
    raise TypeError("Unrecognized linear expression: " + str(expr))
TypeError: Unrecognized linear expression: -3.0

The corresponding constraint is formulated as:

for v in range(len(self.solution)):
    for s in range(len(schedule[v])):
        self.a[v, s] = self.model.NewIntVar(0, self.data.num_T, f'a_{v}_{s}')

for v in range(len(self.solution)):
    for s in range(len(self.schedule[v]) - 1): 
        lhs = self.a[v, s + 1] - self.a[v, s]
        if self.schedule[v][s][1:] == self.schedule[v][s + 1][1:]:
            rhs = self.dwell_times[v][int(s / 2)]
        else:
            rhs = self.data.travel_time[
                (int(self.schedule[v][s][1:]), int(self.schedule[v][s + 1][1:]))]
        name = f"a_{v}_{s}"
        print(lhs)
        print(rhs)
        self.model.Add(lhs >= rhs)

Wherein, I try to print the constraint by:

print(lhs)
print(rhs)

and the output is printed as:

enter image description here

It seems that the project is running correctly. It is so wired. Why is the root cause and how to fix this error?

Upvotes: 1

Views: 171

Answers (1)

Laurent Perron
Laurent Perron

Reputation: 11014

The error is the floating point coefficient, as commented by sascha. I have improved the error reporting.

It will now print:

Floating point constants are not supported in constraints: -3.0

Upvotes: 1

Related Questions