Example #1
0
        static void _2048(Random r)
        {
            for (int i = 0; i < 50; ++i)
            {
                Pool p = new Pool(16, 4, r);
                p.Population = 500;
                Agent2048 a2 = new Agent2048(r);
                a2.Execute   = 5;
                a2.FailLimit = 1;
                a2.InputMode = 3;

                a2.Fit = (int score, int logScore, int bitScore) =>
                {
                    double fit;
                    fit = (long)score * score * score;
                    return((long)fit);
                };

                Writer             w   = new Writer(new FileInfo(string.Format("D://NEAT/2048/CubeScoreInput3/Data{0}.txt", i)));
                Data2048Dictionary d2d = new Data2048Dictionary();
                p.Agent          = a2;
                p.DeltaThreshold = 10;
                p.DeltaDisjoint  = 10;
                p.DeltaWeight    = 4;

                p.LinkMutationChance       = 0.75;
                p.ConnectionMutationChance = 0.5;
                p.NodeMutationChance       = 0.2;

                p.WritePlayData  = true;
                p.WriteSpecies   = true;
                p.Writer         = w;
                p.DataDictionary = d2d;
                w.Start(p, a2.Execute);
                p.Initialize();
                p.DisplayTop = int.MaxValue;
                for (int k = 0; k < 300; ++k)
                {
                    p.Evaluate();
                }
            }
        }
Example #2
0
        public DataDictionary Read()
        {
            DataDictionary d  = null;
            StreamReader   sr = new StreamReader(Filename);
            string         data;

            while ((data = sr.ReadLine()) != null)
            {
                JObject j;
                if (data.StartsWith("\"Settings\""))
                {
                    j                        = JObject.Parse(data.Substring(11));
                    Execution                = j["Execution"].ToObject <int>();
                    Population               = j["Population"].ToObject <int>();
                    DeltaThreshold           = j["DeltaThreshold"].ToObject <double>();
                    DeltaDisjoint            = j["DeltaDisjoint"].ToObject <double>();
                    DeltaWeight              = j["DeltaWeight"].ToObject <double>();
                    PerturbChance            = j["PerturbChance"].ToObject <double>();
                    StepSize                 = j["StepSize"].ToObject <double>();
                    LinkMutationChance       = j["LinkMutationChance"].ToObject <double>();
                    ConnectionMutationChance = j["ConnectionMutationChance"].ToObject <double>();
                    NodeMutationChance       = j["NodeMutationChance"].ToObject <double>();
                    EnableMutationChance     = j["EnableMutationChance"].ToObject <double>();
                    DisableMutationChance    = j["DisableMutationChance"].ToObject <double>();
                    SurviveRate              = j["SurviveRate"].ToObject <double>();
                    Staleness                = j["Staleness"].ToObject <int>();
                    JToken inputToken = j["InputMode"];
                    if (inputToken == null)
                    {
                        InputMode = 0;
                    }
                    else
                    {
                        InputMode = inputToken.ToObject <int>();
                    }
                    JToken gametoken = j["Game"];
                    if (gametoken == null)
                    {
                        GameFactory = new SnakeGameFactory(16, 16);
                        pool        = new Pool(24, 4, null);
                    }
                    else
                    {
                        string game = gametoken.ToString();
                        if (game.StartsWith("Snake"))
                        {
                            string[] ds = game.Split('_');

                            GameFactory = new SnakeGameFactory(int.Parse(ds[1]), int.Parse(ds[2]));
                            pool        = new Pool(24, 4, null);
                        }
                        else if (game.Equals("2048"))
                        {
                            GameFactory = new Game2048Factory();
                            pool        = new Pool(16, 4, null);
                        }
                        else if (game.Equals("Pacman"))
                        {
                            GameFactory = new PacmanGameFactory();
                            pool        = new Pool(1, 4, null);
                        }
                    }
                    d = GameFactory.GetDataDictionary();
                }
                else if (data.StartsWith("\"Gen\""))
                {
                    j   = JObject.Parse(data.Substring(6));
                    Gen = j["gen"].ToObject <int>();
                    Genome g = new Genome(pool);
                    Best.Add(g);
                    g.GenomeId       = j["BestGenome"]["Id"].ToObject <int>();
                    g.FromGeneration = j["BestGenome"]["From"].ToObject <int>();
                    g.Fitness        = j["BestGenome"]["Fitness"].ToObject <long>();
                    d.CreateScore(g.GenomeId, 1);
                    d.AddScore(g.GenomeId, j["BestGenome"]["topScore"].ToObject <int>(), 0);
                    g.ExecutionTime = j["BestGenome"]["ExecutionTime"].ToObject <long>();
                }
                else if (data.StartsWith("\"Genome\""))
                {
                    j = JObject.Parse(data.Substring(9));
                    var ja = j.SelectToken("Genes");
                    foreach (JObject item in ja)
                    {
                        Gene gene = new Gene(item["In"].ToObject <int>(), item["Out"].ToObject <int>(),
                                             item["Weight"].ToObject <double>(), item["Enable"].ToObject <bool>(), item["Innovation"].ToObject <int>());
                        Best[Gen].Genes.Add(gene);
                    }

                    ja = j.SelectToken("food");
                    if (ja != null)
                    {
                        SnakeDataDictionary sdd   = d as SnakeDataDictionary;
                        LinkedList <Square> foods = new LinkedList <Square>();
                        foreach (var food in ja)
                        {
                            foods.AddLast(new Square(food[0].ToObject <int>(), food[1].ToObject <int>()));
                        }
                        sdd.AddFood(Best[Gen].GenomeId, foods);
                    }
                    ja = j.SelectToken("cells");
                    if (ja != null)
                    {
                        Data2048Dictionary        d2d = d as Data2048Dictionary;
                        LinkedList <CreatedCells> cc  = new LinkedList <CreatedCells>();
                        foreach (var cells in ja)
                        {
                            cc.AddLast(new CreatedCells(cells[0].ToObject <int>(), cells[1].ToObject <int>() == 2));
                        }
                        d2d.AddCells(Best[Gen].GenomeId, cc);
                    }
                }
                else if (data.StartsWith("\"Species\""))
                {
                }
            }

            return(d);
        }