Exemple #1
0
        private Block MakeBlock(BlockPair pair, Set set, IProjectFile project_file)
        {
            int startLineNumber = pair.Top.EndLineNumber + 1;
            int endLineNumber   = pair.Bottom.StartLineNumber - 1;

            DocumentRange range = SimianResultsParser.GetDocumentRange(project_file, startLineNumber, endLineNumber, Solution);

            return(new Block(startLineNumber, endLineNumber, project_file, set, range));
        }
 // Emulates receiving a valid block that builds on top of the chain.
 public static BlockPair CreateFakeBlock(NetworkParameters @params, IBlockStore blockStore, params Transaction[] transactions)
 {
     var b = MakeTestBlock(@params, blockStore);
     // Coinbase tx was already added.
     foreach (var tx in transactions)
         b.AddTransaction(tx);
     b.Solve();
     var pair = new BlockPair();
     pair.Block = b;
     pair.StoredBlock = blockStore.GetChainHead().Build(b);
     blockStore.Put(pair.StoredBlock);
     blockStore.SetChainHead(pair.StoredBlock);
     return pair;
 }
Exemple #3
0
        // Emulates receiving a valid block that builds on top of the chain.
        private BlockPair CreateFakeBlock(params Transaction[] transactions)
        {
            var b = _blockStore.GetChainHead().Header.CreateNextBlock(new EcKey().ToAddress(_params));

            foreach (var tx in transactions)
            {
                b.AddTransaction(tx);
            }
            b.Solve();
            var pair = new BlockPair();

            pair.Block       = b;
            pair.StoredBlock = _blockStore.GetChainHead().Build(b);
            _blockStore.Put(pair.StoredBlock);
            _blockStore.SetChainHead(pair.StoredBlock);
            return(pair);
        }
Exemple #4
0
        // Emulates receiving a valid block that builds on top of the chain.
        public static BlockPair CreateFakeBlock(NetworkParameters networkParams, IBlockStore blockStore, params Transaction[] transactions)
        {
            var b = MakeTestBlock(networkParams, blockStore);

            // Coinbase tx was already added.
            foreach (var tx in transactions)
            {
                b.AddTransaction(tx);
            }
            b.Solve();
            var pair = new BlockPair();

            pair.Block       = b;
            pair.StoredBlock = blockStore.GetChainHead().Build(b);
            blockStore.Put(pair.StoredBlock);
            blockStore.SetChainHead(pair.StoredBlock);
            return(pair);
        }
Exemple #5
0
 // Emulates receiving a valid block that builds on top of the chain.
 private BlockPair CreateFakeBlock(params Transaction[] transactions)
 {
     var b = _blockStore.GetChainHead().Header.CreateNextBlock(new EcKey().ToAddress(_params));
     foreach (var tx in transactions)
         b.AddTransaction(tx);
     b.Solve();
     var pair = new BlockPair();
     pair.Block = b;
     pair.StoredBlock = _blockStore.GetChainHead().Build(b);
     _blockStore.Put(pair.StoredBlock);
     _blockStore.SetChainHead(pair.StoredBlock);
     return pair;
 }
Exemple #6
0
        private static void Main(string[] args)
        {
            Population population = null;
            Population enemyPop   = null;

            // game loop
            while (true)
            {
                round++;

                for (int i = 0; i < 8; i++)
                {
                    string[] inputs = Console.ReadLine().Split(' ');
                    int      colorA = int.Parse(inputs[0]); // color of the first block
                    int      colorB = int.Parse(inputs[1]); // color of the attached block
                    _nextBlocks[i] = new BlockPair(colorA, colorB);
                }
                _score = int.Parse(Console.ReadLine());
                for (int i = 0; i < 12; i++)
                {
                    var row = Console.ReadLine(); // One line of the map ('.' = empty, '0' = skull block, '1' to '5' = colored block)
                    for (int j = 0; j < Width; j++)
                    {
                        if (row[j] == '.')
                        {
                            _myGrid.Field[j, i] = new Block(-1, j, i);
                        }
                        else
                        {
                            _myGrid.Field[j, i] = new Block(int.Parse(row[j].ToString()), j, i);
                        }
                    }
                }
                _enemyScore = int.Parse(Console.ReadLine());
                for (int i = 0; i < 12; i++)
                {
                    var row = Console.ReadLine(); // One line of the map ('.' = empty, '0' = skull block, '1' to '5' = colored block)
                    for (int j = 0; j < Width; j++)
                    {
                        if (row[j] == '.')
                        {
                            _enemyGrid.Field[j, i] = new Block(-1, j, i);
                        }
                        else
                        {
                            _enemyGrid.Field[j, i] = new Block(int.Parse(row[j].ToString()), j, i);
                        }
                    }
                }

                //var sb = new StringBuilder();
                //for (int k = 0; k < Height; k++)
                //{
                //    for (int i = 0; i < Width; i++)
                //    {
                //        sb.Append(_myGrid.Field[i, k] + " ");
                //    }

                //    sb.AppendLine();
                //}
                //Console.Error.WriteLine(sb.ToString());

                _myGrid.Save();
                _enemyGrid.Save();
                if (round == 0)
                {
                    population = new Population(PopulationSize, _myGrid);
                    enemyPop   = new Population(PopulationSize, _enemyGrid);
                }
                else
                {
                    population.UpdateGrid(_myGrid);
                    enemyPop.UpdateGrid(_enemyGrid);
                }
                _stopwatch = Stopwatch.StartNew();

                var enemyDna = enemyPop.GetBest(15, 0);
                _enemyNuisancePoints += enemyDna.Nuisance;
                Console.Error.WriteLine("NP: {0}", _enemyNuisancePoints);
                var dna = population.GetBest(80, _enemyNuisancePoints);
                Console.WriteLine(dna.Genes[0]); // "x": the column in which to drop your blocks
                //Console.Error.WriteLine("TALLEST: {0}", dna.Grid.TallestColumn());
                if (!true)
                {
                    _myGrid.ApplyMove(dna.Genes[0], _nextBlocks[0]);
                    _myGrid.RemoveMatches(0, true);
                    var sb = new StringBuilder();
                    for (int k = 0; k < Height; k++)
                    {
                        for (int j = 0; j < Width; j++)
                        {
                            sb.Append(_myGrid.Field[j, k] + " ");
                        }

                        sb.AppendLine();
                    }
                    Console.Error.WriteLine("{0} {1} M:{2}", sb.ToString(), _myGrid.score, dna.Genes[0]);
                }
                if (_enemyNuisancePoints >= 6)
                {
                    _enemyNuisancePoints /= 6;
                }
                Console.Error.WriteLine("Score after 8: {0}\nAvg Sims: {1}", dna.Fitness, round > 0 ? _simulations / round : 0);
            }
        }
Exemple #7
0
            public bool ApplyMove(Gene move, BlockPair blocks)
            {
                //Console.Error.WriteLine("Moving to {0}", move);
                int pos2X, pos2Y;

                switch (move.Rotation)
                {
                case 0:
                    pos2X = 1;
                    pos2Y = 0;
                    break;

                case 1:
                    pos2X = 0;
                    pos2Y = -1;
                    break;

                case 2:
                    pos2X = -1;
                    pos2Y = 0;
                    break;

                default:
                    pos2X = 0;
                    pos2Y = 1;
                    break;
                }
                if (move.Column + pos2X >= Width || move.Column + pos2X < 0)
                {
                    return(false);
                }

                bool placedA = false;
                bool placedB = false;
                bool aFirst  = pos2Y < 0;
                bool bFirst  = pos2Y > 0;

                for (int i = Height - 1; i >= 0; i--)
                {
                    if (i + pos2Y >= Height || i + pos2Y < 0)
                    {
                        continue;
                    }
                    if (!placedA && !bFirst && Field[move.Column, i].Color == Color.Empty)
                    {
                        Field[move.Column, i].Color = blocks.BlockA.Color;
                        aFirst = false;
                        if (placedB)
                        {
                            return(true);
                        }
                        placedA = true;
                    }
                    if (!placedB && !aFirst && Field[move.Column + pos2X, i + pos2Y].Color == Color.Empty)
                    {
                        Field[move.Column + pos2X, i + pos2Y].Color = blocks.BlockB.Color;
                        bFirst = false;
                        if (placedA)
                        {
                            return(true);
                        }
                        placedB = true;
                    }
                }

                return(false);
            }
        /// <inheritdoc/>
        internal override async Task <StepResult> ExecuteAsync(ChainedBlock nextChainedBlock, CancellationToken cancellationToken, bool disposeMode)
        {
            this.logger.LogTrace("({0}:'{1}/{2}',{3}:{4})", nameof(nextChainedBlock), nextChainedBlock.HashBlock, nextChainedBlock.Height, nameof(disposeMode), disposeMode);

            if (!this.BlockStoreLoop.PendingStorage.TryRemove(nextChainedBlock.HashBlock, out this.pendingBlockPairToStore))
            {
                this.logger.LogTrace("(-):{0}", StepResult.Next);
                return(StepResult.Next);
            }

            if (this.BlockStoreLoop.ChainState.IsInitialBlockDownload && !disposeMode)
            {
                if (this.BlockStoreLoop.PendingStorage.Skip(0).Count() < this.BlockStoreLoop.PendingStorageBatchThreshold)
                {
                    this.logger.LogTrace("(-):{0}", StepResult.Stop);
                    return(StepResult.Stop);
                }
            }

            var pendingBlockPairsToStore = new List <BlockPair>();

            pendingBlockPairsToStore.Add(this.pendingBlockPairToStore);
            int pendingStorageBatchSize = this.pendingBlockPairToStore.Block.GetSerializedSize();

            ChainedBlock lastFoundChainedBlock = nextChainedBlock;

            while (!cancellationToken.IsCancellationRequested)
            {
                ChainedBlock inputChainedBlock = nextChainedBlock;
                nextChainedBlock = this.BlockStoreLoop.Chain.GetBlock(nextChainedBlock.Height + 1);

                bool breakExecution = ShouldBreakExecution(inputChainedBlock, nextChainedBlock);
                if (!breakExecution && !this.BlockStoreLoop.PendingStorage.TryRemove(nextChainedBlock.HashBlock, out this.pendingBlockPairToStore))
                {
                    breakExecution = true;
                }

                if (breakExecution)
                {
                    if (!pendingBlockPairsToStore.Any())
                    {
                        break;
                    }
                }
                else
                {
                    pendingBlockPairsToStore.Add(this.pendingBlockPairToStore);
                    pendingStorageBatchSize += this.pendingBlockPairToStore.Block.GetSerializedSize(); // TODO: add the size to the result coming from the signaler
                    lastFoundChainedBlock    = nextChainedBlock;
                }

                if ((pendingStorageBatchSize > BlockStoreLoop.MaxPendingInsertBlockSize) || breakExecution)
                {
                    StepResult result = await PushPendingBlocksToRepository(pendingStorageBatchSize, pendingBlockPairsToStore, lastFoundChainedBlock, cancellationToken, breakExecution);

                    if (result == StepResult.Stop)
                    {
                        break;
                    }

                    pendingBlockPairsToStore.Clear();
                    pendingStorageBatchSize = 0;

                    if (this.BlockStoreLoop.ChainState.IsInitialBlockDownload) // this can be tweaked if insert is effecting the consensus speed
                    {
                        await Task.Delay(this.BlockStoreLoop.PushIntervalIBD, cancellationToken);
                    }
                }
            }

            pendingBlockPairsToStore.Clear();
            pendingBlockPairsToStore = null;

            this.pendingBlockPairToStore = null;

            this.logger.LogTrace("(-):{0}", StepResult.Continue);
            return(StepResult.Continue);
        }
        private Block MakeBlock(BlockPair pair, Set set, IProjectFile project_file)
        {
            int startLineNumber = pair.Top.EndLineNumber + 1;
            int endLineNumber = pair.Bottom.StartLineNumber - 1;

            DocumentRange range = SimianResultsParser.GetDocumentRange(project_file, startLineNumber, endLineNumber, Solution);

            return new Block(startLineNumber, endLineNumber, project_file, set, range);
        }
Exemple #10
0
        /// <inheritdoc/>
        internal override async Task <BlockStoreLoopStepResult> ExecuteAsync(ChainedBlock nextChainedBlock, CancellationToken cancellationToken, bool disposeMode)
        {
            if (!this.BlockStoreLoop.PendingStorage.TryRemove(nextChainedBlock.HashBlock, out this.pendingBlockPairToStore))
            {
                return(BlockStoreLoopStepResult.Next());
            }

            if (this.BlockStoreLoop.ChainState.IsInitialBlockDownload && !disposeMode)
            {
                if (this.BlockStoreLoop.PendingStorage.Skip(0).Count() < this.BlockStoreLoop.PendingStorageBatchThreshold)
                {
                    return(BlockStoreLoopStepResult.Break());
                }
            }

            var pendingBlockPairsToStore = new List <BlockPair>();

            pendingBlockPairsToStore.Add(this.pendingBlockPairToStore);
            var pendingStorageBatchSize = this.pendingBlockPairToStore.Block.GetSerializedSize();

            var lastFoundChainedBlock = nextChainedBlock;

            while (!cancellationToken.IsCancellationRequested)
            {
                var inputChainedBlock = nextChainedBlock;
                nextChainedBlock = this.BlockStoreLoop.Chain.GetBlock(nextChainedBlock.Height + 1);

                var breakExecution = ShouldBreakExecution(inputChainedBlock, nextChainedBlock);

                if (!breakExecution && !this.BlockStoreLoop.PendingStorage.TryRemove(nextChainedBlock.HashBlock, out this.pendingBlockPairToStore))
                {
                    breakExecution = true;
                }

                if (breakExecution)
                {
                    if (!pendingBlockPairsToStore.Any())
                    {
                        break;
                    }
                }
                else
                {
                    pendingBlockPairsToStore.Add(this.pendingBlockPairToStore);
                    pendingStorageBatchSize += this.pendingBlockPairToStore.Block.GetSerializedSize(); // TODO: add the size to the result coming from the signaler
                    lastFoundChainedBlock    = nextChainedBlock;
                }

                if (pendingStorageBatchSize > this.BlockStoreLoop.InsertBlockSizeThreshold || breakExecution)
                {
                    var result = await PushPendingBlocksToRepository(pendingStorageBatchSize, pendingBlockPairsToStore, lastFoundChainedBlock, cancellationToken, breakExecution);

                    if (result.ShouldBreak)
                    {
                        break;
                    }

                    pendingBlockPairsToStore.Clear();
                    pendingStorageBatchSize = 0;

                    if (this.BlockStoreLoop.ChainState.IsInitialBlockDownload) // this can be tweaked if insert is effecting the consensus speed
                    {
                        await Task.Delay(this.BlockStoreLoop.PushIntervalIBD, cancellationToken);
                    }
                }
            }

            pendingBlockPairsToStore.Clear();
            pendingBlockPairsToStore = null;

            this.pendingBlockPairToStore = null;

            return(BlockStoreLoopStepResult.Continue());
        }