Skip to content

Instantly share code, notes, and snippets.

@rustyconover
Created March 6, 2014 03:04
Show Gist options
  • Save rustyconover/9381468 to your computer and use it in GitHub Desktop.
Save rustyconover/9381468 to your computer and use it in GitHub Desktop.
Amazon Size Tiers
% These are the standard size rules, they list the tier and the
% limitations.
%
% Record format: Tier name, longest, median, shortest, max lenght plus
% girth, is media flag, max weight
standard_size_tier_rule(product_size_tier_small_standard_size, 15, 12, 0.75, _, 1, 14/16).
standard_size_tier_rule(product_size_tier_small_standard_size, 15, 12, 0.75, _, 0, 12/16).
standard_size_tier_rule(product_size_tier_large_standard_size, 18, 14, 8, _, _, 20).
% Determine the rules that are standard size tiers
product_size_tier_is_standard(product_size_tier_large_standard_size).
product_size_tier_is_standard(product_size_tier_small_standard_size).
% Anything not standard size is oversize.
product_size_tier_is_oversize(Tier) :-
\+ product_size_tier_is_standard(Tier).
% These are the oversize tier rules.
%
% Record format: Tier name, longest, median, shortest, max length plus
% girth, max weight
oversize_tier_rule(product_size_tier_small_oversize, 60, 30, _, 130, 70).
oversize_tier_rule(product_size_tier_medium_oversize, 108, _, _, 130, 150).
oversize_tier_rule(product_size_tier_large_oversize, 108, _, _, 165, 150).
% Given the dimensions in inches this calculates the volume returning
% cubic inches.
product_volume([Z,Y,X], Volume) :-
Volume is Z * Y * X.
% Dimensional weight is defined as the volume divided by 166
% http://www.amazon.com/gp/help/customer/display.html?nodeId=201119390&pop-up=1
product_dimensional_weight(Dimensions, DimensionalWeight) :-
product_volume(Dimensions, Volume),
DimensionalWeight is Volume / 166.
% If an item is oversize and has a large volume Amazon will use the
% larger of the dimensional weight or the actual weight.
product_oversize_weight(Dimensions, Weight, OversizeWeight) :-
product_volume(Dimensions, Volume),
(Volume > 5184 ->
product_dimensional_weight(Dimensions, DimensionalWeight),
OversizeWeight = max(DimensionalWeight, Weight)
; OversizeWeight = Weight).
% This is the first rule that will determine which tier a product is
% classified as, since it is defined first it will be evaluated first.
product_size_tier([Z, Y, X], Weight, Length_Plus_Girth, Media, Tier) :-
% Match a standard size rule
standard_size_tier_rule(Tier, Longest, Median, Shortest, Max_Length_Plus_Girth, Is_Media, Max_Weight),
% Check the dimensions if they are set.
Z =< Longest,
(ground(Median) -> Y =< Median ; true),
(ground(Shortest) -> X =< Shortest ; true),
(ground(Max_Length_Plus_Girth) -> Length_Plus_Girth =< Max_Length_Plus_Girth ; true),
% Some tiers have different weight restrictions depending if its a
% media item, handle that.
(ground(Is_Media) -> Is_Media = Media ; true),
Weight =< Max_Weight,
% All of these rules have a cut at the end, since once a tier
% matches it should be the tier used, the rules are laid out from
% smallest to largest, so the smallest tier will always be chosen
% without any prolog backtracking.
!.
% This will use the oversize weight rules but apply the same type of
% logic, no oversize tiers are subject to differences based on media.
product_size_tier([Z, Y, X], Weight, Length_Plus_Girth, _, Tier) :-
product_oversize_weight([Z,Y,X], Weight, ValidWeight),
oversize_tier_rule(Tier, Longest, Median, Shortest, Max_Length_Plus_Girth, Max_Weight),
Z =< Longest,
(ground(Median) -> Y =< Median ; true),
(ground(Shortest) -> X =< Shortest ; true),
(ground(Max_Length_Plus_Girth) -> Length_Plus_Girth =< Max_Length_Plus_Girth),
ValidWeight =< Max_Weight,
!.
% Special oversize is an exception because it handles some conditions
% that are logical "or"s rather than "and"s.
product_size_tier([Z, Y, X], Weight, Length_Plus_Girth, _, Tier) :-
product_oversize_weight([Z,Y,X], Weight, ValidWeight),
Tier = product_size_tier_special_oversize,
(Z > 108 ; Length_Plus_Girth > 165 ; ValidWeight > 150),
!.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment