public override void Main() { DateTime start = DateTime.UtcNow; QuadraticAssignmentProblem qap; if (vars.Contains("qap")) qap = vars.qap; else { var provider = new DreznerQAPInstanceProvider(); var instance = provider.GetDataDescriptors().Single(x => x.Name == "dre56"); var data = provider.LoadData(instance); qap = new QuadraticAssignmentProblem(); qap.Load(data); vars.qap = qap; } const uint seed = 0; const int popSize = 100; const int generations = 1000; const double mutationRate = 0.05; var random = new MersenneTwister(seed); var population = new Permutation[popSize]; var qualities = new double[popSize]; var nextGen = new Permutation[popSize]; var nextQual = new double[popSize]; var qualityChart = new DataTable("Quality Chart"); var qualityRow = new DataRow("Best Quality"); qualityChart.Rows.Add(qualityRow); vars.qualityChart = qualityChart; for (int i = 0; i < popSize; i++) { population[i] = new Permutation(PermutationTypes.Absolute, qap.Weights.Rows, random); qualities[i] = QAPEvaluator.Apply(population[i], qap.Weights, qap.Distances); } var bestQuality = qualities.Min(); var bestQualityGeneration = 0; for (int g = 0; g < generations; g++) { var parents = population.SampleProportional(random, 2 * popSize, qualities, windowing: true, inverseProportional: true).ToArray(); for (int i = 0; i < popSize; i++) { nextGen[i] = PartiallyMatchedCrossover.Apply(random, parents[i * 2], parents[i * 2 + 1]); if (random.NextDouble() < mutationRate) Swap2Manipulator.Apply(random, nextGen[i]); nextQual[i] = QAPEvaluator.Apply(nextGen[i], qap.Weights, qap.Distances); if (nextQual[i] < bestQuality) { bestQuality = nextQual[i]; bestQualityGeneration = g; } } qualityRow.Values.Add(bestQuality); Array.Copy(nextGen, population, popSize); Array.Copy(nextQual, qualities, popSize); } vars.elapsed = new TimeSpanValue(DateTime.UtcNow - start); vars.bestQuality = bestQuality; vars.bestQualityFoundAt = bestQualityGeneration; }
private CSharpScript CreateGAQAPScript() { var script = new CSharpScript { Name = ScriptItemName, Description = ScriptItemDescription }; #region Variables var provider = new DreznerQAPInstanceProvider(); var instance = provider.GetDataDescriptors().Single(x => x.Name == ProblemInstanceName); var data = provider.LoadData(instance); var problem = new QuadraticAssignmentProblem(); problem.Load(data); script.VariableStore.Add(ProblemInstanceName, problem); #endregion #region Code script.Code = ScriptSources.GAQAPScriptSource; #endregion return script; }
public void TestQAPLIBInstances() { var provider = new QAPLIBInstanceProvider(); var qap = new QuadraticAssignmentProblem(); var failedInstances = new StringBuilder(); var instances = provider.GetDataDescriptors(); Assert.IsTrue(instances.Any(), "No instances could be found."); foreach (var instance in instances) { try { qap.Load(provider.LoadData(instance)); } catch (Exception ex) { failedInstances.AppendLine(instance + ": " + ex.Message); } } Assert.IsTrue(failedInstances.Length == 0, "Following instances failed: " + Environment.NewLine + failedInstances.ToString()); }
private CSharpScript CreateGAQAPScript() { var script = new CSharpScript { Name = ScriptItemName, Description = ScriptItemDescription }; #region Variables var provider = new DreznerQAPInstanceProvider(); var instance = provider.GetDataDescriptors().Single(x => x.Name == ProblemInstanceName); var data = provider.LoadData(instance); var problem = new QuadraticAssignmentProblem(); problem.Load(data); script.VariableStore.Add(ProblemInstanceName, problem); #endregion #region Code script.Code = ScriptSources.GAQAPScriptSource; #endregion return(script); }
public void TestQAPLIBSolutions() { var provider = new QAPLIBInstanceProvider(); var qap = new QuadraticAssignmentProblem(); var failedInstances = new StringBuilder(); var instances = provider.GetDataDescriptors(); Assert.IsTrue(instances.Any(), "No instances could be found."); foreach (var instance in instances) { qap.Load(provider.LoadData(instance)); if (qaplibInstances.ContainsKey(instance.Name) && qap.BestKnownQuality != null && qap.BestKnownQuality.Value != qaplibInstances[instance.Name]) { failedInstances.AppendLine(instance.Name + ": " + qap.BestKnownQuality.Value.ToString() + " vs " + qaplibInstances[instance.Name]); } } Assert.IsTrue(failedInstances.Length == 0, "Following instances/solutions have suspicious best quality: " + Environment.NewLine + failedInstances.ToString()); }
public void TestQAPLIBLowerBounds() { var provider = new QAPLIBInstanceProvider(); var qap = new QuadraticAssignmentProblem(); var failedInstances = new StringBuilder(); var instances = provider.GetDataDescriptors(); Assert.IsTrue(instances.Any(), "No instances could be found."); foreach (var instance in instances) { if (lowerBounds.ContainsKey(instance.Name)) { qap.Load(provider.LoadData(instance)); if (qap.LowerBound.Value != lowerBounds[instance.Name]) { failedInstances.AppendLine(instance.Name + ": The Gilmore-Lawler lower bound is not valid."); } } } Assert.IsTrue(failedInstances.Length == 0, "Following instances failed for the GLB calculation: " + Environment.NewLine + failedInstances.ToString()); }
public void TestQAPLIBLowerBounds() { var provider = new QAPLIBInstanceProvider(); var qap = new QuadraticAssignmentProblem(); var failedInstances = new StringBuilder(); var instances = provider.GetDataDescriptors(); Assert.IsTrue(instances.Any(), "No instances could be found."); foreach (var instance in instances) { if (lowerBounds.ContainsKey(instance.Name)) { qap.Load(provider.LoadData(instance)); if (qap.LowerBound.Value != lowerBounds[instance.Name]) failedInstances.AppendLine(instance.Name + ": The Gilmore-Lawler lower bound is not valid."); } } Assert.IsTrue(failedInstances.Length == 0, "Following instances failed for the GLB calculation: " + Environment.NewLine + failedInstances.ToString()); }
public void TestQAPLIBSolutions() { var provider = new QAPLIBInstanceProvider(); var qap = new QuadraticAssignmentProblem(); var failedInstances = new StringBuilder(); var instances = provider.GetDataDescriptors(); Assert.IsTrue(instances.Any(), "No instances could be found."); foreach (var instance in instances) { qap.Load(provider.LoadData(instance)); if (qaplibInstances.ContainsKey(instance.Name) && qap.BestKnownQuality != null && qap.BestKnownQuality.Value != qaplibInstances[instance.Name]) failedInstances.AppendLine(instance.Name + ": " + qap.BestKnownQuality.Value.ToString() + " vs " + qaplibInstances[instance.Name]); } Assert.IsTrue(failedInstances.Length == 0, "Following instances/solutions have suspicious best quality: " + Environment.NewLine + failedInstances.ToString()); }
public void TestQAPLIBInstances() { var provider = new QAPLIBInstanceProvider(); var qap = new QuadraticAssignmentProblem(); var failedInstances = new StringBuilder(); var instances = provider.GetDataDescriptors(); Assert.IsTrue(instances.Any(), "No instances could be found."); foreach (var instance in instances) { try { qap.Load(provider.LoadData(instance)); } catch (Exception ex) { failedInstances.AppendLine(instance + ": " + ex.Message); } } Assert.IsTrue(failedInstances.Length == 0, "Following instances failed: " + Environment.NewLine + failedInstances.ToString()); }
public override void Main() { DateTime start = DateTime.UtcNow; QuadraticAssignmentProblem qap; if (vars.Contains("qap")) { qap = vars.qap; } else { var provider = new DreznerQAPInstanceProvider(); var instance = provider.GetDataDescriptors().Single(x => x.Name == "dre56"); var data = provider.LoadData(instance); qap = new QuadraticAssignmentProblem(); qap.Load(data); vars.qap = qap; } const uint seed = 0; const int popSize = 100; const int generations = 1000; const double mutationRate = 0.05; var random = new MersenneTwister(seed); var population = new Permutation[popSize]; var qualities = new double[popSize]; var nextGen = new Permutation[popSize]; var nextQual = new double[popSize]; var qualityChart = new DataTable("Quality Chart"); var qualityRow = new DataRow("Best Quality"); qualityChart.Rows.Add(qualityRow); vars.qualityChart = qualityChart; for (int i = 0; i < popSize; i++) { population[i] = new Permutation(PermutationTypes.Absolute, qap.Weights.Rows, random); qualities[i] = QAPEvaluator.Apply(population[i], qap.Weights, qap.Distances); } var bestQuality = qualities.Min(); var bestQualityGeneration = 0; for (int g = 0; g < generations; g++) { var parents = population.SampleProportional(random, 2 * popSize, qualities, windowing: true, inverseProportional: true).ToArray(); for (int i = 0; i < popSize; i++) { nextGen[i] = PartiallyMatchedCrossover.Apply(random, parents[i * 2], parents[i * 2 + 1]); if (random.NextDouble() < mutationRate) { Swap2Manipulator.Apply(random, nextGen[i]); } nextQual[i] = QAPEvaluator.Apply(nextGen[i], qap.Weights, qap.Distances); if (nextQual[i] < bestQuality) { bestQuality = nextQual[i]; bestQualityGeneration = g; } } qualityRow.Values.Add(bestQuality); Array.Copy(nextGen, population, popSize); Array.Copy(nextQual, qualities, popSize); } vars.elapsed = new TimeSpanValue(DateTime.UtcNow - start); vars.bestQuality = bestQuality; vars.bestQualityFoundAt = bestQualityGeneration; }