<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Aaron Feng: Gecode/r</title>
    <link>http://www.aaronfeng.com/articles/2007/07/05/gecode-r</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Adventures in software development</description>
    <item>
      <title>Gecode/r</title>
      <description>&lt;p&gt;&lt;a href="http://aaronfeng.com/articles/2007/06/27/brain-teaser-constraint-programming"&gt;Recently I solved my first constraint programming problem&lt;/a&gt; using C# (&lt;a href="http://iqueryable.com/2007/06/28/SolvingABCDEFGHI1WithRuby.aspx"&gt;Steve Eichert also solved it via Ruby&lt;/a&gt;).  I decided to poke around the Internet to see if there's an API for constraint programming.  After all, there are real world problems that can be solved using constraint programming.  I can't imagine people just hand roll there own API for complex problems.&lt;/p&gt;

&lt;p&gt;After few searches in google, I came across &lt;a href="http://www.gecode.org/"&gt;Gecode&lt;/a&gt;.  Gecode is an open source API implemented in C++ distributed under the BSD style license.  I noticed there is also Gecode/J which is a Java interface to Gecode.  I started to wonder, is there a Ruby version of Gecode?  &lt;a href="http://code.google.com/soc/2007/ruby/appinfo.html?csaid=731701FB4D3CCC67"&gt;It turns out Andreas Launila recently started Gecode/r under Google Summer of Code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://gecoder.rubyforge.org/"&gt;Gecode/r&lt;/a&gt; is still at the early stage, but there is a downloadable alpha version.  I tried to use it to solve the same problem, however, I ran into some difficulties.  It currently only supports 3 mathematical operations: +, -, *.  No division for now.  Andreas suggested I rewrite the equation by removing the division.  The only problem with rewriting is that the equation actually becomes more complex.  I haven't been able to get my version to work yet.  &lt;/p&gt;

&lt;p&gt;For those who are interested in what Gecode/r looks like, below is an example of solving the classic &lt;a href="http://en.wikipedia.org/wiki/Send+more%3Dmoney"&gt;send more money&lt;/a&gt; problem using gecode/r, which is taken from gecode/r's &lt;a href="http://gecoder.rubyforge.org/examples/index.html"&gt;example&lt;/a&gt; page:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class SendMoreMoney &amp;lt; Gecode::Model
  def initialize
    s,e,n,d,m,o,r,y = @letters = int_var_array(8, 0..9)

    (equation_row(s, e, n, d) + equation_row(m, o, r, e)).must == 
      equation_row(m, o, n, e, y) 

    s.must_not == 0
    m.must_not == 0

    @letters.must_be.distinct

    branch_on @letters, :variable =&amp;gt; :smallest_size, :value =&amp;gt; :min
  end

  def to_s
    %w{s e n d m o r y}.zip(@letters).map do |text, letter|
      &amp;quot;#{text}: #{letter.val}&amp;quot; 
    end.join(', ')
  end

  private

   # A helper to make the linear equation a bit tidier. Takes a number of
   # variables and computes the linear combination as if the variable
   # were digits in a base 10 number. E.g. x,y,z becomes
   # 100*x + 10*y + z .
   def equation_row(*variables)
     variables.inject{ |result, variable| variable + result*10 }
  end
end 
puts SendMoreMoney.new.solve!.to_s&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 05 Jul 2007 21:10:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:2f67ee17-ea6f-42d4-bfe0-f2fa15d1b803</guid>
      <author>Aaron Feng</author>
      <link>http://www.aaronfeng.com/articles/2007/07/05/gecode-r</link>
      <category>programming</category>
    </item>
  </channel>
</rss>
