Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Data/Product.cs @ 13069

Last change on this file since 13069 was 13069, checked in by gkronber, 9 years ago

#2499: imported source code for HeuristicLab.BioBoost from private repository with some changes

File size: 6.8 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Parameters;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8namespace HeuristicLab.BioBoost.Data {
9  [StorableClass]
10  [Item("Product", "Summarizes product information.")]
11  public class Product : DataItem {
12
13    #region Parameters
14    public IValueParameter<StringValue> LabelParameter { get { return (IValueParameter<StringValue>)Parameters["Label"]; } }
15    public IValueParameter<DoubleValue> PriceParameter { get { return (IValueParameter<DoubleValue>)Parameters["Price"]; } }
16    public IValueParameter<DoubleValue> PriceAtSourceParameter { get { return (IValueParameter<DoubleValue>)Parameters["PriceAtSource"]; } }
17    public IValueParameter<DoubleValue> PriceAtTargetParameter { get { return (IValueParameter<DoubleValue>)Parameters["PriceAtTarget"]; } }
18    public IValueParameter<DoubleValue> MaxPenaltyParameter { get { return (IValueParameter<DoubleValue>)Parameters["MaxPenalty"]; } }
19    public IValueParameter<DoubleValue> PercentageParameter { get { return (IValueParameter<DoubleValue>)Parameters["Percentage"]; } }
20    public IValueParameter<DoubleValue> MaxPriceParameter { get { return (IValueParameter<DoubleValue>)Parameters["MaxPrice"]; } }
21    public IValueParameter<ValueParameterCollection> PriceScalingParameter { get { return (IValueParameter<ValueParameterCollection>)Parameters["PriceScaling"]; } }
22    public IValueParameter<ValueParameterCollection> MaxPriceScalingParameter { get { return (IValueParameter<ValueParameterCollection>)Parameters["MaxPriceScaling"]; } }
23    #endregion
24
25    #region Parameter Values
26    public string Label { get { return LabelParameter.Value.Value; } }
27    public double Price { get { return PriceParameter.Value.Value; } }
28    public double PriceAtSource { get { return (PriceAtSourceParameter.Value ?? PriceParameter.Value).Value; } }
29    public double PriceAtTarget { get { return (PriceAtTargetParameter.Value ?? PriceParameter.Value).Value; } }
30    public double MaxPenalty { get { return MaxPenaltyParameter.Value.Value; } }
31    public double Percentage { get { return PercentageParameter.Value.Value; } }
32    public double MaxPrice { get { return MaxPriceParameter.Value.Value; } }
33    public ValueParameterCollection PriceScaling { get { return PriceScalingParameter.Value; } }
34    public ValueParameterCollection MaxPriceScaling { get { return MaxPriceScalingParameter.Value; } }
35    #endregion
36
37    #region Construction & Cloning
38    public Product() {
39      Parameters.Add(new ValueParameter<StringValue>("Label", "The label/name of this product."));
40      Parameters.Add(new ValueParameter<DoubleValue>("Price", "The product's price; can be negative to indicate disposal cost.", new DoubleValue(0)));
41      Parameters.Add(new OptionalValueParameter<DoubleValue>("PriceAtSource", "The product's price at the source/production location."));
42      Parameters.Add(new OptionalValueParameter<DoubleValue>("PriceAtTarget", "The product's price when delivered to the customer."));
43      Parameters.Add(new ValueParameter<DoubleValue>("MaxPenalty", "The maximum penalty factor that is then multiplied with the base price at maximum market saturation.", new DoubleValue(1)));
44      Parameters.Add(new ValueParameter<DoubleValue>("Percentage", "The percentage of the feedstock utilization where the price starts to increase.", new DoubleValue(0.5)));
45      Parameters.Add(new ValueParameter<DoubleValue>("MaxPrice", "The maximum product's price at 100 percent utilization.", new DoubleValue(0)));
46      Parameters.Add(new ValueParameter<ValueParameterCollection>("PriceScaling", "Regional scaling of base price", new ValueParameterCollection()));
47      Parameters.Add(new ValueParameter<ValueParameterCollection>("MaxPriceScaling", "Regional scaling of max price", new ValueParameterCollection()));
48      RegisterEventHandlers();
49    }
50
51    [StorableConstructor]
52    protected Product(bool isDeserializing) : base(isDeserializing) { }
53
54    [StorableHook(HookType.AfterDeserialization)]
55    private void AfterDeserialization() {
56      RegisterEventHandlers();
57      if (!Parameters.ContainsKey("PriceScaling"))
58        Parameters.Add(new ValueParameter<ValueParameterCollection>("PriceScaling", "Regional scaling of base price", new ValueParameterCollection()));
59      if (!Parameters.ContainsKey("MaxPriceScaling"))
60        Parameters.Add(new ValueParameter<ValueParameterCollection>("MaxPriceScaling", "Regional scaling of max price", new ValueParameterCollection()));
61    }
62
63    protected Product(Product orig, Cloner cloner)
64      : base(orig, cloner) {
65      RegisterEventHandlers();
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new Product(this, cloner);
70    }
71    #endregion
72
73    public override void CollectParameterValues(System.Collections.Generic.IDictionary<string, IItem> values) {
74      var tmpDict = new Dictionary<string, IItem>();
75      base.CollectParameterValues(tmpDict);
76      foreach (var entry in tmpDict) {
77        if (entry.Key == "Label") continue;
78        values.Add("Product " + Label + "." + entry.Key, entry.Value);
79      }
80    }
81    private void RegisterEventHandlers() {
82      // when the HL value is changed
83      LabelParameter.ValueChanged += (sender, args) => {
84        UpdateName();
85        LabelParameter.Value.ValueChanged += (s, a) => { UpdateName(); };
86      };
87      // when the HL value's value is changed
88      LabelParameter.Value.ValueChanged += (sender, args) => { UpdateName(); };
89    }
90
91    private void UpdateName() {
92      this.Name = ItemName + ": " + Label;
93    }
94
95    public override bool IsEquivalentTo(DataItem other) {
96      var p = other as Product;
97      if (p == null) return false;
98      return p.Label == Label;
99    }
100
101    public double GetRegionalBasePrice(string regionName) {
102      IValueParameter priceParam = null;
103      IValueParameter<DoubleValue> price;
104      while (regionName.Length >= 2) {
105        if (PriceScaling.TryGetValue(regionName, out priceParam)) break;
106        regionName = regionName.Remove(regionName.Length - 1);
107      }
108      price = priceParam as IValueParameter<DoubleValue>;
109      if (price != null) return Price * price.Value.Value;
110      return Price;
111    }
112
113    public double GetRegionalMaxPrice(string regionName) {
114      IValueParameter maxPriceParam = null;
115      IValueParameter<DoubleValue> maxPrice;
116      while (regionName.Length >= 2) {
117        if (MaxPriceScaling.TryGetValue(regionName, out maxPriceParam)) break;
118        regionName = regionName.Remove(regionName.Length - 1);
119      }
120      maxPrice = maxPriceParam as IValueParameter<DoubleValue>;
121      if (maxPrice != null) return MaxPrice * maxPrice.Value.Value;
122      return MaxPrice;
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.