Skip to content

Commit

Permalink
Picat: Some more mip models
Browse files Browse the repository at this point in the history
  • Loading branch information
hakank committed May 26, 2014
1 parent 44721b3 commit 8b0e94e
Show file tree
Hide file tree
Showing 13 changed files with 1,275 additions and 11 deletions.
102 changes: 102 additions & 0 deletions picat/diet2.pi
@@ -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]].

52 changes: 52 additions & 0 deletions picat/dirichlet.pi
@@ -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]].
31 changes: 20 additions & 11 deletions picat/index.html
Expand Up @@ -36,7 +36,7 @@ <h2>My Picat programs/models</h2>
They are separated in different sections:
<ul>
<li> <a href="#cp">Constraint Programming</a>
<!-- <li> <a href="#mip">MIP models</a> -->
<li> <a href="#mip">MIP models</a>
<li> <a href="#euler">Project Euler problems</a>
<li> <a href="#rosetta">Rosetta Code problems</a>
<li> <a href="#planning_problems">Planning problems</a>
Expand Down Expand Up @@ -64,7 +64,8 @@ <h3>Constraint Programming and SAT models</h3>
<li> <a href="alldifferent_cst.pi">alldifferent_cst.pi</a>: Decomposition of the global constraint <code>alldifferent_cst</code>
<li> <a href="alldifferent_except_0.pi">alldifferent_except_0.pi</a>: Decomposition of the global constraint <code>alldifferent_except_0</code>
<li> <a href="alldifferent_modulo.pi">alldifferent_modulo.pi</a>: Decomposition of the global constraint <code>alldifferent_modulo</code>
<li> <a href="alldifferent_on_intersection.pi">alldifferent_on_intersection.pi</a>: Decomposition of the global constraint <code>alldifferent_on_intersection</code>
<li> <a href="alldifferent_on_intersection.pi">alldifferent_on_intersection.pi</a>: Decomposition of the global constraint <code>alldifferent_on_intersection</code>
<li> <a href="allpartitions.pi">allpartitions.pi</a>: Generate all partitions of a number
<li> <a href="alphametic.pi">alphametic.pi</a>: A fairly general alphametic solver (e.g. SEND+MORE=MONEY)
<li> <a href="alphametic2.pi">alphametic2.pi</a>: A general alphametic solver (e.g. SEND+MORE=MONEY)
<li> <a href="among.pi">among.pi</a>: Decomposition of the global constraint <code>among</code>
Expand Down Expand Up @@ -317,10 +318,9 @@ <h3>Constraint Programming and SAT models</h3>
<li> <a href="zebra.pi">zebra.pi</a>: Zebra puzzle ("Who owns a Zebra, and who drinks water?")
</ul>

<!--
<a name="mip">
<h3>MIP models</h3>
These models use the new mip module, introduced in version 0.3. (<code> import mip.</code>)
These models use the new mip module introduced in version 0.3 (<code> import mip.</code>).

<ul>
<li> <a href="3_jugs_mip.pi">3_jugs_mip.pi</a>: 3 jugs problem (as a shortest path problem), MIP
Expand All @@ -331,18 +331,28 @@ <h3>MIP models</h3>
<li> <a href="curve_fitting1.pi">curve_fitting1.pi</a>: Curve fitting (GLPK) MIP
<li> <a href="curve_fitting2.pi">curve_fitting2.pi</a>: Curve fitting (GLPK) MIP
<li> <a href="dakota_furniture.pi">dakota_furniture.pi</a>: Dakota furniture problem (Winston, MIP)
<li> <a href="dea.pi">dea.pi</a>: Data Envelopment Analysis (DEA) (GLPK, MIP)
<li> <a href="dea.pi">dea.pi</a>: Data Envelopment Analysis (DEA) (GLPK, MIP)
<li> <a href="diet2.pi">diet2.pi</a>: Diet problem (Dantzig, GLPK, MIP)
<li> <a href="dirichlet.pi">dirichlet.pi</a>: Dirichlet problem (MIP)
<li> <a href="dynamical_optimization.pi">dynamical_optimization.pi</a>: Dynamical Optimization problem (MIP)
<li> <a href="egg_basket.pi">egg_basket.pi</a>: Egg Basket problem (Kordemsky, via Martin Chlond), MIP
<li> <a href="games.pi">games.pi</a>: Game theory problem (MIP)
<li> <a href="game_theory_taha.pi">game_theory_taha.pi</a>: Game theory problem (Taha: OR), MIP
<li> <a href="hitchcock_transportation_problem.pi">hitchcock_transportation_problem.pi</a>: (Hitchcock-Koopmans) Transportation problem, MIP
<li> <a href="knapsack_problem_mip.pi">knapsack_problem_mip.pi</a>: Knapsack problem (Rosetta Code, MIP)
<li> <a href="least_square_optimeringslara_286.pi">least_square_optimeringslara_286.pi</a>: Least square problem (MIP)
<li> <a href="markov_chains.pi">markov_chains.pi</a>: Markov Chains (MIP)
<li> <a href="markov_chains_taha.pi">markov_chains_taha.pi</a>: Markov Chains (Taha, MIP)
<li> <a href="mortgage.pi">mortgage.pi</a>: Mortgage (MIP)
<li> <a href="plan_mip.pi">plan_mip.pi</a>: Planning model (GLPK, MIP)
<li> <a href="spreadsheet.pi">spreadsheet.pi</a>: Spreadsheet (MIP)
<li> <a href="stigler.pi">stigler.pi</a>: Stigler's 1939 diet problem (GLPK, MIP)
<li> <a href="sudoku_ip.pi">sudoku_ip.pi</a>: Sudoku solver (MIP)
<li> <a href="transp.pi">transp.pi</a>: Transportation problem (GLPK, MIP)
<li> <a href="transp1.pi">transp1.pi</a>: Transportation problem (OPL, MIP)
<li> <a href="volsay3_mip.pi">volsay3_mip.pi</a>: Volsay problem (MIP)
</ul>

-->

<a name="euler">
<h3>Project Euler problem</h3>
Here are some solutions for the <a href="http://projecteuler.net/">Project Euler</a> problems:<br>
Expand Down Expand Up @@ -391,7 +401,7 @@ <h3>Project Euler problem</h3>
<li> <a href="euler39.pi">euler39.pi</a>: Euler #39
<li> <a href="euler40.pi">euler40.pi</a>: Euler #40
<li> <a href="euler41.pi">euler41.pi</a>: Euler #41
<li> <a href="euler42.pi">euler41.pi</a>: Euler #42 (data file <a href="words.txt">words.txt</a>)
<li> <a href="euler42.pi">euler41.pi</a>: Euler #42
<li> <a href="euler43.pi">euler43.pi</a>: Euler #43
<li> <a href="euler44.pi">euler44.pi</a>: Euler #44
<li> <a href="euler45.pi">euler45.pi</a>: Euler #45
Expand Down Expand Up @@ -565,13 +575,12 @@ <h3>Miscellaneous programs</h3>
<li> <a href="tic_tac_bench.pi">tic_tac_bench.pi</a>
</ul>

<li> <a href="allpartitions.pi">allpartitions.pi</a>: Generate all partitions of a number
<li> <a href="arbitrage_loops.pi">arbitrage_loops.pi</a>: Arbitrage loops. Cf <a href="bitcoin.pi">bitcoin.pi</a>
<li> <a href="analogy.pi">analogy.pi</a>: Evan's Analogy program (ported from Sterling "The Art of Prolog")
<li> <a href="apl_util.pi">apl_util.pi</a>: APL utils. Some utilities inspired by APL and/or K. (Is now a module in Picat distribution.)
<li> <a href="base_conversion.pi">base_conversion.pi</a>: Base conversion.
<li> <a href="beds1.pi">beds1.pi</a>: Bunk beds puzzle
<li> <a href="bitcoin.pi">bitcoind.pi</a>: Reading bitcoin currency and make an optimal arbitrage loop. Cf <a href="arbitrage_loops.pi">arbitrage_loops.pi</a>.
<li> <a href="bitcoin.pi">bitcoin.pi</a>: Reading bitcoin currency and make an optimal arbitrage loop. Cf <a href="arbitrage_loops.pi">arbitrage_loops.pi</a>.
<li> <a href="bridge_party.pi">bridge_party.pi</a>: Bridge party
<li> <a href="bubble_sort.pi">bubble_sort.pi</a>: Bubble sort
<li> <a href="carroll_symbolic_logic_60.pi">carroll_symbolic_logic_60.pi</a>: Lewis Carroll's symbolic logic problem #60
Expand All @@ -591,7 +600,7 @@ <h3>Miscellaneous programs</h3>
<li> <a href="josephus.pi">josephus.pi</a>: Josephus problem
<li> <a href="lace.pi">lace.pi</a>: Lace problem (ported from B-Prolog's entry to 2012 Prolog Programming Contest)
<li> <a href="lcs_tabling.pi">lcs_tabling.pi</a>: Longest Common Subsequence using tabling (ported from B-Prolog)
<li> <a href="lunch_problem.pi">lunc_problem.pi</a>: Lunch problem
<li> <a href="lunch_problem.pi">lunch_problem.pi</a>: Lunch problem
<li> <a href="mankell.pi">mankell.pi</a>: Generate all possible (right and wrong) spellings of 'Henning Mankell' and 'Kjellerstrand' (using a "grammar" etc)
<li> <a href="matrix_slice.pi">matrix_slice.pi</a>: Some list/array/matrix extraction functions. E.g. <code>M.slice(1..3, 2..4)</code> for extracting a sub square
<li> <a href="mcsam.pi">mcsam.pi</a>: MicroSAM program (ported from Sterling "The Art of Prolog")
Expand Down
90 changes: 90 additions & 0 deletions picat/knapsack_problem_mip.pi
@@ -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.
47 changes: 47 additions & 0 deletions picat/least_square_optimeringslara_286.pi
@@ -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.

0 comments on commit 8b0e94e

Please sign in to comment.