Last active
June 16, 2016 15:00
-
-
Save pierre-dargham/c4d54d7acf1a8270d351247eacc73566 to your computer and use it in GitHub Desktop.
Oz Constraint logic programming : 2016-03-21 Exercice 1 (Ships)
This file contains 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
% Author: Pierre Dargham | |
% Author-URI : https://github.com/pierre-dargham | |
declare | |
fun {Transport Fleet Number DailyDelayCost} | |
% Types of ships | |
Types = {Record.arity Fleet} | |
proc {Script Solution} | |
% Solution variables | |
Ships = {FD.record ships Types 0#FD.sup} | |
Contents = {FD.record contents Types 0#Number} | |
Capacity = {FD.decl} | |
Price = {FD.decl} | |
Penalty = {FD.decl} | |
TotalCost = {FD.decl} | |
% Temp variables | |
Penalties = {FD.record penalties Types 0#FD.sup} | |
Prices = {FD.record prices Types 0#FD.sup} | |
{List.forAll Types proc {$ I} Prices.I = Fleet.I.price end} | |
Capacities = {FD.record capacities Types 0#FD.sup} | |
{List.forAll Types proc {$ I} Capacities.I = Fleet.I.capacity end} | |
% Utils | |
fun {Delay Days} | |
if Days < 12 then 0 | |
else Days - 12 | |
end | |
end | |
in | |
% Script return value | |
Solution = solution( | |
fleet:Ships | |
price:Price | |
capacity:Capacity | |
distribution:Contents | |
penalty:Penalty | |
totalCost:TotalCost | |
) | |
% Propagators | |
Capacity >=: Number | |
{FD.sum Contents '=:' Number} | |
% Propagators | |
{List.forAll Types proc {$ I} Ships.I =<: Fleet.I.number end} | |
{List.forAll Types proc {$ I} Contents.I =<: Ships.I*Fleet.I.capacity end} | |
{List.forAll Types proc {$ I} Penalties.I =: DailyDelayCost*{Delay Fleet.I.days}*(Contents.I) end} | |
% Distribution | |
{FD.distribute ff Ships} | |
{FD.distribute ff Contents} | |
% Totals | |
{FD.sumC Ships Prices '=:' Price} | |
{FD.sumC Ships Capacities '=:' Capacity} | |
{FD.sum Penalties '=:' Penalty} | |
TotalCost =: Price + Penalty | |
end | |
% Sort function | |
proc {Order A B} | |
A.totalCost >: B.totalCost | |
end | |
in | |
% Return the best solution | |
{SearchBest Script Order} | |
end | |
% Example 1 | |
{Browse {Transport fleet( | |
a: ship(capacity: 100 number: 5 price: 1000 days: 10) | |
b: ship(capacity: 500 number: 3 price: 3000 days: 14) | |
) | |
1850 3}} | |
% Example 2 | |
{Browse {Transport fleet( | |
a: ship(capacity: 150 number: 10 price: 2000 days: 14) | |
b: ship(capacity: 700 number: 5 price: 3000 days: 15) | |
) | |
1421 10}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment