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: [mg80844] 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:41:13 -0400 (EDT)

> note  huge timing problem as we go up in variables
> Above 5, it hung...

Maybe you can reduce the number of constraints:

equ={
x1+x2+x3+x4+x5==22,
x1<x2<x3<x4<x5,
x1>=1,
x5<=10};

{tme,sol}=Timing[FindInstance[equ,{x1,x2,x3,x4,x5},Integers,30]];

tme
0.015

{x1,x2,x3,x4,x5}/.sol

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

-- 
HTH   :>)
Dana DeLouis
Mathematica 6.0
Using 5.2 Help Files
Studying 4.0 Book.


"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[]);
> 
<snip>



  • Prev by Date: Re: Question about memory consumption in my codes by using Mathematica
  • Next by Date: Help with numerical differentiation
  • Previous by thread: Re: is there a better way to do constraint logic
  • Next by thread: Re: is there a better way to do constraint logic programming in Mathematica?