Skip to content

Instantly share code, notes, and snippets.

@bokner
Last active December 3, 2024 19:33
Show Gist options
  • Save bokner/ec6fb053e97eea97799438845144f248 to your computer and use it in GitHub Desktop.
Save bokner/ec6fb053e97eea97799438845144f248 to your computer and use it in GitHub Desktop.
include "globals.mzn";
int: N;
int: E;
set of int: NODES = 1..N;
set of int: EDGES = 1..E;
array[EDGES] of int: from;
array[EDGES] of int: to;
array[NODES, NODES] of int: distances;
array[NODES, NODES] of int: fuel;
array[NODES] of var bool: path_nodes;
array[EDGES] of var bool: path_edges;
var int: total_fuel;
var int: total_distance;
constraint dpath(N, E, from, to, 1, N, path_nodes, path_edges);
constraint total_distance = sum(e in EDGES)(path_edges[e]*(distances[from[e], to[e]]));
constraint total_fuel = sum(e in EDGES)(path_edges[e]*(fuel[from[e], to[e]]));
constraint total_fuel <= 73;
solve minimize(total_distance);
%% For output (optional)
var set of EDGES: route_edges;
constraint link_set_to_booleans(route_edges, path_edges);
output ["from:\t", show([fix(from[e]) | e in route_edges]), "\n", "to:\t", show([fix(to[e]) | e in route_edges])];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment