Advertisement
Guest User

Add

a guest
Nov 12th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 2.23 KB | None | 0 0
  1. function mutationAdd(pop, p::Float64, matrix::Array{Int64,2}, cost::Array{Int64,1})
  2.     for i in 1:length(pop)
  3.         r = rand()
  4.         if r < p
  5.             # println("superMutation")
  6.             pop[i] = addSimpleDescent(pop[i], matrix, cost)
  7.         # elseif r < 0.6
  8.         #     # pop_int[i] = zero_one_exchange(pop_int[i],matrix,cost)
  9.             # pop_int[i] = one_one_exchange(pop_int[i],matrix,cost)
  10.             # pop_int[i] = one_two_exchange(pop_int[i],matrix,cost)
  11.         end
  12.     end
  13.     return pop
  14. end
  15.  
  16. function addSimpleDescent(sol, matrix::Array{Int64,2}, cost::Array{Int64,1})
  17.     best_sol = zero_one_exchange(sol,matrix,cost)
  18.     while best_sol[2] > sol[2]
  19.         sol = best_sol
  20.         best_sol = zero_one_exchange(sol,matrix,cost)
  21.     end
  22.     return sol
  23. end
  24.  
  25. function zero_one_exchange(sol, matrix::Array{Int64,2}, cost::Array{Int64,1})
  26.     best_voisin = copy(sol[1])
  27.     best_z = copy(sol[2])
  28.     indvar = 0
  29.     stop = false
  30.  
  31.     vars_0 = Int64[]                             # Vecteur contenant l'indice des variables de decision valant 0 dans la solution @sol
  32.     constr_sat = Int64[]                        # Vecteur contenant l'indice des contraintes satisfaites
  33.     for i = 1:length(sol[1])
  34.         if sol[1][i] == 0
  35.             push!(vars_0,i)
  36.         else
  37.             for j = 1:size(matrix)[1]
  38.                 if matrix[j,i] == 1
  39.                     push!(constr_sat,j)
  40.                 end
  41.             end
  42.         end
  43.     end
  44.  
  45.     p_candidat = Int64[]
  46.     for var in vars_0
  47.         vaut0 = false
  48.         for constr in constr_sat
  49.             if matrix[constr,var] == 1
  50.                 vaut0 = true
  51.             end
  52.         end
  53.         if !vaut0
  54.             push!(p_candidat,var)
  55.         end
  56.     end
  57.  
  58.     i = 1
  59.     while i <= length(p_candidat) && !stop
  60.         z_voisin = sol[2] + cost[p_candidat[i]]
  61.         if z_voisin > best_z
  62.             stop = true
  63.             best_z = z_voisin
  64.         else
  65.             i = i+1
  66.         end
  67.     end
  68.  
  69.     if stop
  70.         best_voisin[p_candidat[i]] = 1
  71.     end
  72.     # println("amélioration :", sol[1]," ->", best_z)
  73.     # println("admissibilite du voisin 0-1 :", is_admissible((best_z,best_voisin),cost,matrix))
  74.     return best_voisin, best_z
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement