% linear_opt/3 % A set of points in the plane is defined by the inequalities % % 0 ≤ X ≤ 5, % Y ≥ 0, % X + Y ≤ 7, % X + 2*Y ≥ 4 and % Y ≤ X + 5. % % The predicate linear_opt(X, Y, MaxE) should return the point (X, Y) where the % expression E = -0.4*X + 3.2*Y has the largest value. linear_opt(X, Y, MaxE) :- { X >= 0, Y >= 0, X =< 5, X+Y =< 7, X+2*Y >= 4, Y =< X+5, MaxE = -0.4*X+3.2*Y }, maximize(MaxE). % turkey/3 % The Holiday Meal Turkey Ranch is considering buying two different brands of % turkey feed and blending them to provide a good, low-cost diet for its % turkeys. Each brand of feed contains, in varying proportions, some or all of % the three nutritional ingredients essential for fattening turkeys. Each % kilogram of brand 1 contains 5 grams of ingredient A, 4 grams of ingredient B % and 0.5 grams of ingredient C. Each kilogram of brand 2 contains 10 grams of % ingredient A, 3 grams of ingredient B, but nothing of ingredient C. The brand % 1 feed costs 0.20 € a kilogram, while the brand 2 feed costs 0.30 € a % kilogram. % % The minimum monthly requirement per turkey is: 90 grams of ingredient A; 48 % grams of ingredient B and 1.5 grams of ingredient C. % % Formulate an LP model to help the rancher decide how to mix the two brands of % turkey feed so that the minimum monthly intake requirement for each % nutritional ingredient is met at minimum cost. Write the predicate % turkey(Brand1, Brand2, Cost) that returns the amount (in kg) of brands 1 and % 2 per turkey per month, and the total cost (in €). turkey(Brand1, Brand2, Cost) :- { Cost = Brand1*0.20 + Brand2*0.30, A = Brand1*5 + Brand2*10, B = Brand1*4 + Brand2*3, C = Brand1*0.5, A >= 90, B >= 48, C >= 1.5 }, minimize(Cost). % megabytes/2 % A megabyte is the SI unit meaning 106 bytes, while a mebibyte is the IEC unit % meaning 220 bytes. Write the predicate megabytes(SI, IEC) that converts % between the two using constraints. % % ?- megabytes(2, IEC). % IEC = 1.9073486328125. % ?- megabytes(SI, 2). % SI = 2.097152. megabytes(SI, IEC) :- { SI * 2^20 = IEC * 10^6 }. % max_sum/2 % max_sum(List, Max): Max is the maximal sum of two adjacent elements in List. % % ?- max_sum([4.5, 3.6, 1.2, 6.7], Max). % Max = 8.1. % ?- max_sum([1.1, 1.2, -12.3, 8.8], Max). % Max = 2.3. max_sum([_], S) :- minimize(S). max_sum([A,B|T], S) :- { S >= A + B }, max_sum([B|T], S). % bounding_box/3 % bounding_box(Points, X1/Y1, X2/Y2): X1/Y1 and X2/Y2 are the bottom-left and % top-right points defining the smallest bounding box containing all points in % the list Points. % % ?- bounding_box([4.5/2.3, 3.6/1.2, 6.7/0.1], X1/Y1, X2/Y2). % X1 = 3.6, Y1 = 0.1, X2 = 6.7, Y2 = 2.3. bounding_box([], Xa/Ya, Xb/Yb) :- minimize(Xb - Xa), minimize(Yb - Ya). bounding_box([X/Y|L], Xa/Ya, Xb/Yb) :- { Xa =< X, X =< Xb, Ya =< Y, Y =< Yb }, bounding_box(L, Xa/Ya, Xb/Yb). % center/3 % center(Points, R, X/Y): X/Y is a point in the list Points that is at most R % away from all other points. % % ?- center([1.0/1.1, 2.0/2.1, 3.0/3.1, 4.0/4.1], 4.0, X/Y). % X = 2.0, Y = 2.1 ; % X = 3.0, Y = 3.1. check([], _, _). check([X/Y | T], R, Xc/Yc) :- { (X-Xc)*(X-Xc) + (Y-Yc)*(Y-Yc) =< R*R }, check(T, R, Xc/Yc). center(L, R, Xc/Yc) :- memb(Xc/Yc, L), check(L, R, Xc/Yc).