Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Improving Evolutionary Algorithm

by delirium (Chaplain)
on Feb 19, 2004 at 19:59 UTC ( [id://330337]=note: print w/replies, xml ) Need Help??


in reply to Improving Evolutionary Algorithm

Woo-hoo! More Hofstadter fans!

Strangely, the 9 10 digit problem is solvable without using genetic algorithms or any randomization functions. Here's a solution that just starts with each count at 0 and feeds the last counts into the next sentence, evaluates, and repeats until the sentence is truthful:

#!/usr/bin/perl use strict; use warnings; my @list = 0..9; my %hash; $hash{$_} = 0 for @list; my $valid = 0; my $test = ''; while (!$valid) { $valid = 1; $test = 'This sentence contains '; $test .= "$hash{$_} ${_}s, " for @list; $test .= '. - '; print $test; my %test_hash; for (@list) { $test_hash{$_} = eval("\$test =~ tr/$_//"); $valid = 0 if ($hash{$_} != $test_hash{$_}); $hash{$_} = $test_hash{$_}; } ($valid == 1) ? print "yes\n" : print "no\n"; }

I won't spoil this by printing the answer, but it only took 4 iterations to achieve.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://330337]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2025-03-27 16:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (70 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.