Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,275 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
|
||
Diet problem in Picat. | ||
|
||
From the GLPK model diet.mod | ||
""" | ||
STIGLER'S NUTRITION MODEL | ||
|
||
This model determines a least cost diet which meets the daily | ||
allowances of nutrients for a moderately active man weighing 154 lbs. | ||
|
||
References: | ||
Dantzig G B, "Linear Programming and Extensions." | ||
Princeton University Press, Princeton, New Jersey, 1963, | ||
Chapter 27-1. | ||
""" | ||
|
||
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com | ||
See also my Picat page: http://www.hakank.org/picat/ | ||
|
||
*/ | ||
|
||
|
||
import mip. | ||
|
||
|
||
main => go. | ||
|
||
|
||
go => | ||
a(A), | ||
b(B), | ||
NumNutr = A[1].length, | ||
NumFood = A.length, | ||
|
||
X = new_list(NumFood), | ||
X :: 0.0..1.0, | ||
|
||
Cost #= sum(X), | ||
Cost #>= 0, | ||
|
||
% nutrient balance (units) | ||
foreach(N in 1..NumNutr) | ||
sum([A[F,N] * X[F] : F in 1..NumFood]) #= B[N] | ||
end, | ||
|
||
|
||
solve($[min(Cost)], X), | ||
|
||
printf("\nTotal cost: $%0.4f\n", Cost), | ||
% println(x=X), | ||
println("Dollars of food f to be purchased daily:"), | ||
Food = ["Wheat", "Cornmeal", "Cannedmilk", "Margarine", "Cheese", "Peanut-B", "Lard", | ||
"Liver", "Porkroast", "Salmon", "Greenbeans", "Cabbage", "Onions", "Potatoes", "Spinach", | ||
"Sweet-Pot", "Peaches", "Prunes", "Limabeans", "Navybeans"], | ||
|
||
foreach({F,C} in zip(Food, X)) | ||
printf("%-10w: $%0.4f\n", F, C) | ||
end, | ||
|
||
nl. | ||
|
||
|
||
|
||
% Calorie 3 % thousands | ||
% Protein 70 % grams | ||
% Calcium 0.8 % grams | ||
% Iron 12 % milligrams | ||
% Vitamin-A 5 % thousands IUs | ||
% Vitamin-B1 1.8 % milligrams | ||
% Vitamin-B2 2.7 % milligrams | ||
% Niacin 18 % milligrams | ||
% Vitamin-C 75 % milligrams ; | ||
|
||
b(B) => B = [3.0, 70.0, 0.8, 12.0, 5.0, 1.8, 2.7, 18.0, 75.0]. | ||
|
||
|
||
a(A) => | ||
A = [ | ||
% Calorie Protein Calcium Iron Vitamin-A Vitamin-B1 Vitamin-B2 Niacin Vitamin-C | ||
% (1000) (g) (g) (mg) (1000IU) (mg) mg) (mg) (mg) | ||
[44.7, 1411.0, 2.0, 365.0, 0.0, 55.4, 33.3, 441.0, 0.0], | ||
[36.0, 897.0, 1.7, 99.0, 30.9, 17.4, 7.9, 106.0, 0.0], | ||
[ 8.4, 422.0, 15.1, 9.0, 26.0, 3.0, 23.5, 11.0, 60.0], | ||
[20.6, 17.0, 0.6, 6.0, 55.8, 0.2, 0.0, 0.0, 0.0], | ||
[ 7.4, 448.0, 16.4, 19.0, 28.1, 0.8, 10.3, 4.0, 0.0], | ||
[15.7, 661.0, 1.0, 48.0, 0.0, 9.6, 8.1, 471.0, 0.0], | ||
[41.7, 0.0, 0.0, 0.0, 0.2, 0.0, 0.5, 5.0, 0.0], | ||
[ 2.2, 333.0, 0.2, 139.0, 169.2, 6.4, 50.8, 316.0, 525.0], | ||
[ 4.4, 249.0, 0.3, 37.0, 0.0, 18.2, 3.6, 79.0, 0.0], | ||
[ 5.8, 705.0, 6.8, 45.0, 3.5, 1.0, 4.9, 209.0, 0.0], | ||
[ 2.4, 138.0, 3.7, 80.0, 69.0, 4.3, 5.8, 37.0, 862.0], | ||
[ 2.6, 125.0, 4.0, 36.0, 7.2, 9.0, 4.5, 26.0, 5369.0], | ||
[ 5.8, 166.0, 3.8, 59.0, 16.6, 4.7, 5.9, 21.0, 1184.0], | ||
[14.3, 336.0, 1.8, 118.0, 6.7, 29.4, 7.1, 198.0, 2522.0], | ||
[ 1.1, 106.0, 0.0, 138.0, 918.4, 5.7, 13.8, 33.0, 2755.0], | ||
[ 9.6, 138.0, 2.7, 54.0, 290.7, 8.4, 5.4, 83.0, 1912.0], | ||
[ 8.5, 87.0, 1.7, 173.0, 86.8, 1.2, 4.3, 55.0, 57.0], | ||
[12.8, 99.0, 2.5, 154.0, 85.7, 3.9, 4.3, 65.0, 257.0], | ||
[17.4, 1055.0, 3.7, 459.0, 5.1, 26.9, 38.2, 93.0, 0.0], | ||
[26.9, 1691.0, 11.4, 792.0, 0.0, 38.4, 24.6, 217.0, 0.0]]. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
|
||
Triangle problem in Picat. | ||
|
||
From the lecture notes Modelling with Constraints | ||
http://www.cse.unsw.edu.au/~cs4418/2008/Lectures/Modelling2.ppt, page 45ff | ||
|
||
|
||
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com | ||
See also my Picat page: http://www.hakank.org/picat/ | ||
|
||
*/ | ||
|
||
import mip. | ||
|
||
main => go. | ||
|
||
go => | ||
temp(Temp), | ||
N = Temp.length, | ||
|
||
% Note .vars() here. | ||
Temp.vars() :: 0.0..100.0, | ||
|
||
foreach(I in 2..N-1,J in 2..N-1) | ||
% Temp[I,J] #= 1.0/4.0*(Temp[I-1,J] + Temp[I+1,J] + Temp[I,J-1] + Temp[I,J+1]) | ||
Temp[I,J]*4.0 #= (Temp[I-1,J] + Temp[I+1,J] + Temp[I,J-1] + Temp[I,J+1]) | ||
end, | ||
|
||
solve(Temp), | ||
|
||
foreach(Row in Temp) | ||
foreach(T in Row) | ||
printf("%10.3f ", T) | ||
end, | ||
nl | ||
end, | ||
|
||
HalfN = N div 2, | ||
printf("Tempature at Temp[%d,%d]: %0.3f\n", HalfN, HalfN, Temp[HalfN,HalfN]), | ||
|
||
nl. | ||
|
||
temp(Temp) => | ||
Temp = | ||
[[100.0,100.0,100.0,100.0,100.0,100.0,100.0], | ||
[ 0.0, _, _, _, _, _, 0.0], | ||
[ 0.0, _, _, _, _, _, 0.0], | ||
[ 0.0, _, _, _, _, _, 0.0], | ||
[ 0.0, _, _, _, _, _, 0.0], | ||
[ 0.0, _, _, _, _, _, 0.0], | ||
[ 0.0, _, _, _, _, _, 0.0]]. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
|
||
Knapsack problem (Rosetta Code) in Picat. | ||
|
||
From | ||
http://rosettacode.org/wiki/Knapsack_problem/Unbounded | ||
""" | ||
A traveller gets diverted and has to make an unscheduled stop in what turns out to | ||
be Shangri La. Opting to leave, he is allowed to take as much as he likes of the | ||
following items, so long as it will fit in his knapsack, and he can carry it. He knows | ||
that he can carry no more than 25 'weights' in total; and that the capacity of his | ||
knapsack is 0.25 'cubic lengths'. | ||
|
||
Looking just above the bar codes on the items he finds their weights and volumes. | ||
He digs out his recent copy of a financial paper and gets the value of each item. | ||
|
||
Item Explanation Value (each) weight Volume (each) | ||
------------------------------------------------------------ | ||
panacea (vials of) Incredible healing properties 3000 0.3 0.025 | ||
ichor (ampules of) Vampires blood 1800 0.2 0.015 | ||
gold (bars) Shiney shiney 2500 2.0 0.002 | ||
---------------------------------------------------------------------------- | ||
Knapsack For the carrying of - <=25 <=0.25 | ||
|
||
He can only take whole units of any item,, but there is much more of any item than | ||
he could ever carry | ||
|
||
How many of each item does he take to maximise the value of items he is carrying | ||
away with him? | ||
|
||
Note: | ||
|
||
1. There are four solutions that maximise the value taken. Only one need be given. | ||
""" | ||
|
||
|
||
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com | ||
See also my Picat page: http://www.hakank.org/picat/ | ||
|
||
*/ | ||
|
||
|
||
import mip. | ||
|
||
|
||
main => go. | ||
|
||
|
||
go => | ||
Value = [3000.0, 1800.0, 2500.0 ], | ||
Weight = [ 0.3, 0.2, 2.0 ], | ||
Volume = [ 0.025, 0.015, 0.002], | ||
|
||
N = Value.length, | ||
|
||
X = new_list(N), | ||
X :: 0..1000, | ||
|
||
Z #= sum([X[I]*Value[I] : I in 1..N]), | ||
|
||
foreach(I in 1..N) | ||
X[I] #>= 0 | ||
end, | ||
|
||
knapsack(Volume, X, 0.25), | ||
knapsack(Weight, X, 25.0), | ||
|
||
solve($[max(Z)], X), | ||
|
||
println(z=Z), | ||
println(x=X), | ||
Items = ["panacea","ichor","gold"], | ||
foreach({Item,Num} in zip(Items,X), Num > 0) | ||
printf("Take %d of %w\n", Num,Item) | ||
end, | ||
|
||
println("\nTotal volume:"), | ||
println(sum([X[I]*Volume[I] : I in 1..N])), | ||
|
||
println("Total weight:"), | ||
println(sum([X[I]*Weight[I] : I in 1..N])), | ||
|
||
println("Total cost:"), | ||
println(sum([X[I]*Value[I] : I in 1..N])), | ||
|
||
nl. | ||
|
||
|
||
knapsack(W, Take, WTMax) => | ||
sum([W[I]*Take[I] : I in 1..W.length]) #<= WTMax. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
|
||
Least square in Picat. | ||
|
||
Least square problem from the Swedish book | ||
Optimeringslara, page 286f | ||
|
||
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com | ||
See also my Picat page: http://www.hakank.org/picat/ | ||
|
||
*/ | ||
|
||
|
||
import mip. | ||
|
||
|
||
main => go. | ||
|
||
go => | ||
|
||
% temperature | ||
T = [20.0, 30.0, 80.0,125.0,175.0,225.0,275.0,325.0,360.0,420.0,495.0,540.0,630.0,700.0], | ||
% percentage gas | ||
F = [0.0,5.8 ,14.7,31.6,43.2,58.3,78.4,89.4,96.4,99.1,99.5,99.9,100.0,100.0], | ||
N = T.length, | ||
|
||
M = 4, | ||
A = new_list(M+1), % equation (index 0..M) | ||
A :: -100.0..100.0, | ||
|
||
Z :: -1000.0..1000.0, | ||
Z #= sum([F[I] - (A[0+1] + A[1+1]*T[I] + A[2+1]*T[I]*T[I] + A[3+1]*T[I]*T[I]*T[I] + A[4+1]*T[I]*T[I]*T[I]*T[I]) : I in 1..N]), | ||
|
||
|
||
A[0+1] + 20.0*A[1+1] + 20.0*20.0*A[2+1] + 20.0*20.0*20.0*A[3+1] + 20.0*20.0*20.0*20.0*A[4+1] #= 0.0, | ||
A[0+1] + 700.0*A[1+1] + 700.0*700.0*A[2+1] + 700.0*700.0*700.0*A[3+1] + 700.0*700.0*700.0*700.0*A[4+1] #= 100.0, | ||
foreach(I in 1..N) | ||
(A[1+1] + 2.0*A[2+1]*T[I] + 3.0*A[3+1]*T[I]*T[I] + 4.0*A[4+1]*T[I]*T[I]*T[I]) #>= 0.0 | ||
end, | ||
|
||
solve($[min(Z)],A), | ||
|
||
println(z=Z), | ||
println(a=A), | ||
|
||
nl. | ||
|
Oops, something went wrong.