示例#1
0
        /// <remark>Returns if the cell is contradicted</remark>
        public bool removePatternUpdatingEntropy(int x, int y, PatternId id, PatternStorage patterns)
        {
            this.possibilities[x, y, id.asIndex] = false;

            var cache = this.entropies[x, y];

            cache.reduceWeight(patterns[id.asIndex].weight);
            this.entropies[x, y] = cache;

            return(cache.totalWeight == 0);
        }
示例#2
0
        public static void print(this PatternStorage self)
        {
            for (int i = 0; i < self.len; i++)
            {
                var pattern = self[i];
                pattern.print(self.source, self.N);
                Console.WriteLine($"{i} (weight = {pattern.weight})");
                Console.WriteLine();
            }

            var n = self.len;

            Console.WriteLine($" { n } pattterns found ");
        }
示例#3
0
        public State(int width, int height, PatternStorage patterns, ref RuleData rule)
        {
            int nPatterns = patterns.len;

            this.gridSize = new Vec2i(width, height);

            this.possibilities = new Grid3D <bool>(width, height, nPatterns);
            this.enablerCounts = EnablerCounter.initial(width, height, patterns, ref rule);
            this.entropies     = new Grid2D <EntropyCacheForCell>(width, height);

            var initialEntropyCache = EntropyCacheForCell.initial(patterns);

            for (int i = 0; i < width * height; i++)
            {
                this.entropies.add(initialEntropyCache);
                for (int j = 0; j < nPatterns; j++)
                {
                    this.possibilities.add(true);
                }
            }
        }
示例#4
0
        /// <summary>Choose a possible pattern for an unlocked cell randomly in respect of weights of patterns</summary>
        static PatternId selectPatternForCell(int x, int y, State state, PatternStorage patterns, System.Random rnd)
        {
            int random = rnd.Next(0, state.entropies[x, y].totalWeight);

            int sumWeight = 0;

            for (int id_ = 0; id_ < patterns.len; id_++)
            {
                var id = new PatternId(id_);
                if (!state.isPossible(x, y, id))
                {
                    continue;
                }

                sumWeight += patterns[id_].weight;
                if (sumWeight > random)
                {
                    return(id);
                }
            }

            System.Console.WriteLine("ERROR: tried to select a pattern for a contradicted cell");
            return(new PatternId(-1));
        }
示例#5
0
        static void solveCellWithPattern(int x, int y, PatternId idToLockin, State state, PatternStorage patterns, Propagator propagator)
        {
            state.solveCellWithPattern(x, y, patterns[idToLockin.asIndex].weight);

            // setup next propagation
            for (int i = 0; i < patterns.len; i++)
            {
                var idToRemove = new PatternId(i);
                if (!state.isPossible(x, y, idToRemove) || i == idToLockin.asIndex)
                {
                    continue;
                }

                state.removePattern(x, y, idToRemove);
                propagator.onSolve(x, y, idToRemove);
            }
        }