using System; using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("BoundingBox Aggregator", "Aggregates bounding boxes")] [StorableClass] public class BoundingBoxAggregator : Aggregator { [StorableConstructor] protected BoundingBoxAggregator(bool deserializing) : base(deserializing) { } protected BoundingBoxAggregator(BoundingBoxAggregator original, Cloner cloner) : base(original, cloner) { } public BoundingBoxAggregator() { } public override IDeepCloneable Clone(Cloner cloner) { return new BoundingBoxAggregator(this, cloner); } public override IResult CreateResult() { return new Result("BoundingBox Analysis", Aggregate(items)); } public static DataTable Aggregate(List boundingBoxes) { DataTable table = new DataTable("Bounding Box Analysis"); DataRow volumes = new DataRow("Volumes"); table.Rows.Add(volumes); foreach (DoubleMatrix matrix in boundingBoxes) { double volume = 1; for (int i = 0; i < matrix.Rows; i++) { volume *= matrix[i, 1] - matrix[i, 0]; } volumes.Values.Add(Math.Pow(volume, 1.0 / matrix.Rows)); } DataRow nrOfOverlapsRow = new DataRow("Nr of Overlaps"); nrOfOverlapsRow.VisualProperties.SecondYAxis = true; table.Rows.Add(nrOfOverlapsRow); DataRow minDistanceRow = new DataRow("MinDistance"); DataRow maxDistanceRow = new DataRow("MaxDistance"); table.Rows.Add(minDistanceRow); table.Rows.Add(maxDistanceRow); foreach (DoubleMatrix a in boundingBoxes) { double minDistance = double.MaxValue; double maxDistance = double.MinValue; int nrOfOverlaps = 0; foreach (DoubleMatrix b in boundingBoxes) { if (a == b) continue; double distance = 0; for (int i = 0; i < a.Rows; i++) { if (a[i, 1] < b[i, 0]) distance += square(b[i, 0] - a[i, 1]); else if (b[i, 1] < a[i, 0]) distance += square(a[i, 0] - b[i, 1]); } distance = Math.Sqrt(distance); if (distance == 0) nrOfOverlaps++; minDistance = Math.Min(minDistance, distance); maxDistance = Math.Max(maxDistance, distance); } minDistanceRow.Values.Add(minDistance); maxDistanceRow.Values.Add(maxDistance); nrOfOverlapsRow.Values.Add(nrOfOverlaps); } return table; } private static double square(double x) { return x * x; } } }