static void Run3SATSolver(string filePath, GeneticOptions options, bool verbose = false) { var instance = SATInstanceLoader.LoadInstance(filePath); var problem = new ThreeSATProblem(instance.Formula); var stopwatch = new Stopwatch(); GAResult result = null; double relErr = 0; double time = 0; for (int i = 0; i < MeasurementCount; i++) { stopwatch.Reset(); stopwatch.Start(); result = GeneticAlgorithm.Solve(problem, options, verbose); stopwatch.Stop(); time += stopwatch.Elapsed.TotalMilliseconds; relErr = CalculateRelativeError(instance.OptimalWeight, result.Result.Fitness); } if (verbose) { Console.WriteLine(); PrintConfig(result); } var relativeErr = CalculateRelativeError(instance.OptimalWeight, result.Result.Fitness) * 100; if (verbose) { Console.WriteLine("Optimal result is '{0}' and approx. '{1}'. Relative error is '{2}%'.", instance.OptimalWeight, result.Result.Fitness, relativeErr); } if (verbose) { Console.WriteLine(); } time /= MeasurementCount; Console.WriteLine(FormatCsvResult(instance.InstanceId, time, relErr)); }
public void FitnessFunctionTest() { var formula = new Formula(3, 3); formula.Clauses = new int[] { 1, 2, 3, -1, -2, -3, -1, -2, -3 }; formula.Weights = new int[] { 1, 2, 3 }; var problem = new ThreeSATProblem(formula); var expectedWinner = new Individual(new bool[] { false, true, true }); var initialPopulation = new List <Individual> { new Individual(new bool[] { false, false, true }), new Individual(new bool[] { true, true, true }), expectedWinner }; var population = new Population(100, 2, problem, initialPopulation); population.MakeSelection(3); var winner = population.GetFittestIndividual(); Assert.Equal(expectedWinner, winner); }