public void EqualsReturnsFalseForDifferentSeed() { var firstInstance = new InstanceSeedFile("1", 42); var secondInstance = new InstanceSeedFile("1", 43); Assert.False( object.Equals(firstInstance, secondInstance), $"Instance {firstInstance} should not be equal to instance {secondInstance}."); }
public void TryToGetInstanceFromInstanceIdWorksForInstanceSeedFiles() { var instanceSeedFile = new InstanceSeedFile("dummy", 42); var targetAlgorithmFactory = new DummyTargetAlgorithmFactory <DummyTargetAlgorithm <InstanceSeedFile, TestResult>, InstanceSeedFile, TestResult>() as ITargetAlgorithmFactory <DummyTargetAlgorithm <InstanceSeedFile, TestResult>, InstanceSeedFile, TestResult>; targetAlgorithmFactory.TryToGetInstanceFromInstanceId(instanceSeedFile.ToId(), out var instance).ShouldBeTrue(); instance.Equals(instanceSeedFile).ShouldBeTrue(); }
/// <summary> /// Creates a cancellable task that runs a target algorithm on the given instance. /// </summary> /// <param name="instance">Instance to run on.</param> /// <param name="cancellationToken">Token that is regularly checked for cancellation. /// If cancellation is detected, the task will be stopped.</param> /// <returns>A task that returns statistics about the run on completion.</returns> public Task <TResult> Run(InstanceSeedFile instance, CancellationToken cancellationToken) { // Define process to target algorithm from command line. var processInfo = this.BuildProcessStartInfo(instance); // Make sure output and Error stores are clean. this.Output.Clear(); this.Error.Clear(); return(Task.Run( function: () => { // Start process and make sure it's cancelled if the cancellationToken is cancelled. using (var process = Process.Start(processInfo)) using (var processRegistration = cancellationToken.Register(() => ProcessUtils.CancelProcess(process))) { LoggingHelper.WriteLine(VerbosityLevel.Trace, "Started process."); // Catch all output. // Read streams asynchronously to prevent deadlocks. process.OutputDataReceived += (s, e) => this.Output.AppendLine(e.Data); process.ErrorDataReceived += (s, e) => this.Error.AppendLine(e.Data); process.BeginOutputReadLine(); process.BeginErrorReadLine(); // Wait until end of process. process.WaitForExit(); if (cancellationToken.IsCancellationRequested) { this.HandleCancellation(process, cancellationToken); } // Parse result. OriginalRunResult originalRunResult; try { originalRunResult = this.ExtractRunResult(); } catch (Exception) { // Write information to file if anything goes wrong. this.WriteProcessOutputToFiles(process); throw; } var result = this.CreateRunResult(originalRunResult); LoggingHelper.WriteLine(VerbosityLevel.Debug, $"Result: {result}"); return result; } }, cancellationToken: cancellationToken)); }
public void EqualsReturnsTrueForEqualPathAndSeed() { var path = "1"; var seed = 42; var instance1 = new InstanceSeedFile(path, seed); var instance2 = new InstanceSeedFile(path, seed); Assert.True( object.Equals(instance1, instance2), $"{instance1} and {instance2} are supposedly different, but both encode instance {path}."); }
public void EqualsReturnsFalseForOneObjectNotInstanceSeedFile() { var instance = new InstanceSeedFile("1", 0); var wrongType = new InstanceFile("1"); Assert.False( object.Equals(instance, wrongType), $"Instance {instance} was identified to be equal to {wrongType}."); Assert.False( object.Equals(wrongType, instance), $"Instance {wrongType} was identified to be equal to {instance}."); }
public void CreateInstancesThrowsExceptionIfItCannotOpenFolder() { Exception exception = Assert.Throws <DirectoryNotFoundException>( () => { InstanceSeedFile.CreateInstanceSeedFilesFromDirectory( "foobarFolder", InstanceSeedFileTest.ValidInstanceExtensions, 1, 42); }); }
public void GetHashCodeReturnsDifferentHashCodesForDifferentSeeds() { var instance1 = new InstanceSeedFile("1", 42); var instance2 = new InstanceSeedFile("1", 43); Assert.False(object.Equals(instance1, instance2)); var firstInstanceHash = instance1.GetHashCode(); var secondInstanceHash = instance2.GetHashCode(); Assert.NotEqual( firstInstanceHash, secondInstanceHash); }
public void GetHashCodeReturnsSameHashCodesForEqualPathAndSeed() { var path = "1"; var seed = 42; var instance1 = new InstanceSeedFile(path, seed); var instance2 = new InstanceSeedFile(path, seed); var firstInstanceHash = instance1.GetHashCode(); var secondInstanceHash = instance2.GetHashCode(); Assert.Equal( firstInstanceHash, secondInstanceHash); }
public void CreateInstancesIgnoresNonValidFiles() { // Call method. var instances = InstanceSeedFile.CreateInstanceSeedFilesFromDirectory( this._pathToTestInstanceFolder, InstanceSeedFileTest.ValidInstanceExtensions, 1, 42); // Check that no non valid file has been translated into an instance. var instancePaths = instances.Select(instance => instance.Path); instancePaths.Any(path => InstanceSeedFileTest.NonValidFileNames.Any(path.Contains)) .ShouldBeFalse("Not all non valid files have been ignored."); }
/// <summary> /// Creates the command to execute the target algorithm on a specific instance. /// </summary> /// <param name="instance">The instance to execute the algorithm on.</param> /// <returns>The created command.</returns> private string CreateTargetAlgorithmCommand(InstanceSeedFile instance) { // Run length and instance specific information are not used by any relevant target algorithm. var instanceSpecificInfoDummy = 0; var runLengthDummy = 0; // AcLib assumes positive seeds. Map to complete unsigned range by unchecked environment. uint positiveSeed; unchecked { positiveSeed = (uint)instance.Seed; } return ($"{this.Scenario.Command} {instance.Path} {instanceSpecificInfoDummy} {this.Scenario.CutoffTime.TotalSeconds} {runLengthDummy} {positiveSeed} {this._commandParameters}"); }
public void CreateInstancesCorrectlyExtractsPathsToValidFiles() { // Call method. var instances = InstanceSeedFile.CreateInstanceSeedFilesFromDirectory( this._pathToTestInstanceFolder, InstanceSeedFileTest.ValidInstanceExtensions, 1, 42); // Check that file names of instances match the complete paths of all valid files. var expectedPaths = InstanceSeedFileTest.ValidFileNames .Select(name => this._pathToTestInstanceFolder + Path.DirectorySeparatorChar + name).ToList(); var instancePaths = instances.Select(instance => instance.Path).ToList(); expectedPaths.ShouldBe( instancePaths, true, $"{TestUtils.PrintList(instancePaths)} should have been equal to {TestUtils.PrintList(expectedPaths)}."); }
/// <summary> /// Builds the <see cref="ProcessStartInfo"/> for starting the algorithm on the given instance. /// </summary> /// <param name="instance">The instance to start the algorithm on.</param> /// <returns>The built <see cref="ProcessStartInfo"/>.</returns> private ProcessStartInfo BuildProcessStartInfo(InstanceSeedFile instance) { // Create the process information using the correct program and parameters. var command = this.CreateTargetAlgorithmCommand(instance); var processInfo = new ProcessStartInfo( fileName: command.Split(' ').First(), arguments: command.Substring(command.IndexOf(' ') + 1)) { // Make sure no additional window will be opened on process start. CreateNoWindow = true, UseShellExecute = false, // Redirect output to read information from it. RedirectStandardOutput = true, RedirectStandardError = true, }; return(processInfo); }
BuildLingelingAlgorithmTuner( AlgorithmTunerConfiguration tunerConfig, string trainingInstanceFolder, string testInstanceFolder, LingelingRunnerConfiguration runnerConfig) { IRunEvaluator <InstanceSeedFile, RuntimeResult> runEvaluator; if (runnerConfig.FactorParK == 0) { runEvaluator = new SortByUnpenalizedRuntime <InstanceSeedFile>(tunerConfig.CpuTimeout); } else { runEvaluator = new SortByPenalizedRuntime <InstanceSeedFile>(runnerConfig.FactorParK, tunerConfig.CpuTimeout); } var tuner = new AlgorithmTuner <LingelingRunner, InstanceSeedFile, RuntimeResult, TLearnerModel, TPredictorModel, TSamplingStrategy>( targetAlgorithmFactory: new LingelingRunnerFactory(runnerConfig.PathToExecutable, tunerConfig, runnerConfig.MemoryLimitMegabyte), runEvaluator: runEvaluator, trainingInstances: InstanceSeedFile.CreateInstanceSeedFilesFromDirectory( trainingInstanceFolder, LingelingUtils.ListOfValidFileExtensions, runnerConfig.NumberOfSeeds, runnerConfig.RngSeed), parameterTree: LingelingUtils.CreateParameterTree(), configuration: tunerConfig); if (!string.IsNullOrWhiteSpace(testInstanceFolder)) { tuner.SetTestInstances( InstanceSeedFile.CreateInstanceSeedFilesFromDirectory( testInstanceFolder, LingelingUtils.ListOfValidFileExtensions, runnerConfig.NumberOfSeeds, runnerConfig.RngSeed)); } return(tuner); }
/// <summary> /// Builds an instance of the <see cref="AlgorithmTuner{TTargetAlorithm,TInstance,TResult}" /> class for tuning Gurobi. /// </summary> /// <param name="tunerConfig">The <see cref="AlgorithmTunerConfiguration" /> to use.</param> /// <param name="pathToTrainingInstanceFolder">The path to the folder containing training instances.</param> /// <param name="pathToTestInstanceFolder">The path to test instance folder.</param> /// <param name="gurobiConfigBuilder">The gurobi configuration builder.</param> /// <returns> /// The built instance. /// </returns> public static AlgorithmTuner <GurobiRunner, InstanceSeedFile, GurobiResult> BuildGurobiRunner( AlgorithmTunerConfiguration tunerConfig, string pathToTrainingInstanceFolder, string pathToTestInstanceFolder, GurobiRunnerConfiguration.GurobiRunnerConfigBuilder gurobiConfigBuilder) { var gurobiConfig = Program.BuildGurobiConfigAndCheckThreadCount(gurobiConfigBuilder, tunerConfig); var tuner = new AlgorithmTuner <GurobiRunner, InstanceSeedFile, GurobiResult>( targetAlgorithmFactory: new GurobiRunnerFactory(gurobiConfig, tunerConfig), runEvaluator: new GurobiRunEvaluator(tunerConfig.CpuTimeout), trainingInstances: InstanceSeedFile.CreateInstanceSeedFilesFromDirectory( pathToTrainingInstanceFolder, GurobiUtils.ListOfValidFileExtensions, gurobiConfig.NumberOfSeeds, gurobiConfig.RngSeed), parameterTree: GurobiUtils.CreateParameterTree(), configuration: tunerConfig, customGrayBoxMethods: new GurobiGrayBoxMethods()); try { if (!string.IsNullOrWhiteSpace(pathToTestInstanceFolder)) { var testInstances = InstanceSeedFile.CreateInstanceSeedFilesFromDirectory( pathToTestInstanceFolder, GurobiUtils.ListOfValidFileExtensions, gurobiConfig.NumberOfSeeds, gurobiConfig.RngSeed); tuner.SetTestInstances(testInstances); } } catch { } return(tuner); }
public void ToStringReturnsPathAndSeed() { var instance = new InstanceSeedFile("bar/foo", 42); Assert.Equal("bar/foo_42", instance.ToString()); }
public void SeedIsSetCorrectly() { var instance = new InstanceSeedFile("foo", 42); Assert.Equal(42, instance.Seed); }
public void PathIsSetCorrectly() { var instance = new InstanceSeedFile("bar/foo", 0); Assert.Equal("bar/foo", instance.Path); }