Re: is there a better way to do constraint logic programming in Mathematica?
- To: mathgroup at smc.vnet.net
- Subject: [mg80922] Re: is there a better way to do constraint logic programming in Mathematica?
- From: "sdw" <warwick at jps.net>
- Date: Wed, 5 Sep 2007 03:01:47 -0400 (EDT)
- References: <fbj2jm$nfm$1@smc.vnet.net>
elegant and simple solution. "Dana DeLouis" <dana.del at gmail.com> wrote in message news:fbj2jm$nfm$1 at smc.vnet.net... >> 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> > >