示例#1
0
        private static void RunHomework1(int size)
        {
            Console.WriteLine($"Size {size}:");

            var path              = string.Format(Path, size);
            var instanceProvider  = new TextReaderInstanceProvider(new StreamReader(path));
            var brutteForceSolver = new BrutteForceSolver();
            var heuristicSolver   = new HeuristicSolver();

            var runner = new CompareRunner(instanceProvider, brutteForceSolver, heuristicSolver);

            runner.Run();
        }
        private void TestSolverOnlyPrice(ISolver solver)
        {
            var instanceProvider  = new TextReaderInstanceProvider(new StreamReader(string.Format(Path, 10)));
            var brutteForceSolver = new BrutteForceSolver();

            foreach (var instance in instanceProvider.GetInstances())
            {
                var res1 = brutteForceSolver.Solve(instance);
                var res2 = solver.Solve(instance);

                Assert.AreEqual(res1.Price, res2.Price, $"Prices are not equal for instance ${instance.Id}.");
            }
        }
示例#3
0
        private static void Test1()
        {
            var generator = new RandomInstanceProvider();
            var instance  = generator.GetInstance(25);

            Console.WriteLine(instance.Formula);

            var brutteForceSolver        = new BrutteForceSolver();
            var simulatedAnnealingSolver = new SimulatedAnnealingSolver();

            var solution1 = brutteForceSolver.Solve(instance);
            var solution2 = simulatedAnnealingSolver.Solve(instance);

            Console.WriteLine(solution1);
            Console.WriteLine(solution2);
        }
示例#4
0
        private static void RunSimulatedAnnealingForConfiguration(double initTemperature, double frozenTemperature, double coolingCoeficient, int equilibriumCoeficient)
        {
            Console.WriteLine($"Init temperature:       {initTemperature}");
            Console.WriteLine($"Frozen temperature:     {frozenTemperature}");
            Console.WriteLine($"Cooling coeficient:     {coolingCoeficient}");
            Console.WriteLine($"Equilibrium coeficient: {equilibriumCoeficient}");
            Console.WriteLine();

            var instanceProvider         = new RandomInstanceProvider();
            var exactSolver              = new BrutteForceSolver();
            var simulatedAnnealingSolver = new SimulatedAnnealingSolver(initTemperature, frozenTemperature, coolingCoeficient, equilibriumCoeficient);

            var runner = new CompareRunner();

            runner.Run(instanceProvider, exactSolver, simulatedAnnealingSolver);
            Console.WriteLine();
        }
示例#5
0
        public void TestBrutteForce()
        {
            // n = 4
            // F = (x0 + x2' + x3).(x0' + x1 + x2').(x2 + x3).(x0 + x1 + x2' + x3').(x1' + x2).(x2' + x3')
            // W = (2, 4, 1, 6)
            // expected solution: {1, 0, 0, 1}, weight: 8

            var formula = new LogicalProductFormula(new IFormula[]
            {
                // (x0 + x2' + x3)
                new LogicalSumFormula(new IFormula[]
                {
                    new LiteralFormula(0),
                    new NegateFormula(new LiteralFormula(2)),
                    new LiteralFormula(3)
                }),
                // (x0' + x1 + x2')
                new LogicalSumFormula(new IFormula[]
                {
                    new NegateFormula(new LiteralFormula(0)),
                    new LiteralFormula(1),
                    new NegateFormula(new LiteralFormula(2)),
                }),
                // (x2 + x3)
                new LogicalSumFormula(new IFormula[]
                {
                    new LiteralFormula(2),
                    new LiteralFormula(3)
                }),
                // (x0 + x1 + x2' + x3')
                new LogicalSumFormula(new IFormula[]
                {
                    new LiteralFormula(0),
                    new LiteralFormula(1),
                    new NegateFormula(new LiteralFormula(2)),
                    new NegateFormula(new LiteralFormula(3)),
                }),
                // (x1' + x2)
                new LogicalSumFormula(new IFormula[]
                {
                    new NegateFormula(new LiteralFormula(1)),
                    new LiteralFormula(2)
                }),
                // (x2' + x3')
                new LogicalSumFormula(new IFormula[]
                {
                    new NegateFormula(new LiteralFormula(2)),
                    new NegateFormula(new LiteralFormula(3))
                })
            });

            Console.WriteLine(formula);

            var instance = new Instance
            {
                Formula = formula,
                Weights = new [] { 2, 4, 1, 6 }
            };

            var solver = new BrutteForceSolver();
            var res    = solver.Solve(instance);

            var expectedConfiguration = 1L | (1L << 3);

            Assert.AreEqual(expectedConfiguration, res.Configuration);
            Assert.AreEqual(8, res.Weight);
        }