Beispiel #1
0
        /// <summary>
        /// Solves this TSP using a default solver.
        /// </summary>
        /// <returns></returns>
        public Tour Solve()
        {
            var solver = new IterativeSolver <float, TSProblem, TSPObjective, Tour, float>(
                new Solvers.HillClimbing3OptSolver(), 100, new CheapestInsertionOperator(2), new DirectionLocalSearch());

            return(this.Solve(solver));
        }
        public void TestStopCondition()
        {
            // set the seed manually.
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create solver.
            var best = new SolutionMock()
            {
                Value = 1000
            };
            IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float> solver = null;

            solver = new IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float>(
                new GeneratorMock(), 10, (i, p, o, s) =>
            {
                if (s != null && best.Value > s.Value)
                {     // keep best solution.
                    best = s;
                    if (best.Value < 100)
                    {
                        return(true);
                    }
                }
                return(false);
            });

            // run solver.
            var solution = solver.Solve(new ProblemMock()
            {
                Max = 1000
            }, new ObjectiveMock());

            Assert.AreEqual(best.Value, solution.Value);
        }
        public void TestName()
        {
            // create solver.
            var solver = new IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float>(
                new GeneratorMock(), 10);

            Assert.AreEqual("ITER_[10xMOCK_GENERATOR]", solver.Name);
        }
        public void Solve_WithMultipleInstances_SolvesCorrectly()
        {
            var result = new IterativeSolver(ReadSample("Multiple"), new List <string> {
                "KEYWORD"
            }).Solve();

            Assert.Equal(4, result["KEYWORD"].Count);
        }
Beispiel #5
0
        /// <summary>
        /// Solves this TSP using a default solver.
        /// </summary>
        /// <returns></returns>
        public Tour Solve()
        {
            var solver = new IterativeSolver <float, STSProblem, STSPObjective, Tour, STSPFitness>(
                new STSP.Directed.Solver.RandomSolver(), 100, new IterativeOperator <float, STSProblem, STSPObjective, Tour, STSPFitness>(
                    new CheapestInsertionOperator(3, 4), 250), new DirectionLocalSearchOperator());

            return(this.Solve(solver));
        }
        public void FormatWithSolutions_WithSingleCharFormatterWithManySolutions_FormatsCorrectly()
        {
            var f         = new WordSearchFormatter(new TestSolutionFormatter(), 0);
            var expected  = "***de*\ng*ij*l\nm*o*qr\nstuvwx\nyz***d";
            var words     = new[] { "abc", "bhn", "fkp" };
            var locations = new IterativeSolver(SixByFiveWordSearch(), words).Solve().Values.SelectMany(l => l);

            Assert.Equal(expected, f.Format(SixByFiveWordSearch(), locations));
        }
        public void Solve_WithOverlappingWordsWhenAllowed_FindsAll()
        {
            var result =
                new IterativeSolver(ReadSample("MultipleFromSameLocation"), ReadWords("MultipleFromSameLocation"), true)
                .Solve();

            AssertWordLocation(9, 14, 8, 13, result["KE"][0]);
            AssertWordLocation(9, 14, 7, 12, result["KEY"][0]);
            AssertWordLocation(9, 14, 3, 8, result["KEYWORD"][0]);
            AssertWordLocation(9, 14, 2, 7, result["KEYWORDS"][0]);
            AssertWordLocation(9, 14, 0, 5, result["KEYWORDSER"][0]);
        }
        public void Solve_WithOverlappingWordsWhenDisallowed_FindsOnlyShortest()
        {
            var result =
                new IterativeSolver(ReadSample("MultipleFromSameLocation"), ReadWords("MultipleFromSameLocation"))
                .Solve();

            AssertWordLocation(9, 14, 8, 13, result["KE"][0]);
            Assert.Empty(result["KEY"]);
            Assert.Empty(result["KEYWORD"]);
            Assert.Empty(result["KEYWORDS"]);
            Assert.Empty(result["KEYWORDSER"]);
        }
    public static void Main(string[] args)
    {
        int limit         = 20000;
        int numIterations = 1000;

        var recursiveSolver = new RecursiveSolver();
        var iterativeSolver = new IterativeSolver();

        Console.Write("Testing iterative approach... ");
        GetAverageResults(numIterations, limit, iterativeSolver);

        Console.Write("Testing recursive approach... ");
        GetAverageResults(numIterations, limit, recursiveSolver);

        Console.WriteLine("Complete!");
        Console.ReadKey();
    }
Beispiel #10
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Get a CurrencyHolder holding notations made from dataGrid data in notationsList
            var holder = new CurrencyHolder(notationsList.Select(f => f.GetNotation()).ToList());
            var value  = inputTextBox.Text;

            IChangeRounder rounder = null;

            if (NoroundRadioButton.IsChecked.GetValueOrDefault(false))
            {
                rounder = new NoRounding();
            }
            if (NaiveroundingRadioButton.IsChecked.GetValueOrDefault(false))
            {
                rounder = new NaiveRounder();
            }

            IChangeSolver solver = null;

            if (recursiveRadioButton.IsChecked.GetValueOrDefault(false))
            {
                solver = new RecursiveSolver(rounder);
            }
            if (iterativeRadioButton.IsChecked.GetValueOrDefault(false))
            {
                solver = new IterativeSolver(rounder);
            }

            if (solver != null && decimal.TryParse(value, out decimal amount))
            {
                solver.FindReturnFor(holder, amount);

                // Update dataGrid with result
                notationsList.ForEach(f => f.Update());
                dataGrid.Items.Refresh();

                changeLabel.Content = $"Amount left: {amount - holder.SumTaken()}";
            }
            else
            {
                changeLabel.Content = "Input is not a valid number";
                inputTextBox.Text   = "111.55";
            }
        }
        public void TestNoStopCondition()
        {
            // set the seed manually.
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create solver.
            var solver = new IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float>(
                new GeneratorMock(), 10);

            // run solver.
            var solution = solver.Solve(new ProblemMock()
            {
                Max = 1000
            }, new ObjectiveMock());

            // nothing we can say about the state of the solution.
            // IDEA: improve testability by creating a mock random generator and letting is generate best solutions and then check the result.
            // TODO: when Itinero allows Itinero.Math.Random.StaticRandomGenerator.Set(new DummyGenerator()); improve this part.
        }
        public void Solve_WithTypicalWordSearch_SolvesCorrectly()
        {
            var result = new IterativeSolver(ReadSample("HarryPotter"), ReadWords("HarryPotter")).Solve();

            AssertWordLocation(0, 5, 4, 5, result["HARRY"][0]);
            AssertWordLocation(4, 0, 9, 5, result["POTTER"][0]);
            AssertWordLocation(3, 8, 11, 8, result["JKROWLING"][0]);
            AssertWordLocation(4, 7, 9, 7, result["RONALD"][0]);
            AssertWordLocation(12, 0, 12, 6, result["WEASLEY"][0]);
            AssertWordLocation(4, 9, 11, 9, result["HERMIONE"][0]);
            AssertWordLocation(7, 10, 13, 10, result["GRANGER"][0]);
            AssertWordLocation(13, 1, 13, 8, result["HOGWARTS"][0]);
            AssertWordLocation(4, 11, 13, 11, result["WITCHCRAFT"][0]);
            AssertWordLocation(12, 7, 5, 0, result["WIZARDRY"][0]);
            AssertWordLocation(1, 6, 9, 6, result["VOLDEMORT"][0]);
            AssertWordLocation(0, 10, 4, 10, result["ALBUS"][0]);
            AssertWordLocation(5, 5, 5, 2, result["SCAR"][0]);
            AssertWordLocation(11, 0, 11, 5, result["HAGRID"][0]);
            AssertWordLocation(3, 0, 10, 7, result["DARKARTS"][0]);
            AssertWordLocation(0, 1, 0, 9, result["SLYTHERIN"][0]);
        }
        public void TestStop()
        {
            // set the seed manually.
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create solver.
            var best = new SolutionMock()
            {
                Value = 1000
            };
            IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float> solver = null;

            solver = new IterativeSolver <float, ProblemMock, ObjectiveMock, SolutionMock, float>(
                new GeneratorMock(), 10, (i, p, o, s) =>
            {
                if (s != null && best.Value > s.Value)
                {     // keep best solution.
                    best = s;
                }
                if (i > 5)
                {     // stop solver.
                    solver.Stop();
                }
                if (i > 6)
                {     // solver was not stopped.
                    Assert.Fail("Solver has not stopped!");
                }
                // ... but always return false.
                return(false);
            });

            // run solver.
            var solution = solver.Solve(new ProblemMock()
            {
                Max = 1000
            }, new ObjectiveMock());

            Assert.AreEqual(best.Value, solution.Value);
        }
        public void Launch()
        {
            var result = new IterativeSolver(WordSearch, Words, AllowOverlapping).Solve();

            Console.WriteLine("Solved:\n");
            Console.WriteLine(Formatter.Format(WordSearch, result.Values.SelectMany(list => list)));

            Console.WriteLine();
            Console.WriteLine("Coordinates of Solutions:\n");

            foreach (var word in Words)
            {
                var entry = $"'{word}': ";
                var space = new string(' ', entry.Length);
                var first = result[word].Any() ? result[word][0].ToString() : "[None found]";

                Console.WriteLine(entry + first);

                foreach (var location in result[word].Skip(1))
                {
                    Console.WriteLine(space + location);
                }
            }
        }
        public void TestComplicatedNumber()
        {
            IChangeSolver solver = new IterativeSolver(rounder);

            SolverTestsCommon.TestComplicatedNumber(solver);
        }
        public void TestCustomNotationsWithLeftover()
        {
            IChangeSolver solver = new IterativeSolver(rounder);

            SolverTestsCommon.TestCustomNotationsWithLeftover(solver);
        }
        public void TestSimpleRounding()
        {
            IChangeSolver solver = new IterativeSolver(new NaiveRounder());

            SolverTestsCommon.TestSimpleRounder(solver);
        }