Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > bd5c3d824c3db63ffd9226c15941e6ad > files > 1187

mozart-1.4.0-1mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>3.2 Example: Send More Money</TITLE><LINK href="ozdoc.css" rel="stylesheet" type="text/css"></HEAD><BODY><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node14.html#section.problem.script">&lt;&lt; Prev</A></TD><TD><A href="node13.html">- Up -</A></TD><TD><A href="node16.html#section.problem.explorer">Next &gt;&gt;</A></TD></TR></TABLE><DIV id="section.problem.money"><H2><A name="section.problem.money">3.2 Example: Send More Money</A></H2><P> We will now write a script for the Send More Money Puzzle. </P><DIV class="unnumbered"><H3><A name="label42">Problem Specification</A></H3><P>The Send More Money Problem consists in finding distinct digits for the letters <IMG alt="D" src="latex1.png">, <IMG alt="E" src="latex2.png">, <IMG alt="M" src="latex3.png">, <IMG alt="N" src="latex4.png">, <IMG alt="O" src="latex5.png">, <IMG alt="R" src="latex6.png">, <IMG alt="S" src="latex7.png">, <IMG alt="Y" src="latex8.png"> such that <IMG alt="S" src="latex7.png"> and <IMG alt="M" src="latex3.png"> are different from zero (no leading zeros) and the equation </P><BLOCKQUOTE><P><IMG alt="SEND\;+\;MORE\;=\;MONEY" src="latex9.png"></P></BLOCKQUOTE><P> is satisfied. The unique solution of the problem is <IMG alt="9567+1085=10652" src="latex10.png">. </P></DIV><DIV class="unnumbered"><H3><A name="label43">Model</A></H3><P>We model the problem by having a variable for every letter, where the variable stands for the digit associated with the letter. The constraints are obvious from the problem specification. </P></DIV><DIV class="unnumbered"><H3><A name="label44">Distribution Strategy</A></H3><P>We distribute on the variables for the letters with a first-fail strategy. The variables are ordered according to the alphabetic order of the letters. The strategy tries the least possible value of the selected variable. </P><DIV class="figure" id="progmoney"><HR><P><A name="progmoney"></A></P></DIV><DL class="anonymous"><DD class="code"><CODE><SPAN class="keyword">proc</SPAN><SPAN class="variablename">&nbsp;</SPAN>{<SPAN class="functionname">Money</SPAN>&nbsp;Root}<BR>&nbsp;&nbsp;&nbsp;S&nbsp;E&nbsp;N&nbsp;D&nbsp;M&nbsp;O&nbsp;R&nbsp;Y<BR><SPAN class="keyword">in</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;Root&nbsp;=&nbsp;sol(s:S&nbsp;e:E&nbsp;n:N&nbsp;d:D&nbsp;m:M&nbsp;o:O&nbsp;r:R&nbsp;y:Y)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%&nbsp;<SPAN class="comment">1<BR></SPAN>&nbsp;&nbsp;&nbsp;Root&nbsp;<SPAN class="keyword">:::</SPAN>&nbsp;0<SPAN class="keyword">#</SPAN>9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%&nbsp;<SPAN class="comment">2<BR></SPAN>&nbsp;&nbsp;&nbsp;{FD<SPAN class="keyword">.</SPAN>distinct&nbsp;Root}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%&nbsp;<SPAN class="comment">3<BR></SPAN>&nbsp;&nbsp;&nbsp;S&nbsp;<SPAN class="keyword">\=:</SPAN>&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%&nbsp;<SPAN class="comment">4<BR></SPAN>&nbsp;&nbsp;&nbsp;M&nbsp;<SPAN class="keyword">\=:</SPAN>&nbsp;0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1000<SPAN class="keyword">*</SPAN>S&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;100<SPAN class="keyword">*</SPAN>E&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;10<SPAN class="keyword">*</SPAN>N&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;D&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%&nbsp;<SPAN class="comment">5<BR></SPAN>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1000<SPAN class="keyword">*</SPAN>M&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;100<SPAN class="keyword">*</SPAN>O&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;10<SPAN class="keyword">*</SPAN>R&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;E<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">=:</SPAN>&nbsp;10000<SPAN class="keyword">*</SPAN>M&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;1000<SPAN class="keyword">*</SPAN>O&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;100<SPAN class="keyword">*</SPAN>N&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;10<SPAN class="keyword">*</SPAN>E&nbsp;<SPAN class="keyword">+</SPAN>&nbsp;Y<BR>&nbsp;&nbsp;&nbsp;{FD<SPAN class="keyword">.</SPAN>distribute&nbsp;ff&nbsp;Root}<BR><SPAN class="keyword">end</SPAN></CODE></DD></DL><DIV class="figure"><P class="caption"><STRONG>Figure&nbsp;3.1:</STRONG> A script for the Send More Money Puzzle.</P><HR></DIV><P> </P></DIV><DIV class="unnumbered"><H3><A name="label45">Script</A></H3><P><A href="node15.html#progmoney">Figure&nbsp;3.1</A> shows a script realizing the model and distribution strategy just discussed. The script first declares a local variable for every letter. Then it posts the following constraints: </P><OL type="1"><LI><P><CODE>Root</CODE> is a record that has a field for every letter. The fields of <CODE>Root</CODE> are the digits for the corresponding letters. This constraint is basic.</P></LI><LI><P>The fields of <CODE>Root</CODE> are integers in the domain <CODE>0<SPAN class="keyword">#</SPAN>9</CODE>. This constraint is basic.</P></LI><LI><P>The fields of <CODE>Root</CODE> are pairwise distinct. This constraint is nonbasic.</P></LI><LI><P>The values of the variables <CODE>S</CODE> and <CODE>M</CODE> are different from zero (no leading zeros). These constraints are nonbasic.</P></LI><LI><P>The digits for the letters satisfy the equation <CODE>SEND<SPAN class="keyword">+</SPAN>MORE=MONEY</CODE>. This constraint is nonbasic.</P></LI></OL><P> </P></DIV><DIV class="unnumbered"><H3><A name="label46">Posting of constraints</A></H3><P><A name="label47"></A><EM>posting of constraints</EM> is defined differently for basic and nonbasic constraints. Basic constraints are posted by telling them to the constraint store. Nonbasic constraints are posted by creating propagators implementing them. </P><P>Note that the propagators for <CODE>S<SPAN class="keyword">\=:</SPAN>0</CODE> and <CODE>M<SPAN class="keyword">\=:</SPAN>0</CODE> can immediately write their complete information into the constraint store since the store already knows domains for <CODE>S</CODE> and <CODE>M</CODE>. </P><P>The last line <CODE>{FD<SPAN class="keyword">.</SPAN>distribute&nbsp;ff&nbsp;Root}</CODE> posts a distributor that will distribute on the field of <CODE>Root</CODE> with the first-fail strategy (specified by the atom <CODE>ff</CODE>). Equivalently, we could write </P><BLOCKQUOTE class="code"><CODE>{FD<SPAN class="keyword">.</SPAN>distribute&nbsp;ff&nbsp;[D&nbsp;E&nbsp;M&nbsp;N&nbsp;O&nbsp;R&nbsp;S&nbsp;Y]}</CODE></BLOCKQUOTE><P> and thus specify the variables and their order explicitly. The order of the fields of <I>Root</I> is given by the canonical ordering of the respective features <CODE>d</CODE>, <CODE>e</CODE>, <CODE>m</CODE>, <CODE>n</CODE>, <CODE>o</CODE>, <CODE>r</CODE>, <CODE>s</CODE>, and <CODE>y</CODE>. </P><P>The statement </P><DL class="anonymous"><DD class="code"><CODE>{Browse&nbsp;{SearchAll&nbsp;Money}}</CODE></DD></DL><P> will compute and display the list of all solutions of the Send More Money Puzzle: </P><BLOCKQUOTE class="code"><CODE>[sol(d:7&nbsp;e:5&nbsp;m:1&nbsp;n:6&nbsp;o:0&nbsp;r:8&nbsp;s:9&nbsp;y:2)]</CODE></BLOCKQUOTE><P> </P><P>To understand the search process defined by <CODE>Money</CODE>, we need more information than just the list of solutions found. Obviously, it would be useful to see a graphical representation of the search tree. It would also be nice if we could see for every node of the search tree what information about the solution was accumulated in the constraint store when the respective space became stable. Finally, it would be nice to see for every arc of the tree with which constraint it was distributed. </P><P><A href="node15.html#figconmoneytree">Figure&nbsp;3.2</A> shows the search tree explored by <CODE>Money</CODE> together with the information just mentioned. This gives us a good understanding of the search process defined by <CODE>Money</CODE>. Note that the search tree is quite small compared to the <IMG alt="10^8" src="latex96.png"> candidates a naive generate and test method would have to consider.</P><P> </P><DIV id="figconmoneytree"><HR><P><A name="figconmoneytree"></A></P><DIV align="center"><IMG alt="" src="latex97.png"></DIV><P class="caption"><STRONG>Figure&nbsp;3.2:</STRONG> The search tree explored by <CODE>Money</CODE>.</P><HR></DIV><P></P></DIV></DIV><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node14.html#section.problem.script">&lt;&lt; Prev</A></TD><TD><A href="node13.html">- Up -</A></TD><TD><A href="node16.html#section.problem.explorer">Next &gt;&gt;</A></TD></TR></TABLE><HR><ADDRESS><A href="http://www.ps.uni-sb.de/~schulte/">Christian&nbsp;Schulte</A> and&nbsp;<A href="http://www.ps.uni-sb.de/~smolka/">Gert&nbsp;Smolka</A><BR><SPAN class="version">Version 1.4.0 (20090610)</SPAN></ADDRESS></BODY></HTML>