[Date Index]
[Thread Index]
[Author Index]
is there a better way to do constraint logic programming in Mathematica?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg80786] is there a better way to do constraint logic programming in Mathematica?
*From*: "sdw" <warwick at jps.net>
*Date*: Sat, 1 Sep 2007 00:36:23 -0400 (EDT)
*Organization*: Posted via Supernews, http://www.supernews.com
simplified constraint programming example in Mathematica:
find a set of numbers that add to a particular value - numbers cannot be
the same
note huge timing problem as we go up in variables
Above 5, it hung...
I was really hoping Mathematica had a decent constraint solver, but not sure
now!!
----
sumgroup[num_] := (vars =
Flatten @ Table[ ToExpression["x" <> ToString[i]], {i, 1, num}] ;
r1 = And @@ Map[( 0 < # < 10) & , vars]; Print [r1];
r2 = ( Plus @@ vars) == Round[ 9 num /2]; Print [r2];
r3 = And @@ Rest @Union @
Flatten @ Table[vars[[i]] != vars[[j]], {i, 1, num}, {j, i, num}] ;
Print[r3];
tim = Timing[FindInstance[r1 && r2 && r3, vars, Integers]];
Print[tim] ; Print[]);
For[ nn = 2, nn < 6, nn++, sumgroup[nn]]
0 < x1 < 10 && 0 < x2 < 10
x1 + x2 == 9
x1 != x2
{5.42101*10^-19, {{x1 -> 1, x2 -> 8}}}
""
0 < x1 < 10 && 0 < x2 < 10 && 0 < x3 < 10
x1 + x2 + x3 == 14
x1 != x2 && x1 != x3 && x2 != x3
{5.42101*10^-19, {{x1 -> 1, x2 -> 4, x3 -> 9}}}
""
0 < x1 < 10 && 0 < x2 < 10 && 0 < x3 < 10 && 0 < x4 < 10
x1 + x2 + x3 + x4 == 18
x1 != x2 && x1 != x3 && x1 != x4 && x2 != x3 && x2 != x4 && x3 =
!= x4
{0.094, {{x1 -> 2, x2 -> 3, x3 -> 4, x4 -> 9}}}
""
0 < x1 < 10 && 0 < x2 < 10 && 0 < x3 < 10 && 0 < x4 < 10 && 0 < x5 < 10
x1 + x2 + x3 + x4 + x5 == 22
x1 != x2 && x1 != x3 && x1 != x4 && x1 != x5 && x2 != x3 && x2 =
!= x4 &&
x2 != x5 && x3 != x4 && x3 != x5 && x4 != x5
{33.062, {{x1 -> 1, x2 -> 2, x3 -> 3, x4 -> 7, x5 -> 9}}}
""
Prev by Date:
**Re: fit a BinomialDistribution to exptl data?**
Next by Date:
**Re: problem with parsing InputForm scientific notation (eg. 3.`*^-7)**
Previous by thread:
**Re: Re: fit a BinomialDistribution to exptl data?**
Next by thread:
**Re: is there a better way to do constraint logic**
| |