コード例 #1
0
        public void TestSolutionClosed()
        {
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create problem.
            var problem = TSPHelper.CreateTSP(0, 0, 5, 10);

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

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

                // test contents.
                Assert.AreEqual(50, fitness);
                Assert.AreEqual(0, solution.First);
                Assert.AreEqual(0, solution.Last);
                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);
            }
        }
コード例 #2
0
        private void SolveMultiple100()
        {
            KnapsackInput input;

            for (int i = 0; i < 100; i++)
            {
                input = new KnapsackInput();
                input.GenerateRandomItems();
                var stopwatch     = new Stopwatch();
                var solverBaB     = new BranchAndBoundSolver(input.Items, input.Capacity);
                var solverRand    = new RandomSolver(input.Items, input.Capacity);
                var solverGreedy  = new GreedySolver(input.Items, input.Capacity);
                var solverDynamic = new DynamicProgrammingSolver(input.Items, input.Capacity);
                stopwatch.Start();
                solverBaB.Solve();
                stopwatch.Stop();
                timeResultsBaB.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds));
                stopwatch.Restart();
                solverRand.Solve();
                stopwatch.Stop();
                timeResultsRand.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds));
                stopwatch.Restart();
                solverGreedy.Solve();
                stopwatch.Stop();
                timeResultsGreedy.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds));
                stopwatch.Restart();
                solverDynamic.Solve();
                stopwatch.Stop();
                timeResultsDynamic.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds));
            }
            timeAverageGreedy  = Math.Round((timeResultsGreedy.Sum(x => x.Y) / 100), 4);
            timeAverageRand    = Math.Round((timeResultsRand.Sum(x => x.Y) / 100), 4);
            timeAverageBaB     = Math.Round((timeResultsBaB.Sum(x => x.Y) / 100), 4);
            timeAverageDynamic = Math.Round((timeResultsDynamic.Sum(x => x.Y) / 100), 4);
        }
コード例 #3
0
        public void TestSolutionOpen()
        {
            RandomGeneratorExtensions.GetGetNewRandom = () => new RandomGenerator(4541247);

            // create problem.
            var problem = STSPHelper.CreateDirectedSTSP(0, 5, 10, 1, 40);

            // create the solver.
            var solver    = new RandomSolver();
            var objective = new STSPObjective();

            for (var i = 0; i < 100; i++)
            {
                // generate solution.
                STSPFitness fitness;

                var solution = solver.Solve(problem, objective, out fitness);

                // test contents.
                Assert.AreEqual(problem.First, solution.First);
                Assert.AreEqual(problem.Last, solution.Last);
                Assert.AreEqual(solution.Count, fitness.Customers);
                Assert.IsTrue(40 >= fitness.Weight);
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            if (args.Length == 0 || args[0] == null)
            {
                PrintUsage();
                Console.WriteLine("Press any key to continue . . .");
                Console.ReadLine();
                return;
            }

            var fileLocation = args[0];

            if (File.Exists(fileLocation))
            {
                var data = GetData(fileLocation);

                Solver.NewHighScoreFound += s_NewHighScoreFound;

                var ct = CancellationTokenSource.Token;
                Solver.CancellationTokenSource = CancellationTokenSource;

                try {
                    Task.Factory.StartNew(ListenForKeys);
                    _start = DateTime.Now;
                    Task.Factory.StartNew(() => {
                        Solver.Solve(data.Item1, data.Item2);
                        Console.WriteLine("Press enter to close.");
                        Console.ReadLine();
                    }, CancellationTokenSource.Token).Wait(ct);
                } catch (OperationCanceledException e) {
                    Console.WriteLine("Operation was canceled. Writing current best solution to file.");

                    var currentBest = Solver.BestMoveList;
                    Console.WriteLine(currentBest.ToString(data.Item2.ToList()));
                    //OpenMovesStreamWriter();
                    //board.Reset();
                    //board.BoardChanged += WriteMoveToFile;
                    //board.ApplyMoveList( currentBest );
                    //board.Reset();
                    //CloseMovesStreamWriter();

                    //var lastsolution = solver.LastGeneratedSolution;
                    //Console.WriteLine( lastsolution.ToImportableString() );

                    //WriteBestSolution( currentBest );
                    Console.WriteLine("Press enter to close.");
                    Console.ReadLine();
                } catch (AggregateException e) {
                    foreach (var v in e.InnerExceptions)
                    {
                        Console.WriteLine(e.Message + " " + v.Message);
                    }
                } finally {
                    CancellationTokenSource.Dispose();
                }
            }
        }
コード例 #5
0
        public void TestOpen()
        {
            var problem     = TSPHelper.CreateDirectedTSP(0, 10, 100, 5);
            var random      = new RandomSolver();
            var randomRoute = random.Solve(problem, new Optimization.TSP.Directed.TSPObjective());

            var   op = new DirectionLocalSearch();
            float delta;
            var   optimized = op.Apply(problem, new Optimization.TSP.Directed.TSPObjective(), randomRoute, out delta);
        }
コード例 #6
0
ファイル: ProblemSolver.cs プロジェクト: mblk/ResourcePlaner
        public ISolution Solve()
        {
            AvailableResourceSolver availableResourceSolver = new AvailableResourceSolver(_problem);

            availableResourceSolver.FindAvailableResources();

            RandomSolver solver = new RandomSolver(_problem);

            var solution = solver.Solve();



            return(null);
        }
コード例 #7
0
 private void Rand_Click(object sender, RoutedEventArgs e)
 {
     if (input == null)
     {
         MessageBox.Show("You should choose data first", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
     }
     else
     {
         var stopwatch = new Stopwatch();
         var solver    = new RandomSolver(input.Items, input.Capacity);
         stopwatch.Start();
         var solution = solver.Solve();
         stopwatch.Stop();
         itemsGrid.DataContext    = solution.Items;
         allItemsGrid.DataContext = input.Items;
         resultTextBlock.Text     = "Capacity: " + solution.Capacity + ". Total value = " + solution.Value + ". Total weight = "
                                    + solution.TotalWeight + "." + " Time: " + stopwatch.Elapsed;
     }
 }