示例#1
0
        /// <inheritdoc />
        public FinalAnswer Solve(Model model, IDirectTaskSolver checker)
        {
            _model   = model;
            _answer  = Model.Copy(model);
            _checker = checker;
            Best     = new FinalAnswer(_answer, _checker.Solve(_answer));

            _chain   = RailwayChain.FromModel(_answer, reduceCount: true);
            _current = _chain;

            try
            {
                Main();
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("Алгоритм AmplifierFinalSolver остановлен");
            }
            catch (Exception e)
            {
                Console.WriteLine($"Ошибка в AmplifierFinalSolver: {e}");
            }
            finally
            {
                var ans = Model.Copy(Best.Model);
                ans.Blocks.Clear();
                ans.Blocks.AddRange(_model.Blocks);
                Best = new FinalAnswer(ans, _checker.Solve(ans));
            }

            return(Best);
        }
示例#2
0
        /// <summary>
        /// Решить обратную задачу
        /// </summary>
        /// <param name="model">Неполная модель исходных данных (только блоки DATA и ROUTE)</param>
        /// <param name="checker">Алгоритм решения прямой задачи - для вычисления стоимости</param>
        /// <returns>Полная модель (включая блоки ORDER и TOP)</returns>
        public FinalAnswer Solve(Model model, IDirectTaskSolver checker)
        {
            _model   = model;
            _answer  = Model.Copy(model);
            _checker = checker;

            var chain = RailwayChain.FromModel(_answer, true);

            _current1 = _chain = new Railway(RailwayType.L0);
            foreach (var railway in chain.GetRailways())
            {
                if (railway.IsHead())
                {
                    continue;
                }
                _current1.Append(railway);
                _current1 = _current1.Next;
            }

            _current1 = _current2 = _chain;

            Context = new DrawableContext();

            Context.BotsRating = "Управление:\n" +
                                 "W: Add L3\n" +
                                 "A: Add 2xT4L\n" +
                                 "S: Remove\n" +
                                 "D: Add 2xT4R\n" +
                                 "B: Add Bridge\n" +
                                 "Q: Prev item\n" +
                                 "E: Next item\n" +
                                 "Shift+W: L1\n" +
                                 "Shift+A: T4L\n" +
                                 "Shift+D: T4R\n" +
                                 "Z: Sync curs\n" +
                                 "X: Swap curs\n" +
                                 "C: Change mode\n" +
                                 "V: Insert tmpl\n" +
                                 "";

            SendAnswer();

            while (!Token.IsCancellationRequested)
            {
                Thread.Sleep(500);
            }

            _answer.Blocks.Clear();
            _answer.Blocks.AddRange(_model.Blocks);

            Context = new DrawableContext();
            SendAnswer();

            return(new FinalAnswer()
            {
                Model = _answer, Price = checker.Solve(_answer)
            });
        }
示例#3
0
        /// <inheritdoc />
        public FinalAnswer Solve(Model model, IDirectTaskSolver checker)
        {
            _model   = model ?? throw new ArgumentNullException(nameof(model));
            _checker = checker;

            _answer = Model.Copy(_model);

            Run();

            return(new FinalAnswer()
            {
                Model = _answer,
                Price = _checker.Solve(_answer),
            });
        }
示例#4
0
        /// <summary>
        /// Решить обратную задачу
        /// </summary>
        /// <param name="model">Неполная модель исходных данных (только блоки DATA и ROUTE)</param> расшириться
        /// <param name="directTaskSolver">Алгоритм решения прямой задачи - для вычисления стоимости</param>
        /// <returns>Полная модель</returns>
        public FinalAnswer Solve(Model model, IDirectTaskSolver directTaskSolver)
        {
            _model   = Model.Copy(model);
            _checker = directTaskSolver ?? new DirectTaskSolver();

            Init();

            Start();

            return(new FinalAnswer()
            {
                Model = model,
                Price = _checker.Solve(model),
            });
        }
示例#5
0
        /// <inheritdoc />
        public FinalAnswer Solve(Model model, IDirectTaskSolver checker)
        {
            _model   = model ?? throw new ArgumentNullException(nameof(model));
            _checker = checker;

            _answer = Model.Copy(_model);
            _answer.Topology.Clear();
            _answer.Order.Clear();

            Configuration = SecondSolverConfiguration.Default.Load();
            Configuration.Save();

            Context = new DrawableContext();

            Run();

            return(new FinalAnswer()
            {
                Model = _answer,
                Price = _checker.Solve(_answer),
            });
        }
示例#6
0
        /// <inheritdoc />
        /// <exception cref="ArgumentNullException" />
        public FinalAnswer Solve(Model model, IDirectTaskSolver checker)
        {
            _model = model; // ?? throw new ArgumentNullException(nameof(model));

            var factory = new RailwayFactory();

            // Разброс точек маршрута
            var routesSize = GetRoutesSize(model);

            // Создать стартовую трассу - кольцо
            var cycle = RailwayTemplates.CreateCircle(RailwayType.T4R);

            // Функция отображения модели
            void DisplayStep(IRailwayTemplate t)
            {
                _answer = t.ConvertToModel(_model);
                OnStepEvent?.Invoke(this, new FinalAnswer(_answer, checker.Solve(_answer)));
            }

            DisplayStep(cycle);

            var compass = RailwayTemplates.Compass(cycle);

            var              l            = 2;
            const int        CYCLES_COUNT = 5;
            IRailwayTemplate head         = cycle;

            for (var k = 0; k < CYCLES_COUNT; k++)
            {
                // Left
                for (var i = 0; i < l; i++)
                {
                    if (!head.TryScale(Direction.E, Railway.L3))
                    {
                        break;
                    }
                    DisplayStep(cycle);
                }

                // Up
                var chain1 = factory.BuildTemplate("t2T1L6", model);
                head.TryMutate(chain1);
                DisplayStep(cycle);

                // Up
                for (var i = 0; i < l; i++)
                {
                    if (!chain1.TryScale(Direction.N, Railway.L3))
                    {
                        break;
                    }
                    DisplayStep(cycle);
                }

                // Right
                var chain2 = factory.BuildTemplate("t2T1L6", model);
                chain1.TryMutate(chain2);
                DisplayStep(cycle);

                l += 3;

                // Right
                for (var i = 0; i < l; i++)
                {
                    if (!chain2.TryScale(Direction.W, Railway.L3))
                    {
                        break;
                    }
                    DisplayStep(cycle);
                }

                // Down
                var chain3 = factory.BuildTemplate("t2T1L6", model);
                chain2.TryMutate(chain3);
                DisplayStep(cycle);

                // Down
                for (var i = 0; i < l; i++)
                {
                    if (!chain3.TryScale(Direction.S, Railway.L3))
                    {
                        break;
                    }
                    DisplayStep(cycle);
                }

                // Right
                var chain4 = factory.BuildTemplate("t2T1L6", model);
                chain3.TryMutate(chain4);
                DisplayStep(cycle);

                head = chain4;
                l   += 3;
            }


            // Первый этап - расширяем кольцо вверх, вниз и в сторону

            /*
             * const int count = 16;
             * for (var i = 0; i < count; i++)
             * {
             *  // Блок, к которому добавляем L1
             *  var dest = compass.W;
             *
             *  // Расширение по сторонам
             *  if (i < count / 4) dest = compass.W;
             *  //else if (i < 2 * count / 4) dest = compass.NW;
             *  else if (i < 3 * count / 4) dest = compass.N;
             *  //else if (i < 4 * count / 4) dest = compass.NE;
             *
             *  // Чередование расширений по сторонам
             *  //if (i % 4 == 0) dest = compass.W;
             *  //else if (i % 4 == 1) dest = compass.NW;
             *  //else if (i % 4 == 2) dest = compass.N;
             *  //else if (i % 4 == 3) dest = compass.NE;
             *
             *  var chain = new RailwayChain(new[]
             *  {
             *      new Railway(RailwayType.T8L),
             *      new Railway(RailwayType.T8R),
             *  });
             *  //dest.AppendSymmetric(chain);
             *
             *  dest.AppendSymmetric(Railway.L1);
             *
             *  DisplayStep(cycle);
             * }
             */

            return(new FinalAnswer
            {
                Model = _answer,
                Price = checker.Solve(_answer),
            });
        }