METHINKS IT IS A WEASEL
Posted feedbacks - C#
3だとなかなか収束しないですね。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
public static class MethinksItIsLikeAWeasel
{
const int CANDIDATE_COUNT = 300;
const int VALIDATION_COUNT = 3;
const string GOAL = "METHINKSITISAWEASEL";
static Random _rnd = new Random();
internal static void Start()
{
var strs = CANDIDATE_COUNT
.Make( () => new string( GOAL.Length.Make( () => GetChar() ).ToArray() ) )
.ToArray();
int g = 0;
while( strs.First() != GOAL )
{
strs = strs
.SelectMany( str => VALIDATION_COUNT.Make( () => Replace( str ) ) )
.ToArray()
.OrderByDescending( s => CalcPoint( s ) )
.Take( CANDIDATE_COUNT )
.ToArray();
Console.WriteLine( "{0} {1} {2}", g, CalcPoint( strs.First() ), strs.First() );
g++;
}
}
static int CalcPoint( string str )
{
return str.Select( ( c, i ) => c == GOAL[ i ] ? 1 : 0 ).Sum();
}
static string Replace( string str )
{
int index = _rnd.Next( str.Length );
return str.Remove( index, 1 ).Insert( index, GetChar().ToString() );
}
static char GetChar()
{
return (char)( _rnd.Next( 26 ) + 65 );
}
public static IEnumerable<T> Make<T>( this int count, Func<T> func )
{
return Enumerable.Range( 0, count ).Select( _ => func() );
}
}
}
|
みんな言ってるけど、3じゃだめですね・・・。 評価関数を工夫してどうにかしようと奮闘したけどだめでした。
遺伝子(と呼びたい)に、ムダな領域を追加すると、3でも収束します。実質、変異率を下げたのと同じことですが。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | using System;
using System.Text;
using System.Collections.Generic;
namespace Methinks
{
class Program
{
static String GOAL = "METHINKSITISAWEASEL";
static String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
int CAPACITY_SMALL = 300; //容量
int CHILDREN = 3; //子供の数
int INTRON = 4; //ムダ領域
SortedList<int, String>[] pools = new SortedList<int, String>[] {
new SortedList<int, String>(),
new SortedList<int, String>()
};
Random rand = new Random();
for (int i = 0; i < CAPACITY_SMALL; i++) {
StringBuilder sb = new StringBuilder();
for (int n = 0; n < (GOAL.Length + INTRON); n++) {
sb.Append(CHARS[rand.Next(CHARS.Length)]);
}
AddMember(pools[0], sb.ToString());
}
int g = 0;
int turn = g % 2;
while (pools[turn].Values[0].Substring(0, GOAL.Length) != GOAL) {
Console.WriteLine("{0}: Top = [{1}], {2}", g, pools[turn].Values[0], pools[turn].Keys[0]);
int next = (g + 1) % 2;
pools[next].Clear();
for (int i = 0; i < CAPACITY_SMALL; i++) {
for (int c = 0; c < CHILDREN; c++) {
int pos = rand.Next(GOAL.Length + INTRON);
StringBuilder child = new StringBuilder();
child.Append(pools[turn].Values[i].Substring(0,pos));
child.Append(CHARS[rand.Next(CHARS.Length)]);
child.Append(pools[turn].Values[i].Substring(pos+1));
AddMember(pools[next], child.ToString());
}
}
g++;
turn = g % 2;
}
Console.WriteLine("{0}: Top = [{1}], {2}", g, pools[turn].Values[0], pools[turn].Keys[0]);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
static void AddMember(SortedList<int, String> pool, String member) {
int score = GetScore(member);
while (pool.ContainsKey(score)){
score++;
}
pool.Add(score, member);
}
static int GetScore(String s) {
int score = 0;
for (int i = 0; i < GOAL.Length; i++){
if (s[i] == GOAL[i]){
score--;
}
}
return score * 1000;
}
}
}
|

ytakenaka
#6287()
Rating4/8=0.50
ランダムな文字からMETHINKS IT IS A WEASELを作るプログラムを作れ。
簡単に流れを書いてみます。
1:ランダムな20文字を持つ文字列をもった300個作ります。
2:その文字列が"METHINKSITISAWEASEL"に近いものからソートします。
3:それぞれの文字列のなか1文字を別の文字に変化させたものを3つ用意します。
4:それを2:のソートをして上位300個残す。(900個あるうちで上位300個残すということです。)
5:以後3:と4:を繰り返す。
ランダムな文字変化は大文字だけでいいです。簡単にするために空白文字を外してあります。
METHINKS IT IS WEASELができたら終了。3と4の間でソートしたもので一番上位のものを毎回表示させると変化が楽しめます。:-)
Rickard Dawkinsがブラインドウォッチメイカー(現題:盲目の時計職人)の3章で書いていた有名なものです。さらに一般化してもらってもいいです。
参考
[ reply ]