Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. include "alldifferent.mzn";
  2.  
  3.  
  4. string: Name; % name of datafile
  5. int: n = length(Demand); % n - number of customers
  6. int: NumVehicles; % - number of vehicles available
  7. int: Cap; % capacity of a vehicle. Each vehicle has the same capacity
  8. array [int] of int: Demand; % each customers demand
  9. array [1..n+1] of float: locX; % each customer location (x coordinate)
  10. array [1..n+1] of float: locY; % each customer location (y coordinate)
  11. float: DepotX = locX[n+1]; % x coordinate of depot
  12. float: DepotY = locY[n+1]; % y coordinate of depot
  13.  
  14.  
  15. function int: calculate_distance(float: x1, float: y1, float: x2, float: y2) = let {
  16. float: diff_x = coords_dist(x1, x2);
  17. float: diff_y = coords_dist(y1, y2);
  18. } in round(sqrt(diff_x*diff_x + diff_y*diff_y) * 1000);
  19. function float: coords_dist(float: x, float: y) = if x > 0 /\ y > 0 then abs(x-y) else
  20. if x < 0 /\ y < 0 then abs(abs(x) - abs(y)) else
  21. abs(x) + abs(y) endif endif;
  22.  
  23. array[1..n+1, 1..n+1] of int: distanceMatrix = array2d(1..n+1, 1..n+1, [
  24. if i = j then 0 else calculate_distance(locX[i], locY[i], locX[j], locY[j]) endif | i, j in 1..n+1
  25. ]);
  26.  
  27. array [1..n] of var 1..n: order :: domain;
  28. constraint alldifferent(order);
  29.  
  30. var int: total_distance =
  31. sum([
  32. distanceMatrix[order[i], order[i+1]] | i in 1..n-1
  33. ]);
  34.  
  35. solve minimize total_distance;
  36.  
  37. output ["objective_function = \(total_distance)\n"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement