Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/11/16 17:34:50 (8 years ago)
Author:
abeham
Message:

#2457: worked on problem instance mapping

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs

    r13750 r13751  
    5353    }
    5454
    55     private IntValue maximumEvaluations;
     55    private readonly IntValue maximumEvaluations;
    5656    public IntValue MaximumEvaluations {
    5757      get { return maximumEvaluations; }
    5858    }
    5959
    60     private RunCollection instanceRuns;
     60    private readonly RunCollection instanceRuns;
    6161    public RunCollection InstanceRuns {
    6262      get { return instanceRuns; }
    6363    }
    6464
    65     private RunCollection seededRuns;
     65    private readonly RunCollection seededRuns;
    6666    public RunCollection SeededRuns {
    6767      get { return seededRuns; }
    6868    }
    6969
    70     private RunCollection knowledgeBase;
     70    private readonly RunCollection knowledgeBase;
    7171    public RunCollection KnowledgeBase {
    7272      get { return knowledgeBase; }
    7373    }
    7474
    75     private SingleObjectiveOKBProblem problem;
     75    private readonly SingleObjectiveOKBProblem problem;
    7676    public SingleObjectiveOKBProblem Problem {
    7777      get { return problem; }
    7878    }
    7979
    80     private ItemList<IAlgorithm> suggestedInstances;
    81     private ReadOnlyItemList<IAlgorithm> readOnlySuggestedInstances;
     80    private readonly ItemList<IAlgorithm> suggestedInstances;
     81    private readonly ReadOnlyItemList<IAlgorithm> readOnlySuggestedInstances;
    8282    public ReadOnlyItemList<IAlgorithm> SuggestedInstances {
    8383      get { return readOnlySuggestedInstances; }
    8484    }
    8585
    86     private RunCollection problemInstances;
     86    private readonly RunCollection problemInstances;
    8787    public RunCollection ProblemInstances {
    8888      get { return problemInstances; }
    8989    }
    9090
    91     private CheckedItemList<IScope> solutionSeedingPool;
     91    private readonly CheckedItemList<StringValue> problemCharacteristics;
     92    public CheckedItemList<StringValue> ProblemCharacteristics {
     93      get { return problemCharacteristics; }
     94    }
     95
     96    private readonly CheckedItemList<IScope> solutionSeedingPool;
    9297    public CheckedItemList<IScope> SolutionSeedingPool {
    9398      get { return solutionSeedingPool; }
    9499    }
    95100
    96     private EnumValue<SeedingStrategyTypes> seedingStrategy;
     101    private readonly EnumValue<SeedingStrategyTypes> seedingStrategy;
    97102    public EnumValue<SeedingStrategyTypes> SeedingStrategy {
    98103      get { return seedingStrategy; }
     
    101106    private BidirectionalLookup<long, IRun> algorithmId2RunMapping;
    102107    private BidirectionalDictionary<long, IAlgorithm> algorithmId2AlgorithmInstanceMapping;
    103 
    104     private Run currentInstance;
    105 
     108    private BidirectionalDictionary<long, IRun> problemId2ProblemInstanceMapping;
     109   
    106110    private bool Maximization {
    107111      get { return Problem != null && Problem.ProblemId >= 0 && ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
     
    116120      readOnlySuggestedInstances = suggestedInstances.AsReadOnly();
    117121      problemInstances = new RunCollection();
     122      problemCharacteristics = new CheckedItemList<StringValue>();
    118123      problem = new SingleObjectiveOKBProblem();
    119124      algorithmId2RunMapping = new BidirectionalLookup<long, IRun>();
    120125      algorithmId2AlgorithmInstanceMapping = new BidirectionalDictionary<long, IAlgorithm>();
     126      problemId2ProblemInstanceMapping = new BidirectionalDictionary<long, IRun>();
    121127      solutionSeedingPool = new CheckedItemList<IScope>();
    122128      seedingStrategy = new EnumValue<SeedingStrategyTypes>(SeedingStrategyTypes.NoSeeding);
     
    162168    }
    163169
    164     private void UpdateInstanceProjection() {
    165       currentInstance = null;
    166       var filter = new HashSet<string>();
    167 
    168       if (Problem.ProblemId != -1) {
    169         currentInstance = new Run { Name = Problem.Name };
    170         Problem.Problem.CollectParameterValues(currentInstance.Parameters);
    171         foreach (var c in RunCreationClient.Instance.GetCharacteristicValues(Problem.ProblemId)) {
    172           var key = "Characteristic." + c.Name;
    173           currentInstance.Results.Add(key, RunCreationClient.Instance.ConvertToItem(c));
    174           filter.Add(key);
    175         }
    176       }
    177       // TODO: There is a problem with missing values that has to be solved
    178       // The common set of characteristics among all problems may be too small to be useful
    179       // It has to be decided somehow which problem instances to include in order to obtain the set of features that is expressive and available
    180       var allCharacteristics = ProblemInstances.Select(x => new HashSet<string>(x.Results.Where(y => y.Key.StartsWith("Characteristic.", StringComparison.Ordinal)).Select(y => y.Key))).ToList();
    181       if (allCharacteristics.Count == 0) return;
    182 
    183       var commonCharacteristics = filter.Count > 0 ? filter : allCharacteristics[0];
    184       for (var i = 0; i < allCharacteristics.Count; i++)
    185         commonCharacteristics.IntersectWith(allCharacteristics[i]);
    186 
    187       if (commonCharacteristics.Count == 0) return;
    188 
    189       var instances = new Dictionary<string, double[]>();
    190       foreach (var i in ProblemInstances)
    191         instances[i.Name] = i.Results.Where(x => commonCharacteristics.Contains(x.Key)).OrderBy(x => x.Key).Select(x => (double)((dynamic)x.Value).Value).ToArray();
    192       if (currentInstance != null)
    193         instances[currentInstance.Name] = currentInstance.Results.Where(x => commonCharacteristics.Contains(x.Key)).OrderBy(x => x.Key).Select(x => (double)((dynamic)x.Value).Value).ToArray();
     170    public bool IsCurrentInstance(IRun run) {
     171      if (!problemId2ProblemInstanceMapping.ContainsSecond(run)) return false;
     172      return problemId2ProblemInstanceMapping.GetBySecond(run) == Problem.ProblemId;
     173    }
     174
     175    public void UpdateInstanceProjection() {
     176      if (ProblemCharacteristics.Count == 0) return;
     177
     178      var instances = new Dictionary<IRun, double[]>();
     179      foreach (var run in ProblemInstances) {
     180        var f = 0;
     181        instances[run] = new double[ProblemCharacteristics.CheckedItems.Count()];
     182        foreach (var c in ProblemCharacteristics.CheckedItems.Select(x => x.Value.Value)) {
     183          IItem item;
     184          if (run.Results.TryGetValue(c, out item)) {
     185            instances[run][f] = (double)((dynamic)item).Value;
     186          } else instances[run][f] = 0; // TODO: handle missing values
     187          f++;
     188        }
     189      }
    194190
    195191      var allValues = instances.SelectMany(x => x.Value).ToList();
     
    205201      }
    206202
    207       var key2Idx = new BidirectionalDictionary<string, int>();
     203      var key2Idx = new BidirectionalDictionary<IRun, int>();
    208204      foreach (var kvp in instances.Select((k, i) => new { Index = i, Key = k.Key }))
    209205        key2Idx.Add(kvp.Key, kvp.Index);
     
    222218      #endregion
    223219      #region PCA
    224       var ds = new double[instances.Count, commonCharacteristics.Count];
     220      var ds = new double[instances.Count, ProblemCharacteristics.CheckedItems.Count()];
    225221      foreach (var instance in instances) {
    226222        var arr = instance.Value;
     
    235231      #endregion
    236232      #region SOM
    237       var features = new DoubleMatrix(commonCharacteristics.Count, instances.Count);
     233      var features = new DoubleMatrix(ProblemCharacteristics.CheckedItems.Count(), instances.Count);
    238234      foreach (var instance in instances) {
    239235        var arr = instance.Value;
     
    241237          features[feature, key2Idx.GetByFirst(instance.Key)] = arr[feature];
    242238      }
    243       var somCoords = SOM.Map(features, new MersenneTwister(42), somSize: 20, learningRadius: 20, jittering: true);
     239      var somCoords = SOM.Map(features, new MersenneTwister(42), somSize: 20, learningRadius: 20, iterations: 200, jittering: true);
    244240      #endregion
    245241
     
    249245          double x = 0, y = 0;
    250246          for (var feature = 0; feature < ds.GetLength(1); feature++) {
    251             x += ds[key2Idx.GetByFirst(instance.Name), feature] * v[feature, 0];
    252             y += ds[key2Idx.GetByFirst(instance.Name), feature] * v[feature, 1];
     247            x += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 0];
     248            y += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 1];
    253249          }
    254250
     
    262258
    263259          if (instance.Results.TryGetValue("Projection.MDS.X", out item)) {
    264             ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance.Name), 0];
    265           } else instance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(instance.Name), 0]));
     260            ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 0];
     261          } else instance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(instance), 0]));
    266262          if (instance.Results.TryGetValue("Projection.MDS.Y", out item)) {
    267             ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance.Name), 1];
    268           } else instance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(instance.Name), 1]));
     263            ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 1];
     264          } else instance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(instance), 1]));
    269265
    270266          if (instance.Results.TryGetValue("Projection.SOM.X", out item)) {
    271             ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance.Name), 0];
    272           } else instance.Results.Add("Projection.SOM.X", new DoubleValue(somCoords[key2Idx.GetByFirst(instance.Name), 0]));
     267            ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 0];
     268          } else instance.Results.Add("Projection.SOM.X", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 0]));
    273269          if (instance.Results.TryGetValue("Projection.SOM.Y", out item)) {
    274             ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance.Name), 1];
    275           } else instance.Results.Add("Projection.SOM.Y", new DoubleValue(somCoords[key2Idx.GetByFirst(instance.Name), 1]));
     270            ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 1];
     271          } else instance.Results.Add("Projection.SOM.Y", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 1]));
    276272        }
    277273      } finally { ProblemInstances.UpdateOfRunsInProgress = false; }
    278 
    279       if (currentInstance != null) {
    280         double x = 0, y = 0;
    281         for (var feature = 0; feature < ds.GetLength(1); feature++) {
    282           x += ds[key2Idx.GetByFirst(currentInstance.Name), feature] * v[feature, 0];
    283           y += ds[key2Idx.GetByFirst(currentInstance.Name), feature] * v[feature, 1];
    284         }
    285 
    286         IItem item;
    287         if (currentInstance.Results.TryGetValue("Projection.PCA.X", out item)) {
    288           ((DoubleValue)item).Value = x;
    289         } else currentInstance.Results.Add("Projection.PCA.X", new DoubleValue(x));
    290         if (currentInstance.Results.TryGetValue("Projection.PCA.Y", out item)) {
    291           ((DoubleValue)item).Value = y;
    292         } else currentInstance.Results.Add("Projection.PCA.Y", new DoubleValue(y));
    293 
    294         if (currentInstance.Results.TryGetValue("Projection.MDS.X", out item)) {
    295           ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(currentInstance.Name), 0];
    296         } else currentInstance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(currentInstance.Name), 0]));
    297         if (currentInstance.Results.TryGetValue("Projection.MDS.Y", out item)) {
    298           ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(currentInstance.Name), 1];
    299         } else currentInstance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(currentInstance.Name), 1]));
    300       }
    301     }
    302 
    303     public Tuple<double, double> ProjectCurrentInstance(string projection) {
    304       if (currentInstance == null) return null;
    305       var xKey = "Projection." + projection + ".X";
    306       var yKey = "Projection." + projection + ".Y";
    307       if (!currentInstance.Results.ContainsKey(xKey) || !currentInstance.Results.ContainsKey(yKey))
    308         return null;
    309       var x = ((DoubleValue)currentInstance.Results[xKey]).Value;
    310       var y = ((DoubleValue)currentInstance.Results[yKey]).Value;
    311       return Tuple.Create(x, y);
    312274    }
    313275
     
    455417        ProblemInstances.UpdateOfRunsInProgress = true;
    456418        ProblemInstances.Clear();
     419        var characteristics = new HashSet<string>();
    457420        var totalProblems = adminClient.Problems.Count(x => x.ProblemClassId == probClassId);
    458421        foreach (var problInst in adminClient.Problems.Where(x => x.ProblemClassId == probClassId)) {
     
    470433                foreach (var v in RunCreationClient.Instance.GetCharacteristicValues(problInst.Id)) {
    471434                  probRun.Results.Add("Characteristic." + v.Name, RunCreationClient.Instance.ConvertToItem(v));
     435                  characteristics.Add(v.Name);
    472436                }
    473437                ProblemInstances.Add(probRun);
     438                problemId2ProblemInstanceMapping.Add(problInst.Id, probRun);
    474439              } catch { }
    475440              stream.Close();
     
    561526              var result = ExpectedRuntimeHelper.CalculateErt(kvp.Value, "QualityPerEvaluations", bkQuality * target, maximization);
    562527              var resultName = kvp.Key + "@" + ((target - 1) * 100) + "%";
     528              characteristics.Add(resultName);
    563529              IItem item;
    564530              if (instance.Results.TryGetValue(resultName, out item)) {
     
    568534          }
    569535        }
     536        ProblemCharacteristics.Replace(characteristics.Select(x => new StringValue(x)));
    570537        try {
    571538          KnowledgeBase.UpdateOfRunsInProgress = true;
Note: See TracChangeset for help on using the changeset viewer.