Changeset 13075
- Timestamp:
- 10/28/15 08:01:42 (9 years ago)
- 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 292 292 <Compile Include="SolutionCreation\IdentityIntegerVectorCreator.cs" /> 293 293 <Compile Include="Utils\Closure.cs" /> 294 <Compile Include="Utils\DictionaryExtensions.cs" /> 294 295 <Compile Include="Utils\EnumerableExtensions.cs" /> 295 296 <Compile Include="Utils\ListExtensions.cs" /> -
branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Representation/BioBoostCompoundSolution.cs
r13071 r13075 136 136 [StorableConstructor] 137 137 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) { 139 140 problemDataRef = orig.problemDataRef; // doesn't re-evaluate the solution 140 141 Geometry = orig.Geometry; 141 142 Quality = orig.Quality; 142 Qualities = (DoubleArray) 143 IntValues = orig.IntValues.ToDictionary(kvp => kvp.Key, kvp => (IntArray) 143 Qualities = (DoubleArray)orig.Qualities.Clone(); 144 IntValues = orig.IntValues.ToDictionary(kvp => kvp.Key, kvp => (IntArray)kvp.Value.Clone()); 144 145 if (full) { 145 DoubleValues = orig.DoubleValues.ToDictionary(kvp => kvp.Key, kvp => (DoubleArray) 146 StringValues = orig.StringValues.ToDictionary(kvp => kvp.Key, kvp => (StringArray) 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()); 147 148 if (orig.TransportTargets != null) 148 149 TransportTargets = orig.TransportTargets.ToDictionary(kvp => kvp.Key, kvp => (ItemArray<ItemList<IntValue>>)kvp.Value.Clone()); 149 150 } else { 150 DoubleValues = orig.DoubleValues.Where(kvp => IsSlimKey(kvp.Key)).ToDictionary(kvp => kvp.Key, kvp => (DoubleArray) 151 DoubleValues = orig.DoubleValues.Where(kvp => IsSlimKey(kvp.Key)).ToDictionary(kvp => kvp.Key, kvp => (DoubleArray)kvp.Value.Clone()); 151 152 StringValues = new Dictionary<string, StringArray>(); 152 153 if (orig.TransportTargets != null) … … 171 172 var doubleArray = var.Value as DoubleArray; 172 173 if (doubleArray != null && !DoubleValues.ContainsKey(valueName)) { 173 DoubleValues.Add(valueName, (DoubleArray) 174 DoubleValues.Add(valueName, (DoubleArray)doubleArray.Clone()); 174 175 return true; 175 176 } 176 177 var intArray = var.Value as IntArray; 177 178 if (intArray != null && !IntValues.ContainsKey(valueName)) { 178 IntValues.Add(valueName, (IntArray) 179 IntValues.Add(valueName, (IntArray)intArray.Clone()); 179 180 return true; 180 181 } 181 182 var stringArray = var.Value as StringArray; 182 183 if (stringArray != null && !StringValues.ContainsKey(valueName)) { 183 StringValues.Add(valueName, (StringArray) 184 StringValues.Add(valueName, (StringArray)stringArray.Clone()); 184 185 return true; 185 186 } 186 187 var itemArray = var.Value as ItemArray<ItemList<IntValue>>; 187 188 if (itemArray != null && !TransportTargets.ContainsKey(valueName)) { 188 TransportTargets.Add(valueName, (ItemArray<ItemList<IntValue>>) 189 TransportTargets.Add(valueName, (ItemArray<ItemList<IntValue>>)itemArray.Clone()); 189 190 } 190 191 return false; … … 198 199 var doubleArray = valueParam.Value as DoubleArray; 199 200 if (doubleArray != null && !DoubleValues.ContainsKey(valueName)) { 200 DoubleValues.Add(valueName, (DoubleArray) 201 DoubleValues.Add(valueName, (DoubleArray)doubleArray.Clone()); 201 202 return true; 202 203 } 203 204 var intArray = valueParam.Value as IntArray; 204 205 if (intArray != null && !IntValues.ContainsKey(valueName)) { 205 IntValues.Add(valueName, (IntArray) 206 IntValues.Add(valueName, (IntArray)intArray.Clone()); 206 207 return true; 207 208 } 208 209 var stringArray = valueParam.Value as StringArray; 209 210 if (stringArray != null && !StringValues.ContainsKey(valueName)) { 210 StringValues.Add(valueName, (StringArray) 211 StringValues.Add(valueName, (StringArray)stringArray.Clone()); 211 212 return true; 212 213 } 213 214 var itemArray = valueParam.Value as ItemArray<ItemList<IntValue>>; 214 215 if (itemArray != null && !TransportTargets.ContainsKey(valueName)) { 215 TransportTargets.Add(valueName, (ItemArray<ItemList<IntValue>>) 216 TransportTargets.Add(valueName, (ItemArray<ItemList<IntValue>>)itemArray.Clone()); 216 217 return true; 217 218 } … … 221 222 } 222 223 223 public BioBoostCompoundSolution(IScope scope, BioBoostProblemData problemData) : this() { 224 public BioBoostCompoundSolution(IScope scope, BioBoostProblemData problemData) 225 : this() { 224 226 this.problemDataRef = problemData; // not cloned on purpose 225 227 Geometry = problemData.Geometry; // TODO: maybe clone geometry once? … … 228 230 // TotalCost is the quality produced by the aggregate evaluator 229 231 if (scope.Variables.TryGetValue("TotalCost", out qualityVariable)) { 230 Quality = ((DoubleValue) 232 Quality = ((DoubleValue)qualityVariable.Value).Value; // TODO: should be read only 231 233 } else if (scope.Variables.TryGetValue("Quality", out qualityVariable)) { 232 Quality = ((DoubleValue) 233 } 234 Quality = ((DoubleValue)qualityVariable.Value).Value; // TODO: should be read only 235 } 234 236 if (scope.Variables.TryGetValue("QualityCriteria", out qualityVariable)) { 235 Qualities = (DoubleArray) 236 } 237 Qualities = (DoubleArray)qualityVariable.Value.Clone(); // TODO: should be read only 238 } 237 239 var isUpgradedSolution = scope.Variables.Any(v => v.Name.Contains(LayerDescriptor.AmountsAtSource.FullName)); 238 240 foreach (var p in problemData.Products) { … … 263 265 if (!Parameters.ContainsKey("Quality")) 264 266 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")) 266 268 Parameters.Add(new ValueParameter<DoubleArray>("Qualities", "The quality criteria of the solution as produced by the evaluator.", new DoubleArray())); 267 269 } … … 271 273 if (LocationNames.SequenceEqual(bestSolution.LocationNames)) { // TODO: replace with comparison of problem reference 272 274 Quality = bestSolution.Quality; 273 Qualities = (DoubleArray) 275 Qualities = (DoubleArray)bestSolution.Qualities.Clone(); 274 276 IntValues = bestSolution.IntValues.ToDictionary(kvp => kvp.Key, kvp => (IntArray)kvp.Value.Clone()); 275 277 DoubleValues = bestSolution.DoubleValues.ToDictionary(kvp => kvp.Key, kvp => (DoubleArray)kvp.Value.Clone()); … … 503 505 var feedstockUsageBefore = 504 506 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(); 507 509 508 510 // remove penalized transports … … 511 513 kvp => kvp.Key == LayerDescriptor.ExceedingTransportDistancePenalty.NameWithPrefix(feedstockName)).Value; 512 514 var removableSuppliers = feedstockTransportPenaltyLayer 513 .Select((penalty, idx) => new { penalty, idx})515 .Select((penalty, idx) => new { penalty, idx }) 514 516 .Where(pi => pi.penalty > 0) 515 .Select(pi => new List<int> { pi.idx}).ToList();517 .Select(pi => new List<int> { pi.idx }).ToList(); 516 518 517 519 // remove undersize central plants 518 520 var centralConversion = ProblemDataReference.Conversions[feedstockName].First(); 519 var minCentralScalingFactor = centralConversion.MinCapacity /centralConversion.DesignCapacity;521 var minCentralScalingFactor = centralConversion.MinCapacity / centralConversion.DesignCapacity; 520 522 var centralScalingFactors = 521 523 DoubleValues.First(kvp => kvp.Key == LayerDescriptor.ScalingFactors.NameWithPrefix(intermediateName)).Value; 522 524 removableSuppliers.AddRange(centralScalingFactors 523 .Select((factor, idx) => new { factor, idx})525 .Select((factor, idx) => new { factor, idx }) 524 526 .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()) 526 528 .Where(l => l.Count > 0)); 527 529 528 530 // remove undersize decentral plants 529 531 var decentralConversion = ProblemDataReference.Conversions[intermediateName].First(); 530 var minDeCentralScalingFactor = decentralConversion.MinCapacity /decentralConversion.DesignCapacity;532 var minDeCentralScalingFactor = decentralConversion.MinCapacity / decentralConversion.DesignCapacity; 531 533 var decentralScalingFactors = 532 534 DoubleValues.First(kvp => kvp.Key == LayerDescriptor.ScalingFactors.NameWithPrefix(feedstockName)).Value; 533 535 removableSuppliers.AddRange(decentralScalingFactors 534 .Select((factor, idx) => new { factor, idx})536 .Select((factor, idx) => new { factor, idx }) 535 537 .Where(fi => fi.factor < minDeCentralScalingFactor) 536 .Select(fi => GetSuppliers(new[] { decentralPlantSources}, fi.idx).ToList())538 .Select(fi => GetSuppliers(new[] { decentralPlantSources }, fi.idx).ToList()) 537 539 .Where(l => l.Count > 0)); 538 540 … … 549 551 EvaluateAndUpdateAllResults(); 550 552 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]; 552 554 DoubleValues.Remove(LayerDescriptor.Utilizations.NameWithPrefix(feedstockName)); 553 555 DoubleValues.Add(LayerDescriptor.Utilizations.NameWithPrefix(feedstockName), utilizations); … … 555 557 successes++; 556 558 } 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); 559 561 lastK = k; 560 562 } … … 564 566 565 567 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; 569 571 return utilizationFactor; 570 572 } 571 573 572 574 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 } 594 595 } 595 596
Note: See TracChangeset
for help on using the changeset viewer.