Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13757 for branches


Ignore:
Timestamp:
04/13/16 16:25:12 (9 years ago)
Author:
abeham
Message:

#2547:

  • worked on problem instance mapping
  • started working on improved suggestion algorithm
Location:
branches/PerformanceComparison
Files:
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.Analysis/3.3/QualityAnalysis/ExpectedRuntimeHelper.cs

    r12956 r13757  
    1 using System.Collections.Generic;
     1using HeuristicLab.Optimization;
     2using System;
     3using System.Collections.Generic;
    24using System.Globalization;
    35using System.Linq;
    4 using HeuristicLab.Optimization;
    56
    67namespace HeuristicLab.Analysis {
    78  public static class ExpectedRuntimeHelper {
    8     public static ErtCalculationResult CalculateErt(List<IRun> runs, string indexedDataTableName, double target, bool maximization) {
     9    public static ErtCalculationResult CalculateErt(IEnumerable<IEnumerable<Tuple<double, double>>> convGraphs, double target, bool maximization) {
    910      var successful = new List<double>();
    1011      var unsuccessful = new List<double>();
    11       foreach (var r in runs) {
     12      foreach (var graph in convGraphs) {
    1213        var targetAchieved = false;
    13         var row = ((IndexedDataTable<double>)r.Results[indexedDataTableName]).Rows.First();
    14         foreach (var v in row.Values) {
     14        var lastEffort = double.MaxValue;
     15        foreach (var v in graph) {
    1516          if (maximization && v.Item2 >= target || !maximization && v.Item2 <= target) {
    1617            successful.Add(v.Item1);
     
    1819            break;
    1920          }
     21          lastEffort = v.Item1;
    2022        }
    21         if (!targetAchieved) unsuccessful.Add(row.Values.Last().Item1);
     23        if (!targetAchieved) unsuccessful.Add(lastEffort);
    2224      }
    2325
     
    3234      }
    3335      return new ErtCalculationResult(successful.Count, (successful.Count + unsuccessful.Count), ert);
     36    }
     37
     38    public static ErtCalculationResult CalculateErt(List<IRun> runs, string indexedDataTableName, double target, bool maximization) {
     39      return CalculateErt(runs.Select(r => ((IndexedDataTable<double>)r.Results[indexedDataTableName]).Rows.First().Values), target, maximization);
    3440    }
    3541  }
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/Enums.cs

    r13756 r13757  
    2626    SeedByCloning
    2727  }
     28
     29  public enum ProblemInstanceProximityType {
     30    FeatureSpace,
     31    PCA,
     32    MDS,
     33    SOM
     34  }
    2835}
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/HeuristicLab.OptimizationExpertSystem.Common-3.3.csproj

    r13750 r13757  
    150150    <Compile Include="KnowledgeCenter.cs" />
    151151    <Compile Include="Plugin.cs" />
    152     <Compile Include="SeedingStrategyTypes.cs" />
     152    <Compile Include="Enums.cs" />
    153153    <None Include="Properties\AssemblyInfo.cs.frame" />
    154154    <Compile Include="ProblemCharacteristicAnalysis\CharacteristicCalculator.cs" />
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs

    r13752 r13757  
    5959    }
    6060
     61    private readonly DoubleValue minimumTarget;
     62    public DoubleValue MinimumTarget {
     63      get { return minimumTarget; }
     64    }
     65   
    6166    private readonly RunCollection instanceRuns;
    6267    public RunCollection InstanceRuns {
     
    9499    public ReadOnlyCheckedItemList<StringValue> ProblemCharacteristics {
    95100      get { return readonlyProblemCharacteristics; }
    96     }
     101    }
     102
     103    private readonly EnumValue<ProblemInstanceProximityType> problemInstanceProximity;
     104    public EnumValue<ProblemInstanceProximityType> ProblemInstanceProximity {
     105      get { return problemInstanceProximity; }
     106    }
     107
     108    private readonly DoubleValue problemInstanceNeighborhoodFactor;
     109    public DoubleValue ProblemInstanceNeighborhoodFactor {
     110      get { return problemInstanceNeighborhoodFactor; }
     111    }
    97112
    98113    private readonly CheckedItemList<IScope> solutionSeedingPool;
     
    116131    public KnowledgeCenter() {
    117132      maximumEvaluations = new IntValue(10000);
     133      minimumTarget = new DoubleValue(1.05);
    118134      instanceRuns = new RunCollection();
    119135      seededRuns = new RunCollection();
     
    123139      problemInstances = new RunCollection();
    124140      problemCharacteristics = new CheckedItemList<StringValue>();
     141      problemInstanceProximity = new EnumValue<ProblemInstanceProximityType>(ProblemInstanceProximityType.FeatureSpace);
     142      problemInstanceNeighborhoodFactor = new DoubleValue(1);
    125143      readonlyProblemCharacteristics = problemCharacteristics.AsReadOnly();
    126144      problem = new SingleObjectiveOKBProblem();
     
    139157    private void RegisterEventHandlers() {
    140158      maximumEvaluations.ValueChanged += MaximumEvaluationsOnValueChanged;
     159      minimumTarget.ValueChanged += MinimumTargetOnValueChanged;
    141160      problem.ProblemChanged += ProblemOnProblemChanged;
    142161      problem.Solutions.ItemsAdded += ProblemSolutionsChanged;
     
    157176      problemCharacteristics.CollectionReset += CharacteristicChanged;
    158177      problemCharacteristics.CheckedItemsChanged += CharacteristicChanged;
     178      problemInstanceProximity.ValueChanged += ProblemInstanceProximityChanged;
     179      problemInstanceNeighborhoodFactor.ValueChanged += ProblemInstanceProximityChanged;
    159180    }
    160181
    161182    private void MaximumEvaluationsOnValueChanged(object sender, EventArgs eventArgs) {
     183      UpdateSuggestions();
     184    }
     185
     186    private void MinimumTargetOnValueChanged(object sender, EventArgs e) {
    162187      UpdateSuggestions();
    163188    }
     
    181206    }
    182207
     208    private void ProblemInstanceProximityChanged(object sender, EventArgs e) {
     209      UpdateSuggestions();
     210    }
     211
    183212    public bool IsCurrentInstance(IRun run) {
    184213      if (!problemId2ProblemInstanceMapping.ContainsSecond(run)) return false;
     
    189218      if (ProblemCharacteristics.Count == 0) return;
    190219
     220      var instances = GetProblemCharacteristics();
     221
     222      var key2Idx = new BidirectionalDictionary<IRun, int>();
     223      foreach (var kvp in instances.Select((k, i) => new { Index = i, Key = k.Key }))
     224        key2Idx.Add(kvp.Key, kvp.Index);
     225
     226      #region MDS
     227      Func<double[], double[], double> euclid = (a, b) => Math.Sqrt(a.Zip(b, (x, y) => (x - y)).Sum(x => x * x));
     228      var num = instances.Count;
     229      var matrix = new DoubleMatrix(num, num);
     230      for (var i = 0; i < num - 1; i++) {
     231        for (var j = i + 1; j < num; j++) {
     232          matrix[i, j] = matrix[j, i] = euclid(instances[key2Idx.GetBySecond(i)], instances[key2Idx.GetBySecond(j)]);
     233        }
     234      }
     235
     236      var coords = MultidimensionalScaling.KruskalShepard(matrix);
     237      #endregion
     238      #region PCA
     239      var ds = new double[instances.Count, ProblemCharacteristics.CheckedItems.Count()];
     240      foreach (var instance in instances) {
     241        var arr = instance.Value;
     242        for (var feature = 0; feature < arr.Length; feature++)
     243          ds[key2Idx.GetByFirst(instance.Key), feature] = arr[feature];
     244      }
     245
     246      int info;
     247      double[] s2;
     248      double[,] v;
     249      alglib.pcabuildbasis(ds, ds.GetLength(0), ds.GetLength(1), out info, out s2, out v);
     250      #endregion
     251      #region SOM
     252      var features = new DoubleMatrix(ProblemCharacteristics.CheckedItems.Count(), instances.Count);
     253      foreach (var instance in instances) {
     254        var arr = instance.Value;
     255        for (var feature = 0; feature < arr.Length; feature++)
     256          features[feature, key2Idx.GetByFirst(instance.Key)] = arr[feature];
     257      }
     258      var somCoords = SOM.Map(features, new MersenneTwister(42), somSize: 20, learningRadius: 20, iterations: 200, jittering: true);
     259      #endregion
     260
     261      ProblemInstances.UpdateOfRunsInProgress = true;
     262      try {
     263        foreach (var instance in ProblemInstances) {
     264          double x = 0, y = 0;
     265          for (var feature = 0; feature < ds.GetLength(1); feature++) {
     266            x += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 0];
     267            y += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 1];
     268          }
     269
     270          IItem item;
     271          if (instance.Results.TryGetValue("Projection.PCA.X", out item)) {
     272            ((DoubleValue)item).Value = x;
     273          } else instance.Results.Add("Projection.PCA.X", new DoubleValue(x));
     274          if (instance.Results.TryGetValue("Projection.PCA.Y", out item)) {
     275            ((DoubleValue)item).Value = y;
     276          } else instance.Results.Add("Projection.PCA.Y", new DoubleValue(y));
     277
     278          if (instance.Results.TryGetValue("Projection.MDS.X", out item)) {
     279            ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 0];
     280          } else instance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(instance), 0]));
     281          if (instance.Results.TryGetValue("Projection.MDS.Y", out item)) {
     282            ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 1];
     283          } else instance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(instance), 1]));
     284
     285          if (instance.Results.TryGetValue("Projection.SOM.X", out item)) {
     286            ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 0];
     287          } else instance.Results.Add("Projection.SOM.X", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 0]));
     288          if (instance.Results.TryGetValue("Projection.SOM.Y", out item)) {
     289            ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 1];
     290          } else instance.Results.Add("Projection.SOM.Y", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 1]));
     291        }
     292      } finally { ProblemInstances.UpdateOfRunsInProgress = false; }
     293    }
     294
     295    private Dictionary<IRun, double[]> GetProblemCharacteristics() {
    191296      var instances = new Dictionary<IRun, double[]>();
    192297      var values = new List<double>[ProblemCharacteristics.CheckedItems.Count()];
     
    207312      var median = values.Select(x => x.Count == 0 ? 0.0 : x.Median()).ToList();
    208313
    209       var allValues = instances.Values.Select(x => x.Select((f, i) => new { Idx = i, Val = double.IsNaN(f) ? median[i] : f }).ToList())
    210                                       .SelectMany(x => x)
    211                                       .GroupBy(x => x.Idx, x => x.Val)
    212                                       .OrderBy(x => x.Key).ToList();
     314      var allValues = instances.Values.Select(x => x.Select((f, i) => new {Idx = i, Val = double.IsNaN(f) ? median[i] : f}).ToList())
     315        .SelectMany(x => x)
     316        .GroupBy(x => x.Idx, x => x.Val)
     317        .OrderBy(x => x.Key).ToList();
    213318      var avg = allValues.Select(x => x.Average()).ToList();
    214319      var stdev = allValues.Select(x => x.StandardDeviation()).ToList();
     
    222327        }
    223328      }
    224 
    225       var key2Idx = new BidirectionalDictionary<IRun, int>();
    226       foreach (var kvp in instances.Select((k, i) => new { Index = i, Key = k.Key }))
    227         key2Idx.Add(kvp.Key, kvp.Index);
    228 
    229       #region MDS
    230       Func<double[], double[], double> euclid = (a, b) => Math.Sqrt(a.Zip(b, (x, y) => (x - y)).Sum(x => x * x));
    231       var num = instances.Count;
    232       var matrix = new DoubleMatrix(num, num);
    233       for (var i = 0; i < num - 1; i++) {
    234         for (var j = i + 1; j < num; j++) {
    235           matrix[i, j] = matrix[j, i] = euclid(instances[key2Idx.GetBySecond(i)], instances[key2Idx.GetBySecond(j)]);
    236         }
    237       }
    238 
    239       var coords = MultidimensionalScaling.KruskalShepard(matrix);
    240       #endregion
    241       #region PCA
    242       var ds = new double[instances.Count, ProblemCharacteristics.CheckedItems.Count()];
    243       foreach (var instance in instances) {
    244         var arr = instance.Value;
    245         for (var feature = 0; feature < arr.Length; feature++)
    246           ds[key2Idx.GetByFirst(instance.Key), feature] = arr[feature];
    247       }
    248 
    249       int info;
    250       double[] s2;
    251       double[,] v;
    252       alglib.pcabuildbasis(ds, ds.GetLength(0), ds.GetLength(1), out info, out s2, out v);
    253       #endregion
    254       #region SOM
    255       var features = new DoubleMatrix(ProblemCharacteristics.CheckedItems.Count(), instances.Count);
    256       foreach (var instance in instances) {
    257         var arr = instance.Value;
    258         for (var feature = 0; feature < arr.Length; feature++)
    259           features[feature, key2Idx.GetByFirst(instance.Key)] = arr[feature];
    260       }
    261       var somCoords = SOM.Map(features, new MersenneTwister(42), somSize: 20, learningRadius: 20, iterations: 200, jittering: true);
    262       #endregion
    263 
    264       ProblemInstances.UpdateOfRunsInProgress = true;
    265       try {
    266         foreach (var instance in ProblemInstances) {
    267           double x = 0, y = 0;
    268           for (var feature = 0; feature < ds.GetLength(1); feature++) {
    269             x += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 0];
    270             y += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 1];
    271           }
    272 
    273           IItem item;
    274           if (instance.Results.TryGetValue("Projection.PCA.X", out item)) {
    275             ((DoubleValue)item).Value = x;
    276           } else instance.Results.Add("Projection.PCA.X", new DoubleValue(x));
    277           if (instance.Results.TryGetValue("Projection.PCA.Y", out item)) {
    278             ((DoubleValue)item).Value = y;
    279           } else instance.Results.Add("Projection.PCA.Y", new DoubleValue(y));
    280 
    281           if (instance.Results.TryGetValue("Projection.MDS.X", out item)) {
    282             ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 0];
    283           } else instance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(instance), 0]));
    284           if (instance.Results.TryGetValue("Projection.MDS.Y", out item)) {
    285             ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 1];
    286           } else instance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(instance), 1]));
    287 
    288           if (instance.Results.TryGetValue("Projection.SOM.X", out item)) {
    289             ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 0];
    290           } else instance.Results.Add("Projection.SOM.X", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 0]));
    291           if (instance.Results.TryGetValue("Projection.SOM.Y", out item)) {
    292             ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 1];
    293           } else instance.Results.Add("Projection.SOM.Y", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 1]));
    294         }
    295       } finally { ProblemInstances.UpdateOfRunsInProgress = false; }
     329      return instances;
    296330    }
    297331
     
    434468        problemClassFilter.Value = adminClient.ProblemClasses.Single(x => x.Id == probClassId).Name;
    435469
     470        problemId2ProblemInstanceMapping.Clear();
    436471        progress.Status = "Downloading problem instances...";
    437472        progress.ProgressValue = 0;
     
    550585
    551586          if (!algInstRunDict.ContainsKey(probInstanceName)) continue;
    552           foreach (var kvp in algInstRunDict[probInstanceName]) {
    553             // TODO: Things needs to be configurable here (table name, targets)
    554             foreach (var target in new[] { 1, 1.01, 1.05, 1.1, 1.2, 1.5 }) {
     587          // TODO: Things needs to be configurable here (table name, targets)
     588          foreach (var target in new[] { 1, 1.01, 1.05, 1.1, 1.2, 1.5 }) {
     589            var indexMap = new BidirectionalDictionary<string, int>();
     590            var dict = new Dictionary<string, double>();
     591            var idx = 0;
     592            foreach (var kvp in algInstRunDict[probInstanceName]) {
    555593              var result = ExpectedRuntimeHelper.CalculateErt(kvp.Value, "QualityPerEvaluations", bkQuality * target, maximization);
    556               var resultName = kvp.Key + "@" + ((target - 1) * 100) + "%";
    557               characteristics.Add(resultName);
    558               IItem item;
    559               if (instance.Results.TryGetValue(resultName, out item)) {
    560                 ((DoubleValue)item).Value = Math.Log10(result.ExpectedRuntime);
    561               } else instance.Results.Add(resultName, new DoubleValue(Math.Log10(result.ExpectedRuntime)));
     594              indexMap.Add(kvp.Key, idx);
     595              dict[kvp.Key] = !double.IsNaN(result.ExpectedRuntime) ? result.ExpectedRuntime : int.MaxValue;
     596              idx++;
     597            }
     598            var points = dict.OrderBy(x => indexMap.GetByFirst(x.Key)).Select(x => x.Value > 0 ? Math.Log10(x.Value) : 0).ToArray();
     599            int[] clusters;
     600            Ckmeans1dClusters(points, 5, out clusters);
     601            var ranks = clusters.Select((c, i) => new { Cluster = c, Perf = dict[indexMap.GetBySecond(i)] })
     602                                .GroupBy(x => x.Cluster, x => x.Perf)
     603                                .Select(x => new { Cluster = x.Key, AvgPerf = x.Average() })
     604                                .OrderBy(x => x.AvgPerf)
     605                                .Select((c, i) => new { Cluster = c.Cluster, Rank = i })
     606                                .ToDictionary(x => x.Cluster, x => x.Rank);
     607            for (var i = 0; i < clusters.Length; i++) {
     608              var resultName = "Rank." + indexMap.GetBySecond(i) + "@" + ((target - 1) * 100) + "%";
     609              instance.Results[resultName] = new IntValue(dict[indexMap.GetBySecond(i)] < int.MaxValue ? ranks[clusters[i]] : 6);
    562610            }
    563611          }
     
    576624      } finally { progress.Finish(); ProblemInstances.UpdateOfRunsInProgress = false; }
    577625      UpdateInstanceProjection();
     626      UpdateSuggestions();
    578627    }
    579628
    580629    private void UpdateSuggestions() {
    581630      if (Problem == null) return;
     631      var piDistances = GetProblemDistances();
     632      var maxDistance = piDistances.Max();
     633      // Weighted combination of algorithm performances using distance-based exponentially falling weights
     634      // Scale distances by maxDistance
     635      // Parameter to influence dampening factor
     636      // Algorithm performances are given in expected run time
     637      // Using convergence graphs up to maximum evaluations effort
     638      // Care has to be taken if not every algorithm has been executed on every problem instance
    582639      var instances = new SortedList<double, IAlgorithm>();
    583640      foreach (var relevantRuns in knowledgeBase.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single())) {
     
    608665    }
    609666
     667    private DoubleMatrix GetProblemDistances() {
     668      var matrix = new DoubleMatrix(ProblemInstances.Count, ProblemInstances.Count);
     669      switch (ProblemInstanceProximity.Value) {
     670        case ProblemInstanceProximityType.MDS:
     671        case ProblemInstanceProximityType.PCA:
     672        case ProblemInstanceProximityType.SOM:
     673          int i = 0, j = 0;
     674          foreach (var a in ProblemInstances) {
     675            double xa, ya;
     676            GetProjectionCoordinates(a, out xa, out ya);
     677            j = 0;
     678            foreach (var b in ProblemInstances) {
     679              double xb, yb;
     680              GetProjectionCoordinates(b, out xb, out yb);
     681              matrix[i, j] = Math.Sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb));
     682              j++;
     683            }
     684            i++;
     685          }
     686          break;
     687        case ProblemInstanceProximityType.FeatureSpace:
     688          int k = 0, l = 0;
     689          var features = GetProblemCharacteristics();
     690          foreach (var a in ProblemInstances) {
     691            l = 0;
     692            var fa = features[a];
     693            foreach (var b in ProblemInstances) {
     694              var sum = features[b].Select((t, f) => (fa[f] - t) * (fa[f] - t)).Sum();
     695              matrix[k, l] = Math.Sqrt(sum);
     696              l++;
     697            }
     698            k++;
     699          }
     700          break;
     701        default: throw new InvalidOperationException(string.Format("Unkonwn proximity type {0}", ProblemInstanceProximity.Value));
     702      }
     703      return matrix;
     704    }
     705
     706    private void GetProjectionCoordinates(IRun problemInstance, out double x, out double y) {
     707      x = ((DoubleValue)problemInstance.Results["Projection." + ProblemInstanceProximity.Value + ".X"]).Value;
     708      y = ((DoubleValue)problemInstance.Results["Projection." + ProblemInstanceProximity.Value + ".Y"]).Value;
     709    }
     710
    610711    public event EventHandler<EventArgs<IProgress>> DownloadStarted;
    611712    private void OnDownloadStarted(IProgress progress) {
     
    619720      if (handler != null) handler(this, new EventArgs<IAlgorithm>(instance));
    620721    }
     722
     723    // implement further classes and methods
     724    private static SortedList<double, int> Ckmeans1dClusters(double[] estimations, int k, out int[] clusterValues) {
     725      int nPoints = estimations.Length;
     726      var distinct = estimations.Distinct().OrderBy(x => x).ToArray();
     727      var max = distinct.Max();
     728      if (distinct.Length <= k) {
     729        var dict = distinct.Select((v, i) => new { Index = i, Value = v }).ToDictionary(x => x.Value, y => y.Index);
     730        for (int i = distinct.Length; i < k; i++)
     731          dict.Add(max + i - distinct.Length + 1, i);
     732
     733        clusterValues = new int[nPoints];
     734        for (int i = 0; i < nPoints; i++)
     735          if (!dict.ContainsKey(estimations[i])) clusterValues[i] = 0;
     736          else clusterValues[i] = dict[estimations[i]];
     737
     738        return new SortedList<double, int>(dict);
     739      }
     740
     741      var n = distinct.Length;
     742      var D = new double[n, k];
     743      var B = new int[n, k];
     744
     745      for (int m = 0; m < k; m++) {
     746        for (int j = m; j <= n - k + m; j++) {
     747          if (m == 0)
     748            D[j, m] = SumOfSquaredDistances(distinct, 0, j + 1);
     749          else {
     750            var minD = double.MaxValue;
     751            var minI = 0;
     752            for (int i = 1; i <= j; i++) {
     753              var d = D[i - 1, m - 1] + SumOfSquaredDistances(distinct, i, j + 1);
     754              if (d < minD) {
     755                minD = d;
     756                minI = i;
     757              }
     758            }
     759            D[j, m] = minD;
     760            B[j, m] = minI;
     761          }
     762        }
     763      }
     764
     765      var centers = new SortedList<double, int>();
     766      var upper = B[n - 1, k - 1];
     767      var c = Mean(distinct, upper, n);
     768      centers.Add(c, k - 1);
     769      for (int i = k - 2; i >= 0; i--) {
     770        var lower = B[upper - 1, i];
     771        var c2 = Mean(distinct, lower, upper);
     772        centers.Add(c2, i);
     773        upper = lower;
     774      }
     775
     776      clusterValues = new int[nPoints];
     777      for (int i = 0; i < estimations.Length; i++) {
     778        clusterValues[i] = centers.MinItems(x => Math.Abs(estimations[i] - x.Key)).First().Value;
     779      }
     780
     781      return centers;
     782    }
     783
     784    private static double SumOfSquaredDistances(double[] x, int start, int end) {
     785      if (start == end) throw new InvalidOperationException();
     786      if (start + 1 == end) return 0.0;
     787      double mean = 0.0;
     788      for (int i = start; i < end; i++) {
     789        mean += x[i];
     790      }
     791      mean /= (end - start);
     792      var sum = 0.0;
     793      for (int i = start; i < end; i++) {
     794        sum += (x[i] - mean) * (x[i] - mean);
     795      }
     796      return sum;
     797    }
     798
     799    private static double Mean(double[] x, int start, int end) {
     800      if (start == end) throw new InvalidOperationException();
     801      double mean = 0.0;
     802      for (int i = start; i < end; i++) {
     803        mean += x[i];
     804      }
     805      mean /= (end - start);
     806      return mean;
     807    }
    621808  }
    622809}
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/Views/UnderstandingProblemInstanceView.Designer.cs

    r13752 r13757  
    5454      this.mapSplitContainer = new System.Windows.Forms.SplitContainer();
    5555      this.showCharacteristicsCheckBox = new System.Windows.Forms.CheckBox();
     56      this.fromZeroCheckBox = new System.Windows.Forms.CheckBox();
    5657      this.invPropCheckBox = new System.Windows.Forms.CheckBox();
    5758      this.sizeLabel = new System.Windows.Forms.Label();
     
    6263      this.instancesTabPage = new System.Windows.Forms.TabPage();
    6364      this.problemInstancesView = new HeuristicLab.MainForm.WindowsForms.ViewHost();
     65      this.colorComboBox = new System.Windows.Forms.ComboBox();
     66      this.colorLabel = new System.Windows.Forms.Label();
    6467      this.problemInstancesTabControl.SuspendLayout();
    6568      this.mapTabPage.SuspendLayout();
     
    8285      this.problemInstancesTabControl.Name = "problemInstancesTabControl";
    8386      this.problemInstancesTabControl.SelectedIndex = 0;
    84       this.problemInstancesTabControl.Size = new System.Drawing.Size(1000, 760);
     87      this.problemInstancesTabControl.Size = new System.Drawing.Size(1108, 760);
    8588      this.problemInstancesTabControl.TabIndex = 8;
    8689      //
     
    9194      this.mapTabPage.Name = "mapTabPage";
    9295      this.mapTabPage.Padding = new System.Windows.Forms.Padding(3);
    93       this.mapTabPage.Size = new System.Drawing.Size(992, 734);
     96      this.mapTabPage.Size = new System.Drawing.Size(1100, 734);
    9497      this.mapTabPage.TabIndex = 1;
    9598      this.mapTabPage.Text = "Map";
     
    106109      //
    107110      this.mapSplitContainer.Panel1.Controls.Add(this.showCharacteristicsCheckBox);
     111      this.mapSplitContainer.Panel1.Controls.Add(this.fromZeroCheckBox);
    108112      this.mapSplitContainer.Panel1.Controls.Add(this.invPropCheckBox);
     113      this.mapSplitContainer.Panel1.Controls.Add(this.colorLabel);
    109114      this.mapSplitContainer.Panel1.Controls.Add(this.sizeLabel);
    110115      this.mapSplitContainer.Panel1.Controls.Add(this.projectionLabel);
    111116      this.mapSplitContainer.Panel1.Controls.Add(this.instanceMapChart);
     117      this.mapSplitContainer.Panel1.Controls.Add(this.colorComboBox);
    112118      this.mapSplitContainer.Panel1.Controls.Add(this.sizeComboBox);
    113119      this.mapSplitContainer.Panel1.Controls.Add(this.projectionComboBox);
    114120      this.mapSplitContainer.Panel1.Padding = new System.Windows.Forms.Padding(0, 3, 0, 0);
    115121      this.mapSplitContainer.Panel2Collapsed = true;
    116       this.mapSplitContainer.Size = new System.Drawing.Size(986, 728);
     122      this.mapSplitContainer.Size = new System.Drawing.Size(1094, 728);
    117123      this.mapSplitContainer.SplitterDistance = 847;
    118124      this.mapSplitContainer.TabIndex = 12;
     
    122128      this.showCharacteristicsCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
    123129      this.showCharacteristicsCheckBox.Appearance = System.Windows.Forms.Appearance.Button;
    124       this.showCharacteristicsCheckBox.Location = new System.Drawing.Point(957, 4);
     130      this.showCharacteristicsCheckBox.Location = new System.Drawing.Point(1065, 5);
    125131      this.showCharacteristicsCheckBox.Name = "showCharacteristicsCheckBox";
    126132      this.showCharacteristicsCheckBox.Size = new System.Drawing.Size(26, 23);
     
    130136      this.showCharacteristicsCheckBox.CheckedChanged += new System.EventHandler(this.showCharacteristicsCheckBox_CheckedChanged);
    131137      //
     138      // fromZeroCheckBox
     139      //
     140      this.fromZeroCheckBox.AutoSize = true;
     141      this.fromZeroCheckBox.Checked = true;
     142      this.fromZeroCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
     143      this.fromZeroCheckBox.Location = new System.Drawing.Point(639, 8);
     144      this.fromZeroCheckBox.Name = "fromZeroCheckBox";
     145      this.fromZeroCheckBox.Size = new System.Drawing.Size(69, 17);
     146      this.fromZeroCheckBox.TabIndex = 17;
     147      this.fromZeroCheckBox.Text = "from zero";
     148      this.fromZeroCheckBox.UseVisualStyleBackColor = true;
     149      this.fromZeroCheckBox.CheckedChanged += new System.EventHandler(this.fromZeroCheckBox_CheckedChanged);
     150      //
    132151      // invPropCheckBox
    133152      //
     
    135154      this.invPropCheckBox.Checked = true;
    136155      this.invPropCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
    137       this.invPropCheckBox.Location = new System.Drawing.Point(579, 12);
     156      this.invPropCheckBox.Location = new System.Drawing.Point(515, 8);
    138157      this.invPropCheckBox.Name = "invPropCheckBox";
    139158      this.invPropCheckBox.Size = new System.Drawing.Size(118, 17);
     
    146165      //
    147166      this.sizeLabel.AutoSize = true;
    148       this.sizeLabel.Location = new System.Drawing.Point(264, 12);
     167      this.sizeLabel.Location = new System.Drawing.Point(251, 12);
    149168      this.sizeLabel.Name = "sizeLabel";
    150169      this.sizeLabel.Size = new System.Drawing.Size(30, 13);
     
    192211      this.instanceMapChart.Series.Add(series1);
    193212      this.instanceMapChart.Series.Add(series2);
    194       this.instanceMapChart.Size = new System.Drawing.Size(980, 692);
     213      this.instanceMapChart.Size = new System.Drawing.Size(974, 692);
    195214      this.instanceMapChart.TabIndex = 12;
    196215      //
     
    199218      this.sizeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    200219      this.sizeComboBox.FormattingEnabled = true;
    201       this.sizeComboBox.Location = new System.Drawing.Point(300, 6);
     220      this.sizeComboBox.Location = new System.Drawing.Point(287, 6);
    202221      this.sizeComboBox.Name = "sizeComboBox";
    203       this.sizeComboBox.Size = new System.Drawing.Size(273, 21);
     222      this.sizeComboBox.Size = new System.Drawing.Size(222, 21);
    204223      this.sizeComboBox.TabIndex = 13;
    205224      this.sizeComboBox.SelectedIndexChanged += new System.EventHandler(this.SizeComboBoxOnSelectedIndexChanged);
     
    221240      this.instancesTabPage.Name = "instancesTabPage";
    222241      this.instancesTabPage.Padding = new System.Windows.Forms.Padding(3);
    223       this.instancesTabPage.Size = new System.Drawing.Size(992, 734);
     242      this.instancesTabPage.Size = new System.Drawing.Size(1106, 734);
    224243      this.instancesTabPage.TabIndex = 0;
    225244      this.instancesTabPage.Text = "Instances";
     
    235254      this.problemInstancesView.Name = "problemInstancesView";
    236255      this.problemInstancesView.ReadOnly = false;
    237       this.problemInstancesView.Size = new System.Drawing.Size(986, 728);
     256      this.problemInstancesView.Size = new System.Drawing.Size(1100, 728);
    238257      this.problemInstancesView.TabIndex = 0;
    239258      this.problemInstancesView.ViewsLabelVisible = true;
    240259      this.problemInstancesView.ViewType = null;
     260      //
     261      // colorComboBox
     262      //
     263      this.colorComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
     264      this.colorComboBox.FormattingEnabled = true;
     265      this.colorComboBox.Location = new System.Drawing.Point(768, 6);
     266      this.colorComboBox.Name = "colorComboBox";
     267      this.colorComboBox.Size = new System.Drawing.Size(222, 21);
     268      this.colorComboBox.TabIndex = 13;
     269      this.colorComboBox.SelectedIndexChanged += new System.EventHandler(this.colorComboBox_SelectedIndexChanged);
     270      //
     271      // colorLabel
     272      //
     273      this.colorLabel.AutoSize = true;
     274      this.colorLabel.Location = new System.Drawing.Point(728, 9);
     275      this.colorLabel.Name = "colorLabel";
     276      this.colorLabel.Size = new System.Drawing.Size(34, 13);
     277      this.colorLabel.TabIndex = 15;
     278      this.colorLabel.Text = "Color:";
    241279      //
    242280      // UnderstandingProblemInstanceView
     
    246284      this.Controls.Add(this.problemInstancesTabControl);
    247285      this.Name = "UnderstandingProblemInstanceView";
    248       this.Size = new System.Drawing.Size(1000, 760);
     286      this.Size = new System.Drawing.Size(1108, 760);
    249287      this.problemInstancesTabControl.ResumeLayout(false);
    250288      this.mapTabPage.ResumeLayout(false);
     
    273311    private System.Windows.Forms.ComboBox sizeComboBox;
    274312    private System.Windows.Forms.ComboBox projectionComboBox;
     313    private System.Windows.Forms.CheckBox fromZeroCheckBox;
     314    private System.Windows.Forms.Label colorLabel;
     315    private System.Windows.Forms.ComboBox colorComboBox;
    275316  }
    276317}
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/Views/UnderstandingProblemInstanceView.cs

    r13752 r13757  
    2828using System;
    2929using System.Collections.Generic;
     30using System.Drawing;
    3031using System.Linq;
    3132using System.Text.RegularExpressions;
     
    6263        UpdateProjectionComboBox();
    6364        UpdateSizeComboBox();
     65        UpdateColorComboBox();
    6466      }
    6567    }
     
    7678      UpdateProjectionComboBox();
    7779      UpdateSizeComboBox();
     80      UpdateColorComboBox();
    7881      UpdateProjection();
    7982    }
     
    108111      } finally { SuppressEvents = false; }
    109112    }
     113
     114    private void UpdateColorComboBox() {
     115      try {
     116        SuppressEvents = true;
     117        var selected = colorComboBox.SelectedIndex >= 0 ? (string)colorComboBox.SelectedItem : null;
     118        colorComboBox.Items.Clear();
     119        colorComboBox.Items.Add(string.Empty);
     120        foreach (var str in Content.ProblemInstances.ResultNames.Where(x => x.StartsWith("Rank."))) {
     121          colorComboBox.Items.Add(str);
     122          if (selected == str) colorComboBox.SelectedItem = str;
     123        }
     124        if (selected == null && colorComboBox.Items.Count > 0)
     125          colorComboBox.SelectedIndex = 0;
     126      } finally { SuppressEvents = false; }
     127    }
    110128   
    111129    private IEnumerable<string> GetProjections() {
     
    130148      var projection = (string)projectionComboBox.SelectedItem;
    131149      var size = sizeComboBox.SelectedIndex >= 0 ? (string)sizeComboBox.SelectedItem : string.Empty;
    132 
    133       DoProjectProblemInstances(instancesSeries, currentInstanceSeries, projection, size, invPropCheckBox.Checked);
    134     }
    135 
    136     private void DoProjectProblemInstances(Series instancesSeries, Series currentInstanceSeries, string projection, string size, bool invProp) {
     150      var color = colorComboBox.SelectedIndex >= 0 ? (string)colorComboBox.SelectedItem : string.Empty;
     151
     152      DoProjectProblemInstances(instancesSeries, currentInstanceSeries, projection,
     153        size, color, invPropCheckBox.Checked, fromZeroCheckBox.Checked);
     154    }
     155
     156    private void DoProjectProblemInstances(Series instancesSeries, Series currentInstanceSeries, string projection, string size, string color, bool invProp, bool fromZero) {
    137157      instancesSeries.Points.Clear();
    138158      currentInstanceSeries.Points.Clear();
     
    143163        if (sizes.Count > 0) {
    144164          maxSize = sizes.Max(x => x.Value);
    145           minSize = sizes.Min(x => x.Value);
     165          if (maxSize < 0 && fromZero) {
     166            maxSize = 0;
     167            minSize = sizes.Min(x => x.Value);
     168          } else if (fromZero) {
     169            minSize = 0;
     170          } else {
     171            minSize = sizes.Min(x => x.Value);
     172          }
    146173        }
    147174      }
     
    159186        if (maxSize > minSize && run.Results.TryGetValue(size, out item)) {
    160187          var dItem = item as Data.DoubleValue;
     188          if (dItem == null && item is IntValue) dItem = new DoubleValue(((IntValue)item).Value);
    161189          if (dItem != null) {
    162190            if (double.IsNaN(dItem.Value))
     
    171199        } else dataPoint.MarkerSize = 1;
    172200
     201        if (!string.IsNullOrEmpty(color)) {
     202          if (run.Results.TryGetValue(color, out item)) {
     203            var v = (int)(item is DoubleValue ? ((DoubleValue)item).Value : (item is IntValue ? ((IntValue)item).Value : int.MaxValue));
     204            switch (v) {
     205              case 0: dataPoint.MarkerColor = Color.Green; break;
     206              case 1: dataPoint.MarkerColor = Color.LightSeaGreen; break;
     207              case 2: dataPoint.MarkerColor = Color.CornflowerBlue; break;
     208              case 3: dataPoint.MarkerColor = Color.PaleVioletRed; break;
     209              case 4: dataPoint.MarkerColor = Color.IndianRed; break;
     210              default: dataPoint.MarkerColor = Color.LightGray; break;
     211            }
     212          }
     213        }
    173214        if (Content.IsCurrentInstance(run)) currentInstanceSeries.Points.Add(dataPoint);
    174215        else instancesSeries.Points.Add(dataPoint);
     
    185226    }
    186227
     228    private void colorComboBox_SelectedIndexChanged(object sender, EventArgs e) {
     229      UpdateProjection();
     230    }
     231
    187232    private void InvPropCheckBoxOnCheckedChanged(object sender, EventArgs e) {
     233      UpdateProjection();
     234    }
     235
     236    private void fromZeroCheckBox_CheckedChanged(object sender, EventArgs e) {
    188237      UpdateProjection();
    189238    }
Note: See TracChangeset for help on using the changeset viewer.