Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Data/Product.cs @ 15711

Last change on this file since 15711 was 13071, checked in by gkronber, 8 years ago

#2499: added license headers and removed unused usings

File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.BioBoost.Data {
30  [StorableClass]
31  [Item("Product", "Summarizes product information.")]
32  public class Product : DataItem {
33
34    #region Parameters
35    public IValueParameter<StringValue> LabelParameter { get { return (IValueParameter<StringValue>)Parameters["Label"]; } }
36    public IValueParameter<DoubleValue> PriceParameter { get { return (IValueParameter<DoubleValue>)Parameters["Price"]; } }
37    public IValueParameter<DoubleValue> PriceAtSourceParameter { get { return (IValueParameter<DoubleValue>)Parameters["PriceAtSource"]; } }
38    public IValueParameter<DoubleValue> PriceAtTargetParameter { get { return (IValueParameter<DoubleValue>)Parameters["PriceAtTarget"]; } }
39    public IValueParameter<DoubleValue> MaxPenaltyParameter { get { return (IValueParameter<DoubleValue>)Parameters["MaxPenalty"]; } }
40    public IValueParameter<DoubleValue> PercentageParameter { get { return (IValueParameter<DoubleValue>)Parameters["Percentage"]; } }
41    public IValueParameter<DoubleValue> MaxPriceParameter { get { return (IValueParameter<DoubleValue>)Parameters["MaxPrice"]; } }
42    public IValueParameter<ValueParameterCollection> PriceScalingParameter { get { return (IValueParameter<ValueParameterCollection>)Parameters["PriceScaling"]; } }
43    public IValueParameter<ValueParameterCollection> MaxPriceScalingParameter { get { return (IValueParameter<ValueParameterCollection>)Parameters["MaxPriceScaling"]; } }
44    #endregion
45
46    #region Parameter Values
47    public string Label { get { return LabelParameter.Value.Value; } }
48    public double Price { get { return PriceParameter.Value.Value; } }
49    public double PriceAtSource { get { return (PriceAtSourceParameter.Value ?? PriceParameter.Value).Value; } }
50    public double PriceAtTarget { get { return (PriceAtTargetParameter.Value ?? PriceParameter.Value).Value; } }
51    public double MaxPenalty { get { return MaxPenaltyParameter.Value.Value; } }
52    public double Percentage { get { return PercentageParameter.Value.Value; } }
53    public double MaxPrice { get { return MaxPriceParameter.Value.Value; } }
54    public ValueParameterCollection PriceScaling { get { return PriceScalingParameter.Value; } }
55    public ValueParameterCollection MaxPriceScaling { get { return MaxPriceScalingParameter.Value; } }
56    #endregion
57
58    #region Construction & Cloning
59    public Product() {
60      Parameters.Add(new ValueParameter<StringValue>("Label", "The label/name of this product."));
61      Parameters.Add(new ValueParameter<DoubleValue>("Price", "The product's price; can be negative to indicate disposal cost.", new DoubleValue(0)));
62      Parameters.Add(new OptionalValueParameter<DoubleValue>("PriceAtSource", "The product's price at the source/production location."));
63      Parameters.Add(new OptionalValueParameter<DoubleValue>("PriceAtTarget", "The product's price when delivered to the customer."));
64      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)));
65      Parameters.Add(new ValueParameter<DoubleValue>("Percentage", "The percentage of the feedstock utilization where the price starts to increase.", new DoubleValue(0.5)));
66      Parameters.Add(new ValueParameter<DoubleValue>("MaxPrice", "The maximum product's price at 100 percent utilization.", new DoubleValue(0)));
67      Parameters.Add(new ValueParameter<ValueParameterCollection>("PriceScaling", "Regional scaling of base price", new ValueParameterCollection()));
68      Parameters.Add(new ValueParameter<ValueParameterCollection>("MaxPriceScaling", "Regional scaling of max price", new ValueParameterCollection()));
69      RegisterEventHandlers();
70    }
71
72    [StorableConstructor]
73    protected Product(bool isDeserializing) : base(isDeserializing) { }
74
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77      RegisterEventHandlers();
78      if (!Parameters.ContainsKey("PriceScaling"))
79        Parameters.Add(new ValueParameter<ValueParameterCollection>("PriceScaling", "Regional scaling of base price", new ValueParameterCollection()));
80      if (!Parameters.ContainsKey("MaxPriceScaling"))
81        Parameters.Add(new ValueParameter<ValueParameterCollection>("MaxPriceScaling", "Regional scaling of max price", new ValueParameterCollection()));
82    }
83
84    protected Product(Product orig, Cloner cloner)
85      : base(orig, cloner) {
86      RegisterEventHandlers();
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new Product(this, cloner);
91    }
92    #endregion
93
94    public override void CollectParameterValues(System.Collections.Generic.IDictionary<string, IItem> values) {
95      var tmpDict = new Dictionary<string, IItem>();
96      base.CollectParameterValues(tmpDict);
97      foreach (var entry in tmpDict) {
98        if (entry.Key == "Label") continue;
99        values.Add("Product " + Label + "." + entry.Key, entry.Value);
100      }
101    }
102    private void RegisterEventHandlers() {
103      // when the HL value is changed
104      LabelParameter.ValueChanged += (sender, args) => {
105        UpdateName();
106        LabelParameter.Value.ValueChanged += (s, a) => { UpdateName(); };
107      };
108      // when the HL value's value is changed
109      LabelParameter.Value.ValueChanged += (sender, args) => { UpdateName(); };
110    }
111
112    private void UpdateName() {
113      this.Name = ItemName + ": " + Label;
114    }
115
116    public override bool IsEquivalentTo(DataItem other) {
117      var p = other as Product;
118      if (p == null) return false;
119      return p.Label == Label;
120    }
121
122    public double GetRegionalBasePrice(string regionName) {
123      IValueParameter priceParam = null;
124      IValueParameter<DoubleValue> price;
125      while (regionName.Length >= 2) {
126        if (PriceScaling.TryGetValue(regionName, out priceParam)) break;
127        regionName = regionName.Remove(regionName.Length - 1);
128      }
129      price = priceParam as IValueParameter<DoubleValue>;
130      if (price != null) return Price * price.Value.Value;
131      return Price;
132    }
133
134    public double GetRegionalMaxPrice(string regionName) {
135      IValueParameter maxPriceParam = null;
136      IValueParameter<DoubleValue> maxPrice;
137      while (regionName.Length >= 2) {
138        if (MaxPriceScaling.TryGetValue(regionName, out maxPriceParam)) break;
139        regionName = regionName.Remove(regionName.Length - 1);
140      }
141      maxPrice = maxPriceParam as IValueParameter<DoubleValue>;
142      if (maxPrice != null) return MaxPrice * maxPrice.Value.Value;
143      return MaxPrice;
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.