Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include "alldifferent.mzn";
- string: Name; % name of datafile
- int: n = length(Demand); % n - number of customers
- int: NumVehicles; % - number of vehicles available
- int: Cap; % capacity of a vehicle. Each vehicle has the same capacity
- array [int] of int: Demand; % each customers demand
- array [1..n+1] of float: locX; % each customer location (x coordinate)
- array [1..n+1] of float: locY; % each customer location (y coordinate)
- float: DepotX = locX[n+1]; % x coordinate of depot
- float: DepotY = locY[n+1]; % y coordinate of depot
- function int: calculate_distance(float: x1, float: y1, float: x2, float: y2) = let {
- float: diff_x = coords_dist(x1, x2);
- float: diff_y = coords_dist(y1, y2);
- } in round(sqrt(diff_x*diff_x + diff_y*diff_y) * 1000);
- function float: coords_dist(float: x, float: y) = if x > 0 /\ y > 0 then abs(x-y) else
- if x < 0 /\ y < 0 then abs(abs(x) - abs(y)) else
- abs(x) + abs(y) endif endif;
- array[1..n+1, 1..n+1] of int: distanceMatrix = array2d(1..n+1, 1..n+1, [
- if i = j then 0 else calculate_distance(locX[i], locY[i], locX[j], locY[j]) endif | i, j in 1..n+1
- ]);
- array [1..n] of var 1..n: order :: domain;
- constraint alldifferent(order);
- var int: total_distance =
- sum([
- distanceMatrix[order[i], order[i+1]] | i in 1..n-1
- ]);
- solve minimize total_distance;
- output ["objective_function = \(total_distance)\n"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement