コード例 #1
0
        public override long Evaluate(Genome g, DataDictionary dd)
        {
            Data2048Dictionary d2d = dd as Data2048Dictionary;
            long best = long.MinValue;
            long sum  = 0;

            dd.CreateScore(g.GenomeId, Execute);
            dd.CreateLifetime(g.GenomeId, Execute);

            int lifetime;

            for (int i = 0; i < Execute; ++i)
            {
                LinkedList <CreatedCells> cellLog = new LinkedList <CreatedCells>();
                Initialize(cellLog);
                lifetime = 0;
                while (!Gameover)
                {
                    double[] output = g.EvaluateNetwork(GetInputs());
                    int[]    order  = new int[4] {
                        0, 1, 2, 3
                    };
                    Array.Sort(output, order);

                    int index = 3;
                    while (!Shift((Direction)order[index]))
                    {
                        if (++FailCount >= FailLimit)
                        {
                            Gameover = true;
                            break;
                        }

                        --index;
                    }
                    if (Gameover)
                    {
                        break;
                    }
                    CreateRandom(SizeEmpty(), cellLog);
                    SetGameover();
                    ++lifetime;
                }
                sum += GetFitness();

                if (GetFitness() > best)
                {
                    best = GetFitness();
                    d2d.AddCells(g.GenomeId, cellLog);
                }

                dd.AddScore(g.GenomeId, Score, i);
            }
            return(sum / Execute);
        }
コード例 #2
0
            // 순서대로 (1,0), (0,1), (1,1), (0,0)을 입력값에 넣었을 때,
            // 정답이 '참'인 경우 출력값이 1보다 적으면 정답에서 출력값의 차를 제곱한 값과,
            // 정답이 '거짓'인 경우 출력값이 -1보다 크면 정답에서 출력값의 차를 제곱한 값의 합을 계산하고, (정답이 참 또는 거짓이라는게 정답을 맞췄다는 의미가 아니다. xor 연산의 결과가 참 또는 거짓인지를 의미한다.)
            // 그 값의 역수에 1000을 곱한 값을 적합도 함수로 사용하였다.
            public override long Evaluate(Genome g, DataDictionary dd)
            {
                int    score   = 0;
                double fitness = 0;
                double data;
                double val;

                if ((data = g.EvaluateNetwork(new double[] { 1, 0 })[0]) > 0)
                {
                    ++score;
                }
                val = MathUtils.Sigmoid(data) - 1;
                if (val > 0)
                {
                    val = 0;
                }
                fitness += val * val;
                if ((data = g.EvaluateNetwork(new double[] { 0, 1 })[0]) > 0)
                {
                    ++score;
                }
                val = MathUtils.Sigmoid(data) - 1;
                if (val > 0)
                {
                    val = 0;
                }
                fitness += val * val;
                if ((data = g.EvaluateNetwork(new double[] { 1, 1 })[0]) < 0)
                {
                    ++score;
                }
                val = MathUtils.Sigmoid(data) + 1;
                if (val < 0)
                {
                    val = 0;
                }
                fitness += val * val;
                if ((data = g.EvaluateNetwork(new double[] { 0, 0 })[0]) < 0)
                {
                    ++score;
                }
                val = MathUtils.Sigmoid(data) + 1;
                if (val < 0)
                {
                    val = 0;
                }
                fitness += val * val;


                dd.CreateScore(g.GenomeId, 1);
                dd.AddScore(g.GenomeId, score, 0);
                return((long)(1 / fitness * 1000));
            }
コード例 #3
0
            // 순서대로 (1,0), (0,1), (1,1), (0,0)을 입력값에 넣었을 때,
            // (1,0)과 (0,1)에서는 참을 나타내는 0보다 큰 값,
            // (0,0)과 (1,1)에서는 거짓을 나타내는 0보다 작은 값(이 기준은 임의로 잡은 것이다.)이 나오면 맞는 결과라 보고
            // 맞춘 횟수를 적합도로 설정하였다.
            public override long Evaluate(Genome g, DataDictionary dd)
            {
                int score = 0;

                if (g.EvaluateNetwork(new double[] { 1, 0 })[0] > 0)
                {
                    ++score;
                }
                if (g.EvaluateNetwork(new double[] { 0, 1 })[0] > 0)
                {
                    ++score;
                }
                if (g.EvaluateNetwork(new double[] { 1, 1 })[0] < 0)
                {
                    ++score;
                }
                if (g.EvaluateNetwork(new double[] { 0, 0 })[0] < 0)
                {
                    ++score;
                }
                dd.CreateScore(g.GenomeId, 1);
                dd.AddScore(g.GenomeId, score, 0);
                return(score + 1);
            }
コード例 #4
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);
        }