0

I am using docplex (CpoModel) with Python to solve a CSP and I have the following problem: when I test it with a very simple infeasible problem, it does not return that the problem is infeasible in a reasonable amount of time (it never stops). However, for feasible problems, it finishes and outputs a solution. Is there something wrong in my code or is this caused by lack of optimization in the search strategies? If so, how can I optimize the search with docplex?

Here is the code in case there is some error there:

def solve(G):
mdl = CpoModel(name='unit')



epsilon=0.1
length = len(G.nodes())*2


variables={}

for v in G.nodes():
        
    variables[v] = (mdl.integer_var(0, 2*length**2, name="v"+str(v)+"-1"), mdl.integer_var(0, 2*length**2, name="v"+str(v)+"-2"))
    
   
    
    mdl.add_constraint(variables[v][1] - variables[v][0] >= length+epsilon) #this is verified
    
for (u,v) in G.edges(): 
    
    
    mdl.add_constraint(mdl.logical_or(mdl.logical_or(mdl.logical_or(
                    mdl.logical_and((variables[u][0]-variables[v][0]<=0-epsilon), variables[v][0]-variables[u][0]<=length-epsilon),
                    mdl.logical_and((variables[v][0]-variables[u][0]<=0-epsilon), (variables[u][0]-variables[v][0]<=length-epsilon))),
                    mdl.logical_or(
                    mdl.logical_and((variables[u][1]-variables[v][0]<=0-epsilon), (variables[v][0]-variables[u][1]<=length-epsilon)),
                    mdl.logical_and((variables[v][0]-variables[u][1]<=0-epsilon), (variables[u][1]-variables[v][0]<=length-epsilon)))),
    mdl.logical_or(mdl.logical_or(mdl.logical_and((variables[u][0]-variables[v][1]<=0-epsilon), (variables[v][1]-variables[u][0]<=length-epsilon)),
                    mdl.logical_and((variables[v][1]-variables[u][0]<=0-epsilon), (variables[u][0]-variables[v][1]<=length-epsilon))),
                   
    mdl.logical_or(mdl.logical_and((variables[u][1]-variables[v][1]<=0-epsilon),  (variables[v][1]-variables[u][1]<=length-epsilon)),
                    mdl.logical_and((variables[v][1]-variables[u][1]<=0-epsilon), (variables[u][1]-variables[v][1]<=length-epsilon))))))
                    
    



for (u,v) in (nx.complement(G)).edges(): 
    
    
    mdl.add_constraint(mdl.logical_or(
        mdl.logical_or(
                    (variables[u][1] - variables[v][0] <= -length-epsilon),
                    mdl.logical_and(mdl.logical_and((variables[u][0] - variables[v][0] <= -length-epsilon), (variables[v][0] - variables[u][1] <= -length-epsilon)), (variables[u][1] - variables[v][1] <= -length-epsilon))),
        mdl.logical_or(            
        mdl.logical_or(mdl.logical_and((variables[u][0] - variables[v][0] <= -length-epsilon), (variables[v][1] - variables[u][1] <= -length-epsilon)),
                    (variables[v][1] - variables[u][0] <= -length-epsilon)), 
        mdl.logical_or(
                    mdl.logical_and(mdl.logical_and((variables[v][0] - variables[u][0] <= -length-epsilon), (variables[u][0] - variables[v][1] <= -length-epsilon)), (variables[v][1] - variables[u][1] <= -length-epsilon)),
                    mdl.logical_and((variables[v][0] - variables[u][0] <= -length-epsilon), (variables[u][1] - variables[v][1] <= -length-epsilon))))
        ))
    
   


sol = mdl.solve( )

sol.get_search_status()

sol.print_solution()
user606273
  • 143
  • 6
  • This is very broad and it's hard to answer. But yeah, it's not uncommon to see that proving infeasibility is much harder than finding a feasible solution (and there is also some theoretical aspect to it: poly-time checks exist for problems in NP, but there might be a need for exponential-length proofs of infeasibility in co-NP problems). In general, i consider CP, at least out of the box (and CPO might actual be more than pure CP), the most troublesome in regards to this kind of proof-work compared to math-opt (cuts) and SAT (clause-learning). Maybe there is some merrit in trying those? – sascha Nov 24 '21 at 23:29
  • I added the code in case you can see an error in the program for CPO. Otherwise I will try the other options. – user606273 Nov 25 '21 at 07:50
  • My experience with Google OR-Tools for product configuration problems is that it is generally faster to find that the model is infeasible than to find a solution. If the constraint propagation is strong and there isn't much backtracking, the solver finds the infeasibility during the early stages of the solution search. – Christopher Hamkins Nov 26 '21 at 10:10

0 Answers0