Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: is there a better way to do constraint logic programming in Mathematica?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg80834] Re: is there a better way to do constraint logic programming in Mathematica?
  • From: "Dana DeLouis" <dana.del at gmail.com>
  • Date: Tue, 4 Sep 2007 03:36:05 -0400 (EDT)

Would this brute-force method be of interest?
The logic might not be totally correct, but should be easy to adjust.

fx[num_] := Module[
  {tot = Round[9*num/2], ip},
  Print["Total: " <> ToString[tot]];
  ip = IntegerPartitions[tot, {num}];
  Select[ip, Length[#] == Length[Union[#]] &]]

fx[5]

Total: 22

{12,4,3,2,1},
{11,5,3,2,1},
{10,6,3,2,1},
{10,5,4,2,1},
{9,7,3,2,1},
{9,6,4,2,1},
{9,5,4,3,1},
{8,7,4,2,1},
{8,6,5,2,1},
{8,6,4,3,1},
{8,5,4,3,2},
{7,6,5,3,1},
{7,6,4,3,2}

Timing[fx[7]]

Total: 32

{0.015, 
{11,6,5,4,3,2,1},
{10,7,5,4,3,2,1},
{9,8,5,4,3,2,1},
{9,7,6,4,3,2,1},
{8,7,6,5,3,2,1}

-- 
HTH   :>)
Dana DeLouis


"sdw" <warwick at jps.net> wrote in message
news:fbar15$qft$1 at smc.twtelecom.net...
> 
> 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]]
> 
<snip>



  • Prev by Date: RE: Re: Re: Rule-based programming: declare variables to be a number
  • Next by Date: Re: Rule-based programming: declare variables to be a number
  • Previous by thread: Re: is there a better way to do constraint logic programming in Mathematica?
  • Next by thread: Re: is there a better way to do constraint logic programming in Mathematica?