4

I want to have x vehicle which goes on a graph going from vertex 1 and ending in the same all vertices as to be seen one time by one and only one vehicle (for those who know, i'm interesting in a PDPTW problem, but i'm stuck at this point)

using JuMP
using Cbc

nbVertex = 5
nbTransp = 2

model = Model(optimizer_with_attributes(Cbc.Optimizer, "seconds" => limiteExec))
set_optimizer_attribute(model, "PrimalTolerance", 1e-9)
print(model)


#les variables de decisions
@variable(model, route[1:nbVertex,1:nbVertex,1:nbTransp],Bin) #if road between 2 vertex is taken by vehic v
@variable(model, object>=0)

@constraint(model, [v in 1:nbTransp],sum(route[1,i,v] for i in 1:nbVertex)==1)#starting at one  

@constraint(model, [v in 1:nbTransp],sum(route[i,1,v] for i in 1:nbVertex)==1)#ending at one 

@constraint(model, [j=2: nbVertex],sum(route[i,j,v] for i in 1:nbVertex , v in 1 : nbTransp if i != j )==1) 
# all vertices as to be seen by one and only one vehicule

@constraint(model, [j=1:nbVertex, v= 1: nbTransp],sum(route[i,j,v] for i in 1:nbVertex if i != j)-sum(route[j,k,v] for k in 1:nbVertex if k != j)==0)
# here is the constraint


@objective(model, Min,object)

@show model

optimize!(model)

for  k in  1:nbTransp
    dataTmp=Matrix(undef,2,0) 
    for i in 1:nbVertex
        for  j in 1:nbVertex
           if value.(route[i,j,k])==1
                dataTmp=hcat(dataTmp,[i,j])
                println("vehicule ", k, " from ", i, " to ", j, ": $(route[i,j,k]) " )             
           end
        end
    end
end
vehicule 1 from 1 to 2: route[1,2,1] 
vehicule 1 from 2 to 1: route[2,1,1]
vehicule 2 from 1 to 3: route[1,3,2]
vehicule 2 from 3 to 1: route[3,1,2]
vehicule 2 from 4 to 5: route[4,5,2]
vehicule 2 from 5 to 4: route[5,4,2]

why is vehicle 2 looping in 4->5->4->5->4 ...?

logankilpatrick
  • 13,148
  • 7
  • 44
  • 125
victor roy
  • 51
  • 1

1 Answers1

4

You need to add constraint forbidding cycles in the graph. If you represent your route as x and the set of vertices as N (that is N = 1:nbVertex it can be denoted as: enter image description here

This makes sure that for any given sub-set of vertices you will have less travels than the number of vertices.

In practice this constraint will look something like this:

using Combinatorics
N = 1:nbVertex
for E in powerset(N,2,nbVertex)
        @constraint(mo, [k in K], ∑(route[i,j,k] for i ∈ E, j ∈ E if i != j) <= length(E)-1)
end

The problem is that the number of possible cycles grows very quickly when the size of N increases. You could try to mitigate it by using a lazy constraints callbacks approach (there is lots of literature on that) - unfortunately it is available only with commercial solvers. GLPK supports lazy constraints but the last time I tested it it was buggy. CBC has no support for lazy constraints.

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62