Last active
December 9, 2018 20:37
-
-
Save abeham/1b35f7101d8fca751924c5b9e73f5ea9 to your computer and use it in GitHub Desktop.
MiniZinc model that solves a math puzzle involving bananas and a camel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% A farmer has 300 bananas which she wants to sell at a market 100km away. | |
% Her camel can carry 100 bananas at once, and eats one banana per km. | |
% What is the most bananas she can bring to the market? | |
int: capacity = 100; | |
int: distance = 100; | |
int: init_stock = 300; | |
int: maxtrips = ceil(init_stock / capacity); | |
int: maxlegs = 4; % maximum amount of legs the journey is partitioned into, a leg consists of hauling a certain load over a certain distance | |
set of int: legs = 1..maxlegs; | |
array[legs] of var 0..distance: travel; % the distance of each leg | |
array[legs] of var 0..init_stock: consumed; % how much banana the camel consumes in this leg | |
array[legs] of var 0..init_stock: load; % how much banana is carried in this leg | |
array[legs] of var 0..maxtrips: trips; % how many trips in direction of the market are required to haul the load | |
array[0..maxlegs] of var 0..init_stock: stock; % the remaining banana at the end of the leg | |
constraint sum(l in legs)(travel[l]) = distance; % the sum of the travelled distance must equal the distance to the market | |
constraint stock[0] = init_stock; | |
constraint forall(l in legs)(stock[l] = load[l] - consumed[l]); % the remaining bananas at the end of the leg | |
constraint forall(l in legs)(load[l] <= stock[l-1]); % we must not carry more banana than we had at the end of the last leg | |
constraint forall(l in legs)(consumed[l] = 2 * (trips[l] - 1) * travel[l] + travel[l]); % we have one less back trips than forth trips | |
constraint forall(l in legs)(trips[l] >= load[l] / capacity); % the number of trips required to carry the load | |
solve maximize stock[maxlegs]; % we want to have as much bananas hauled to the market as possible |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment