示例#1
0
        static void Snake(Random r)
        {
            for (int i = 0; i < 50; ++i)
            {
                Pool p = new Pool(24, 4, r);
                p.Population = 500;
                SnakeAgent sa = new SnakeAgent(8, 8, r);
                sa.Execute = 5;

                sa.InputMode = 3; // 0 food bi, tale m
                                  // 1 food m, tale m
                                  // 2 food bi tale bi
                                  // 3 food m tale bi

                sa.Fit = (int score, int lifetime) =>
                {
                    double fit = 1;

                    if (score <= 20)
                    {
                        fit *= score * score * score;
                    }
                    else
                    {
                        fit = 8000;
                        for (int j = 0; j < score - 20; ++j)
                        {
                            fit *= 1.25;
                        }
                    }
                    return((long)(fit * 5));
                };
                Writer w = new Writer(new FileInfo(string.Format("D://NEAT/Snake88/RC125P3ScoreInput3/Data{0}.txt", i)));

                SnakeDataDictionary sdd = new SnakeDataDictionary();
                p.Agent          = sa;
                p.DeltaThreshold = 4;
                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 = sdd;
                w.Start(p, sa.Execute);
                p.Initialize();
                p.DisplayTop = int.MaxValue;


                for (int k = 0; k < 300; ++k)
                {
                    p.Evaluate();
                    //w.Sw.Flush();
                }
            }
        }
示例#2
0
        static void SnakeM(Random r)
        {
            Console.Write("Index : ");
            int    index = int.Parse(Console.ReadLine());
            int    x     = index % 5;
            int    y     = index / 5 % 25;
            int    z     = index / 25;
            double l     = (x + 1) * 0.2;
            double c     = (y + 1) * 0.2;
            double n     = (z + 1) * 0.2;

            Console.Write("Max : ");
            int max = int.Parse(Console.ReadLine());

            Console.WriteLine("Current Location : ");
            int current = int.Parse(Console.ReadLine());

            for (int j = index; j < 125; ++j)
            {
                for (int i = current; i < max; ++i)
                {
                    Pool p = new Pool(24, 4, r);
                    p.Population = 500;
                    SnakeAgent sa = new SnakeAgent(16, 16, r);
                    sa.Execute = 5;
                    SnakeDataDictionary sdd = new SnakeDataDictionary();
                    p.Agent          = sa;
                    p.DeltaThreshold = 4;
                    p.DeltaDisjoint  = 10;
                    p.DeltaWeight    = 4;

                    p.LinkMutationChance       = l;
                    p.ConnectionMutationChance = c;
                    p.NodeMutationChance       = n;
                    Writer w = new Writer(new FileInfo(string.Format("D://NEAT/DATA{0}_{1}.txt", index, i)));

                    p.WritePlayData  = true;
                    p.WriteSpecies   = true;
                    p.Writer         = w;
                    p.DataDictionary = sdd;
                    w.Start(p, sa.Execute);
                    p.Initialize();
                    p.DisplayTop    = int.MaxValue;
                    p.WritePlayData = true;

                    for (int k = 0; k < 300; ++k)
                    {
                        p.Evaluate();
                        w.Sw.Flush();
                    }
                }
                current = 0;
            }
        }
示例#3
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);
        }