Exemple #1
0
        public void TestStopCondition()
        {
            // set the seed manually.
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create solver.
            var solver = new VNSSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float>(
                new GeneratorMock(), new PerturberMock(), new LocalSearchMock(), (i, l, p, o, s) =>
            {
                return(s.Value < 100);
            });

            // run solver but stop when after a few reported improvements.
            var best = new SolutionMock()
            {
                Value = 1000
            };

            solver.IntermidiateResult += (s) =>
            {
                best = s;
            };
            var solution = solver.Solve(new ProblemMock()
            {
                Max = 1000
            }, new ObjectiveMock());

            Assert.AreEqual(best.Value, solution.Value);
            Assert.IsTrue(solution.Value < 100);
        }
Exemple #2
0
        public void TestName()
        {
            // create solver.
            var solver = new VNSSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float>(
                new GeneratorMock(), new PerturberMock(), new LocalSearchMock());

            Assert.AreEqual("VNS_[MOCK_GENERATOR_MOCK_PERTURBER_MOCK_LOCALSEARCH]", solver.Name);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            OsmSharp.Logging.Log.Enable();
            OsmSharp.Logging.Log.RegisterListener(new OsmSharp.WinForms.UI.Logging.ConsoleTraceListener());

            // set the seed manually.
            OsmSharp.Math.Random.StaticRandomGenerator.Set(116542346);

            var vnsSolver = new VNSSolver();
            var objective = new FeasibleObjective();

            Program.SolveAll(vnsSolver, "OsmSharp.TSPTW.Benchmark.Problems.AFG", objective);
            Program.SolveAll(vnsSolver, "OsmSharp.TSPTW.Benchmark.Problems.Dumas", objective);

            Console.ReadLine();
        }
Exemple #4
0
        public void TestSolution5ClosedFixed()
        {
            // create problem.
            var problem = TSPTWHelper.CreateDirectedTSPTW(0, 4, 5, 10, 1);

            problem.Times.SetWeight(0, 1, 2);
            problem.Times.SetWeight(1, 2, 2);
            problem.Times.SetWeight(2, 3, 2);
            problem.Times.SetWeight(3, 4, 2);
            problem.Times.SetWeight(4, 0, 2);
            problem.Windows[2] = new TimeWindow()
            {
                Min = 3,
                Max = 5
            };
            problem.Windows[4] = new TimeWindow()
            {
                Min = 7,
                Max = 9
            };
            var objective = new TSPTWObjective();

            // create the solver.
            var solver = new VNSSolver();

            for (int i = 0; i < 10; i++)
            {
                // generate solution.
                float fitness;
                var   solution = solver.Solve(problem, objective, out fitness);

                // test contents.
                Assert.IsTrue(fitness <= 12);
                var solutionList = new List <int>();
                foreach (var directed in solution)
                {
                    solutionList.Add(DirectedHelper.ExtractId(directed));
                }
                Assert.AreEqual(5, solutionList.Count);
                Assert.AreEqual(0, solutionList[0]);
                Assert.AreEqual(4, solutionList[4]);
            }
        }
Exemple #5
0
        public void TestSolution5ClosedNotFixed()
        {
            // create problem.
            var problem = TSPTWHelper.CreateTSPTW(0, 0, 5, 10);

            problem.Times[0][1] = 2;
            problem.Times[1][2] = 2;
            problem.Times[2][3] = 2;
            problem.Times[3][4] = 2;
            problem.Times[4][0] = 2;
            problem.Windows[2]  = new TimeWindow()
            {
                Min = 3,
                Max = 5
            };
            problem.Windows[4] = new TimeWindow()
            {
                Min = 7,
                Max = 9
            };
            var objective = new TSPTWObjective();

            // create the solver.
            var solver = new VNSSolver();

            for (int i = 0; i < 10; i++)
            {
                // generate solution.
                float fitness;
                var   solution = solver.Solve(problem, objective, out fitness);

                // test contents.
                Assert.AreEqual(10, fitness);
                var solutionList = new List <int>(solution);
                Assert.AreEqual(0, solutionList[0]);
                Assert.IsTrue(solutionList.Remove(0));
                Assert.IsTrue(solutionList.Remove(1));
                Assert.IsTrue(solutionList.Remove(2));
                Assert.IsTrue(solutionList.Remove(3));
                Assert.IsTrue(solutionList.Remove(4));
                Assert.AreEqual(0, solutionList.Count);
            }
        }
Exemple #6
0
        public void TestSolution1()
        {
            // create problem.
            var problem   = TSPTWHelper.CreateDirectedTSPTW(0, 0, 1, 0, 1);
            var objective = new TSPTWObjective();

            // create the solver.
            var solver = new VNSSolver();

            for (int i = 0; i < 10; i++)
            {
                // generate solution.
                float fitness;
                var   solution = solver.Solve(problem, objective, out fitness);

                // test contents.
                Assert.IsTrue(fitness < 2);
                var solutionList = new List <int>(solution);
                Assert.AreEqual(0, DirectedHelper.ExtractId(solutionList[0]));
            }
        }
Exemple #7
0
        public void TestSolution1()
        {
            // create problem.
            var problem   = TSPTWHelper.CreateTSPTW(0, 0, 1, 0);
            var objective = new TSPTWObjective();

            // create the solver.
            var solver = new VNSSolver();

            for (int i = 0; i < 10; i++)
            {
                // generate solution.
                float fitness;
                var   solution = solver.Solve(problem, objective, out fitness);

                // test contents.
                Assert.AreEqual(0, fitness);
                var solutionList = new List <int>(solution);
                Assert.AreEqual(0, solutionList[0]);
                Assert.IsTrue(solutionList.Remove(0));
                Assert.AreEqual(0, solutionList.Count);
            }
        }
Exemple #8
0
        public void TestSolution5ClosedNotFixed()
        {
            // create problem.
            var problem = TSPTWHelper.CreateDirectedTSPTW(0, 0, 5, 10, 1);

            problem.Times.SetWeight(0, 1, 2);
            problem.Times.SetWeight(1, 2, 2);
            problem.Times.SetWeight(2, 3, 2);
            problem.Times.SetWeight(3, 4, 2);
            problem.Times.SetWeight(4, 0, 2);
            problem.Windows[2] = new TimeWindow()
            {
                Min = 3,
                Max = 5
            };
            problem.Windows[4] = new TimeWindow()
            {
                Min = 7,
                Max = 9
            };
            var objective = new TSPTWObjective();

            // create the solver.
            var solver = new VNSSolver();

            for (int i = 0; i < 10; i++)
            {
                // generate solution.
                float fitness;
                var   solution = solver.Solve(problem, objective, out fitness);

                // test contents.
                Assert.AreEqual(10, fitness);
                var solutionList = new List <int>(solution);
                Assert.AreEqual(5, solutionList.Count);
            }
        }
Exemple #9
0
        public void TestStop()
        {
            // set the seed manually.
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create solver.
            var solver = new VNSSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float>(
                new GeneratorMock(), new PerturberMock(), new LocalSearchMock());

            // run solver but stop when after a few reported improvements.
            var intermediateCount = 0;
            var best = new SolutionMock()
            {
                Value = 1000
            };

            solver.IntermidiateResult += (s) =>
            {
                intermediateCount++;
                if (intermediateCount > 5)
                { // this should never ever happen!
                    Assert.Fail("Solver has not stopped!");
                }
                if (intermediateCount == 5)
                { // stop solver.
                    solver.Stop();
                    best = s;
                }
            };
            var solution = solver.Solve(new ProblemMock()
            {
                Max = 1000
            }, new ObjectiveMock());

            Assert.AreEqual(best.Value, solution.Value);
        }