Пример #1
0
        private void CalculateKLDivergence(PatternCounts p, PatternCounts q, double w)
        {
            List <string> x       = new List <string>();
            double        total_p = 0;

            foreach (string key in p.Keys)
            {
                x.Add(key);
                total_p += p[key];
            }
            double total_q = 0;

            foreach (string key in q.Keys)
            {
                x.Add(key);
                total_q += q[key];
            }
            this._first  = 0;
            this._second = 0;
            foreach (string key in x)
            {
                double p_dash = (this._epsilon) / ((total_p + this._epsilon) * (1 + this._epsilon));
                double q_dash = (this._epsilon) / ((total_q + this._epsilon) * (1 + this._epsilon));
                if (p.ContainsKey(key))
                {
                    p_dash = (p[key] + this._epsilon) / ((total_p + this._epsilon) * (1 + this._epsilon));
                }
                if (q.ContainsKey(key))
                {
                    q_dash = (q[key] + this._epsilon) / ((total_q + this._epsilon) * (1 + this._epsilon));
                }
                this._first  += p_dash * Math.Log(p_dash / q_dash);
                this._second += q_dash * Math.Log(q_dash / p_dash);
            }
            this._fitness    = -(w * this._first + (1 - w) * this._second);
            this._calculated = true;
        }
Пример #2
0
        public static Tuple <Dictionary <int, PatternCounts>, Dictionary <int, PatternList>, Dictionary <int, BorderPatternList> > CalculateTilePatternProbabilities(List <int[, ]> maps, List <int> tp_sizes, WarpOptions warp = null, BorderOptions borders = null)
        {
            Dictionary <int, PatternCounts>     p               = new Dictionary <int, PatternCounts>();
            Dictionary <int, PatternList>       patterns        = new Dictionary <int, PatternList>();
            Dictionary <int, BorderPatternList> border_patterns = new Dictionary <int, BorderPatternList>();

            foreach (int size in tp_sizes)
            {
                p[size]               = new PatternCounts();
                patterns[size]        = new PatternList();
                border_patterns[size] = new BorderPatternList();
                for (int i = 0; i < maps.Count; i++)
                {
                    int[,] map = maps[i];
                    int ySize = size;
                    if (warp != null && warp["y"])
                    {
                        ySize = 1;
                    }
                    for (int y = 0; y < map.GetLength(0) - ySize + 1; y++)
                    {
                        int xSize = size;
                        if (warp != null && warp["x"])
                        {
                            xSize = 1;
                        }
                        for (int x = 0; x < map.GetLength(1) - xSize + 1; x++)
                        {
                            Pattern pattern = CalculateTilePattern(map, x, y, size);

                            p[size].Add(pattern);

                            if (borders != null)
                            {
                                bool temp_border = false;
                                if (borders["top"] && y == 0)
                                {
                                    temp_border = true;
                                    border_patterns[size].Add("top", pattern);
                                }
                                if (borders["bot"] && y == map.GetLength(0) - size)
                                {
                                    temp_border = true;
                                    border_patterns[size].Add("bot", pattern);
                                }
                                if (borders["left"] && x == 0)
                                {
                                    temp_border = true;
                                    border_patterns[size].Add("left", pattern);
                                }
                                if (borders["right"] && x == map.GetLength(1) - size)
                                {
                                    temp_border = true;
                                    border_patterns[size].Add("right", pattern);
                                }
                                if (!temp_border)
                                {
                                    patterns[size].Add(pattern);
                                }
                            }
                            else
                            {
                                patterns[size].Add(pattern);
                            }
                        }
                    }
                }
            }
            return(Tuple.Create(p, patterns, border_patterns));
        }