public void CancellationWorks()
        {
            var gurobiEnvironment   = new GRBEnv();
            var runnerConfiguration =
                new GurobiRunnerConfiguration.GurobiRunnerConfigBuilder().Build(TimeSpan.FromSeconds(30));

            var timer = new Stopwatch();

            timer.Start();

            // Run Gurobi.
            var gurobiRunner = new GurobiRunner(gurobiEnvironment, runnerConfiguration);
            var runner       = gurobiRunner.Run(
                new InstanceSeedFile(GurobiRunnerTests.PathToTestInstance, GurobiRunnerTests.TestInstanceSeed),
                this._cancellationTokenSource.Token);

            // Cancel task and expect it to be cancelled.
            Thread.Sleep(100);
            this._cancellationTokenSource.Cancel();
            runner.Wait();
            timer.Stop();
            var result = runner.Result;

            result.IsCancelled.ShouldBeTrue();
            timer.Elapsed.ShouldBeLessThan(TimeSpan.FromSeconds(1));
            gurobiEnvironment.Dispose();
        }
Beispiel #2
0
        public void CancellationWorks()
        {
            var gurobiEnvironment   = new GRBEnv();
            var runnerConfiguration =
                new GurobiRunnerConfiguration.GurobiRunnerConfigBuilder().Build(TimeSpan.FromSeconds(1));
            var tunerConfiguration = new AlgorithmTunerConfiguration();

            // Note, that this cancellation token source is never used in GurobiRunner.Run().
            var cancellationTokenSource = new CancellationTokenSource(500);

            var timer = new Stopwatch();

            timer.Start();

            var gurobiRunner = new GurobiRunner(gurobiEnvironment, runnerConfiguration, tunerConfiguration);
            var runner       = gurobiRunner.Run(
                new InstanceSeedFile(GurobiRunnerTests.PathToTestInstance, GurobiRunnerTests.TestInstanceSeed),
                cancellationTokenSource.Token);

            runner.Wait();
            timer.Stop();
            var result = runner.Result;

            result.IsCancelled.ShouldBeTrue();
            timer.Elapsed.ShouldBeGreaterThan(TimeSpan.FromMilliseconds(1000));
            timer.Elapsed.ShouldBeLessThan(TimeSpan.FromMilliseconds(1900));
            gurobiEnvironment.Dispose();
        }
Beispiel #3
0
        public void BuildWithFallBackPrioritizesFallbackArgumentsCorrectly()
        {
            const int ThreadCountFallback   = 12;
            const int NumberOfSeedsFallback = 10;

            var fallback = new GurobiRunnerConfiguration.GurobiRunnerConfigBuilder()
                           .SetThreadCount(ThreadCountFallback)
                           .SetNumberOfSeeds(NumberOfSeedsFallback)
                           .Build(TimeSpan.FromSeconds(5));

            var config = new GurobiRunnerConfiguration.GurobiRunnerConfigBuilder()
                         .SetThreadCount(GurobiRunnerConfiguration.GurobiRunnerConfigBuilder.ThreadCountDefault)
                         .BuildWithFallback(fallback);

            config.ThreadCount.ShouldBe(GurobiRunnerConfiguration.GurobiRunnerConfigBuilder.ThreadCountDefault);
            config.NumberOfSeeds.ShouldBe(NumberOfSeedsFallback);
        }
Beispiel #4
0
        public void TryToGetResultFromStringArrayWorksForGurobiResults()
        {
            var timeout             = TimeSpan.FromSeconds(30);
            var gurobiResult        = new GurobiResult(0.5, timeout, TargetAlgorithmStatus.CancelledByTimeout, true);
            var gurobiConfiguration = new GurobiRunnerConfiguration.GurobiRunnerConfigBuilder().Build(timeout);
            var tunerConfiguration  = new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder().SetCpuTimeout(timeout)
                                      .Build(1);
            var targetAlgorithmFactory =
                new GurobiRunnerFactory(gurobiConfiguration, tunerConfiguration) as
                ITargetAlgorithmFactory <GurobiRunner, InstanceSeedFile, GurobiResult>;

            targetAlgorithmFactory.TryToGetResultFromStringArray(gurobiResult.ToStringArray(), out var result).ShouldBeTrue();
            result.TargetAlgorithmStatus.ShouldBe(gurobiResult.TargetAlgorithmStatus);
            result.IsCancelled.ShouldBe(gurobiResult.IsCancelled);
            result.Runtime.ShouldBe(gurobiResult.Runtime);
            result.Gap.ShouldBe(gurobiResult.Gap);
            result.HasValidSolution.ShouldBe(gurobiResult.HasValidSolution);
        }