Changeset 13075


Ignore:
Timestamp:
10/28/15 08:01:42 (4 years ago)
Author:
gkronber
Message:

#2499: added missing file DictionaryExtensions.cs

Location:
branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/HeuristicLab.Problems.BioBoost-3.3.csproj

    r13069 r13075  
    292292    <Compile Include="SolutionCreation\IdentityIntegerVectorCreator.cs" />
    293293    <Compile Include="Utils\Closure.cs" />
     294    <Compile Include="Utils\DictionaryExtensions.cs" />
    294295    <Compile Include="Utils\EnumerableExtensions.cs" />
    295296    <Compile Include="Utils\ListExtensions.cs" />
  • branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Representation/BioBoostCompoundSolution.cs

    r13071 r13075  
    136136    [StorableConstructor]
    137137    private BioBoostCompoundSolution(bool isDeserializing) : base(isDeserializing) { }
    138     private BioBoostCompoundSolution(BioBoostCompoundSolution orig, Cloner cloner, bool full = true) : base(orig, cloner) {
     138    private BioBoostCompoundSolution(BioBoostCompoundSolution orig, Cloner cloner, bool full = true)
     139      : base(orig, cloner) {
    139140      problemDataRef = orig.problemDataRef; // doesn't re-evaluate the solution
    140141      Geometry = orig.Geometry;
    141142      Quality = orig.Quality;
    142       Qualities = (DoubleArray) orig.Qualities.Clone();
    143       IntValues = orig.IntValues.ToDictionary(kvp => kvp.Key, kvp => (IntArray) kvp.Value.Clone());
     143      Qualities = (DoubleArray)orig.Qualities.Clone();
     144      IntValues = orig.IntValues.ToDictionary(kvp => kvp.Key, kvp => (IntArray)kvp.Value.Clone());
    144145      if (full) {
    145         DoubleValues = orig.DoubleValues.ToDictionary(kvp => kvp.Key, kvp => (DoubleArray) kvp.Value.Clone());
    146         StringValues = orig.StringValues.ToDictionary(kvp => kvp.Key, kvp => (StringArray) kvp.Value.Clone());
     146        DoubleValues = orig.DoubleValues.ToDictionary(kvp => kvp.Key, kvp => (DoubleArray)kvp.Value.Clone());
     147        StringValues = orig.StringValues.ToDictionary(kvp => kvp.Key, kvp => (StringArray)kvp.Value.Clone());
    147148        if (orig.TransportTargets != null)
    148149          TransportTargets = orig.TransportTargets.ToDictionary(kvp => kvp.Key, kvp => (ItemArray<ItemList<IntValue>>)kvp.Value.Clone());
    149150      } else {
    150         DoubleValues = orig.DoubleValues.Where(kvp => IsSlimKey(kvp.Key)).ToDictionary(kvp => kvp.Key, kvp => (DoubleArray) kvp.Value.Clone());
     151        DoubleValues = orig.DoubleValues.Where(kvp => IsSlimKey(kvp.Key)).ToDictionary(kvp => kvp.Key, kvp => (DoubleArray)kvp.Value.Clone());
    151152        StringValues = new Dictionary<string, StringArray>();
    152153        if (orig.TransportTargets != null)
     
    171172      var doubleArray = var.Value as DoubleArray;
    172173      if (doubleArray != null && !DoubleValues.ContainsKey(valueName)) {
    173         DoubleValues.Add(valueName, (DoubleArray) doubleArray.Clone());
     174        DoubleValues.Add(valueName, (DoubleArray)doubleArray.Clone());
    174175        return true;
    175176      }
    176177      var intArray = var.Value as IntArray;
    177178      if (intArray != null && !IntValues.ContainsKey(valueName)) {
    178         IntValues.Add(valueName, (IntArray) intArray.Clone());
     179        IntValues.Add(valueName, (IntArray)intArray.Clone());
    179180        return true;
    180181      }
    181182      var stringArray = var.Value as StringArray;
    182183      if (stringArray != null && !StringValues.ContainsKey(valueName)) {
    183         StringValues.Add(valueName, (StringArray) stringArray.Clone());
     184        StringValues.Add(valueName, (StringArray)stringArray.Clone());
    184185        return true;
    185186      }
    186187      var itemArray = var.Value as ItemArray<ItemList<IntValue>>;
    187188      if (itemArray != null && !TransportTargets.ContainsKey(valueName)) {
    188         TransportTargets.Add(valueName, (ItemArray<ItemList<IntValue>>) itemArray.Clone());
     189        TransportTargets.Add(valueName, (ItemArray<ItemList<IntValue>>)itemArray.Clone());
    189190      }
    190191      return false;
     
    198199          var doubleArray = valueParam.Value as DoubleArray;
    199200          if (doubleArray != null && !DoubleValues.ContainsKey(valueName)) {
    200             DoubleValues.Add(valueName, (DoubleArray) doubleArray.Clone());
     201            DoubleValues.Add(valueName, (DoubleArray)doubleArray.Clone());
    201202            return true;
    202203          }
    203204          var intArray = valueParam.Value as IntArray;
    204205          if (intArray != null && !IntValues.ContainsKey(valueName)) {
    205             IntValues.Add(valueName, (IntArray) intArray.Clone());
     206            IntValues.Add(valueName, (IntArray)intArray.Clone());
    206207            return true;
    207208          }
    208209          var stringArray = valueParam.Value as StringArray;
    209210          if (stringArray != null && !StringValues.ContainsKey(valueName)) {
    210             StringValues.Add(valueName, (StringArray) stringArray.Clone());
     211            StringValues.Add(valueName, (StringArray)stringArray.Clone());
    211212            return true;
    212213          }
    213214          var itemArray = valueParam.Value as ItemArray<ItemList<IntValue>>;
    214215          if (itemArray != null && !TransportTargets.ContainsKey(valueName)) {
    215             TransportTargets.Add(valueName, (ItemArray<ItemList<IntValue>>) itemArray.Clone());
     216            TransportTargets.Add(valueName, (ItemArray<ItemList<IntValue>>)itemArray.Clone());
    216217            return true;
    217218          }
     
    221222    }
    222223
    223     public BioBoostCompoundSolution(IScope scope, BioBoostProblemData problemData) : this() {
     224    public BioBoostCompoundSolution(IScope scope, BioBoostProblemData problemData)
     225      : this() {
    224226      this.problemDataRef = problemData; // not cloned on purpose
    225227      Geometry = problemData.Geometry; // TODO: maybe clone geometry once?
     
    228230      // TotalCost is the quality produced by the aggregate evaluator
    229231      if (scope.Variables.TryGetValue("TotalCost", out qualityVariable)) {
    230         Quality = ((DoubleValue) qualityVariable.Value).Value; // TODO: should be read only
     232        Quality = ((DoubleValue)qualityVariable.Value).Value; // TODO: should be read only
    231233      } else if (scope.Variables.TryGetValue("Quality", out qualityVariable)) {
    232         Quality = ((DoubleValue) qualityVariable.Value).Value; // TODO: should be read only
    233       } 
     234        Quality = ((DoubleValue)qualityVariable.Value).Value; // TODO: should be read only
     235      }
    234236      if (scope.Variables.TryGetValue("QualityCriteria", out qualityVariable)) {
    235         Qualities = (DoubleArray) qualityVariable.Value.Clone(); // TODO: should be read only
    236       } 
     237        Qualities = (DoubleArray)qualityVariable.Value.Clone(); // TODO: should be read only
     238      }
    237239      var isUpgradedSolution = scope.Variables.Any(v => v.Name.Contains(LayerDescriptor.AmountsAtSource.FullName));
    238240      foreach (var p in problemData.Products) {
     
    263265      if (!Parameters.ContainsKey("Quality"))
    264266        Parameters.Add(new FixedValueParameter<DoubleValue>("Quality", new DoubleValue(double.NaN))); // TODO: use different quality value to indicate that the value is not known?
    265       if(!Parameters.ContainsKey("Qualities"))
     267      if (!Parameters.ContainsKey("Qualities"))
    266268        Parameters.Add(new ValueParameter<DoubleArray>("Qualities", "The quality criteria of the solution as produced by the evaluator.", new DoubleArray()));
    267269    }
     
    271273      if (LocationNames.SequenceEqual(bestSolution.LocationNames)) { // TODO: replace with comparison of problem reference
    272274        Quality = bestSolution.Quality;
    273         Qualities = (DoubleArray) bestSolution.Qualities.Clone();
     275        Qualities = (DoubleArray)bestSolution.Qualities.Clone();
    274276        IntValues = bestSolution.IntValues.ToDictionary(kvp => kvp.Key, kvp => (IntArray)kvp.Value.Clone());
    275277        DoubleValues = bestSolution.DoubleValues.ToDictionary(kvp => kvp.Key, kvp => (DoubleArray)kvp.Value.Clone());
     
    503505      var feedstockUsageBefore =
    504506        utilizations.Zip(
    505           (DoubleArray) ((IValueParameter) problemDataRef.FeedstockPotentials[feedstockName + "Potentials"]).Value,
    506           (u, p) => u*p).Sum();
     507          (DoubleArray)((IValueParameter)problemDataRef.FeedstockPotentials[feedstockName + "Potentials"]).Value,
     508          (u, p) => u * p).Sum();
    507509
    508510      // remove penalized transports
     
    511513          kvp => kvp.Key == LayerDescriptor.ExceedingTransportDistancePenalty.NameWithPrefix(feedstockName)).Value;
    512514      var removableSuppliers = feedstockTransportPenaltyLayer
    513         .Select((penalty, idx) => new {penalty, idx})
     515        .Select((penalty, idx) => new { penalty, idx })
    514516        .Where(pi => pi.penalty > 0)
    515         .Select(pi => new List<int> {pi.idx}).ToList();
     517        .Select(pi => new List<int> { pi.idx }).ToList();
    516518
    517519      // remove undersize central plants
    518520      var centralConversion = ProblemDataReference.Conversions[feedstockName].First();
    519       var minCentralScalingFactor = centralConversion.MinCapacity/centralConversion.DesignCapacity;
     521      var minCentralScalingFactor = centralConversion.MinCapacity / centralConversion.DesignCapacity;
    520522      var centralScalingFactors =
    521523        DoubleValues.First(kvp => kvp.Key == LayerDescriptor.ScalingFactors.NameWithPrefix(intermediateName)).Value;
    522524      removableSuppliers.AddRange(centralScalingFactors
    523         .Select((factor, idx) => new {factor, idx})
     525        .Select((factor, idx) => new { factor, idx })
    524526        .Where(fi => fi.factor < minCentralScalingFactor)
    525         .Select(fi => GetSuppliers(new[] {centralPlantSources, decentralPlantSources}, fi.idx).ToList())
     527        .Select(fi => GetSuppliers(new[] { centralPlantSources, decentralPlantSources }, fi.idx).ToList())
    526528        .Where(l => l.Count > 0));
    527529
    528530      // remove undersize decentral plants
    529531      var decentralConversion = ProblemDataReference.Conversions[intermediateName].First();
    530       var minDeCentralScalingFactor = decentralConversion.MinCapacity/decentralConversion.DesignCapacity;
     532      var minDeCentralScalingFactor = decentralConversion.MinCapacity / decentralConversion.DesignCapacity;
    531533      var decentralScalingFactors =
    532534        DoubleValues.First(kvp => kvp.Key == LayerDescriptor.ScalingFactors.NameWithPrefix(feedstockName)).Value;
    533535      removableSuppliers.AddRange(decentralScalingFactors
    534         .Select((factor, idx) => new {factor, idx})
     536        .Select((factor, idx) => new { factor, idx })
    535537        .Where(fi => fi.factor < minDeCentralScalingFactor)
    536         .Select(fi => GetSuppliers(new[] {decentralPlantSources}, fi.idx).ToList())
     538        .Select(fi => GetSuppliers(new[] { decentralPlantSources }, fi.idx).ToList())
    537539        .Where(l => l.Count > 0));
    538540
     
    549551        EvaluateAndUpdateAllResults();
    550552        if (Quality < oldQuality) {
    551           for (int i = 0; i<utilizations.Length; i++) utilizations[i] = oldUtilizations[i];
     553          for (int i = 0; i < utilizations.Length; i++) utilizations[i] = oldUtilizations[i];
    552554          DoubleValues.Remove(LayerDescriptor.Utilizations.NameWithPrefix(feedstockName));
    553555          DoubleValues.Add(LayerDescriptor.Utilizations.NameWithPrefix(feedstockName), utilizations);
     
    555557          successes++;
    556558        }
    557         if (k++ > lastK + removableSuppliers.Count/20 && logger != null) {
    558           logger.Write("{0}%...", k*100/removableSuppliers.Count);
     559        if (k++ > lastK + removableSuppliers.Count / 20 && logger != null) {
     560          logger.Write("{0}%...", k * 100 / removableSuppliers.Count);
    559561          lastK = k;
    560562        }
     
    564566
    565567      var feedstockUsageAfter =
    566         utilizations.Zip((DoubleArray) ((IValueParameter) problemDataRef.FeedstockPotentials[feedstockName + "Potentials"]).Value,
    567           (u, p) => u*p).Sum();
    568       var utilizationFactor = feedstockUsageAfter/feedstockUsageBefore;
     568        utilizations.Zip((DoubleArray)((IValueParameter)problemDataRef.FeedstockPotentials[feedstockName + "Potentials"]).Value,
     569          (u, p) => u * p).Sum();
     570      var utilizationFactor = feedstockUsageAfter / feedstockUsageBefore;
    569571      return utilizationFactor;
    570572    }
    571573
    572574    public static IEnumerable<int> GetSuppliers(IEnumerable<IDictionary<int, int[]>> suppliers, int i) {
    573       throw new NotImplementedException();  // waiting for source for DictionaryExtensions
    574       //       var lists = suppliers.ToList();
    575 //       switch (lists.Count) {
    576 //         case 0: {
    577 //           yield break;
    578 //         }
    579 //         case 1: {
    580 //           foreach (var j in lists[0].ElementAtOrDefault(i, new int[0]))
    581 //             yield return j;
    582 //           break;
    583 //         }
    584 //         default: {
    585 //           foreach (
    586 //             var x in lists[0].ApplyAt(i,
    587 //                 v => v.Select(j => GetSuppliers(lists.Skip(1), j))
    588 //                       .Aggregate(Enumerable.Empty<int>(), (l1, l2) => l1.Concat(l2)),
    589 //                 () => new int[0]))
    590 //             yield return x;
    591 //           break;
    592 //         }
    593 //       }
     575      var lists = suppliers.ToList();
     576      switch (lists.Count) {
     577        case 0: {
     578            yield break;
     579          }
     580        case 1: {
     581            foreach (var j in lists[0].ElementAtOrDefault(i, new int[0]))
     582              yield return j;
     583            break;
     584          }
     585        default: {
     586            foreach (
     587              var x in lists[0].ApplyAt(i,
     588                  v => v.Select(j => GetSuppliers(lists.Skip(1), j))
     589                        .Aggregate(Enumerable.Empty<int>(), (l1, l2) => l1.Concat(l2)),
     590                  () => new int[0]))
     591              yield return x;
     592            break;
     593          }
     594      }
    594595    }
    595596
Note: See TracChangeset for help on using the changeset viewer.