Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17777 was 16575, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.BioBoost addon to compile with new HL.Persistence

File size: 7.7 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;
28using HEAL.Attic;
29
30namespace HeuristicLab.BioBoost.Data {
31  [StorableType("EF528EDA-45D6-4DAB-862A-C05BCD4147CC")]
32  [Item("Product", "Summarizes product information.")]
33  public class Product : DataItem {
34
35    #region Parameters
36    public IValueParameter<StringValue> LabelParameter { get { return (IValueParameter<StringValue>)Parameters["Label"]; } }
37    public IValueParameter<DoubleValue> PriceParameter { get { return (IValueParameter<DoubleValue>)Parameters["Price"]; } }
38    public IValueParameter<DoubleValue> PriceAtSourceParameter { get { return (IValueParameter<DoubleValue>)Parameters["PriceAtSource"]; } }
39    public IValueParameter<DoubleValue> PriceAtTargetParameter { get { return (IValueParameter<DoubleValue>)Parameters["PriceAtTarget"]; } }
40    public IValueParameter<DoubleValue> MaxPenaltyParameter { get { return (IValueParameter<DoubleValue>)Parameters["MaxPenalty"]; } }
41    public IValueParameter<DoubleValue> PercentageParameter { get { return (IValueParameter<DoubleValue>)Parameters["Percentage"]; } }
42    public IValueParameter<DoubleValue> MaxPriceParameter { get { return (IValueParameter<DoubleValue>)Parameters["MaxPrice"]; } }
43    public IValueParameter<ValueParameterCollection> PriceScalingParameter { get { return (IValueParameter<ValueParameterCollection>)Parameters["PriceScaling"]; } }
44    public IValueParameter<ValueParameterCollection> MaxPriceScalingParameter { get { return (IValueParameter<ValueParameterCollection>)Parameters["MaxPriceScaling"]; } }
45    #endregion
46
47    #region Parameter Values
48    public string Label { get { return LabelParameter.Value.Value; } }
49    public double Price { get { return PriceParameter.Value.Value; } }
50    public double PriceAtSource { get { return (PriceAtSourceParameter.Value ?? PriceParameter.Value).Value; } }
51    public double PriceAtTarget { get { return (PriceAtTargetParameter.Value ?? PriceParameter.Value).Value; } }
52    public double MaxPenalty { get { return MaxPenaltyParameter.Value.Value; } }
53    public double Percentage { get { return PercentageParameter.Value.Value; } }
54    public double MaxPrice { get { return MaxPriceParameter.Value.Value; } }
55    public ValueParameterCollection PriceScaling { get { return PriceScalingParameter.Value; } }
56    public ValueParameterCollection MaxPriceScaling { get { return MaxPriceScalingParameter.Value; } }
57    #endregion
58
59    #region Construction & Cloning
60    public Product() {
61      Parameters.Add(new ValueParameter<StringValue>("Label", "The label/name of this product."));
62      Parameters.Add(new ValueParameter<DoubleValue>("Price", "The product's price; can be negative to indicate disposal cost.", new DoubleValue(0)));
63      Parameters.Add(new OptionalValueParameter<DoubleValue>("PriceAtSource", "The product's price at the source/production location."));
64      Parameters.Add(new OptionalValueParameter<DoubleValue>("PriceAtTarget", "The product's price when delivered to the customer."));
65      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)));
66      Parameters.Add(new ValueParameter<DoubleValue>("Percentage", "The percentage of the feedstock utilization where the price starts to increase.", new DoubleValue(0.5)));
67      Parameters.Add(new ValueParameter<DoubleValue>("MaxPrice", "The maximum product's price at 100 percent utilization.", new DoubleValue(0)));
68      Parameters.Add(new ValueParameter<ValueParameterCollection>("PriceScaling", "Regional scaling of base price", new ValueParameterCollection()));
69      Parameters.Add(new ValueParameter<ValueParameterCollection>("MaxPriceScaling", "Regional scaling of max price", new ValueParameterCollection()));
70      RegisterEventHandlers();
71    }
72
73    [StorableConstructor]
74    protected Product(StorableConstructorFlag _) : base(_) { }
75
76    [StorableHook(HookType.AfterDeserialization)]
77    private void AfterDeserialization() {
78      RegisterEventHandlers();
79      if (!Parameters.ContainsKey("PriceScaling"))
80        Parameters.Add(new ValueParameter<ValueParameterCollection>("PriceScaling", "Regional scaling of base price", new ValueParameterCollection()));
81      if (!Parameters.ContainsKey("MaxPriceScaling"))
82        Parameters.Add(new ValueParameter<ValueParameterCollection>("MaxPriceScaling", "Regional scaling of max price", new ValueParameterCollection()));
83    }
84
85    protected Product(Product orig, Cloner cloner)
86      : base(orig, cloner) {
87      RegisterEventHandlers();
88    }
89
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new Product(this, cloner);
92    }
93    #endregion
94
95    public override void CollectParameterValues(System.Collections.Generic.IDictionary<string, IItem> values) {
96      var tmpDict = new Dictionary<string, IItem>();
97      base.CollectParameterValues(tmpDict);
98      foreach (var entry in tmpDict) {
99        if (entry.Key == "Label") continue;
100        values.Add("Product " + Label + "." + entry.Key, entry.Value);
101      }
102    }
103    private void RegisterEventHandlers() {
104      // when the HL value is changed
105      LabelParameter.ValueChanged += (sender, args) => {
106        UpdateName();
107        LabelParameter.Value.ValueChanged += (s, a) => { UpdateName(); };
108      };
109      // when the HL value's value is changed
110      LabelParameter.Value.ValueChanged += (sender, args) => { UpdateName(); };
111    }
112
113    private void UpdateName() {
114      this.Name = ItemName + ": " + Label;
115    }
116
117    public override bool IsEquivalentTo(DataItem other) {
118      var p = other as Product;
119      if (p == null) return false;
120      return p.Label == Label;
121    }
122
123    public double GetRegionalBasePrice(string regionName) {
124      IValueParameter priceParam = null;
125      IValueParameter<DoubleValue> price;
126      while (regionName.Length >= 2) {
127        if (PriceScaling.TryGetValue(regionName, out priceParam)) break;
128        regionName = regionName.Remove(regionName.Length - 1);
129      }
130      price = priceParam as IValueParameter<DoubleValue>;
131      if (price != null) return Price * price.Value.Value;
132      return Price;
133    }
134
135    public double GetRegionalMaxPrice(string regionName) {
136      IValueParameter maxPriceParam = null;
137      IValueParameter<DoubleValue> maxPrice;
138      while (regionName.Length >= 2) {
139        if (MaxPriceScaling.TryGetValue(regionName, out maxPriceParam)) break;
140        regionName = regionName.Remove(regionName.Length - 1);
141      }
142      maxPrice = maxPriceParam as IValueParameter<DoubleValue>;
143      if (maxPrice != null) return MaxPrice * maxPrice.Value.Value;
144      return MaxPrice;
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.